[virt-who] Fix constructor parameters in vdsm and rhevm modes
by Radek Novacek
commit 69dcffe281aa1333cf6d319b7067b7476428d308
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Aug 29 09:06:38 2014 +0200
Fix constructor parameters in vdsm and rhevm modes
virt/rhevm/rhevm.py | 13 +++++++------
virt/vdsm/vdsm.py | 20 ++++++++++++++------
2 files changed, 21 insertions(+), 12 deletions(-)
---
diff --git a/virt/rhevm/rhevm.py b/virt/rhevm/rhevm.py
index 570fa7b..a4069fb 100644
--- a/virt/rhevm/rhevm.py
+++ b/virt/rhevm/rhevm.py
@@ -35,11 +35,12 @@ except ImportError:
class RhevM(virt.HypervisorVirt):
CONFIG_TYPE = "rhevm"
- def __init__(self, logger, url, username, password):
+ def __init__(self, logger, config):
self.logger = logger
- self.url = url
+ self.config = config
+ self.url = self.config.server
if "//" not in self.url:
- self.url = "//" + url
+ self.url = "//" + self.config.server
parsed = urlparse.urlsplit(self.url, "https")
if ":" not in parsed[1]:
netloc = parsed[1] + ":8443"
@@ -47,13 +48,13 @@ class RhevM(virt.HypervisorVirt):
netloc = parsed[1]
self.url = urlparse.urlunsplit((parsed[0], netloc, parsed[2], "", ""))
- self.username = username
- self.password = password
+ self.username = self.config.username
+ self.password = self.config.password
self.hosts_url = urlparse.urljoin(self.url, "/api/hosts")
self.vms_url = urlparse.urljoin(self.url, "/api/vms")
- self.auth = base64.encodestring('%s:%s' % (username, password))[:-1]
+ self.auth = base64.encodestring('%s:%s' % (self.config.username, self.config.password))[:-1]
def get(self, url):
"""
diff --git a/virt/vdsm/vdsm.py b/virt/vdsm/vdsm.py
index 06b27a3..84cdbc3 100644
--- a/virt/vdsm/vdsm.py
+++ b/virt/vdsm/vdsm.py
@@ -35,8 +35,9 @@ class VdsmError(Exception):
class Vdsm(DirectVirt):
CONFIG_TYPE = "vdsm"
- def __init__(self, logger):
+ def __init__(self, logger, config):
self.logger = logger
+ self.config = config
self._readConfig("/etc/vdsm/vdsm.conf")
self.connect()
@@ -45,13 +46,20 @@ class Vdsm(DirectVirt):
parser.read(configName)
try:
self.ssl = parser.get("vars", "ssl").lower() in ["1", "true"]
- if self.ssl:
+ except (NoSectionError, NoOptionError):
+ self.ssl = True
+
+ if self.ssl:
+ try:
self.trust_store_path = parser.get("vars", "trust_store_path")
- else:
- self.trust_store_path = None
+ except (NoSectionError, NoOptionError):
+ self.trust_store_path = '/etc/pki/vdsm'
+ else:
+ self.trust_store_path = None
+ try:
self.management_port = parser.get("addresses", "management_port")
- except (NoSectionError, NoOptionError), e:
- raise VdsmError("Error in vdsm configuration file: %s" % str(e))
+ except (NoSectionError, NoOptionError):
+ self.management_port = 54321
def _getLocalVdsName(self, tsPath):
p = subprocess.Popen([
9 years, 8 months
[virt-who] hyperv: don't use deprecated md5 module
by Radek Novacek
commit a8f50d6c5175cd481864593a28bacb7a9b6abeeb
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Aug 8 11:04:58 2014 +0200
hyperv: don't use deprecated md5 module
virt/hyperv/ntlm.py | 8 ++++++--
1 files changed, 6 insertions(+), 2 deletions(-)
---
diff --git a/virt/hyperv/ntlm.py b/virt/hyperv/ntlm.py
index c8d9c36..69991f6 100644
--- a/virt/hyperv/ntlm.py
+++ b/virt/hyperv/ntlm.py
@@ -18,11 +18,15 @@ import subprocess
try:
import hashlib
- from hashlib import md5, algorithms
+ from hashlib import md5
except ImportError:
- algorithms = []
from md5 import md5
+try:
+ from hashlib import algorithms
+except ImportError:
+ algorithms = []
+
import hmac
import random
from socket import gethostname
9 years, 8 months
[virt-who] esx: disable error when esx host doesn't have guests
by Radek Novacek
commit 0e5e1d504bc9f35f890b123629f719ef3d155564
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Aug 8 11:03:23 2014 +0200
esx: disable error when esx host doesn't have guests
It works fine in this case, no need to show an error.
virt/esx/esx.py | 2 --
1 files changed, 0 insertions(+), 2 deletions(-)
---
diff --git a/virt/esx/esx.py b/virt/esx/esx.py
index 7330299..cdca920 100644
--- a/virt/esx/esx.py
+++ b/virt/esx/esx.py
@@ -242,8 +242,6 @@ class Esx(virt.HypervisorVirt):
else:
self.logger.error("No UUID for virtual machine %s", self.vms[obj.obj.value].name)
self.vms[obj.obj.value].uuid = None
- else:
- self.logger.error("Cannot read VM list from ESX for host %s (len(obj) <= 1)" % self.hosts[host.obj.value].uuid)
except AttributeError:
# This means that there is no guest on given host
9 years, 8 months
[virt-who] libvirtd: use info() when state() is not implemented
by Radek Novacek
commit 92dda3b5a7c62201ce21aaa1741038255c429ad6
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Mon Aug 4 14:12:37 2014 +0200
libvirtd: use info() when state() is not implemented
state() method is not implemented on older libvirt, use first value from
info() instead.
virt/virt.py | 5 +++--
1 files changed, 3 insertions(+), 2 deletions(-)
---
diff --git a/virt/virt.py b/virt/virt.py
index 3db2a85..288d800 100644
--- a/virt/virt.py
+++ b/virt/virt.py
@@ -36,8 +36,9 @@ class Domain(dict):
try:
self['state'] = domain.state(0)[0]
except AttributeError:
- # Some versions of libvirt doesn't have domain.state() method
- pass
+ # Some versions of libvirt doesn't have domain.state() method,
+ # use first value from info instead
+ self['state'] = domain.info()[0]
class Virt(object):
9 years, 8 months
[virt-who] libvirt: properly handle libvirt shutdown when doing background monitoring
by Radek Novacek
commit 45f61bf021cfee2c5d387502cf13fab06d0660a7
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Aug 1 16:10:30 2014 +0200
libvirt: properly handle libvirt shutdown when doing background monitoring
tests/test_libvirtd.py | 5 +++--
virt/libvirtd/libvirtd.py | 38 +++++++++++++++++++++++++++-----------
2 files changed, 30 insertions(+), 13 deletions(-)
---
diff --git a/tests/test_libvirtd.py b/tests/test_libvirtd.py
index bb3f428..4494524 100644
--- a/tests/test_libvirtd.py
+++ b/tests/test_libvirtd.py
@@ -25,7 +25,8 @@ import logging
from config import Config
from virt import Virt, Domain, VirtError
-from virt.libvirtd.libvirtd import LibvirtMonitor
+from virt.libvirtd.libvirtd import LibvirtMonitor, VirEventLoopThread
+import virt.libvirtd.libvirtd
def raiseLibvirtError(*args, **kwargs):
@@ -54,7 +55,7 @@ class TestLibvirtd(unittest.TestCase):
self.assertRaises(VirtError, libvirtd.listDomains)
@patch('libvirt.openReadOnly')
- @patch('threading.Thread')
+ @patch('virt.libvirtd.libvirtd.VirEventLoopThread')
def test_monitoring(self, thread, virt):
event = threading.Event()
LibvirtMonitor().set_event(event)
diff --git a/virt/libvirtd/libvirtd.py b/virt/libvirtd/libvirtd.py
index 2b7a3b3..a46869c 100644
--- a/virt/libvirtd/libvirtd.py
+++ b/virt/libvirtd/libvirtd.py
@@ -26,12 +26,17 @@ import threading
import virt
-eventLoopThread = None
+class VirEventLoopThread(threading.Thread):
+ def __init__(self, logger, *args, **kwargs):
+ self._terminated = threading.Event()
+ threading.Thread.__init__(self, *args, **kwargs)
+ def run(self):
+ while not self._terminated.is_set():
+ libvirt.virEventRunDefaultImpl()
-def virEventLoopNativeRun():
- while True:
- libvirt.virEventRunDefaultImpl()
+ def terminate(self):
+ self._terminated.set()
class LibvirtMonitor(object):
@@ -45,7 +50,6 @@ class LibvirtMonitor(object):
cls._instance.logger = logging.getLogger("rhsm-app")
cls._instance.event = None
cls._instance.eventLoopThread = None
- cls._instance.terminate = threading.Event()
cls._instance.domainIds = []
cls._instance.definedDomains = []
cls._instance.vc = None
@@ -61,15 +65,19 @@ class LibvirtMonitor(object):
libvirt.virEventRegisterDefaultImpl()
if self.eventLoopThread is not None and self.eventLoopThread.isAlive():
self.eventLoopThread.terminate()
- self.eventLoopThread = threading.Thread(target=virEventLoopNativeRun, name="libvirtEventLoop")
+ self.eventLoopThread = VirEventLoopThread(self.logger, name="libvirtEventLoop")
self.eventLoopThread.setDaemon(True)
self.eventLoopThread.start()
self._create_connection()
def _create_connection(self):
- self.vc = libvirt.openReadOnly('')
try:
- self.vc.registerCloseCallback(self._close_callback, None, None)
+ self.vc = libvirt.openReadOnly('')
+ except libvirt.libvirtError, e:
+ self.logger.warn("Unable to connect to libvirt: %s" % str(e))
+ return
+ try:
+ self.vc.registerCloseCallback(self._close_callback, None)
except AttributeError:
self.logger.warn("Can't monitor libvirtd restarts due to bug in libvirt-python")
self.vc.domainEventRegister(self._callback, None)
@@ -86,9 +94,17 @@ class LibvirtMonitor(object):
def _callback(self, *args, **kwargs):
self.event.set()
- def _close_callback(self, *args, **kwargs):
- self.logger.info("Connection to libvirtd lost, reconnecting")
- self._create_connection()
+ def _close_callback(self, conn, reason, opaque):
+ reasonStrings = ("Error", "End-of-file", "Keepalive", "Client")
+ self.logger.info("Connection to libvirtd lost: %s, reconnecting" % reasonStrings[reason])
+ # it might be just a restart, give it some time to recover
+ time.sleep(2)
+ try:
+ self.vc.close()
+ except Exception, e:
+ pass
+ self.vc = None
+ self.event.set()
def set_event(self, event):
self.event = event
9 years, 8 months
[virt-who] test: fix hypervisorCheckIn function parameters
by Radek Novacek
commit 988c99474211a0aabaa3aadc08386e98c9c576e2
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Aug 1 16:09:44 2014 +0200
test: fix hypervisorCheckIn function parameters
tests/test_satellite.py | 15 ++++++++++++---
1 files changed, 12 insertions(+), 3 deletions(-)
---
diff --git a/tests/test_satellite.py b/tests/test_satellite.py
index e3cbac9..d493694 100644
--- a/tests/test_satellite.py
+++ b/tests/test_satellite.py
@@ -28,6 +28,8 @@ import tempfile
import pickle
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
+from mock import MagicMock
+
from manager.satellite import Satellite, SatelliteError
TEST_SYSTEM_ID = 'test-system-id'
@@ -104,7 +106,10 @@ class TestSatellite(unittest.TestCase):
options = Options("wrong_server", "abc", "def")
s = Satellite(self.logger, options)
#self.assertRaises(SatelliteError, s.connect, "wrong_server", "abc", "def")
- s.hypervisorCheckIn("owner", "env", {}, "test")
+ options.env = "ENV"
+ options.owner = "OWNER"
+
+ s.hypervisorCheckIn(options, {}, "test")
#self.assertRaises(SatelliteError, s.connect, "localhost", "abc", "def")
def test_new_system(self):
@@ -121,13 +126,15 @@ class TestSatellite(unittest.TestCase):
def test_hypervisorCheckIn(self):
options = Options("http://localhost:8080", "username", "password")
options.force_register = True
+ options.env = "ENV"
+ options.owner = "OWNER"
s = Satellite(self.logger, options)
mapping = {
'host-1': ['guest1-1', 'guest1-2'],
'host-2': ['guest2-1', 'guest2-2', 'guest2-3']
}
- result = s.hypervisorCheckIn("owner", "env", mapping, "type")
+ result = s.hypervisorCheckIn(options, mapping, "type")
self.assertTrue("failedUpdate" in result)
self.assertTrue("created" in result)
self.assertTrue("updated" in result)
@@ -140,6 +147,8 @@ class TestSatellite(unittest.TestCase):
f.close()
options = Options("http://localhost:8080", "username", "password")
+ options.env = "ENV"
+ options.owner = "OWNER"
s = Satellite(self.logger, options)
s.HYPERVISOR_SYSTEMID_FILE = filename.replace(TEST_SYSTEM_ID, '%s')
@@ -147,7 +156,7 @@ class TestSatellite(unittest.TestCase):
'host-1': ['guest1-1', 'guest1-2'],
'host-2': ['guest2-1', 'guest2-2', 'guest2-3']
}
- result = s.hypervisorCheckIn("owner", "env", mapping, "type")
+ result = s.hypervisorCheckIn(options, mapping, "type")
self.assertTrue("failedUpdate" in result)
self.assertTrue("created" in result)
self.assertTrue("updated" in result)
9 years, 8 months