def vsphere_check_with_api(api, run_time, text): """Uses api to perform checking of vms on vsphere-type provider. Args: api: api endpoint to vsphere run_time: when this run time is exceeded for the VM, it will be deleted text: when this string is found in the name of VM, it may be deleted """ vms = VirtualMachine.all(api.api) vms_to_delete = [] templates_to_delete = [] nightly_templates = [VirtualMachine.get(api.api, name=x) for x in api.list_template() if "miq-nightly" in x] nightly_templates.sort(key=lambda x: datetime.datetime.strptime(x.name[-12:], "%Y%m%d%H%M")) if len(nightly_templates) > MRU_NIGHTLIES: for template in nightly_templates[:-MRU_NIGHTLIES]: templates_to_delete.append(template.name) for vm in vms: vm_name = vm.name running_for = vm.summary.quickStats.uptimeSeconds / SEC_IN_DAY if running_for >= run_time and is_affected(vm_name, text): print("To delete: {} with runtime: {}".format(vm_name, running_for)) vms_to_delete.append(vm_name) return (vms_to_delete, templates_to_delete)
def vsphere_check_with_api(api, run_time, text): """Uses api to perform checking of vms on vsphere-type provider. Args: api: api endpoint to vsphere run_time: when this run time is exceeded for the VM, it will be deleted text: when this string is found in the name of VM, it may be deleted """ vms = VirtualMachine.all(api.api) vms_to_delete = [] templates_to_delete = [] nightly_templates = [ VirtualMachine.get(api.api, name=x) for x in api.list_template() if "miq-nightly" in x ] nightly_templates.sort( key=lambda x: datetime.datetime.strptime(x.name[-12:], "%Y%m%d%H%M")) if len(nightly_templates) > MRU_NIGHTLIES: for template in nightly_templates[:-MRU_NIGHTLIES]: templates_to_delete.append(template.name) for vm in vms: vm_name = vm.name running_for = vm.summary.quickStats.uptimeSeconds / SEC_IN_DAY if running_for >= run_time and is_affected(vm_name, text): print("To delete: {} with runtime: {}".format( vm_name, running_for)) vms_to_delete.append(vm_name) return (vms_to_delete, templates_to_delete)
def exportVM(serverIp, user, passwd, vmName, workingDir): try: print "Connecting to the server...." client = Client(serverIp, user, passwd) except WebFault: print "Can't connect to the server" sys.exit(1) print "Connected" validVms = {} if vmName <> 'all': try: vm = VirtualMachine.get(client, name=vmName) if vm.runtime.powerState <> 'poweredOff': print 'Skipping VM:' + vm.name + ' VM is not powered off' sys.exit(5) if len(vm.network) <> 1: print 'Skipping VM:' + vm.name + ' The number of network devices is not equal to 1' sys.exit(5) vmdkPath = getVMDKUri(serverIp, vm) if vmdkPath != None: validVms[vm.name] = vmdkPath except ObjectNotFoundError: print 'Invalid VM name' client.logout() sys.exit(2) else: # inspect all vms vms = VirtualMachine.all(client) for vm in vms: if vm.runtime.powerState <> 'poweredOff': print 'Skipping VM:' + vm.name + ' VM is not powered off' continue if len(vm.network) <> 1: print 'Skipping VM:' + vm.name + ' The number of network devices is not equal to 1' continue vmdkPath = getVMDKUri(serverIp, vm) if vmdkPath != None: validVms[vm.name] = vmdkPath else: continue client.logout() if len(validVms.keys()) == 0: print 'Nothing to export' sys.exit(2) # get vmdks for all valid vms for vmName in validVms.keys(): directory = workingDir + '/' + vmName + '/' if not os.path.exists(directory): os.makedirs(directory) VmdkUri = validVms[vmName] downloadFile(VmdkUri, user, passwd, directory + vmName + '.vmdk') extends = parseVMDK(directory + vmName + '.vmdk') if extends == None: print 'No accessable extends' sys.exit(3) else: available = getAvalableDiskSpaceBytes(workingDir) for s in extends.values(): available = available - s if available < 0: print 'There is not enough free disk space to download all extends for VM:' + vmName exit(4) for e in extends.keys(): m = re.match('^(.+?)/folder/(.+?)/(.+?)\?(.+)$', VmdkUri) uri = m.group(1) + '/folder/' + m.group(2) + '/' + urllib2.quote(e) + '?' + m.group(4) downloadFile(uri, user, passwd, directory + e) sys.exit(0)