Attempt graceful shutdown of Docker services before reboot

This commit is contained in:
Sam A. 2023-04-19 22:58:17 +02:00
parent 89751a3125
commit b437aa28ce
Signed by: samsapti
GPG key ID: CBBBE7371E81C4EA
15 changed files with 70 additions and 39 deletions

View file

@ -11,4 +11,4 @@ ssd_mount_point: /opt/{{ ssd_name }}
ssh_keys: ssh_keys:
- sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIFWZGLov8wPBNxuvnaPK+8vv6wK5hHUVEFzXKsN9QeuBAAAADHNzaDpzYW1zYXB0aQ== ssh:samsapti - sk-ssh-ed25519@openssh.com AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIFWZGLov8wPBNxuvnaPK+8vv6wK5hHUVEFzXKsN9QeuBAAAADHNzaDpzYW1zYXB0aQ== ssh:samsapti
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPd/4fQV7CL8/KVwbo/phiV5UdXFBIDlkZ+ps8C7FeRf - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPd/4fQV7CL8/KVwbo/phiV5UdXFBIDlkZ+ps8C7FeRf cardno:14 336 332

View file

@ -30,7 +30,7 @@ case $1 in
if [ "$2" = "-f" ] || [ "$2" = "--force" ]; then if [ "$2" = "-f" ] || [ "$2" = "--force" ]; then
$BASE_CMD --tags "$1" --extra-vars "force_reboot=true" $BASE_CMD --tags "$1" --extra-vars "force_reboot=true"
else else
$BASE_CMD --tags "$1" $BASE_CMD --tags "$1" --extra-vars "reboot=true"
fi fi
;; ;;
services) services)

View file

@ -0,0 +1,33 @@
# vim: ft=yaml.ansible
---
- name: Create base directory for Docker volumes
ansible.builtin.file:
name: "{{ base_volume }}"
owner: root
mode: u=rwx,g=rx,o=rx
state: directory
- name: Copy Docker daemon config file
ansible.builtin.template:
src: daemon.json.j2
dest: /etc/docker/daemon.json
owner: root
mode: u=rw,g=r,o=r
register: daemon_config
- name: Disable and (re)start Docker daemon
ansible.builtin.service:
name: "{{ item }}"
enabled: false
state: "{{ 'restarted' if daemon_config.changed else 'started' }}"
loop:
- docker.socket
- docker.service
- name: Create Docker network for services
community.docker.docker_network:
name: services
ipam_config:
- subnet: 172.16.0.0/16
gateway: 172.16.0.1
state: present

View file

@ -37,6 +37,12 @@
- docker - docker
- docker-compose - docker-compose
- name: Configure Docker
ansible.builtin.import_tasks: config.yml
tags:
- services
- reboot
- name: Set up Docker services - name: Set up Docker services
ansible.builtin.import_tasks: services.yml ansible.builtin.import_tasks: services.yml
tags: tags:

View file

@ -1,37 +1,5 @@
# vim: ft=yaml.ansible # vim: ft=yaml.ansible
--- ---
- name: Copy Docker daemon config file
ansible.builtin.template:
src: daemon.json.j2
dest: /etc/docker/daemon.json
owner: root
mode: u=rw,g=r,o=r
register: daemon_config
- name: Disable and (re)start Docker daemon
ansible.builtin.service:
name: "{{ item }}"
enabled: false
state: "{{ 'restarted' if daemon_config.changed else 'started' }}"
loop:
- docker.socket
- docker.service
- name: Create base directory for Docker volumes
ansible.builtin.file:
name: "{{ base_volume }}"
owner: root
mode: u=rwx,g=rx,o=rx
state: directory
- name: Create Docker network for services
community.docker.docker_network:
name: services
ipam_config:
- subnet: 172.16.0.0/16
gateway: 172.16.0.1
state: present
- name: Deploy services - name: Deploy services
ansible.builtin.include_tasks: services/{{ item.key }}.yml ansible.builtin.include_tasks: services/{{ item.key }}.yml
loop: "{{ services | dict2items }}" loop: "{{ services | dict2items }}"

View file

@ -22,6 +22,7 @@
- name: Deploy Caddy Docker container - name: Deploy Caddy Docker container
community.docker.docker_container: community.docker.docker_container:
name: caddy name: caddy
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: caddy:{{ services.caddy.version }} image: caddy:{{ services.caddy.version }}
restart_policy: always restart_policy: always
networks: networks:

