def run(self): while self.stop is False: try: if not creator_q.empty(): vm_type, params = creator_q.get(True, 10) if params.number > 1: rnd = ''.join( random.choice(string.ascii_letters) for _ in range(6)) vm_name = '%s-%s' % (params.vmname, rnd) else: vm_name = params.name vm_creation_sem.acquire() # create lock to avoid double creation single_lock.acquire() op = False if vm_type is 'smart': from ArubaCloud.objects import SmartVmCreator creator = SmartVmCreator( name=vm_name, admin_password=params.admin_pwd, template_id=params.template, auth_obj=pool[params.dc].auth) creator.set_type(size=params.pkg) op = creator.commit(url=pool[params.dc].wcf_baseurl) elif vm_type is 'pro': from ArubaCloud.objects import ProVmCreator creator = ProVmCreator(name=vm_name, admin_password=params.adminpwd, template_id=params.template, auth_obj=pool[params.dc].auth) if params.buyip is True: ip = pool[params.dc].purchase_ip() creator.add_public_ip(ip.resid) creator.add_virtual_disk(params.disk1) if params.disk2 > 0: creator.add_virtual_disk(params.disk2) if params.disk3 > 0: creator.add_virtual_disk(params.disk3) if params.disk4 > 0: creator.add_virtual_disk(params.disk4) creator.set_cpu_qty(int(params.cpuqty)) creator.set_ram_qty(int(params.ramqty)) op = creator.commit(url=pool[params.dc].wcf_baseurl) ret = True if op is True else False single_lock.release() if ret is False: self.logger.warning('Cannot create VM.') else: while pool[params.dc].find_job( vm_name) is not 'JOBNOTFOUND': time.sleep(10) cprint('Creation of VM: %s Done.' % vm_name, 'green') creator_q.task_done() vm_creation_sem.release() time.sleep(0.25) except Queue.Empty: pass
from ArubaCloud.PyArubaAPI import CloudInterface if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-d', '--datacenter', help='Specify datacenter to login.', action='store', type=int, dest='dc') parser.add_argument('-u', '--username', help='Specify username.', action='store', dest='username') parser.add_argument('-w', '--password', help='Specify password.', action='store', dest='password') parser.add_argument('-t', '--template', help='Specify template.', action='store', dest='template') parser.add_argument('-n', '--name', help='Specify VM name', action='store', dest='vmname') parser.add_argument('--vmpassword', help='Specify VM admin password.', action='store', dest='vmpassword', default='ArubaCloud2015') p = parser.parse_args() i = CloudInterface(dc=p.dc, debug_level=logging.DEBUG) i.login(username=p.username, password=p.password, load=False) ip = i.purchase_ip(debug=True) from ArubaCloud.objects import ProVmCreator c = ProVmCreator(name=p.vmname, admin_password=p.vmpassword, template_id=p.template, auth_obj=i.auth) c.set_cpu_qty(2) c.set_ram_qty(6) c.add_public_ip(public_ip_address_resource_id=ip.resid) c.add_virtual_disk(40) c.add_virtual_disk(40) pprint(c.get_json()) print(c.commit(url=i.wcf_baseurl, debug=True))
'--name', help='Specify VM name', action='store', dest='vmname') parser.add_argument('--vmpassword', help='Specify VM admin password.', action='store', dest='vmpassword', default='ArubaCloud2015') p = parser.parse_args() i = CloudInterface(dc=p.dc, debug_level=logging.DEBUG) i.login(username=p.username, password=p.password, load=False) ip = i.purchase_ip(debug=True) from ArubaCloud.objects import ProVmCreator c = ProVmCreator(name=p.vmname, admin_password=p.vmpassword, template_id=p.template, auth_obj=i.auth) c.set_cpu_qty(2) c.set_ram_qty(6) c.add_public_ip(public_ip_address_resource_id=ip.resid) c.add_virtual_disk(40) c.add_virtual_disk(40) pprint(c.get_json()) print(c.commit(url=i.wcf_baseurl, debug=True))
def aruba(cls, dc, username, password, servertype=None, flavor=None, servername=None, servpass=None, image=None, number=None, serverid=None, cpu=None, ram=None, disk=None, token=None, insert=None, reboot=None, remove=None, rebuild=None): if insert: global templ, image_id, i if dc in range(1, 6, 1): token = CloudInterface(dc=dc) if None in servertype: templ = token.find_template(hv=4) elif "lowcost" in servertype: templ = token.find_template(hv=3) elif "vmware" in servertype: templ = token.find_template(hv=2) elif "hyperv" in servertype: templ = token.find_template(hv=1) for t in templ: if image in t.id_code and "True" in t.enabled: image_id = t.template_id return image_id size = {"small", 'medium', 'large', 'extra large'} for i in size: if flavor not in i: Exception("size error") else: return i token.login(username=username, password=password, load=True) if servertype is None: if number is None: c = SmartVmCreator(name=servername, admin_password=servpass, template_id=image_id, auth_obj=token.auth) c.set_type(size=i) c.commit(url=token.wcf_baseurl, debug=True) else: a = 0 while a < int(number): a += 1 c = SmartVmCreator(name=servername + a, admin_password=servpass, template_id=image_id, auth_obj=token.auth) c.set_type(size=i) c.commit(url=token.wcf_baseurl, debug=True) else: if number is None: ip = token.purchase_ip() pvm = ProVmCreator(name=servername, admin_password=servpass, template_id=image_id, auth_obj=token.auth) pvm.set_cpu_qty(int(cpu)) pvm.set_ram_qty(int(ram)) pvm.add_virtual_disk(int(disk)) pvm.add_public_ip(public_ip_address_resource_id=ip.resid, primary_ip_address=True) pvm.commit(url=token.wcf_baseurl, debug=True) time.sleep(60) else: a = 0 while a < int(number): a += 1 ip = token.purchase_ip() pvm = ProVmCreator(name=servername + a, admin_password=servpass, template_id=image_id, auth_obj=token.auth) pvm.set_cpu_qty(int(cpu)) pvm.set_ram_qty(int(ram)) pvm.add_virtual_disk(int(disk)) pvm.add_public_ip(public_ip_address_resource_id=ip.resid, primary_ip_address=True) pvm.commit(url=token.wcf_baseurl, debug=True) time.sleep(60) elif reboot: token = CloudInterface(dc=dc) token.login(username=username, password=password, load=True) token.poweroff_server(server_id=serverid) time.sleep(60) token.poweron_server(server_id=serverid) elif remove: token = CloudInterface(dc=dc) token.login(username=username, password=password, load=True) token.poweroff_server(server_id=serverid) time.sleep(60) token.delete_vm(server_id=serverid) elif rebuild: token = CloudInterface(dc=dc) token.login(username=username, password=password, load=True) for vm in token.get_vm(pattern=serverid): vm.poweroff() time.sleep(60) vm.reinitialize(admin_password=servpass)