def main(): supportedArgs = [(["h:", "host="], "localhost", "Host name", "host"), (["u:", "user="******"root", "User name", "user"), (["p:", "pwd="], "ca$hc0w", "Password", "pwd"), (["v:", "vmname="], "", "VM prefix (deletes everything by default)", "vmname")] supportedToggles = [(["usage", "help"], False, "Show usage information", "usage")] args = arguments.Arguments(sys.argv, supportedArgs, supportedToggles) if args.GetKeyValue("usage") == True: args.Usage() sys.exit(0) # Connect si = Connect(host=args.GetKeyValue("host"), user=args.GetKeyValue("user"), pwd=args.GetKeyValue("pwd")) # Process command line vmname = args.GetKeyValue("vmname") vms = folder.FindPrefix(vmname) if len(vms) != 0: for vm in vms: try: vm.Destroy() except Exception, e: raise
def CleanupVm(vmname, useLlpm=False): with LogSelfOp() as logOp: if isinstance(vmname, vim.VirtualMachine): Log("Cleaning up VMs with name %s" % vmname.name) oldVms = [vmname] else: Log("Cleaning up VMs with name %s" % vmname) oldVms = folder.FindPrefix(vmname) for oldVm in oldVms: if oldVm.GetRuntime().GetPowerState() == PowerState.poweredOn: vm.PowerOff(oldVm) ftInfo = oldVm.config.ftInfo if ftInfo and ftInfo.role == 1: # If the VM is a primary, unregister all secondaries # before deleting the VM. ftMgr = host.GetFaultToleranceMgr(connect.GetSi()) WaitForTask(ftMgr.UnregisterSecondary(oldVm, None)) Log("Destroying VM") if useLlpm == True: vmConfig = oldVm.GetConfig() hw = vmConfig.GetHardware() if vmConfig.flags.faultToleranceType and \ vmConfig.flags.faultToleranceType == "recordReplay": hw.SetDevice([]) vmConfig.SetHardware(hw) llpm = invt.GetLLPM() llpm.DeleteVm(vmConfig) else: vm.Destroy(oldVm)
def CleanupVm(self, vmname): oldVms = folder.FindPrefix(vmname) for oldVm in oldVms: if oldVm.GetRuntime().GetPowerState() == \ Vim.VirtualMachine.PowerState.poweredOn: vm.PowerOff(oldVm) vm.Destroy(oldVm)
def main(): """ Simple command-line program for creating virtual machines on a system managed by hostd. """ options = GetOptions() Connect(host=options.server, user=options.user, pwd=options.password) dsList = {} vms = folder.FindPrefix(options.vm_name) print "" print "---------------------------- " print "Virtual machine information: " print "---------------------------- " for vm1 in vms: print "" vm1.RefreshStorageInfo() name = vm1.GetName() layoutEx = vm1.GetLayoutEx() storageInfo = vm1.GetStorage() storageSummary = vm1.GetSummary().GetStorage() print "Virtual machine name: " + name print "Layout: (retrieved at %s)" % (str(layoutEx.GetTimestamp())) files = layoutEx.GetFile() print "%-40s %-20s %20s" \ % ("File name", "File type", "File size") print "%-40s %-20s %20s" \ % ("---------", "---------", "---------") for file in files: size = getNiceSize(file.size) print "%-40s %-20s %20s" \ % (file.name, file.type, size) print "" print "Summary: (retrieved at %s)" % str(storageSummary.GetTimestamp()) print "Committed: %s\tUncommitted: %s\tUnshared: %s" \ % (getNiceSize(storageSummary.committed), \ getNiceSize(storageSummary.uncommitted), \ getNiceSize(storageSummary.unshared)) print "" print "Storage information: (retrieved at %s)" \ % str(storageInfo.GetTimestamp()) usages = storageInfo.GetPerDatastoreUsage() print "%-30s %25s %25s %20s" % ("Datastore name", "Committed", \ "Uncommitted", "Unshared") print "%-30s %25s %25s %20s" % ("--------------", "---------", \ "-----------", "--------") for dsUsage in usages: print "%-30s %25s %25s %20s" \ % (dsUsage.datastore.summary.name, getNiceSize(dsUsage.committed), getNiceSize(dsUsage.uncommitted), getNiceSize(dsUsage.unshared)) dsList[dsUsage.datastore.summary.name] = dsUsage.datastore print ""
def Cleanup(vmName, sessions, doCleanup): for hostName, si in sessions.iteritems(): SetSi(si) oldVms = folder.FindPrefix(vmName) if oldVms: if doCleanup: Log("Cleaning up old vms with name: " + vmName) for oldVm in oldVms: if oldVm.GetRuntime().GetPowerState( ) == Vim.VirtualMachine.PowerState.poweredOn: vm.PowerOff(oldVm) oldVm.Destroy()
def main(): """ Simple command-line program for listing the virtual machines on a system managed by hostd. """ options = GetOptions() Connect(host=options.host, user=options.user, pwd=options.password) for vm in folder.FindPrefix(options.vmprefix): PrintVmInfo(vm)
def Cleanup(vmName, doCleanup): if options.vcHost: return oldVms = folder.FindPrefix(vmName) if oldVms: if doCleanup: Log("Cleaning up old vms with name: " + vmName) for oldVm in oldVms: if oldVm.GetRuntime().GetPowerState( ) == Vim.VirtualMachine.PowerState.poweredOn: vm.PowerOff(oldVm) oldVm.Destroy() else: Log("Please cleanup unit test vms from previous runs") sys.exit(-1)
def main(): """ Simple command-line program for listing the virtual machines on a system managed by hostd. """ options = GetOptions() si = Connect(host=options.host, user=options.user, pwd=options.password, port=int(options.port), namespace="vim25/5.5") atexit.register(Disconnect, si) for vm in folder.FindPrefix(options.vmprefix): PrintVmInfo(vm)
def CleanupVm(vmname, useLlpm=False): if isinstance(vmname, vim.VirtualMachine): Log("Cleaning up VMs with name %s" % vmname.name) oldVms = [vmname] else: Log("Cleaning up VMs with name %s" % vmname) oldVms = folder.FindPrefix(vmname) for oldVm in oldVms: if oldVm.GetRuntime().GetPowerState() == PowerState.poweredOn: vm.PowerOff(oldVm) Log("Destroying VM") if useLlpm == True: vmConfig = oldVm.GetConfig() llpm = invt.GetLLPM() llpm.DeleteVm(vmConfig) else: vm.Destroy(oldVm)
def CleanupVm(vmname, useLlpm = False): Log("Cleaning up VMs with name " + vmname) oldVms = folder.FindPrefix(vmname) for oldVm in oldVms: if oldVm.GetRuntime().GetPowerState() == \ Vim.VirtualMachine.PowerState.poweredOn: vm.PowerOff(oldVm) Log("Destroying VM") if useLlpm == True: vmConfig = oldVm.GetConfig() hw = vmConfig.GetHardware() hw.SetDevice([]) vmConfig.SetHardware(hw) llpm = invt.GetLLPM() llpm.DeleteVm(vmConfig) else: vm.Destroy(oldVm)
def main(): supportedArgs = [ (["h:", "host="], "localhost", "Host name", "host"), (["u:", "user="******"root", "User name", "user"), (["p:", "pwd="], "ca$hc0w", "Password", "pwd"), (["v:", "vmname="], "", "Name of the virtual machine", "vmname"), (["d:", "snapshotdesc="], "Quick Dummy", "Snapshot description", "snapshotdesc"), (["s:", "snapshotname="], "TestSnapshot", "Name of the snapshot", "snapshotname")] supportedToggles = [ (["usage", "help"], False, "Show usage information", "usage"), (["dontDelete", "d"], False, "Dont delete created snapshot", "del")] args = arguments.Arguments(sys.argv, supportedArgs, supportedToggles) if args.GetKeyValue("usage") == True: args.Usage() sys.exit(0) # Connect si = Connect(host=args.GetKeyValue("host"), user=args.GetKeyValue("user"), pwd=args.GetKeyValue("pwd")) # Process command line vmname = args.GetKeyValue("vmname") snapshotname = args.GetKeyValue("snapshotname") snapshotdesc = args.GetKeyValue("snapshotdesc") doDelete = not args.GetKeyValue("del") if len(vmname) == 0 or len(snapshotname) == 0: args.Usage() sys.exit(0) aVm = folder.FindPrefix(vmname)[0] task = aVm.CreateSnapshot(snapshotname, snapshotdesc, memory=False, quiesce=False) snap = aVm.GetSnapshot().currentSnapshot print "Snapshot: %s" % repr(snap) if doDelete: print "Waiting for snapshot to be created: %s ..." % repr(snap) WaitForTask(task) print "Snapshot created. Removing ..." vm.RemoveSnapshot(snap)
def CleanupVm(vmname): si = connect.GetSi() Log("Cleaning up VMs with name " + vmname) oldVms = folder.FindPrefix(vmname) for oldVm in oldVms: try: if oldVm is None or oldVm.config is None or oldVm.config.ftInfo is None: continue if oldVm.config.ftInfo.role != 1: continue if oldVm.GetRuntime().GetPowerState() == \ vim.VirtualMachine.PowerState.poweredOn: vm.PowerOff(oldVm) ftInfo = oldVm.config.ftInfo Log("Destroying VM") vmConfig = oldVm.GetConfig() hw = vmConfig.GetHardware() hw.SetDevice([]) vmConfig.SetHardware(hw) vm.Destroy(oldVm) except vmodl.fault.ManagedObjectNotFound as e: pass
def CurrentVmCount(vmName, sessions): vmCount = [] for hostName, si in sessions.iteritems(): SetSi(si) vmCount.append(len(folder.FindPrefix(vmName))) return vmCount