View file

@ -16,6 +16,7 @@
- name: Deploy Emby Docker container - name: Deploy Emby Docker container
community.docker.docker_container: community.docker.docker_container:
name: emby_app name: emby_app
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: emby/embyserver_arm64v8:{{ services.emby.version }} image: emby/embyserver_arm64v8:{{ services.emby.version }}
restart_policy: always restart_policy: always
env: env:

View file

@ -8,6 +8,7 @@
- name: Deploy Monero node Docker container - name: Deploy Monero node Docker container
community.docker.docker_container: community.docker.docker_container:
name: monerod_node name: monerod_node
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: sethsimmons/simple-monerod:{{ services.monerod.version }} image: sethsimmons/simple-monerod:{{ services.monerod.version }}
restart_policy: always restart_policy: always
networks: networks:

View file

@ -37,6 +37,7 @@
- name: Deploy Nextcloud with Docker Compose - name: Deploy Nextcloud with Docker Compose
community.docker.docker_compose: community.docker.docker_compose:
project_name: nextcloud project_name: nextcloud
state: "{{ 'absent' if stop is defined and stop else 'present' }}"
pull: true pull: true
definition: definition:
version: '3.8' version: '3.8'

View file

@ -15,6 +15,7 @@
- name: Deploy Postfix Docker container - name: Deploy Postfix Docker container
community.docker.docker_container: community.docker.docker_container:
name: postfix name: postfix
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: boky/postfix:{{ services.postfix.version }} image: boky/postfix:{{ services.postfix.version }}
restart_policy: always restart_policy: always
env: env:

View file

@ -3,6 +3,7 @@
- name: Deploy Restic with Docker Compose - name: Deploy Restic with Docker Compose
community.docker.docker_compose: community.docker.docker_compose:
project_name: restic project_name: restic
state: "{{ 'absent' if stop is defined and stop else 'present' }}"
pull: true pull: true
definition: definition:
version: '3.8' version: '3.8'

View file

@ -3,6 +3,7 @@
- name: Deploy snowflake-proxy Docker container - name: Deploy snowflake-proxy Docker container
community.docker.docker_container: community.docker.docker_container:
name: snowflake-proxy name: snowflake-proxy
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: thetorproject/snowflake-proxy:{{ services.snowflake.version }} image: thetorproject/snowflake-proxy:{{ services.snowflake.version }}
restart_policy: always restart_policy: always
network_mode: host network_mode: host

View file

@ -8,6 +8,7 @@
- name: Deploy Watchtower Docker container - name: Deploy Watchtower Docker container
community.docker.docker_container: community.docker.docker_container:
name: watchtower name: watchtower
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: containrrr/watchtower:{{ services.watchtower.version }} image: containrrr/watchtower:{{ services.watchtower.version }}
restart_policy: always restart_policy: always
networks: networks:

View file

@ -15,6 +15,7 @@
- name: Deploy Wireguard Docker container - name: Deploy Wireguard Docker container
community.docker.docker_container: community.docker.docker_container:
name: wireguard name: wireguard
state: "{{ 'absent' if stop is defined and stop else 'started' }}"
image: linuxserver/wireguard:{{ services.wireguard.version }} image: linuxserver/wireguard:{{ services.wireguard.version }}
restart_policy: always restart_policy: always
networks: networks:

View file

@ -5,16 +5,31 @@
path: /var/run/reboot-required path: /var/run/reboot-required
register: needs_reboot register: needs_reboot
- name: Reboot host - name: Include docker_services role for service shutdown
ansible.builtin.reboot: ansible.builtin.include_role:
connect_timeout: 120 name: docker_services
tasks_from: services.yml
apply:
ignore_errors: true
vars:
stop: true
when: needs_reboot.stat.exists or when: needs_reboot.stat.exists or
(force_reboot is defined and force_reboot) (force_reboot is defined and force_reboot)
register: reboot
- name: Reboot host
ansible.builtin.reboot:
when: needs_reboot.stat.exists or
(force_reboot is defined and force_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)
- name: Re-gather facts - name: Re-gather facts
ansible.builtin.setup: ansible.builtin.setup:
filter: filter:
- ansible_mounts - ansible_mounts
- ansible_swaptotal_mb - ansible_swaptotal_mb
when: reboot.rebooted is defined and reboot.rebooted when: rebooted.rebooted is defined and rebooted.rebooted