-
Notifications
You must be signed in to change notification settings - Fork 23
/
execution.go
1063 lines (965 loc) · 37.5 KB
/
execution.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// 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 ansible
import (
"archive/tar"
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/hashicorp/consul/api"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/ystia/yorc/config"
"github.com/ystia/yorc/deployments"
"github.com/ystia/yorc/events"
"github.com/ystia/yorc/helper/consulutil"
"github.com/ystia/yorc/helper/executil"
"github.com/ystia/yorc/helper/provutil"
"github.com/ystia/yorc/helper/stringutil"
"github.com/ystia/yorc/log"
"github.com/ystia/yorc/prov"
"github.com/ystia/yorc/prov/operations"
"github.com/ystia/yorc/tasks"
"github.com/ystia/yorc/tosca"
)
const ansibleConfig = `[defaults]
host_key_checking=False
timeout=30
stdout_callback = json
retry_files_save_path = #PLAY_PATH#
`
const ansibleFactCaching = `
gathering = smart
fact_caching = jsonfile
fact_caching_connection = #FACTS_CACHE_PATH#/facts_cache
`
type ansibleRetriableError struct {
root error
}
func (are ansibleRetriableError) Error() string {
return are.root.Error()
}
// IsRetriable checks if a given error is an Ansible retriable error
func IsRetriable(err error) bool {
_, ok := err.(ansibleRetriableError)
return ok
}
// IsOperationNotImplemented checks if a given error is an error indicating that an operation is not implemented
func IsOperationNotImplemented(err error) bool {
_, ok := err.(operationNotImplemented)
return ok
}
type operationNotImplemented struct {
msg string
}
func (oni operationNotImplemented) Error() string {
return oni.msg
}
type hostConnection struct {
host string
port int
user string
instanceID string
privateKey string
password string
}
type execution interface {
resolveExecution() error
execute(ctx context.Context, retry bool) error
}
type ansibleRunner interface {
runAnsible(ctx context.Context, retry bool, currentInstance, ansibleRecipePath string) error
}
type executionCommon struct {
kv *api.KV
cfg config.Configuration
ctx context.Context
deploymentID string
taskID string
NodeName string
operation prov.Operation
NodeType string
Description string
OperationRemoteBaseDir string
OperationRemotePath string
KeepOperationRemotePath bool
ArchiveArtifacts string
CacheFacts bool
EnvInputs []*operations.EnvInput
VarInputsNames []string
Primary string
BasePrimary string
Dependencies []string
hosts map[string]hostConnection
OperationPath string
NodePath string
NodeTypePath string
Artifacts map[string]string
OverlayPath string
Context map[string]string
Outputs map[string]string
HaveOutput bool
isRelationshipTargetNode bool
isPerInstanceOperation bool
isOrchestratorOperation bool
IsCustomCommand bool
relationshipType string
ansibleRunner ansibleRunner
sourceNodeInstances []string
targetNodeInstances []string
cli *client.Client
}
func newExecution(ctx context.Context, kv *api.KV, cfg config.Configuration, taskID, deploymentID, nodeName string, operation prov.Operation, cli *client.Client) (execution, error) {
execCommon := &executionCommon{kv: kv,
cfg: cfg,
ctx: ctx,
deploymentID: deploymentID,
NodeName: nodeName,
//KeepOperationRemotePath property is required to be public when resolving templates.
KeepOperationRemotePath: cfg.Ansible.KeepOperationRemotePath,
ArchiveArtifacts: strconv.FormatBool(cfg.Ansible.ArchiveArtifacts),
CacheFacts: cfg.Ansible.CacheFacts,
operation: operation,
VarInputsNames: make([]string, 0),
EnvInputs: make([]*operations.EnvInput, 0),
taskID: taskID,
Outputs: make(map[string]string),
cli: cli,
}
if err := execCommon.resolveOperation(); err != nil {
return nil, err
}
isBash, err := deployments.IsTypeDerivedFrom(kv, deploymentID, operation.ImplementationArtifact, implementationArtifactBash)
if err != nil {
return nil, err
}
isPython, err := deployments.IsTypeDerivedFrom(kv, deploymentID, operation.ImplementationArtifact, implementationArtifactPython)
if err != nil {
return nil, err
}
isAnsible, err := deployments.IsTypeDerivedFrom(kv, deploymentID, operation.ImplementationArtifact, implementationArtifactAnsible)
if err != nil {
return nil, err
}
var exec execution
if isBash || isPython {
execScript := &executionScript{executionCommon: execCommon, isPython: isPython}
execCommon.ansibleRunner = execScript
exec = execScript
} else if isAnsible {
execAnsible := &executionAnsible{executionCommon: execCommon}
execCommon.ansibleRunner = execAnsible
exec = execAnsible
} else {
return nil, errors.Errorf("Unsupported artifact implementation for node: %q, operation: %s, primary implementation: %q", nodeName, operation.Name, execCommon.Primary)
}
return exec, exec.resolveExecution()
}
func (e *executionCommon) resolveOperation() error {
e.NodePath = path.Join(consulutil.DeploymentKVPrefix, e.deploymentID, "topology/nodes", e.NodeName)
var err error
e.NodeType, err = deployments.GetNodeType(e.kv, e.deploymentID, e.NodeName)
if err != nil {
return err
}
e.NodeTypePath = path.Join(consulutil.DeploymentKVPrefix, e.deploymentID, "topology/types", e.NodeType)
if e.operation.RelOp.IsRelationshipOperation {
e.relationshipType, err = deployments.GetRelationshipForRequirement(e.kv, e.deploymentID, e.NodeName, e.operation.RelOp.RequirementIndex)
if err != nil {
return err
}
err = e.resolveIsPerInstanceOperation(e.operation.Name)
if err != nil {
return err
}
} else if strings.Contains(e.operation.Name, "custom") {
e.IsCustomCommand = true
}
operationNodeType := e.NodeType
if e.operation.RelOp.IsRelationshipOperation {
operationNodeType = e.relationshipType
}
e.OperationPath, e.Primary, err = deployments.GetOperationPathAndPrimaryImplementation(e.kv, e.deploymentID, e.operation.ImplementedInNodeTemplate, operationNodeType, e.operation.Name)
if err != nil {
return err
}
if e.OperationPath == "" || e.Primary == "" {
return operationNotImplemented{msg: fmt.Sprintf("primary implementation missing for operation %q of type %q in deployment %q is missing", e.operation.Name, e.NodeType, e.deploymentID)}
}
e.Primary = strings.TrimSpace(e.Primary)
log.Debugf("Operation Path: %q, primary implementation: %q", e.OperationPath, e.Primary)
e.BasePrimary = path.Base(e.Primary)
kvPair, _, err := e.kv.Get(e.OperationPath+"/implementation/dependencies", nil)
if err != nil {
return err
}
if kvPair != nil {
e.Dependencies = strings.Split(string(kvPair.Value), ",")
} else {
e.Dependencies = make([]string, 0)
}
kvPair, _, err = e.kv.Get(e.OperationPath+"/description", nil)
if err != nil {
return errors.Wrap(err, "Consul query failed: ")
}
if kvPair != nil && len(kvPair.Value) > 0 {
e.Description = string(kvPair.Value)
}
// if operation_host is not overridden by requirement, we retrieve operation/implementation definition info
if e.operation.OperationHost == "" {
kvPair, _, err = e.kv.Get(e.OperationPath+"/implementation/operation_host", nil)
if err != nil {
return errors.Wrap(err, "Consul query failed: ")
}
if kvPair != nil && len(kvPair.Value) > 0 {
e.operation.OperationHost = string(kvPair.Value)
}
}
e.isOrchestratorOperation = operations.IsOrchestratorHostOperation(e.operation)
e.isRelationshipTargetNode = operations.IsRelationshipTargetNodeOperation(e.operation)
return e.resolveInstances()
}
func (e *executionCommon) resolveInstances() error {
var err error
if e.operation.RelOp.IsRelationshipOperation {
e.targetNodeInstances, err = tasks.GetInstances(e.kv, e.taskID, e.deploymentID, e.operation.RelOp.TargetNodeName)
if err != nil {
return err
}
}
e.sourceNodeInstances, err = tasks.GetInstances(e.kv, e.taskID, e.deploymentID, e.NodeName)
return err
}
func (e *executionCommon) resolveArtifacts() error {
log.Debugf("Resolving artifacts")
var err error
if e.operation.RelOp.IsRelationshipOperation {
// First get linked node artifacts
if e.isRelationshipTargetNode {
e.Artifacts, err = deployments.GetArtifactsForNode(e.kv, e.deploymentID, e.operation.RelOp.TargetNodeName)
if err != nil {
return err
}
} else {
e.Artifacts, err = deployments.GetArtifactsForNode(e.kv, e.deploymentID, e.NodeName)
if err != nil {
return err
}
}
// Then get relationship type artifacts
var arts map[string]string
arts, err = deployments.GetArtifactsForType(e.kv, e.deploymentID, e.relationshipType)
if err != nil {
return err
}
for artName, art := range arts {
e.Artifacts[artName] = art
}
} else {
e.Artifacts, err = deployments.GetArtifactsForNode(e.kv, e.deploymentID, e.NodeName)
if err != nil {
return err
}
}
log.Debugf("Resolved artifacts: %v", e.Artifacts)
return nil
}
func (e *executionCommon) setHostConnection(kv *api.KV, host, instanceID, capType string, conn *hostConnection) error {
hasEndpoint, err := deployments.IsTypeDerivedFrom(e.kv, e.deploymentID, capType, "yorc.capabilities.Endpoint.ProvisioningAdmin")
if err != nil {
return err
}
if hasEndpoint {
found, user, err := deployments.GetInstanceCapabilityAttribute(e.kv, e.deploymentID, host, instanceID, "endpoint", "credentials", "user")
if err != nil {
return err
}
if found {
conn.user = config.DefaultConfigTemplateResolver.ResolveValueWithTemplates("host.user", user).(string)
} else {
mess := fmt.Sprintf("[Warning] No user set for connection:%+v", conn)
log.Printf(mess)
events.WithContextOptionalFields(e.ctx).NewLogEntry(events.LogLevelWARN, e.deploymentID).RegisterAsString(mess)
}
found, password, err := deployments.GetInstanceCapabilityAttribute(e.kv, e.deploymentID, host, instanceID, "endpoint", "credentials", "token")
if err != nil {
return err
}
if found && password != "" {
conn.password = config.DefaultConfigTemplateResolver.ResolveValueWithTemplates("host.password", password).(string)
}
found, privateKey, err := deployments.GetInstanceCapabilityAttribute(e.kv, e.deploymentID, host, instanceID, "endpoint", "credentials", "keys", "0")
if err != nil {
return err
}
if found && privateKey != "" {
conn.privateKey = config.DefaultConfigTemplateResolver.ResolveValueWithTemplates("host.privateKey", privateKey).(string)
}
found, port, err := deployments.GetInstanceCapabilityAttribute(e.kv, e.deploymentID, host, instanceID, "endpoint", "port")
if err != nil {
return err
}
if found && port != "" {
conn.port, err = strconv.Atoi(port)
if err != nil {
return errors.Wrapf(err, "Failed to convert port value:%q to int", port)
}
}
}
return nil
}
func (e *executionCommon) resolveHostsOrchestratorLocal(nodeName string, instances []string) error {
e.hosts = make(map[string]hostConnection, len(instances))
for i := range instances {
instanceName := operations.GetInstanceName(nodeName, instances[i])
e.hosts[instanceName] = hostConnection{host: instanceName, instanceID: instances[i]}
}
return nil
}
func (e *executionCommon) resolveHostsOnCompute(nodeName string, instances []string) error {
hostedOnList := make([]string, 0)
hostedOnList = append(hostedOnList, nodeName)
parentHost, err := deployments.GetHostedOnNode(e.kv, e.deploymentID, nodeName)
if err != nil {
return err
}
for parentHost != "" {
hostedOnList = append(hostedOnList, parentHost)
parentHost, err = deployments.GetHostedOnNode(e.kv, e.deploymentID, parentHost)
if err != nil {
return err
}
}
hosts := make(map[string]hostConnection)
var found bool
for i := len(hostedOnList) - 1; i >= 0 && !found; i-- {
host := hostedOnList[i]
capType, err := deployments.GetNodeCapabilityType(e.kv, e.deploymentID, host, "endpoint")
if err != nil {
return err
}
hasEndpoint, err := deployments.IsTypeDerivedFrom(e.kv, e.deploymentID, capType, "tosca.capabilities.Endpoint")
if err != nil {
return err
}
if hasEndpoint {
for _, instance := range instances {
_, ipAddress, err := deployments.GetInstanceCapabilityAttribute(e.kv, e.deploymentID, host, instance, "endpoint", "ip_address")
if err != nil {
return err
}
if ipAddress != "" {
ipAddress = config.DefaultConfigTemplateResolver.ResolveValueWithTemplates("host.ip_address", ipAddress).(string)
instanceName := operations.GetInstanceName(nodeName, instance)
hostConn := hostConnection{host: ipAddress, instanceID: instance}
err = e.setHostConnection(e.kv, host, instance, capType, &hostConn)
if err != nil {
mess := fmt.Sprintf("[ERROR] failed to set host connection with error: %+v", err)
log.Debug(mess)
events.WithContextOptionalFields(e.ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(mess)
return err
}
hosts[instanceName] = hostConn
found = true
}
}
}
}
if len(hosts) == 0 {
return errors.Errorf("Failed to resolve hosts for node %q", nodeName)
}
e.hosts = hosts
return nil
}
func (e *executionCommon) resolveHosts(nodeName string) error {
// Resolve hosts from the hostedOn hierarchy from bottom to top by finding the first node having a capability
// named endpoint and derived from "tosca.capabilities.Endpoint"
log.Debugf("Resolving hosts for node %q", nodeName)
instances := e.sourceNodeInstances
if e.isRelationshipTargetNode {
instances = e.targetNodeInstances
}
if e.isOrchestratorOperation {
return e.resolveHostsOrchestratorLocal(nodeName, instances)
}
return e.resolveHostsOnCompute(nodeName, instances)
}
func (e *executionCommon) resolveContext() error {
execContext := make(map[string]string)
newNode := provutil.SanitizeForShell(e.NodeName)
if !e.operation.RelOp.IsRelationshipOperation {
execContext["NODE"] = newNode
}
var instances []string
if e.isRelationshipTargetNode {
instances = e.targetNodeInstances
} else {
instances = e.sourceNodeInstances
}
names := make([]string, len(instances))
for i := range instances {
instanceName := operations.GetInstanceName(e.NodeName, instances[i])
names[i] = instanceName
}
if !e.operation.RelOp.IsRelationshipOperation {
e.VarInputsNames = append(e.VarInputsNames, "INSTANCE")
execContext["INSTANCES"] = strings.Join(names, ",")
if host, err := deployments.GetHostedOnNode(e.kv, e.deploymentID, e.NodeName); err != nil {
return err
} else if host != "" {
execContext["HOST"] = host
}
} else {
if host, err := deployments.GetHostedOnNode(e.kv, e.deploymentID, e.NodeName); err != nil {
return err
} else if host != "" {
execContext["SOURCE_HOST"] = host
}
if host, err := deployments.GetHostedOnNode(e.kv, e.deploymentID, e.operation.RelOp.TargetNodeName); err != nil {
return err
} else if host != "" {
execContext["TARGET_HOST"] = host
}
execContext["SOURCE_NODE"] = newNode
if e.isRelationshipTargetNode && !e.isPerInstanceOperation {
execContext["SOURCE_INSTANCE"] = names[0]
} else {
e.VarInputsNames = append(e.VarInputsNames, "SOURCE_INSTANCE")
}
sourceNames := make([]string, len(e.sourceNodeInstances))
for i := range e.sourceNodeInstances {
sourceNames[i] = operations.GetInstanceName(e.NodeName, e.sourceNodeInstances[i])
}
execContext["SOURCE_INSTANCES"] = strings.Join(sourceNames, ",")
execContext["TARGET_NODE"] = provutil.SanitizeForShell(e.operation.RelOp.TargetNodeName)
targetNames := make([]string, len(e.targetNodeInstances))
for i := range e.targetNodeInstances {
targetNames[i] = operations.GetInstanceName(e.operation.RelOp.TargetNodeName, e.targetNodeInstances[i])
}
execContext["TARGET_INSTANCES"] = strings.Join(targetNames, ",")
if !e.isRelationshipTargetNode && !e.isPerInstanceOperation {
if len(targetNames) == 0 {
log.Debugf("No target instance defined in context %+v", e)
} else {
execContext["TARGET_INSTANCE"] = targetNames[0]
}
} else {
e.VarInputsNames = append(e.VarInputsNames, "TARGET_INSTANCE")
}
}
capabilitiesCtx, err := operations.GetTargetCapabilityPropertiesAndAttributes(e.ctx, e.kv, e.deploymentID, e.NodeName, e.operation)
if err != nil {
return err
}
for k, v := range capabilitiesCtx {
execContext[k] = v
}
execContext["DEPLOYMENT_ID"] = e.deploymentID
e.Context = execContext
return nil
}
func (e *executionCommon) resolveOperationOutputPath() error {
//Here we get the modelable entity output of the operation
entities, _, err := e.kv.Keys(e.OperationPath+"/outputs/", "/", nil)
if err != nil {
return err
}
if len(entities) == 0 {
return nil
}
e.HaveOutput = true
//We iterate over all entity of the output in this operation
for _, entity := range entities {
//We get the name of the output
outputKeys, _, err := e.kv.Keys(entity, "/", nil)
if err != nil {
return err
}
for _, output := range outputKeys {
//We get the expression of the output
kvPair, _, err := e.kv.Get(output+"/expression", nil)
if err != nil {
return err
}
if kvPair == nil {
return errors.Errorf("Operation output expression is missing for key: %q", output)
}
va := &tosca.ValueAssignment{}
err = yaml.Unmarshal(kvPair.Value, va)
if err != nil {
return errors.Wrap(err, "Fail to parse operation output, check the following expression : ")
}
if va.Type != tosca.ValueAssignmentFunction {
return errors.Errorf("Output %q for operation %v is not a valid get_operation_output TOSCA function", path.Base(output), e.operation)
}
oof := va.GetFunction()
if oof.Operator != tosca.GetOperationOutputOperator {
return errors.Errorf("Output %q for operation %v (%v) is not a valid get_operation_output TOSCA function", path.Base(output), e.operation, oof)
}
targetContext := oof.Operands[0].String() == "TARGET"
sourceContext := oof.Operands[0].String() == "SOURCE"
if (targetContext || sourceContext) && !e.operation.RelOp.IsRelationshipOperation {
return errors.Errorf("Can't resolve an operation output in SOURCE or TARGET context without a relationship operation: %q", va.String())
}
var instancesIds []string
if targetContext {
instancesIds = e.targetNodeInstances
} else {
instancesIds = e.sourceNodeInstances
}
//For each instance of the node we create a new entry in the output map
for _, instanceID := range instancesIds {
//We decide to add an in to differentiate if we export many time the same output
b := uint32(time.Now().Nanosecond())
interfaceName := strings.ToLower(url.QueryEscape(oof.Operands[1].String()))
operationName := strings.ToLower(url.QueryEscape(oof.Operands[2].String()))
outputVariableName := url.QueryEscape(oof.Operands[3].String())
if targetContext {
e.Outputs[outputVariableName+"_"+fmt.Sprint(b)] = path.Join("instances", e.operation.RelOp.TargetNodeName, instanceID, "outputs", interfaceName, operationName, outputVariableName)
} else {
//If we are with an expression type {get_operation_output : [ SELF, ...]} in a relationship we store the result in the corresponding relationship instance
if oof.Operands[0].String() == "SELF" && e.operation.RelOp.IsRelationshipOperation {
relationShipPrefix := filepath.Join("relationship_instances", e.NodeName, e.operation.RelOp.RequirementIndex, instanceID)
e.Outputs[outputVariableName+"_"+fmt.Sprint(b)] = path.Join(relationShipPrefix, "outputs", interfaceName, operationName, outputVariableName)
} else if oof.Operands[0].String() == "HOST" {
// In this case we continue because the parsing has change this type on {get_operation_output : [ SELF, ...]} on the host node
continue
} else {
//In all others case we simply save the result of the output on the instance directory of the node
e.Outputs[outputVariableName+"_"+fmt.Sprint(b)] = path.Join("instances", e.NodeName, instanceID, "outputs", interfaceName, operationName, outputVariableName)
}
}
}
}
}
return nil
}
// resolveIsPerInstanceOperation sets e.isPerInstanceOperation to true if the given operationName contains one of the following patterns (case doesn't matter):
// add_target, remove_target, add_source, remove_source, target_changed
// And in case of a relationship operation the relationship does not derive from "tosca.relationships.HostedOn" as it makes no sense till we scale at compute level
func (e *executionCommon) resolveIsPerInstanceOperation(operationName string) error {
op := strings.ToLower(operationName)
if strings.Contains(op, "add_target") || strings.Contains(op, "remove_target") || strings.Contains(op, "target_changed") || strings.Contains(op, "add_source") || strings.Contains(op, "remove_source") {
// Do not call the call the operation several time for a "HostedOn" relationship (makes no sense till we scale at compute level)
if hostedOn, err := deployments.IsTypeDerivedFrom(e.kv, e.deploymentID, e.relationshipType, "tosca.relationships.HostedOn"); err != nil || hostedOn {
e.isPerInstanceOperation = false
return err
}
e.isPerInstanceOperation = true
return nil
}
e.isPerInstanceOperation = false
return nil
}
func (e *executionCommon) resolveInputs() error {
var err error
e.EnvInputs, e.VarInputsNames, err = operations.ResolveInputsWithInstances(e.kv, e.deploymentID, e.NodeName, e.taskID, e.operation, e.sourceNodeInstances, e.targetNodeInstances)
return err
}
func (e *executionCommon) resolveExecution() error {
log.Debugf("Preparing execution of operation %q on node %q for deployment %q", e.operation.Name, e.NodeName, e.deploymentID)
ovPath, err := filepath.Abs(filepath.Join(e.cfg.WorkingDirectory, "deployments", e.deploymentID, "overlay"))
if err != nil {
return err
}
e.OverlayPath = ovPath
if err = e.resolveInputs(); err != nil {
return err
}
if err = e.resolveArtifacts(); err != nil {
return err
}
if e.isRelationshipTargetNode {
err = e.resolveHosts(e.operation.RelOp.TargetNodeName)
} else {
err = e.resolveHosts(e.NodeName)
}
if err != nil {
return err
}
if err = e.resolveOperationOutputPath(); err != nil {
return err
}
return e.resolveContext()
}
func (e *executionCommon) execute(ctx context.Context, retry bool) error {
if e.isPerInstanceOperation {
var nodeName string
var instances []string
if !e.isRelationshipTargetNode {
nodeName = e.operation.RelOp.TargetNodeName
instances = e.targetNodeInstances
} else {
nodeName = e.NodeName
instances = e.sourceNodeInstances
}
for _, instanceID := range instances {
instanceName := operations.GetInstanceName(nodeName, instanceID)
log.Debugf("Executing operation %q, on node %q, with current instance %q", e.operation.Name, e.NodeName, instanceName)
err := e.executeWithCurrentInstance(ctx, retry, instanceName)
if err != nil {
return err
}
}
} else {
return e.executeWithCurrentInstance(ctx, retry, "")
}
return nil
}
func (e *executionCommon) generateHostConnectionForOrchestratorOperation(ctx context.Context, buffer *bytes.Buffer) error {
if e.cli != nil && e.cfg.Ansible.HostedOperations.DefaultSandbox != nil {
containerID, err := createSandbox(ctx, e.cli, e.cfg.Ansible.HostedOperations.DefaultSandbox, e.deploymentID)
if err != nil {
return err
}
buffer.WriteString(" ansible_connection=docker ansible_host=")
buffer.WriteString(containerID)
} else if e.cfg.Ansible.HostedOperations.UnsandboxedOperationsAllowed {
buffer.WriteString(" ansible_connection=local")
} else {
actualRootCause := "there is no sandbox configured to handle it"
if e.cli == nil {
actualRootCause = "connection to docker failed (see logs)"
}
err := errors.Errorf("Ansible provisioning: you are trying to execute an operation on the orchestrator host but %s and execution on the actual orchestrator host is disallowed by configuration", actualRootCause)
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).Registerf("%v", err)
return err
}
return nil
}
func (e *executionCommon) generateHostConnection(ctx context.Context, buffer *bytes.Buffer, host hostConnection) error {
buffer.WriteString(host.host)
if e.isOrchestratorOperation {
err := e.generateHostConnectionForOrchestratorOperation(ctx, buffer)
if err != nil {
return err
}
} else {
sshUser := host.user
if sshUser == "" {
// Use root as default user
sshUser = "root"
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelWARN, e.deploymentID).RegisterAsString("Ansible provisioning: Missing ssh user information, trying to use root user.")
}
sshPassword := host.password
sshPrivateKey := host.privateKey
if sshPrivateKey == "" && sshPassword == "" {
sshPrivateKey = "~/.ssh/yorc.pem"
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelWARN, e.deploymentID).RegisterAsString("Ansible provisioning: Missing ssh password or private key information, trying to use default private key ~/.ssh/yorc.pem.")
}
buffer.WriteString(fmt.Sprintf(" ansible_ssh_user=%s ansible_ssh_common_args=\"-o ConnectionAttempts=20\"", sshUser))
// Set with priority private key against password
if sshPrivateKey != "" {
// TODO if not a path store it somewhere
// Note whould be better if we can use it directly https://github.com/ansible/ansible/issues/22382
buffer.WriteString(fmt.Sprintf(" ansible_ssh_private_key_file=%s", sshPrivateKey))
} else if sshPassword != "" {
// TODO use vault
buffer.WriteString(fmt.Sprintf(" ansible_ssh_pass=%s", sshPassword))
}
// Specify SSH port when different than default 22
if host.port != 0 && host.port != 22 {
buffer.WriteString(fmt.Sprintf(" ansible_ssh_port=%d", host.port))
}
}
buffer.WriteString("\n")
return nil
}
func (e *executionCommon) executeWithCurrentInstance(ctx context.Context, retry bool, currentInstance string) error {
// Create a cancel func here to remove docker sandboxes as soon as we exit this function
ctx, cancelFn := context.WithCancel(ctx)
defer cancelFn()
ansiblePath := filepath.Join(e.cfg.WorkingDirectory, "deployments", e.deploymentID, "ansible")
ansiblePath, err := filepath.Abs(ansiblePath)
if err != nil {
return err
}
var ansibleRecipePath string
if e.operation.RelOp.IsRelationshipOperation {
ansibleRecipePath = filepath.Join(ansiblePath, e.NodeName, e.relationshipType, e.operation.RelOp.TargetRelationship, e.operation.Name, currentInstance)
} else {
ansibleRecipePath = filepath.Join(ansiblePath, e.NodeName, e.operation.Name, currentInstance)
}
if err = os.RemoveAll(ansibleRecipePath); err != nil {
err = errors.Wrapf(err, "Failed to remove ansible recipe directory %q for node %q operation %q", ansibleRecipePath, e.NodeName, e.operation.Name)
log.Debugf("%+v", err)
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
ansibleHostVarsPath := filepath.Join(ansibleRecipePath, "host_vars")
if err = os.MkdirAll(ansibleHostVarsPath, 0775); err != nil {
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
log.Debugf("Generating hosts files hosts: %+v ", e.hosts)
var buffer bytes.Buffer
buffer.WriteString("[all]\n")
for instanceName, host := range e.hosts {
err = e.generateHostConnection(ctx, &buffer, host)
if err != nil {
return err
}
var perInstanceInputsBuffer bytes.Buffer
for _, varInput := range e.VarInputsNames {
if varInput == "INSTANCE" {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("INSTANCE: %q\n", instanceName))
} else if varInput == "SOURCE_INSTANCE" {
if !e.isPerInstanceOperation {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("SOURCE_INSTANCE: %q\n", instanceName))
} else {
if e.isRelationshipTargetNode {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("SOURCE_INSTANCE: %q\n", currentInstance))
} else {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("SOURCE_INSTANCE: %q\n", instanceName))
}
}
} else if varInput == "TARGET_INSTANCE" {
if !e.isPerInstanceOperation {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("TARGET_INSTANCE: %q\n", instanceName))
} else {
if e.isRelationshipTargetNode {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("TARGET_INSTANCE: %q\n", instanceName))
} else {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("TARGET_INSTANCE: %q\n", currentInstance))
}
}
} else {
for _, envInput := range e.EnvInputs {
if envInput.Name == varInput && (envInput.InstanceName == instanceName || e.isPerInstanceOperation && envInput.InstanceName == currentInstance) {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("%s: %q\n", varInput, envInput.Value))
goto NEXT
}
}
if e.operation.RelOp.IsRelationshipOperation {
var hostedOn bool
hostedOn, err = deployments.IsTypeDerivedFrom(e.kv, e.deploymentID, e.relationshipType, "tosca.relationships.HostedOn")
if err != nil {
return err
} else if hostedOn {
// In case of operation for relationships derived from HostedOn we should match the inputs with the same instanceID
instanceIDIdx := strings.LastIndex(instanceName, "_")
// Get index
if instanceIDIdx > 0 {
instanceID := instanceName[instanceIDIdx:]
for _, envInput := range e.EnvInputs {
if envInput.Name == varInput && strings.HasSuffix(envInput.InstanceName, instanceID) {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("%s: %q\n", varInput, envInput.Value))
goto NEXT
}
}
}
}
}
// Not found with the combination inputName/instanceName let's use the first that matches the input name
for _, envInput := range e.EnvInputs {
if envInput.Name == varInput {
perInstanceInputsBuffer.WriteString(fmt.Sprintf("%s: %q\n", varInput, envInput.Value))
goto NEXT
}
}
return errors.Errorf("Unable to find a suitable input for input name %q and instance %q", varInput, instanceName)
}
NEXT:
}
if perInstanceInputsBuffer.Len() > 0 {
if err = ioutil.WriteFile(filepath.Join(ansibleHostVarsPath, host.host+".yml"), perInstanceInputsBuffer.Bytes(), 0664); err != nil {
return errors.Wrapf(err, "Failed to write vars for host %q file: %v", host, err)
}
}
}
if e.isOrchestratorOperation {
buffer.WriteString("\n[all:vars]\n")
buffer.WriteString("ansible_python_interpreter=/usr/bin/env python\n")
}
if err = ioutil.WriteFile(filepath.Join(ansibleRecipePath, "hosts"), buffer.Bytes(), 0664); err != nil {
err = errors.Wrap(err, "Failed to write hosts file")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
ansibleCfgContent := strings.Replace(ansibleConfig, "#PLAY_PATH#", ansibleRecipePath, -1)
if e.CacheFacts {
ansibleCfgCacheContent := strings.Replace(ansibleFactCaching, "#FACTS_CACHE_PATH#", ansiblePath, -1)
ansibleCfgContent += ansibleCfgCacheContent
}
if err = ioutil.WriteFile(filepath.Join(ansibleRecipePath, "ansible.cfg"), []byte(ansibleCfgContent), 0664); err != nil {
err = errors.Wrap(err, "Failed to write ansible.cfg file")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
// e.OperationRemoteBaseDir is an unique base temp directory for multiple executions
e.OperationRemoteBaseDir = stringutil.UniqueTimestampedName(e.cfg.Ansible.OperationRemoteBaseDir+"_", "")
if e.operation.RelOp.IsRelationshipOperation {
e.OperationRemotePath = path.Join(e.OperationRemoteBaseDir, e.NodeName, e.relationshipType, e.operation.Name)
} else {
e.OperationRemotePath = path.Join(e.OperationRemoteBaseDir, e.NodeName, e.operation.Name)
}
log.Debugf("OperationRemotePath:%s", e.OperationRemotePath)
// Build archives for artifacts
for artifactName, artifactPath := range e.Artifacts {
tarPath := filepath.Join(ansibleRecipePath, artifactName+".tar")
buildArchive(e.OverlayPath, artifactPath, tarPath)
}
err = e.ansibleRunner.runAnsible(ctx, retry, currentInstance, ansibleRecipePath)
if err != nil {
return err
}
if e.HaveOutput {
outputsFiles, err := filepath.Glob(filepath.Join(ansibleRecipePath, "*-out.csv"))
if err != nil {
err = errors.Wrapf(err, "Output retrieving of Ansible execution for node %q failed", e.NodeName)
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
for _, outFile := range outputsFiles {
fi, err := os.Open(outFile)
if err != nil {
err = errors.Wrapf(err, "Output retrieving of Ansible execution for node %q failed", e.NodeName)
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
r := csv.NewReader(fi)
// If LazyQuotes is true, a quote may appear in an unquoted field
// and non-doubled quote may appear in a quoted field.
// fix issue: https://github.com/golang/go/issues/21672
r.LazyQuotes = true
records, err := r.ReadAll()
if err != nil {
err = errors.Wrapf(err, "Output retrieving of Ansible execution for node %q failed", e.NodeName)
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
for _, line := range records {
if err = consulutil.StoreConsulKeyAsString(path.Join(consulutil.DeploymentKVPrefix, e.deploymentID, "topology", e.Outputs[line[0]]), line[1]); err != nil {
return err
}
}
}
}
return nil
}
func (e *executionCommon) checkAnsibleRetriableError(ctx context.Context, err error) error {
log.Debugf(err.Error())
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
// Exit Code 4 is corresponding to unreachable host and is eligible for connection retries
// https://github.com/ansible/ansible/blob/devel/lib/ansible/executor/task_queue_manager.py
if status.ExitStatus() == 4 {
return ansibleRetriableError{root: err}
}
}
}
return err
}
func (e *executionCommon) getInstanceIDFromHost(host string) (string, error) {
for _, hostConn := range e.hosts {
if hostConn.host == host {
return hostConn.instanceID, nil
}
}
return "", errors.Errorf("Unknown host %q", host)
}
func (e *executionCommon) executePlaybook(ctx context.Context, retry bool, ansibleRecipePath string, logFn logAnsibleOutputInConsulFn) error {
cmd := executil.Command(ctx, "ansible-playbook", "-i", "hosts", "run.ansible.yml")
if _, err := os.Stat(filepath.Join(ansibleRecipePath, "run.ansible.retry")); retry && (err == nil || !os.IsNotExist(err)) {
cmd.Args = append(cmd.Args, "--limit", filepath.Join("@", ansibleRecipePath, "run.ansible.retry"))
}
if e.cfg.Ansible.DebugExec {
cmd.Args = append(cmd.Args, "-vvvv")
}
if !e.isOrchestratorOperation {
if e.cfg.Ansible.UseOpenSSH {
cmd.Args = append(cmd.Args, "-c", "ssh")
} else {
cmd.Args = append(cmd.Args, "-c", "paramiko")
}
}
cmd.Dir = ansibleRecipePath
var outbuf bytes.Buffer
errbuf := events.NewBufferedLogEntryWriter()
cmd.Stdout = &outbuf
cmd.Stderr = errbuf
errCloseCh := make(chan bool)
defer close(errCloseCh)
// Register log entry via error buffer
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RunBufferedRegistration(errbuf, errCloseCh)
defer func(buffer *bytes.Buffer) {