-
Notifications
You must be signed in to change notification settings - Fork 23
/
execution_ansible.go
204 lines (189 loc) · 7.04 KB
/
execution_ansible.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
// 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 (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/pkg/errors"
"github.com/ystia/yorc/v3/events"
)
const uploadArtifactsPlaybook = `
- name: Upload artifacts
hosts: all
strategy: free
tasks:
[[[ range $artName, $art := .Artifacts ]]] [[[printf "- file: path=\"{{ ansible_env.HOME}}/%s/%s\" state=directory mode=0755" $.OperationRemotePath (path $art)]]]
[[[printf "- copy: src=\"%s/%s\" dest=\"{{ ansible_env.HOME}}/%s/%s\"" $.OverlayPath $art $.OperationRemotePath (path $art)]]]
[[[end]]]
`
const ansiblePlaybook = `
- import_playbook: [[[.PlaybookPath]]]
[[[if .HaveOutput]]]
- name: Retrieving Operation outputs
hosts: all
strategy: free
tasks:
[[[printf "- file: path=\"{{ ansible_env.HOME}}/%s\" state=directory mode=0755" $.OperationRemotePath]]]
[[[printf "- template: src=\"outputs.csv.j2\" dest=\"{{ ansible_env.HOME}}/%s/out.csv\"" $.OperationRemotePath]]]
[[[printf "- fetch: src=\"{{ ansible_env.HOME}}/%s/out.csv\" dest={{dest_folder}}/{{ansible_host}}-out.csv flat=yes" $.OperationRemotePath]]]
[[[end]]]
[[[if not .KeepOperationRemotePath]]]
- name: Cleanup temp directories
hosts: all
strategy: free
tasks:
- file: path="{{ ansible_env.HOME}}/[[[.OperationRemoteBaseDir]]]" state=absent
[[[end]]]
`
type executionAnsible struct {
*executionCommon
PlaybookPath string
isAlienAnsible bool
}
func (e *executionAnsible) runAnsible(ctx context.Context, retry bool, currentInstance, ansibleRecipePath string) error {
var err error
if !e.isAlienAnsible {
e.PlaybookPath, err = filepath.Abs(filepath.Join(e.OverlayPath, e.Primary))
} else {
var playbook string
for _, envInput := range e.EnvInputs {
if envInput.Name == "PLAYBOOK_ENTRY" {
playbook = envInput.Value
break
}
}
if playbook == "" {
err = errors.New("No PLAYBOOK_ENTRY input found for an alien4cloud ansible implementation")
}
if err == nil {
e.PlaybookPath, err = filepath.Abs(filepath.Join(e.OverlayPath, filepath.Dir(e.Primary), playbook))
}
}
if err != nil {
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
ansibleGroupsVarsPath := filepath.Join(ansibleRecipePath, "group_vars")
if err = os.MkdirAll(ansibleGroupsVarsPath, 0775); err != nil {
err = errors.Wrap(err, "Failed to create group_vars directory: ")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
var buffer bytes.Buffer
for _, envInput := range e.EnvInputs {
if envInput.InstanceName != "" {
buffer.WriteString(envInput.InstanceName)
buffer.WriteString("_")
}
v, err := e.encodeEnvInputValue(envInput, ansibleRecipePath)
if err != nil {
return err
}
buffer.WriteString(fmt.Sprintf("%s: %s", envInput.Name, v))
buffer.WriteString("\n")
}
if !e.isAlienAnsible {
for artName, art := range e.Artifacts {
buffer.WriteString(artName)
buffer.WriteString(": \"{{ansible_env.HOME}}/")
buffer.WriteString(e.OperationRemotePath)
buffer.WriteString("/")
buffer.WriteString(art)
buffer.WriteString("\"\n")
}
} else {
for artName, art := range e.Artifacts {
buffer.WriteString(artName)
buffer.WriteString(": \"")
buffer.WriteString(e.OverlayPath)
buffer.WriteString("/")
buffer.WriteString(art)
buffer.WriteString("\"\n")
}
}
for contextKey, contextValue := range e.Context {
buffer.WriteString(fmt.Sprintf("%s: %q", contextKey, contextValue))
buffer.WriteString("\n")
}
for contextKey, contextValue := range e.CapabilitiesCtx {
v, err := e.encodeTOSCAValue(contextValue, ansibleRecipePath)
if err != nil {
return err
}
buffer.WriteString(fmt.Sprintf("%s: %s", contextKey, v))
buffer.WriteString("\n")
}
buffer.WriteString("dest_folder: \"")
buffer.WriteString(ansibleRecipePath)
buffer.WriteString("\"\n")
if err = ioutil.WriteFile(filepath.Join(ansibleGroupsVarsPath, "all.yml"), buffer.Bytes(), 0664); err != nil {
err = errors.Wrap(err, "Failed to write global group vars file: ")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
if e.HaveOutput {
buffer.Reset()
for outputName := range e.Outputs {
buffer.WriteString(outputName)
buffer.WriteString(",{{")
idx := strings.LastIndex(outputName, "_")
buffer.WriteString(outputName[:idx])
buffer.WriteString("}}\n")
}
if err = ioutil.WriteFile(filepath.Join(ansibleRecipePath, "outputs.csv.j2"), buffer.Bytes(), 0664); err != nil {
err = errors.Wrap(err, "Failed to generate operation outputs file: ")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
}
buffer.Reset()
funcMap := template.FuncMap{
// The name "path" is what the function will be called in the template text.
"path": filepath.Dir,
"abs": filepath.Abs,
"cut": cutAfterLastUnderscore,
}
tmpl := template.New("execTemplate").Delims("[[[", "]]]").Funcs(funcMap)
var playbook string
if e.isAlienAnsible || len(e.Artifacts) == 0 {
playbook = ansiblePlaybook
} else {
playbook = uploadArtifactsPlaybook + ansiblePlaybook
}
tmpl, err = tmpl.Parse(playbook)
if err != nil {
err = errors.Wrap(err, "Failed to generate ansible playbook")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
if err = tmpl.Execute(&buffer, e); err != nil {
err = errors.Wrap(err, "Failed to Generate ansible playbook template")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
if err = ioutil.WriteFile(filepath.Join(ansibleRecipePath, "run.ansible.yml"), buffer.Bytes(), 0664); err != nil {
err = errors.Wrap(err, "Failed to write playbook file")
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelERROR, e.deploymentID).RegisterAsString(err.Error())
return err
}
events.WithContextOptionalFields(ctx).NewLogEntry(events.LogLevelDEBUG, e.deploymentID).RegisterAsString(fmt.Sprintf("Ansible recipe for node %q: executing %q on remote host(s)", e.NodeName, filepath.Base(e.PlaybookPath)))
outputHandler := &playbookOutputHandler{execution: e, context: ctx}
return e.executePlaybook(ctx, retry, ansibleRecipePath, outputHandler)
}