Skip to content

Commit

Permalink
mounts: clean up the docker service tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Oct 27, 2023
1 parent a8ad22b commit 67a93c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
7 changes: 1 addition & 6 deletions roles/remote/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
name: rclone
when: use_remote and ('mounts' in ansible_run_tags) and remote_update_rclone

- name: "Initialize 'remote_stopped_docker' variable"
ansible.builtin.set_fact:
remote_stopped_docker: false

- name: Check to see if docker is installed
ansible.builtin.stat:
path: "/usr/bin/docker"
Expand All @@ -37,10 +33,9 @@
register: saltbox_managed_containers
ignore_errors: true

- name: "Set 'containers_list' and 'remote_stopped_docker' variables"
- name: "Set 'containers_list' variable"
ansible.builtin.set_fact:
containers_list: "{{ saltbox_managed_containers.stdout }}"
remote_stopped_docker: true

- name: "Stop all running Docker containers"
ansible.builtin.shell: "docker stop {{ containers_list }}"
Expand Down
12 changes: 7 additions & 5 deletions roles/unionfs/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
- name: MergerFS Tasks
ansible.builtin.include_tasks: "subtasks/mergerfs.yml"

- name: Docker Daemon Tasks
ansible.builtin.import_tasks: "subtasks/docker/daemon.yml"
- name: Mounts block
when: ('mounts' in ansible_run_tags)
block:
- name: Docker Daemon Tasks
ansible.builtin.import_tasks: "subtasks/docker/daemon.yml"

# Start Docker Containers
# Start Docker Containers

- name: Docker Containers Start Tasks
ansible.builtin.import_tasks: "subtasks/docker/containers_start.yml"
- name: Docker Containers Start Tasks
ansible.builtin.import_tasks: "subtasks/docker/containers_start.yml"
41 changes: 25 additions & 16 deletions roles/unionfs/tasks/subtasks/docker/containers_stop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@
# GNU General Public License v3.0 #
#########################################################################
---
- name: "Docker | Containers Stop | Gather list of running Docker containers"
ansible.builtin.shell: "docker ps --format '{{ '{{' }} .Names{{ '}}' }}' --filter label=com.github.saltbox.saltbox_managed=true | xargs echo -n"
register: saltbox_managed_containers
ignore_errors: true
- name: Populate Service Facts
ansible.builtin.service_facts:

# This is needed to not conflict with the other role
- name: "Docker | Containers Stop | Set 'containers_list' variable"
- name: Get Docker service state
ansible.builtin.set_fact:
containers_list: "{{ saltbox_managed_containers.stdout }}"
remote_docker_service_running: "{{ (services['docker.service'] is defined) and (services['docker.service']['state'] == 'running') }}"
when: remote_docker_binary.stat.exists

- name: "Docker | Containers Stop | Stop all running Docker containers"
ansible.builtin.shell: "docker stop {{ containers_list }}"
ignore_errors: true
when: (containers_list | trim | length > 0)
- name: Tasks for when Docker exists and is running
when: remote_docker_binary.stat.exists and remote_docker_service_running
block:
- name: "Gather list of running Docker containers"
ansible.builtin.shell: "docker ps --format '{{ '{{' }} .Names{{ '}}' }}' --filter label=com.github.saltbox.saltbox_managed=true | xargs echo -n"
register: saltbox_managed_containers
ignore_errors: true

- name: "Docker | Containers Stop | Stop docker service"
ansible.builtin.systemd:
name: docker
state: stopped
ignore_errors: true
- name: "Set 'containers_list' variable"
ansible.builtin.set_fact:
containers_list: "{{ saltbox_managed_containers.stdout }}"

- name: "Stop all running Docker containers"
ansible.builtin.shell: "docker stop {{ containers_list }}"
ignore_errors: true
when: (containers_list | trim | length > 0)

- name: Stop docker service
ansible.builtin.systemd:
name: docker
state: stopped
24 changes: 12 additions & 12 deletions roles/unionfs/tasks/subtasks/docker/daemon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
mode: "0644"
force: "{{ docker_service_force }}"

- name: "Docker | Daemon | Tasks for when Docker was stopped by Remote role"
when: remote_stopped_docker
block:
- name: "Docker | Daemon | Start docker service"
ansible.builtin.systemd:
name: docker
state: started
enabled: true
daemon_reload: true
- name: "Docker | Daemon | Run systemd daemon-reload"
ansible.builtin.systemd:
daemon_reload: true

- name: "Docker | Daemon | Wait for 30 seconds before commencing"
ansible.builtin.wait_for:
timeout: 30
- name: "Docker | Daemon | Start docker service"
ansible.builtin.systemd:
name: docker
state: restarted
enabled: true

- name: "Docker | Daemon | Wait for 30 seconds before commencing"
ansible.builtin.wait_for:
timeout: 30

0 comments on commit 67a93c0

Please sign in to comment.