[virt-who] Don't catch SystemExit exception.
by Radek Novacek
commit b3d1e34955b095afeb3c33729c2dcefff17417fe
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Dec 9 15:28:09 2011 +0100
Don't catch SystemExit exception.
Fix for python 2.4.
virt-who.py | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index 32be133..134274f 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -126,6 +126,9 @@ class VirtWho(object):
self.checkConnections()
self.subscriptionManager.sendVirtGuests(self.virt.listDomains())
return True
+ except SystemExit,e:
+ # In python2.4 SystemExit is inherited from Exception, so must be catched extra
+ raise e
except VirtError, e:
# Communication with virtualization supervisor failed
logger.exception(e)
12 years, 5 months
[virt-who] Initial implementation of vsphere support.
by Radek Novacek
commit a402994b186fea6d67808ef5181148e1a56844cd
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 16 17:59:35 2011 +0100
Initial implementation of vsphere support.
So far as a standalone script only.
vsphere.py | 33 ++++++++++++++++++++++++++++-----
1 files changed, 28 insertions(+), 5 deletions(-)
---
diff --git a/vsphere.py b/vsphere.py
index da91015..c6fdc43 100644
--- a/vsphere.py
+++ b/vsphere.py
@@ -2,9 +2,9 @@
import sys
import suds
-#import logging
-#logging.basicConfig(level=logging.INFO)
-#logging.getLogger('suds.client').setLevel(logging.DEBUG)
+import logging
+logging.basicConfig(level=logging.INFO)
+logging.getLogger('suds.client').setLevel(logging.DEBUG)
def get_search_filter_spec(client, begin_entity, property_spec):
""" Build a PropertyFilterSpec capable of full inventory traversal.
@@ -147,7 +147,7 @@ class VSphere:
hostObjs.append(host)
# Get list of host uuids, names and virtual machines
- object_contents = self.RetrieveProperties('HostSystem', ['name', 'vm', 'hardware'], hostObjs)
+ object_contents = self.RetrieveProperties('HostSystem', ['name', 'vm', 'hardware', 'config'], hostObjs)
vmObjs = []
for host in object_contents:
for propSet in host.propSet:
@@ -161,6 +161,18 @@ class VSphere:
v = VM(vm)
self.vms[vm.value] = v
self.hosts[host.obj.value].vms.append(v)
+ elif propSet.name == 'config':
+ self.hosts[host.obj.value].osType = propSet.val.product.osType
+ self.hosts[host.obj.value].product = propSet.val.product.fullName
+ if hasattr(propSet.val.network, 'pnic'):
+ for nic in propSet.val.network.pnic:
+ self.hosts[host.obj.value].nics[nic.mac] = Nic(nic.device, nic.mac, nic.spec.ip.ipAddress)
+ if hasattr(propSet.val.network, 'consoleVnic'):
+ for nic in propSet.val.network.consoleVnic:
+ self.hosts[host.obj.value].nics[nic.spec.mac].ip = nic.spec.ip.ipAddress
+ if hasattr(propSet.val.network, 'vnic'):
+ for nic in propSet.val.network.vnic:
+ self.hosts[host.obj.value].nics[nic.spec.mac].ip = nic.spec.ip.ipAddress
# Get list of virtual machine uuids
object_contents = self.RetrieveProperties('VirtualMachine', ['name', 'config'], vmObjs)
@@ -209,7 +221,9 @@ class VSphere:
for cluster in self.clusters.values():
print "ComputeResource: %s" % cluster.name
for host in cluster.hosts:
- print "\tHostSystem: %s (%s)" % (host.name, host.uuid)
+ print "\tHostSystem: %s (%s) <%s, %s>" % (host.name, host.uuid, host.osType, host.product)
+ for mac, nic in host.nics.items():
+ print "\t\tNIC: %s (MAC: %s, IP: %s)" % (nic.device, mac, nic.ip)
for vm in host.vms:
print "\t\tVirtualMachine: %s (%s)" % (vm.name, vm.uuid)
@@ -230,7 +244,10 @@ class Host:
self.value = obj.value
self.name = name
self.uuid = None
+ self.osType = None
+ self.product = None
+ self.nics = {}
self.vms = []
class VM:
@@ -241,6 +258,12 @@ class VM:
self.name = name
self.uuid = None
+class Nic:
+ def __init__(self, device, mac, ip):
+ self.device = device
+ self.mac = mac
+ self.ip = ip
+
if __name__ == '__main__':
# TODO: read from config
if len(sys.argv) < 4:
12 years, 5 months
[virt-who] Vsphere test script for listing virtual machines in vCenter
by Radek Novacek
commit a26462d8cf3201711fe893c40033721f0c6b8859
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Mon Oct 31 09:04:23 2011 +0100
Vsphere test script for listing virtual machines in vCenter
vsphere.py | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 252 insertions(+), 0 deletions(-)
---
diff --git a/vsphere.py b/vsphere.py
new file mode 100644
index 0000000..da91015
--- /dev/null
+++ b/vsphere.py
@@ -0,0 +1,252 @@
+
+import sys
+import suds
+
+#import logging
+#logging.basicConfig(level=logging.INFO)
+#logging.getLogger('suds.client').setLevel(logging.DEBUG)
+
+def get_search_filter_spec(client, begin_entity, property_spec):
+ """ Build a PropertyFilterSpec capable of full inventory traversal.
+
+ By specifying all valid traversal specs we are creating a PFS that
+ can recursively select any object under the given enitity.
+ """
+
+ # The selection spec for additional objects we want to filter
+ ss_strings = ['resource_pool_traversal_spec',
+ 'resource_pool_vm_traversal_spec',
+ 'folder_traversal_spec',
+ 'datacenter_host_traversal_spec',
+ 'datacenter_vm_traversal_spec',
+ 'compute_resource_rp_traversal_spec',
+ 'compute_resource_host_traversal_spec',
+ 'host_vm_traversal_spec']
+
+ # Create a selection spec for each of the strings specified above
+ selection_specs = []
+ for ss_string in ss_strings:
+ sp = client.factory.create('ns0:SelectionSpec')
+ sp.name = ss_string
+ selection_specs.append(sp)
+
+ rpts = client.factory.create('ns0:TraversalSpec')
+ rpts.name = 'resource_pool_traversal_spec'
+ rpts.type = 'ResourcePool'
+ rpts.path = 'resourcePool'
+ rpts.selectSet = [selection_specs[0], selection_specs[1]]
+
+ rpvts = client.factory.create('ns0:TraversalSpec')
+ rpvts.name = 'resource_pool_vm_traversal_spec'
+ rpvts.type = 'ResourcePool'
+ rpvts.path = 'vm'
+
+ crrts = client.factory.create('ns0:TraversalSpec')
+ crrts.name = 'compute_resource_rp_traversal_spec'
+ crrts.type = 'ComputeResource'
+ crrts.path = 'resourcePool'
+ crrts.selectSet = [selection_specs[0], selection_specs[1]]
+
+ crhts = client.factory.create('ns0:TraversalSpec')
+ crhts.name = 'compute_resource_host_traversal_spec'
+ crhts.type = 'ComputeResource'
+ crhts.path = 'host'
+
+ dhts = client.factory.create('ns0:TraversalSpec')
+ dhts.name = 'datacenter_host_traversal_spec'
+ dhts.type = 'Datacenter'
+ dhts.path = 'hostFolder'
+ dhts.selectSet = [selection_specs[2]]
+
+ dvts = client.factory.create('ns0:TraversalSpec')
+ dvts.name = 'datacenter_vm_traversal_spec'
+ dvts.type = 'Datacenter'
+ dvts.path = 'vmFolder'
+ dvts.selectSet = [selection_specs[2]]
+
+ hvts = client.factory.create('ns0:TraversalSpec')
+ hvts.name = 'host_vm_traversal_spec'
+ hvts.type = 'HostSystem'
+ hvts.path = 'vm'
+ hvts.selectSet = [selection_specs[2]]
+
+ fts = client.factory.create('ns0:TraversalSpec')
+ fts.name = 'folder_traversal_spec'
+ fts.type = 'Folder'
+ fts.path = 'childEntity'
+ fts.selectSet = [selection_specs[2], selection_specs[3],
+ selection_specs[4], selection_specs[5],
+ selection_specs[6], selection_specs[7],
+ selection_specs[1]]
+
+ obj_spec = client.factory.create('ns0:ObjectSpec')
+ obj_spec.obj = begin_entity
+ obj_spec.selectSet = [fts, dvts, dhts, crhts, crrts, rpts, hvts, rpvts]
+
+ pfs = client.factory.create('ns0:PropertyFilterSpec')
+ pfs.propSet = [property_spec]
+ pfs.objectSet = [obj_spec]
+ return pfs
+
+
+class VSphere:
+ def __init__(self, url, username, password):
+ self.url = url
+
+ # Connect to the vCenter server
+ self.client = suds.client.Client("%s/sdk/vimService.wsdl" % url)
+
+ self.client.set_options(location="%s/sdk" % url)
+
+ # Get Meta Object Reference to ServiceInstance which is the root object of the inventory
+ self.moRef = suds.sudsobject.Property('ServiceInstance')
+ self.moRef._type = 'ServiceInstance'
+
+ # Service Content object defines properties of the ServiceInstance object
+ self.sc = self.client.service.RetrieveServiceContent(_this=self.moRef)
+
+ # Login to server using given credentials
+ self.client.service.Login(_this=self.sc.sessionManager, userName=username, password=password)
+
+ self.clusters = {}
+ self.hosts = {}
+ self.vms = {}
+
+ def scan(self):
+ """
+ Scan method does full inventory traversal on the vCenter machine. It finds
+ all ComputeResources, Hosts and VirtualMachines.
+ """
+
+ # Find all ComputeResources in whole vsphere tree
+ ts = self.client.factory.create('ns0:PropertySpec')
+ ts.type = 'ComputeResource'
+ ts.pathSet = 'name'
+ ts.all = True
+ object_content = self.client.service.RetrieveProperties(_this=self.sc.propertyCollector,
+ specSet=[get_search_filter_spec(self.client, self.sc.rootFolder, [ts])])
+
+ # Get properties of each cluster
+ clusterObjs = []
+ for cluster in object_content:
+ for propSet in cluster.propSet:
+ if propSet.name == "name":
+ self.clusters[cluster.obj.value] = Cluster(cluster.obj, propSet.val)
+ clusterObjs.append(cluster.obj)
+
+ # Get list of hosts from cluster
+ object_contents = self.RetrieveProperties('ComputeResource', 'host', clusterObjs)
+ hostObjs = []
+ for cluster in object_contents:
+ for propSet in cluster.propSet:
+ if propSet.name == 'host':
+ for host in propSet.val.ManagedObjectReference:
+ h = Host(host)
+ self.hosts[host.value] = h
+ self.clusters[cluster.obj.value].hosts.append(h)
+ hostObjs.append(host)
+
+ # Get list of host uuids, names and virtual machines
+ object_contents = self.RetrieveProperties('HostSystem', ['name', 'vm', 'hardware'], hostObjs)
+ vmObjs = []
+ for host in object_contents:
+ for propSet in host.propSet:
+ if propSet.name == "name":
+ self.hosts[host.obj.value].name = propSet.val
+ elif propSet.name == "hardware":
+ self.hosts[host.obj.value].uuid = propSet.val.systemInfo.uuid
+ elif propSet.name == "vm":
+ for vm in propSet.val.ManagedObjectReference:
+ vmObjs.append(vm)
+ v = VM(vm)
+ self.vms[vm.value] = v
+ self.hosts[host.obj.value].vms.append(v)
+
+ # Get list of virtual machine uuids
+ object_contents = self.RetrieveProperties('VirtualMachine', ['name', 'config'], vmObjs)
+ for obj in object_contents:
+ for propSet in obj.propSet:
+ if propSet.name == 'name':
+ self.vms[obj.obj.value].name = propSet.val
+ elif propSet.name == 'config':
+ self.vms[obj.obj.value].uuid = propSet.val.uuid
+
+
+ def RetrieveProperties(self, propSetType, propSetPathSet, objects):
+ """
+ Retrieve properties (defined by propSetPathSet) of objects of type propSetType.
+
+ propSetType - name of the type to query
+ propSetPathSet - property or list of properties to obtain
+ objects - get properties of each object from this list
+
+ return - object_properties struct
+ """
+
+ # PropertyFilterSpec is constructed from PropertySpec and ObjectSpec
+ propSet = self.client.factory.create('ns0:PropertySpec')
+ propSet.type = propSetType
+ propSet.all = False
+ propSet.pathSet = propSetPathSet
+
+ objectSets = []
+ for obj in objects:
+ objectSet = self.client.factory.create('ns0:ObjectSpec')
+ objectSet.obj = obj
+ objectSets.append(objectSet)
+
+ pfs = self.client.factory.create('ns0:PropertyFilterSpec')
+ pfs.propSet = [propSet]
+ pfs.objectSet = objectSets
+
+ # Query the VSphere server
+ return self.client.service.RetrieveProperties(_this=self.sc.propertyCollector, specSet=[pfs])
+
+ def printLayout(self):
+ """
+ Prints the layout of vCenter.
+ """
+ for cluster in self.clusters.values():
+ print "ComputeResource: %s" % cluster.name
+ for host in cluster.hosts:
+ print "\tHostSystem: %s (%s)" % (host.name, host.uuid)
+ for vm in host.vms:
+ print "\t\tVirtualMachine: %s (%s)" % (vm.name, vm.uuid)
+
+class Cluster:
+ def __init__(self, obj, name=None):
+ self.obj = obj
+ self._type = obj._type
+ self.value = obj.value
+ self.name = name
+ self.uuid = None
+
+ self.hosts = []
+
+class Host:
+ def __init__(self, obj, name=None):
+ self.obj = obj
+ self._type = obj._type
+ self.value = obj.value
+ self.name = name
+ self.uuid = None
+
+ self.vms = []
+
+class VM:
+ def __init__(self, obj, name=None):
+ self.obj = obj
+ self.value = obj.value
+ self._type = obj._type
+ self.name = name
+ self.uuid = None
+
+if __name__ == '__main__':
+ # TODO: read from config
+ if len(sys.argv) < 4:
+ print "Usage: %s url username password"
+ sys.exit(0)
+
+ vsphere = VSphere(sys.argv[1], sys.argv[2], sys.argv[3])
+ vsphere.scan()
+ vsphere.printLayout()
12 years, 5 months
[virt-who] Bump to version 0.4
by Radek Novacek
commit c44b681a553f45bba4227ae597d22dcb612199de
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Wed Nov 30 16:48:15 2011 +0100
Bump to version 0.4
Makefile | 2 +-
virt-who.spec | 7 +++++--
2 files changed, 6 insertions(+), 3 deletions(-)
---
diff --git a/Makefile b/Makefile
index 28ac69e..f009b54 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
name = virt-who
-version = 0.3
+version = 0.4
.PHONY: pack check install srpm rpm rpmlint upload
diff --git a/virt-who.spec b/virt-who.spec
index f1e3b22..bb561fd 100644
--- a/virt-who.spec
+++ b/virt-who.spec
@@ -1,6 +1,6 @@
Name: virt-who
-Version: 0.3
-Release: 2%{?dist}
+Version: 0.4
+Release: 1%{?dist}
Summary: Agent for reporting virtual guest IDs to subscription-manager
Group: System Environment/Base
@@ -66,6 +66,9 @@ fi
%changelog
+* Wed Nov 30 2011 Radek Novacek <rnovacek(a)redhat.com> 0.4-1
+- Version 0.4
+
* Thu Oct 06 2011 Radek Novacek <rnovacek(a)redhat.com> - 0.3-2
- Requires python-rhsm >= 0.96.13 (contains fix for char limit in uuid list)
12 years, 5 months