From fbf9d17a64974b49172bc5c9c1654621c96e8582 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Tue, 23 Aug 2022 21:27:42 -0600 Subject: [PATCH] Beginnings of click support --- app/build_containers.py | 57 ++++++++++++----------- app/deploy_system.py | 58 ++++++++++++----------- app/setup_repositories.py | 98 ++++++++++++++++++++------------------- orchestrator.py | 13 ++++++ 4 files changed, 124 insertions(+), 102 deletions(-) diff --git a/app/build_containers.py b/app/build_containers.py index 74f8295f..d1cc3c7e 100644 --- a/app/build_containers.py +++ b/app/build_containers.py @@ -25,6 +25,7 @@ import sys import argparse from decouple import config import subprocess +import click parser = argparse.ArgumentParser( description="build the set of containers required for a complete stack", @@ -37,40 +38,42 @@ parser.add_argument("--dry-run", action="store_true", help="don\'t do anything, args = parser.parse_args() -verbose = args.verbose -quiet = args.quiet +@click.command() +def command(): + verbose = args.verbose + quiet = args.quiet -dev_root_path = os.path.expanduser(config("CERC_REPO_BASE_DIR", default="~/cerc")) + dev_root_path = os.path.expanduser(config("CERC_REPO_BASE_DIR", default="~/cerc")) -if not args.quiet: - print(f'Dev Root is: {dev_root_path}') + if not args.quiet: + print(f'Dev Root is: {dev_root_path}') -if not os.path.isdir(dev_root_path): - print(f'Dev root directory doesn\'t exist, creating') + if not os.path.isdir(dev_root_path): + print(f'Dev root directory doesn\'t exist, creating') -with open("container-image-list.txt") as container_list_file: - containers = container_list_file.read().splitlines() + with open("container-image-list.txt") as container_list_file: + containers = container_list_file.read().splitlines() -if verbose: - print(f'Containers: {containers}') - -def process_container(container): - if not quiet: - print(f"Building: {container}") - build_script_filename = os.path.join("container-build",container.replace("/","-"),"build.sh") if verbose: - print(f"Script: {build_script_filename}") - if not os.path.exists(build_script_filename): - print(f"Error, script: {build_script_filename} doesn't exist") - sys.exit(1) - if not args.dry_run: - # We need to export CERC_REPO_BASE_DIR - build_result = subprocess.run(build_script_filename, shell=True, env={'CERC_REPO_BASE_DIR':dev_root_path}) - # TODO: check result in build_result.returncode - print(f"Result is: {build_result}") + print(f'Containers: {containers}') -for container in containers: - process_container(container) + def process_container(container): + if not quiet: + print(f"Building: {container}") + build_script_filename = os.path.join("container-build",container.replace("/","-"),"build.sh") + if verbose: + print(f"Script: {build_script_filename}") + if not os.path.exists(build_script_filename): + print(f"Error, script: {build_script_filename} doesn't exist") + sys.exit(1) + if not args.dry_run: + # We need to export CERC_REPO_BASE_DIR + build_result = subprocess.run(build_script_filename, shell=True, env={'CERC_REPO_BASE_DIR':dev_root_path}) + # TODO: check result in build_result.returncode + print(f"Result is: {build_result}") + + for container in containers: + process_container(container) diff --git a/app/deploy_system.py b/app/deploy_system.py index a80c7b2f..369fe73c 100644 --- a/app/deploy_system.py +++ b/app/deploy_system.py @@ -19,6 +19,7 @@ import os import argparse from decouple import config from python_on_whales import DockerClient +import click def include_exclude_check(s, args): if args.include == None and args.exclude == None: @@ -47,38 +48,39 @@ args = parser.parse_args() verbose = args.verbose quiet = args.quiet -print(args) +@click.command() +def command(): -with open("cluster-list.txt") as cluster_list_file: - clusters = cluster_list_file.read().splitlines() + with open("cluster-list.txt") as cluster_list_file: + clusters = cluster_list_file.read().splitlines() -if verbose: - print(f'Cluster components: {clusters}') + if verbose: + print(f'Cluster components: {clusters}') -# Construct a docker compose command suitable for our purpose + # Construct a docker compose command suitable for our purpose -compose_files = [] -for cluster in clusters: - 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}") + compose_files = [] + for cluster in clusters: + 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}") -if verbose: - print(f"files: {compose_files}") + if verbose: + print(f"files: {compose_files}") -# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ -docker = DockerClient(compose_files=compose_files) + # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ + docker = DockerClient(compose_files=compose_files) -command = args.command[0] -if not args.dry_run: - 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() + command = args.command[0] + if not args.dry_run: + 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() diff --git a/app/setup_repositories.py b/app/setup_repositories.py index b9ee06a4..f185055e 100644 --- a/app/setup_repositories.py +++ b/app/setup_repositories.py @@ -22,6 +22,7 @@ import argparse from decouple import config import git from tqdm import tqdm +import click class GitProgress(git.RemoteProgress): def __init__(self): @@ -52,58 +53,61 @@ parser.add_argument("--pull", action="store_true", help="pull from remote in alr args = parser.parse_args() -verbose = args.verbose -quiet = args.quiet +@click.command() +def command(): -dev_root_path = os.path.expanduser(config("DEV_ROOT", default="~/cerc")) + verbose = args.verbose + quiet = args.quiet -if not args.quiet: - print(f'Dev Root is: {dev_root_path}') + dev_root_path = os.path.expanduser(config("DEV_ROOT", default="~/cerc")) -if not os.path.isdir(dev_root_path): - if not quiet: - print(f'Dev root directory doesn\'t exist, creating') - os.makedirs(dev_root_path) + if not args.quiet: + print(f'Dev Root is: {dev_root_path}') -with open("repository-list.txt") as repository_list_file: - repos = repository_list_file.read().splitlines() + if not os.path.isdir(dev_root_path): + if not quiet: + print(f'Dev root directory doesn\'t exist, creating') + os.makedirs(dev_root_path) -if verbose: - print (f'Repos: {repos}') + with open("repository-list.txt") as repository_list_file: + repos = repository_list_file.read().splitlines() -def process_repo(repo): - full_github_repo_path = f'git@github.com:{repo}' - repoName = repo.split("/")[-1] - full_filesystem_repo_path = os.path.join(dev_root_path, repoName) - is_present = os.path.isdir(full_filesystem_repo_path) - if not quiet: - present_text = f'already exists active branch: {git.Repo(full_filesystem_repo_path).active_branch}' if is_present else 'Needs to be fetched' - print(f'Checking: {full_filesystem_repo_path}: {present_text}') - # Quick check that it's actually a repo - if is_present: - if not is_git_repo(full_filesystem_repo_path): - print(f'Error: {full_filesystem_repo_path} does not contain a valid git repository') - sys.exit(1) - else: - if args.pull: - if verbose: - print(f'Running git pull for {full_filesystem_repo_path}') - if not args.check_only: - repo = git.Repo(full_filesystem_repo_path) - origin = repo.remotes.origin - origin.pull(progress = None if quiet else GitProgress()) - else: - print("(git pull skipped)") - if not is_present: - # Clone - if verbose: - print(f'Running git clone for {full_github_repo_path} into {full_filesystem_repo_path}') - if not args.check_only: - git.Repo.clone_from(full_github_repo_path, full_filesystem_repo_path, - progress = None if quiet else GitProgress()) - else: - print("(git clone skipped)") + if verbose: + print (f'Repos: {repos}') + + def process_repo(repo): + full_github_repo_path = f'git@github.com:{repo}' + repoName = repo.split("/")[-1] + full_filesystem_repo_path = os.path.join(dev_root_path, repoName) + is_present = os.path.isdir(full_filesystem_repo_path) + if not quiet: + present_text = f'already exists active branch: {git.Repo(full_filesystem_repo_path).active_branch}' if is_present else 'Needs to be fetched' + print(f'Checking: {full_filesystem_repo_path}: {present_text}') + # Quick check that it's actually a repo + if is_present: + if not is_git_repo(full_filesystem_repo_path): + print(f'Error: {full_filesystem_repo_path} does not contain a valid git repository') + sys.exit(1) + else: + if args.pull: + if verbose: + print(f'Running git pull for {full_filesystem_repo_path}') + if not args.check_only: + repo = git.Repo(full_filesystem_repo_path) + origin = repo.remotes.origin + origin.pull(progress = None if quiet else GitProgress()) + else: + print("(git pull skipped)") + if not is_present: + # Clone + if verbose: + print(f'Running git clone for {full_github_repo_path} into {full_filesystem_repo_path}') + if not args.check_only: + git.Repo.clone_from(full_github_repo_path, full_filesystem_repo_path, + progress = None if quiet else GitProgress()) + else: + print("(git clone skipped)") -for repo in repos: - process_repo(repo) + for repo in repos: + process_repo(repo) diff --git a/orchestrator.py b/orchestrator.py index 09e4aa92..9212de13 100644 --- a/orchestrator.py +++ b/orchestrator.py @@ -13,3 +13,16 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import click + +from .app import setup_repositories +from .app import build_containers +from .app import deploy_system + +@click.group() +def main_command_group(): + pass + +main_command_group.add_command(setup_repositories.command) +main_command_group.add_command(build_containers.command) +main_command_group.add_command(deploy_system.command)