Add --restart and --recreate and always reboot on reboot argument

This commit is contained in:
Sam A. 2023-08-05 18:09:43 +02:00
parent cdae01a6eb
commit d356a5383d
Signed by: samsapti
GPG Key ID: CBBBE7371E81C4EA
12 changed files with 64 additions and 53 deletions

View File

@ -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 ;;

View File

@ -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: ''

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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: ''

View File

@ -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: ''

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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)