Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
328 lines (285 loc) · 11.3 KB
/
Copy pathapp.py
File metadata and controls
328 lines (285 loc) · 11.3 KB
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
327
328
import ansible_runner
from flask import Flask, request
import json
import os
import sys
import yaml
app = Flask(__name__)
KEYS_DIR = "/keys"
FILES_DIR = "/files"
WORK_DIR = "/var/tmp"
ROLES_DIR = os.environ.get("ROLES_DIR", "{}/roles".format(WORK_DIR))
COLLECTIONS_DIR = os.environ.get("COLLECTIONS_DIR", "{}/collections".format(WORK_DIR))
os.environ['ANSIBLE_COLLECTIONS_PATH'] = COLLECTIONS_DIR
os.environ['ANSIBLE_ROLES_PATH'] = ROLES_DIR
CONSTRUCTS = {}
def resolve_role_url(role_url):
if len(role_url) == 0:
return { 'error': "A valid role identifier is required." }
url_components = role_url.split('.')
if len(url_components) == 3:
# url represents role apart of a collection - ensure collection exists locally
# components = 1. namespace, 2. collection, 3. role
if os.path.isdir("{dir}/ansible_collections/{namespace}/{collection}/roles/{role}".format(
dir=COLLECTIONS_DIR,
namespace=url_components[0],
collection=url_components[1],
role=url_components[2]
)):
return { 'message:' "Collection role, {}, exists.".format('.'.join(url_components)) }
install_id = "{namespace}.{collection}".format(namespace=url_components[0], collection=url_components[1])
cmd_args = [
'collection',
"install",
install_id,
"--collections-path",
COLLECTIONS_DIR
]
out, err, rc = ansible_runner.run_command(
executable_cmd='ansible-galaxy',
cmdline_args=cmd_args,
input_fd=sys.stdin
)
if rc == 0:
return { 'message': "{} successfully installed. {}".format(role_url, out) }
else:
return { 'error': err, 'rc': rc }
elif len(url_components) == 2:
# url represents a remote galaxy server role - ensure role exists locally
# components = 1. namespace, 2. role
if os.path.isdir("{}/{}".format(ROLES_DIR, '.'.join(url_components))):
return { 'message:' "Role, {}, exists.".format('.'.join(url_components)) }
install_id = "{namespace}.{role}".format(namespace=url_components[0], role=url_components[1])
cmd_args = [
'role',
"install",
install_id,
"--roles-path",
ROLES_DIR
]
out, err, rc = ansible_runner.run_command(
executable_cmd='ansible-galaxy',
cmdline_args=cmd_args,
input_fd=sys.stdin
)
if rc == 0:
return { 'message': "{} successfully installed. {}".format(role_url, out) }
else:
return { 'error': err, 'rc': rc }
else:
# url likely represents a locally installed role - ensure role exists locally
if os.path.isdir("{}/{}".format(ROLES_DIR, role_url)):
return { 'message:' "Role, {}, exists.".format(role_url) }
return { 'error': "Invalid role url/identifier provided." }
def generate_setup(spec):
with open("{dir}/{id}.yaml".format(dir=WORK_DIR, id=spec['id']), 'w') as f:
for role in spec['setup']:
status = resolve_role_url(role['url'])
if 'error' in status:
return status
role_id = "{dir}/{url}".format(dir=ROLES_DIR, url=role['url'])
if len(role['url'].split('.')) >= 2:
role_id = role['url']
d = [{
"name": "Deploy {role} roles".format(role=role['type']),
"hosts": role['type'],
"roles" : [{
"role": "{id}".format(id=role_id),
"vars": role['properties']
}]
}]
yaml.dump(d, f, default_flow_style=False)
return { 'message': "{id} setup completed successfully.".format(id=spec['id']) }
def generate_inventory(spec):
with open("{dir}/inventory-{id}.yaml".format(dir=WORK_DIR, id=spec['id']), 'w') as f:
i = {"all": {
"hosts": {},
"children": {}
}}
for host in spec['inventory']:
i['all']['hosts'][host['name']] = {
"ansible_host": host['address'],
"ansible_user": host['user']
}
for role in host['roles']:
if not i['all']['children'].get(role):
i['all']['children'][role] = { "hosts": { host['name']: {}} }
else:
i['all']['children'][role]['hosts'][host['name']] = {}
yaml.dump(i, f, default_flow_style=False)
return { 'message': "{id} inventory generation completed successfully.".format(id=spec['id']) }
def load_construct_file(cid):
data = {}
with open("{dir}/{id}.yaml".format(dir=WORK_DIR, id=cid)) as file:
data = yaml.safe_load(file)
return data
def update_constructs_datastore(construct):
CONSTRUCTS[construct['id']] = construct['setup']
@app.route("/v1/key", methods=['POST'])
def key():
if 'ssh' in request.files:
file = request.files['ssh']
path = "{}/{}".format(KEYS_DIR, file.filename)
if not os.path.exists(KEYS_DIR):
os.makedirs(KEYS_DIR)
file.save(path)
os.chmod(path, 0o600)
return { 'message': "SSH key, {key}, successfully uploaded.".format(key=file.filename) }
return { 'error': "No SSH key was provided for upload." }
@app.route("/v1/files", methods=['POST'])
def files():
if 'file' in request.files:
file = request.files['file']
path = "{}/{}".format(FILES_DIR, file.filename)
if not os.path.exists(FILES_DIR):
os.makedirs(FILES_DIR)
file.save(path)
os.chmod(path, 0o600)
return { 'message': "File, {f}, successfully uploaded.".format(f=file.filename) }
return { 'error': "No File was provided for upload." }
@app.route("/v1/view/<cid>", methods=['GET'])
def view(cid):
if request.method == 'GET':
cmd_args = [
'--inventory',
"{dir}/inventory-{id}.yaml".format(dir=WORK_DIR, id=cid),
'--module-name',
'setup'
]
if 'ssh_key' in request.args and os.path.exists("{}/{}".format(KEYS_DIR, request.args['ssh_key'])):
cmd_args.extend(["--private-key", "{}/{}".format(KEYS_DIR, request.args['ssh_key'])])
else:
cmd_args.append("--ask-pass")
if 'target' in request.args:
cmd_args.append(request.args['target'])
else:
cmd_args.append('all')
if 'hosts' in request.args:
cmd_args.extend(['--limit', request.args['hosts']])
if 'filter' in request.args:
cmd_args.extend(['-a', "filter='{}'".format(request.args['filter'])])
out, err, rc = ansible_runner.run_command(
executable_cmd='ansible',
cmdline_args=cmd_args,
input_fd=sys.stdin
)
if rc == 0:
trim_start = out.find('{')
out = out[trim_start:]+'}'
return {
'id': cid,
'message': "{id} successfully viewed.".format(id=cid),
'view': json.loads(out.replace("\n", "").replace(" ", "")),
}
else:
return {
'id': cid,
'error': err,
'result': rc
}
@app.route("/v1/run/<cid>", methods=['POST'])
def run(cid):
if request.method == 'POST':
if not request.get_json(silent=True) and 'run_args' not in request.get_json(force=True):
return {
'id': cid,
'error': 'Specification of command "run_args" is required within request data - None provided.',
'result': ''
}
data = request.get_json(force=True)
cmd_args = [
'--inventory',
"{dir}/inventory-{id}.yaml".format(dir=WORK_DIR, id=cid),
'--args',
data['run_args']
]
if 'ssh_key' in request.args and os.path.exists("{}/{}".format(KEYS_DIR, request.args['ssh_key'])):
cmd_args.extend(["--private-key", "{}/{}".format(KEYS_DIR, request.args['ssh_key'])])
else:
cmd_args.append("--ask-pass")
if 'target' in request.args:
cmd_args.append(request.args['target'])
else:
cmd_args.append('all')
if 'hosts' in request.args:
cmd_args.extend(['--limit', request.args['hosts']])
if 'module' in request.args:
cmd_args.extend(['--module-name', request.args['module']])
out, err, rc = ansible_runner.run_command(
executable_cmd='ansible',
cmdline_args=cmd_args,
input_fd=sys.stdin
)
trim_start = out.find('>>')
out = out[trim_start:]
if rc == 0:
return {
'id': cid,
'message': "{cmd} sucessfully executed.".format(cmd=data['run_args']),
# compress/cleanse output for simpler downstream parsing
'result': out.strip().replace(" ", "")
}
else:
return {
'id': cid,
'error': err,
'result': rc
}
@app.route("/v1/construct", methods=['GET', 'POST'])
def construct():
if request.method == 'POST':
data = request.get_json(force=True)
generate_inventory(data)
status = generate_setup(data)
if 'error' in status:
return status
cmd_args = [
'{dir}/{id}.yaml'.format(dir=WORK_DIR, id=data['id']),
'-i',
'{dir}/inventory-{id}.yaml'.format(dir=WORK_DIR, id=data['id']),
'-v'
]
if 'ssh_key' in request.args and os.path.exists("/{}/{}".format(KEYS_DIR, request.args['ssh_key'])):
cmd_args.extend(["--private-key", "{}/{}".format(KEYS_DIR, request.args['ssh_key'])])
else:
cmd_args.append("--ask-pass")
_, err, rc = ansible_runner.run_command(
executable_cmd='ansible-playbook',
cmdline_args=cmd_args,
input_fd=sys.stdin,
output_fd=sys.stdout,
error_fd=sys.stderr,
)
if rc == 0:
update_constructs_datastore(data)
return {
'id': data['id'],
'message': "{id}, successfully constructed.".format(id=data['id']),
'construct': load_construct_file(data['id']),
}
else:
return {
'id': data['id'],
'error': err,
'result': rc
}
elif request.method == 'GET':
if 'id' in request.args and request.args['id'] in CONSTRUCTS:
return {
'id': request.args['id'],
'message': "{id} successfully retrieved.".format(id=request.args['id']),
'construct': load_construct_file(request.args['id'])
}
elif 'id' in request.args and request.args['id'] not in CONSTRUCTS:
return {
'id': request.args['id'],
'error': "Retrieval failed - construct does not exist.".format(id=request.args['id'])
}
else:
ret = {
'message': "Constructs successfully retrieved",
'constructs': []
}
for k in CONSTRUCTS.keys():
ret['constructs'].append({ k: load_construct_file(k) })
return ret