From d8563cd53dfe199fdcfc29e370fa8af7eedcabfe Mon Sep 17 00:00:00 2001 From: Cyrille Pontvieux Date: Tue, 12 Oct 2021 16:28:01 +0200 Subject: [PATCH] compose-dirs v1.4.0 update --- CHANGELOG.md | 4 +++- capp | 29 +++++++++++++++++++---------- capp-installer-template | 22 +++++++++++----------- testindocker | 2 +- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00fe4ac..b6d4b09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,12 @@ CHANGELOG ========= -Next +version 2.6.0 ------------- - Updated image versions. - `capp` can be tested in docker. +- `compose-dirs` updated to version `1.4.0`. +- More verbose tasks on `deploy`/`undeploy`. version 2.5.0 ------------- diff --git a/capp b/capp index a01faff..aa3d335 100755 --- a/capp +++ b/capp @@ -57,7 +57,7 @@ from yaml import ( from verify_dca import verify_checksum -__version__ = (2, 5, 0) +__version__ = (2, 6, 0) def printflush(*args, **kwargs): @@ -603,6 +603,7 @@ class CApp: return sorted(hooks) def _run_hook(self, hook_file, app, target_env, version, compose_file, target_dir): + printflush(f"Run hook {hook_file}") run(['/usr/bin/sudo', str(hook_file), app, target_env, str(version), str(compose_file), str(target_dir)], stdin=0, check=True, text=True) def _to_bytes(self, value_unit): @@ -802,12 +803,16 @@ class CApp: f.truncate() f.write('\n'.join(deps)) f.write('\n') - run(['sudo', '/usr/local/bin/compose-dirs', 'update'], check=True, text=True) + printflush("Update systemd") + run(['sudo', '/usr/local/bin/compose-dirs', 'update', f'{app}/{target_env}'], check=True, text=True) systemd_svc = self._get_systemd_service_name(app, target_env) if args.start: + printflush("Restart application") run(['sudo', 'systemctl', 'restart', systemd_svc], check=True, text=True) elif run(['systemctl', 'is-active', systemd_svc], capture_output=True).returncode == 0: + printflush("Stop application") run(['sudo', 'systemctl', 'stop', systemd_svc], check=True, text=True) + printflush("Cleaning dangling images…") run(['docker', 'image', 'prune', '-f'], text=True) self._post_deploy(app, target_env, version, target_dir / 'docker-compose.yml', target_dir) @@ -875,7 +880,7 @@ class CApp: signature = b64decode(signature_base64) for pubkey_file in (f for f in self.pubkeys_dir.iterdir() if f.is_file() and f.suffix == '.pub'): if self._verify_signature(signature, compose_file, pubkey_file): - printflush("signature: 'OK'") + printflush("Signature: 'OK'") break else: raise ValueError("metadata signature mismatch") @@ -1060,13 +1065,13 @@ class CApp: self._run_hook(hook_file.resolve(), app, target_env, version, compose_file, target_dir) def _pull_images(self, target_dir): - printflush(end='') if target_dir.is_dir() and (target_dir / 'docker-compose.yml').is_file(): + printflush("Pulling images…") run(['docker-compose', 'pull', '--ignore-pull-failures', '--quiet'], cwd=target_dir, text=True) def _build_images(self, target_dir, quiet=True, use_cache=True): - printflush(end='') if target_dir.is_dir() and (target_dir / 'docker-compose.yml').is_file(): + printflush("Building images…") cmd = ['docker-compose', 'build', '--pull'] if quiet: cmd.append('--quiet') @@ -1075,14 +1080,16 @@ class CApp: run(cmd, cwd=target_dir, text=True) def _load_images(self, image_files): - printflush(end='') for image_file in image_files: + printflush(f"Loading image {image_file}…") run(f'zcat "{str(image_file)}" | docker image load', shell=True, check=True, text=True) - run(['docker', 'image', 'prune', '-f'], text=True) + if image_files: + printflush("Cleaning dangling images…") + run(['docker', 'image', 'prune', '-f'], text=True) def _clean_volumes(self, target_dir): - printflush(end='') if target_dir.is_dir() and (target_dir / 'docker-compose.yml').is_file(): + printflush("Cleaning volumes…") run(['docker-compose', 'down', '-v'], cwd=target_dir, text=True) def _copy_context(self, src_dir, dest_dir): @@ -1117,7 +1124,8 @@ class CApp: f.truncate() f.write('\n'.join(deps)) f.write('\n') - run(['sudo', '/usr/local/bin/compose-dirs', 'update'], check=True, text=True) + printflush("Update systemd") + run(['sudo', '/usr/local/bin/compose-dirs', 'update', f'{app}/{target_env}'], check=True, text=True) if target_dir.is_dir(): with open(target_dir / 'docker-compose.yml', encoding='utf8') as f: dc = full_load(f) @@ -1135,6 +1143,7 @@ class CApp: if location_nginx_config_file.is_file(): location_nginx_config_file.unlink() dc_down_args = '-v --rmi all' if args.all else '--rmi local' + printflush("Destroying application…") run(f"docker-compose down {dc_down_args} 2>&1 | grep -v ^Network | grep -v '^Removing network'", shell=True, cwd=target_dir, text=True) self._post_undeploy(app, target_env, version, target_dir / 'docker-compose.yml', target_dir) rmtree(target_dir, ignore_errors=True) @@ -1205,7 +1214,7 @@ class CApp: self.check_right(Right.STATUS, app=app, env=target_env) self.trace_action('status', vars(args)) svc = self._get_systemd_service_name(app, target_env) - execlp('systemctl', 'systemctl', 'status', '--no-pager', svc) + execlp('systemctl', 'systemctl', 'status', '--no-pager', '--lines=0', svc) def action_logs(self, args): app = args.app diff --git a/capp-installer-template b/capp-installer-template index 7af3607..20b686b 100644 --- a/capp-installer-template +++ b/capp-installer-template @@ -1,13 +1,14 @@ #!/bin/bash set -e CAPP_VER=to_replace -COMPOSE_SYSTEMD_VER=1.3.0 +COMPOSE_SYSTEMD_VER=v1.4.0 def_min_dca_ver=2 def_cont_mem='10G' def_cont_proc=1000 usage() { echo "$0 default_hostname default_email [min_dca_ver=$def_min_dca_ver] [container_max_memory_size=$def_cont_mem] [container_max_process=$def_cont_proc] [nohttps]" >&2 echo " container_max_memory_size should be expressed with M or G units" >&2 + # shellcheck disable=SC2086 exit ${1:-1} } if [ "$1" = "-v" ] || [ "$1" = "--version" ]; then @@ -17,6 +18,7 @@ fi if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then usage 0 fi +# shellcheck disable=SC2046 if [ $(id -u) -ne 0 ]; then echo "Root required" >&2 exit 1 @@ -65,12 +67,12 @@ default_hostname=${dh} EOF mkdir -p /etc/capp/users /etc/capp/pubkeys /etc/capp/rights /etc/capp/hooks.d/{pre_deploy,post_deploy,pre_undeploy,post_undeploy} tmpdir=$(mktemp -d) -cd $tmpdir -curl -Ls https://github.com/jrd/compose-systemd/archive/v${COMPOSE_SYSTEMD_VER}.tar.gz | tar xzf - +cd "$tmpdir" +curl -Ls https://github.com/jrd/compose-systemd/archive/${COMPOSE_SYSTEMD_VER}.tar.gz | tar xzf - tail -n+$offset "$me" | base64 -d | tar xJf - -cp compose-systemd-${COMPOSE_SYSTEMD_VER}/compose-dirs.conf /etc/ -cp compose-systemd-${COMPOSE_SYSTEMD_VER}/compose-dirs /usr/local/bin/ -ln -sf $(which docker-compose) /usr/local/bin/dc +cp compose-systemd-*/compose-dirs.conf /etc/ +cp compose-systemd-*/compose-dirs /usr/local/bin/ +ln -sf "$(which docker-compose)" /usr/local/bin/dc mkdir -p /var/docker-volumes/nginx-proxy/{vhost.d,certs} /etc/compose /home/deploy/dca id -u compose >/dev/null 2>&1 || useradd --home-dir /etc/compose --no-create-home --user-group --shell /usr/sbin/nologin -c "Compose" -l compose chown :compose /var/docker-volumes/nginx-proxy/vhost.d /etc/capp/users /etc/capp/pubkeys /etc/capp/rights @@ -163,11 +165,9 @@ Cmnd_Alias CAPP_CMD = /usr/local/bin/capp EOF chmod u=r,g=r,o= /etc/sudoers.d/{compose,deploy} set +e -cd /etc/compose/dca -docker-compose pull -cd /etc/compose/proxy -docker-compose pull -rm -rf $tmpdir +(cd /etc/compose/dca; docker-compose pull) +(cd /etc/compose/proxy/gen; ./get-nginx-tmpl; cd ..; docker-compose pull) +rm -rf "$tmpdir" /usr/local/bin/compose-dirs install /usr/local/bin/compose-dirs update /usr/local/bin/compose-dirs start diff --git a/testindocker b/testindocker index b8cbdcb..b38f2ff 100755 --- a/testindocker +++ b/testindocker @@ -28,6 +28,7 @@ Then add temporary the IP to your `/etc/hosts` file: IP test.capp myapp.test.capp anysubdomain.test.capp EOF trap 'docker rmi capptest' EXIT +trap 'docker kill capptest; docker rmi capptest' EXIT docker run \ --name capptest \ --rm \ @@ -41,5 +42,4 @@ docker run \ echo "Wait for systemd to start" sleep 1 docker exec -t capptest /root/capp-installer test.capp mail@example.com "$@" -trap 'docker kill capptest; docker rmi capptest' EXIT docker logs -f capptest