From 23faf8f1098b9f64d476e8c2df41591a30d1c901 Mon Sep 17 00:00:00 2001 From: Sam Al-Sapti Date: Sat, 5 Aug 2023 14:36:12 +0200 Subject: [PATCH] Add --restart and --recreate --- provision.sh | 29 ++++++++++++++----- roles/docker_services/tasks/config.yml | 2 +- .../docker_services/tasks/services/caddy.yml | 6 ++-- roles/docker_services/tasks/services/emby.yml | 5 ++-- roles/docker_services/tasks/services/ipfs.yml | 5 ++-- .../tasks/services/monerod.yml | 5 ++-- .../tasks/services/nextcloud.yml | 5 ++-- .../docker_services/tasks/services/pihole.yml | 5 ++-- .../tasks/services/postfix.yml | 5 ++-- .../docker_services/tasks/services/restic.yml | 5 ++-- .../tasks/services/snowflake.yml | 5 ++-- .../tasks/services/watchtower.yml | 5 ++-- .../tasks/services/wireguard.yml | 5 ++-- roles/os_config/tasks/reboot.yml | 2 +- 14 files changed, 56 insertions(+), 33 deletions(-) diff --git a/provision.sh b/provision.sh index 7e9af8c..68c2314 100755 --- a/provision.sh +++ b/provision.sh @@ -13,7 +13,7 @@ usage() { printf '$ %s\n' "$0 [--dry] docker_config" printf '$ %s\n' "$0 [--dry] users [--init]" printf '$ %s\n' "$0 [--dry] reboot" - printf '$ %s\n' "$0 [--dry] services [--down] [SINGLE_SERVICE]" + printf '$ %s\n' "$0 [--dry] services [--down|--restart|--recreate] [SINGLE_SERVICE]" } install_modules() { @@ -57,16 +57,29 @@ case $TAG in install_modules if [ "$1" = "--down" ]; then - DOWN=1 + 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'")" diff --git a/roles/docker_services/tasks/config.yml b/roles/docker_services/tasks/config.yml index 2e98725..4238ab9 100644 --- a/roles/docker_services/tasks/config.yml +++ b/roles/docker_services/tasks/config.yml @@ -16,7 +16,7 @@ loop: - docker.socket - docker.service - when: stop is undefined or not stop + when: down is undefined or not down - name: Configure cron job to prune unused Docker data weekly ansible.builtin.cron: diff --git a/roles/docker_services/tasks/services/caddy.yml b/roles/docker_services/tasks/services/caddy.yml index cb10f1b..6d3cef3 100644 --- a/roles/docker_services/tasks/services/caddy.yml +++ b/roles/docker_services/tasks/services/caddy.yml @@ -34,9 +34,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 }}" - recreate: "{{ dockerfile.changed }}" + state: "{{ 'absent' if down is defined and down else 'started' }}" + restart: "{{ restart is defined and restart }}" + recreate: "{{ dockerfile.changed or (recreate is defined and recreate) }}" image: custom/caddy:{{ services.caddy.version }}-alpine restart_policy: always default_host_ip: '' diff --git a/roles/docker_services/tasks/services/emby.yml b/roles/docker_services/tasks/services/emby.yml index e5287c7..3212cc5 100644 --- a/roles/docker_services/tasks/services/emby.yml +++ b/roles/docker_services/tasks/services/emby.yml @@ -16,8 +16,9 @@ - name: Deploy Emby Docker container community.docker.docker_container: name: emby - 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: emby/embyserver_arm64v8:{{ services.emby.version }} restart_policy: always env: diff --git a/roles/docker_services/tasks/services/ipfs.yml b/roles/docker_services/tasks/services/ipfs.yml index 5c64dba..77d745d 100644 --- a/roles/docker_services/tasks/services/ipfs.yml +++ b/roles/docker_services/tasks/services/ipfs.yml @@ -22,8 +22,9 @@ - name: Deploy IPFS Kubo Docker container community.docker.docker_container: name: ipfs_kubo - 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: ipfs/kubo:{{ services.ipfs.version }} restart_policy: always default_host_ip: '' diff --git a/roles/docker_services/tasks/services/monerod.yml b/roles/docker_services/tasks/services/monerod.yml index 0bea3da..2cde9cc 100644 --- a/roles/docker_services/tasks/services/monerod.yml +++ b/roles/docker_services/tasks/services/monerod.yml @@ -10,8 +10,9 @@ - name: Deploy Monero node Docker container community.docker.docker_container: name: monerod - 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: sethsimmons/simple-monerod:{{ services.monerod.version }} restart_policy: always default_host_ip: '' diff --git a/roles/docker_services/tasks/services/nextcloud.yml b/roles/docker_services/tasks/services/nextcloud.yml index 8871df2..8550b2c 100644 --- a/roles/docker_services/tasks/services/nextcloud.yml +++ b/roles/docker_services/tasks/services/nextcloud.yml @@ -37,8 +37,9 @@ - name: Deploy Nextcloud with Docker Compose community.docker.docker_compose: project_name: nextcloud - 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' }}" pull: true definition: version: '3.8' diff --git a/roles/docker_services/tasks/services/pihole.yml b/roles/docker_services/tasks/services/pihole.yml index c4c234b..82460f4 100644 --- a/roles/docker_services/tasks/services/pihole.yml +++ b/roles/docker_services/tasks/services/pihole.yml @@ -37,8 +37,9 @@ - name: Deploy Pi-hole with Docker Compose community.docker.docker_compose: project_name: pihole - 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' }}" pull: true definition: version: '3.8' diff --git a/roles/docker_services/tasks/services/postfix.yml b/roles/docker_services/tasks/services/postfix.yml index e3cc0f6..207261e 100644 --- a/roles/docker_services/tasks/services/postfix.yml +++ b/roles/docker_services/tasks/services/postfix.yml @@ -15,8 +15,9 @@ - name: Deploy Postfix Docker container community.docker.docker_container: name: postfix - 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: boky/postfix:{{ services.postfix.version }} restart_policy: always env: diff --git a/roles/docker_services/tasks/services/restic.yml b/roles/docker_services/tasks/services/restic.yml index f4ad59f..2b33a1e 100644 --- a/roles/docker_services/tasks/services/restic.yml +++ b/roles/docker_services/tasks/services/restic.yml @@ -3,8 +3,9 @@ - name: Deploy Restic with Docker Compose community.docker.docker_compose: project_name: restic - 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' }}" pull: true definition: version: '3.8' diff --git a/roles/docker_services/tasks/services/snowflake.yml b/roles/docker_services/tasks/services/snowflake.yml index 5a61d11..63d8d56 100644 --- a/roles/docker_services/tasks/services/snowflake.yml +++ b/roles/docker_services/tasks/services/snowflake.yml @@ -3,8 +3,9 @@ - name: Deploy snowflake-proxy Docker container community.docker.docker_container: name: snowflake-proxy - 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: thetorproject/snowflake-proxy:{{ services.snowflake.version }} restart_policy: always network_mode: host 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/wireguard.yml b/roles/docker_services/tasks/services/wireguard.yml index d5e2d81..6dba35e 100644 --- a/roles/docker_services/tasks/services/wireguard.yml +++ b/roles/docker_services/tasks/services/wireguard.yml @@ -10,8 +10,9 @@ - name: Deploy Wireguard Docker container community.docker.docker_container: name: wireguard - 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: linuxserver/wireguard:{{ services.wireguard.version }} restart_policy: always default_host_ip: '' diff --git a/roles/os_config/tasks/reboot.yml b/roles/os_config/tasks/reboot.yml index 44a5470..fab3a96 100644 --- a/roles/os_config/tasks/reboot.yml +++ b/roles/os_config/tasks/reboot.yml @@ -12,7 +12,7 @@ apply: ignore_errors: true vars: - stop: true + down: true when: needs_reboot.stat.exists or (do_reboot is defined and do_reboot)