From 2f6dce97384718f6a35d1d9a12d3d1ae5128a271 Mon Sep 17 00:00:00 2001
From: Lucas Alvares Gomes <lucasagomes@gmail.com>
Date: Tue, 15 Dec 2015 16:04:58 +0000
Subject: [PATCH] Keep the console logs for all boots

This patch adds a libvirt hook that saves the VM console log everytime
it shutdown. This is great for troubleshooting because it allow us to
see what happened with the VM on every boot instead of only the last one.

Closes-Bug: #1506829
Related-Bug: #1393099
Change-Id: I4881dfc816a05c0fd2610b0b977e41c44a7e9517
---
 devstack/files/hooks/qemu | 13 +++++++++++++
 devstack/lib/ironic       | 26 ++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100755 devstack/files/hooks/qemu

diff --git a/devstack/files/hooks/qemu b/devstack/files/hooks/qemu
new file mode 100755
index 0000000000..974a8877c8
--- /dev/null
+++ b/devstack/files/hooks/qemu
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+VM_LOG=%LOG_DIR%/$1
+VM_ACTION=$2
+
+if [ $VM_ACTION = "release" ]; then
+    if [ ! -f "${VM_LOG}_console.log" ]; then
+        return 0
+    fi
+
+    NOW=$(date +"%d-%m-%Y-%H:%M:%S")
+    mv "${VM_LOG}_console.log" "${VM_LOG}_console_${NOW}.log"
+fi
diff --git a/devstack/lib/ironic b/devstack/lib/ironic
index a39d025197..c3b4a07b2b 100644
--- a/devstack/lib/ironic
+++ b/devstack/lib/ironic
@@ -35,6 +35,7 @@ GITDIR["ironic-lib"]=$DEST/ironic-lib
 
 IRONIC_DIR=$DEST/ironic
 IRONIC_DEVSTACK_DIR=$IRONIC_DIR/devstack
+IRONIC_DEVSTACK_FILES_DIR=$IRONIC_DEVSTACK_DIR/files
 IRONIC_PYTHON_AGENT_DIR=$DEST/ironic-python-agent
 IRONIC_DATA_DIR=$DATA_DIR/ironic
 IRONIC_STATE_PATH=/var/lib/ironic
@@ -89,6 +90,7 @@ IRONIC_AUTHORIZED_KEYS_FILE=${IRONIC_AUTHORIZED_KEYS_FILE:-$HOME/.ssh/authorized
 # By default, baremetal VMs will console output to file.
 IRONIC_VM_LOG_CONSOLE=${IRONIC_VM_LOG_CONSOLE:-True}
 IRONIC_VM_LOG_DIR=${IRONIC_VM_LOG_DIR:-$IRONIC_DATA_DIR/logs/}
+IRONIC_VM_LOG_ROTATE=$(trueorfalse True IRONIC_VM_LOG_ROTATE)
 
 # Use DIB to create deploy ramdisk and kernel.
 IRONIC_BUILD_DEPLOY_RAMDISK=$(trueorfalse True IRONIC_BUILD_DEPLOY_RAMDISK)
@@ -239,6 +241,7 @@ function _config_ironic_apache_wsgi {
 # runs that would need to clean up.
 function cleanup_ironic {
     sudo rm -rf $IRONIC_AUTH_CACHE_DIR $IRONIC_CONF_DIR
+    sudo rm -rf $IRONIC_VM_LOG_DIR/*
 }
 
 # configure_ironic_dirs() - Create all directories required by Ironic and
@@ -547,11 +550,34 @@ function create_ovs_taps {
     neutron net-update $ironic_net_id --shared true
 }
 
+function setup_qemu_log_hook {
+    local libvirt_service_name
+    local qemu_hook=/etc/libvirt/hooks/qemu
+
+    # Copy the qemu hook to the right directory
+    sudo cp $IRONIC_DEVSTACK_FILES_DIR/hooks/qemu $qemu_hook
+    sudo sed -e "
+        s|%LOG_DIR%|$IRONIC_VM_LOG_DIR|g;
+    " -i $qemu_hook
+
+    # Restart the libvirt daemon
+    libvirt_service_name="libvirt-bin"
+    if is_fedora; then
+        libvirt_service_name="libvirtd"
+    fi
+
+    restart_service $libvirt_service_name
+}
+
 function create_bridge_and_vms {
     # Call libvirt setup scripts in a new shell to ensure any new group membership
     sudo su $STACK_USER -c "$IRONIC_SCRIPTS_DIR/setup-network"
     if [[ "$IRONIC_VM_LOG_CONSOLE" == "True" ]] ; then
         local log_arg="$IRONIC_VM_LOG_DIR"
+
+        if [[ "$IRONIC_VM_LOG_ROTATE" == "True" ]] ; then
+            setup_qemu_log_hook
+        fi
     else
         local log_arg=""
     fi