/
ansible_assets.go
219 lines (180 loc) · 6.03 KB
/
ansible_assets.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
package builder
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"gopkg.in/yaml.v2"
"github.com/werf/werf/pkg/stapel"
"github.com/werf/werf/pkg/util"
)
func (b *Ansible) createStageWorkDirStructure(userStageName string) error {
stageWorkDir, err := b.stageHostWorkDir(userStageName)
if err != nil {
return err
}
// playbook with tasks for a stage
stagePlaybook, err := b.stagePlaybook(userStageName)
if err != nil {
return err
}
data, err := yaml.Marshal(stagePlaybook)
if err != nil {
return err
}
writeFile(filepath.Join(stageWorkDir, "playbook.yml"), string(data))
// generate inventory with localhost and python in stapel
writeFile(filepath.Join(stageWorkDir, "hosts"), b.assetsHosts())
// generate ansible config for solo mode
writeFile(filepath.Join(stageWorkDir, "ansible.cfg"), b.assetsAnsibleCfg())
// save config dump for pretty errors
stageConfig, err := b.stageConfig(userStageName)
if err != nil {
return err
}
data, err = json.Marshal(stageConfig["dump_config"])
if err != nil {
return err
}
writeFile(filepath.Join(stageWorkDir, "dump_config.json"), string(data))
// Ansible-playbook starter: setup python path without PYTHONPATH environment var
ioutil.WriteFile(
filepath.Join(stageWorkDir, "ansible-playbook"),
[]byte(fmt.Sprintf(
`#!%s
import sys
sys.path.append("%s")
import os
path = os.environ.get('PATH', '')
prepend_path = os.environ.get('ANSIBLE_PREPEND_SYSTEM_PATH', '')
append_path = os.environ.get('ANSIBLE_APPEND_SYSTEM_PATH', '')
path_components = []
if prepend_path != '':
path_components.append(prepend_path)
if path != '':
path_components.append(path)
if append_path != '':
path_components.append(append_path)
os.environ['PATH'] = os.pathsep.join(path_components)
execfile("%s")
`, stapel.PythonBinPath(), path.Join(b.containerWorkDir(), "lib"), stapel.AnsiblePlaybookBinPath())),
os.FileMode(0o777),
)
stageWorkDirLib := filepath.Join(stageWorkDir, "lib")
if err := mkdirP(stageWorkDirLib); err != nil {
return err
}
// crypt.py hack
// TODO should be in stapel image
writeFile(filepath.Join(stageWorkDirLib, "crypt.py"), b.assetsCryptPy())
// sitecustomize with mocks
writeFile(filepath.Join(stageWorkDirLib, "sitecustomize.py"), b.assetsSiteCustomizePy())
callbackPackageDir := filepath.Join(stageWorkDirLib, "callback")
if err := mkdirP(callbackPackageDir); err != nil {
return err
}
writeFile(filepath.Join(callbackPackageDir, "__init__.py"), b.assetsCallbackInitPy())
if livePyPath, exist := os.LookupEnv("WERF_DEBUG_ANSIBLE_LIVE_PY_PATH"); exist {
// hardlink a local live.py into workdir to ease ansible callback development
er := os.Link(livePyPath, filepath.Join(callbackPackageDir, "live.py"))
if er != nil {
return er
}
} else {
writeFile(filepath.Join(callbackPackageDir, "live.py"), b.assetsCallbackLivePy())
}
// add werf specific stdout callback for ansible
if werfPyPath, exist := os.LookupEnv("WERF_DEBUG_ANSIBLE_WERF_PY_PATH"); exist {
// hardlink to a local live.py into workdir to ease ansible callback development
er := os.Link(werfPyPath, filepath.Join(callbackPackageDir, "werf.py"))
if er != nil {
return er
}
} else {
writeFile(filepath.Join(callbackPackageDir, "werf.py"), b.assetsCallbackWerfPy())
}
werfPackageDir := filepath.Join(stageWorkDirLib, "werf")
if err := mkdirP(werfPackageDir); err != nil {
return err
}
writeFile(filepath.Join(werfPackageDir, "__init__.py"), b.assetsWerfInitPy())
writeFile(filepath.Join(werfPackageDir, "live_stdout.py"), b.assetsWerfLiveStdoutPy())
writeFile(filepath.Join(werfPackageDir, "tee_popen.py"), b.assetsWerfTeePopenPy())
return nil
}
func (b *Ansible) stagePlaybook(userStageName string) ([]map[string]interface{}, error) {
playbook := map[string]interface{}{
"hosts": "all",
"gather_facts": "no",
}
stageConfig, err := b.stageConfig(userStageName)
if err != nil {
return nil, err
}
playbook["tasks"] = stageConfig["tasks"]
playbooks := []map[string]interface{}{playbook}
return playbooks, nil
}
// query tasks from ansible config
// create dump_config structure
// returns structure:
// { 'tasks' => [array of tasks for stage],
// 'dump_config' => {
// 'dump_config_doc' => 'dump of doc',
// 'dump_config_sections' => {'task_0'=>'dump for task 0', 'task_1'=>'dump for task 1', ... }}
func (b *Ansible) stageConfig(userStageName string) (map[string]interface{}, error) {
dumpConfigSections := map[string]interface{}{}
var tasks []interface{}
for ind, ansibleTask := range b.stageTasks(userStageName) {
task, err := util.InterfaceToMapStringInterface(ansibleTask.Config)
if err != nil {
return nil, err
}
var tags []string
if _, ok := task["tags"]; ok {
if val, ok := task["tags"].(string); ok {
tags = append(tags, val)
} else if tags, err = util.InterfaceToStringArray(task["tags"]); err != nil {
return nil, err
}
}
dumpTag := fmt.Sprintf("task_%d", ind)
tags = append(tags, dumpTag)
task["tags"] = tags
dumpConfigSections[dumpTag] = ansibleTask.GetDumpConfigSection()
tasks = append(tasks, task)
}
dumpConfig := map[string]interface{}{
"dump_config_doc": b.config.GetDumpConfigSection(),
"dump_config_sections": dumpConfigSections,
}
result := map[string]interface{}{
"dump_config": dumpConfig,
"tasks": tasks,
}
return result, nil
}
func (b *Ansible) stageHostTmpDir(userStageName string) (string, error) {
p := filepath.Join(b.extra.TmpPath, fmt.Sprintf("ansible-tmpdir-%s", userStageName))
if err := mkdirP(filepath.Join(p, "local")); err != nil {
return "", err
}
if err := mkdirP(filepath.Join(p, "remote")); err != nil {
return "", err
}
return p, nil
}
func (b *Ansible) containerWorkDir() string {
return path.Join(b.extra.ContainerWerfPath, "ansible-workdir")
}
func (b *Ansible) containerTmpDir() string {
return path.Join(b.extra.ContainerWerfPath, "ansible-tmpdir")
}
func mkdirP(path string) error {
return os.MkdirAll(path, os.FileMode(0o775))
}
func writeFile(path, content string) error {
return ioutil.WriteFile(path, []byte(content), os.FileMode(0o664))
}