public VirtualMachine CreateVM()
        {
            if (RegisteredVirtualMachineService.GetRegisteredVMImagePaths().Contains(VM.ImagePathName))
                throw new InvalidDataException("Specified VM path already exists");
            if (!VM.ImagePathName.StartsWith(AppConfiguration.GetDatastore()) || VM.BaseImageFullPhysicalPath.StartsWith(AppConfiguration.GetDatastore()))
                throw new InvalidDataException("Invalid ImagePathName: doesn't contain datastore name, or BaseImageFullPhysicalPath does contain datastore name");
            if (VM.ImagePathName.Length < 8 || VM.BaseImageFullPhysicalPath.Length < 8 || VM.IP.Length < 7)
                throw new InvalidDataException("CreateVM required field unspecified or too short");

            //this will run async, but probably should report status and handle errors in individual steps better
            try
            {
                CopyVMFiles(VM.BaseImageFullPhysicalPath, VM.ImagePathName);
            }
            catch (Exception ex)
            {
                throw new SchedulerInfo("Error copying files, VM creation aborted.", ex);
            }

            // Allot time to finish copying the file
            System.Threading.Thread.Sleep(16 * 1000);

            RegisteredVirtualMachineService service = null;
            try
            {
                RegisteredVirtualMachineService.GetVirtualHost().Register(VM.ImagePathName);
                service = new RegisteredVirtualMachineService(VM.ImagePathName);
                // Make triple-double-dog sure that the VM is online and ready.
                // Allow VM time to power on
                service.PowerOn();
                System.Threading.Thread.Sleep(180 * 1000);

                // Allow VM time to reboot
                service.Reboot();
                System.Threading.Thread.Sleep(250 * 1000);
            }
            catch (Exception ex)
            {
                new SchedulerInfo("Error registering or first-booting new VM, will attempt to continue", ex).LogElmah();
            }
            SetIPHostname(service);

            // Allow VM time to reboot
            service.Reboot();
            System.Threading.Thread.Sleep(250 * 1000);

            if (!VM.IsAutoStarted)
                service.PowerOff();

            return VM;

            //http://vmwaretasks.codeplex.com/discussions/276715
            //"[ha-datacenter/standard] Windows Server 2003/Windows Server 2003.vmx"
            //http://communities.vmware.com/message/1688542#1688542
            //http://panoskrt.wordpress.com/2009/01/20/clone-virtual-machine-on-vmware-server-20/
            //we don't seem to have vmware-vdiskmanager
        }
 public void PowerOffTest()
 {
     VirtualMachineRepository vmr = new VirtualMachineRepository();
     VirtualMachine vm = vmr.GetAllRegisteredVirtualMachines().GetEnumerator().Current; // TODO: Initialize to an appropriate value
     RegisteredVirtualMachineService target = new RegisteredVirtualMachineService(vm); // TODO: Initialize to an appropriate value
     if (target.GetStatus() != VirtualMachine.PAUSED && target.GetStatus() != VirtualMachine.POWERINGOFF && target.GetStatus() != VirtualMachine.STOPPED)
     {
         target.PowerOff();
         Assert.IsTrue(target.GetStatus() == VirtualMachine.STOPPED || target.GetStatus() == VirtualMachine.POWERINGOFF);
     }
     else
         Assert.Inconclusive();
 }
예제 #3
0
        public static void ArchivePendingVMs()
        {
            var ls = dataDB.VirtualMachines.Where(v => v.IsPendingArchive);
            foreach (var pendingVM in ls)
            {
                new SchedulerInfo("Beginning archive of project " + pendingVM.Hostname).LogElmah();
                var service = new ArchiveVirtualMachineService();
                try
                {
                    //TODO insert archiving code
                    RegisteredVirtualMachineService rvms = new RegisteredVirtualMachineService(pendingVM);
                    rvms.PowerOff();
                    string vmFilename = RegisteredVirtualMachineService.ConvertPathToPhysical(pendingVM.ImagePathName);
                    vmFilename = vmFilename.Substring(0, vmFilename.LastIndexOf('\\'));
                    if (!Models.VirtualMachine.ArchiveFile(vmFilename, vmFilename + ".7z"))
                    {
                        //It was a failure
                        //IMPORTANT: if it fails, should it not continue?
                        new SchedulerInfo("VM archiving failed").LogElmah();
                    }
                }
                catch (Exception ex)
                {
                    new SchedulerInfo("Uncaught VM archive error", ex).LogElmah();
                }

                //important: if that excepts, this should probably still continue?
                try
                {
                    dataDB.VirtualMachines.Remove(pendingVM);
                    //TODO create & add an ArchivedVM
                    //dataDB.VirtualMachines.Add(regVM);
                }
                catch (Exception ex)
                {
                    new SchedulerInfo("Error updating database post-VM archive, may need manual modification", ex).LogElmah();
                }
                try
                {
                    dataDB.SaveChanges();
                }

                catch (Exception ex)
                {
                    new SchedulerInfo("Error updating database post-VM archive, may need manual modification", ex).LogElmah();
                }

            }
            new SchedulerInfo("All archiving completed").LogElmah();
        }
예제 #4
0
 private void PowerOff(VirtualMachine vm, RegisteredVirtualMachineService service)
 {
     vm.Status = VirtualMachine.POWERINGOFF;
     dataDB.SaveChanges();
     service.PowerOff();
     vm.Status = VirtualMachine.STOPPED;
     vm.LastStopped = DateTime.Now;
     dataDB.SaveChanges();
 }