/
main.go
338 lines (283 loc) · 7.8 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
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ovh/cds/engine/api/worker"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
)
var (
// WorkerID is a unique identifier for this worker
WorkerID string
// key is the token generated by the user owning the worker
key string
name string
api string
model int64
hatchery int64
basedir string
logChan chan sdk.Log
// port of variable exporter HTTP server
exportport int
// current actionBuild is here to allow var export
ab sdk.ActionBuild
buildVariables []sdk.Variable
// Git ssh configuration
pkey string
gitssh string
startTimestamp *time.Time
nbActionsDone int
status struct {
Name string `json:"name"`
Heartbeat time.Time `json:"heartbeat"`
Status string `json:"status"`
Model int64 `json:"model"`
}
)
var mainCmd = &cobra.Command{
Use: "worker",
Short: "CDS Worker",
Run: func(cmd *cobra.Command, args []string) {
viper.SetEnvPrefix("cds")
viper.AutomaticEnv()
log.Initialize()
log.Notice("What a good time to be alive\n")
var err error
name, err = os.Hostname()
if err != nil {
log.Notice("Cannot retrieve hostname: %s\n", err)
return
}
hatchS := viper.GetString("hatchery")
hatchery, err = strconv.ParseInt(hatchS, 10, 64)
if err != nil {
fmt.Printf("WARNING: Invalid hatchery ID (%s)\n", err)
}
api = viper.GetString("api")
if api == "" {
fmt.Printf("--api not provided, aborting.\n")
return
}
key = viper.GetString("key")
if key == "" {
fmt.Printf("--key not provided, aborting.\n")
return
}
givenName := viper.GetString("name")
if givenName != "" {
name = givenName
}
status.Name = name
basedir = viper.GetString("basedir")
if basedir == "" {
basedir = os.TempDir()
}
model = int64(viper.GetInt("model"))
status.Model = model
port, err := server()
if err != nil {
sdk.Exit("cannot bind port for worker export: %s\n", err)
}
exportport = port
now := time.Now()
startTimestamp = &now
// start logger routine
logChan = make(chan sdk.Log)
go logger(logChan)
go heartbeat()
queuePolling()
},
}
func init() {
flags := mainCmd.Flags()
flags.String("log-level", "notice", "Log Level : debug, info, notice, warning, critical")
viper.BindPFlag("log_level", flags.Lookup("log-level"))
flags.String("api", "", "URL of CDS API")
viper.BindPFlag("api", flags.Lookup("api"))
flags.String("key", "", "CDS KEY")
viper.BindPFlag("key", flags.Lookup("key"))
flags.Bool("single-use", false, "Exit after executing an action")
viper.BindPFlag("single_use", flags.Lookup("single-use"))
flags.String("name", "", "Name of worker")
viper.BindPFlag("name", flags.Lookup("name"))
flags.Int("model", 0, "Model of worker")
viper.BindPFlag("model", flags.Lookup("model"))
flags.Int("hatchery", 0, "Hatchery spawing worker")
viper.BindPFlag("hatchery", flags.Lookup("hatchery"))
flags.String("basedir", "", "Worker working directory")
viper.BindPFlag("basedir", flags.Lookup("basedir"))
flags.Int("ttl", 30, "Worker time to live (minutes)")
viper.BindPFlag("ttl", flags.Lookup("ttl"))
mainCmd.AddCommand(cmdExport)
mainCmd.AddCommand(cmdUpload)
}
func main() {
sdk.SetAgent(sdk.WorkerAgent)
mainCmd.Execute()
}
// Will be removed when websocket conn is implemented
// for now, poll the /queue
func queuePolling() {
for {
if WorkerID == "" {
log.Notice("[WORKER] Disconnected from CDS engine, trying to register...\n")
if err := register(api, name, key); err != nil {
log.Notice("Cannot register: %s\n", err)
time.Sleep(10 * time.Second)
continue
}
}
//We we've done nothing until ttl is over, let's exit
if nbActionsDone == 0 && startTimestamp.Add(time.Duration(viper.GetInt("ttl"))*time.Minute).Before(time.Now()) {
log.Notice("Time to exit.")
unregister()
os.Exit(0)
}
checkQueue()
time.Sleep(5 * time.Second)
}
}
func checkQueue() {
//Set the status to checking to avoid beeing killed while checking queue, actions and requirements
sdk.SetWorkerStatus(sdk.StatusChecking)
defer sdk.SetWorkerStatus(sdk.StatusWaiting)
queue, err := sdk.GetBuildQueue()
if err != nil {
log.Notice("checkQueue> Cannot get build queue: %s\n", err)
time.Sleep(5 * time.Second)
WorkerID = ""
return
}
log.Notice("checkQueue> %d Actions in queue", len(queue))
for i := range queue {
requirementsOK := true
// Check requirement
log.Notice("checkQueue> Checking requirements for action [%d] %s", queue[i].ID, queue[i].ActionName)
for _, r := range queue[i].Requirements {
ok, err := checkRequirement(r)
if err != nil {
postCheckRequirementError(&r, err)
requirementsOK = false
continue
}
if !ok {
requirementsOK = false
continue
}
}
if requirementsOK {
log.Notice("checkQueue> Taking action %s", queue[i].ID)
takeAction(queue[i])
}
}
log.Notice("checkQueue> Nothing to do...")
}
func postCheckRequirementError(r *sdk.Requirement, err error) {
s := fmt.Sprintf("Error checking requirement Name=%s Type=%s Value=%s :%s", r.Name, r.Type, r.Value, err)
btes := []byte(s)
sdk.Request("POST", "/queue/requirements/errors", btes)
}
func takeAction(b sdk.ActionBuild) {
nbActionsDone++
gitssh = ""
pkey = ""
path := fmt.Sprintf("/queue/%d/take", b.ID)
data, code, err := sdk.Request("POST", path, nil)
if err != nil {
log.Notice("takeAction> Cannot take action %d:%s\n", b.PipelineActionID, err)
return
}
if code != http.StatusOK {
return
}
abi := worker.ActionBuildInfo{}
err = json.Unmarshal([]byte(data), &abi)
if err != nil {
log.Notice("takeAction> Cannot unmarshal action: %s\n", err)
return
}
// Reset build variables
ab = abi.ActionBuild
buildVariables = nil
res := run(abi.Action, abi.ActionBuild, abi.Secrets)
// Give time to buffered logs to be sent
time.Sleep(3 * time.Second)
path = fmt.Sprintf("/queue/%d/result", b.ID)
body, err := json.MarshalIndent(res, " ", " ")
if err != nil {
log.Notice("takeAction>Cannot marshal result: %s\n", err)
return
}
code = 300
var isThereAnyHopeLeft = 50
for code >= 300 {
_, code, err = sdk.Request("POST", path, body)
if err == nil && code == http.StatusNotFound {
unregister() // well...
log.Notice("takeAction> Cannot send build result: ActionBuild does not exists anymore\n")
break
}
if err == nil && code < 300 {
sendLog(b.ID, "SYSTEM", "BuildResult sent.\n")
fmt.Printf("BuildResult sent.\n")
break
}
if err != nil {
log.Notice("takeAction> Cannot send build result: %s\n", err)
} else {
log.Notice("takeAction> Cannot send build result: HTTP %d\n", code)
}
time.Sleep(5 * time.Second)
isThereAnyHopeLeft--
if isThereAnyHopeLeft < 0 {
log.Notice("takeAction> Could not send built result 50 times, giving up\n")
break
}
}
if viper.GetBool("single_use") {
// Give time to logs to be flushed
time.Sleep(2 * time.Second)
// Unregister from engine
err := unregister()
if err != nil {
log.Warning("takeAction> could not unregister: %s\n", err)
}
// then exit
log.Notice("takeAction> --single_use is on, exiting\n")
os.Exit(0)
}
}
func heartbeat() {
for {
time.Sleep(10 * time.Second)
if WorkerID == "" {
log.Notice("[WORKER] Disconnected from CDS engine, trying to register...\n")
if err := register(api, name, key); err != nil {
log.Notice("Cannot register: %s\n", err)
continue
}
}
_, code, err := sdk.Request("POST", "/worker/refresh", nil)
if err != nil || code >= 300 {
log.Notice("heartbeat> cannot refresh beat: %d %s\n", code, err)
WorkerID = ""
}
}
}
func unregister() error {
uri := "/worker/unregister"
_, code, err := sdk.Request("POST", uri, nil)
if err != nil {
return err
}
if code > 300 {
return fmt.Errorf("HTTP %d", code)
}
return nil
}