Ejemplo 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
Ejemplo 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
Ejemplo n.º 3
0
 def testClone(self):
     """Test Medium.clone()"""
     harddisk = HardDisk.open(self.testHDpath)
     harddisk.clone(self.cloneHDpath)
     clonedisk = HardDisk.find(self.cloneHDpath)
     self.assertEqual(harddisk.format, harddisk.format)
     self.assertEqual(harddisk.logicalSize, harddisk.logicalSize)
     self.assertNotEqual(harddisk.id, clonedisk.id)
Ejemplo n.º 4
0
 def testClone(self):
     """Test Medium.clone()"""
     harddisk = HardDisk.open(self.testHDpath)
     harddisk.clone(self.cloneHDpath)
     clonedisk = HardDisk.find(self.cloneHDpath)
     self.assertEqual(harddisk.format, harddisk.format)
     self.assertEqual(harddisk.logicalSize, harddisk.logicalSize)
     self.assertNotEqual(harddisk.id, clonedisk.id)
Ejemplo n.º 5
0
 def testFind(self):
     """Test HardDisk.find()"""
     self.assertEqual(False, HardDisk.isRegistered(self.testHDpath))
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(True, HardDisk.isRegistered(self.testHDpath))
     hd = HardDisk.find(self.testHDpath)
     self.assertEqual(harddisk.id, hd.id)
     harddisk.close()
     self.assertEqual(False, HardDisk.isRegistered(self.testHDpath))
Ejemplo n.º 6
0
 def testFind(self):
     """Test HardDisk.find()"""
     self.assertEqual(False, HardDisk.isRegistered(self.testHDpath))
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(True, HardDisk.isRegistered(self.testHDpath))
     hd = HardDisk.find(self.testHDpath)
     self.assertEqual(harddisk.id, hd.id)
     harddisk.close()
     self.assertEqual(False, HardDisk.isRegistered(self.testHDpath))
Ejemplo n.º 7
0
    def harddisk(cls, string):
        """Load a harddisk described by string.

        Will open disk if needed."""
        # TODO: Should also support string being UUID
        if HardDisk.isRegistered(string):
            hd = HardDisk.find(string)
        else:
            hd = HardDisk.open(string)
        return hd
Ejemplo n.º 8
0
    def harddisk(cls, string):
        """Load a harddisk described by string.

        Will open disk if needed."""
        # TODO: Should also support string being UUID
        if HardDisk.isRegistered(string):
            hd = HardDisk.find(string)
        else:
            hd = HardDisk.open(string)
        return hd
