-
Notifications
You must be signed in to change notification settings - Fork 6
/
ansiblew
executable file
·257 lines (185 loc) · 7.48 KB
/
ansiblew
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
#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import with_statement
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import time
import urllib2
from contextlib import closing
DEFAULT_ANSIBLE_VERSION = "2.2.0.0"
DEFAULT_VIRTUALENV_VERSION = "15.1.0"
VERBOSE_LOGGING = os.getenv("ANSIBLEW_VERBOSE") in ("1", "true")
ANSIBLE_COMMAND = os.getenv("ANSIBLEW_COMMAND", "ansible-playbook")
def die(*args):
"""Prints the given messages and exits the application with an error."""
print(*args, file=sys.stderr)
sys.exit(1)
def info(*args):
print(*args)
def debug(*args):
if VERBOSE_LOGGING:
print(*args)
def load_config():
filename = os.path.join(os.path.dirname(sys.argv[0]), "ansiblew.json")
if not os.path.exists(filename):
die("Could not find config file 'ansiblew.json'")
try:
with open(filename, "rb") as fp:
content = fp.read()
config = json.loads(content)
# create a canonicalized hash
h = hashlib.md5()
h.update(json.dumps(config, sort_keys=True))
return config, h.hexdigest()[:8]
except OSError:
die("Could not load config file 'ansiblew.json'")
def find_workspace():
workspace = os.path.expanduser("~/.ansiblew")
if "~" in workspace:
workspace = "/tmp/ansiblew"
return workspace
def mkdirs(path):
if not os.path.exists(path):
debug("Make directory at", path)
os.makedirs(path)
def download(url):
info("Download", url)
with closing(urllib2.urlopen(url)) as fp:
return fp.read()
def check_call(*args, **kwargs):
fail_on_error = kwargs.pop("fail_on_error", True)
capture_output = kwargs.pop("capture_output", False)
# check if we need to send input to the process
stdin = kwargs.pop("stdin", None)
if stdin is not None:
kwargs["stdin"] = subprocess.PIPE
if capture_output:
kwargs["stdout"] = subprocess.PIPE
kwargs["stderr"] = subprocess.STDOUT
if capture_output and stdin:
raise ValueError("stdin can not be used with capture_output")
debug("Executing command", " ".join(args))
proc = subprocess.Popen(args, **kwargs)
output = ""
if capture_output:
lines = []
for line in proc.stdout:
lines.append(line)
sys.stdout.write(line)
output = "".join(lines)
else:
# wait for the process to finish
proc.communicate(stdin)
# check if the process finished with an error
retcode = proc.poll()
if retcode and fail_on_error:
die("Command %s failed with return code %d" % (args[0], retcode))
return retcode, output
def atomically(func):
def wrapper(target, *args, **kwargs):
if os.path.exists(target):
debug("Previous target found in", target)
return
temp_target = target + ".%d" % (int(1000 * time.time()) % 10000)
try:
mkdirs(temp_target)
# call real code
func(temp_target, *args, **kwargs)
try:
# atomic rename, might fail if someone else created the new directory concurrently
os.rename(temp_target, target)
except OSError:
pass
finally:
if os.path.exists(temp_target):
debug("Cleanup temporary directory at", temp_target)
shutil.rmtree(temp_target, ignore_errors=True)
# double check if directory exists now.
if not os.path.exists(target):
die("Renaming of temporary directory to %s failed" % target)
return wrapper
@atomically
def install_virtualenv(target, url):
tar_content = download(url)
check_call("tar", "xz", "--strip-components=1", stdin=tar_content, cwd=target)
def analyze_install_error_output(output):
"""Analyzes the error output, prints a message and dies."""
packages = []
if "include <pyconfig.h>" in output:
packages.append("python2-devel")
if "include <ffi.h>" in output:
packages.append("libffi-devel")
if "include <openssl" in output:
packages.append("openssl-devel")
if packages:
die("\n\n[ERROR] Looks like packages are missing. Try: yum install %s" % " ".join(packages))
else:
die("\n\n[ERROR] Ansible could not be installed for some reason. Check log above.")
@atomically
def install_ansible(target, virtualenv_binary, ansible_version, extra_packages):
info("Create virtualenv in", target)
check_call(virtualenv_binary, target)
info("Install ansible in", target)
python = os.path.join(target, "bin", "python")
pip = os.path.join(target, "bin", "pip")
rc, output = check_call(python, pip, "install", "https://releases.ansible.com/ansible/ansible-%s.tar.gz" % ansible_version,
capture_output=True, fail_on_error=False)
if rc:
raise analyze_install_error_output(output)
if extra_packages:
info("Install extra packages")
check_call(python, pip, "install", *extra_packages)
info("Make venv relocatable to move it into its final place")
check_call(virtualenv_binary, "--relocatable", target)
info("Verify that the installed ansible executes")
ansible = os.path.join(target, "bin", "ansible")
check_call(ansible, "--version")
@atomically
def install_vars_plugins(target):
info("Installing vars plugin to set ansible_python_interpreter.")
with open(target + "/interpreter.py", "wb") as fp:
fp.write(re.sub(r'^\s+[|] ', '', """
| class VarsModule(object):
| def __init__(self, *args):
| pass
| def run(self, *args, **kwargs):
| return {'ansible_python_interpreter': 'python'}
""", 0, re.MULTILINE))
def update_wrapper():
"""Update the wrapper in place."""
content = download("https://raw.githubusercontent.com/flachnetz/ansiblew/master/ansiblew")
info("Replacing script at %s with new version" % sys.argv[0])
with open(sys.argv[0], "wb") as fp:
fp.write(content)
def main():
if len(sys.argv) == 2 and sys.argv[1] == "update":
return update_wrapper()
config, config_hash = load_config()
extra_requirements = config.get("requirements", [])
ansible_version = config.get("ansible_version", DEFAULT_ANSIBLE_VERSION)
venv_version = config.get("virtualenv_version", DEFAULT_VIRTUALENV_VERSION)
debug("Run with virtualenv v%s, ansible v%s, packages=%s" % (
venv_version, ansible_version, ", ".join(extra_requirements)))
# where to put files
workspace = os.path.join(find_workspace(), ansible_version, config_hash)
workspace_venv = os.path.join(workspace, "venv")
workspace_ansible = os.path.join(workspace, "ansible")
# download and extract virtualenv
install_virtualenv(workspace_venv, "https://github.com/pypa/virtualenv/archive/%s.tar.gz" % venv_version)
# install ansible in a virtualenv
virtualenv = os.path.join(workspace_venv, "virtualenv.py")
install_ansible(workspace_ansible, virtualenv, ansible_version, extra_requirements)
workspace_vars = os.path.join(workspace, "vars_plugins")
install_vars_plugins(workspace_vars)
os.putenv("ANSIBLE_VARS_PLUGINS", workspace_vars)
# replace this process with the target process using os.exec
ansible = os.path.join(workspace_ansible, "bin", ANSIBLE_COMMAND)
os.execv(ansible, sys.argv)
if __name__ == '__main__':
sys.exit(main() or 0)