From Crimson Armadillo, 1 Month ago, written in Plain Text.
This paste will slip away in 9 Months.
Embed
  1. #!/usr/bin/env python
  2. import os
  3. from os import environ as env
  4. from keystoneauth1 import identity
  5. from keystoneauth1 import session
  6. from keystoneauth1.identity import v3
  7.  
  8. from keystoneclient.v3 import client as keystoneclient
  9. from novaclient import client as novaclient
  10. from glanceclient import Client as glanceclient
  11. from neutronclient.v2_0 import client as neutronclient
  12.  
  13. import argparse
  14. import getpass
  15. #### GLOBALS
  16.  
  17. def get_env():
  18.     options = {}
  19.     options['username'] = env['OS_USERNAME']
  20.     options['password'] = env['OS_PASSWORD']
  21.     options['project_name'] = env['OS_TENANT_NAME']
  22.     options['auth_url'] = env['OS_AUTH_URL']
  23.     return options
  24.  
  25. options = get_env()
  26.  
  27. # Add your own architectures along with their authentication endpoints
  28. cluster_auth_urls = {
  29.     'x86': 'https://oprod-controller1.osuosl.org:35357/v3/',
  30.     'power': 'https://openpower-controller.osuosl.org:35357/v3/'
  31. }
  32.  
  33. ####
  34.  
  35. parser = argparse.ArgumentParser(description='Interactively or premeditatively spinup an OpenStack instance.')
  36. args = {}
  37.  
  38. def parseArguments():
  39.     parser.add_argument(
  40.       '-f', '--flavor',
  41.       nargs=1,
  42.       help='Flavor Name'
  43.     )
  44.     parser.add_argument(
  45.       '-n', '--network',
  46.       nargs=1,
  47.       help='Network Name'
  48.     )
  49.     parser.add_argument(
  50.       '-p', '--project',
  51.       nargs=1,
  52.       help='Project Name'
  53.     )
  54.     parser.add_argument(
  55.       '-i', '--image',
  56.       nargs=1,
  57.       help='Project Name'
  58.     )
  59.     parser.add_argument(
  60.       '-k', '--keypair',
  61.       nargs=1,
  62.       help='SSH Keypair Name'
  63.     )
  64.     parser.add_argument(
  65.       '-u', '--username',
  66.       nargs=1,
  67.       help='SSH Keypair Name'
  68.     )
  69.     parser.add_argument(
  70.       'cluster',
  71.       help='Name of cluster. Either "x86" or "power"',
  72.     )
  73.     parser.add_argument(
  74.       'name',
  75.       metavar='INSTANCE_NAME',
  76.       help='Name of Instance',
  77.     )
  78.     return parser.parse_args()
  79.  
  80. def setGlobals(arguments):
  81.     global cluster_auth_urls
  82.     global options
  83.  
  84.     options['instance_name'] = arguments.name
  85.     options['cluster_name'] = arguments.cluster
  86.  
  87.     try:
  88.         options['auth_url'] = cluster_auth_urls[options['cluster_name']]
  89.     except KeyError:
  90.         print 'No such server ' + options['cluster_name'] + '.'
  91.         print 'Defaulting to ' + cluster_auth_urls['x86'] + '.'
  92.         options['auth_url'] = cluster_auth_urls['x86']
  93.  
  94.     if arguments.username != None:
  95.         options['username'] = arguments.username[0]
  96.         options['password'] = getpass.getpass(prompt='Password for %s' % username)
  97.     if arguments.network != None:
  98.         options['network_name'] = arguments.network[0]
  99.     if arguments.project != None:
  100.         options['project_name'] = arguments.project[0]
  101.     if arguments.image != None:
  102.         options['image_name'] = arguments.image[0]
  103.     if arguments.flavor != None:
  104.         options['flavor_name'] = arguments.flavor[0]
  105.     if arguments.keypair != None:
  106.         options['keypair_name'] = arguments.keypair[0]
  107.  
  108. def processArgs():
  109.     args = parseArguments()
  110.     setGlobals(args)
  111.  
  112. def getSession():
  113.     global options
  114.     if getSession.sess == None:
  115.         auth = v3.Password(
  116.             auth_url=options['auth_url'],
  117.             username=options['username'],
  118.             password=options['password'],
  119.             project_name=options['project_name'],
  120.             project_domain_name='default',
  121.             user_domain_name='default'
  122.         )
  123.         getSession.sess = session.Session(auth=auth)
  124.     return getSession.sess
  125.  
  126. getSession.sess = None
  127.  
  128. def getUserChoice(header, typestring, iterable, key=None):
  129.     print header
  130.     # Iterate over each item passed in.
  131.     for i in range(0, len(iterable)):
  132.         if isinstance(iterable[i], dict):
  133.             print '[%s] %s' % (i + 1, iterable[i][key])
  134.         else:
  135.             print '[%s] %s' % (i + 1, iterable[i])
  136.  
  137.     choice = input('Choose your ' + typestring + ': ')
  138.     return iterable[choice-1]
  139.  
  140. def getFlavor(apiobject):
  141.     global options
  142.     if 'flavor_name' in options:
  143.         for flavor in apiobject.flavors.list():
  144.             if flavor.name == options['flavor_name']:
  145.                 return flavor
  146.         try:
  147.             input('No matching flavor found... Press enter to continue: ')
  148.         except SyntaxError:
  149.             pass
  150.  
  151.     return getUserChoice(
  152.         "List of all valid flavors: ",
  153.         'flavor',
  154.         apiobject.flavors.list()
  155.     )
  156.  
  157. def getImage(apiobject):
  158.     global options
  159.     if 'image_name' in options:
  160.         for image in apiobject.images.list():
  161.             if image[u'name'] == options['image_name']:
  162.                 return image
  163.         try:
  164.             input('No matching image found... Press enter to continue: ')
  165.         except SyntaxError:
  166.             pass
  167.  
  168.     return getUserChoice(
  169.         "List of all valid images: ",
  170.         'image',
  171.         list(apiobject.images.list()),
  172.         key=u'name'
  173.     )
  174.  
  175. def getNetwork(apiobject):
  176.     global options
  177.     if 'network_name' in options:
  178.         for network in apiobject.list_networks()['networks']:
  179.             if network[u'name'] == options['network_name']:
  180.                 return network
  181.         try:
  182.             input('No matching network found... Press enter to continue: ')
  183.         except SyntaxError:
  184.             pass
  185.  
  186.     return getUserChoice(
  187.         "List of all networks: ",
  188.         'network',
  189.         apiobject.list_networks()['networks'],
  190.         key=u'name'
  191.     )
  192.  
  193. def getKeypair(apiobject):
  194.     global options
  195.     if 'keypair_name' in options:
  196.         for keypair in apiobject.keypairs.list():
  197.             if keypair.name == options['keypair_name']:
  198.                 return keypair
  199.         try:
  200.             input('No matching keypair found... Press enter to continue: ')
  201.         except SyntaxError:
  202.             pass
  203.  
  204.     return getUserChoice(
  205.         "List of all keypairs",
  206.         'keypair',
  207.         apiobject.keypairs.list()
  208.     )
  209.  
  210. #### Beginning of doing work
  211.  
  212. processArgs()
  213.  
  214. keystone = keystoneclient.Client(session=getSession())
  215. glance = glanceclient('2', session=getSession())
  216. nova = novaclient.Client('2.1', session=getSession())
  217.  
  218. def get_neutron_credentials():
  219.     d = {}
  220.     d['username'] = os.environ['OS_USERNAME']
  221.     d['password'] = os.environ['OS_PASSWORD']
  222.     d['auth_url'] = os.environ['OS_AUTH_URL']
  223.     d['tenant_name'] = os.environ['OS_TENANT_NAME']
  224.     return d
  225.  
  226. neutron = neutronclient.Client(
  227.     **get_neutron_credentials()
  228. )
  229.  
  230. vm = nova.servers.create(
  231.     options['instance_name'],
  232.     getImage(glance),
  233.     getFlavor(nova),
  234.     key_name=getKeypair(nova).name,
  235.     nics=[{'net-id': getNetwork(neutron)['id']}]
  236. )