/
runner.go
518 lines (431 loc) · 12.6 KB
/
runner.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package tasks
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/util"
)
const (
taskFailStatus = "error"
taskTypeID = "task"
)
type task struct {
task db.Task
template db.Template
sshKey db.AccessKey
inventory db.Inventory
repository db.Repository
environment db.Environment
users []int
projectID int
hosts []string
alertChat string
alert bool
prepared bool
}
func (t *task) fail() {
t.task.Status = taskFailStatus
t.updateStatus()
t.sendMailAlert()
t.sendTelegramAlert()
}
func (t *task) prepareRun() {
t.prepared = false
defer func() {
fmt.Println("Stopped preparing task")
objType := taskTypeID
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " finished - " + strings.ToUpper(t.task.Status)
if err := (db.Event{
ProjectID: &t.projectID,
ObjectType: &objType,
ObjectID: &t.task.ID,
Description: &desc,
}.Insert()); err != nil {
t.panicOnError(err, "Fatal error inserting an event")
}
}()
t.log("Preparing: " + strconv.Itoa(t.task.ID))
err := checkTmpDir(util.Config.TmpPath)
if err != nil {
t.log("Creating tmp dir failed: " + err.Error())
t.fail()
return
}
if err := t.populateDetails(); err != nil {
t.log("Error: " + err.Error())
t.fail()
return
}
objType := taskTypeID
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " is preparing"
if err := (db.Event{
ProjectID: &t.projectID,
ObjectType: &objType,
ObjectID: &t.task.ID,
Description: &desc,
}.Insert()); err != nil {
t.log("Fatal error inserting an event")
panic(err)
}
t.log("Prepare task with template: " + t.template.Alias + "\n")
if err := t.installKey(t.repository.SSHKey); err != nil {
t.log("Failed installing ssh key for repository access: " + err.Error())
t.fail()
return
}
if err := t.updateRepository(); err != nil {
t.log("Failed updating repository: " + err.Error())
t.fail()
return
}
if err := t.installInventory(); err != nil {
t.log("Failed to install inventory: " + err.Error())
t.fail()
return
}
if err := t.runGalaxy(); err != nil {
t.log("Running galaxy failed: " + err.Error())
t.fail()
return
}
// todo: write environment
if stderr, err := t.listPlaybookHosts(); err != nil {
t.log("Listing playbook hosts failed: " + err.Error() + "\n" + stderr)
t.fail()
return
}
t.prepared = true
}
func (t *task) run() {
defer func() {
fmt.Println("Stopped running tasks")
resourceLocker <- &resourceLock{lock: false, holder: t}
now := time.Now()
t.task.End = &now
t.updateStatus()
objType := taskTypeID
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " finished - " + strings.ToUpper(t.task.Status)
if err := (db.Event{
ProjectID: &t.projectID,
ObjectType: &objType,
ObjectID: &t.task.ID,
Description: &desc,
}.Insert()); err != nil {
t.log("Fatal error inserting an event")
panic(err)
}
}()
{
now := time.Now()
t.task.Status = "running"
t.task.Start = &now
t.updateStatus()
}
objType := taskTypeID
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " is running"
if err := (db.Event{
ProjectID: &t.projectID,
ObjectType: &objType,
ObjectID: &t.task.ID,
Description: &desc,
}.Insert()); err != nil {
t.log("Fatal error inserting an event")
panic(err)
}
t.log("Started: " + strconv.Itoa(t.task.ID))
t.log("Run task with template: " + t.template.Alias + "\n")
if err := t.runPlaybook(); err != nil {
t.log("Running playbook failed: " + err.Error())
t.fail()
return
}
t.task.Status = "success"
t.updateStatus()
}
func (t *task) fetch(errMsg string, ptr interface{}, query string, args ...interface{}) error {
err := db.Mysql.SelectOne(ptr, query, args...)
if err == sql.ErrNoRows {
t.log(errMsg)
return err
}
if err != nil {
t.fail()
panic(err)
}
return nil
}
//nolint: gocyclo
func (t *task) populateDetails() error {
// get template
if err := t.fetch("Template not found!", &t.template, "select * from project__template where id=?", t.task.TemplateID); err != nil {
return err
}
var project db.Project
// get project alert setting
if err := t.fetch("Alert setting not found!", &project, "select alert, alert_chat from project where id=?", t.template.ProjectID); err != nil {
return err
}
t.alert = project.Alert
t.alertChat = project.AlertChat
// get project users
var users []struct {
ID int `db:"id"`
}
if _, err := db.Mysql.Select(&users, "select user_id as id from project__user where project_id=?", t.template.ProjectID); err != nil {
return err
}
t.users = []int{}
for _, user := range users {
t.users = append(t.users, user.ID)
}
// get access key
if err := t.fetch("Template Access Key not found!", &t.sshKey, "select * from access_key where id=?", t.template.SSHKeyID); err != nil {
return err
}
if t.sshKey.Type != "ssh" {
t.log("Non ssh-type keys are currently not supported: " + t.sshKey.Type)
return errors.New("Unsupported SSH Key")
}
// get inventory
if err := t.fetch("Template Inventory not found!", &t.inventory, "select * from project__inventory where id=?", t.template.InventoryID); err != nil {
return err
}
// get inventory services key
if t.inventory.KeyID != nil {
if err := t.fetch("Inventory AccessKey not found!", &t.inventory.Key, "select * from access_key where id=?", *t.inventory.KeyID); err != nil {
return err
}
}
// get inventory ssh key
if t.inventory.SSHKeyID != nil {
if err := t.fetch("Inventory Ssh Key not found!", &t.inventory.SSHKey, "select * from access_key where id=?", *t.inventory.SSHKeyID); err != nil {
return err
}
}
// get repository
if err := t.fetch("Repository not found!", &t.repository, "select * from project__repository where id=?", t.template.RepositoryID); err != nil {
return err
}
// get repository access key
if err := t.fetch("Repository Access Key not found!", &t.repository.SSHKey, "select * from access_key where id=?", t.repository.SSHKeyID); err != nil {
return err
}
if t.repository.SSHKey.Type != "ssh" {
t.log("Repository Access Key is not 'SSH': " + t.repository.SSHKey.Type)
return errors.New("Unsupported SSH Key")
}
// get environment
if len(t.task.Environment) == 0 && t.template.EnvironmentID != nil {
err := t.fetch("Environment not found", &t.environment, "select * from project__environment where id=?", *t.template.EnvironmentID)
if err != nil {
return err
}
} else if len(t.task.Environment) > 0 {
t.environment.JSON = t.task.Environment
}
return nil
}
func (t *task) installKey(key db.AccessKey) error {
t.log("access key " + key.Name + " installed")
path := key.GetPath()
if key.Key != nil {
if err := ioutil.WriteFile(path+"-cert.pub", []byte(*key.Key), 0600); err != nil {
return err
}
}
return ioutil.WriteFile(path, []byte(*key.Secret), 0600)
}
func (t *task) updateRepository() error {
repoName := "repository_" + strconv.Itoa(t.repository.ID)
_, err := os.Stat(util.Config.TmpPath + "/" + repoName)
cmd := exec.Command("git")//nolint: gas
cmd.Dir = util.Config.TmpPath
gitSSHCommand := "ssh -o StrictHostKeyChecking=no -i " + t.repository.SSHKey.GetPath()
cmd.Env = t.envVars(util.Config.TmpPath, util.Config.TmpPath, &gitSSHCommand)
repoURL, repoTag := t.repository.GitURL, "master"
if split := strings.Split(repoURL, "#"); len(split) > 1 {
repoURL, repoTag = split[0], split[1]
}
if err != nil && os.IsNotExist(err) {
t.log("Cloning repository " + repoURL)
cmd.Args = append(cmd.Args, "clone", "--recursive", "--branch", repoTag, repoURL, repoName)
} else if err != nil {
return err
} else {
t.log("Updating repository " + repoURL)
cmd.Dir += "/" + repoName
cmd.Args = append(cmd.Args, "pull", "origin", repoTag)
}
t.logCmd(cmd)
return cmd.Run()
}
func (t *task) runGalaxy() error {
args := []string{
"install",
"-r",
"roles/requirements.yml",
"-p",
"./roles/",
"--force",
}
cmd := exec.Command("ansible-galaxy", args...)//nolint: gas
cmd.Dir = util.Config.TmpPath + "/repository_" + strconv.Itoa(t.repository.ID)
gitSSHCommand := "ssh -o StrictHostKeyChecking=no -i " + t.repository.SSHKey.GetPath()
cmd.Env = t.envVars(util.Config.TmpPath, cmd.Dir, &gitSSHCommand)
if _, err := os.Stat(cmd.Dir + "/roles/requirements.yml"); err != nil {
return nil
}
t.logCmd(cmd)
return cmd.Run()
}
func (t *task) listPlaybookHosts() (string, error) {
args, err := t.getPlaybookArgs()
if err != nil {
return "", err
}
args = append(args, "--list-hosts")
cmd := exec.Command("ansible-playbook", args...)//nolint: gas
cmd.Dir = util.Config.TmpPath + "/repository_" + strconv.Itoa(t.repository.ID)
cmd.Env = t.envVars(util.Config.TmpPath, cmd.Dir, nil)
var errb bytes.Buffer
cmd.Stderr = &errb
out, err := cmd.Output()
re := regexp.MustCompile(`(?m)^\\s{6}(.*)$`)
matches := re.FindAllSubmatch(out, 20)
hosts := make([]string, len(matches))
for i := range matches {
hosts[i] = string(matches[i][1])
}
t.hosts = hosts
return errb.String(), err
}
func (t *task) runPlaybook() error {
args, err := t.getPlaybookArgs()
if err != nil {
return err
}
cmd := exec.Command("ansible-playbook", args...)//nolint: gas
cmd.Dir = util.Config.TmpPath + "/repository_" + strconv.Itoa(t.repository.ID)
cmd.Env = t.envVars(util.Config.TmpPath, cmd.Dir, nil)
t.logCmd(cmd)
cmd.Stdin = strings.NewReader("")
return cmd.Run()
}
//nolint: gocyclo
func (t *task) getPlaybookArgs() ([]string, error) {
playbookName := t.task.Playbook
if len(playbookName) == 0 {
playbookName = t.template.Playbook
}
var inventory string
switch t.inventory.Type {
case "file":
inventory = t.inventory.Inventory
default:
inventory = util.Config.TmpPath + "/inventory_" + strconv.Itoa(t.task.ID)
}
args := []string{
"-i", inventory,
}
if t.inventory.SSHKeyID != nil {
args = append(args, "--private-key="+t.inventory.SSHKey.GetPath())
}
if t.task.Debug {
args = append(args, "-vvvv")
}
if t.task.DryRun {
args = append(args, "--check")
}
if len(t.environment.JSON) > 0 {
var js map[string]interface{}
err := json.Unmarshal([]byte(t.environment.JSON), &js)
if err != nil {
t.log("JSON is not valid")
return nil, err
}
extraVar, err := removeCommandEnvironment(t.environment.JSON, js)
if err != nil {
t.log("Could not remove command environment, if existant it will be passed to --extra-vars. This is not fatal but be aware of side effects")
}
args = append(args, "--extra-vars", extraVar)
}
var extraArgs []string
if t.template.Arguments != nil {
err := json.Unmarshal([]byte(*t.template.Arguments), &extraArgs)
if err != nil {
t.log("Could not unmarshal arguments to []string")
return nil, err
}
}
if t.template.OverrideArguments {
args = extraArgs
} else {
args = append(args, extraArgs...)
args = append(args, playbookName)
}
return args, nil
}
func (t *task) envVars(home string, pwd string, gitSSHCommand *string) []string {
env := os.Environ()
env = append(env, fmt.Sprintf("HOME=%s", home))
env = append(env, fmt.Sprintf("PWD=%s", pwd))
env = append(env, fmt.Sprintln("PYTHONUNBUFFERED=1"))
//env = append(env, fmt.Sprintln("GIT_FLUSH=1"))
env = append(env, extractCommandEnvironment(t.environment.JSON)...)
if gitSSHCommand != nil {
env = append(env, fmt.Sprintf("GIT_SSH_COMMAND=%s", *gitSSHCommand))
}
return env
}
// extractCommandEnvironment unmarshalls a json string, extracts the ENV key from it and returns it as
// []string where strings are in key=value format
func extractCommandEnvironment(envJSON string) []string {
env := make([]string, 0)
var js map[string]interface{}
err := json.Unmarshal([]byte(envJSON), &js)
if err == nil {
if cfg, ok := js["ENV"]; ok {
switch v := cfg.(type) {
case map[string]interface{}:
for key, val := range v {
env = append(env, fmt.Sprintf("%s=%s", key, val))
}
}
}
}
return env
}
// removeCommandEnvironment removes the ENV key from task environments and returns the resultant json encoded string
// which can be passed as the --extra-vars flag values
func removeCommandEnvironment(envJSON string, envJs map[string]interface{}) (string, error) {
if _, ok := envJs["ENV"]; ok {
delete(envJs, "ENV")
ev, err := json.Marshal(envJs)
if err != nil {
return envJSON, err
}
envJSON = string(ev)
}
return envJSON, nil
}
// checkTmpDir checks to see if the temporary directory exists
// and if it does not attempts to create it
func checkTmpDir(path string) error {
var err error
if _, err = os.Stat(path); err != nil {
if os.IsNotExist(err) {
return os.MkdirAll(path, 0700)
}
}
return err
}