From 9cc129a7d86dda17be3dd819bf641b2d199da7f5 Mon Sep 17 00:00:00 2001
From: Vinzenz Feenstra <vfeenstr@redhat.com>
Date: Fri, 23 May 2014 09:58:26 +0200
Subject: [PATCH] Implement default configuration values

When adding new config values to the configuration files, it's possible
that due to the installation the configuration files won't get updated,
when they are modified by an user/administrator on that given system.

By using defaults, we can override this problem and ensure a proper
application start, when config values are missing which are mandatory,
to the functionality.

Change-Id: I5ce9d457e826448aad205ce4abda5dc4b0210804
Signed-off-by: Vinzenz Feenstra <vfeenstr@redhat.com>
Bug-Url: https://bugzilla.redhat.com/1101129
---
 Makefile.am                                     |  1 +
 configurations/Makefile.am                      | 16 +++++++
 configurations/default-logger.conf              | 41 ++++++++++++++++
 configurations/default.conf                     | 19 ++++++++
 configurations/ovirt-guest-agent.conf           | 63 +++++++++++++++++++++++++
 configure.ac                                    |  1 +
 ovirt-guest-agent.rhel6.spec                    |  3 ++
 ovirt-guest-agent.spec                          |  3 ++
 ovirt-guest-agent/Makefile.am                   |  8 +---
 ovirt-guest-agent/Makefile.el5                  |  3 +-
 ovirt-guest-agent/OVirtGuestService.py          | 28 +++++++++--
 ovirt-guest-agent/bytesio.py                    | 40 ++++++++++++++++
 ovirt-guest-agent/ovirt-guest-agent.conf        | 61 ------------------------
 ovirt-guest-agent/ovirt-guest-agent.py          | 25 +++++++++-
 19 files changed, 341 insertions(+), 127 deletions(-)
 create mode 100644 configurations/Makefile.am
 create mode 100644 configurations/default-logger.conf
 create mode 100644 configurations/default.conf
 create mode 100644 configurations/ovirt-guest-agent.conf
 create mode 100644 ovirt-guest-agent/bytesio.py
 delete mode 100644 ovirt-guest-agent/ovirt-guest-agent.conf

diff --git a/Makefile.am b/Makefile.am
index ff73c6a..acc2eee 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,7 @@
 ACLOCAL_AMFLAGS=-I m4
 
 SUBDIRS =                \
+    configurations       \
     ovirt-guest-agent    \
     GinaSSO              \
     tests                \
