[virt-who] satellite: use proper hypervisor type in VM name
by Radek Novacek
commit 2a44c5e74487f454ee3140231edcab9c946522d2
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 26 09:18:30 2014 +0100
satellite: use proper hypervisor type in VM name
virt-who reported this as name of VM:
VM from None hypervisor ad58b739-5288-4cbc-a984-bd771612d670
Now fixed to correct string:
VM from esx hypervisor ad58b739-5288-4cbc-a984-bd771612d670
tests/test_manager.py | 6 +++---
virtwho.py | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
---
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 8f777ae..6bbdebb 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -121,7 +121,7 @@ class TestSatellite(TestManager):
manager = Manager.fromOptions(self.logger, options)
options.env = "ENV"
options.owner = "OWNER"
- manager.hypervisorCheckIn(options, self.mapping)
+ manager.hypervisorCheckIn(options, self.mapping, 'ABC')
manager.server.registration.virt_notify.assert_called_with(ANY, [
[
0, 'exists', 'system', {'uuid': '0000000000000000', 'identity': 'host'}
@@ -131,7 +131,7 @@ class TestSatellite(TestManager):
0, 'exists', 'domain', {
'state': 'running',
'memory_size': 0,
- 'name': 'VM from None hypervisor ad58b739-5288-4cbc-a984-bd771612d670',
+ 'name': 'VM from ABC hypervisor ad58b739-5288-4cbc-a984-bd771612d670',
'virt_type': 'fully_virtualized',
'vcpus': 1,
'uuid': '2147647e6f064ac0982d6902c259f9d6'
@@ -140,7 +140,7 @@ class TestSatellite(TestManager):
0, 'exists', 'domain', {
'state': 'running',
'memory_size': 0,
- 'name': 'VM from None hypervisor ad58b739-5288-4cbc-a984-bd771612d670',
+ 'name': 'VM from ABC hypervisor ad58b739-5288-4cbc-a984-bd771612d670',
'virt_type': 'fully_virtualized',
'vcpus': 1,
'uuid': 'd5ffceb5f79d41bea4c1204f836e144a'
diff --git a/virtwho.py b/virtwho.py
index 9b826f2..85ff3ee 100644
--- a/virtwho.py
+++ b/virtwho.py
@@ -151,7 +151,7 @@ class VirtWho(object):
if not virt.isHypervisor():
manager.sendVirtGuests(virtualGuests)
else:
- result = manager.hypervisorCheckIn(virt.config, virtualGuests)
+ result = manager.hypervisorCheckIn(virt.config, virtualGuests, virt.config.type)
# Show the result of hypervisorCheckIn
for fail in result['failedUpdate']:
9 years, 6 months
[virt-who] virt: always call base class constructor
by Radek Novacek
commit 7b54ce087ebe8474aec6ec0bd1a7b90218054283
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 12 11:01:11 2014 +0100
virt: always call base class constructor
virt/esx/esx.py | 2 +-
virt/hyperv/hyperv.py | 2 +-
virt/libvirtd/libvirtd.py | 3 +--
virt/rhevm/rhevm.py | 3 +--
virt/vdsm/vdsm.py | 3 +--
virt/virt.py | 3 ++-
6 files changed, 7 insertions(+), 9 deletions(-)
---
diff --git a/virt/esx/esx.py b/virt/esx/esx.py
index 743887b..7e2302a 100644
--- a/virt/esx/esx.py
+++ b/virt/esx/esx.py
@@ -112,7 +112,7 @@ class Esx(virt.Virt):
CONFIG_TYPE = "esx"
def __init__(self, logger, config):
- self.logger = logger
+ super(Esx, self).__init__(logger, config)
self.url = config.server
self.username = config.username
self.password = config.password
diff --git a/virt/hyperv/hyperv.py b/virt/hyperv/hyperv.py
index 3302ea1..b2cdfbe 100644
--- a/virt/hyperv/hyperv.py
+++ b/virt/hyperv/hyperv.py
@@ -179,7 +179,7 @@ class HyperV(virt.Virt):
CONFIG_TYPE = "hyperv"
def __init__(self, logger, config):
- self.logger = logger
+ super(HyperV, self).__init__(logger, config)
url = config.server
self.username = config.username
self.password = config.password
diff --git a/virt/libvirtd/libvirtd.py b/virt/libvirtd/libvirtd.py
index a45c2bf..a9a9b28 100644
--- a/virt/libvirtd/libvirtd.py
+++ b/virt/libvirtd/libvirtd.py
@@ -137,9 +137,8 @@ class Libvirtd(virt.Virt):
CONFIG_TYPE = "libvirt"
def __init__(self, logger, config, registerEvents=True):
+ super(Libvirtd, self).__init__(logger, config)
self.changedCallback = None
- self.logger = logger
- self.config = config
self.registerEvents = registerEvents
self._host_uuid = None
libvirt.registerErrorHandler(lambda ctx, error: None, None)
diff --git a/virt/rhevm/rhevm.py b/virt/rhevm/rhevm.py
index 8bb540e..2da373b 100644
--- a/virt/rhevm/rhevm.py
+++ b/virt/rhevm/rhevm.py
@@ -36,8 +36,7 @@ class RhevM(virt.Virt):
CONFIG_TYPE = "rhevm"
def __init__(self, logger, config):
- self.logger = logger
- self.config = config
+ super(RhevM, self).__init__(logger, config)
self.url = self.config.server
if "//" not in self.url:
self.url = "//" + self.config.server
diff --git a/virt/vdsm/vdsm.py b/virt/vdsm/vdsm.py
index 2c0ad17..c3aaa51 100644
--- a/virt/vdsm/vdsm.py
+++ b/virt/vdsm/vdsm.py
@@ -37,8 +37,7 @@ class Vdsm(Virt):
CONFIG_TYPE = "vdsm"
def __init__(self, logger, config):
- self.logger = logger
- self.config = config
+ super(Vdsm, self).__init__(logger, config)
self._readConfig("/etc/vdsm/vdsm.conf")
self.connect()
diff --git a/virt/virt.py b/virt/virt.py
index 8d1de05..e8f1e50 100644
--- a/virt/virt.py
+++ b/virt/virt.py
@@ -42,7 +42,8 @@ class Domain(dict):
class Virt(object):
- def __init__(self, config):
+ def __init__(self, logger, config):
+ self.logger = logger
self.config = config
@classmethod
9 years, 6 months
[virt-who] Display found configurations and their modes in the log file
by Radek Novacek
commit 88a98833e65e281a62c735fbd69a89f314858f48
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 5 12:40:41 2014 +0100
Display found configurations and their modes in the log file
virtwho.py | 11 +++++++----
1 files changed, 7 insertions(+), 4 deletions(-)
---
diff --git a/virtwho.py b/virtwho.py
index d474d5c..9b826f2 100644
--- a/virtwho.py
+++ b/virtwho.py
@@ -444,7 +444,7 @@ def _main(logger, options):
virtWho = VirtWho(logger, options)
if options.virtType is not None:
- config = Config("virt-who", options.virtType, options.server,
+ config = Config(None, options.virtType, options.server,
options.username, options.password, options.owner, options.env)
virtWho.configManager.addConfig(config)
if len(virtWho.configManager.configs) == 0:
@@ -452,9 +452,12 @@ def _main(logger, options):
# fallback to using libvirt as default virt backend
logger.info("No configurations found, using libvirt as backend")
virtWho.configManager.addConfig(Config("virt-who", "libvirt"))
- else:
- for config in virtWho.configManager.configs:
- logger.info("Using virt-who configuration: %s" % config.name)
+
+ for config in virtWho.configManager.configs:
+ if config.name is None:
+ logger.info('Using commandline or sysconfig configuration ("%s" mode)' % config.type)
+ else:
+ logger.info('Using configuration "%s" ("%s" mode)' % (config.name, config.type))
def reload(signal, stackframe):
virtWho.reload()
9 years, 6 months
[virt-who] vdsm: fix regular expression for reading CN from cert
by Radek Novacek
commit 6719f6c36fac3c7d0efe7c4bc703ef3abda6250d
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 5 10:46:56 2014 +0100
vdsm: fix regular expression for reading CN from cert
virt/vdsm/vdsm.py | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
---
diff --git a/virt/vdsm/vdsm.py b/virt/vdsm/vdsm.py
index e1e9a47..2c0ad17 100644
--- a/virt/vdsm/vdsm.py
+++ b/virt/vdsm/vdsm.py
@@ -72,7 +72,7 @@ class Vdsm(Virt):
out, err = p.communicate()
if p.returncode != 0:
return '0'
- return re.search('/CN=([^/]+)/', out).group(1)
+ return re.search('/CN=([^/$\n]+)', out).group(1)
def _secureConnect(self):
addr = self._getLocalVdsName(self.trust_store_path)
9 years, 6 months
[virt-who] libvirt: treat remote libvirt as hypervisor
by Radek Novacek
commit 7e24a094b71d19c6ec7c6ae047e9a79a0e3ffc46
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 5 10:46:28 2014 +0100
libvirt: treat remote libvirt as hypervisor
manager/manager.py | 2 +-
tests/test_subscriptionmanager.py | 1 +
virt/__init__.py | 2 +-
virt/esx/esx.py | 2 +-
virt/hyperv/hyperv.py | 2 +-
virt/libvirtd/libvirtd.py | 33 ++++++++++++++++++++++++++++++---
virt/rhevm/rhevm.py | 2 +-
virt/vdsm/vdsm.py | 7 +++++--
virt/virt.py | 15 +++++++++------
virtwho.py | 16 ++++++++--------
10 files changed, 58 insertions(+), 24 deletions(-)
---
diff --git a/manager/manager.py b/manager/manager.py
index f8ed382..27a246d 100644
--- a/manager/manager.py
+++ b/manager/manager.py
@@ -27,7 +27,7 @@ class Manager(object):
def sendVirtGuests(self, domains):
raise NotImplementedError()
- def hypervisorCheckIn(self, owner, env, mapping, type=None):
+ def hypervisorCheckIn(self, config, mapping, type=None):
raise NotImplementedError()
@classmethod
diff --git a/tests/test_subscriptionmanager.py b/tests/test_subscriptionmanager.py
index 35e1665..512d2f5 100644
--- a/tests/test_subscriptionmanager.py
+++ b/tests/test_subscriptionmanager.py
@@ -10,6 +10,7 @@ from base import TestBase
from config import Config
from manager.subscriptionmanager import SubscriptionManager
+from virt import Virt
import rhsm.config
import rhsm.certificate
diff --git a/virt/__init__.py b/virt/__init__.py
index aebc492..23979a9 100644
--- a/virt/__init__.py
+++ b/virt/__init__.py
@@ -1,3 +1,3 @@
-from virt import Virt, DirectVirt, HypervisorVirt, VirtError, Domain
+from virt import Virt, VirtError, Domain
diff --git a/virt/esx/esx.py b/virt/esx/esx.py
index cdca920..743887b 100644
--- a/virt/esx/esx.py
+++ b/virt/esx/esx.py
@@ -108,7 +108,7 @@ def get_search_filter_spec(client, begin_entity, property_spec):
return pfs
-class Esx(virt.HypervisorVirt):
+class Esx(virt.Virt):
CONFIG_TYPE = "esx"
def __init__(self, logger, config):
diff --git a/virt/hyperv/hyperv.py b/virt/hyperv/hyperv.py
index 8b48c46..3302ea1 100644
--- a/virt/hyperv/hyperv.py
+++ b/virt/hyperv/hyperv.py
@@ -175,7 +175,7 @@ class HyperVAuthFailed(HyperVException):
pass
-class HyperV(virt.HypervisorVirt):
+class HyperV(virt.Virt):
CONFIG_TYPE = "hyperv"
def __init__(self, logger, config):
diff --git a/virt/libvirtd/libvirtd.py b/virt/libvirtd/libvirtd.py
index 60fdb32..a45c2bf 100644
--- a/virt/libvirtd/libvirtd.py
+++ b/virt/libvirtd/libvirtd.py
@@ -26,6 +26,12 @@ import urlparse
import virt
+# Import XML parser
+try:
+ from elementtree import ElementTree
+except ImportError:
+ from xml.etree import ElementTree
+
class VirEventLoopThread(threading.Thread):
def __init__(self, logger, *args, **kwargs):
@@ -126,7 +132,7 @@ def libvirt_cred_request(credentials, config):
return 0
-class Libvirtd(virt.DirectVirt):
+class Libvirtd(virt.Virt):
""" Class for interacting with libvirt. """
CONFIG_TYPE = "libvirt"
@@ -135,8 +141,12 @@ class Libvirtd(virt.DirectVirt):
self.logger = logger
self.config = config
self.registerEvents = registerEvents
+ self._host_uuid = None
libvirt.registerErrorHandler(lambda ctx, error: None, None)
+ def isHypervisor(self):
+ return bool(self.config.server)
+
def _get_url(self):
if self.config.server:
scheme = username = netloc = path = None
@@ -188,9 +198,13 @@ class Libvirtd(virt.DirectVirt):
def listDomains(self):
""" Get list of all domains. """
- domains = []
self._connect()
+ domains = self._listDomains()
+ self.virt.close()
+ return domains
+ def _listDomains(self):
+ domains = []
try:
# Active domains
for domainID in self.virt.listDomainsID():
@@ -209,9 +223,22 @@ class Libvirtd(virt.DirectVirt):
except libvirt.libvirtError, e:
self.virt.close()
raise virt.VirtError(str(e))
- self.virt.close()
return domains
+ def _remote_host_uuid(self):
+ if self._host_uuid is None:
+ xml = ElementTree.fromstring(self.virt.getCapabilities())
+ self._host_uuid = xml.find('host/uuid').text
+ return self._host_uuid
+
+ def getHostGuestMapping(self):
+ self._connect()
+ mapping = {
+ self._remote_host_uuid(): self._listDomains()
+ }
+ self.virt.close()
+ return mapping
+
def canMonitor(self):
return True
diff --git a/virt/rhevm/rhevm.py b/virt/rhevm/rhevm.py
index a4069fb..8bb540e 100644
--- a/virt/rhevm/rhevm.py
+++ b/virt/rhevm/rhevm.py
@@ -32,7 +32,7 @@ except ImportError:
from xml.etree import ElementTree
-class RhevM(virt.HypervisorVirt):
+class RhevM(virt.Virt):
CONFIG_TYPE = "rhevm"
def __init__(self, logger, config):
diff --git a/virt/vdsm/vdsm.py b/virt/vdsm/vdsm.py
index af0c797..e1e9a47 100644
--- a/virt/vdsm/vdsm.py
+++ b/virt/vdsm/vdsm.py
@@ -26,14 +26,14 @@ import xmlrpclib
from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
import subprocess
-from virt import DirectVirt
+from virt import Virt
class VdsmError(Exception):
pass
-class Vdsm(DirectVirt):
+class Vdsm(Virt):
CONFIG_TYPE = "vdsm"
def __init__(self, logger, config):
@@ -42,6 +42,9 @@ class Vdsm(DirectVirt):
self._readConfig("/etc/vdsm/vdsm.conf")
self.connect()
+ def isHypervisor(self):
+ return False
+
def _readConfig(self, configName):
parser = SafeConfigParser()
parser.read(configName)
diff --git a/virt/virt.py b/virt/virt.py
index 288d800..8d1de05 100644
--- a/virt/virt.py
+++ b/virt/virt.py
@@ -59,9 +59,8 @@ class Virt(object):
import hyperv
for subcls in cls.__subclasses__():
- for subsubcls in subcls.__subclasses__():
- if config.type == subsubcls.CONFIG_TYPE:
- return subsubcls(logger, config)
+ if config.type == subcls.CONFIG_TYPE:
+ return subcls(logger, config)
raise KeyError("Invalid config type: %s" % config.type)
def canMonitor(self):
@@ -79,12 +78,16 @@ class Virt(object):
"""
raise NotImplementedError()
+ def isHypervisor(self):
+ """
+ Return True if the virt instance represents hypervisor and the guests
+ should be obtained by using getHostGuestMapping method. Otherwise
+ plain listDomains will be used
+ """
+ return True
-class DirectVirt(Virt):
def listDomains(self):
raise NotImplementedError()
-
-class HypervisorVirt(Virt):
def getHostGuestMapping(self):
raise NotImplementedError()
diff --git a/virtwho.py b/virtwho.py
index 783bad8..d474d5c 100644
--- a/virtwho.py
+++ b/virtwho.py
@@ -107,8 +107,9 @@ class VirtWho(object):
retry - Should be True on first run, False on second.
return - True if sending is successful, False otherwise
"""
+ virt = Virt.fromConfig(self.logger, config)
try:
- virtualGuests = self._readGuests(config)
+ virtualGuests = self._readGuests(virt)
except (KeyboardInterrupt, SystemExit):
raise
except Exception, e:
@@ -122,7 +123,7 @@ class VirtWho(object):
return False
try:
- self._sendGuests(config, virtualGuests)
+ self._sendGuests(virt, virtualGuests)
except (KeyboardInterrupt, SystemExit):
raise
except Exception, e:
@@ -137,21 +138,20 @@ class VirtWho(object):
return False
return True
- def _readGuests(self, config):
- virt = Virt.fromConfig(self.logger, config)
+ def _readGuests(self, virt):
if not self.options.oneshot and virt.canMonitor():
virt.startMonitoring(self.sync_event)
- if config.type not in ["esx", "rhevm", "hyperv"]:
+ if not virt.isHypervisor():
return virt.listDomains()
else:
return virt.getHostGuestMapping()
- def _sendGuests(self, config, virtualGuests):
+ def _sendGuests(self, virt, virtualGuests):
manager = Manager.fromOptions(self.logger, self.options)
- if config.type not in ["esx", "rhevm", "hyperv"]:
+ if not virt.isHypervisor():
manager.sendVirtGuests(virtualGuests)
else:
- result = manager.hypervisorCheckIn(config, virtualGuests)
+ result = manager.hypervisorCheckIn(virt.config, virtualGuests)
# Show the result of hypervisorCheckIn
for fail in result['failedUpdate']:
9 years, 6 months
[virt-who] password: fix decoding user password from crypted form
by Radek Novacek
commit f759f0fde748cfb88e0451dd19715794a5ca1e53
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Nov 7 14:12:22 2014 +0100
password: fix decoding user password from crypted form
Encrypted passwords in the config file (returned by virt-who-password)
are always converted to hex because they contain unprintable bytes.
Converting from hex was missing.
config.py | 3 ++-
tests/test_config.py | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletions(-)
---
diff --git a/config.py b/config.py
index 0df1159..a7f6ae3 100644
--- a/config.py
+++ b/config.py
@@ -22,6 +22,7 @@ import os
import logging
from ConfigParser import SafeConfigParser, NoOptionError, Error, MissingSectionHeaderError
from password import Password
+from binascii import unhexlify
VIRTWHO_CONF_DIR = "/etc/virt-who.d/"
@@ -83,7 +84,7 @@ class Config(object):
if password is None:
try:
crypted = parser.get(name, "encrypted_password")
- password = Password.decrypt(crypted)
+ password = Password.decrypt(unhexlify(crypted))
except NoOptionError:
password = None
diff --git a/tests/test_config.py b/tests/test_config.py
index 08eb4f2..776e07b 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -24,6 +24,8 @@ import shutil
from config import ConfigManager, InvalidOption
from tempfile import mkdtemp
from base import TestBase, unittest
+from binascii import hexlify
+from mock import patch
class TestReadingConfigs(TestBase):
@@ -94,6 +96,28 @@ env=staging
manager = ConfigManager(self.config_dir)
self.assertEqual(len(manager.configs), 0)
+ @patch('password.Password._read_key_iv')
+ def testCryptedPassword(self, password):
+ from password import Password
+ password.return_value = (hexlify(Password._generate_key()), hexlify(Password._generate_key()))
+ passwd = "TestSecretPassword!"
+ crypted = hexlify(Password.encrypt(passwd))
+
+ filename = os.path.join(self.config_dir, "test.conf")
+ with open(filename, "w") as f:
+ f.write("""
+[test]
+type=esx
+server=1.2.3.4
+username=admin
+encrypted_password=%s
+owner=root
+env=staging
+""" % crypted)
+ manager = ConfigManager(self.config_dir)
+ self.assertEqual(len(manager.configs), 1)
+ self.assertEqual(manager.configs[0].password, passwd)
+
def testNoOptionsConfig(self):
with open(os.path.join(self.config_dir, "test.conf"), "w") as f:
f.write("""
9 years, 6 months