stack-orchestrator/app/deploy_system.py

88 lines
2.9 KiB
Python
Raw Normal View History

2022-08-23 21:58:32 +00:00
# Copyright © 2022 Cerc
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
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-24 03:27:42 +00:00
import click
2022-08-14 04:02:10 +00:00
2022-08-24 18:53:51 +00:00
def include_exclude_check(s, include, exclude):
if include == None and exclude == None:
2022-08-14 04:58:13 +00:00
return True
2022-08-24 18:53:51 +00:00
if include != None:
include_list = include.split(",")
2022-08-14 04:58:13 +00:00
return s in include_list
2022-08-24 18:53:51 +00:00
if exclude != None:
exclude_list = exclude.split(",")
2022-08-14 04:58:13 +00:00
return s not in exclude_list
2022-08-24 03:27:42 +00:00
@click.command()
2022-08-24 18:53:51 +00:00
@click.option('--include', help="only start these components")
@click.option('--exclude', help="don\'t start these components")
2022-08-24 19:01:35 +00:00
@click.argument('command') # help: command: up|down|ps
2022-08-24 18:36:58 +00:00
@click.pass_context
2022-08-24 18:53:51 +00:00
def command(ctx, include, exclude, command):
'''deploy a stack'''
# TODO: implement option exclusion and command value constraint lost with the move from argparse to click
2022-08-24 18:36:58 +00:00
quiet = ctx.obj.quiet
verbose = ctx.obj.verbose
2022-08-24 19:53:48 +00:00
dry_run = ctx.obj.dry_run
2022-08-24 03:27:42 +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-24 18:53:51 +00:00
if include_exclude_check(cluster, include, exclude):
2022-08-24 03:27:42 +00:00
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}")
if verbose:
print(f"files: {compose_files}")
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
docker = DockerClient(compose_files=compose_files)
2022-08-24 18:36:58 +00:00
if not dry_run:
2022-08-24 03:27:42 +00:00
if command == "up":
if verbose:
print("Running compose up")
docker.compose.up(detach=True)
elif command == "down":
if verbose:
print("Running compose down")
docker.compose.down()
2022-08-24 19:53:48 +00:00
elif command == "ps":
if verbose:
print("Running compose ps")
docker.compose.ps()
elif command == "logs":
if verbose:
print("Running compose logs")
docker.compose.logs()