diff --git a/configurations/Makefile.am b/configurations/Makefile.am
new file mode 100644
index 0000000..2f2864e
--- /dev/null
+++ b/configurations/Makefile.am
@@ -0,0 +1,13 @@
+
+EXTRA_DIST= \
+    $(NULL)
+
+agentconfdir=$(sysconfdir)
+dist_agentconf_DATA=ovirt-guest-agent.conf
+
+agentdefaultconfdir=$(datadir)/ovirt-guest-agent
+dist_agentdefaultconf_DATA=\
+    default.conf \
+    default-logger.conf \
+    $(NULL)
+
diff --git a/configurations/default-logger.conf b/configurations/default-logger.conf
new file mode 100644
index 0000000..898fb92
--- /dev/null
+++ b/configurations/default-logger.conf
@@ -0,0 +1,41 @@
+[loggers]
+keys=root
+
+[handlers]
+keys=console,syslog,logfile
+
+[formatters]
+keys=long,simple,none,sysform
+
+[logger_root]
+level=INFO
+handlers=logfile
+propagate=0
+
+[handler_syslog]
+class=handlers.SysLogHandler
+formatter=sysform
+args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
+
+[handler_logfile]
+class=handlers.RotatingFileHandler
+args=('/var/log/ovirt-guest-agent/ovirt-guest-agent.log', 'a', 100*1024, 5)
+formatter=long
+
+[handler_console]
+class: StreamHandler
+args: []
+formatter: none
+
+[formatter_simple]
+format: %(name)s:%(levelname)s:  %(message)s
+
+[formatter_none]
+format: %(message)s
+
+[formatter_long]
+format: %(threadName)s::%(levelname)s::%(asctime)s::%(module)s::%(lineno)d::%(name)s::%(message)s
+
+[formatter_sysform]
+format= %(asctime)s %(levelname)s %(message)s
+datefmt=
diff --git a/configurations/default.conf b/configurations/default.conf
new file mode 100644
index 0000000..c377f0c
--- /dev/null
+++ b/configurations/default.conf
@@ -0,0 +1,19 @@
+#
+# default.conf
+#
+
+[general]
+
+heart_beat_rate = 5
+report_user_rate = 10
+report_num_cpu_rate = 60
+report_application_rate = 120
+report_disk_usage = 300
+
+# RHEL/Fedora Packages: kernel ovirt-guest-agent-common xorg-x11-drv-qxl
+# Debian Packages:      linux-image xserver-xorg-video-qxl
+applications_list = kernel rhevm-guest-agent-common xorg-x11-drv-qxl linux-image xserver-xorg-video-qxl
+ignored_fs = rootfs tmpfs autofs cgroup selinuxfs udev mqueue nfsd proc sysfs devtmpfs hugetlbfs rpc_pipefs devpts securityfs debugfs binfmt_misc fuse.gvfsd-fuse fuse.gvfs-fuse-daemon fusectl usbfs
+
+[virtio]
+device = /dev/virtio-ports/com.redhat.rhevm.vdsm
diff --git a/configurations/ovirt-guest-agent.conf b/configurations/ovirt-guest-agent.conf
new file mode 100644
index 0000000..80509f5
--- /dev/null
+++ b/configurations/ovirt-guest-agent.conf
@@ -0,0 +1,63 @@
+#
+# ovirt-guest-agent.conf
+#
+
+[general]
+
+# heart_beat_rate = 5
+# report_user_rate = 10
+# report_num_cpu_rate = 60
+# report_application_rate = 120
+# report_disk_usage = 300
+
+## Generic:              ovirt-guest-agent
+## RHEL/Fedora Packages: kernel ovirt-guest-agent-common xorg-x11-drv-qxl
+## Debian Packages:      linux-image xserver-xorg-video-qxl
+## openSUSE Packages:    xf86-video-qxl kernel-desktop kernel-default kernel-trace kernel-vanilla kernel-debug kernel-ec2 kernel-xen
+# applications_list = kernel ovirt-guest-agent ovirt-guest-agent-common xorg-x11-drv-qxl linux-image xserver-xorg-video-qxl xf86-video-qxl kernel-desktop kernel-default kernel-trace kernel-vanilla kernel-debug kernel-ec2 kernel-xen
+# ignored_fs = rootfs tmpfs autofs cgroup selinuxfs udev mqueue nfsd proc sysfs devtmpfs hugetlbfs rpc_pipefs devpts securityfs debugfs binfmt_misc fuse.gvfsd-fuse fuse.gvfs-fuse-daemon fusectl usbfs
+
+[virtio]
+# device = /dev/virtio-ports/com.redhat.rhevm.vdsm
+
+#[loggers]
+#keys=root
+
+#[handlers]
+#keys=console,syslog,logfile
+
+#[formatters]
+#keys=long,simple,none,sysform
+
+#[logger_root]
+#level=INFO
+#handlers=logfile
+#propagate=0
+
+#[handler_syslog]
+#class=handlers.SysLogHandler
+#formatter=sysform
+#args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
+
+#[handler_logfile]
+#class=handlers.RotatingFileHandler
+#args=('/var/log/ovirt-guest-agent/ovirt-guest-agent.log', 'a', 100*1024, 5)
+#formatter=long
+
+#[handler_console]
+#class: StreamHandler
+#args: []
+#formatter: none
+
+#[formatter_simple]
+#format: %(name)s:%(levelname)s:  %(message)s
+
+#[formatter_none]
+#format: %(message)s
+
+#[formatter_long]
+#format: %(threadName)s::%(levelname)s::%(asctime)s::%(module)s::%(lineno)d::%(name)s::%(message)s
+
+#[formatter_sysform]
+#format= %(asctime)s %(levelname)s %(message)s
+#datefmt=
diff --git a/configure.ac b/configure.ac
index 75183fa..fc3d7cb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -208,6 +208,7 @@ AC_SUBST(extensionsdatadir)
 
 AC_CONFIG_FILES([
     Makefile
+    configurations/Makefile
     GinaSSO/Makefile
     ovirt-guest-agent/Makefile
     ovirt-guest-agent/ovirt-guest-agent
diff --git a/ovirt-guest-agent.rhel6.spec b/ovirt-guest-agent.rhel6.spec
index 7cff303..e5997d6 100644
--- a/ovirt-guest-agent.rhel6.spec
+++ b/ovirt-guest-agent.rhel6.spec
@@ -123,6 +123,9 @@ fi
 %{_datadir}/ovirt-guest-agent/ovirt-shutdown
 %{_datadir}/ovirt-guest-agent/ovirt-hibernate
 
+%attr (644,root,root) %{_datadir}/ovirt-guest-agent/default.conf
+%attr (644,root,root) %{_datadir}/ovirt-guest-agent/default-logger.conf
+
 %attr (755,root,root) %{_datadir}/ovirt-guest-agent/LockActiveSession.py*
 %attr (755,root,root) %{_datadir}/ovirt-guest-agent/hibernate
 
diff --git a/ovirt-guest-agent.spec b/ovirt-guest-agent.spec
index a66723d..39c3a3e 100644
--- a/ovirt-guest-agent.spec
+++ b/ovirt-guest-agent.spec
@@ -232,6 +232,9 @@ fi
 %attr (755,root,root) %{_datadir}/ovirt-guest-agent/LockActiveSession.py*
 %attr (755,root,root) %{_datadir}/ovirt-guest-agent/hibernate
 
+%attr (644,root,root) %{_datadir}/ovirt-guest-agent/default.conf
+%attr (644,root,root) %{_datadir}/ovirt-guest-agent/default-logger.conf
+
 %{_unitdir}/ovirt-guest-agent.service
 
 %files pam-module
diff --git a/ovirt-guest-agent/Makefile.am b/ovirt-guest-agent/Makefile.am
index 9257363..f5dca4e 100644
--- a/ovirt-guest-agent/Makefile.am
+++ b/ovirt-guest-agent/Makefile.am
@@ -21,12 +21,9 @@ noinst_PYTHON=\
     $(NULL)
 
 dist_pkgdata_SCRIPTS= \
-	hibernate \
+    hibernate \
     LockActiveSession.py \
-	$(NULL)
-
-agentconfdir=$(sysconfdir)
-agentconf_DATA=ovirt-guest-agent.conf
+    $(NULL)
 
 dbusconfdir=$(sysconfdir)/dbus-1/system.d
 dbusconf_DATA=org.ovirt.vdsm.Credentials.conf
@@ -55,7 +52,6 @@ EXTRA_DIST= \
     Makefile.el5 \
     README-windows.txt \
     win-guest-agent-build-exe.bat \
-    ovirt-guest-agent.ini \
     $(NULL)
 
 CLEANFILES = \
diff --git a/ovirt-guest-agent/Makefile.el5 b/ovirt-guest-agent/Makefile.el5
index de3b60e..69d69f7 100644
--- a/ovirt-guest-agent/Makefile.el5
+++ b/ovirt-guest-agent/Makefile.el5
@@ -4,7 +4,8 @@
 #
 
 FILES=ovirt-guest-agent.py OVirtAgentLogic.py VirtIoChannel.py \
-	GuestAgentLinux2.py hibernate
+	GuestAgentLinux2.py hibernate bytesio.py ../configurations/default.conf \
+	../configurations/default-logger.conf
 
 AGENTDIR=/usr/share/ovirt-guest-agent
 CONFDIR=/etc
diff --git a/ovirt-guest-agent/OVirtGuestService.py b/ovirt-guest-agent/OVirtGuestService.py
index b785fa0..fbb2186 100644
--- a/ovirt-guest-agent/OVirtGuestService.py
+++ b/ovirt-guest-agent/OVirtGuestService.py
@@ -8,11 +8,16 @@ from GuestAgentWin32 import WinVdsAgent
 import logging
 import logging.config
 import servicemanager
-import ConfigParser
 import os
+import os.path
 import _winreg
+import ConfigParser
+import io
+import cStringIO
 
 AGENT_CONFIG = 'rhev-agent.ini'
+AGENT_DEFAULT_CONFIG = 'default.ini'
+AGENT_DEFAULT_LOG_CONFIG = 'default-logger.ini'
 
 # Values from WM_WTSSESSION_CHANGE message
 # (http://msdn.microsoft.com/en-us/library/aa383828.aspx)
@@ -32,7 +37,7 @@ class OVirtGuestService(win32serviceutil.ServiceFramework):
         win32serviceutil.ServiceFramework.__init__(self, args)
         self._shutting_down = False
 
-        global AGENT_CONFIG
+        global AGENT_CONFIG, AGENT_DEFAULT_CONFIG, AGENT_DEFAULT_LOG_CONFIG
         regKey = "System\\CurrentControlSet\\services\\%s" % self._svc_name_
         hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regKey)
         filePath = _winreg.QueryValueEx(hkey, "ImagePath")[0].replace('"', '')
@@ -44,8 +49,20 @@ class OVirtGuestService(win32serviceutil.ServiceFramework):
             hkey.Close()
         filePath = os.path.dirname(filePath)
         AGENT_CONFIG = os.path.join(filePath, AGENT_CONFIG)
-
-        logging.config.fileConfig(AGENT_CONFIG)
+        AGENT_DEFAULT_CONFIG = os.path.join(filePath, AGENT_DEFAULT_CONFIG)
+        AGENT_DEFAULT_LOG_CONFIG = os.path.join(filePath,
+                                                AGENT_DEFAULT_LOG_CONFIG)
+
+        cparser = ConfigParser.ConfigParser()
+        if os.path.exists(AGENT_DEFAULT_LOG_CONFIG):
+            cparser.read(AGENT_DEFAULT_LOG_CONFIG)
+        cparser.read(AGENT_CONFIG)
+        strio = cStringIO.StringIO()
+        cparser.write(strio)
+        bio = io.BytesIO(strio.getvalue())
+        logging.config.fileConfig(bio)
+        bio.close()
+        strio.close()
 
     # Overriding this method in order to accept session change notifications.
     def GetAcceptedControls(self):
@@ -61,8 +78,9 @@ class OVirtGuestService(win32serviceutil.ServiceFramework):
         # Write a 'started' event to the event log...
         self.ReportEvent(servicemanager.PYS_SERVICE_STARTED)
         logging.info("Starting OVirt Guest Agent service")
-
         config = ConfigParser.ConfigParser()
+        if os.path.exists(AGENT_DEFAULT_CONFIG):
+            config.read(AGENT_DEFAULT_CONFIG)
         config.read(AGENT_CONFIG)
 
         self.vdsAgent = WinVdsAgent(config)
diff --git a/ovirt-guest-agent/bytesio.py b/ovirt-guest-agent/bytesio.py
new file mode 100644
index 0000000..37aef50
--- /dev/null
+++ b/ovirt-guest-agent/bytesio.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+class BytesIO:
+    def __init__(self, buffer):
+        self._data = buffer
+        if not self._data:
+            self._data = str()
+        self._pos = 0
+
+    def getvalue(self):
+        return self._data
+
+    def close(self):
+        pass
+
+    def readline(self):
+        return self.read(self._data[self._pos:].find('\n') + 1)
+
+    def read(self, n=None):
+        if n == None:
+            n = -1
+        if not isinstance(n, (int, long)):
+            raise TypeError("Argument must be an integer")
+        if n < 0:
+            n = len(self._data)
+        if len(self._data) <= self._pos:
+            return ''
+        newpos = min(len(self._data), self._pos + n)
+        b = self._data[self._pos : newpos]
+        self._pos = newpos
+        return b
+
+    def readable(self):
+        return True
+
+    def writable(self):
+        return True
+
+    def seekable(self):
+        return False
diff --git a/ovirt-guest-agent/ovirt-guest-agent.conf b/ovirt-guest-agent/ovirt-guest-agent.conf
deleted file mode 100644
index d3c984e..0000000
--- a/ovirt-guest-agent/ovirt-guest-agent.conf
+++ /dev/null
@@ -1,61 +0,0 @@
-#
-# ovirt-guest-agent.conf
-#
-
-[general]
-
-heart_beat_rate = 5
-report_user_rate = 10
-report_num_cpu_rate = 60
-report_application_rate = 120
-report_disk_usage = 300
-# RHEL/Fedora Packages: kernel ovirt-guest-agent-common xorg-x11-drv-qxl
-# Debian Packages:      linux-image xserver-xorg-video-qxl
-applications_list = kernel rhevm-guest-agent-common xorg-x11-drv-qxl linux-image xserver-xorg-video-qxl
-ignored_fs = rootfs tmpfs autofs cgroup selinuxfs udev mqueue nfsd proc sysfs devtmpfs hugetlbfs rpc_pipefs devpts securityfs debugfs binfmt_misc fuse.gvfsd-fuse fuse.gvfs-fuse-daemon fusectl usbfs
-
-[virtio]
-
-device = /dev/virtio-ports/com.redhat.rhevm.vdsm
-
-[loggers]
-keys=root
-
-[handlers]
-keys=console,syslog,logfile
-
-[formatters]
-keys=long,simple,none,sysform
-
-[logger_root]
-level=INFO
-handlers=logfile
-propagate=0
-
-[handler_syslog]
-class=handlers.SysLogHandler
-formatter=sysform
-args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)
-
-[handler_logfile]
-class=handlers.RotatingFileHandler 
-args=('/var/log/ovirt-guest-agent/ovirt-guest-agent.log', 'a', 100*1024, 5)
-formatter=long
-
-[handler_console]
-class: StreamHandler
-args: []
-formatter: none
-
-[formatter_simple]
-format: %(name)s:%(levelname)s:  %(message)s
-
-[formatter_none]
-format: %(message)s
-
-[formatter_long]
-format: %(threadName)s::%(levelname)s::%(asctime)s::%(module)s::%(lineno)d::%(name)s::%(message)s
-
-[formatter_sysform]
-format= %(asctime)s %(levelname)s %(message)s
-datefmt=
diff --git a/ovirt-guest-agent/ovirt-guest-agent.py b/ovirt-guest-agent/ovirt-guest-agent.py
index c7d60c8..173ff6b 100644
--- a/ovirt-guest-agent/ovirt-guest-agent.py
+++ b/ovirt-guest-agent/ovirt-guest-agent.py
@@ -22,21 +22,42 @@ import signal
 import sys
 import getopt
 import ConfigParser
