-
Notifications
You must be signed in to change notification settings - Fork 23
/
execution_ansible.go
155 lines (140 loc) · 5.74 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
// 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"
"text/template"
"github.com/pkg/errors"
"strings"
"github.com/ystia/yorc/events"
)
const ansiblePlaybook = `
- 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]]]
- 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
}
func (e *executionAnsible) runAnsible(ctx context.Context, retry bool, currentInstance, ansibleRecipePath string) error {
var err error
e.PlaybookPath, err = filepath.Abs(filepath.Join(e.OverlayPath, e.Primary))
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("_")
}
buffer.WriteString(fmt.Sprintf("%s: %q", envInput.Name, envInput.Value))
buffer.WriteString("\n")
}
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")
}
for contextKey, contextValue := range e.Context {
buffer.WriteString(fmt.Sprintf("%s: %q", contextKey, contextValue))
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)
tmpl, err = tmpl.Parse(ansiblePlaybook)
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)))
return e.executePlaybook(ctx, retry, ansibleRecipePath, logAnsibleOutputInConsul)
}