Advertisement
Guest User

Untitled

a guest
Dec 30th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from novaclient import client
  3. from os import environ
  4. import re
  5.  
  6. """
  7. Script to set hostnames / IP addresses mapping
  8. in /etc/hosts
  9. """
  10.  
  11. try:
  12. USERNAME = environ['OS_USERNAME']
  13. PASSWORD = environ['OS_PASSWORD']
  14. AUTH_URL = environ['OS_AUTH_URL']
  15. except Exception as e:
  16. raise Exception("Missing one or more authentication details: %s" % e)
  17.  
  18. try:
  19. PROJECT_NAME = environ['OS_PROJECT_NAME']
  20. except Exception as e:
  21. PROJECT_NAME = environ['OS_TENANT_NAME']
  22.  
  23. try:
  24. OS_CLOUD = environ['OS_CLOUDNAME']
  25. except Exception as e:
  26. raise Exception("Missing OS_CLOUDNAME")
  27.  
  28. if OS_CLOUD != 'undercloud':
  29. raise Exception("You need to load the undercloud authentication details")
  30.  
  31.  
  32. def main():
  33.  
  34. """ Example names:
  35. overcloud-controller-2
  36. overcloud-novacompute-0 """
  37.  
  38. # Regex to extract short name from
  39. full_name_regex = r'^overcloud\-(.+?)$'
  40.  
  41. nova = client.Client('2', USERNAME, PASSWORD, PROJECT_NAME,
  42. AUTH_URL, connection_pool=True)
  43.  
  44. prep_list = dict()
  45.  
  46. # Get all servers
  47. servers = nova.servers.list()
  48.  
  49. for server in servers:
  50.  
  51. # Get networks/IP for ctlplane
  52. networks = server.networks
  53. if ('ctlplane' in networks and len(networks['ctlplane'])):
  54.  
  55. short_name = ''
  56.  
  57. # Extract shortnames
  58. m = re.search(full_name_regex, server.name)
  59. if m:
  60. short_name_prep = m.group(1)
  61. short_name_prep = re.sub('-','', short_name_prep)
  62. short_name = re.sub('nova','', short_name_prep)
  63.  
  64. # Save in temporary dict
  65. prep_list[networks['ctlplane'][0]] = server.name + ' ' + short_name
  66.  
  67. print "Add the following to your /etc/hosts:\n"
  68. print '###setHostsStart###'
  69. for k, v in prep_list.items():
  70. print "%s\t%s" % (k ,v)
  71. print '###setHostsEnd###'
  72.  
  73. if __name__ == "__main__":
  74. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement