Merge pull request #137 from cerc-io/dboreham/missing-stack-error

Error check stack config
This commit is contained in:
David Boreham 2023-01-19 14:14:34 -07:00 committed by GitHub
commit edbf674df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 32 deletions

View File

@ -1,4 +1,4 @@
# Copyright © 2022 Cerc # Copyright © 2022, 2023 Cerc
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU Affero General Public License as published by
@ -27,8 +27,7 @@ import subprocess
import click import click
import importlib.resources import importlib.resources
from pathlib import Path from pathlib import Path
import yaml from .util import include_exclude_check, get_parsed_stack_config
from .util import include_exclude_check
# TODO: find a place for this # TODO: find a place for this
# epilog="Config provided either in .env or settings.ini or env vars: CERC_REPO_BASE_DIR (defaults to ~/cerc)" # epilog="Config provided either in .env or settings.ini or env vars: CERC_REPO_BASE_DIR (defaults to ~/cerc)"
@ -70,13 +69,8 @@ def command(ctx, include, exclude):
containers_in_scope = [] containers_in_scope = []
if stack: if stack:
# In order to be compatible with Python 3.8 we need to use this hack to get the path: stack_config = get_parsed_stack_config(stack)
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure containers_in_scope = stack_config['containers']
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
containers_in_scope = stack_config['containers']
else: else:
containers_in_scope = all_containers containers_in_scope = all_containers

View File

@ -1,4 +1,4 @@
# Copyright © 2022 Cerc # Copyright © 2022, 2023 Cerc
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU Affero General Public License as published by
@ -23,10 +23,8 @@ import sys
from decouple import config from decouple import config
import click import click
import importlib.resources import importlib.resources
from pathlib import Path
from python_on_whales import docker, DockerException from python_on_whales import docker, DockerException
import yaml from .util import include_exclude_check, get_parsed_stack_config
from .util import include_exclude_check
@click.command() @click.command()
@click.option('--include', help="only build these packages") @click.option('--include', help="only build these packages")
@ -62,13 +60,9 @@ def command(ctx, include, exclude):
packages_in_scope = [] packages_in_scope = []
if stack: if stack:
# In order to be compatible with Python 3.8 we need to use this hack to get the path: stack_config = get_parsed_stack_config(stack)
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure # TODO: syntax check the input here
stack_file_path = Path(__file__).absolute().parent.joinpath("data", "stacks", stack, "stack.yml") packages_in_scope = stack_config['npms']
with stack_file_path:
stack_config = yaml.safe_load(open(stack_file_path, "r"))
# TODO: syntax check the input here
packages_in_scope = stack_config['npms']
else: else:
packages_in_scope = all_packages packages_in_scope = all_packages

View File

@ -1,4 +1,4 @@
# Copyright © 2022 Cerc # Copyright © 2022, 2023 Cerc
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU Affero General Public License as published by
@ -22,8 +22,7 @@ from python_on_whales import DockerClient
import click import click
import importlib.resources import importlib.resources
from pathlib import Path from pathlib import Path
import yaml from .util import include_exclude_check, get_parsed_stack_config
from .util import include_exclude_check
@click.command() @click.command()
@ -62,13 +61,9 @@ def command(ctx, include, exclude, cluster, command, services):
pods_in_scope = [] pods_in_scope = []
if stack: if stack:
# In order to be compatible with Python 3.8 we need to use this hack to get the path: stack_config = get_parsed_stack_config(stack)
# See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure # TODO: syntax check the input here
stack_file_path = Path(__file__).absolute().parent.joinpath("data", "stacks", stack, "stack.yml") pods_in_scope = stack_config['pods']
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: else:
pods_in_scope = all_pods pods_in_scope = all_pods

View File

@ -1,4 +1,4 @@
# Copyright © 2022 Cerc # Copyright © 2022, 2023 Cerc
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU Affero General Public License as published by
@ -13,6 +13,12 @@
# 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 os.path
import sys
import yaml
from pathlib import Path
def include_exclude_check(s, include, exclude): def include_exclude_check(s, include, exclude):
if include is None and exclude is None: if include is None and exclude is None:
return True return True
@ -22,3 +28,23 @@ def include_exclude_check(s, include, exclude):
if exclude is not None: if exclude is not None:
exclude_list = exclude.split(",") exclude_list = exclude.split(",")
return s not in exclude_list return s not in exclude_list
def get_parsed_stack_config(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")
try:
with stack_file_path:
stack_config = yaml.safe_load(open(stack_file_path, "r"))
return stack_config
except FileNotFoundError as error:
# We try here to generate a useful diagnostic error
# First check if the stack directory is present
stack_directory = stack_file_path.parent
if os.path.exists(stack_directory):
print(f"Error: stack.yml file is missing from stack: {stack}")
else:
print(f"Error: stack: {stack} does not exist")
print(f"Exiting, error: {error}")
sys.exit(1)