This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
deploy.py
503 lines (447 loc) · 14.8 KB
/
deploy.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"""
Deploy methods for Atmosphere
"""
import os
import re
import json
import subprocess
from threepio import logger, deploy_logger
from django.conf import settings
from atmosphere.settings import secrets
from core.core_logging import create_instance_logger
from core.models import Provider, Identity, Instance, SSHKey, AtmosphereUser
from service.exceptions import AnsibleDeployException
def ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=[],
limit_hosts={},
extra_vars={},
raise_exception=True,
debug=False
):
"""
Use service.ansible to deploy to an instance.
"""
if not check_ansible():
return []
# Expecting to be path-relative to the playbook path, so use basename
if type(limit_playbooks) == str:
limit_playbooks = limit_playbooks.split(",")
if type(limit_playbooks) != list:
raise Exception(
"Invalid 'limit_playbooks' argument (%s). Expected List" %
limit_playbooks
)
if limit_playbooks == []:
limit_playbooks = sorted(os.listdir(playbooks_dir))
logger = create_instance_logger(
deploy_logger, instance_ip, username, instance_id
)
hostname = build_host_name(instance_id, instance_ip)
configure_ansible(debug=debug)
if not limit_hosts:
if hostname:
limit_hosts = hostname
else:
limit_hosts = instance_ip
host_file = settings.ANSIBLE_HOST_FILE
identity = Identity.find_instance(instance_id)
if identity:
time_zone = identity.provider.timezone
extra_vars.update({
"TIMEZONE": time_zone,
})
shared_users = list(
AtmosphereUser.users_for_instance(instance_id).values_list(
'username', flat=True
)
)
if not shared_users:
shared_users = [username]
if username not in shared_users:
shared_users.append(username)
extra_vars.update({
"SHARED_USERS": shared_users,
})
extra_vars.update({
"ATMOUSERNAME": username,
})
extra_vars.update({
"INSTANCE_UUID": instance_id,
})
playbook_results = execute_playbooks(
playbooks_dir,
host_file,
extra_vars,
limit_hosts,
logger=logger,
limit_playbooks=limit_playbooks
)
if raise_exception:
raise_playbook_errors(
playbook_results, instance_id, instance_ip, hostname
)
return playbook_results
def ready_to_deploy(instance_ip, username, instance_id):
"""
Use service.ansible to deploy to an instance.
"""
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'utils')
extra_vars = {
"SSH_IDENTITY_FILE": settings.ATMOSPHERE_PRIVATE_KEYFILE,
}
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=['check_networking.yml'],
extra_vars=extra_vars
)
def deploy_mount_volume(
instance_ip,
username,
instance_id,
device,
mount_location=None,
device_type='ext4'
):
"""
Use service.ansible to mount volume to an instance.
"""
extra_vars = {
"VOLUME_DEVICE": device,
"VOLUME_MOUNT_LOCATION": mount_location,
"VOLUME_DEVICE_TYPE": device_type,
}
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'instance_actions')
limit_playbooks = ['mount_volume.yml']
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=limit_playbooks,
extra_vars=extra_vars
)
def deploy_unmount_volume(instance_ip, username, instance_id, device):
"""
Use service.ansible to mount volume to an instance.
"""
extra_vars = {"src": device}
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'instance_actions')
limit_playbooks = ['unmount_volume.yml']
playbook_results = ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=limit_playbooks,
extra_vars=extra_vars,
raise_exception=False
)
return playbook_results
def deploy_prepare_snapshot(instance_ip, username, instance_id, extra_vars={}):
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'imaging')
limit_playbooks = ['prepare_instance_snapshot.yml']
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=limit_playbooks,
extra_vars=extra_vars
)
def deploy_check_volume(
instance_ip, username, instance_id, device, device_type='ext4'
):
"""
Use ansible to check if an attached volume has run mkfs.
"""
extra_vars = {
"VOLUME_DEVICE": device,
"VOLUME_DEVICE_TYPE": device_type,
}
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'instance_actions')
limit_playbooks = ['check_volume.yml']
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=limit_playbooks,
extra_vars=extra_vars
)
def instance_deploy(instance_ip, username, instance_id, limit_playbooks=[]):
"""
Use service.ansible to deploy to an instance.
"""
extra_vars = {
"SSH_IDENTITY_FILE": settings.ATMOSPHERE_PRIVATE_KEYFILE,
"VNCLICENSE": secrets.ATMOSPHERE_VNC_LICENSE,
}
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'instance_deploy')
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks=limit_playbooks,
extra_vars=extra_vars
)
def user_deploy(instance_ip, username, instance_id, first_deploy=True):
"""
Use service.ansible to deploy to an instance.
#NOTE: This method will _NOT_ work if you do not run instance deployment *FIRST*!
# In order to add user-ssh keys to root, you will need root access to the VM that is *not* configured in this playbook.
"""
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'user_deploy')
instance = Instance.objects.get(provider_alias=instance_id)
image_scripts = instance.source.providermachine.application_version.boot_scripts.all(
)
instance_scripts = instance.scripts.all()
scripts = image_scripts.union(instance_scripts)
# Example 'all members' strategy:
if not instance.project:
raise Exception(
"Expected this instance to have a project, found None: %s" %
instance
)
group = instance.project.owner
group_ssh_keys = SSHKey.keys_for_group(group)
user_keys = [k.pub_key for k in group_ssh_keys]
async_scripts, deploy_scripts = [], []
for script in scripts:
if not (first_deploy or script.run_every_deploy):
continue
if script.wait_for_deploy:
deploy_scripts.append(script)
else:
async_scripts.append(script)
format_script = lambda s: {"name": s.get_title_slug(), "text": s.get_text()}
extra_vars = {
"USERSSHKEYS": user_keys,
"ASYNC_SCRIPTS": map(format_script, async_scripts),
"DEPLOY_SCRIPTS": map(format_script, deploy_scripts)
}
playbook_results = ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
extra_vars=extra_vars,
raise_exception=False
)
hostname = build_host_name(instance_id, instance_ip)
# An error has occurred during deployment!
# If the failure was not related to users boot-scripts,
# handle as a generic ansible failure.
return raise_playbook_errors(
playbook_results, instance_id, instance_ip, hostname
)
def run_utility_playbooks(
instance_ip,
username,
instance_id,
limit_playbooks=[],
raise_exception=True
):
"""
Use service.ansible to deploy utility_playbooks to an instance.
'limit_playbooks' is a list of strings
that should match the filename you wish to include
(Ex: check_networking.yml)
"""
playbooks_dir = os.path.join(settings.ANSIBLE_PLAYBOOKS_DIR, 'utils')
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks,
raise_exception=raise_exception
)
def select_install_playbooks(install_action):
"""
This function would take an install_action, say:
`apache` or `nginx` or `R` or `Docker`
and return as a result, the list of playbooks required to make it happen.
"""
raise Exception("Unknown installation action:%s" % install_action)
def user_deploy_install(
instance_ip, username, instance_id, install_action, install_args
):
"""
Placeholder function to show how a user-initialized install from Troposphere might be handled on the backend.
"""
playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
playbooks_dir = os.path.join(playbooks_dir, 'user_customizations')
limit_playbooks = select_install_playbooks(install_action)
return ansible_deployment(
instance_ip,
username,
instance_id,
playbooks_dir,
limit_playbooks,
extra_vars=install_args
)
def execute_playbooks(
playbook_dir, host_file, extra_vars, host, logger=None, limit_playbooks=[]
):
# Force requirement of a logger for 2.0 playbook runs
if not logger:
logger = deploy_logger
inventory_dir = os.path.join(settings.ANSIBLE_ROOT, "ansible", "inventory")
# Run playbooks
results = []
for pb in limit_playbooks:
pb_path = os.path.join(playbook_dir, pb)
logger.info("Executing playbook {}".format(pb_path))
args = [
"ansible-playbook", "--inventory={}".format(inventory_dir),
"--limit={}".format(host),
"--extra-vars={}".format(json.dumps(extra_vars)), pb_path
]
logger.info("args to ansible-playbook: {}".format(args))
proc = subprocess.Popen(
args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
ansible_output, _ = proc.communicate() # capture output
logger.debug(ansible_output)
logger.info("playbook {} result {}".format(pb, proc.returncode))
results.append(proc.returncode)
if results[-1] != 0:
break
return results
def check_ansible():
"""
If the playbooks and roles directory exist then ANSIBLE_* settings
variables are likely configured.
"""
exists = os.path.exists(settings.ANSIBLE_PLAYBOOKS_DIR) and\
os.path.exists(settings.ANSIBLE_ROLES_PATH)
if not exists:
logger.warn(
"Ansible is not configured. Verify your "
"ANSIBLE_* settings variables"
)
return exists
def configure_ansible(debug=False):
"""
Configure ansible to work with service.ansible and subspace.
"""
os.environ["ANSIBLE_DEBUG"] = "true" if debug else "false"
if settings.ANSIBLE_CONFIG_FILE:
os.environ["ANSIBLE_CONFIG"] = settings.ANSIBLE_CONFIG_FILE
os.environ[
"PYTHONOPTIMIZE"
] = "1" #NOTE: Required to run ansible2 + celery + prefork concurrency
def build_host_name(instance_id, ip):
"""
Build host name from the configuration in settings
See:
* INSTANCE_HOSTNAMING_FORMAT
* INSTANCE_HOSTNAMING_DOMAIN (Required if you use `%(domain)s`)
"""
try:
provider = Provider.objects.get(
instancesource__instances__provider_alias=instance_id
)
hostname_format_str = provider.cloud_config['deploy']['hostname_format']
except Provider.DoesNotExist:
logger.warn(
"Using an instance %s that is *NOT* in your database. Cannot determine hostnaming format. Using IP address as hostname."
)
return raw_hostname(ip)
except (KeyError, TypeError):
logger.warn(
"Cloud config ['deploy']['hostname_format'] is missing -- using IP address as a hostname."
)
return raw_hostname(ip)
# Convert IP into a dictionary broken into octets
hostnaming_format_map = create_hostnaming_map(ip)
try:
if all(
(str_val not in hostname_format_str)
for str_val in ['one', 'two', 'three', 'four']
):
raise ValueError(
"Invalid HOSTNAME_FORMAT: Expected string containing "
"at least one of the IP octets. Received: %s"
"(ex:'vm%%(three)s-%%(four)s.my_domain.com')" %
hostname_format_str
)
return hostname_format_str % hostnaming_format_map
except (KeyError, TypeError, ValueError):
logger.exception(
"Invalid instance_hostname_format: %s" % hostname_format_str
)
return raw_hostname(ip)
def create_hostnaming_map(ip):
try:
regex = re.compile(
"(?P<one>[0-9]+)\.(?P<two>[0-9]+)\."
"(?P<three>[0-9]+)\.(?P<four>[0-9]+)"
)
r = regex.search(ip)
(one, two, three, four) = r.groups()
domain = getattr(settings, 'INSTANCE_HOSTNAMING_DOMAIN', None)
hostname_map = {
'one': one,
'two': two,
'three': three,
'four': four,
'domain': domain
}
return hostname_map
except Exception:
raise Exception(
"IPv4 Address expected: <%s> is not of the format VVV.XXX.YYY.ZZZ" %
ip
)
def raw_hostname(ip):
"""
For now, return raw IP
"""
return ip
def execution_has_unreachable(playbook_results):
"""Return value 4 means unreachable in ansible-playbook"""
if type(playbook_results) != list:
playbook_results = [playbook_results]
return 4 in playbook_results
def execution_has_failures(playbook_results):
"""Return value 2 means failure in ansible-playbook"""
if type(playbook_results) != list:
playbook_results = [playbook_results]
return 2 in playbook_results
def raise_playbook_errors(
playbook_results, instance_id, instance_ip, hostname, allow_failures=False
):
"""
Return value 4 means unreachable/dark
Return value 2 means failure
"""
if not type(playbook_results) == list:
playbook_results = [playbook_results]
error_message = ""
for rc in playbook_results:
if rc == 4:
error_message += "Unreachable"
elif not allow_failures and rc == 2:
error_message += "Failed"
if error_message:
msg = "Instance: %s IP:%s %s - %s" % (
instance_id, instance_ip,
'Hostname: ' + hostname if hostname else "", error_message
)
raise AnsibleDeployException(msg)