diff --git a/provision.sh b/provision.sh index 9e828fa..68c2314 100755 --- a/provision.sh +++ b/provision.sh @@ -4,16 +4,16 @@ set -e usage() { printf '%s\n' "Usage:" - printf '$ %s\n' "$0 [-h|--help]" + printf '$ %s\n' "$0 [--help]" printf '$ %s\n' "$0 [--dry] os" printf '$ %s\n' "$0 [--dry] base" printf '$ %s\n' "$0 [--dry] firewall" printf '$ %s\n' "$0 [--dry] ssh" printf '$ %s\n' "$0 [--dry] docker" printf '$ %s\n' "$0 [--dry] docker_config" - printf '$ %s\n' "$0 [--dry] users [-i|--init]" - printf '$ %s\n' "$0 [--dry] reboot [-f|--force]" - printf '$ %s\n' "$0 [--dry] services [-d|--down] [SINGLE_SERVICE]" + printf '$ %s\n' "$0 [--dry] users [--init]" + printf '$ %s\n' "$0 [--dry] reboot" + printf '$ %s\n' "$0 [--dry] services [--down|--restart|--recreate] [SINGLE_SERVICE]" } install_modules() { @@ -39,46 +39,52 @@ fi case $TAG in "") - install_modules; $BASE_CMD ;; + install_modules; $EXEC "$BASE_CMD" ;; os|base|firewall|ssh|docker|docker_config) - install_modules; $BASE_CMD --tags "$TAG" ;; + install_modules; $EXEC "$BASE_CMD --tags '$TAG'" ;; users) install_modules - if [ "$1" = "-i" ] || [ "$1" = "--init" ]; then + if [ "$1" = "--init" ]; then $EXEC "$BASE_CMD --user root --tags '$TAG'" else $EXEC "$BASE_CMD --tags '$TAG'" fi ;; reboot) - install_modules - - if [ "$1" = "-f" ] || [ "$1" = "--force" ]; then - $EXEC "$BASE_CMD --tags '$TAG' --extra-vars 'force_reboot=true'" - else - $EXEC "$BASE_CMD --tags '$TAG' --extra-vars 'reboot=true'" - fi - ;; + install_modules; $EXEC "$BASE_CMD --tags '$TAG' --extra-vars 'do_reboot=true'" ;; services) install_modules - if [ "$1" = "-d" ] || [ "$1" = "--down" ]; then - DOWN=1 + if [ "$1" = "--down" ]; then + ACTION="down" + shift + elif [ "$1" = "--restart" ]; then + ACTION="restart" + shift + elif [ "$1" = "--recreate" ]; then + ACTION="recreate" shift fi - if [ -z "$DOWN" ] && [ -n "$1" ]; then - VARS="single_service=$1" - elif [ -n "$DOWN" ] && [ -z "$1" ]; then - VARS="stop=true" - elif [ -n "$DOWN" ] && [ -n "$1" ]; then - VARS='{"stop": true, "single_service": "'$1'"}' + case $1 in + --*) + ;; + *) + SINGLE_SERVICE="$1" ;; + esac + + if [ -z "$ACTION" ] && [ -n "$SINGLE_SERVICE" ]; then + VARS="single_service=$SINGLE_SERVICE" + elif [ -n "$ACTION" ] && [ -z "$SINGLE_SERVICE" ]; then + VARS="$ACTION=true" + elif [ -n "$ACTION" ] && [ -n "$SINGLE_SERVICE" ]; then + VARS='{"'$ACTION'": true, "single_service": "'$SINGLE_SERVICE'"}' fi $EXEC "$BASE_CMD --tags '$TAG' $(test -z "$VARS" || echo "--extra-vars '$VARS'")" ;; - -h|--help) + --help) usage ;; *) usage >&2; exit 1 ;; diff --git a/roles/docker_services/tasks/services/caddy.yml b/roles/docker_services/tasks/services/caddy.yml index 7078193..7fa596f 100644 --- a/roles/docker_services/tasks/services/caddy.yml +++ b/roles/docker_services/tasks/services/caddy.yml @@ -22,8 +22,9 @@ - name: Deploy Caddy Docker container community.docker.docker_container: name: caddy - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: caddy:{{ services.caddy.version }} restart_policy: always default_host_ip: '' diff --git a/roles/docker_services/tasks/services/jitsi_meet.yml b/roles/docker_services/tasks/services/jitsi_meet.yml index d7806ed..007672e 100644 --- a/roles/docker_services/tasks/services/jitsi_meet.yml +++ b/roles/docker_services/tasks/services/jitsi_meet.yml @@ -28,8 +28,9 @@ - name: Deploy Jitsi Meet with Docker Compose community.docker.docker_compose: project_name: jitsi_meet - state: "{{ 'absent' if stop is defined and stop else 'present' }}" - restarted: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'present' }}" + restarted: "{{ restart is defined and restart }}" + recreate: "{{ 'always' if recreate is defined and recreate else 'smart' }}" remove_orphans: true pull: true definition: @@ -113,5 +114,5 @@ container: jitsi_meet_prosody_1 command: | /usr/bin/prosodyctl --config /config/prosody.cfg.lua register {{ services.jitsi_meet.username }} meet.jitsi {{ secrets.jitsi_meet.password }} - when: (stop is undefined or not stop) and + when: (down is undefined or not down) and not jitsi_meet_user.stat.exists diff --git a/roles/docker_services/tasks/services/lingva.yml b/roles/docker_services/tasks/services/lingva.yml index c993478..9e65e3f 100644 --- a/roles/docker_services/tasks/services/lingva.yml +++ b/roles/docker_services/tasks/services/lingva.yml @@ -3,8 +3,9 @@ - name: Deploy Lingva Docker container community.docker.docker_container: name: lingva - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: thedaviddelta/lingva-translate:{{ services.lingva.version }} restart_policy: always env: diff --git a/roles/docker_services/tasks/services/samsapti_dev.yml b/roles/docker_services/tasks/services/samsapti_dev.yml index 31468f7..936d12a 100644 --- a/roles/docker_services/tasks/services/samsapti_dev.yml +++ b/roles/docker_services/tasks/services/samsapti_dev.yml @@ -10,8 +10,9 @@ - name: Deploy samsapti.dev Docker container community.docker.docker_container: name: samsapti.dev - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: nginx:{{ services.samsapti_dev.version }} restart_policy: always networks: diff --git a/roles/docker_services/tasks/services/searxng.yml b/roles/docker_services/tasks/services/searxng.yml index 8c84b4a..13087a7 100644 --- a/roles/docker_services/tasks/services/searxng.yml +++ b/roles/docker_services/tasks/services/searxng.yml @@ -32,8 +32,9 @@ - name: Deploy SearXNG with Docker Compose community.docker.docker_compose: project_name: searxng - state: "{{ 'absent' if stop is defined and stop else 'present' }}" - restarted: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'present' }}" + restarted: "{{ restart is defined and restart }}" + recreate: "{{ 'always' if recreate is defined and recreate else 'smart' }}" remove_orphans: true pull: true definition: diff --git a/roles/docker_services/tasks/services/simplexmq_smp.yml b/roles/docker_services/tasks/services/simplexmq_smp.yml index e8cd220..1186c17 100644 --- a/roles/docker_services/tasks/services/simplexmq_smp.yml +++ b/roles/docker_services/tasks/services/simplexmq_smp.yml @@ -15,8 +15,9 @@ - name: Deploy SMP server Docker container community.docker.docker_container: name: smp-server - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: simplexchat/smp-server:{{ services.simplexmq_smp.version }} restart_policy: always default_host_ip: '' diff --git a/roles/docker_services/tasks/services/simplexmq_xftp.yml b/roles/docker_services/tasks/services/simplexmq_xftp.yml index 25fa10e..2ba6b26 100644 --- a/roles/docker_services/tasks/services/simplexmq_xftp.yml +++ b/roles/docker_services/tasks/services/simplexmq_xftp.yml @@ -16,8 +16,9 @@ - name: Deploy XFTP server Docker container community.docker.docker_container: name: xftp-server - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: simplexchat/xftp-server:{{ services.simplexmq_xftp.version }} restart_policy: always default_host_ip: '' diff --git a/roles/docker_services/tasks/services/tor.yml b/roles/docker_services/tasks/services/tor.yml index f6e4586..5cbaff9 100644 --- a/roles/docker_services/tasks/services/tor.yml +++ b/roles/docker_services/tasks/services/tor.yml @@ -3,8 +3,9 @@ - name: Deploy Tor Docker container community.docker.docker_container: name: tor - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: goldy/tor-hidden-service:{{ services.tor.version }} restart_policy: always env: diff --git a/roles/docker_services/tasks/services/watchtower.yml b/roles/docker_services/tasks/services/watchtower.yml index 32e273e..a1d3d5c 100644 --- a/roles/docker_services/tasks/services/watchtower.yml +++ b/roles/docker_services/tasks/services/watchtower.yml @@ -8,8 +8,9 @@ - name: Deploy Watchtower Docker container community.docker.docker_container: name: watchtower - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: containrrr/watchtower:{{ services.watchtower.version }} restart_policy: always networks: diff --git a/roles/docker_services/tasks/services/wkd.yml b/roles/docker_services/tasks/services/wkd.yml index ed00307..8c0b107 100644 --- a/roles/docker_services/tasks/services/wkd.yml +++ b/roles/docker_services/tasks/services/wkd.yml @@ -17,8 +17,9 @@ - name: Deploy WKD Docker container community.docker.docker_container: name: wkd - state: "{{ 'absent' if stop is defined and stop else 'started' }}" - restart: "{{ stop is undefined or not stop }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ recreate is defined and recreate }}" image: nginx:{{ services.wkd.version }} restart_policy: always networks: diff --git a/roles/os_config/tasks/reboot.yml b/roles/os_config/tasks/reboot.yml index 1d72fec..b6de4f9 100644 --- a/roles/os_config/tasks/reboot.yml +++ b/roles/os_config/tasks/reboot.yml @@ -12,17 +12,12 @@ apply: ignore_errors: true vars: - stop: true + down: true when: needs_reboot.stat.exists or - (force_reboot is defined and force_reboot) + (do_reboot is defined and do_reboot) - name: Reboot host ansible.builtin.reboot: when: needs_reboot.stat.exists or - (force_reboot is defined and force_reboot) + (do_reboot is defined and do_reboot) register: rebooted - -- name: End play if reboot is not needed - ansible.builtin.meta: end_play - when: reboot is defined and reboot and - (rebooted.rebooted is undefined or not rebooted.rebooted)