Ejemplo n.º 9
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing size argument")
     size = cls.string_to_size(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing path argument")
     path = args.pop(0)
     verboseMsg("Creating Hard Disk at %s with size %d" % (path, size))
     HardDisk.createWithStorage(path, size)
     return 0
Ejemplo n.º 10
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     if len(args) < 1:
         raise Exception("Missing size argument")
     size = cls.string_to_size(args.pop(0))
     if len(args) < 1:
         raise Exception("Missing path argument")
     path = args.pop(0)
     verboseMsg("Creating Hard Disk at %s with size %d" % (path, size))
     HardDisk.createWithStorage(path, size)
     return 0
Ejemplo n.º 11
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()
Ejemplo n.º 12
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
Ejemplo n.º 13
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()
Ejemplo n.º 14
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
Ejemplo n.º 15
0
 def testOpen(self):
     """Test HardDisk.open()"""
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(os.path.abspath(self.testHDpath), harddisk.location)
     self.assertEqual(os.path.basename(self.testHDpath),
                      harddisk.basename())
     harddisk.close()
Ejemplo n.º 16
0
 def testEject(self):
     """Test VirtualMachine.eject()"""
     machine = VirtualMachine.open(self.testVMpath)
     machine.register()
     self.assertEqual(True, machine.isRegistered())
     harddisk = HardDisk.open(self.testHDpath)
     machine.attachMedium(harddisk)
     machine.eject()
     self.assertEqual(False, machine.isRegistered())
     harddisk.close()
Ejemplo n.º 17
0
 def testMedium(self):
     """Test Medium basics"""
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(os.path.abspath(self.testHDpath), harddisk.location)
     self.assertEqual(os.path.basename(self.testHDpath), harddisk.basename())
     self.assertEqual(os.path.dirname(os.path.abspath(self.testHDpath)),
                      harddisk.dirname())
     self.assertEqual(os.path.basename(self.testHDpath), str(harddisk))
     self.assertNotEqual(None, harddisk.id)
     self.assertNotEqual(None, harddisk.getIMedium())
     self.assertNotEqual(None, harddisk.name)
     harddisk.close()
Ejemplo n.º 18
0
 def testAttachMedium(self):
     """Test VirtualMachine.attachMedium() and related functions"""
     machine = VirtualMachine.open(self.testVMpath)
     machine.register()
     harddisk = HardDisk.open(self.testHDpath)
     machine.attachMedium(harddisk)
     mediums = machine.getAttachedMediums()
     self.assertEqual(1, len(mediums))
     self.assertEqual(mediums[0].deviceType, HardDisk)
     machine.detachMedium(harddisk)
     machine.unregister()
     harddisk.close()
Ejemplo n.º 19
0
 def testMedium(self):
     """Test Medium basics"""
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(os.path.abspath(self.testHDpath), harddisk.location)
     self.assertEqual(os.path.basename(self.testHDpath),
                      harddisk.basename())
     self.assertEqual(os.path.dirname(os.path.abspath(self.testHDpath)),
                      harddisk.dirname())
     self.assertEqual(os.path.basename(self.testHDpath), str(harddisk))
     self.assertNotEqual(None, harddisk.id)
     self.assertNotEqual(None, harddisk.getIMedium())
     self.assertNotEqual(None, harddisk.name)
     harddisk.close()
Ejemplo n.º 20
0
 def testPowerOn(self):
     """Test powering on a VM."""
     machine = VirtualMachine.open(self.testVMpath)
     machine.register()
     harddisk = HardDisk.open(self.testHDpath)
     machine.attachMedium(harddisk)
     machine.powerOn(type="vrdp")
     machine.waitUntilRunning()
     sleep(5)
     machine.powerOff(wait=True)
     machine.detachMedium(harddisk)
     harddisk.close()
     machine.unregister()
Ejemplo n.º 21
0
 def testGetHardDrives(self):
     """Test VirtualMachine.getHardDrives() method"""
     machine = VirtualMachine.open(self.testVMpath)
     machine.register()
     # Attach something so it's interesting
     harddisk = HardDisk.open(self.testHDpath)
     machine.attachMedium(harddisk)
     mediums = machine.getAttachedMediums()
     self.assertEqual(1, len(mediums))
     self.assertEqual(mediums[0].deviceType, HardDisk)
     hds = machine.getHardDrives()
     self.assertNotEqual(hds, None)
     self.assertTrue(isinstance(hds, list))
     self.assertEqual(len(hds), 1)
     machine.detachMedium(harddisk)
     machine.unregister()
Ejemplo n.º 22
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     mode = "gui"  # or "vrdp"
     if len(args) < 1:
         raise Exception("Missing virtual machine filename argument")
     vm = VirtualMachine.open(args.pop(0))
     atexit.register(vm.eject)
     if not vm.isRegistered():
         vm.register()
     for hd in args:
         hd = HardDisk.find(hd)
         vm.attachMedium(hd)
     verboseMsg("Starting VM in %s mode" % mode)
     vm.powerOn(type=mode)
     # Wait until machine is running or we have a race condition
     # where it still might be down when we call waitUntilDown()
     vm.waitUntilRunning()
     verboseMsg("VM started. Waiting until power down...")
     vm.waitUntilDown()
     verboseMsg("VM powered down.")
     # Let atexit clean up
     return 0
Ejemplo n.º 23
0
 def invoke(cls, args):
     """Invoke the command. Return exit code for program."""
     mode = "gui" # or "vrdp"
     if len(args) < 1:
         raise Exception("Missing virtual machine filename argument")
     vm = VirtualMachine.open(args.pop(0))
     atexit.register(vm.eject)
     if not vm.isRegistered():
         vm.register()
     for hd in args:
         hd = HardDisk.find(hd)
         vm.attachMedium(hd)
     verboseMsg("Starting VM in %s mode" % mode)
     vm.powerOn(type=mode)
     # Wait until machine is running or we have a race condition
     # where it still might be down when we call waitUntilDown()
     vm.waitUntilRunning()
     verboseMsg("VM started. Waiting until power down...")
     vm.waitUntilDown()
     verboseMsg("VM powered down.")
     # Let atexit clean up
     return 0
Ejemplo n.º 24
0
 def testGetType(self):
     """Test Medium.deviceType"""
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(harddisk.deviceType, HardDisk)
Ejemplo n.º 25
0
 def testOpen(self):
     """Test HardDisk.open()"""
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(os.path.abspath(self.testHDpath), harddisk.location)
     self.assertEqual(os.path.basename(self.testHDpath), harddisk.basename())
     harddisk.close()
Ejemplo n.º 26
0
 def testGetType(self):
     """Test Medium.deviceType"""
     harddisk = HardDisk.open(self.testHDpath)
     self.assertEqual(harddisk.deviceType, HardDisk)