[virt-who] Fix crash on SIGHUP signal when system is being unregistered
by Radek Novacek
commit 887d164740bb39399bd3b8119973846a40cda02f
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri May 16 11:10:29 2014 +0200
Fix crash on SIGHUP signal when system is being unregistered
virt-who.py | 8 +++++++-
1 files changed, 7 insertions(+), 1 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index 3bc12ed..9262ebc 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -264,7 +264,13 @@ class VirtWho(object):
pass
self.virt = None
self.subscriptionManager = None
- self.checkConnections()
+ try:
+ self.checkConnections()
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except Exception, e:
+ exceptionCheck(e)
+ pass
self.logger.debug("virt-who configution reloaded")
self.doReload = False
9 years, 11 months
[virt-who] Support for sending additional information together with guest uuids
by Radek Novacek
commit ee127e3a7c4281776f84c2a09229b73be54cd054
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Jan 10 16:51:32 2014 +0100
Support for sending additional information together with guest uuids
So for state and virt type are send when using libvirt.
subscriptionmanager.py | 40 ++++++++++++++++++++++++++++++----------
vdsm.py | 13 +------------
virt-who.spec | 4 ++--
virt.py | 16 +++++++++++++---
4 files changed, 46 insertions(+), 27 deletions(-)
---
diff --git a/subscriptionmanager.py b/subscriptionmanager.py
index 51345d9..0045d38 100644
--- a/subscriptionmanager.py
+++ b/subscriptionmanager.py
@@ -70,18 +70,38 @@ class SubscriptionManager:
raise SubscriptionManagerError("Unable to obtain status from server, UEPConnection is likely not usable.")
def sendVirtGuests(self, domains):
- """ Update consumer facts with UUIDs of virtual guests. """
-
- # Get comma separated list of UUIDs
- uuids = []
- for domain in domains:
- uuids.append(domain.UUIDString())
- uuids.sort()
-
- self.logger.debug("Sending list of uuids: %s" % uuids)
+ """
+ Update consumer facts with info about virtual guests.
+
+ :param domain: List of guest UUIDs for current machine or list of
+ dictionaries in the format: [
+ {
+ 'guestId': <uuid of guest>,
+ 'attributes': { # supplemental list a attributes, supported are following:
+ 'hypervisorType': <type of hypervisor, e.g. QEMU>,
+ 'virtWhoType': <virtwho type of operation, e.g. libvirt>,
+ 'active': <1 if guest is active, 0 otherwise, -1 on error>
+ },
+ },
+ ...
+ ]
+ :type domain: list of str or list of dict domains
+ """
+
+ # Sort the list
+ key = None
+ if len(domains) > 0:
+ if isinstance(domains[0], basestring):
+ key = "guestId"
+ domains.sort(key=key)
+
+ if key is not None:
+ self.logger.debug("Sending list of uuids: %s" % [domain[key] for domain in domains])
+ else:
+ self.logger.debug("Sending list of uuids: %s" % domains)
# Send list of guest uuids to the server
- self.connection.updateConsumer(self.uuid(), guest_uuids=uuids)
+ self.connection.updateConsumer(self.uuid(), guest_uuids=domains)
def hypervisorCheckIn(self, owner, env, mapping, type=None):
""" Send hosts to guests mapping to subscription manager. """
diff --git a/vdsm.py b/vdsm.py
index 957ea55..2802f75 100644
--- a/vdsm.py
+++ b/vdsm.py
@@ -28,17 +28,6 @@ import subprocess
class VdsmError(Exception):
pass
-class Domain:
- """ Class for compatibility with libvirt Domain class. Only UUID supported. """
- def __init__(self, uuid):
- self.uuid = uuid
-
- def UUIDString(self):
- return self.uuid
-
- def __str__(self):
- return self.uuid
-
class VDSM:
def __init__(self, logger):
self.logger = logger
@@ -101,7 +90,7 @@ class VDSM:
self.logger.error("Unable to list virtual machines: %s" % response['status']['message'])
else:
for vm in response['vmList']:
- domains.append(Domain(vm['vmId']))
+ domains.append(vm['vmId'])
return domains
def ping(self):
diff --git a/virt-who.spec b/virt-who.spec
index 0c7e069..a5ef0b6 100644
--- a/virt-who.spec
+++ b/virt-who.spec
@@ -12,8 +12,8 @@ BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildArch: noarch
BuildRequires: python2-devel
Requires: libvirt-python
-# python-rhsm 1.0.4 contains new module for reading certificates
-Requires: python-rhsm >= 1.0.4
+# python-rhsm 1.10.10 has required call for guestId support
+Requires: python-rhsm >= 1.10.10
# python-suds is required for vSphere support
Requires: python-suds
# m2crypto is required for Hyper-V support
diff --git a/virt.py b/virt.py
index 9a09a04..cb20d71 100644
--- a/virt.py
+++ b/virt.py
@@ -24,6 +24,16 @@ import event
class VirtError(Exception):
pass
+class Domain(dict):
+ def __init__(self, virt, domain):
+ self['guestId'] = domain.UUIDString()
+ self['attributes'] = {
+ 'hypervisorType': virt.getType(),
+ 'virtWhoType': "libvirt",
+ 'active': 1 if domain.isActive() else 0
+ }
+ self['state'] = domain.state(0)[0]
+
class Virt:
""" Class for interacting with libvirt. """
def __init__(self, logger, registerEvents=True):
@@ -52,13 +62,13 @@ class Virt:
if domain.UUIDString() == "00000000-0000-0000-0000-000000000000":
# Don't send Domain-0 on xen (zeroed uuid)
continue
- domains.append(domain)
+ domains.append(Domain(self.virt, domain))
self.logger.debug("Virtual machine found: %s: %s" % (domain.name(), domain.UUIDString()))
# Non active domains
for domainName in self.virt.listDefinedDomains():
domain = self.virt.lookupByName(domainName)
- domains.append(domain)
+ domains.append(Domain(self.virt, domain))
self.logger.debug("Virtual machine found: %s: %s" % (domainName, domain.UUIDString()))
except libvirt.libvirtError, e:
raise VirtError(str(e))
@@ -86,7 +96,7 @@ class Virt:
hasDomain = True
break
if not hasDomain:
- l.append(dom)
+ l.append(Domain(self.virt, dom))
try:
self.changedCallback(l)
except Exception, e:
9 years, 11 months
[virt-who] Make sure that virt-who ends cleanly on sigint/sigterm signal
by Radek Novacek
commit f99bb08e3fa8be840b9570ffa1ca9c4a770fdc48
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Apr 30 12:51:41 2014 +0200
Make sure that virt-who ends cleanly on sigint/sigterm signal
Python 2.4 has different exception handling -- when virt-who was
terminated it didn't ended cleanly but raised an exception.
virt-who.py | 36 ++++++++++++++++++++++++++++++++----
1 files changed, 32 insertions(+), 4 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index 8cd68f6..3bc12ed 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -23,6 +23,7 @@ import os
import time
import atexit
import signal
+import errno
from virt import Virt, VirtError
from vdsm import VDSM
@@ -126,7 +127,10 @@ class VirtWho(object):
except SatelliteError, e:
self.logger.exception("Unable to connect to the RHN Satellite:")
raise
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception, e:
+ exceptionCheck(e)
self.logger.exception("Unknown error")
raise
@@ -176,7 +180,10 @@ class VirtWho(object):
logger = self.logger
try:
self.checkConnections()
- except Exception,e:
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except Exception, e:
+ exceptionCheck(e)
if retry:
logger.exception("Unable to create connection:")
return self._send(False)
@@ -189,7 +196,10 @@ class VirtWho(object):
virtualGuests = self.virt.listDomains()
else:
virtualGuests = self.virt.getHostGuestMapping()
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception, e:
+ exceptionCheck(e)
# Communication with virtualization supervisor failed
self.virt = None
# Retry once
@@ -215,7 +225,10 @@ class VirtWho(object):
for created in result['created']:
guests = [x['guestId'] for x in created['guestIds']]
logger.info("Created host: %s with guests: [%s]", created['uuid'], ", ".join(guests))
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception, e:
+ exceptionCheck(e)
# Communication with subscription manager failed
self.subscriptionManager = None
# Retry once
@@ -312,6 +325,8 @@ def checkPidFile():
# Process no longer exists
print >>sys.stderr, "PID file exists but associated process does not, deleting PID file"
os.remove(PIDFILE)
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception:
pass
@@ -325,6 +340,8 @@ def createPidFile(logger=None):
f = open(PIDFILE, "w")
f.write("%d" % os.getpid())
f.close()
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception, e:
if logger is not None:
logger.error("Unable to create pid file: %s" % str(e))
@@ -332,11 +349,20 @@ def createPidFile(logger=None):
def cleanup(sig=None, stack=None):
try:
os.remove(PIDFILE)
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception:
pass
- if sig is not None and sig in [signal.SIGINT, signal.SIGTERM, signal.SIGKILL]:
- sys.exit(0)
+def exceptionCheck(e):
+ try:
+ # This happens when connection to server is interrupted (CTRL+C or signal)
+ if e.args[0] == errno.EALREADY:
+ sys.exit(0)
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except Exception:
+ pass
def main():
checkPidFile()
@@ -522,6 +548,8 @@ def main():
signal.signal(signal.SIGHUP, virtWho.queueReload)
try:
virtWho.checkConnections()
+ except (KeyboardInterrupt, SystemExit):
+ raise
except Exception:
pass
@@ -566,7 +594,7 @@ def main():
if __name__ == '__main__':
try:
main()
- except SystemExit:
+ except (SystemExit, KeyboardInterrupt):
raise
except Exception, e:
logger = log.getLogger(False, False)
9 years, 11 months