stack-orchestrator/app/deploy_system.py

314 lines
13 KiB
Python
Raw Normal View History

2023-01-19 21:13:57 +00:00
# Copyright © 2022, 2023 Cerc
2022-08-23 21:58:32 +00:00
# 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
2023-02-20 13:23:21 +00:00
import copy
2022-08-14 04:02:10 +00:00
import os
2022-10-07 13:12:43 +00:00
import sys
from dataclasses import dataclass
2023-02-17 20:34:51 +00:00
from decouple import config
2023-02-17 21:15:35 +00:00
import subprocess
2023-03-09 05:22:43 +00:00
from python_on_whales import DockerClient, DockerException
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
2023-01-19 21:13:57 +00:00
from .util import include_exclude_check, get_parsed_stack_config
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
2023-01-24 05:09:56 +00:00
@click.argument('extra_args', nargs=-1) # help: command: up|down|ps <service1> <service2>
2022-08-24 18:36:58 +00:00
@click.pass_context
2023-01-24 05:09:56 +00:00
def command(ctx, include, exclude, cluster, command, extra_args):
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
2023-01-26 23:47:06 +00:00
debug = ctx.obj.debug
2022-08-24 18:36:58 +00:00
quiet = ctx.obj.quiet
verbose = ctx.obj.verbose
2023-02-17 20:34:51 +00:00
local_stack = ctx.obj.local_stack
2022-08-24 19:53:48 +00:00
dry_run = ctx.obj.dry_run
2023-01-18 22:37:12 +00:00
stack = ctx.obj.stack
2022-08-24 03:27:42 +00:00
2023-02-20 13:09:35 +00:00
cluster_context = _make_cluster_context(ctx.obj, include, exclude, cluster)
2022-08-24 03:27:42 +00:00
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
2023-02-20 13:09:35 +00:00
docker = DockerClient(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster)
2022-08-24 03:27:42 +00:00
2023-01-24 05:09:56 +00:00
extra_args_list = list(extra_args) 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":
container_exec_env = _make_runtime_env(ctx.obj)
for attr, value in container_exec_env.items():
os.environ[attr] = value
2022-08-24 03:27:42 +00:00
if verbose:
print(f"Running compose up with container_exec_env: {container_exec_env}, extra_args: {extra_args_list}")
2023-02-20 13:09:35 +00:00
for pre_start_command in cluster_context.pre_start_commands:
2023-02-20 13:43:06 +00:00
_run_command(ctx.obj, cluster_context.cluster, pre_start_command)
2023-01-24 05:09:56 +00:00
docker.compose.up(detach=True, services=extra_args_list)
2023-02-20 13:09:35 +00:00
for post_start_command in cluster_context.post_start_commands:
2023-02-20 13:43:06 +00:00
_run_command(ctx.obj, cluster_context.cluster, post_start_command)
_orchestrate_cluster_config(ctx.obj, cluster_context.config, docker, container_exec_env)
2022-08-24 03:27:42 +00:00
elif command == "down":
if verbose:
print("Running compose down")
docker.compose.down()
2023-01-26 06:22:05 +00:00
elif command == "exec":
if extra_args_list is None or len(extra_args_list) < 2:
print("Usage: exec <service> <cmd>")
sys.exit(1)
service_name = extra_args_list[0]
2023-03-05 01:45:57 +00:00
command_to_exec = ["sh", "-c"] + extra_args_list[1:]
container_exec_env = _make_runtime_env(ctx.obj)
2023-01-26 06:22:05 +00:00
if verbose:
print(f"Running compose exec {service_name} {command_to_exec}")
2023-03-09 05:22:43 +00:00
try:
docker.compose.execute(service_name, command_to_exec, envs=container_exec_env)
except DockerException as error:
print(f"container command returned error exit status")
2023-01-24 05:09:56 +00:00
elif command == "port":
if extra_args_list is None or len(extra_args_list) < 2:
print("Usage: port <service> <exposed-port>")
sys.exit(1)
service_name = extra_args_list[0]
exposed_port = extra_args_list[1]
if verbose:
2023-01-26 06:22:05 +00:00
print(f"Running compose port {service_name} {exposed_port}")
2023-01-24 05:09:56 +00:00
mapped_port_data = docker.compose.port(service_name, exposed_port)
print(f"{mapped_port_data[0]}:{mapped_port_data[1]}")
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")
logs_output = docker.compose.logs(services=extra_args_list if extra_args_list is not None else [])
2023-03-07 18:57:24 +00:00
print(logs_output)
2023-02-17 20:34:51 +00:00
2023-02-20 13:23:21 +00:00
def get_stack_status(ctx, stack):
ctx_copy = copy.copy(ctx)
ctx_copy.stack = stack
2023-02-20 13:43:06 +00:00
cluster_context = _make_cluster_context(ctx_copy, None, None, None)
2023-02-20 13:23:21 +00:00
docker = DockerClient(compose_files=cluster_context.compose_files, compose_project_name=cluster_context.cluster)
# TODO: refactor to avoid duplicating this code above
if ctx.verbose:
print("Running compose ps")
container_list = docker.compose.ps()
if len(container_list) > 0:
if ctx.debug:
print(f"Container list from compose ps: {container_list}")
return True
else:
if ctx.debug:
print("No containers found from compose ps")
False
2023-02-20 13:09:35 +00:00
def _make_runtime_env(ctx):
container_exec_env = {
"CERC_HOST_UID": f"{os.getuid()}",
"CERC_HOST_GID": f"{os.getgid()}"
}
container_exec_env.update({"CERC_SCRIPT_DEBUG": "true"} if ctx.debug else {})
return container_exec_env
2023-02-20 13:09:35 +00:00
def _make_cluster_context(ctx, include, exclude, cluster):
if ctx.local_stack:
dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")]
print(f'Local stack dev_root_path (CERC_REPO_BASE_DIR) overridden to: {dev_root_path}')
else:
dev_root_path = os.path.expanduser(config("CERC_REPO_BASE_DIR", default="~/cerc"))
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
compose_dir = Path(__file__).absolute().parent.joinpath("data", "compose")
if cluster is None:
# Create default unique, stable cluster name from confile file path and stack name if provided
2023-02-20 13:09:35 +00:00
# TODO: change this to the config file path
path = os.path.realpath(sys.argv[0])
unique_cluster_descriptor = f"{path},{ctx.stack},{include},{exclude}"
if ctx.debug:
print(f"pre-hash descriptor: {unique_cluster_descriptor}")
hash = hashlib.md5(unique_cluster_descriptor.encode()).hexdigest()
2023-02-20 13:09:35 +00:00
cluster = f"laconic-{hash}"
if ctx.verbose:
print(f"Using cluster name: {cluster}")
# See: https://stackoverflow.com/a/20885799/1701505
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 ctx.stack:
stack_config = get_parsed_stack_config(ctx.stack)
# TODO: syntax check the input here
pods_in_scope = stack_config['pods']
else:
pods_in_scope = all_pods
# Convert all pod definitions to v1.1 format
pods_in_scope = _convert_to_new_format(pods_in_scope)
if ctx.verbose:
print(f"Pods: {pods_in_scope}")
# Construct a docker compose command suitable for our purpose
compose_files = []
pre_start_commands = []
post_start_commands = []
for pod in pods_in_scope:
pod_name = pod["name"]
pod_repository = pod["repository"]
pod_path = pod["path"]
if include_exclude_check(pod_name, include, exclude):
if pod_repository is None or pod_repository == "internal":
compose_file_name = os.path.join(compose_dir, f"docker-compose-{pod_path}.yml")
else:
pod_root_dir = os.path.join(dev_root_path, pod_repository.split("/")[-1], pod["path"])
compose_file_name = os.path.join(pod_root_dir, "docker-compose.yml")
pod_pre_start_command = pod["pre_start_command"]
pod_post_start_command = pod["post_start_command"]
if pod_pre_start_command is not None:
pre_start_commands.append(os.path.join(pod_root_dir, pod_pre_start_command))
if pod_post_start_command is not None:
post_start_commands.append(os.path.join(pod_root_dir, pod_post_start_command))
compose_files.append(compose_file_name)
else:
if ctx.verbose:
print(f"Excluding: {pod_name}")
if ctx.verbose:
print(f"files: {compose_files}")
cluster_config = stack_config['config'] if 'config' in stack_config else None
return cluster_context(cluster, compose_files, pre_start_commands, post_start_commands, cluster_config)
2023-02-20 13:09:35 +00:00
class cluster_context:
def __init__(self, cluster, compose_files, pre_start_commands, post_start_commands, config) -> None:
2023-02-20 13:09:35 +00:00
self.cluster = cluster
self.compose_files = compose_files
self.pre_start_commands = pre_start_commands
self.post_start_commands = post_start_commands
self.config = config
2023-02-20 13:09:35 +00:00
2023-02-17 20:34:51 +00:00
def _convert_to_new_format(old_pod_array):
new_pod_array = []
for old_pod in old_pod_array:
if isinstance(old_pod, dict):
new_pod_array.append(old_pod)
else:
new_pod = {
"name": old_pod,
"repository": "internal",
"path": old_pod
}
new_pod_array.append(new_pod)
return new_pod_array
2023-02-17 21:15:35 +00:00
2023-02-17 22:31:43 +00:00
def _run_command(ctx, cluster_name, command):
2023-02-17 21:15:35 +00:00
if ctx.verbose:
print(f"Running command: {command}")
command_dir = os.path.dirname(command)
command_file = os.path.join(".", os.path.basename(command))
2023-02-17 22:31:43 +00:00
command_env = os.environ.copy()
command_env["CERC_SO_COMPOSE_PROJECT"] = cluster_name
if ctx.debug:
command_env["CERC_SCRIPT_DEBUG"] = "true"
command_result = subprocess.run(command_file, shell=True, env=command_env, cwd=command_dir)
2023-02-17 21:15:35 +00:00
if command_result.returncode != 0:
print(f"FATAL Error running command: {command}")
sys.exit(1)
def _orchestrate_cluster_config(ctx, cluster_config, docker, container_exec_env):
@dataclass
class ConfigDirective:
source_container: str
source_variable: str
destination_container: str
destination_variable: str
if cluster_config is not None:
for container in cluster_config:
container_config = cluster_config[container]
if ctx.verbose:
print(f"{container} config: {container_config}")
for directive in container_config:
pd = ConfigDirective(
container_config[directive].split(".")[0],
container_config[directive].split(".")[1],
container,
directive
)
if ctx.verbose:
print(f"Setting {pd.destination_container}.{pd.destination_variable} = {pd.source_container}.{pd.source_variable}")
# TODO: fix the script paths so they're consistent between containers
source_value = docker.compose.execute(pd.source_container,
["sh", "-c",
f"sh /docker-entrypoint-scripts.d/export-{pd.source_variable}.sh"],
tty=False,
envs=container_exec_env)
# TODO: handle the case that the value is not yet available
if ctx.debug:
print(f"fetched source value: {source_value}")
destination_output = docker.compose.execute(pd.destination_container,
["sh", "-c",
f"sh /scripts/import-{pd.destination_variable}.sh {pd.source_variable}"],
tty=False,
envs=container_exec_env)
if ctx.debug:
print(f"destination output: {destination_output}")
# TODO: detect errors here