Skip to content

Instantly share code, notes, and snippets.

@nuriel77
Created March 31, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuriel77/a1f6f01c25e4c2c4c440fbabd33dd9e8 to your computer and use it in GitHub Desktop.
Save nuriel77/a1f6f01c25e4c2c4c440fbabd33dd9e8 to your computer and use it in GitHub Desktop.
Get ironic - nova server name relations
#!/usr/bin/env python
from keystoneauth1 import identity
from keystoneauth1 import session
from ironicclient import client as ironic_client
from novaclient import client as nova_client
from os import environ
import getopt
import sys
class Hosts():
def __init__(self, ironic, nova, ironic_node=None,
nova_server=None):
self.ironic = ironic
self.nova = nova
self.nova_server = nova_server
self.ironic_node = ironic_node
@property
def get_ironic_nodes(self):
return self.ironic.node.list()
def get_server_by_uuid(self, uuid):
return self.nova.servers.list(detailed=True,
search_opts={'uuid': uuid})
def get_server_by_name(self, name):
return self.nova.servers.list(detailed=True,
search_opts={'name': name})
def start_query(self):
if not self.nova_server:
self.get_via_ironic()
else:
self.get_via_nova()
def get_via_nova(self):
server = self.get_server_by_name(self.nova_server)
if not len(server):
sys.exit(1)
node = self.ironic.node.get_by_instance_uuid(server[0].id)
print ("%s %s %s" % (node.name, server[0].name,
server[0].addresses['ctlplane'][0]['addr']))
def get_via_ironic(self):
nodes = self.get_ironic_nodes
for node in nodes:
if self.ironic_node and node.name != self.ironic_node:
continue
if node.instance_uuid == None:
continue
server = self.get_server_by_uuid(node.instance_uuid)
print ("%s %s %s" % (node.name, server[0].name,
server[0].addresses['ctlplane'][0]['addr']))
def usage():
print """Usage:\n\
-h|--help ... help\n\
-i|--ironic ... By ironic node\n\
-n|--nova ... By nova server\n\
"""
sys.exit(0)
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:n:",
["help",
"ironic=",
"nova="])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
nova_server = None
ironic_node = None
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
if opt in ("-i", "--ironic"):
ironic_node = arg
continue
if opt in ("-n", "--nova"):
nova_server = arg
continue
print "Unknown option '%s'" % opt
usage()
sys.exit(1)
if nova_server is not None and ironic_node is not None:
print "You can only choose either nova server or ironic node"
sys.exit(1)
try:
environ['OS_CLOUDNAME']
except:
pass
else:
if environ['OS_CLOUDNAME'] == 'overcloud':
log("Overcloud auth details loaded."
" Make sure you load stackrc!")
sys.exit(1)
try:
environ['OS_TENANT_NAME']
except Exception as e:
try:
environ['OS_PROJECT_NAME']
except Exception as e:
raise Exception("Did not find OS_PROJECT_NAME!"
" Missing authentication details.")
else:
environ['OS_TENANT_NAME'] = environ['OS_PROJECT_NAME']
try:
environ['OS_TENANT_NAME']
environ['OS_PASSWORD']
environ['OS_USERNAME']
environ['OS_AUTH_URL']
except Exception as e:
raise Exception('Missing authentication credentials: %s' % e)
username = environ['OS_USERNAME']
password = environ['OS_PASSWORD']
project_name = environ['OS_TENANT_NAME']
auth_url = environ['OS_AUTH_URL']
kwargs = {'os_username': username,
'os_password': password,
'os_auth_url': auth_url,
'os_project_name': project_name}
try:
ironic = ironic_client.get_client(1, **kwargs)
except Exception as e:
raise Exception("Error: %s" % e)
auth = identity.Password(auth_url=auth_url,
username=username,
password=password,
project_name=project_name)
sess = session.Session(auth=auth)
try:
nova = nova_client.Client(2, session=sess)
except Exception as e:
raise Exception("Error: %s" % e)
Hosts(ironic=ironic,
nova=nova,
ironic_node=ironic_node,
nova_server=nova_server).start_query()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment