Exemplo n.º 1
0
 def _cleanup(self):
     """Unregister test HD and VM if they are registered."""
     # Do machine first to detach any HDs
     try:
         machine = VirtualMachine.find(self.testVMname)
     except:
         pass
     else:
         machine.eject()
     try:
         harddisk = HardDisk.find(self.testHDpath)
     except:
         pass
     else:
         harddisk.close()
     try:
         machine = VirtualMachine.find(self.cloneVMname)
     except Exception as e:
         pass
     else:
         machine.eject()
         machine.delete()
     try:
         clonedisk = HardDisk.find(self.cloneHDpath)
     except:
         pass
     else:
         clonedisk.close()
     try:
         os.remove(self.cloneHDpath)
     except:
         pass
Exemplo n.º 2
0
 def _cleanup(self):
     """Unregister test HD and VM if they are registered."""
     # Do machine first to detach any HDs
     try:
         machine = VirtualMachine.find(self.testVMname)
     except:
         pass
     else:
         machine.eject()
     try:
         harddisk = HardDisk.find(self.testHDpath)
     except:
         pass
     else:
         harddisk.close()
     try:
         machine = VirtualMachine.find(self.cloneVMname)
     except Exception as e:
         pass
     else:
         machine.eject()
         machine.delete()
     try:
         clonedisk = HardDisk.find(self.cloneHDpath)
     except:
         pass
     else:
         clonedisk.close()
     try:
         os.remove(self.cloneHDpath)
     except:
         pass
Exemplo n.º 3
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument")
     vm = VirtualMachine.find(args.pop(0))
     vm.resume()
     return 0
