-
Notifications
You must be signed in to change notification settings - Fork 0
/
salt-wrapper.py
392 lines (350 loc) · 12.9 KB
/
salt-wrapper.py
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/python
import sys
import argparse
import json
import salt.client
import requests
import socket
import random
import os
import yaml
import time
# disable ssl warnings...
#requests.packages.urllib3.disable_warnings()
parser = argparse.ArgumentParser(description='SaltStack HA helper script used \
for registering and running orchestration agains multiple masters')
parser.add_argument('function', help='The function to be run (i.e. register or \
orchestrate_heat or orchestrate_vra)')
parser.add_argument('username', help='The REST API endpoint authentication \
username', default='', nargs='?')
parser.add_argument('password', help='The REST API endpoint authentication \
password', default='', nargs='?')
parser.add_argument('-e', '--environment', help='The environment in which the \
orchestration to be run (i.e. dev, test, prod, etc...)', default='base')
parser.add_argument('-a', '--automation', help='The pattern which to be run \
(i.e. three_tier_lamp, etc...)')
parser.add_argument('-o', '--orchestration', help='The pattern orchestration \
to be run (i.e. dev, test, prod, etc...)')
parser.add_argument('-p', '--pillar', help='The pillar data to use with the \
orchestration', action='append', type=lambda kv: kv.split("="),
dest='pillar')
parser.add_argument('-w', '--wait_url', help='The WaitCondition URL to signal \
after the orchestration is finished')
parser.add_argument('-t', '--token', help='The WaitCondition Authentication \
Token to signal after the orchestration is finsihed')
parser.add_argument('-s', '--stack_id', help='The ID of the heat stack that \
the orchestration should be run against')
parser.add_argument('-f', '--pillar_file', help='The file containing pillar \
data to use with the orchestration (in YAML format)')
parser.add_argument('-z', '--sleep_time', help='How much time to sleep between \
pushing stack pillar to git and executing orchestration \
(in seconds)', default=60)
parser.add_argument('-d', '--port', help='The REST API endpoint port to use \
(default is 8000)', default=8000)
parser.add_argument('-b', '--flask_port', help='The Flask Application port', \
default=5000)
args = parser.parse_args()
# Some defaults
if os.name == 'nt':
minion_opts = salt.config.minion_config('C:\salt\conf\minion')
else:
minion_opts = salt.config.minion_config('/etc/salt/minion')
metadata_file = 'http://169.254.0.1:8080/metadata.yaml'
def __get_token__(master, username, password):
print "Authenticating to:", master
url = "https://{0}:{1}/login".format(master, args.port)
headers = {
"Accept": "application/json"
}
postdata = {
"username": username,
"password": password,
"eauth": "pam"
}
try:
req = requests.post(url=url, headers=headers,
data=postdata, verify=False)
if req.status_code == 200:
return req.json()["return"][0]["token"]
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __remove_token__(master, token):
print "Deauthenticating"
url = "https://{0}:{1}/logout".format(master, args.port)
headers = {
"Accept": "application/json",
"X-Auth-Token": token
}
try:
req = requests.post(url=url, headers=headers, verify=False)
if req.status_code == 200:
return True
else:
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __accept_client__(master, token, minion):
print "Accepting client key for:", minion
url = "https://{0}:{1}/hook/minions/key/accept".format(master, args.port)
headers = {
"Accept": "application/json",
"X-Auth-Token": token
}
postdata = {
"minion": minion
}
try:
req = requests.post(url=url, headers=headers,
data=postdata, verify=False)
if req.status_code == 200:
return True
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __check_client__(master, token, minion):
print "Checking if client key for:", minion, "is accepted"
url = "https://{0}:{1}/keys".format(master, args.port)
headers = {
"Accept": "application/json",
"X-Auth-Token": token
}
try:
req = requests.get(url=url, headers=headers, verify=False)
if req.status_code == 200:
if minion in req.json()["return"]["minions"]:
return "accepted"
elif minion in req.json()["return"]["minions_pre"]:
return "unaccepted"
else:
return "nonexistent"
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __return_working_masters__(masters):
working_masters = []
for master in masters:
try:
sock = socket.create_connection((master, args.port), timeout=10)
working_masters.append(master)
except:
print "Having troubles connnecting to", master
return(working_masters)
def __inject_sys_state_if_needed(master):
url = "http://{0}:{1}/add_sys_state/".format('134.168.48.59', args.flask_port)
headers = {
"Accept": "application/yaml"
}
postdata = {
"pattern": args.automation,
"orchestration": args.orchestration
}
try:
req = requests.post(url=url, headers=headers,
json=postdata, verify=False)
if req.status_code == 200:
time.sleep(float(args.sleep_time))
return True
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __run_heat_orchestration__(master, token, environment,
automation, orchestration):
if args.pillar is not None:
pillar = dict(args.pillar)
else:
pillar = 'None'
if args.pillar_file is not None:
__load_pillar_from_file__(args.pillar_file, master)
__inject_sys_state_if_needed(master)
if args.wait_url is not None:
wait_url = args.wait_url
if args.token is not None:
wait_token = args.token
else:
wait_token = 'None'
if args.stack_id is not None:
stack_id = args.stack_id
__refresh_stack_pillar__(master, token, args.stack_id)
url = "https://{0}:{1}/hook/cmd/run_heat".format(master, args.port)
headers = {
"Accept": "application/json",
"X-Auth-Token": token
}
postdata = {
"environment": environment,
"automation": automation,
"orchestration": orchestration,
"pillar": pillar,
"wait_url": wait_url,
"token": wait_token,
"stack_id": stack_id
}
try:
req = requests.post(url=url, headers=headers,
json=postdata, verify=False)
if req.status_code == 200:
return True
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __run_vra_orchestration__(master, token, environment,
automation, orchestration):
if args.pillar is not None:
pillar = dict(args.pillar)
else:
pillar = 'None'
if args.pillar_file is not None:
__load_pillar_from_file__(args.pillar_file, master)
if args.stack_id is not None:
stack_id = args.stack_id
__refresh_stack_pillar__(master, token, args.stack_id)
url = "https://{0}:{1}/hook/cmd/run_vra".format(master, args.port)
headers = {
"Accept": "application/json",
"X-Auth-Token": token
}
postdata = {
"environment": environment,
"automation": automation,
"orchestration": orchestration,
"pillar": pillar,
"stack_id": stack_id
}
try:
req = requests.post(url=url, headers=headers,
json=postdata, verify=False)
if req.status_code == 200:
return True
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __load_pillar_from_file__(file, master):
with open(file, 'r') as stream:
pillar_dict = yaml.load(stream)
url = "http://{0}:{1}/create_pillar_data/".format('134.168.48.59', args.flask_port)
headers = {
"Accept": "application/yaml"
}
postdata = {
"stackid": args.stack_id,
"env_var": args.environment,
"pillardata_dict": pillar_dict,
"pattern": args.automation
}
try:
req = requests.post(url=url, headers=headers,
json=postdata, verify=False)
if req.status_code == 200:
time.sleep(float(args.sleep_time))
return True
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __refresh_stack_pillar__(master, token, stack_id):
# sends a call to the salt/netapi/hook/refresh_pillar reactor
# this refreshes pillar data for the whole stack
# wait for the previous reactor (fileserver.update) to finish
time.sleep(float(args.sleep_time))
print "Sendind refresh pillar signal to master: " + master
print "Stack id: " + stack_id
url = "https://{0}:{1}/hook/refresh_pillar".format(master, args.port)
headers = {
"Accept": "application/json",
"X-Auth-Token": token
}
postdata = {
"stack_id": stack_id
}
try:
req = requests.post(url=url, headers=headers,
data=postdata, verify=False)
if req.status_code == 200:
# wait for reactor to finish
time.sleep(float(args.sleep_time))
return True
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def __fetch_metadata__():
print "Fetching metadata"
url = metadata_file
try:
req = requests.get(url=url, verify=False)
if req.status_code == 200:
metadata = yaml.load(req.content)
args.username = metadata["salt_api_user"]
args.password = metadata["salt_api_password"]
args.port = metadata["salt_api_port"]
else:
print req.text
return False
except (requests.exceptions.RequestException) as e:
print e.message
return False
def main():
'''main function'''
if args.username == '' or args.password == '':
print "Username and/or password are empty, fetching metadata file"
__fetch_metadata__()
if args.function == 'register':
print 'Registering...'
working_masters = __return_working_masters__(minion_opts['master'])
for master in working_masters:
token = __get_token__(master, args.username, args.password)
if __check_client__(master, token,
minion_opts['id']) == "accepted":
print 'Minion key is accepted'
elif __check_client__(master, token,
minion_opts['id']) == "unaccepted":
__accept_client__(master, token, minion_opts['id'])
else:
print "Minion key is not yet registered"
__remove_token__(master, token)
elif args.function == 'orchestrate_heat':
print 'Orchestrating...'
master = random.choice(__return_working_masters__
(minion_opts['master']))
token = __get_token__(master, args.username, args.password)
__run_heat_orchestration__(master, token, args.environment,
args.automation, args.orchestration)
__remove_token__(master, token)
elif args.function == 'orchestrate_vra':
print 'Orchestrating...'
master = random.choice(__return_working_masters__
(minion_opts['master']))
token = __get_token__(master, args.username, args.password)
__run_vra_orchestration__(master, token, args.environment,
args.automation, args.orchestration)
__remove_token__(master, token)
else:
print 'Try again...'
parser.print_help()
sys.exit(2)
if __name__ == '__main__':
main()