-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
290 lines (237 loc) · 6.34 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
type jobConfig struct {
ID string `json:"id"`
Command string `json:"command"`
Input string `json:"input"`
Timeout int `json:"timeout"`
}
type runResult struct {
Success bool
Output string
Logs string
}
type jobNotify struct {
RunID string `json:"run_id"`
Success bool `json:"success"`
Output string `json:"output"`
Logs string `json:"logs"`
}
func getCommandsList() string {
listJob := jobConfig{
ID: "list",
Command: "list",
Input: "{}",
}
res := execJob(listJob)
if res.Success == false {
log.Fatal("Could not fetch commands list")
}
return res.Output
}
// Poll the API for a job to run
func poll(commands string) (*jobConfig, error) {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
log.Println("Polling from", hostname)
client := &http.Client{
Timeout: time.Second * 10,
}
payload := fmt.Sprintf("{\"commands\": %s}", commands)
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", os.Getenv("ZETTO_HOST"), "pop"), bytes.NewBufferString(payload))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", fmt.Sprintf("ApiKey %s", os.Getenv("ZETTO_API_KEY")))
req.Header.Add("X-Runner-Name", hostname)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 404 {
// No error, just not found
return nil, nil
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, fmt.Errorf("Polling error %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
job := jobConfig{}
err = decoder.Decode(&job)
if err != nil {
log.Fatal(err)
}
return &job, nil
}
// Execute a job and returns the runs result
func execJob(job jobConfig) runResult {
// Prepare command : $RUNNER <command> <input>"
runner := strings.Split(os.Getenv("ZETTO_RUNNER"), " ")
runner = append(runner, job.Command)
runner = append(runner, job.Input)
cmd := exec.Command(runner[0], runner[1:]...)
// Collect stdout and stderr into local buffers for after the execution
outBuf := new(bytes.Buffer)
logBuf := new(bytes.Buffer)
cmd.Stdout = outBuf
cmd.Stderr = logBuf
// Start the command
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
// Create a channel for it to notify its completion (with its exit code)
done := make(chan int)
// Asynchronous goroutine
go func() {
// Wait for the command to finish
err := cmd.Wait()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
// Standard exit error : notify the status through the channel
done <- exitError.ExitCode()
} else {
// Something wrong happened, let's crash
log.Fatalf("cmd.Wait: %v", err)
}
} else {
// Finished without an error : notify the status zero through the channel
done <- 0
}
// Close the channel, we won't need it anymore
close(done)
}()
// Setup a timer after which the command should be killed
timeoutDuration := job.Timeout
if timeoutDuration == 0 {
timeoutDuration = 15
}
timeout := time.NewTimer(time.Duration(timeoutDuration) * time.Second)
// Prepare a variable into which the exist code will be stored
var exitCode int
// Wait simultaneously for an execution end, or the timeout completion
select {
case exitCode = <-done:
// Execution ended, stop the timeout
if !timeout.Stop() {
<-timeout.C
}
case <-timeout.C:
// Timeout triggered, kill the process, and return an exit code of 143
log.Println("Execution timeout, killing process")
if err := cmd.Process.Kill(); err != nil {
log.Fatal("failed to kill process: ", err)
}
// Wait for the done channel, which should be triggered after the kill. Apparently this emits a -1 exit code
exitCode = <-done
}
// Fetch the command logs through STDERR
logStr := logBuf.String()
// Return a failed run if the exit code is not zero
if exitCode != 0 {
log.Println("EXIT CODE", exitCode)
return runResult{
Success: false,
Output: "null",
Logs: logStr,
}
}
// Successful run : fetch the output through STDOUT, and return a successful run
outStr := outBuf.String()
return runResult{
Success: true,
Output: outStr,
Logs: logStr,
}
}
// Notify the API of a run's result
func notify(job jobConfig, result runResult) error {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Timeout: time.Second * 10,
}
notifyPayload := jobNotify{
RunID: job.ID,
Success: result.Success,
Output: result.Output,
Logs: result.Logs,
}
payload, err := json.Marshal(notifyPayload)
if err != nil {
log.Fatal(err)
}
log.Printf("Sending payload %s\n", payload)
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", os.Getenv("ZETTO_HOST"), "notify"), bytes.NewBuffer(payload))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", fmt.Sprintf("ApiKey %s", os.Getenv("ZETTO_API_KEY")))
req.Header.Add("X-Runner-Name", hostname)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return err
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return fmt.Errorf("Notify error %d", res.StatusCode)
}
return nil
}
func main() {
log.Print("Started")
// Check availability of configuration
if os.Getenv("ZETTO_HOST") == "" {
log.Fatal("Missing ZETTO_HOST environment")
}
if os.Getenv("ZETTO_API_KEY") == "" {
log.Fatal("Missing ZETTO_API_KEY environment")
}
if os.Getenv("ZETTO_RUNNER") == "" {
log.Fatal("Missing ZETTO_RUNNER environment")
}
pollingInterval, err := strconv.Atoi(os.Getenv("ZETTO_POLLING_INTERVAL"))
if err != nil {
log.Println("Could not parse env ZETTO_POLLING_INTERVAL, defaulting to 10 seconds")
pollingInterval = 10
}
// TODO : fetch available jobs in order to send them with hre polling request
commands := getCommandsList()
// Infinite loop
for {
jobconfig, err := poll(commands)
if err != nil {
log.Println("Error fetching a job :", err)
os.Exit(1)
}
if jobconfig == nil {
log.Println("No job found, waiting")
// Todo : sleep here
time.Sleep(time.Duration(pollingInterval) * time.Second)
continue
}
runresult := execJob(*jobconfig)
err = notify(*jobconfig, runresult)
if err != nil {
log.Println("Error notifying job result :", err)
os.Exit(1)
}
}
}