def create_vm(vms, source_vapp_resource, vm_cfg, master_name, master=False): try: global lastMasterIp vm = vapp.get_vm(vm_cfg['name']) vm_obj = VM(client, resource=vm) ip = vm_obj.list_nics()[0]['ip_address'].ljust(20) if master: #gen_key(vm_cfg['name'],os.environ['SSH_USERNAME'],os.environ['SSH_PASSWORD'], ip) lastMasterIp = ip if vm_obj.is_powered_on() == True: print(" VM '{0}' ... OK".format(vm_cfg['name'])) else: print(" VM '{0}' ... DOWN -> starting ...\r".format( vm_cfg['name']), end='') result = vm_obj.power_on() handle_task(client, result) print(" VM '{0}' ... UP {1}".format(vm_cfg['name'], "".ljust(20))) except EntityNotFoundException: print(" VM '{0}' ... NONE -> marked for creation ...".format( vm_cfg['name'])) spec = { 'source_vm_name': vm_cfg['source_vm_name'], 'vapp': source_vapp_resource } spec['target_vm_name'] = vm_cfg['name'] spec['hostname'] = vm_cfg['hostname'] vms.append(spec)
try: vdc_resource = org.get_vdc(cfg.vdc['vdc_name']) vdc = VDC(client, resource=vdc_resource) print("VDC already exists: {0}".format(vdc.name)) except Exception: print("VDC does not exist, creating: {0}".format(cfg.vdc['vdc_name'])) vdc_kwargs = cfg.vdc # Vet the netpool and pvcd arguments as either can be '*' in which # case we need to find default values. _fill_in_pvdc_default(client, vdc_kwargs) _fill_in_netpool_default(client, vdc_kwargs) # Now create the VDC. admin_vdc_resource = org.create_org_vdc(**vdc_kwargs) # The 'admin_vdc_resource' is not a task but an AdminVdc entity. Tasks # are two levels down. handle_task(client, admin_vdc_resource.Tasks.Task[0]) org.reload() vdc_resource = org.get_vdc(cfg.vdc['vdc_name']) vdc = VDC(client, resource=vdc_resource) print("VDC now exists: {0}".format(vdc.name)) # Ensure the catalog exists. For now we don't do anything special with # permissions. As with VDC's we reload the org if we create a catalog # so that it's visible in future calls. try: catalog_resource = org.get_catalog_resource(cfg.catalog['name']) print("Catalog already exists: {0}".format(cfg.catalog['name'])) except Exception: print("Catalog does not exist, creating: {0}".format(cfg.catalog['name'])) catalog_kwargs = cfg.catalog catalog_resource = org.create_catalog(**catalog_kwargs)
client = Client(cfg.vcd_host, verify_ssl_certs=False, log_file='pyvcloud.log', log_requests=True, log_headers=True, log_bodies=True) client.set_highest_supported_version() client.set_credentials( BasicLoginCredentials(cfg.vcd_admin_user, "System", cfg.vcd_admin_password)) # Load the org. If it does not exist, there's nothing to do. print("Fetching Org...") try: org_record = client.get_org_by_name(cfg.org) except Exception: print("Org does not exist, nothing to be done") sys.exit(0) # Delete the org. print("Org exists, deleting: {0}".format(cfg.org)) sys_admin_resource = client.get_admin() system = System(client, admin_resource=sys_admin_resource) resource = system.delete_org(cfg.org, True, True) handle_task(client, resource) print("Deleted the org...") # Log out. print("All done!") client.logout()
# Check for vApps and create them if they don't exist. We have to # reload the VDC object so it has all the links to current vApps. vdc.reload() try: vapp = vdc.get_vapp(cfg.vapp['name']) print("vApp exists: {0}".format(cfg.vapp['name'])) except Exception: print("vApp does not exist: name={0}".format(cfg.vapp['name'])) vapp = vdc.create_vapp(name=cfg.vapp['name'], description=cfg.vapp['description'], network=cfg.vapp['network'], accept_all_eulas=cfg.vapp['accept_all_eulas']) print(" vApp '{0}' ... instanciated ...\r".format(cfg.vapp['name']), end='') handle_task(client, vapp.Tasks.Task[0]) print(" vApp '{0}' ... OK {1}".format(cfg.vapp['name'], "".ljust(20))) # We don't track the task as instantiating a vApp takes a while. # Uncomment below if you want to ensure the vApps are available. # handle_task(client, vapp_resource.Tasks.Task[0]) vdc.reload() vapp_resource = vdc.get_vapp(cfg.vapp['name']) vapp = VApp(client, resource=vapp_resource) vms = [] for vm_cfg in cfg.vapp['vms']: print(vm_cfg) source_vapp_resource = client.get_resource(catalog_item.Entity.get('href')) create_master_vm(vms, source_vapp_resource, vm_cfg)
vapp = VApp(client, resource=vapp_resource) vms = [] for vm_cfg in cfg.vapp['vms']: try: vm = vapp.get_vm(vm_cfg['name']) vm_obj = VM(client, resource=vm) if vm_obj.is_powered_on() == True: print(" VM '{0}' ... OK".format(vm_cfg['name'])) else: print(" VM '{0}' ... DOWN".format(vm_cfg['name']), end='') except EntityNotFoundException: print(" VM '{0}' ... NONE ".format(vm_cfg['name'])) result = vapp.add_vms(vms) handle_task(client, result) print("Statuses ...") while (vapp.get_all_vms() is None or len(vapp.get_all_vms()) < len(cfg.vapp['vms'])): print(" VMs ... {0}\r".format("waiting full start ...".ljust(20)), end='') vapp.reload() time.sleep(5) for vm in vapp.get_all_vms(): vm_obj = VM(client, resource=vm) while (vm_obj.is_powered_on() == False): print(" VM '{0}' ... {1}\r".format(vm.get('name'), "DOWN (waiting)".ljust(20)), end='') vm_obj.reload()
vdc_resource = org.get_vdc(cfg.org.vdc_name) vdc = VDC(client, resource=vdc_resource) print("VDC already exists: {0}".format(cfg.org.vdc_name)) except Exception: print("VDC {0} does not exist, exiting".format(cfg.org['vdc_name'])) sys.exit() # Ensure Direct Org Network doesn't exist. print("Fetching Network...") try: network_resource = vdc.get_direct_orgvdc_network( cfg.org.org_direct_nw.name) print("Network already exists: {0}".format(cfg.org.org_direct_nw.name)) except Exception: print("Network {0} does not exist, creating".format( cfg.org.org_direct_nw.name)) network_res = vdc.create_directly_connected_vdc_network( network_name=cfg.org.org_direct_nw.name, parent_network_name=cfg.org.org_direct_nw.parent_network, is_shared=cfg.org.org_direct_nw.is_shared) handle_task(client, network_res.Tasks.Task[0]) # Final report ------------------------------------------------------------------------------------ new_direct_network = vdc.get_direct_orgvdc_network(cfg.org.org_direct_nw.name) print( '\nNew Direct network is created -------------------------------------------------' ) print('name: {}'.format(new_direct_network.attrib['name'])) print('href: {}'.format(new_direct_network.attrib['href']))