You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.1 KiB

  1. #!/usr/bin/env python
  2. #
  3. # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  4. #
  5. # Show all ports of all VM instances for
  6. # all users and topologies.
  7. # Like the console port and the VNC port
  8. #
  9. # rschmied@cisco.com
  10. #
  11. # modified by sebastian.rieger@informatik.hs-fulda.de to display telnet console ports of running nodes in a topology
  12. #
  13. import os, libvirt, re, sys
  14. from keystoneclient import client as keystone
  15. from novaclient import client as nova
  16. from xml.dom.minidom import parseString
  17. """
  18. In [14]: m.group(0)
  19. Out[14]: '</guest/endpoint>-<Sample_Project@asa-test-topo-yJHnoK>-<iosv-2>-<Multipoint Connection-1>'
  20. In [15]: m.group(1)
  21. Out[15]: '/guest/endpoint'
  22. In [16]: m.group(2)
  23. Out[16]: 'Sample_Project'
  24. In [16]: m.group(3)
  25. Out[16]: 'asa-test-topo'
  26. In [17]: m.group(4)
  27. Out[17]: 'iosv-2'
  28. In [18]: m.group(5)
  29. Out[18]: 'Multipoint Connection-1'
  30. will not match jumphost ports!
  31. not interested in these, anyway
  32. """
  33. class KeystoneV3NovaAuthPlugin(object):
  34. def __init__(self, keystone_client):
  35. self.keystone_client = keystone_client
  36. def authenticate(self, client, fake_auth_url):
  37. client.auth_url = fake_auth_url
  38. client.service_catalog = self.keystone_client.service_catalog
  39. client.auth_token = self.keystone_client.auth_token
  40. client.tenant_id = self.keystone_client.tenant_id
  41. client.management_url = self.keystone_client.service_catalog.url_for(
  42. attr='region',
  43. filter_value=client.region_name,
  44. endpoint_type=client.endpoint_type,
  45. service_type=client.service_type,
  46. service_name=client.service_name).rstrip('/')
  47. def getports(user,simulation):
  48. # Sample output / field mapping
  49. # </guest/endpoint>-<Sample_Topologies@single-server-WO9N_h>-<csr1000v-1>
  50. # USER PROJECT TOPOLOGY NODE
  51. # </advcompnet/endpoint>-<advcompnet-lab1-dcn-scenario1-ymaMSJ>-<veos-4>
  52. # USER TOPOLOGY NODE
  53. prog=re.compile(r'</(.*)/endpoint>-<(.*)-[_0-9a-z]{6}>-<(.*)>', re.IGNORECASE)
  54. # table=list()
  55. try:
  56. libvirt_uri = os.environ['LIBVIRT_DEFAULT_URI']
  57. except:
  58. libvirt_uri = "qemu:///system"
  59. print "LIBVIRT_DEFAULT_URI env not set!"
  60. print "Using default '" + libvirt_uri + "'"
  61. conn=libvirt.openReadOnly(libvirt_uri)
  62. kc = keystone.Client(auth_url=os.environ['OS_AUTH_URL'],
  63. username=os.environ['OS_USERNAME'], password=os.environ['OS_PASSWORD'],
  64. project_name=os.environ['OS_TENANT_NAME'])
  65. kc.session.auth = kc
  66. kc.authenticate()
  67. nc=nova.Client('2', os.environ['OS_USERNAME'], os.environ['OS_PASSWORD'],
  68. os.environ['OS_TENANT_NAME'], auth_system='keystonev3', auth_plugin=KeystoneV3NovaAuthPlugin(kc), auth_url='http://fake/v2.0')
  69. for server in nc.servers.list(search_opts={'all_tenants': True}):
  70. m=prog.match(server.name)
  71. if m:
  72. try:
  73. domain=conn.lookupByUUIDString(server.id)
  74. except:
  75. print "Domain not found / not running"
  76. return 1
  77. else:
  78. doc=parseString(domain.XMLDesc(flags=0))
  79. # get the VNC port
  80. #port=doc.getElementsByTagName('graphics')[0].getAttribute('port')
  81. # get the serial console TCP port
  82. for i in doc.getElementsByTagName('source'):
  83. if i.parentNode.nodeName == u'console':
  84. console=i.getAttribute('service')
  85. # get the instance name
  86. name=doc.getElementsByTagName('name')[0].childNodes[0].nodeValue
  87. # print info
  88. if simulation == "*":
  89. if m.group(1) == user:
  90. print m.group(3) + "=" + console
  91. else:
  92. if m.group(1) == user and server.name.find(simulation) != -1:
  93. print m.group(3) + "=" + console
  94. def main():
  95. if len(sys.argv) != 3:
  96. sys.stdout.write(str(sys.argv[0]))
  97. print ": username and simulation (e.g., project name or session-id) needed as argument! bailing out"
  98. return 1
  99. else:
  100. user = str(sys.argv[1]).strip()
  101. simulation = str(sys.argv[2]).strip()
  102. getports(user,simulation)
  103. return 0
  104. if __name__ == '__main__':
  105. sys.exit(main())