Ejemplo n.º 1
0
        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 PowerOnTest()
 {
     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.STOPPED)
     {
         target.PowerOn();
         Assert.IsTrue(target.GetStatus() == VirtualMachine.POWERINGON || target.GetStatus() == VirtualMachine.RUNNING);
     }
     else
         Assert.Inconclusive();
 }
 public void PauseTest()
 {
     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.RUNNING)
     {
         target.Pause();
         Assert.Equals(target.GetStatus(), VirtualMachine.PAUSED);
     }
     else
         Assert.Inconclusive();
 }
Ejemplo n.º 4
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();
        }
 public void ToggleVMStatusTest()
 {
     VirtualMachineRepository target = new VirtualMachineRepository(); // TODO: Initialize to an appropriate value
     VirtualMachine vm = target.GetAllRegisteredVirtualMachines().GetEnumerator().Current;
     var service = new RegisteredVirtualMachineService(vm.ImagePathName);
     if (service.IsRunning())
     {
         int status = target.ToggleVMStatus(vm.VirtualMachineId);
         Assert.IsTrue(status == VirtualMachine.STOPPED || status == VirtualMachine.POWERINGOFF);
     }
     else
     {
         int status = target.ToggleVMStatus(vm.VirtualMachineId);
         Assert.IsTrue(status == VirtualMachine.RUNNING || status == VirtualMachine.POWERINGON);
     }
 }
Ejemplo n.º 6
0
 private void PowerOn(VirtualMachine vm, RegisteredVirtualMachineService service)
 {
     vm.Status = VirtualMachine.POWERINGON;
     dataDB.SaveChanges();
     service.PowerOn();
     vm.Status = VirtualMachine.RUNNING;
     vm.LastStarted = DateTime.Now;
     dataDB.SaveChanges();
 }
Ejemplo n.º 7
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();
 }
Ejemplo n.º 8
0
        private void InitializeDataContext()
        {
            var registeredImages = RegisteredVirtualMachineService.GetRegisteredVMImagePaths();

            foreach (var image in registeredImages)
            {
                int startIndex = image.IndexOf("] ") + "] ".Length;
                int length = image.IndexOf('/', startIndex) - startIndex;
                string projectName = image.Substring(startIndex, length);
                var service = new RegisteredVirtualMachineService(image);

                if (!dataDB.Projects.Select(p => p.ProjectName).Contains(projectName))
                {
                    var project = new Project(projectName);
                    dataDB.Projects.Add(project);
                    dataDB.SaveChanges();
                }

                startIndex = image.LastIndexOf('/') + 1;
                length = image.LastIndexOf('.') - startIndex;
                string machineName = image.Substring(startIndex, length);

                VirtualMachine vm;

                try
                {
                    vm = dataDB.VirtualMachines.Single(v => v.MachineName == machineName);
                }
                catch (Exception)
                {
                    vm = new VirtualMachine();
                    dataDB.VirtualMachines.Add(vm);
                }

                //PowerOn(vm, service);
                vm.MachineName = machineName;
                vm.ImagePathName = image;
                vm.Status = service.GetStatus();

                //TODO reset
                //vm.Hostname = "test-hostname";
                vm.Hostname = service.GetHostname();
                vm.IP = service.GetIP();
                vm.Project = dataDB.Projects.Single(p => p.ProjectName == projectName);

                dataDB.SaveChanges();
            }
        }
Ejemplo n.º 9
0
        public int ToggleVMStatus(int id)
        {
            VirtualMachine vm = dataDB.VirtualMachines.Single(d => d.VirtualMachineId == id);
            var service = new RegisteredVirtualMachineService(vm.ImagePathName);

            if (service.IsRunning())
                PowerOff(vm, service);
            else
                PowerOn(vm, service);

            return vm.Status;
        }
Ejemplo n.º 10
0
        public void ReinitializeAllRegisteredVirtualMachines()
        {
            var imagePathNames = RegisteredVirtualMachineService.GetRegisteredVMImagePaths();

            foreach (var image in imagePathNames)
            {
                int startIndex = image.IndexOf("] ") + "] ".Length;
                int length = image.IndexOf('/', startIndex) - startIndex;
                string projectName = image.Substring(startIndex, length);
                var service = new RegisteredVirtualMachineService(image);

                if (!dataDB.Projects.Select(p => p.ProjectName).Contains(projectName))
                {
                    var project = new Project(projectName);
                    dataDB.Projects.Add(project);
                    dataDB.SaveChanges();
                }

                startIndex = image.LastIndexOf('/') + 1;
                length = image.LastIndexOf('.') - startIndex;
                string machineName = image.Substring(startIndex, length);

                if (!dataDB.VirtualMachines.Select(v => v.MachineName).Contains(machineName))
                {
                    VirtualMachine vm = new VirtualMachine();

                    vm.MachineName = machineName;
                    vm.ImagePathName = image;
                    vm.Status = service.GetStatus();
                    vm.Hostname = service.GetHostname();
                    vm.IP = service.GetIP();
                    vm.Project = dataDB.Projects.Single(p => p.ProjectName == projectName);

                    dataDB.VirtualMachines.Add(vm);
                    dataDB.SaveChanges();
                }
            }
        }
Ejemplo n.º 11
0
        public ICollection<Project> GetAllProjects()
        {
            foreach (var vm in dataDB.VirtualMachines.Where(v => v.Status != VirtualMachine.ARCHIVED &&
                v.Status != VirtualMachine.PENDING))
            {
                RegisteredVirtualMachineService service = null;

                try
                {
                    service = new RegisteredVirtualMachineService(vm.ImagePathName);
                }
                catch (Vestris.VMWareLib.VMWareException)
                {
                    dataDB.VirtualMachines.Remove(vm);
                    continue;
                }

                vm.Status = service.GetStatus();

                if (vm.IP == null && vm.Status == VirtualMachine.RUNNING)
                    vm.IP = service.GetIP();

                if (vm.Hostname == null && vm.Status == VirtualMachine.RUNNING)
                    vm.Hostname = service.GetHostname();
            }

            dataDB.SaveChanges();
            return dataDB.Projects.ToList();
        }
 public void SetHostnameTest()
 {
     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
     string hostname = string.Empty; // TODO: Initialize to an appropriate value
     target.SetHostname(hostname);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
 public void SetIPTest()
 {
     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
     string oldIP = target.GetIP();
     //set the new IP to the same first several sets of characters, but change the last few
     string value = oldIP.Substring(0, oldIP.LastIndexOf('.')) + ".200";
     target.SetIP(value);
     string newIP = target.GetIP();
     target.SetIP(oldIP);
     Assert.Equals(newIP, value);
 }
Ejemplo n.º 14
0
        private void SetIPHostname(RegisteredVirtualMachineService service, bool retry = true)
        {
            try
            {
                System.Threading.Thread.Sleep(12 * 1000);
                service.SetHostname(VM.Hostname);
                System.Threading.Thread.Sleep(8 * 1000);
                service.SetIP(VM.IP.ToString());
                System.Threading.Thread.Sleep(8 * 1000);

            }
            catch (Exception ex)
            {
                if (retry) SetIPHostname(service, false);
                else new SchedulerInfo("Error setting IP or hostname of new VM, will need to be manually set but will continue", ex).LogElmah();
            }
        }