+import cStringIO
+
+io = None
+try:
+    import io as modio
+    io = modio
+except ImportError:
+    import bytesio as modio
+    io = modio
+
 from GuestAgentLinux2 import LinuxVdsAgent
 
 AGENT_CONFIG = '/etc/ovirt-guest-agent.conf'
+AGENT_DEFAULT_CONFIG = '/usr/share/ovirt-guest-agent/default.conf'
+AGENT_DEFAULT_LOG_CONFIG = '/usr/share/ovirt-guest-agent/default-logger.conf'
 AGENT_PIDFILE = '/run/ovirt-guest-agent.pid'
 
 
 class OVirtAgentDaemon:
 
     def __init__(self):
-        logging.config.fileConfig(AGENT_CONFIG)
+        cparser = ConfigParser.ConfigParser()
+        cparser.read(AGENT_DEFAULT_LOG_CONFIG)
+        cparser.read(AGENT_CONFIG)
+        strio = cStringIO.StringIO()
+        cparser.write(strio)
+        bio = io.BytesIO(strio.getvalue())
+        logging.config.fileConfig(bio)
+        bio.close()
+        strio.close()
 
     def run(self, daemon, pidfile):
         logging.info("Starting oVirt guest agent")
-
         config = ConfigParser.ConfigParser()
+        config.read(AGENT_DEFAULT_CONFIG)
+        config.read(AGENT_DEFAULT_LOG_CONFIG)
         config.read(AGENT_CONFIG)
 
         self.agent = LinuxVdsAgent(config)
-- 
1.9.0

