-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgodeploy.go
343 lines (298 loc) · 8.15 KB
/
godeploy.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
// execute commands only from this directory
var CmdDir = flag.String("dir", "./cmds", "Commands dir")
var ExcludePat = flag.String("exclude", "",
"Exclude shell file name pattern. Ex: *.pyc,a.out")
// this lock is used to not allow 2 commands to run at once
var commandLock *sync.Mutex
// return a list of available jobs that can be run
func jobEntries() ([]string, error) {
var entries []string
dir, err := os.Open(*CmdDir)
if err != nil {
return entries, err
}
defer dir.Close()
dirInfoSlice, _ := dir.Readdir(-1)
for _, fileinfo := range dirInfoSlice {
fileName := fileinfo.Name()
excludes := strings.Split(*ExcludePat, ",")
matched := false
for _, exclude := range excludes {
matched, _ = filepath.Match(exclude, fileName)
if matched {
break
}
}
if !matched {
entries = append(entries, fileName)
}
}
return entries, nil
}
// validates the command to be run and returns it's execution path
func validateCmd(cmd string) (string, error) {
jobs, err := jobEntries()
if err != nil {
return "", err
}
for _, job := range jobs {
if cmd == job {
return filepath.Join(*CmdDir, cmd), nil
}
}
return "", errors.New("Command not found")
}
// this is a combined output channel used for both stdout and stderr pipes
type combinedOutput struct {
data []byte
exit bool // use this to signal that we ended reading from the pipe
}
// read from pipe (stdout/stderr) and send back using channel
func readPipe(pipe io.ReadCloser, pipeChan chan combinedOutput) {
buf := make([]byte, 1024)
_, err := pipe.Read(buf)
var out combinedOutput
for err == nil {
// send the output of the command to the channel
out.data = buf
out.exit = false
pipeChan <- out
// read some more
_, err = pipe.Read(buf)
}
out.data = nil
out.exit = true
pipeChan <- out
}
/* Run the command and send back the results on channels */
func runCommand(command string, outChan chan string, errChan chan error) {
// no matter what happens, close the channel
defer close(errChan)
cmdPath, err := validateCmd(command)
if err != nil {
errChan <- err
log.Print("Invalid command: ", err)
return
}
cmd := exec.Command(cmdPath)
stdout, err := cmd.StdoutPipe()
if err != nil {
errChan <- err
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
errChan <- err
return
}
// run the command, but don't block
if err := cmd.Start(); err != nil {
errChan <- err
return
}
// combine the out from both pipes
comChan := make(chan combinedOutput)
go readPipe(stdout, comChan)
go readPipe(stderr, comChan)
count := 0
for out := range comChan {
if out.exit == true {
if count == 1 {
//close(comChan)
break
}
count++
}
outChan <- string(out.data)
}
// nothing more to send.. we can close the channel here
close(outChan)
// report any errors including the exit code of the command
err = cmd.Wait()
errChan <- err
}
/* /run/ - this handler will send the output of a running command */
func runHandler(response http.ResponseWriter, request *http.Request) {
// forcing the output content type
header := response.Header()
// for some reason if text/plain is passed
// Chrome thinks it's a application/octet-stream and tries
// to download the log.
header["Content-Type"] = []string{"text/html; charset=UTF-8"}
header["Connection"] = []string{"close"}
header["Vary"] = []string{"User-Agent"}
// aquiring the lock. Should block here
commandLock.Lock()
defer commandLock.Unlock()
jobName := request.URL.Path[len("/run/"):]
// TODO: fix this and use the HTTP headers with user authentication
userName := "Anonymous"
// create a new log entry
logEntry := JobLogEntry{
Name: jobName,
User: userName,
Start: time.Now().UTC(),
}
// Adding a <pre> here because we want pretty output
// the browser. We close it at end when we finished reading
// from the command's output
fmt.Fprintf(response, "<pre>")
response.(http.Flusher).Flush()
logId := NewLogEntry(logEntry)
log.Printf("Started job: %s: %s", logId, jobName)
// We'll use two channels. The error and the command output channel
// the error channel is used to get the errors thrown while running
// the job and the output channel is for returning the output of
// the command
outChan := make(chan string)
errChan := make(chan error)
go runCommand(jobName, outChan, errChan)
// TODO: perhaps implement some kind of timeout?
for {
select {
case content, closed := <-outChan:
AppendLog(logId, content)
fmt.Fprintf(response, content)
if !closed {
UpdateLog(logId, time.Now().UTC(), "0")
fmt.Fprintf(response, "</pre>") // close <pre>
log.Print("Finished job: " + jobName)
return
}
response.(http.Flusher).Flush()
case err, _ := <-errChan:
errStr := ""
if err != nil {
errStr = err.Error()
// TODO: maybe there is another way to get the
// exit status?
if strings.Contains(errStr, "exit status") {
// Ex: exit status 0
msgparts := strings.Split(err.Error(), " ")
// Ex: we get "0" here
status := msgparts[2]
// Rename the log file to store
// the exit status
UpdateLog(logId, time.Now().UTC(), status)
log.Print("Finished job: " + jobName)
return
}
}
AppendLog(logId, errStr)
fmt.Fprintf(response, errStr)
response.(http.Flusher).Flush()
}
}
}
// This will be useful for some pagination
type ResponseLogs struct {
Entries JobLogEntries
Length int
}
/* /logs will return the latest logs ordered by date from the logs folder */
func logsHandler(w http.ResponseWriter, r *http.Request) {
// forcing the output content type
header := w.Header()
header["Content-Type"] = []string{"application/json"}
// if we have a name of the log then we should get the contents of the log
var dataJson []byte
var err error
if r.FormValue("id") != "" {
var body string
data := make(map[string]string, 1)
body, err = LogEntryBody(r.FormValue("id"))
data["body"] = string(body)
dataJson, err = json.Marshal(data)
if err != nil {
log.Print("Failed to encode json: ", err)
}
} else {
start := 0
offset := 50 // number of items per page
job := r.FormValue("job") // filter by job name
logEntries := LogEntries(job)
logEntriesLen := len(logEntries)
if offset >= logEntriesLen {
offset = logEntriesLen
}
resp := ResponseLogs{
Entries: logEntries[start:offset],
Length: logEntriesLen,
}
page, err := strconv.ParseInt(r.FormValue("page"), 10, 32)
if err == nil {
start = int(page) * offset
if start >= resp.Length {
start = resp.Length - offset
}
offset += start
if offset >= resp.Length {
offset = resp.Length
}
resp.Entries = resp.Entries[start:offset]
}
dataJson, err = json.Marshal(resp)
if err != nil {
log.Print("Failed to encode json: ", err)
}
}
fmt.Fprintf(w, string(dataJson))
}
/* /listJobs will display the available jobs that can be run */
func jobsHandler(w http.ResponseWriter, r *http.Request) {
// forcing the output content type
header := w.Header()
header["Content-Type"] = []string{"application/json"}
entries, err := jobEntries()
if err != nil {
log.Print("Error loading available jobs: ", err)
}
dataJson, err := json.Marshal(entries)
if err != nil {
log.Print("Failed to encode json")
}
fmt.Fprintf(w, string(dataJson))
}
/* This is a helper wrapper. Allows us to log some stuff */
func DefaultWrapper(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func main() {
flag.Parse()
// create the lock
commandLock = new(sync.Mutex)
http.HandleFunc("/run/", runHandler)
http.HandleFunc("/logs", logsHandler)
http.HandleFunc("/jobs", jobsHandler)
// serve other static stuff
http.Handle("/", http.StripPrefix("/",
http.FileServer(http.Dir("./static"))))
// command running handler
// server index.html at the end
port := ":8000"
log.Printf("Starting on " + port)
err := http.ListenAndServe(port, DefaultWrapper(http.DefaultServeMux))
if err != nil {
log.Fatal(err)
}
}