stack-orchestrator/app/build_containers.py

108 lines
4.3 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-12 14:54:09 +00:00
# Builds or pulls containers for the system components
# env vars:
2022-08-23 22:02:38 +00:00
# CERC_REPO_BASE_DIR defaults to ~/cerc
2022-08-12 14:54:09 +00:00
# TODO: display the available list of containers; allow re-build of either all or specific containers
import os
from decouple import config
import subprocess
2022-08-24 03:27:42 +00:00
import click
2023-01-08 03:44:08 +00:00
import importlib
2022-09-28 16:07:13 +00:00
from .util import include_exclude_check
2022-08-12 14:54:09 +00:00
2022-08-24 18:53:51 +00:00
# TODO: find a place for this
2022-08-24 18:36:58 +00:00
# epilog="Config provided either in .env or settings.ini or env vars: CERC_REPO_BASE_DIR (defaults to ~/cerc)"
2022-08-12 14:54:09 +00:00
2022-10-04 01:37:18 +00:00
2022-08-24 03:27:42 +00:00
@click.command()
2022-09-28 16:07:13 +00:00
@click.option('--include', help="only build these containers")
@click.option('--exclude', help="don\'t build these containers")
2022-08-24 18:36:58 +00:00
@click.pass_context
2022-09-28 16:07:13 +00:00
def command(ctx, include, exclude):
2022-08-24 18:53:51 +00:00
'''build the set of containers required for a complete stack'''
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
local_stack = ctx.obj.local_stack
2022-08-12 14:54:09 +00:00
2023-01-08 04:02:14 +00:00
# See: https://stackoverflow.com/a/20885799/1701505
from . import data
container_build_dir = importlib.resources.path(data, "container-build")
2022-12-07 18:38:32 +00:00
if local_stack:
2022-10-06 12:50:42 +00:00
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"))
2022-08-12 14:54:09 +00:00
2022-08-24 18:36:58 +00:00
if not quiet:
2022-08-24 03:27:42 +00:00
print(f'Dev Root is: {dev_root_path}')
2022-08-12 14:54:09 +00:00
2022-08-24 03:27:42 +00:00
if not os.path.isdir(dev_root_path):
2022-10-04 01:37:18 +00:00
print('Dev root directory doesn\'t exist, creating')
2022-08-12 14:54:09 +00:00
2023-01-08 03:44:08 +00:00
with importlib.resources.open_text(data, "container-image-list.txt") as container_list_file:
containers = container_list_file.read().splitlines()
2022-08-12 14:54:09 +00:00
if verbose:
2022-08-24 03:27:42 +00:00
print(f'Containers: {containers}')
2023-01-06 20:54:03 +00:00
# TODO: make this configurable
container_build_env = {
2023-01-06 22:57:14 +00:00
"CERC_NPM_URL": "http://gitea.local:3000/api/packages/cerc-io/npm/",
"CERC_NPM_AUTH_TOKEN": os.environ["CERC_NPM_AUTH_TOKEN"],
"CERC_REPO_BASE_DIR": dev_root_path
2023-01-06 20:54:03 +00:00
}
2022-08-24 03:27:42 +00:00
def process_container(container):
if not quiet:
print(f"Building: {container}")
2022-12-07 18:38:32 +00:00
build_dir = os.path.join(container_build_dir, container.replace("/", "-"))
build_script_filename = os.path.join(build_dir, "build.sh")
2022-08-24 03:27:42 +00:00
if verbose:
2022-12-07 18:38:32 +00:00
print(f"Build script filename: {build_script_filename}")
if os.path.exists(build_script_filename):
build_command = build_script_filename
else:
if verbose:
print(f"No script file found: {build_script_filename}, using default build script")
repo_dir = container.split('/')[1]
# TODO: make this less of a hack -- should be specified in some metadata somewhere
# Check if we have a repo for this container. If not, set the context dir to the container-build subdir
repo_full_path = os.path.join(dev_root_path, repo_dir)
repo_dir_or_build_dir = repo_dir if os.path.exists(repo_full_path) else build_dir
2023-01-08 04:02:14 +00:00
build_command = os.path.join(container_build_dir, "default-build.sh") + f" {container} {repo_dir_or_build_dir}"
2022-08-24 18:36:58 +00:00
if not dry_run:
2022-12-07 18:38:32 +00:00
if verbose:
print(f"Executing: {build_command}")
2023-01-06 22:57:14 +00:00
build_result = subprocess.run(build_command, shell=True, env=container_build_env)
2022-08-24 03:27:42 +00:00
# TODO: check result in build_result.returncode
print(f"Result is: {build_result}")
2022-08-24 19:53:48 +00:00
else:
print("Skipped")
2022-08-24 03:27:42 +00:00
for container in containers:
2022-09-28 16:07:13 +00:00
if include_exclude_check(container, include, exclude):
process_container(container)
else:
if verbose:
print(f"Excluding: {container}")