/
installstep.go
executable file
·664 lines (587 loc) · 18.3 KB
/
installstep.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
* Copyright 2018-2022, CS Systemes d'Information, http://csgroup.eu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package operations
import (
"bytes"
"context"
"fmt"
"os"
"reflect"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"github.com/CS-SI/SafeScale/v21/lib/server/resources"
"github.com/CS-SI/SafeScale/v21/lib/server/resources/enums/hostproperty"
"github.com/CS-SI/SafeScale/v21/lib/server/resources/enums/installaction"
propertiesv1 "github.com/CS-SI/SafeScale/v21/lib/server/resources/properties/v1"
"github.com/CS-SI/SafeScale/v21/lib/utils"
"github.com/CS-SI/SafeScale/v21/lib/utils/cli/enums/outputs"
"github.com/CS-SI/SafeScale/v21/lib/utils/concurrency"
"github.com/CS-SI/SafeScale/v21/lib/utils/data"
"github.com/CS-SI/SafeScale/v21/lib/utils/data/serialize"
"github.com/CS-SI/SafeScale/v21/lib/utils/debug"
"github.com/CS-SI/SafeScale/v21/lib/utils/fail"
"github.com/CS-SI/SafeScale/v21/lib/utils/template"
"github.com/CS-SI/SafeScale/v21/lib/utils/temporal"
)
const (
targetHosts = "hosts"
targetMasters = "masters"
targetNodes = "nodes"
targetGateways = "gateways"
)
type stepResult struct {
completed bool // if true, the script has been run to completion
retcode int
output string
success bool // if true, the script has finished, and the result is a success
err error // if an error occurred, 'err' contains it
}
// Successful returns true if the script has finished AND its results is a success
func (sr stepResult) Successful() bool {
return sr.success
}
// Completed returns true if the script has finished, false otherwise
func (sr stepResult) Completed() bool {
return sr.completed
}
func (sr stepResult) Error() error {
return sr.err
}
func (sr stepResult) ErrorMessage() string {
var msg string
if sr.err != nil {
msg = sr.err.Error()
}
if msg == "" && sr.retcode != 0 {
recoveredErr := ""
if sr.output != "" {
lastMsg := ""
lines := strings.Split(sr.output, "\n")
for _, line := range lines {
if strings.Contains(line, "+ echo '") {
lastMsg = line
}
}
if len(lastMsg) > 0 {
recoveredErr = lastMsg[8 : len(lastMsg)-1]
}
}
if len(recoveredErr) > 0 {
msg = fmt.Sprintf("exited with error code %d: %s", sr.retcode, recoveredErr)
} else {
msg = fmt.Sprintf("exited with error code %d", sr.retcode)
}
}
return msg
}
type stepTargets map[string]string
// parse converts the content of specification file loaded inside struct to
// standardized values (0, 1 or *)
func (st stepTargets) parse() (string, string, string, string, fail.Error) {
var (
hostT, masterT, nodeT, gwT string
ok bool
)
if hostT, ok = st[targetHosts]; ok {
switch strings.ToLower(hostT) {
case "":
fallthrough
case "false":
fallthrough
case "no":
fallthrough
case "none":
fallthrough
case "0":
hostT = "0"
case "yes":
fallthrough
case "true":
fallthrough
case "1":
hostT = "1"
default:
return "", "", "", "", fail.SyntaxError("invalid value '%s' for target '%s'", hostT, targetHosts)
}
}
if masterT, ok = st[targetMasters]; ok {
switch strings.ToLower(masterT) {
case "":
fallthrough
case "false":
fallthrough
case "no":
fallthrough
case "none":
fallthrough
case "0":
masterT = "0"
case "any":
fallthrough
case "one":
fallthrough
case "1":
masterT = "1"
case "all":
fallthrough
case "*":
masterT = "*"
default:
return "", "", "", "", fail.SyntaxError("invalid value '%s' for target '%s'", masterT, targetMasters)
}
}
if nodeT, ok = st[targetNodes]; ok {
switch strings.ToLower(nodeT) {
case "":
fallthrough
case "false":
fallthrough
case "no":
fallthrough
case "none":
nodeT = "0"
case "any":
fallthrough
case "one":
fallthrough
case "1":
nodeT = "1"
case "all":
fallthrough
case "*":
nodeT = "*"
default:
return "", "", "", "", fail.SyntaxError("invalid value '%s' for target '%s'", nodeT, targetNodes)
}
}
if gwT, ok = st[targetGateways]; ok {
switch strings.ToLower(gwT) {
case "":
fallthrough
case "false":
fallthrough
case "no":
fallthrough
case "none":
fallthrough
case "0":
gwT = "0"
case "any":
fallthrough
case "one":
fallthrough
case "1":
gwT = "1"
case "all":
fallthrough
case "*":
gwT = "*"
default:
return "", "", "", "", fail.SyntaxError("invalid value '%s' for target '%s'", gwT, targetGateways)
}
}
if hostT == "0" && masterT == "0" && nodeT == "0" && gwT == "0" {
return "", "", "", "", fail.SyntaxError("no targets identified")
}
return hostT, masterT, nodeT, gwT, nil
}
// step is a struct containing the needed information to apply the installation
// step on all selected host targets
type step struct {
// Worker is a back pointer to the caller
Worker *worker
// GetName is the name of the step
Name string
// Action is the action of the step (check, add, remove)
Action installaction.Enum
// // Targets contains the host targets to select
// Targets stepTargets
// Script contains the script to execute
Script string
// WallTime contains the maximum time the step must run
WallTime time.Duration
// YamlKey contains the root yaml key on the specification file
YamlKey string
// Serial tells if step can be performed in parallel on selected host or not
Serial bool
}
// Run executes the step on all the concerned hosts
func (is *step) Run(task concurrency.Task, hosts []resources.Host, v data.Map, s resources.FeatureSettings) (outcomes resources.UnitResults, ferr fail.Error) {
outcomes = &unitResults{}
if task.Aborted() {
lerr, err := task.LastError()
if err != nil {
return outcomes, fail.AbortedError(nil, "aborted")
}
return outcomes, fail.AbortedError(lerr, "aborted")
}
if is.Serial || s.Serialize {
return is.loopSeriallyOnHosts(task, hosts, v)
}
return is.loopConcurrentlyOnHosts(task, hosts, v)
}
func (is *step) loopSeriallyOnHosts(task concurrency.Task, hosts []resources.Host, v data.Map) (outcomes resources.UnitResults, ferr fail.Error) {
tracer := debug.NewTracer(task, true, "").Entering()
defer tracer.Exiting()
defer fail.OnExitLogError(&ferr, tracer.TraceMessage())
ctx := task.Context()
outcomes = &unitResults{}
var (
subtask concurrency.Task
outcome concurrency.TaskResult
clonedV data.Map
)
for _, h := range hosts {
var xerr fail.Error
tracer.Trace("%s(%s):step(%s)@%s: starting", is.Worker.action.String(), is.Worker.feature.GetName(), is.Name, h.GetName())
clonedV, xerr = is.initLoopTurnForHost(ctx, h, v)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
return nil, xerr
}
subtask, xerr = concurrency.NewTaskWithParent(task, concurrency.InheritParentIDOption, concurrency.AmendID(fmt.Sprintf("/host/%s", h.GetName())))
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
return nil, xerr
}
outcome, xerr = subtask.Run(is.taskRunOnHost, runOnHostParameters{Host: h, Variables: clonedV})
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
return nil, xerr
}
if outcome != nil {
outcomes.AddOne(h.GetName(), outcome.(resources.UnitResult))
}
if !outcomes.Successful() {
if is.Worker.action == installaction.Check { // Checks can fail and it's ok
tracer.Trace("%s(%s):step(%s)@%s finished in %s: not present",
is.Worker.action.String(), is.Worker.feature.GetName(), is.Name, h.GetName(),
temporal.FormatDuration(time.Since(is.Worker.startTime)))
} else { // other steps are expected to succeed
tracer.Trace("%s(%s):step(%s)@%s failed in %s: %s",
is.Worker.action.String(), is.Worker.feature.GetName(), is.Name, h.GetName(),
temporal.FormatDuration(time.Since(is.Worker.startTime)), outcomes.ErrorMessages())
}
} else {
tracer.Trace("%s(%s):step(%s)@%s succeeded in %s.",
is.Worker.action.String(), is.Worker.feature.GetName(), is.Name, h.GetName(),
temporal.FormatDuration(time.Since(is.Worker.startTime)))
}
}
return outcomes, nil
}
func (is *step) loopConcurrentlyOnHosts(task concurrency.Task, hosts []resources.Host, v data.Map) (outcomes resources.UnitResults, ferr fail.Error) {
tracer := debug.NewTracer(task, true, "").Entering()
defer tracer.Exiting()
defer fail.OnExitLogError(&ferr, tracer.TraceMessage())
outcomes = &unitResults{}
ctx := task.Context()
var (
clonedV data.Map
subtask concurrency.Task
)
tg, xerr := concurrency.NewTaskGroupWithParent(task)
if xerr != nil {
return nil, xerr
}
var taskErr fail.Error
subtasks := map[string]concurrency.Task{}
for _, h := range hosts {
clonedV, taskErr = is.initLoopTurnForHost(ctx, h, v) // FIXME: Profile this
taskErr = debug.InjectPlannedFail(taskErr)
if taskErr != nil {
abErr := tg.AbortWithCause(taskErr)
if abErr != nil {
logrus.Warnf("there was an error trying to abort TaskGroup: %s", spew.Sdump(abErr))
}
break
}
subtask, taskErr = tg.Start(is.taskRunOnHost, runOnHostParameters{Host: h, Variables: clonedV})
taskErr = debug.InjectPlannedFail(taskErr)
if taskErr != nil {
abErr := tg.AbortWithCause(taskErr)
if abErr != nil {
logrus.Warnf("there was an error trying to abort TaskGroup: %s", spew.Sdump(abErr))
}
break
}
subtasks[h.GetName()] = subtask
}
tgr, xerr := tg.WaitGroup()
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
if len(subtasks) != len(hosts) {
logrus.Warnf("Not all tasks were started, there should be one task per host, is not the case: %d tasks and %d hosts", len(subtasks), len(hosts))
}
logrus.Errorf("Critical error: [%s], also look at step outcomes below for more information", spew.Sdump(xerr))
if taskErr != nil {
_ = taskErr.AddConsequence(xerr)
} else {
taskErr = xerr
}
}
wrongs, outcomes, cerr := is.collectOutcomes(subtasks, tgr)
if cerr != nil {
if wrongs == 0 && len(subtasks) == len(hosts) {
inconsistency := fail.InconsistentError("CRITICAL problem: there is a discrepancy between WaitGroup and its individual results: %w", cerr)
inconsistency.Annotate("wrongs", wrongs).Annotate("outcomes", outcomes)
return nil, inconsistency
}
if taskErr != nil {
_ = taskErr.AddConsequence(cerr)
return nil, taskErr
}
return nil, cerr
}
return outcomes, taskErr
}
// collectOutcomes collects results from subtasks
func (is *step) collectOutcomes(subtasks map[string]concurrency.Task, results concurrency.TaskGroupResult) (int, resources.UnitResults, fail.Error) {
outcomes := &unitResults{}
wrongs := 0
for k, s := range subtasks {
sid, err := s.ID()
if err != nil {
return 0, nil, err
}
outcome := results[sid]
if outcome != nil {
oko, ok := outcome.(stepResult)
if !ok {
return wrongs, nil, fail.InconsistentError("outcome should be a stepResult (implements resources.UnitResult)")
}
outcomes.AddOne(k, oko)
if oko.err != nil || !strings.Contains(oko.output, "exit 0") {
wrongs++
}
}
}
return wrongs, outcomes, nil
}
// initLoopTurnForHost inits the coming loop turn for a specific Host
func (is *step) initLoopTurnForHost(ctx context.Context, host resources.Host, v data.Map) (clonedV data.Map, ferr fail.Error) {
is.Worker.startTime = time.Now()
var xerr fail.Error
var cerr error
clonedV, cerr = v.FakeClone()
if cerr != nil {
return nil, fail.Wrap(cerr)
}
clonedV["HostIP"], xerr = host.GetPrivateIP(ctx)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
logrus.Errorf("aborting because of %s", xerr.Error())
return nil, xerr
}
clonedV["ShortHostname"] = host.GetName()
domain := ""
xerr = host.Review(func(clonable data.Clonable, props *serialize.JSONProperties) fail.Error {
return props.Inspect(hostproperty.DescriptionV1, func(clonable data.Clonable) fail.Error {
hostDescriptionV1, ok := clonable.(*propertiesv1.HostDescription)
if !ok {
return fail.InconsistentError("'*propertiesv1.HostDescription' expected, '%s' provided", reflect.TypeOf(clonable).String())
}
domain = hostDescriptionV1.Domain
if domain != "" {
domain = "." + domain
}
return nil
})
})
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
logrus.Errorf("aborting because of %s", xerr.Error())
return nil, xerr
}
var sn resources.Subnet
sn, xerr = host.GetDefaultSubnet(ctx)
if xerr != nil {
return nil, xerr
}
clonedV["CIDR"], xerr = sn.GetCIDR()
if xerr != nil {
return nil, xerr
}
clonedV["Hostname"] = host.GetName() + domain
// logrus.Warningf("Checking variable substitution for: %s", spew.Sdump(clonedV))
clonedV, xerr = realizeVariables(clonedV)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
return nil, fail.Wrap(xerr, "failed to realize variables")
}
return clonedV, nil
}
type runOnHostParameters struct {
Host resources.Host
Variables data.Map
}
// taskRunOnHost ...
// Respects interface concurrency.TaskFunc
// func (is *step) runOnHost(host *protocol.Host, v Variables) Resources.UnitResult {
func (is *step) taskRunOnHost(task concurrency.Task, params concurrency.TaskParameters) (result concurrency.TaskResult, ferr fail.Error) {
defer fail.OnPanic(&ferr)
defer func() {
if result != nil {
if sres, ok := result.(stepResult); ok {
if !sres.Completed() || !sres.Successful() || sres.Error() != nil {
dur := spew.Sdump(result)
if !strings.Contains(dur, "check_") {
logrus.Debugf("task result: %s", spew.Sdump(result))
}
}
}
}
if ferr != nil {
logrus.Debugf("task error: %v", ferr)
}
}()
var ok bool
if params == nil {
return nil, fail.InvalidParameterCannotBeNilError("params")
}
p, ok := params.(runOnHostParameters)
if !ok {
return nil, fail.InvalidParameterError("params", "must be of type 'runOnHostParameters'")
}
if task == nil {
return nil, fail.InvalidParameterCannotBeNilError("task")
}
// Updates variables in step script
command, xerr := replaceVariablesInString(is.Script, p.Variables)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
problem := fail.Wrap(xerr, "failed to finalize installer script for step '%s'", is.Name)
return stepResult{err: problem}, problem
}
hidesOutput := strings.Contains(command, "set +x\n")
if hidesOutput {
command = strings.Replace(command, "set +x\n", "\n", 1)
command = strings.Replace(command, "exec 2>&1\n", "exec 2>&7\n", 1)
}
// Uploads then executes command
filename := fmt.Sprintf("%s/feature.%s.%s_%s.sh", utils.TempFolder, is.Worker.feature.GetName(), strings.ToLower(is.Action.String()), is.Name)
rfcItem := Item{
Remote: filename,
}
defer func() {
_ = os.Remove(rfcItem.Local)
}()
xerr = rfcItem.UploadString(task.Context(), command, p.Host)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
logrus.Warnf("failure uploading script: %v", xerr)
problem := fail.Wrap(xerr, "failure uploading script")
return stepResult{err: problem}, problem
}
if !hidesOutput {
command = fmt.Sprintf("sudo -- bash -x -c 'sync; chmod u+rx %s; bash -x -c %s; exit ${PIPESTATUS}'", filename, filename)
} else {
command = fmt.Sprintf("sudo -- bash -x -c 'sync; chmod u+rx %s; captf=$(mktemp); bash -x -c \"BASH_XTRACEFD=7 %s 7>$captf 2>&7\"; rc=${PIPESTATUS};cat $captf; rm $captf; exit ${rc}'", filename, filename)
}
// If retcode is 126, iterate a few times...
rounds := 10
var (
retcode int
outrun string
outerr string
)
svc := p.Host.Service()
timings, xerr := svc.Timings()
if xerr != nil {
return nil, xerr
}
connTimeout := timings.ConnectionTimeout()
for {
retcode, outrun, outerr, xerr = p.Host.Run(task.Context(), command, outputs.COLLECT, connTimeout, is.WallTime)
if retcode == 126 {
logrus.Debugf("Text busy happened")
}
// Executes the script on the remote host
if retcode != 126 || rounds == 0 {
if retcode == 126 {
logrus.Warnf("Text busy killed the script")
}
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
xerr.Annotate("retcode", retcode)
xerr.Annotate("stdout", outrun)
xerr.Annotate("stderr", outerr)
return stepResult{err: xerr, retcode: retcode, output: outrun}, xerr
}
break
}
if !(strings.Contains(outrun, "bad interpreter") || strings.Contains(outerr, "bad interpreter")) {
if xerr != nil {
if !strings.Contains(xerr.Error(), "bad interpreter") {
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
xerr.Annotate("retcode", retcode)
xerr.Annotate("stdout", outrun)
xerr.Annotate("stderr", outerr)
return stepResult{err: xerr, retcode: retcode, output: outrun}, xerr
}
break
}
} else {
break
}
}
rounds--
time.Sleep(timings.SmallDelay())
}
return stepResult{success: retcode == 0, completed: true, err: nil, retcode: retcode, output: outrun}, nil
}
// realizeVariables replaces any template occurring in every variable
func realizeVariables(variables data.Map) (data.Map, fail.Error) {
cloneV, cerr := variables.FakeClone()
if cerr != nil {
return nil, fail.Wrap(cerr)
}
for k, v := range cloneV {
if variable, ok := v.(string); ok && variable != "" {
varTemplate, xerr := template.Parse("realize_var", variable)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
return cloneV, fail.SyntaxError("error parsing variable '%s': %s", k, xerr.Error())
}
buffer := bytes.NewBufferString("")
err := varTemplate.Option("missingkey=error").Execute(buffer, variables)
err = debug.InjectPlannedError(err)
if err != nil {
return cloneV, fail.ConvertError(err)
}
cloneV[k] = buffer.String()
}
}
return cloneV, nil
}
// replaceVariablesInString ...
func replaceVariablesInString(text string, v data.Map) (string, fail.Error) {
tmpl, xerr := template.Parse("replaceVariablesInString", text)
xerr = debug.InjectPlannedFail(xerr)
if xerr != nil {
return "", fail.SyntaxError("failed to parse: %s", xerr.Error())
}
dataBuffer := bytes.NewBufferString("")
err := tmpl.Option("missingkey=error").Execute(dataBuffer, v)
err = debug.InjectPlannedError(err)
if err != nil {
return "", fail.Wrap(err, "failed to replace variables")
}
return dataBuffer.String(), nil
}