1
0
stack-orchestrator/app/deploy_system.py

131 lines
5.1 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
2022-10-07 13:12:43 +00:00
import hashlib
2022-08-14 04:02:10 +00:00
import os
2022-10-07 13:12:43 +00:00
import sys
2022-08-14 04:02:10 +00:00
from python_on_whales import DockerClient
2022-08-24 03:27:42 +00:00
import click
2023-01-12 04:56:05 +00:00
import importlib.resources
2023-01-10 03:43:04 +00:00
from pathlib import Path
import yaml
from .util import include_exclude_check
2022-08-14 04:58:13 +00:00
2022-10-04 01:37:18 +00:00
2022-08-24 03:27:42 +00:00
@click.command()
2022-10-04 01:37:18 +00:00
@click.option("--include", help="only start these components")
@click.option("--exclude", help="don\'t start these components")
2022-10-07 12:48:30 +00:00
@click.option("--cluster", help="specify a non-default cluster name")
2022-11-08 05:22:54 +00:00
@click.argument('command', required=True) # help: command: up|down|ps
@click.argument('services', nargs=-1) # help: command: up|down|ps <service1> <service2>
2022-08-24 18:36:58 +00:00
@click.pass_context
2022-11-08 05:22:54 +00:00
def command(ctx, include, exclude, cluster, command, services):
2022-08-24 18:53:51 +00:00
'''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
stack = ctx.obj.stack
2022-08-24 03:27:42 +00:00
2023-01-09 03:11:30 +00:00
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
compose_dir = Path(__file__).absolute().parent.joinpath("data", "compose")
2023-01-08 04:02:14 +00:00
2022-10-07 13:12:43 +00:00
if cluster is None:
# Create default unique, stable cluster name from confile file path
# TODO: change this to the config file path
path = os.path.realpath(sys.argv[0])
hash = hashlib.md5(path.encode()).hexdigest()
cluster = f"laconic-{hash}"
if verbose:
print(f"Using cluster name: {cluster}")
2023-01-09 03:11:30 +00:00
# See: https://stackoverflow.com/a/20885799/1701505
2023-01-08 03:44:08 +00:00
from . import data
with importlib.resources.open_text(data, "pod-list.txt") as pod_list_file:
all_pods = pod_list_file.read().splitlines()
pods_in_scope = []
if stack:
# In order to be compatible with Python 3.8 we need to use this hack to get the path:
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
stack_file_path = Path(__file__).absolute().parent.joinpath("data", "stacks", stack, "stack.yml")
with stack_file_path:
stack_config = yaml.safe_load(open(stack_file_path, "r"))
# TODO: syntax check the input here
pods_in_scope = stack_config['pods']
else:
pods_in_scope = all_pods
2022-08-24 03:27:42 +00:00
if verbose:
print(f"Pods: {pods_in_scope}")
2022-08-24 03:27:42 +00:00
# Construct a docker compose command suitable for our purpose
compose_files = []
for pod in pods_in_scope:
2022-10-07 13:12:43 +00:00
if include_exclude_check(pod, include, exclude):
2023-01-08 04:02:14 +00:00
compose_file_name = os.path.join(compose_dir, f"docker-compose-{pod}.yml")
2022-08-24 03:27:42 +00:00
compose_files.append(compose_file_name)
else:
2022-11-30 02:54:08 +00:00
if verbose:
2022-10-07 13:12:43 +00:00
print(f"Excluding: {pod}")
2022-08-24 03:27:42 +00:00
if verbose:
print(f"files: {compose_files}")
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
2022-10-07 13:12:43 +00:00
docker = DockerClient(compose_files=compose_files, compose_project_name=cluster)
2022-08-24 03:27:42 +00:00
services_list = list(services) or None
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:
2022-11-08 05:22:54 +00:00
print(f"Running compose up for services: {services_list}")
docker.compose.up(detach=True, services=services_list)
2022-08-24 03:27:42 +00:00
elif command == "down":
if verbose:
print("Running compose down")
docker.compose.down()
elif command == "ps":
2022-08-24 19:53:48 +00:00
if verbose:
print("Running compose ps")
2022-10-24 18:39:51 +00:00
container_list = docker.compose.ps()
2022-10-24 22:02:11 +00:00
if len(container_list) > 0:
print("Running containers:")
for container in container_list:
print(f"id: {container.id}, name: {container.name}, ports: ", end="")
ports = container.network_settings.ports
comma = ""
for port_mapping in ports.keys():
mapping = ports[port_mapping]
print(comma, end="")
if mapping is None:
print(f"{port_mapping}", end="")
else:
print(f"{mapping[0]['HostIp']}:{mapping[0]['HostPort']}->{port_mapping}", end="")
comma = ", "
print()
2022-10-24 22:02:11 +00:00
else:
print("No containers running")
2022-08-24 19:53:48 +00:00
elif command == "logs":
if verbose:
print("Running compose logs")
docker.compose.logs()