From d648be03ecad23d4111412fdd2baa369af88bcc2 Mon Sep 17 00:00:00 2001
From: Clint Byrum <clint@fewbar.com>
Date: Thu, 19 Jul 2018 17:24:22 -0700
Subject: [PATCH] Add safe shutdown systemd jobs to tools

Without these jobs, a compute node that is rebooted or powered off may
violently kill off the VMs running inside of it. This has been kept
separate from the main portion of kolla-ansible since no current role
modifies the systemd jobs of the system.

Change-Id: I0a4424f97b5ad872ff0398258c1dc42d31d0ef07
---
 tools/kolla-libvirt-guests.service | 19 +++++++++++++++++++
 tools/setup-compute-libvirt.yml    | 24 ++++++++++++++++++++++++
 tools/virt-guest-shutdown.target   |  5 +++++
 3 files changed, 48 insertions(+)
 create mode 100644 tools/kolla-libvirt-guests.service
 create mode 100644 tools/setup-compute-libvirt.yml
 create mode 100644 tools/virt-guest-shutdown.target

diff --git a/tools/kolla-libvirt-guests.service b/tools/kolla-libvirt-guests.service
new file mode 100644
index 000000000..b43910ce5
--- /dev/null
+++ b/tools/kolla-libvirt-guests.service
@@ -0,0 +1,19 @@
+[Unit]
+Description=Manage libvirt guests in kolla safely
+After=docker.service
+After=systemd-machined.service
+After=virt-guest-shutdown.target
+Requires=docker.service
+Requires=virt-guest-shutdown.target
+
+[Install]
+WantedBy=multi-user.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+TimeoutStopSec=610
+ExecStart=-/usr/bin/docker exec nova_libvirt /bin/rm -f /var/lib/libvirt/libvirt-guests
+ExecStart=-/usr/bin/docker start nova_compute
+ExecStop=/usr/bin/docker stop nova_compute
+ExecStop=/usr/bin/docker exec nova_libvirt /bin/sh -x /usr/libexec/libvirt-guests.sh shutdown
diff --git a/tools/setup-compute-libvirt.yml b/tools/setup-compute-libvirt.yml
new file mode 100644
index 000000000..8c3f46a54
--- /dev/null
+++ b/tools/setup-compute-libvirt.yml
@@ -0,0 +1,24 @@
+# This playbook modifies the system shutdown sequence to ensure that
+# nova-compute is shutdown safely, including guests, when used in
+# conjunction with systemd-machined and libvirt.
+---
+- name: Set up compute libvirts to shutdown guests safely
+  hosts: compute
+  become: true
+  vars:
+    systemd_artifacts:
+      - kolla-libvirt-guests.service
+      - virt-guest-shutdown.target
+  tasks:
+    - name: Install systemd artifacts
+      copy:
+        src: "{{ item }}"
+        dest: "/usr/lib/systemd/system/{{ item }}"
+      with_items: "{{ systemd_artifacts }}"
+
+    - name: Enable/start systemd artifacts
+      systemd:
+        enabled: yes
+        state: started
+        name: "{{ item }}"
+      with_items: "{{ systemd_artifacts }}"
diff --git a/tools/virt-guest-shutdown.target b/tools/virt-guest-shutdown.target
new file mode 100644
index 000000000..7510cfec3
--- /dev/null
+++ b/tools/virt-guest-shutdown.target
@@ -0,0 +1,5 @@
+[Unit]
+Description=Coordination target for machined controlled guests
+
+[Install]
+WantedBy=multi-user.target
-- 
GitLab