#!/usr/bin/env python
#
# Show all ports of all VM instances for
# all users and topologies.
# Like the console port and the VNC port
#
# <EMAIL>
#


import os, libvirt, re, prettytable, sys
from novaclient.v1_1 import Client
from xml.dom.minidom import parseString

def main():
	# Sample output / field mapping
	# </guest/endpoint>-<Sample_Topologies@single-server-WO9N_h>-<csr1000v-1>
	#         USER            PROJECT          TOPOLOGY             NODE
	prog=re.compile(r'</(.*)/endpoint>-<(.*)@(.*)-[_0-9a-z]{6}>-<(.*)>', re.IGNORECASE)

	# table=list()
	try:
		libvirt_uri = os.environ['LIBVIRT_DEFAULT_URI']
	except:
		libvirt_uri = "qemu:///system"
		print "LIBVIRT_DEFAULT_URI env not set!"
		print "Using default '" + libvirt_uri + "'"
	conn=libvirt.openReadOnly(libvirt_uri)

	nc=Client(username=os.environ['OS_USERNAME'], api_key=os.environ['OS_PASSWORD'], 
		project_id=os.environ['OS_TENANT_NAME'], auth_url=os.environ['OS_AUTH_URL'])

	pt = prettytable.PrettyTable(["User", "Topology", "Node", "VNC", "Console"])
	pt.align = "l"

	for server in nc.servers.list(search_opts={'all_tenants': True}):
		m=prog.match(server.name)
		if m:
			try:
				domain=conn.lookupByUUIDString(server.id)
			except:
				print "Domain not found / not running"
				return 1
			else:
				doc=parseString(domain.XMLDesc(flags=0))
			for i in doc.getElementsByTagName('graphics'):
				port=i.getAttribute('port')
			for i in doc.getElementsByTagName('source'):
				if i.parentNode.nodeName == u'console':
					console=i.getAttribute('service')
			pt.add_row([m.group(1), m.group(3), m.group(4), port, console])
	print pt.get_string(sortby="Topology")

if __name__ == '__main__':
	sys.exit(main())



#!/usr/bin/env python
#
# Show all ports of all VM instances for
# all users and topologies.
# Like the console port and the VNC port
#
# <EMAIL>
#


import os, libvirt, re, prettytable, sys
from novaclient.v1_1 import Client
from xml.dom.minidom import parseString

def main():
	# Sample output / field mapping
	# </guest/endpoint>-<Sample_Topologies@single-server-WO9N_h>-<csr1000v-1>
	#         USER            PROJECT          TOPOLOGY             NODE
	prog=re.compile(r'</(.*)/endpoint>-<(.*)@(.*)-[_0-9a-z]{6}>-<(.*)>', re.IGNORECASE)

	# table=list()
	try:
		libvirt_uri = os.environ['LIBVIRT_DEFAULT_URI']
	except:
		libvirt_uri = "qemu:///system"
		print "LIBVIRT_DEFAULT_URI env not set!"
		print "Using default '" + libvirt_uri + "'"
	conn=libvirt.openReadOnly(libvirt_uri)

	nc=Client(username=os.environ['OS_USERNAME'], api_key=os.environ['OS_PASSWORD'], 
		project_id=os.environ['OS_TENANT_NAME'], auth_url=os.environ['OS_AUTH_URL'])

	pt = prettytable.PrettyTable(["User", "Topology", "Node", "VNC", "Console"])
	pt.align = "l"

	for server in nc.servers.list(search_opts={'all_tenants': True}):
		m=prog.match(server.name)
		if m:
			try:
				domain=conn.lookupByUUIDString(server.id)
			except:
				print "Domain not found / not running"
				return 1
			else:
				doc=parseString(domain.XMLDesc(flags=0))
			for i in doc.getElementsByTagName('graphics'):
				port=i.getAttribute('port')
			for i in doc.getElementsByTagName('source'):
				if i.parentNode.nodeName == u'console':
					console=i.getAttribute('service')
			pt.add_row([m.group(1), m.group(3), m.group(4), port, console])
	print pt.get_string(sortby="Topology")

if __name__ == '__main__':
	sys.exit(main())



#!/usr/bin/env python # # Show all ports of all VM instances for # all users and topologies. # Like the console port and the VNC port # # <EMAIL> # # Sample output / field mapping # </guest/endpoint>-<Sample_Topologies@single-server-WO9N_h>-<csr1000v-1> #         USER            PROJECT          TOPOLOGY             NODE # table=list()