Skip to content

Commit

Permalink
feat: Add default config for project-name (#82)
Browse files Browse the repository at this point in the history
* Add default config for project-name. It also allows to use environment variable for the default project-name
* Split the smoke tests in smaller tests
* Kill all the container before starting a new test
* Replace all the usage of `kxgr` to `sugar` (kxgr is still a valid alias for containers-sugar)
* Fix default services list used from the configuration file
  • Loading branch information
xmnlab authored Jul 17, 2023
1 parent cc41796 commit 1dab380
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 78 deletions.
11 changes: 11 additions & 0 deletions .containers-sugar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ compose-app: docker-compose
env-file: .env
defaults:
group: {{ env.KXGR_GROUP }}
project-name: sugar-{{ env.KXGR_PROJECT_NAME }}
service-groups:
- name: group1
project-name: project1 # optional
Expand Down Expand Up @@ -38,3 +39,13 @@ service-groups:
- name: service1-2
- name: service2-1
- name: service2-2

- name: group-defaults
compose-path:
- tests/containers/group1/compose.yaml
env-file: .env
services:
# default: service1-1,service1-2
available:
- name: service1-1
- name: service1-2
3 changes: 3 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
group: ci-tests-${{ matrix.python_version }}-${{ github.ref }}
cancel-in-progress: true

env:
KXGR_PROJECT_NAME: ${{ matrix.python_version }}-${{ github.ref }}

steps:
- uses: actions/checkout@v3

Expand Down
178 changes: 110 additions & 68 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,82 +55,124 @@ build:
poetry build


.ONESHELL:
.PHONY: smoke-tests
smoke-tests:
# note: don't change the order of the commands heres
# SMOKE TESTS

.ONESHELL:
.PHONY: docker-killall
docker-killall:
set -ex
docker kill `docker ps -q` || true
set +ex

# test default group option
kxgr build --verbose

.ONESHELL:
.PHONY: smoke-test-group-1
smoke-test-group-1: docker-killall
set -ex
# group 1
kxgr --help
kxgr --version
kxgr build --verbose --group group1 --all
kxgr build --verbose --group group1
kxgr build --verbose --group group1 --services service1-1
kxgr pull --verbose --group group1 --all
kxgr pull --verbose --group group1
kxgr pull --verbose --group group1 --services service1-1
kxgr ext start --verbose --group group1 --all --options -d
kxgr ext restart --verbose --group group1 --all --options -d
kxgr exec --verbose --group group1 --service service1-1 --options -T --cmd env
kxgr stop --verbose --group group1 --all
kxgr run --verbose --group group1 --service service1-1 --options -T --cmd env
kxgr down --verbose --group group1
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
set +ex

.ONESHELL:
.PHONY: smoke-test-group-2
smoke-test-group-2: docker-killall
set -ex
# group 2
kxgr build --verbose --group group2 --all
kxgr build --verbose --group group2
kxgr build --verbose --group group2 --services service2-1
kxgr pull --verbose --group group2 --all
kxgr pull --verbose --group group2
kxgr pull --verbose --group group2 --services service2-1
kxgr ext start --verbose --group group2 --all --options -d
kxgr ext restart --verbose --group group2 --all --options -d
kxgr exec --verbose --group group2 --service service2-1 --options -T --cmd env
kxgr stop --verbose --group group2 --all
kxgr run --verbose --group group2 --service service2-1 --options -T --cmd env
kxgr down --verbose --group group2
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
set +ex

.ONESHELL:
.PHONY: smoke-test-group-mix
smoke-test-group-mix: docker-killall
set -ex
# group mix
kxgr build --verbose --group group-mix --all
kxgr build --verbose --group group-mix
kxgr build --verbose --group group-mix --services service1-1,service2-1
kxgr pull --verbose --group group-mix --all
kxgr pull --verbose --group group-mix
kxgr pull --verbose --group group-mix --services service1-1,service2-1
kxgr ext start --verbose --group group-mix --all --options -d
kxgr ext restart --verbose --group group-mix --all --options -d
kxgr exec --verbose --group group-mix --service service2-1 --options -T --cmd env
kxgr stop --verbose --group group-mix --all
kxgr run --verbose --group group-mix --service service2-1 --options -T --cmd env
kxgr down --verbose --group group-mix
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
set +ex

.ONESHELL:
.PHONY: smoke-test-main
smoke-test-main: docker-killall
set -ex
# general tests main profile/plugins
kxgr build --verbose --group group1
kxgr config --verbose --group group1
kxgr create --verbose --group group1
kxgr ext start --verbose --group group1 --options -d
kxgr ext restart --verbose --group group1 --options -d
kxgr exec --verbose --group group1 --service service1-1 --options -T --cmd env
kxgr images --verbose --group group1
kxgr logs --verbose --group group1
kxgr port --verbose --group group1
kxgr ps --verbose --group group1
kxgr pull --verbose --group group1
kxgr push --verbose --group group1
kxgr run --verbose --group group1 --service service1-1 --options -T --cmd env
kxgr top --verbose --group group1
kxgr up --verbose --group group1 --options -d
kxgr version --verbose --group group1
kxgr events --verbose --group group1 --options --json --dry-run
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
sugar port --verbose --group group1
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 --group group1
sugar events --verbose --group group1 --options --json --dry-run
set +ex


.ONESHELL:
.PHONY: smoke-test-defaults
smoke-test-defaults: docker-killall
$(MAKE) docker-killall
set -ex
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 restart --verbose --group group-defaults --options -d
docker ps|grep $$KXGR_PROJECT_NAME
set +ex

.ONESHELL:
.PHONY: smoke-final-tests
smoke-final-tests: docker-killall
# keep these ones at the end
kxgr pause --verbose --group group1
kxgr unpause --verbose --group group1
kxgr kill --verbose --group group1
kxgr stop --verbose --group group1
kxgr rm --verbose --group group1 --options --force
kxgr down --verbose --group group1
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

.ONESHELL:
.PHONY: smoke-tests
smoke-tests: smoke-test-group-1 smoke-test-group-2 smoke-test-group-mix smoke-test-main smoke-test-defaults smoke-final-tests
sugar --help
sugar --version
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ These commands are available in the main profile/plugin, so
you don't need to specify any extra parameter to access them.

For extra commands, we are gathering them into a profile/plugin called
`ext`, so you can access them using something like: `kxgr ext restart`.
`ext`, so you can access them using something like: `sugar ext restart`.

The current available **ext** commands are:

Expand Down Expand Up @@ -100,30 +100,30 @@ service-groups:
- name: service1
```
**NOTE**: containers-sugar has an convenient alias `kxgr` that helps to
**NOTE**: containers-sugar has an convenient alias `sugar` that helps to
keep the command line shorter, where **k** stands for *containers*,
**x** stands for *su* (*shu* sound), and **gr** stands for *gar*.
In another words, you can use `containers-sugar` or `kxgr` CLI.
In another words, you can use `containers-sugar` or `sugar` CLI.

Some examples of how to use it:

* build the defaults services (service1,service3) for group1:
`kxgr build --group group1`
`sugar build --group group1`

* build the all services (there is no default service defined) for group2:
`kxgr build --group group2`
`sugar build --group group2`

* build all services (ignore default) for group1:
`kxgr build --group group1 --all`
`sugar build --group group1 --all`

* start the default services for group1:
`kxgr ext start --group group1`
`sugar ext start --group group1`

* restart all services (ignore defaults) for group1:
`kxgr ext restart --group group1 --all`
`sugar ext restart --group group1 --all`

* restart service1 and service2 for group1:
`kxgr ext restart --group group1 --services service1,service2`
`sugar ext restart --group group1 --services service1,service2`


**NOTE**: If you use: ```default: group: {{ env.ENV }}```, you don't need to
Expand Down
2 changes: 1 addition & 1 deletion containers_sugar/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _get_args():
parser = argparse.ArgumentParser(
prog='containers-sugar',
description=(
'containers-sugar (or kxgr) is a tool that help you to organize'
'containers-sugar (or sugar) is a tool that help you to organize'
"and simplify your containers' stack."
),
epilog=(
Expand Down
13 changes: 13 additions & 0 deletions containers_sugar/sugar.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,21 @@ def _filter_service_group(self):
else:
group_name = self.args.get('service_group')

default_project_name = self.defaults.get('project-name')

for g in groups:
if g['name'] == group_name:
if default_project_name and 'project-name' not in g:
# just use default value if "project-name" is not set
g['project-name'] = default_project_name
if not g.get('services', {}).get('default'):
# if default is not given or it is empty or null,
# use as default all the services available
default_services = [
service['name']
for service in g.get('services', {}).get('available')
]
g['services']['default'] = ','.join(default_services)
self.service_group = g
return

Expand Down

0 comments on commit 1dab380

Please sign in to comment.