SHARE
TWEET

Untitled

a guest May 28th, 2015 390 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. import logging
  4. import os
  5. from neutronclient.neutron import client as neutronc
  6. from novaclient import client as novac
  7. from optparse import OptionParser
  8. import inspect
  9. #logging.basicConfig(level=logging.INFO)
  10.  
  11. def parse_args():
  12.         parser = OptionParser()
  13.         parser.add_option("-p", "--project", dest="project_id",
  14.                   help="project ID which needs quota modifications")
  15.         (options, args) = parser.parse_args()
  16.         return options, args
  17.  
  18. def init_openstack():
  19.         global neutron, nova
  20.         neutron = neutronc.Client('2.0', auth_url=os.environ['OS_AUTH_URL'],
  21.                                 tenant_name=os.environ['OS_TENANT_NAME'],
  22.                                 username=os.environ['OS_USERNAME'],
  23.                                 password=os.environ['OS_PASSWORD'],
  24.                                 region_name=os.environ['OS_REGION_NAME'])
  25.         nova = novac.Client('2', auth_url=os.environ['OS_AUTH_URL'],
  26.                                 tenant_id=os.environ['OS_TENANT_ID'],
  27.                                 username=os.environ['OS_USERNAME'],
  28.                                 api_key=os.environ['OS_PASSWORD'],
  29.                                 region_name=os.environ['OS_REGION_NAME'])
  30.         neutron.format = 'json'
  31.         nova.format='json'
  32.         return
  33.  
  34. def get_quota_info(project_id):
  35.         '''gets quota info from the user'''
  36.         # need to find a way to validate the tenant id...
  37.  
  38.         #print project_id
  39.         nova_qs = nova.quotas.get(project_id)
  40.         #print nova_qs
  41.         neutron_qs = neutron.show_quota(project_id)
  42.         #print neutron_qs
  43.         #neutron_qs = neutron.get_quotas_tenant(project_id)
  44.  
  45.         instances = raw_input('Enter number of VMs (instances) ['+str(nova_qs.instances)+']:')
  46.         if not instances:
  47.                 instances = nova_qs.instances
  48.         else:
  49.                 instances = int(instances)
  50.         if instances < 2 or instances > 8:
  51.                 print 'Instances outside valid range (2-8)'
  52.                 exit(1)
  53.         vcpus = raw_input('Enter total number of CPUs (cores) ['+str(nova_qs.cores)+']:')
  54.         if not vcpus:
  55.                 vcpus = nova_qs.cores
  56.         else:
  57.                 vcpus = int(vcpus)
  58.         if vcpus < 2 or vcpus > 10:
  59.                 print 'vCPUs outside valid range (2-10)'
  60.                 exit(1)
  61.         ram = raw_input('Enter total RAM (in MB) ['+str(nova_qs.ram)+']:')
  62.         if not ram:
  63.                 ram = nova_qs.ram
  64.         else:
  65.                 ram = int(ram)
  66.         if ram < 4096 or ram > 10240:
  67.                 print 'RAM outside valid range (4096-10240)'
  68.                 exit(1)
  69.         print 'Skipping disk configuration for now...'
  70.         #harddisk = raw_input('Enter total harddisk (in GB):')
  71.         #if hardisk < 50 or harddisk > 100:
  72.         #       print 'Hardidsk outside valid range (50-100)'
  73.         #       exit(1)
  74.         public_ips = raw_input('Enter total public IPs [(nova),'+str(nova_qs.floating_ips)+', (neutron) '+str(neutron_qs['quota']['floatingip'])+']:')
  75.         if not public_ips:
  76.                 public_ips = neutron_qs['quota']['floatingip']
  77.         else:
  78.                 public_ips = int(public_ips)
  79.         if public_ips < 0 or public_ips > 4:
  80.                 print 'Public IPs outside valid range (1-4)'
  81.                 exit(1)
  82.         ret_val = {'instances': instances, 'cores': vcpus, 'ram': ram, 'public_ips': public_ips }
  83.         return ret_val
  84.  
  85. def update_quotas(project_id, q_info):
  86.         print
  87.         print 'Updating quotas for user '+project_id
  88.         nova_qs = nova.quotas.get(project_id)
  89.         neutron_qs = neutron.show_quota(project_id)
  90.         #print 'nova - instances(), cores (), floatingips()'
  91.         nova_qs = nova.quotas.update(project_id, instances=q_info['instances'], cores=q_info['cores'], floating_ips=q_info['public_ips'])
  92.         #print 'neutron - floatingips()'
  93.         neutron_qs = neutron.update_quota(project_id, {'quota': {'floatingip': q_info['public_ips'] }})
  94.  
  95.         print
  96.         print 'Quotas updated...'
  97.         print
  98.         nova_qs = nova.quotas.get(project_id).to_dict()
  99.         print 'Nova quota for project '+project_id
  100.         for k,v in nova_qs.iteritems():
  101.                 print '   '+str(k)+': '+str(v)
  102.         neutron_qs = neutron.show_quota(project_id)
  103.         print
  104.         print 'Neutron quota for project '+project_id
  105.         for k,v in neutron_qs['quota'].iteritems():
  106.                 print '   '+str(k)+': '+str(v)
  107.  
  108. def get_project_id():
  109.         tenant_id = raw_input( 'Enter project id:')
  110.         #no validation right now...
  111.         return tenant_id
  112.  
  113. def main():
  114.         '''main function'''
  115.         opts, args = parse_args()
  116.         if opts.project_id is None:
  117.                 project_id = get_project_id()
  118.         else:
  119.                 project_id = opts.project_id
  120.         print "Updating quotas for project_id "+project_id
  121.         init_openstack()
  122.         q_info = get_quota_info(project_id)
  123.         update_quotas(project_id, q_info)
  124.  
  125. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top