Beginnings of click support

This commit is contained in:
David Boreham 2022-08-23 21:27:42 -06:00
parent 8e75a1cedb
commit fbf9d17a64
4 changed files with 124 additions and 102 deletions

View File

@ -25,6 +25,7 @@ import sys
import argparse import argparse
from decouple import config from decouple import config
import subprocess import subprocess
import click
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="build the set of containers required for a complete stack", description="build the set of containers required for a complete stack",
@ -37,24 +38,26 @@ parser.add_argument("--dry-run", action="store_true", help="don\'t do anything,
args = parser.parse_args() args = parser.parse_args()
verbose = args.verbose @click.command()
quiet = args.quiet 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: if not args.quiet:
print(f'Dev Root is: {dev_root_path}') print(f'Dev Root is: {dev_root_path}')
if not os.path.isdir(dev_root_path): if not os.path.isdir(dev_root_path):
print(f'Dev root directory doesn\'t exist, creating') print(f'Dev root directory doesn\'t exist, creating')
with open("container-image-list.txt") as container_list_file: with open("container-image-list.txt") as container_list_file:
containers = container_list_file.read().splitlines() containers = container_list_file.read().splitlines()
if verbose: if verbose:
print(f'Containers: {containers}') print(f'Containers: {containers}')
def process_container(container): def process_container(container):
if not quiet: if not quiet:
print(f"Building: {container}") print(f"Building: {container}")
build_script_filename = os.path.join("container-build",container.replace("/","-"),"build.sh") build_script_filename = os.path.join("container-build",container.replace("/","-"),"build.sh")
@ -69,7 +72,7 @@ def process_container(container):
# TODO: check result in build_result.returncode # TODO: check result in build_result.returncode
print(f"Result is: {build_result}") print(f"Result is: {build_result}")
for container in containers: for container in containers:
process_container(container) process_container(container)

View File

@ -19,6 +19,7 @@ import os
import argparse import argparse
from decouple import config from decouple import config
from python_on_whales import DockerClient from python_on_whales import DockerClient
import click
def include_exclude_check(s, args): def include_exclude_check(s, args):
if args.include == None and args.exclude == None: if args.include == None and args.exclude == None:
@ -47,18 +48,19 @@ args = parser.parse_args()
verbose = args.verbose verbose = args.verbose
quiet = args.quiet quiet = args.quiet
print(args) @click.command()
def command():
with open("cluster-list.txt") as cluster_list_file: with open("cluster-list.txt") as cluster_list_file:
clusters = cluster_list_file.read().splitlines() clusters = cluster_list_file.read().splitlines()
if verbose: if verbose:
print(f'Cluster components: {clusters}') 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 = [] compose_files = []
for cluster in clusters: for cluster in clusters:
if include_exclude_check(cluster, args): if include_exclude_check(cluster, args):
compose_file_name = os.path.join("compose", f"docker-compose-{cluster}.yml") compose_file_name = os.path.join("compose", f"docker-compose-{cluster}.yml")
compose_files.append(compose_file_name) compose_files.append(compose_file_name)
@ -66,14 +68,14 @@ for cluster in clusters:
if not quiet: if not quiet:
print(f"Excluding: {cluster}") print(f"Excluding: {cluster}")
if verbose: if verbose:
print(f"files: {compose_files}") print(f"files: {compose_files}")
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
docker = DockerClient(compose_files=compose_files) docker = DockerClient(compose_files=compose_files)
command = args.command[0] command = args.command[0]
if not args.dry_run: if not args.dry_run:
if command == "up": if command == "up":
if verbose: if verbose:
print("Running compose up") print("Running compose up")

View File

@ -22,6 +22,7 @@ import argparse
from decouple import config from decouple import config
import git import git
from tqdm import tqdm from tqdm import tqdm
import click
class GitProgress(git.RemoteProgress): class GitProgress(git.RemoteProgress):
def __init__(self): def __init__(self):
@ -52,26 +53,29 @@ parser.add_argument("--pull", action="store_true", help="pull from remote in alr
args = parser.parse_args() args = parser.parse_args()
verbose = args.verbose @click.command()
quiet = args.quiet def command():
dev_root_path = os.path.expanduser(config("DEV_ROOT", default="~/cerc")) verbose = args.verbose
quiet = args.quiet
if not args.quiet: dev_root_path = os.path.expanduser(config("DEV_ROOT", default="~/cerc"))
if not args.quiet:
print(f'Dev Root is: {dev_root_path}') print(f'Dev Root is: {dev_root_path}')
if not os.path.isdir(dev_root_path): if not os.path.isdir(dev_root_path):
if not quiet: if not quiet:
print(f'Dev root directory doesn\'t exist, creating') print(f'Dev root directory doesn\'t exist, creating')
os.makedirs(dev_root_path) os.makedirs(dev_root_path)
with open("repository-list.txt") as repository_list_file: with open("repository-list.txt") as repository_list_file:
repos = repository_list_file.read().splitlines() repos = repository_list_file.read().splitlines()
if verbose: if verbose:
print (f'Repos: {repos}') print (f'Repos: {repos}')
def process_repo(repo): def process_repo(repo):
full_github_repo_path = f'git@github.com:{repo}' full_github_repo_path = f'git@github.com:{repo}'
repoName = repo.split("/")[-1] repoName = repo.split("/")[-1]
full_filesystem_repo_path = os.path.join(dev_root_path, repoName) full_filesystem_repo_path = os.path.join(dev_root_path, repoName)
@ -105,5 +109,5 @@ def process_repo(repo):
print("(git clone skipped)") print("(git clone skipped)")
for repo in repos: for repo in repos:
process_repo(repo) process_repo(repo)

View File

@ -13,3 +13,16 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
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)