Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from novaclient import client
- from os import environ
- import re
- """
- Script to set hostnames / IP addresses mapping
- in /etc/hosts
- """
- try:
- USERNAME = environ['OS_USERNAME']
- PASSWORD = environ['OS_PASSWORD']
- AUTH_URL = environ['OS_AUTH_URL']
- except Exception as e:
- raise Exception("Missing one or more authentication details: %s" % e)
- try:
- PROJECT_NAME = environ['OS_PROJECT_NAME']
- except Exception as e:
- PROJECT_NAME = environ['OS_TENANT_NAME']
- try:
- OS_CLOUD = environ['OS_CLOUDNAME']
- except Exception as e:
- raise Exception("Missing OS_CLOUDNAME")
- if OS_CLOUD != 'undercloud':
- raise Exception("You need to load the undercloud authentication details")
- def main():
- """ Example names:
- overcloud-controller-2
- overcloud-novacompute-0 """
- # Regex to extract short name from
- full_name_regex = r'^overcloud\-(.+?)$'
- nova = client.Client('2', USERNAME, PASSWORD, PROJECT_NAME,
- AUTH_URL, connection_pool=True)
- prep_list = dict()
- # Get all servers
- servers = nova.servers.list()
- for server in servers:
- # Get networks/IP for ctlplane
- networks = server.networks
- if ('ctlplane' in networks and len(networks['ctlplane'])):
- short_name = ''
- # Extract shortnames
- m = re.search(full_name_regex, server.name)
- if m:
- short_name_prep = m.group(1)
- short_name_prep = re.sub('-','', short_name_prep)
- short_name = re.sub('nova','', short_name_prep)
- # Save in temporary dict
- prep_list[networks['ctlplane'][0]] = server.name + ' ' + short_name
- print "Add the following to your /etc/hosts:\n"
- print '###setHostsStart###'
- for k, v in prep_list.items():
- print "%s\t%s" % (k ,v)
- print '###setHostsEnd###'
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement