diff --git a/.makim.yaml b/.makim.yaml index 1631a2d..18db3f4 100644 --- a/.makim.yaml +++ b/.makim.yaml @@ -93,7 +93,13 @@ groups: sugar stop --verbose --group group2 --all sugar run --verbose --group group2 --service service2-1 --options -T --cmd env sugar down --verbose --group group2 - + smoke-services: + help: + dependencies: + - target: docker.killall + run: | + sugar build --verbose --config-file tests/containers/.services.sugar.yaml + smoke-mix: help: Run smoke tests for group mix dependencies: @@ -174,6 +180,7 @@ groups: - target: tests.smoke-main - target: tests.smoke-defaults - target: tests.smoke-final + - target: tests.smoke-services run: | sugar --help sugar --version diff --git a/makim.yaml b/makim.yaml deleted file mode 100644 index cb8cc87..0000000 --- a/makim.yaml +++ /dev/null @@ -1,186 +0,0 @@ -version: 1.0.0 -shell: bash -groups: - clean: - env-file: .env - targets: - all: - help: Remove build artifacts, compiled files, and cache - run: | - rm -fr build/ - rm -fr dist/ - rm -fr .eggs/ - find . -name '*.egg-info' -exec rm -fr {} + - find . -name '*.egg' -exec rm -f {} + - find . -name '*.pyc' -exec rm -f {} + - find . -name '__pycache__' -exec rm -fr {} + - find . -name '*~' -exec rm -f {} + - rm -f .coverage - rm -fr htmlcov/ - rm -fr .pytest_cache - - docs: - help: Commands for building and previewing the documentation - targets: - build: - help: Build the documentation - run: mkdocs build --config-file docs/mkdocs.yaml - - preview: - help: Preview the documentation - dependencies: - - target: docs-build - run: mkdocs serve --watch docs --config-file docs/mkdocs.yaml - - package: - help: helper commands for handling the package - targets: - build: - help: Build the project - run: poetry build - - tests: - help: Test commands - targets: - linter: - help: Run lint checks - run: | - pre-commit install - pre-commit run --all-files --verbose - - unit: - help: Run tests - args: - params: - help: Extra parameters for pytest - type: str - default: "-vv" - run: pytest -s {{ args.params }} tests - - smoke-1: - help: Run smoke tests for group 1 - dependencies: - - target: docker.killall - run: | - sugar build --verbose - sugar build --verbose --group group1 --all - sugar build --verbose --group group1 - sugar build --verbose --group group1 --services service1-1 - sugar pull --verbose --group group1 --all - sugar pull --verbose --group group1 - sugar pull --verbose --group group1 --services service1-1 - sugar ext start --verbose --group group1 --all --options -d - sugar ext restart --verbose --group group1 --all --options -d - sugar exec --verbose --group group1 --service service1-1 --options -T --cmd env - sugar stop --verbose --group group1 --all - sugar run --verbose --group group1 --service service1-1 --options -T --cmd env - sugar down --verbose --group group1 - - smoke-2: - help: Run smoke tests for group 2 - dependencies: - - target: docker.killall - run: | - sugar build --verbose --group group2 --all - sugar build --verbose --group group2 - sugar build --verbose --group group2 --services service2-1 - sugar pull --verbose --group group2 --all - sugar pull --verbose --group group2 - sugar pull --verbose --group group2 --services service2-1 - sugar ext start --verbose --group group2 --all --options -d - sugar ext restart --verbose --group group2 --all --options -d - sugar exec --verbose --group group2 --service service2-1 --options -T --cmd env - sugar stop --verbose --group group2 --all - sugar run --verbose --group group2 --service service2-1 --options -T --cmd env - sugar down --verbose --group group2 - - smoke-mix: - help: Run smoke tests for group mix - dependencies: - - target: docker.killall - run: | - sugar build --verbose --group group-mix --all - sugar build --verbose --group group-mix - sugar build --verbose --group group-mix --services service1-1,service2-1 - sugar pull --verbose --group group-mix --all - sugar pull --verbose --group group-mix - sugar pull --verbose --group group-mix --services service1-1,service2-1 - sugar ext start --verbose --group group-mix --all --options -d - sugar ext restart --verbose --group group-mix --all --options -d - sugar exec --verbose --group group-mix --service service2-1 --options -T --cmd env - sugar stop --verbose --group group-mix --all - sugar run --verbose --group group-mix --service service2-1 --options -T --cmd env - sugar down --verbose --group group-mix - - smoke-main: - help: Run smoke tests for group main - dependencies: - - target: docker.killall - run: | - # general tests main profile/plugins - sugar build --verbose --group group1 - sugar config --verbose --group group1 - sugar create --verbose --group group1 - sugar ext start --verbose --group group1 --options -d - sugar ext restart --verbose --group group1 --options -d - sugar exec --verbose --group group1 --service service1-1 --options -T --cmd env - sugar images --verbose --group group1 - sugar logs --verbose --group group1 - # port is not complete supported - # sugar port --verbose --group group1 --service service1-1 - sugar ps --verbose --group group1 - sugar pull --verbose --group group1 - sugar push --verbose --group group1 - sugar run --verbose --group group1 --service service1-1 --options -T --cmd env - sugar top --verbose --group group1 - sugar up --verbose --group group1 --options -d - sugar version --verbose - # port is not complete supported - # sugar events --verbose --group group1 --service service1-1 --options --json --dry-run - - smoke-defaults: - help: Run smoke tests for group defaults - dependencies: - - target: docker.killall - run: | - export KXGR_PROJECT_NAME="test-`python -c 'from uuid import uuid4; print(uuid4().hex[:7])'`" - echo $KXGR_PROJECT_NAME - sugar build --verbose --group group-defaults - sugar ext start --verbose --group group-defaults --options -d - sugar ext restart --verbose --group group-defaults --options -d - docker ps|grep $KXGR_PROJECT_NAME - sugar ext stop --verbose --group group-defaults - - smoke-final: - help: Run final smoke tests - dependencies: - - target: docker.killall - run: | - sugar ext restart --verbose --group group-defaults --options -d - sugar pause --verbose --group group1 - sugar unpause --verbose --group group1 - sugar kill --verbose --group group1 - sugar stop --verbose --group group1 - sugar rm --verbose --group group1 --options --force - sugar down --verbose --group group1 - - smoke: - help: Run final smoke tests - dependencies: - - target: docker.killall - - target: tests.smoke-1 - - target: tests.smoke-2 - - target: tests.smoke-mix - - target: tests.smoke-main - - target: tests.smoke-defaults - - target: tests.smoke-final - run: | - sugar --help - sugar --version - - docker: - help: Commands for docker - targets: - killall: - help: Kill all running Docker containers - run: docker kill `docker ps -q` || true diff --git a/src/sugar/core.py b/src/sugar/core.py index 8e66c9c..10bf567 100644 --- a/src/sugar/core.py +++ b/src/sugar/core.py @@ -66,6 +66,7 @@ def __init__( self._load_config() self._load_env() self._load_defaults() + self._load_root_services() self._verify_args() self._load_compose_app() self._load_compose_args() @@ -118,6 +119,34 @@ def _call_compose_app( def _check_config_file(self): return Path(self.config_file).exists() + # Check if services item is given + def _check_services_item(self): + return hasattr(self.config, 'services') + + # set default group main + def _load_root_services(self) -> None: + """Load services attribute in the root of the configuration.""" + # must set the default group + services = self.config.get('services', {}) + + if not services: + return + + self.config['groups'] = { + 'main': { + 'project-name': services.get('project-name'), + 'compose-path': services.get('compose-path'), + 'env-file': services.get('env-file'), + 'services': { + 'default': services.get('default'), + 'available': services.get('available'), + }, + } + } + self.defaults['group'] = 'main' + self.service_group = deepcopy(self.config['groups']['main']) + del self.config['services'] + def _filter_service_group(self): groups = self.config['groups'] @@ -169,6 +198,19 @@ def _load_config(self): f_content = io.StringIO(content) self.config = yaml.safe_load(f_content) + # check if either services or groups are present + if not (self.config.get('services') or self.config.get('groups')): + KxgrLogs.raise_error( + 'Either `services` OR `groups` flag must be given', + KxgrErrorType.KXGR_INVALID_CONFIGURATION, + ) + # check if both services and groups are present + if self.config.get('services') and self.config.get('groups'): + KxgrLogs.raise_error( + '`services` and `groups` flags given, only 1 is allowed.', + KxgrErrorType.KXGR_INVALID_CONFIGURATION, + ) + def _load_compose_app(self): compose_cmd = self.config.get('compose-app', '') if compose_cmd.replace(' ', '-') != 'docker-compose': @@ -186,7 +228,7 @@ def _load_compose_app(self): def _load_compose_args(self): self._filter_service_group() - if 'env-file' in self.service_group: + if self.service_group.get('env-file'): self.compose_args.extend( ['--env-file', self.service_group['env-file']] ) diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..0070883 --- /dev/null +++ b/test.yaml @@ -0,0 +1,4 @@ +- name: group2 + project-name: null #optional + compose-path: containers/tests2/group2/compose.yaml + env-file: .env diff --git a/tests/containers/.services.sugar.yaml b/tests/containers/.services.sugar.yaml new file mode 100644 index 0000000..ddf9d21 --- /dev/null +++ b/tests/containers/.services.sugar.yaml @@ -0,0 +1,12 @@ +version: 1.0 +compose-app: docker compose +defaults: + project-name: sugar-{{ env.KXGR_PROJECT_NAME }} +services: + project-name: project1 # optional + compose-path: tests/containers/group1/compose.yaml + default: service1-1,service1-3 + available: + - name: service1-1 + - name: service1-2 + - name: service1-3