Exemplo n.º 4
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing virtual machine argument")
     vm = VirtualMachine.find(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing target directory argument")
     targetDir = args.pop(0)
     verboseMsg("Backing up %s to %s" % (vm, targetDir))
     if vm.isRunning():
         verboseMsg("Pausing VM...")
         # Must wait until paused or will have race condition for lock
         # on disks.
         vm.pause(wait=True)
         atexit.register(vm.resume)
     # Todo: Backup settings file in some way.
     # Todo: Want to back up devices than hard drives?
     disks = vm.getHardDrives()
     for disk in disks:
         targetFilename = os.path.join(targetDir, disk.basename())
         # Todo: Need to resolve file already existing here.
         verboseMsg("Backing up disk %s to %s (%d bytes)" %
                    (disk, targetFilename, disk.size))
         progress = disk.clone(targetFilename, wait=False)
         show_progress(progress)
         # Remove newly created clone from registry
         clone = HardDisk.find(targetFilename)
         clone.close()
Exemplo n.º 5
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing source VM name argument")
     srcVM = VirtualMachine.find(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing target VM name argument")
     targetName = args.pop(0)
     message("Cloning %s to %s" % (srcVM, targetName))
     cloneVM = srcVM.clone(targetName)
     # Now clone and attach disks
     disks = srcVM.getHardDrives()
     for disk in disks:
         # Generate new HD filename by prefixing new VM name.
         # Not the greatest, but not sure what the best way is.
         targetFilename = os.path.join(disk.dirname(),
                                       "%s-%s" % (targetName, disk.name))
         # Todo: Need to resolve file already existing here.
         message("Cloning disk %s to %s (%d bytes)" %
                 (disk, os.path.basename(targetFilename), disk.size))
         progress = disk.clone(targetFilename, wait=False)
         show_progress(progress)
         cloneHD = HardDisk.find(targetFilename)
         message("Attaching %s to %s" % (cloneHD, cloneVM))
         cloneVM.attachMedium(cloneHD)
     return 0
Exemplo n.º 6
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     mode = "gui"
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument")
     vm = VirtualMachine.find(args.pop(0))
     vm.powerOn(type=mode)
Exemplo n.º 7
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing source VM name argument")
     srcVM = VirtualMachine.find(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing target VM name argument")
     targetName = args.pop(0)
     message("Cloning %s to %s" % (srcVM, targetName))
     cloneVM = srcVM.clone(targetName)
     # Now clone and attach disks
     disks = srcVM.getHardDrives()
     for disk in disks:
         # Generate new HD filename by prefixing new VM name.
         # Not the greatest, but not sure what the best way is.
         targetFilename = os.path.join(disk.dirname(),
                                       "%s-%s" % (targetName, disk.name))
         # Todo: Need to resolve file already existing here.
         message("Cloning disk %s to %s (%d bytes)"
                 % (disk,
                    os.path.basename(targetFilename),
                    disk.size))
         progress = disk.clone(targetFilename, wait=False)
         show_progress(progress)
         cloneHD = HardDisk.find(targetFilename)
         message("Attaching %s to %s" % (cloneHD, cloneVM))
         cloneVM.attachMedium(cloneHD)
     return 0
Exemplo n.º 8
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     mode = "gui"
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument");
     vm = VirtualMachine.find(args.pop(0))
     vm.powerOn(type=mode)
Exemplo n.º 9
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing virtual machine argument")
     vm = VirtualMachine.find(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing target directory argument")
     targetDir = args.pop(0)
     verboseMsg("Backing up %s to %s" % (vm, targetDir))
     if vm.isRunning():
         verboseMsg("Pausing VM...")
         # Must wait until paused or will have race condition for lock
         # on disks.
         vm.pause(wait=True)
         atexit.register(vm.resume)
     # Todo: Backup settings file in some way.
     # Todo: Want to back up devices than hard drives?
     disks = vm.getHardDrives()
     for disk in disks:
         targetFilename = os.path.join(targetDir, disk.basename())
         # Todo: Need to resolve file already existing here.
         verboseMsg("Backing up disk %s to %s (%d bytes)" % (disk,
                                                             targetFilename,
                                                             disk.size))
         progress = disk.clone(targetFilename, wait=False)
         show_progress(progress)
         # Remove newly created clone from registry
         clone = HardDisk.find(targetFilename)
         clone.close()
Exemplo n.º 10
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing VM name")
     vm = VirtualMachine.find(args.pop(0))
     snapshot = vm.getCurrentSnapshot()
     progress = vm.deleteSnapshot(snapshot, wait=False)
     show_progress(progress)
Exemplo n.º 11
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing VM name")
     vm = VirtualMachine.find(args.pop(0))
     snapshot = vm.getCurrentSnapshot()
     progress = vm.deleteSnapshot(snapshot, wait=False)
     show_progress(progress)
Exemplo n.º 12
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument")
     for name in args:
         vm = VirtualMachine.find(name)
         verboseMsg("Ejecting %s" % vm)
         vm.eject()
     return 0
Exemplo n.º 13
0
 def testRegister(self):
     """Test VirtualMachine.register() and related functions"""
     machine = VirtualMachine.open(self.testVMpath)
     machine.register()
     self.assertEqual(True, machine.isRegistered())
     m2 = VirtualMachine.find(machine.name)
     self.assertEqual(machine.id, m2.id)
     machine.unregister()
     self.assertEqual(False, machine.isRegistered())
Exemplo n.º 14
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument");
     for name in args:
         vm = VirtualMachine.find(name)
         verboseMsg("Ejecting %s" % vm)
         vm.eject()
     return 0
Exemplo n.º 15
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing virtual machine argument")
     vm = VirtualMachine.find(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing hard disk filenames")
     for hd in args:
         hd = cls.harddisk(hd)
         verboseMsg("Attaching %s" % hd)
         vm.attachMedium(hd)
     return 0
Exemplo n.º 16
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument");
     for filename in args:
         vm = VirtualMachine.find(args.pop(0))
         if vm.isRegistered():
             verboseMsg("Unregistering VM %s" % vm)
             vm.unregister()
         else:
             errorMsg("VM \"%s\" is not registered." % vm)
     return 0
Exemplo n.º 17
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing VM name")
     vm = VirtualMachine.find(args.pop(0))
     if len(args) == 0:
         raise Exception("Missing snapshot name")
     name = args.pop(0)
     description = None
     if len(args) > 0:
         description = args.pop(0)
     vm.takeSnapshot(name, description)
Exemplo n.º 18
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing VM name")
     vm = VirtualMachine.find(args.pop(0))
     if len(args) == 0:
         raise Exception("Missing snapshot name")
     name = args.pop(0)
     description = None
     if len(args) > 0:
         description = args.pop(0)
     vm.takeSnapshot(name, description)
Exemplo n.º 19
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing virtual machine argument")
     vm = VirtualMachine.find(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing hard disk filenames")
     for hd in args:
         hd = cls.harddisk(hd)
         verboseMsg("Attaching %s" % hd)
         vm.attachMedium(hd)
     return 0
Exemplo n.º 20
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) == 0:
         raise Exception("Missing virtual machine name argument")
     for filename in args:
         vm = VirtualMachine.find(args.pop(0))
         if vm.isRegistered():
             verboseMsg("Unregistering VM %s" % vm)
             vm.unregister()
         else:
             errorMsg("VM \"%s\" is not registered." % vm)
     return 0
Exemplo n.º 21
0
 def invoke(cls, args):
     if len(args) == 0:
         vms = VirtualMachine.getAll()
         verboseMsg("Registered VMs:")
         for vm in vms:
             try:
                 print "\t%s" % vm
             except Exception as e:
                 errorMsg("Could not display VM: %s" % str(e))
     else:
         for vmName in args:
             try:
                 vm = VirtualMachine.find(vmName)
                 print_vm(vm)
             except Exception as e:
                 errorMsg("Could not display information about VM \"%s\": %s" % (vmName, str(e)))
Exemplo n.º 22
0
 def invoke(cls, args):
     if len(args) == 0:
         vms = VirtualMachine.getAll()
         verboseMsg("Registered VMs:")
         for vm in vms:
             try:
                 print "\t%s" % vm
             except Exception as e:
                 errorMsg("Could not display VM: %s" % str(e))
     else:
         for vmName in args:
             try:
                 vm = VirtualMachine.find(vmName)
                 print_vm(vm)
             except Exception as e:
                 errorMsg(
                     "Could not display information about VM \"%s\": %s" %
                     (vmName, str(e)))