-
Notifications
You must be signed in to change notification settings - Fork 23
/
executor.go
116 lines (102 loc) · 4.45 KB
/
executor.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
// 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 (
"context"
"math/rand"
"time"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"github.com/ystia/yorc/config"
"github.com/ystia/yorc/events"
"github.com/ystia/yorc/helper/stringutil"
"github.com/ystia/yorc/log"
"github.com/ystia/yorc/prov"
"github.com/ystia/yorc/tasks"
)
type defaultExecutor struct {
r *rand.Rand
cli *client.Client
}
// NewExecutor returns an Executor
func NewExecutor() prov.OperationExecutor {
cli, err := client.NewEnvClient()
if err != nil {
err = errors.Wrap(err, "failed to create docker execution client, docker sandboxing for operation hosted on orchestrator is disabled")
log.Printf("%v", err)
}
return &defaultExecutor{r: rand.New(rand.NewSource(time.Now().UnixNano())), cli: cli}
}
func (e *defaultExecutor) ExecOperation(ctx context.Context, conf config.Configuration, taskID, deploymentID, nodeName string, operation prov.Operation) error {
consulClient, err := conf.GetConsulClient()
if err != nil {
return err
}
kv := consulClient.KV()
logOptFields, ok := events.FromContext(ctx)
if !ok {
return errors.New("Missing context log fields")
}
logOptFields[events.NodeID] = nodeName
logOptFields[events.ExecutionID] = taskID
logOptFields[events.OperationName] = stringutil.GetLastElement(operation.Name, ".")
logOptFields[events.InterfaceName] = stringutil.GetAllExceptLastElement(operation.Name, ".")
ctx = events.NewContext(ctx, logOptFields)
exec, err := newExecution(ctx, kv, conf, taskID, deploymentID, nodeName, operation, e.cli)
if err != nil {
if IsOperationNotImplemented(err) {
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelDEBUG, deploymentID).Registerf("Voluntary bypassing error: %s", err.Error())
return nil
}
return err
}
instances, err := tasks.GetInstances(kv, taskID, deploymentID, nodeName)
if err != nil {
return err
}
logForAllInstances(ctx, deploymentID, instances, events.LogLevelINFO, "Start the ansible execution of: %s with operation : %s", nodeName, operation.Name)
// Execute operation
err = exec.execute(ctx, conf.Ansible.ConnectionRetries != 0)
if err == nil {
return nil
}
if !IsRetriable(err) {
logForAllInstances(ctx, deploymentID, instances, events.LogLevelERROR, "Ansible execution for operation %q on node %q failed", operation.Name, nodeName)
return err
}
// Retry operation if error is retriable and AnsibleConnectionRetries > 0
log.Debugf("Ansible Connection Retries:%d", conf.Ansible.ConnectionRetries)
if conf.Ansible.ConnectionRetries > 0 {
for i := 0; i < conf.Ansible.ConnectionRetries; i++ {
logForAllInstances(ctx, deploymentID, instances, events.LogLevelWARN, "Caught a retriable error from Ansible: '%v'. Let's retry in few seconds (%d/%d)", err, i+1, conf.Ansible.ConnectionRetries)
time.Sleep(time.Duration(e.r.Int63n(10)) * time.Second)
err = exec.execute(ctx, i != 0)
if err == nil {
return nil
}
if !IsRetriable(err) {
logForAllInstances(ctx, deploymentID, instances, events.LogLevelERROR, "Ansible execution for operation %q on node %q failed", operation.Name, nodeName)
return err
}
}
logForAllInstances(ctx, deploymentID, instances, events.LogLevelERROR, "Giving up retries for Ansible error: '%v' (%d/%d)", err, conf.Ansible.ConnectionRetries, conf.Ansible.ConnectionRetries)
}
logForAllInstances(ctx, deploymentID, instances, events.LogLevelERROR, "Ansible execution for operation %q on node %q failed", operation.Name, nodeName)
return err
}
func logForAllInstances(ctx context.Context, deploymentID string, instances []string, level events.LogLevel, msg string, args ...interface{}) {
for _, instanceID := range instances {
events.WithContextOptionalFields(events.AddLogOptionalFields(ctx, events.LogOptionalFields{events.InstanceID: instanceID})).NewLogEntry(level, deploymentID).Registerf(msg, args...)
}
}