-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcontrol.go
More file actions
122 lines (98 loc) · 2.31 KB
/
control.go
File metadata and controls
122 lines (98 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
/*
Control
This is the part of Ground Control that handles invoking
shell commands and exposing the UI.
The invoking part needs commands to be present in the configuration
like so:
```
"controls" : {
"xbmc": {
"on" : "/etc/init.d/xbmc start",
"off" : "/etc/init.d/xbmc stop"
}
}
```
While controls are accessed RESTfully like so:
```
POST controls/control_name/on
POST controls/control_name/off
POST controls/control_name/once
GET controls/control_name/status
```
*/
import (
"encoding/json"
"errors"
"log"
"net/http"
"os/exec"
"strings"
)
type Control struct {
Mount string
controls map[string]interface{}
}
type ActionResult struct {
Output string `json:"output"`
}
func NewControl(controls map[string]interface{}) (h *Control) {
c := &Control{Mount: "/controls/", controls: controls}
return c
}
func (self *Control) Handler(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, self.Mount)
hdr := w.Header()
hdr.Add("Access-Control-Allow-Origin", "*")
switch {
case r.Method == "GET" && path == "all":
enc := json.NewEncoder(w)
enc.Encode(&self.controls)
case r.Method == "POST":
tuple := strings.Split(path, "/")
if len(tuple) != 2 {
notFound(w)
return
}
control := tuple[0]
action := tuple[1]
cmd, err := multimap(self.controls, control, action)
if err != nil {
notFound(w)
return
}
log.Println("Running", cmd)
// Some commands just hang on Output(), leave it out for now
// lets go with "ok" for every successful command.
err = exec.Command("sh", "-c", cmd).Start()
out := "ok"
log.Println("Done")
if err != nil {
// minor error?
w.WriteHeader(400)
actionResponse(w, "error.")
log.Println(err)
}
actionResponse(w, string(out))
default:
notFound(w)
}
}
func notFound(w http.ResponseWriter) {
w.WriteHeader(404)
actionResponse(w, "not found")
}
func actionResponse(w http.ResponseWriter, out string) {
enc := json.NewEncoder(w)
enc.Encode(&ActionResult{Output: string(out)})
}
func multimap(mmap map[string]interface{}, control string, action string) (res string, err error) {
l1 := mmap[control]
if l1 != nil {
l2 := l1.(map[string]interface{})[action]
if l2 != nil {
return l2.(string), nil
}
}
return "", errors.New("No such control/action")
}