Procházet zdrojové kódy

stop docker containers during dnf updates

Blaine Story před 2 roky
rodič
revize
27adf7e959

+ 26 - 0
roles/docker/tasks/main.yml

@@ -170,3 +170,29 @@
   loop:
     - docker-pull.timer
     - docker-purge.timer
+
+- name: Add script for restarting all containers
+  template:
+    src: restart-containers.sh.j2
+    dest: /usr/local/bin/restart-containers.sh
+    owner: root
+    group: root
+    mode: '0750'
+
+- name: Check if dnf-automatic is enabled
+  stat:
+    path: /etc/systemd/system/dnf-automatic-install.service.d
+  register: dnf_automatic
+
+- name: Modify dnf-automatic to turn off containers while updating
+  template:
+    src: stop-containers.conf.j2
+    dest: /etc/systemd/system/dnf-automatic-install.service.d/stop-containers.conf
+    owner: root
+    group: root
+    mode: '0644'
+  when:
+    - dnf_automatic.stat.exists
+  notify:
+    - Refresh systemd service files
+

+ 8 - 0
roles/docker/templates/restart-containers.sh.j2

@@ -0,0 +1,8 @@
+#!/bin/bash
+# {{ ansible_managed }}
+
+declare -a arr=({{ containers | map(attribute='name') | flatten | map('regex_replace', '^(.*)$', '"\\1"') | flatten | join(' ') }})
+
+for i in "${arr[@]}"; do
+  systemctl restart $i
+done

+ 6 - 0
roles/docker/templates/stop-containers.conf.j2

@@ -0,0 +1,6 @@
+[Unit]
+Conflicts={{ containers | map(attribute='name') | flatten | join('.service ') }}.service
+
+[Service]
+ExecStartPre=-/usr/bin/sleep 15
+ExecStopPost=-/usr/local/bin/restart-containers.sh