stack-orchestrator/app/deploy-system.py

70 lines
2.3 KiB
Python
Raw Normal View History

2022-08-14 04:02:10 +00:00
# Deploys the system components using docker-compose
import os
import argparse
from decouple import config
from python_on_whales import DockerClient
2022-08-14 04:58:13 +00:00
def include_exclude_check(s, args):
if args.include == None and args.exclude == None:
return True
if args.include != None:
include_list = args.include.split(",")
return s in include_list
if args.exclude != None:
exclude_list = args.exclude.split(",")
return s not in exclude_list
2022-08-14 04:02:10 +00:00
parser = argparse.ArgumentParser(
description="deploy the complete stack"
)
2022-08-14 05:06:55 +00:00
parser.add_argument("command", type=str, nargs=1, choices=['up', 'down', 'ps'], help="command: up|down|ps")
2022-08-14 04:02:10 +00:00
parser.add_argument("--verbose", action="store_true", help="increase output verbosity")
parser.add_argument("--quiet", action="store_true", help="don\'t print informational output")
parser.add_argument("--check-only", action="store_true", help="looks at what\'s already there and checks if it looks good")
parser.add_argument("--dry-run", action="store_true", help="don\'t do anything, just print the commands that would be executed")
2022-08-14 04:58:13 +00:00
group = parser.add_mutually_exclusive_group()
group.add_argument("--exclude", type=str, help="don\'t start these components")
group.add_argument("--include", type=str, help="only start these components")
2022-08-14 04:02:10 +00:00
args = parser.parse_args()
verbose = args.verbose
quiet = args.quiet
2022-08-14 04:42:15 +00:00
print(args)
2022-08-14 04:02:10 +00:00
with open("cluster-list.txt") as cluster_list_file:
clusters = cluster_list_file.read().splitlines()
if verbose:
print(f'Cluster components: {clusters}')
# Construct a docker compose command suitable for our purpose
compose_files = []
for cluster in clusters:
2022-08-14 04:58:13 +00:00
if include_exclude_check(cluster, args):
compose_file_name = os.path.join("compose", f"docker-compose-{cluster}.yml")
compose_files.append(compose_file_name)
else:
if not quiet:
print(f"Excluding: {cluster}")
2022-08-14 04:02:10 +00:00
2022-08-14 04:58:13 +00:00
if verbose:
print(f"files: {compose_files}")
2022-08-14 04:02:10 +00:00
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
docker = DockerClient(compose_files=compose_files)
2022-08-14 05:23:16 +00:00
command = args.command[0]
2022-08-14 04:42:15 +00:00
if not args.dry_run:
2022-08-14 05:06:55 +00:00
if command == "up":
2022-08-14 05:23:16 +00:00
if verbose:
print("Running compose up")
2022-08-15 12:14:42 +00:00
docker.compose.up(detach=True)
2022-08-14 05:06:55 +00:00
elif command == "down":
2022-08-14 05:23:16 +00:00
if verbose:
print("Running compose down")
2022-08-14 05:06:55 +00:00
docker.compose.down()