/
ansiblecall.go
326 lines (273 loc) · 8.05 KB
/
ansiblecall.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
/*
Local caller to call any Ansible module on the current machine.
Used by a client.
*/
package nanocms_callers
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/karrick/godirwalk"
"github.com/thoas/go-funk"
wzlib_traits "github.com/infra-whizz/wzlib/traits"
wzlib_traits_attributes "github.com/infra-whizz/wzlib/traits/attributes"
wzlib_utils "github.com/infra-whizz/wzlib/utils"
)
func NewAnsibleLocalModuleCaller(modulename string) *AnsibleModule {
am := new(AnsibleModule)
am.modType = 0
am.stateRoots = make([]string, 0)
am.name = strings.ToLower(strings.TrimPrefix(modulename, "ansible."))
am.args = map[string]interface{}{}
am.pyexe = []string{"/usr/bin/python3"}
am.chroot = "/"
return am
}
// SetChroot sets another root where an Ansible module should be ran.
// Chrooted Ansible module is called via PCE (Python Chroot Executor) wrapper,
// which is embedded inside the binary of this wzd via wzcmslib.
// See wzcmslib/nanorunners/wrappers/pce.py for more details.
//
// Default "/". In this case PCE is not used.
func (am *AnsibleModule) SetChroot(root string) *AnsibleModule {
am.chroot = root
return am
}
// SetStateRoots where module is going to be found.
// NOTE: if the same module is located in a various roots, then first win
func (am *AnsibleModule) SetStateRoots(roots ...string) *AnsibleModule {
am.stateRoots = append(am.stateRoots, roots...)
return am
}
// Prepare PCE
func (am *AnsibleModule) preparePCE() error {
if am.chroot == "/" {
return nil
}
var err error
am.pce, err = ioutil.TempFile("/tmp", ".waka-pce-")
if err != nil {
return err
}
return ioutil.WriteFile(am.pce.Name(), NewWzPyPce().Get("pce"), 0644)
}
func (am *AnsibleModule) removePCE() error {
if am.pce == nil {
return nil
}
if err := os.Remove(am.pce.Name()); err != nil {
return err
}
am.pce.Close()
am.pce = nil
return nil
}
// SetPyInterpreter path, such as "/usr/bin/python3" or "/usr/bin/env python" etc
func (am *AnsibleModule) SetPyInterpreter(pyexe string) *AnsibleModule {
if pyexe == "" {
// Skip, if empty shebang
return am
}
am.pyexe = []string{}
for _, cmd := range strings.Split(pyexe, " ") {
if cmd != "" {
am.pyexe = append(am.pyexe, cmd)
}
}
return am
}
func (am *AnsibleModule) resolvePlatformPath() string {
traits := wzlib_traits.NewWzTraitsContainer()
wzlib_traits_attributes.NewSysInfo().Load(traits)
sysName := traits.Get("os.sysname")
if sysName == nil {
return fmt.Sprintf("generic/%s", traits.Get("arch"))
}
return fmt.Sprintf("%s/%s", sysName, traits.Get("arch"))
}
// Resolve module path in 2.10+ collections style
func (am *AnsibleModule) resolveModulePath() (string, error) {
modPath := ""
platformPath := am.resolvePlatformPath()
for _, stateRoot := range am.stateRoots {
moduleRoot := filepath.Clean(path.Join(stateRoot, "modules"))
suffBinPath := filepath.Clean(path.Join(moduleRoot, "bin", platformPath, strings.ReplaceAll(am.name, ".", "/")))
suffPyPath := filepath.Clean(path.Join(moduleRoot, strings.ReplaceAll(am.name, ".", "/")+".py"))
if err := godirwalk.Walk(moduleRoot, &godirwalk.Options{
Unsorted: true,
FollowSymbolicLinks: true,
Callback: func(pth string, info *godirwalk.Dirent) error {
contentType, _ := wzlib_utils.FileContentTypeByPath(pth)
switch contentType {
case "application/octet-stream":
if strings.HasSuffix(pth, suffBinPath) {
modPath = pth
am.modType = BINARY
return fmt.Errorf("Binary module found")
}
case "text/plain":
if strings.HasSuffix(pth, suffPyPath) {
modPath = pth
am.modType = SCRIPT
return fmt.Errorf("Python module found")
}
}
return nil
},
ErrorCallback: func(pth string, err error) godirwalk.ErrorAction { return godirwalk.SkipNode },
}); err != nil {
break
}
}
modPath = wzlib_utils.RemovePrefix(modPath, am.chroot)
am.GetLogger().Debugf("Ansible module path: %s", modPath)
return modPath, nil
}
// SetArgs sets the key/value arguments
func (am *AnsibleModule) SetArgs(kwargs map[string]interface{}) *AnsibleModule {
for k, v := range kwargs {
am.AddArg(k, v)
}
return am
}
// AddArg adds an argument with key/value
func (am *AnsibleModule) AddArg(key string, value interface{}) *AnsibleModule {
am.args[key] = value
return am
}
// Call Ansible module
func (am *AnsibleModule) Call() (map[string]interface{}, error) {
var ret map[string]interface{}
stdout, stderr, err := am.execModule()
if stderr != "" {
am.GetLogger().Errorf("Ansible call error:\n'%s'", stderr)
}
if err != nil && stdout == "" && stderr == "" {
return nil, err
}
err = json.Unmarshal([]byte(stdout), &ret)
if err != nil {
return nil, err
}
return ret, nil
}
// Execute module.
// There are certain rules how that works:
// - if everything is chrooted, everything runs chrooted.
// - but if "root" is specified, module is NOT ran as chrooted, but parameter just passed to the module
// - If everything is chrooted and "root" specified, its value is overwritten with the current chroot
func (am *AnsibleModule) execModule() (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
// Resolve module and set its type. Based on this, config file is made then
exePath, err := am.resolveModulePath()
if err != nil {
return "", "", err
}
// Create configuration JSON file
cfg, err := am.makeConfigFile()
if err != nil {
return "", "", err
}
defer os.Remove(cfg.Name())
var chrootExit func() error
if am.modType == BINARY {
// should we run module chrooted?
var cnt bool
if am.chroot != "/" {
cnt = !funk.Contains(am.args, "root")
} else {
cnt = false
}
if cnt {
// Run chrooted
conf := new(wzlib_utils.WzContainerParam)
conf.Root = am.chroot
conf.Command = exePath
conf.Args = []string{wzlib_utils.RemovePrefix(cfg.Name(), am.chroot)}
return wzlib_utils.NewWzContainer(conf).Run()
} else {
// Run directly
sh := exec.Command(path.Join(am.chroot, exePath), cfg.Name())
// TODO: Move to a function (this is a code repeat!)
sh.Stdout = &stdout
sh.Stderr = &stderr
err = sh.Run()
if err != nil {
am.GetLogger().Errorf("Module '%s' failed: %s", exePath, err.Error())
}
if chrootExit != nil {
err = chrootExit()
}
return stdout.String(), stderr.String(), err
}
// TODO: Split to two methods!
} else if am.modType == SCRIPT {
// Python module
if err := am.preparePCE(); err != nil {
return "", "", err
}
defer am.removePCE()
var cmd []string
if am.pce != nil {
cmd = append(am.pyexe, am.pce.Name(), "-r", am.chroot, "-c", exePath,
"-j", wzlib_utils.RemovePrefix(cfg.Name(), am.chroot))
} else {
cmd = append(am.pyexe, exePath, cfg.Name())
}
debugMessage := ""
if am.chroot != "/" {
debugMessage = fmt.Sprintf(" (inside: %s)", am.chroot)
}
am.GetLogger().Debugf("Calling Ansible module%s:\n'%s'", debugMessage, strings.Join(cmd, " "))
sh := exec.Command(cmd[0], cmd[1:]...)
sh.Stdout = &stdout
sh.Stderr = &stderr
err = sh.Run()
if err != nil {
am.GetLogger().Errorf("Module '%s' failed: %s", exePath, err.Error())
}
if chrootExit != nil {
err = chrootExit()
}
return stdout.String(), stderr.String(), err
}
return "", "", fmt.Errorf("Module %s was not found", am.name)
}
// Create a temporary config file and return a path to it.
func (am *AnsibleModule) makeConfigFile() (*os.File, error) {
var prefix string
if am.chroot != "/" {
prefix = am.chroot
if funk.Contains(am.args, "root") {
am.args["root"] = am.chroot // asking for root
}
}
f, err := ioutil.TempFile(prefix+"/tmp", "nst-ansible-")
if err != nil {
return nil, err
}
var data []byte
if am.modType == BINARY {
data, err = json.Marshal(am.args)
} else if am.modType == SCRIPT {
data, err = json.Marshal(map[string]interface{}{"ANSIBLE_MODULE_ARGS": am.args})
} else {
panic("An attempt to call an unresolved module type (binary or Ansible-native)")
}
if err != nil {
return nil, err
}
_, err = f.WriteString(string(data))
f.Close()
if err != nil {
os.Remove(f.Name())
}
return f, err
}