This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
/
bootloader.py
588 lines (467 loc) · 23.1 KB
/
bootloader.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import logging
import asyncio
import pathlib
import argparse
import copy
import sys
import os
import re
import shutil
import base64
import zipfile
from pathlib import Path
import aiodocker
import aiohttp
import yaml
import yaml.scanner
from tenacity import retry, stop_after_attempt, wait_exponential
from common.config import config, static
from common.docker_helpers import (create_secret, get_secret, delete_secret, get_network, connect_to_aiodocker,
docker_context, stream_docker_log, logger as docker_logger, disconnect_from_network,
update_service, get_replicas)
logging.basicConfig(level=logging.DEBUG, format="{asctime} - {name} - {levelname}:{message}", style='{')
logger = logging.getLogger("BOOTLOADER")
static.set_local_hostname("local_bootloader")
COMPOSE_BASE = {
"version": "3.5",
"services": {},
"networks": {"walkoff_network": {"external": True}},
"secrets": {"encryption_key": {"external": True}}
}
APP_NAME_PREFIX = "walkoff_"
p = Path('./apps').glob('**/*')
volume_names = (static.REGISTRY_VOLUME, static.MINIO_VOLUME, static.MONGO_VOLUME, static.PORTAINER_VOLUME)
key_names = (static.ENCRYPTION_KEY, static.INTERNAL_KEY, static.MONGO_KEY, static.REDIS_KEY,
static.MINIO_ACCESS_KEY, static.MINIO_SECRET_KEY)
def parse_yaml(path):
with open(path) as fp:
try:
return yaml.safe_load(fp)
except yaml.YAMLError as e:
logger.info(f"Invalid yaml: {path}. {e}")
except yaml.scanner.ScannerError as e:
logger.info(f"Invalid yaml: {path}. {e}")
def dump_yaml(path, obj):
with open(path, 'w') as fp:
try:
return yaml.dump(obj, fp)
except yaml.YAMLError as e:
logger.info(f"Invalid yaml: {path}. {e}")
def parse_env_file(path):
with open(path) as fp:
return [line.strip() for line in fp]
def compose_from_app(path: pathlib.Path, name):
env_txt = path / "env.txt"
env_file = {}
if env_txt.exists():
env_file = {"environment": parse_env_file(env_txt)}
compose = copy.deepcopy(COMPOSE_BASE)
build = {"build": {"context": str(path), "dockerfile": "Dockerfile"}}
image = {"image": f"{config.DOCKER_REGISTRY}/{APP_NAME_PREFIX}{name}:{path.name}"}
networks = {"networks": ["walkoff_network"]}
deploy = {"deploy": {"mode": "replicated", "replicas": 0, "restart_policy": {"condition": "none"}}}
config_mount = {"configs": ["common_env.yml"]}
secret_mount = {"secrets": ["walkoff_encryption_key", "walkoff_redis_key"]}
shared_path = os.getcwd() + "/data/shared"
final_mount = shared_path + ":/app/shared"
volumes_mount = {"volumes": [final_mount]}
compose["services"] = {name: {**build, **image, **networks, **deploy, **config_mount,
**secret_mount, **volumes_mount, **env_file}}
return compose
async def log_proc_output(proc, silent=False):
stdout, stderr = await proc.communicate()
if not silent:
if proc.returncode:
for line in stderr.decode().split('\n'):
if line != '':
logger.error(line)
else:
for line in stdout.decode().split('\n'):
if line != '':
logger.info(line)
def merge_composes(base, others):
if not isinstance(base, dict):
base = parse_yaml(base)
if base.get("services") is None:
base["services"] = {}
if not isinstance(others[0], dict):
others = [parse_yaml(o) for o in others]
for o in others:
base["services"].update(o.get("services", {}))
return base
def generate_app_composes():
# TODO: Probably find a way to incorporate the app repo in here as well to eliminate mounting files to umpire
composes = []
for app in pathlib.Path(config.APPS_PATH).iterdir():
if not app.is_dir():
try:
zip_ref = zipfile.ZipFile(app, 'r')
zip_ref.extractall(config.APPS_PATH)
zip_ref.close()
os.remove(app)
except Exception as e:
logger.error(f"Zip error: {e}")
continue
for app in pathlib.Path(config.APPS_PATH).iterdir():
# grabs only directories and ignores all __* directories i.e. __pycache__
if app.is_dir() and not re.fullmatch(r"(__.*)", app.name):
for version in app.iterdir():
# grabs all valid version directories of form "v0.12.3.45..."
if re.fullmatch(r"((\d\.?)+)", version.name):
composes.append(compose_from_app(version, f"app_{app.name}"))
logger.info(f"Generated compose for {app.name} version: {version.name}")
return composes
async def are_you_sure(prompt):
try:
resp = input(f"{prompt}\n\nAre you sure? (yes/no): ")
while resp.lower() not in ("yes", "no"):
resp = input("Please answer 'yes' or 'no': ")
except KeyboardInterrupt:
return False
if resp.lower() == "yes":
return True
else:
return False
async def create_encryption_key(docker_client, key_name, value=None):
try:
await get_secret(docker_client, key_name)
except aiodocker.exceptions.DockerError:
logger.info(f"Creating secret {key_name}...")
try:
key = value if value else base64.urlsafe_b64encode(os.urandom(32))
await create_secret(docker_client, key_name, key)
return key
except aiodocker.exceptions.DockerError as e:
logger.exception(f"Could not create Docker Secret {key_name}, exiting. Reason: {e}")
os._exit(1)
else:
logger.info(f"Skipping secret {key_name} creation, it already exists.")
return "Key already existed."
async def delete_encryption_key(docker_client, key_name):
try:
await delete_secret(docker_client, key_name)
logger.info(f"Deleted secret {key_name}...")
except aiodocker.exceptions.DockerError:
logger.info(f"Skipping secret {key_name} deletion, it doesn't exist.")
async def check_for_network(docker_client):
try:
await get_network(docker_client, "walkoff_network")
return True
except aiodocker.exceptions.DockerError:
return False
# async def delete_dir_contents(path):
# logger.info(f"Deleting directory contents of {path}...")
# try:
# for root, dirs, files in os.walk(path):
# for f in files:
# os.unlink(os.path.join(root, f))
# for d in dirs:
# shutil.rmtree(os.path.join(root, d))
# except Exception as e:
# logger.exception(f"Could not remove contents in {path}. Reason: {e}")
@retry(stop=stop_after_attempt(10), wait=wait_exponential(min=1, max=10))
async def delete_volume(docker_client, volume_name):
try:
vol = aiodocker.docker.DockerVolume(docker_client, volume_name)
await vol.delete()
logger.info(f"Deleted volume {volume_name}.")
except aiodocker.exceptions.DockerError as e:
if e.status == 404:
logger.info(f"Skipping removal of {volume_name}, it doesn't exist.")
else:
logger.info(f"Failed to remove volume {volume_name}, a container may still be using it. "
f"Waiting to try again...")
raise e
@retry(stop=stop_after_attempt(10), wait=wait_exponential(min=1, max=10))
async def deploy_compose(compose):
try:
if not isinstance(compose, dict):
compose = parse_yaml(compose)
dump_yaml(config.TMP_COMPOSE, compose)
compose = config.TMP_COMPOSE
proc = await asyncio.create_subprocess_exec("docker", "stack", "deploy", "--compose-file", compose, "walkoff",
stderr=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE)
await log_proc_output(proc)
if proc.returncode:
raise OSError
else:
return True
except Exception as e:
logger.info("Failed deploying, waiting to try again...")
raise e
@retry(stop=stop_after_attempt(10), wait=wait_exponential(min=1, max=10))
async def rm_stack(stack_name):
try:
logger.info("Removing Walkoff stack and related artifacts...")
proc = await asyncio.create_subprocess_exec("docker", "stack", "rm", stack_name, stderr=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE)
await log_proc_output(proc)
if proc.returncode:
raise OSError
else:
return proc
except Exception as e:
logger.info("Failed to fully remove stack, waiting to try again...")
raise e
async def build_image(docker_client, repo, dockerfile, context_dir, dockerignore):
logger.info(f"Building {repo} with {dockerfile} in {context_dir}")
with docker_context(Path(context_dir), dockerignore=dockerignore) as context:
log_stream = await docker_client.images.build(fileobj=context, tag=repo, rm=True,
forcerm=True, pull=True, stream=True,
path_dockerfile=dockerfile,
encoding="application/x-tar")
await stream_docker_log(log_stream)
async def pull_image(docker_client, repo):
logger.info(f"Pulling image {repo}")
try:
await docker_client.images.pull(repo)
logger.info(f"Pulled image {repo}.")
return True
except aiodocker.exceptions.DockerError as e:
logger.exception(f"Failed to pull image: {e}")
return False
async def push_image(docker_client, repo):
logger.info(f"Pushing image {repo}.")
try:
await docker_client.images.push(repo)
# await stream_docker_log(log_stream)
logger.info(f"Pushed image {repo}.")
return True
except aiodocker.exceptions.DockerError as e:
logger.exception(f"Failed to push image: {e}")
return False
async def image_exists(docker_client, repo):
try:
await docker_client.images.inspect(repo)
logger.info(f"Image exists {repo}.")
return True
except aiodocker.exceptions.DockerError as e:
logger.info(f"Image doesnt exist {repo}.")
return False
async def force_service_update(docker_client, service_name, image):
logger.info(f"Forcing service update for {service_name}...")
await update_service(docker_client, service_name, image=image, force=True)
await asyncio.sleep(3)
logger.info(f"Scaling {service_name} to 0...")
await update_service(docker_client, service_name, image=image, force=True, mode={"replicated": {"Replicas": 0}})
replicas = 1
while replicas > 0:
replicas = (await get_replicas(docker_client, service_name))['running']
await asyncio.sleep(2)
logger.info(f"Scaling {service_name} back to 1...")
await update_service(docker_client, service_name, image=image, force=True, mode={"replicated": {"Replicas": 1}})
while replicas < 1:
replicas = (await get_replicas(docker_client, service_name))['running']
await asyncio.sleep(2)
logger.info(f"Forced service update for {service_name}.")
class Bootloader:
""" A class to hold the logic for each of the possible commands. This follows the dispatch pattern we us in app_base
for calling actions in apps. The pattern as applied to the CLI follows close to this example:
https://chase-seibert.github.io/blog/2014/03/21/python-multilevel-argparse.html#
"""
def __init__(self, session=None, docker_client=None):
self.session: aiohttp.ClientSession = session
self.docker_client: aiodocker.Docker = docker_client
with open(".dockerignore") as f:
self.dockerignore = [line.strip() for line in f.readlines()]
@staticmethod
async def run():
""" Landing pad to launch primary command and do whatever async init the bootloader needs. """
# TODO: fill in the helps, and further develop cli with the end user in mind
commands = {"up", "down", "refresh"}
parser = argparse.ArgumentParser()
parser.add_argument("command", choices=commands)
parser.add_argument("args", nargs=argparse.REMAINDER)
logger.setLevel("DEBUG")
docker_logger.setLevel("DEBUG")
# Parse out the command
args = parser.parse_args(sys.argv[1:2])
async with aiohttp.ClientSession() as session, connect_to_aiodocker() as docker_client:
bootloader = Bootloader(session, docker_client)
if hasattr(bootloader, args.command):
await getattr(bootloader, args.command)()
else:
logger.error("Invalid command.")
# TODO: Pipe this through the logger. print_help() accepts a file kwarg that we can use to do this
parser.print_help()
@retry(stop=stop_after_attempt(10), wait=wait_exponential(min=1, max=10))
async def wait_for_registry(self):
try:
async with self.session.get(f"http://{static.REGISTRY_SERVICE}:5000") as resp:
if resp.status == 200:
return True
else:
raise ConnectionError
except Exception as e:
logger.info("Registry not available yet, waiting to try again...")
raise e
@retry(stop=stop_after_attempt(10), wait=wait_exponential(min=1, max=10))
async def wait_for_minio(self):
try:
async with self.session.get(f"http://{config.MINIO}/minio/health/ready") as resp:
if resp.status == 200:
return True
else:
raise ConnectionError
except Exception as e:
logger.info("Minio not available yet, waiting to try again...")
raise e
async def up(self):
# Set up a subcommand parser
parser = argparse.ArgumentParser(description="Bring the WALKOFF stack up and initialize it")
parser.add_argument("-b", "--build", action="store_true",
help="Builds and pushes all WALKOFF components to local registry.")
parser.add_argument("-d", "--debug", action="store_true",
help="Set log level to debug.")
parser.add_argument("-y", "--yes", action="store_true",
help="Skips all verification questions.")
parser.add_argument("-r", "--resources", action="store_true",
help="Only runs the resource services.")
parser.add_argument("-o", "--offline", action="store_true",
help="Runs WALKOFF offline")
# Parse out the command
args = parser.parse_args(sys.argv[2:])
debug_pw = None
debug_pw_encryption = None
if args.debug:
logger.setLevel("DEBUG")
docker_logger.setLevel("DEBUG")
if args.yes or await are_you_sure(
"You specified -d/--debug, which will use 'walkoff123456' as the password for all "
"resources, This should be used for debug only. "
"(Choose 'no' to use randomly generated passwords.)"):
debug_pw = b"walkoff123456"
debug_pw_encryption = b"walkoff123456789987654321walkoff"
# Create Walkoff encryption key
await create_encryption_key(self.docker_client, static.ENCRYPTION_KEY, debug_pw_encryption)
# Create internal user key
await create_encryption_key(self.docker_client, static.INTERNAL_KEY, debug_pw)
# Create Minio secret key
await create_encryption_key(self.docker_client, static.MINIO_ACCESS_KEY, b"walkoff123456")
await create_encryption_key(self.docker_client, static.MINIO_SECRET_KEY, debug_pw)
# Create Mongo user password
await create_encryption_key(self.docker_client, static.MONGO_KEY, debug_pw)
# Create Redis key
await create_encryption_key(self.docker_client, static.REDIS_KEY, debug_pw)
# Create volumes
logger.info("Creating volumes for persisting (registry, minio, mongo, portainer)...")
for volume in volume_names:
await self.docker_client.volumes.create({"name": volume})
# Bring up the base compose with the registry
base_compose = parse_yaml(config.BASE_COMPOSE)
for service_name, service in base_compose["services"].items():
if args.offline:
if not await image_exists(self.docker_client, service["image"]):
if not await pull_image(self.docker_client, service["image"]):
logger.info("Failed to pull requisite images, aborting deployment. "
"Please use the 'down' command to clean up. ")
return
else:
if not await pull_image(self.docker_client, service["image"]):
logger.info("Failed to pull requisite images, aborting deployment. "
"Please use the 'down' command to clean up. ")
return
logger.info("Deploying base services (registry, minio, mongo, portainer, redis)...")
await deploy_compose(base_compose)
if args.resources:
return
await self.wait_for_registry()
# Merge the base, walkoff, and app composes
app_composes = generate_app_composes()
walkoff_compose = parse_yaml(config.WALKOFF_COMPOSE)
merged_compose = merge_composes(walkoff_compose, app_composes)
dump_yaml(config.TMP_COMPOSE, merged_compose)
if args.build:
walkoff_app_sdk = walkoff_compose["services"]["app_sdk"]
await build_image(self.docker_client, walkoff_app_sdk["image"],
walkoff_app_sdk["build"]["dockerfile"],
walkoff_app_sdk["build"]["context"],
self.dockerignore)
await push_image(self.docker_client, walkoff_app_sdk["image"])
builders = []
pushers = []
for service_name, service in walkoff_compose["services"].items():
if "build" in service:
build_func = build_image(self.docker_client, service["image"],
service["build"]["dockerfile"],
service["build"]["context"],
self.dockerignore)
push_func = push_image(self.docker_client, service["image"])
if args.debug:
await build_func
await push_func
else:
builders.append(build_func)
pushers.append(push_func)
if not args.debug:
logger.info("Building Docker images asynchronously, this could take some time...")
await asyncio.gather(*builders)
logger.info("Build process complete.")
logger.info("Pushing Docker images asynchronously, this could take some time...")
await asyncio.gather(*pushers)
logger.info("Push process complete.")
await self.wait_for_minio()
# await self.push_to_minio()
logger.info("Deploying Walkoff stack...")
return_code = await deploy_compose(merged_compose)
logger.info("Walkoff stack deployed, it may take a little time to converge. \n"
"Use 'docker stack services walkoff' to check on Walkoff services. \n"
"Web interface should be available at 'https://127.0.0.1:8080' once walkoff_resource_nginx is up.")
return return_code
async def down(self):
# Set up a subcommand parser
parser = argparse.ArgumentParser(description="Remove the WALKOFF stack and optionally related artifacts.")
parser.add_argument("-c", "--clean", action="store_true",
help="Removes all encryption keys, volumes, data.")
parser.add_argument("-d", "--debug", action="store_true",
help="Set log level to debug.")
parser.add_argument("-y", "--yes", action="store_true",
help="Skips all verification questions.")
# Parse out the command
args = parser.parse_args(sys.argv[2:])
if args.debug:
logger.setLevel("DEBUG")
docker_logger.setLevel("DEBUG")
proc = await rm_stack("walkoff")
# if not args.skipnetwork:
# logger.info("Waiting for containers to exit and network to be removed...")
# await exponential_wait(check_for_network, [self.docker_client], "Network walkoff_default still exists")
if args.clean:
if args.yes or await are_you_sure("Are you sure you want to remove all WALKOFF data? This will remove "
"all of WALKOFF's docker secrets, docker volumes, and data for "
"walkoff_resource services,"):
for key in key_names:
await delete_encryption_key(self.docker_client, key)
for volume in volume_names:
await delete_volume(self.docker_client, volume)
logger.info("Walkoff stack removed, it may take a few seconds to stop all containers.")
return proc.returncode
async def refresh(self):
parser = argparse.ArgumentParser(description="Rebuild a specific service and force it to update.")
parser.add_argument("-s", "--service",
help="Name of the service to rebuild and update. "
"You can specify a prefix ('walkoff_app' or 'walkoff_core') "
"to rebuild all in that category.")
parser.add_argument("-y", "--yes", action="store_true",
help="Skips all verification questions.")
args = parser.parse_args(sys.argv[2:])
compose = parse_yaml(config.TMP_COMPOSE)['services']
service_yaml = compose.get(args.service)
if service_yaml:
if args.yes or await are_you_sure("Forcing a service to update will disrupt any work it is currently "
"doing. It is not yet guaranteed that a service will pick back up "
"where it left off. "):
if "build" in service_yaml:
await build_image(self.docker_client, service_yaml["image"],
service_yaml["build"]["dockerfile"],
service_yaml["build"]["context"],
self.dockerignore)
await push_image(self.docker_client, service_yaml["image"])
service_name = f"walkoff_{args.service}"
await force_service_update(self.docker_client, service_name, service_yaml["image"])
else:
services = list(compose.keys())
services.sort()
logger.exception(f"No such service, valid services: {services}.")
if __name__ == "__main__":
asyncio.run(Bootloader.run())