Esempio n. 1
0
 private void _VerifyDisksAllAdded(IVimVm replicaVm, VmDiskInfo[] savedDisks)
 {
     if (!CUtils.WaitForResult(this._VerifyDisksRetryCount, this._VerifyDisksWaitBetweenRetry, (Func <bool>)(() => this._GetVMDKInfo(replicaVm).Length == savedDisks.Length)))
     {
         throw new OculiServiceServiceException(0, "Not enough drives are attached to the replica");
     }
 }
        private void _AddDisksTo(IVimVm replicaVm, VmDiskInfo[] disks)
        {
            ScsiControllerInfo[] scsiControllersInfo = replicaVm.GetScsiControllersInfo();
            this._Logger.Information("Creating and adding drives to the replica vm", "CreateESXVm");
            this._Context.SetLowLevelState("CreatingDisks");
            List <VmDiskInfo> source = new List <VmDiskInfo>();

            foreach (VmDiskInfo disk in disks)
            {
                this._CheckStopping();
                disk.CtrlKey = scsiControllersInfo[disk.CtrlKey - 1].CtrlKey;
                string file = disk.File;
                if (!string.IsNullOrEmpty(disk.PreExistingDiskPath))
                {
                    this._ProcessPrestagedDisk(replicaVm, disk);
                }
                else
                {
                    source.Add(disk);
                }
                this._Logger.Information(string.Format("disk \"{0}\" created/added.  Actual file used is \"{1}\"", (object)file, (object)disk.File));
            }
            if (source.Count <= 0)
            {
                return;
            }
            source.Select <VmDiskInfo, long>((Func <VmDiskInfo, long>)(d => d.SizeMB)).Aggregate <long>((Func <long, long, long>)((acc, next) => acc += next));
            CreateVirtualDiskOp.CreateVirtualDiskCallContext virtualDiskCallContext = new CreateVirtualDiskOp.CreateVirtualDiskCallContext();
            this._CreateVirtualDisks(replicaVm, source.ToArray(), (VimClientlContext)virtualDiskCallContext);
        }
 private static void _VerifyDisksAllAdded(IVimVm replicaVm, VmDiskInfo[] savedDisks)
 {
     if (!CUtils.WaitForResult(36, 5000, (Func <bool>)(() => replicaVm.GetVMDKInfo().Length == savedDisks.Length)))
     {
         throw new OculiServiceServiceException(0, "Not enough drives are attached to the replica");
     }
 }
Esempio n. 4
0
        public void Invoke()
        {
            this._Logger.Information("Creating and Configuring the replica vm.");
            this._Context.SetHighAndLowLevelState("PROVISIONING", "ConfiguringVm");
            this._CheckVolumesSpecified();
            this._ESXHost  = this._GetHelperESXHost();
            this._HelperVm = this._GetHelperVm();
            this._HelperSCSIControllers = this._GetScsiControllersInfo(this._HelperVm);
            this._Datastores            = this._GetDatastores();
            this._ValidateHelperForJob();
            this._CheckStopping();
            this._CheckVmDoesntExist();
            this._ReportVmInfo();
            this._CheckStopping();
            VmCreationInfo vmCreationInfo = this._BuildCreationInfo();
            IVimVm         vmWithNoDisks  = this._CreateVmWithNoDisks(vmCreationInfo);

            this._Logger.Verbose("Setting up replica vm information for " + this._Context.JobInfoWrapper.VmName);
            vmCreationInfo.Disks[0].SizeMB = Math.Max(vmCreationInfo.Disks[0].SizeMB, 2060L);
            this._Logger.Verbose("Finished creating replica vm:" + this._Context.JobInfoWrapper.VmName);
            this._CheckStopping();
            this._Context.JobInfoWrapper.VmUuid = new Guid(vmWithNoDisks.Uuid);
            this._Logger.Verbose("Saving the  VM's GUID: " + vmWithNoDisks.Uuid);
            if (this._Context.ESXHost.VC_Vim != null && !this._Context.ESXHost.VC_Vim.IsVirtualCenter && this._VirtualNicType() == VirtualNicType.Vmxnet3)
            {
                this._PowerOnAndOff();
            }
            this._CreateAnyAdditionalSCSIControllers(vmWithNoDisks, vmCreationInfo.Disks);
            this._AddDisksTo(vmWithNoDisks, vmCreationInfo.Disks);
            this._VerifyDisksAllAdded(vmWithNoDisks, vmCreationInfo.Disks);
            this._Logger.Information("Creation and configuration of the replica vm complete.");
        }
Esempio n. 5
0
        public static List <ScsiSlot> GetAvailableScsiSlots(IVimVm v)
        {
            List <ScsiSlot> scsiSlotList             = new List <ScsiSlot>();
            Dictionary <int, List <int> > dictionary = new Dictionary <int, List <int> >();

            foreach (ScsiControllerInfo scsiControllerInfo in v.GetScsiControllersInfo())
            {
                List <int> intList = new List <int>(15);
                for (int index = 0; index < 16; ++index)
                {
                    if (index != 7)
                    {
                        intList.Add(index);
                    }
                }
                dictionary[scsiControllerInfo.BusNumber] = intList;
            }
            foreach (VmdkProperties vmdkProperties in v.GetVMDKInfo())
            {
                if (dictionary.ContainsKey(vmdkProperties.BusNumber))
                {
                    dictionary[vmdkProperties.BusNumber].Remove(vmdkProperties.UnitNumber);
                }
            }
            foreach (KeyValuePair <int, List <int> > keyValuePair in dictionary)
            {
                foreach (int u in keyValuePair.Value)
                {
                    scsiSlotList.Add(new ScsiSlot(keyValuePair.Key, u));
                }
            }
            return(scsiSlotList);
        }
Esempio n. 6
0
 protected virtual void _EnsureVmFolderExists(IVimVm replicaVm, VmDiskInfo disk)
 {
     if (disk.Location.DirectoryExist(replicaVm.Name, this._Context.ESXHost.ClientCtx))
     {
         return;
     }
     disk.Location.CreateDirectory(replicaVm.Name);
 }
Esempio n. 7
0
 private void _AddDisksTo(IVimVm replicaVm, VmDiskInfo[] disks)
 {
     ScsiControllerInfo[] scsiControllersInfo = this._GetScsiControllersInfo(replicaVm);
     this._Logger.Verbose("Creating and adding drives to the replica vm");
     this._Context.SetLowLevelState("CreatingDisks");
     this._GetDatastore();
     foreach (VmDiskInfo disk in disks)
     {
         this._CheckStopping();
         bool   flag = false;
         string file = disk.File;
         disk.CtrlKey = scsiControllersInfo[disk.CtrlKey - 1].CtrlKey;
         if (!string.IsNullOrEmpty(disk.PreExistingDiskPath))
         {
             try
             {
                 IVimDatastore location = disk.Location;
                 this._Logger.Verbose("Getting Vmdks Info on datastore: " + location.Name);
                 string fileName = Path.GetFileName(disk.PreExistingDiskPath.Replace("\\", "/"));
                 Dictionary <string, VmdkFileInfo> vmdksFileInfo = location.GetVmdksFileInfo(disk.PreExistingDiskPath, this._Context.ESXHost.ClientCtx);
                 if (vmdksFileInfo == null && vmdksFileInfo.Count < 2)
                 {
                     throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " could not be found.", false);
                 }
                 VmdkFileInfo fileInfo1 = vmdksFileInfo[fileName];
                 VmdkFileInfo fileInfo2 = vmdksFileInfo[fileInfo1.DataFileName];
                 if (fileInfo1.Size + fileInfo2.Size <= 0UL)
                 {
                     throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " does not have a valid configuration.", false);
                 }
                 this._EnsureVmFolderExists(replicaVm, disk);
                 string targetFile1 = this._VimUtils_GetVolumeName(location) + this._Context.JobInfoWrapper.VmName + "/" + fileInfo1.Name;
                 this._Logger.Verbose(string.Format("Pre-stage folder vmdks \"{0}\" moving to destination folder \"{1}\"", (object)fileInfo1.FullName, (object)targetFile1));
                 this._VimDatastore_MoveFilesByFullName(location, fileInfo1, targetFile1);
                 string targetFile2 = this._VimUtils_GetVolumeName(location) + this._Context.JobInfoWrapper.VmName + "/" + fileInfo2.DataFileName;
                 this._Logger.Verbose(string.Format("Pre-stage folder vmdks \"{0}\" moving to destination folder \"{1}\"", (object)fileInfo2.FullName, (object)targetFile2));
                 this._VimDatastore_MoveFilesByFullName(location, fileInfo2, targetFile2);
                 this._Logger.Verbose(string.Format("Pre-stage Disk found and trying to add \"{0}\"", (object)targetFile1), "CreateESXVm");
                 disk.File = targetFile1;
                 this._AddVirtualDisk(replicaVm, disk);
                 flag = true;
             }
             catch (Exception ex)
             {
                 this._Logger.Error(ex, "Exception thrown on attaching a disk:");
                 throw ex;
             }
         }
         if (!flag)
         {
             this._Logger.Verbose(string.Format("Trying to create disk \"{0}\"", (object)file));
             this._CreateVirtualDisk(replicaVm, disk);
         }
         this._Logger.Verbose(string.Format("disk \"{0}\" created/added.  Actual file used is \"{1}\"", (object)file, (object)disk.File));
     }
 }
Esempio n. 8
0
        public static int GetSCSIBusFromControllerKey(IVimVm v, int ctrlKey)
        {
            ScsiControllerInfo scsiControllerInfo = ((IEnumerable <ScsiControllerInfo>)v.GetScsiControllersInfo()).FirstOrDefault <ScsiControllerInfo>((Func <ScsiControllerInfo, bool>)(si => si.CtrlKey == ctrlKey));

            if (scsiControllerInfo == null)
            {
                throw new OculiServiceServiceException(0, "Internal Error: missing scsi controller with key" + ctrlKey.ToString());
            }
            return(scsiControllerInfo.BusNumber);
        }
Esempio n. 9
0
        private IVimVm _CreateVmWithNoDisks(VmCreationInfo vmCreationInfo)
        {
            this._CheckStopping();
            this._Context.SetLowLevelState("CreatingVm");
            VmDiskInfo[] disks = vmCreationInfo.Disks;
            vmCreationInfo.Disks = (VmDiskInfo[])null;
            IVimVm vm = this._CreateVm(vmCreationInfo, this._ESXHost);

            vmCreationInfo.Disks = disks;
            return(vm);
        }
Esempio n. 10
0
 public void Invoke()
 {
     try
     {
         IVimVm replicaVm = this._Vm();
         if (replicaVm == null || !replicaVm.IsPoweredOn())
         {
             return;
         }
         this._StopVm(replicaVm, this._VC(), this._Context.Logger, 120);
     }
     catch (Exception ex)
     {
         this._Logger.Warning(ex, "Exception caught powering down the replica.  It may be down already and that's ok.  Exception:");
     }
 }
Esempio n. 11
0
        private void _CreateAnyAdditionalSCSIControllers(IVimVm replicaVm, VmDiskInfo[] savedDisks)
        {
            int num = savedDisks.Length / 15 + 1;

            if (num <= 1)
            {
                return;
            }
            this._Logger.Verbose("Creating additional SCSI controllers on replica vm");
            this._Context.SetLowLevelState("CreatingScsiCtrl");
            int[] scsiBusNumbers = new int[num - 1];
            for (int index = 1; index < num; ++index)
            {
                scsiBusNumbers[index - 1] = index;
            }
            this._AddScsiController(replicaVm, scsiBusNumbers);
        }
Esempio n. 12
0
 public void Invoke()
 {
     try
     {
         IVimVm vimVm = this._SourceVm();
         if (vimVm == null || !vimVm.IsPoweredOn())
         {
             return;
         }
         vimVm.PowerOff(this._ClientCtx());
     }
     catch (Exception ex)
     {
         this._Logger.Warning(ex, "There was an exception powering off the VM, using backup strategy: ");
         this._ShutdownSource();
     }
 }
Esempio n. 13
0
        public IVimVm[] GetVmsAndProperties(ManagedObjectAndProperties[] managedObjectsAndProperties)
        {
            List <IVimVm> vimVmList = new List <IVimVm>();

            foreach (ManagedObjectAndProperties objectsAndProperty in managedObjectsAndProperties)
            {
                if (objectsAndProperty.ManagedObject.type == "VirtualMachine")
                {
                    IVimVm vimVm = (IVimVm) new Vm(this.VcService, objectsAndProperty.ManagedObject);
                    vimVm.GetCommonProperties(objectsAndProperty.Properties);
                    if (!vimVm.VMProperties.IsTemplate)
                    {
                        vimVmList.Add(vimVm);
                    }
                }
            }
            return(vimVmList.ToArray());
        }
        private void _CreateAnyAdditionalSCSIControllers(IVimVm replicaVm, VmDiskInfo[] savedDisks)
        {
            ScsiControllerInfo[] scsiControllersInfo = replicaVm.GetScsiControllersInfo();
            int num = savedDisks.Length / 15 + 1;

            if (num <= scsiControllersInfo.Length)
            {
                return;
            }
            this._Logger.Information("Creating additional SCSI controllers on replica vm");
            this._Context.SetLowLevelState("CreatingScsiCtrl");
            int[] busNumbers = new int[num - scsiControllersInfo.Length];
            for (int length = scsiControllersInfo.Length; length < num; ++length)
            {
                busNumbers[length - scsiControllersInfo.Length] = length;
            }
            replicaVm.AddScsiCtrl(busNumbers, this._ScsiControllerType(), this._Context.ESXHost.ClientCtx);
        }
 public void Invoke(IVimVm vm)
 {
     this._Vm = vm;
     this._Logger.FormatInformation("Adding disk to the vm {0}", (object)this._Vm.Name);
     this._Context.SetLowLevelState("ConfiguringVm");
     this._ESXHost  = this._GetESXHostVim();
     this._HelperVm = this._GetHelperVm();
     this._HelperSCSIControllers = this._HelperVm.GetScsiControllersInfo();
     this._Logger.Information("Setting up VMDK file information");
     this._AssignControllersToVolumes();
     VmDiskInfo[] vmDiskInfoArray = this._BuildDiskList(this._Context.JobInfoWrapper.VolumePersistedState);
     if (vmDiskInfoArray.Length == 0)
     {
         throw new OculiServiceException(0, "There are no disks to create");
     }
     vmDiskInfoArray[0].SizeMB = Math.Max(vmDiskInfoArray[0].SizeMB, 2060L);
     this._CheckStopping();
     this._CreateAnyAdditionalSCSIControllers(this._Vm, vmDiskInfoArray);
     this._AddDisksTo(this._Vm, vmDiskInfoArray);
     AddDisksToExistingVmESX._VerifyDisksAllAdded(this._Vm, vmDiskInfoArray);
     this._Logger.Information("All disks added");
 }
 private void _ProcessPrestagedDisk(IVimVm replicaVm, VmDiskInfo disk)
 {
     try
     {
         IVimDatastore location = disk.Location;
         this._Logger.Information("Getting Vmdks Info on datastore: " + location.Name);
         string fileName = Path.GetFileName(disk.PreExistingDiskPath.Replace("\\", "/"));
         Dictionary <string, VmdkFileInfo> vmdksFileInfo = location.GetVmdksFileInfo(disk.PreExistingDiskPath, this._Context.ESXHost.ClientCtx);
         if (vmdksFileInfo == null && vmdksFileInfo.Count < 2)
         {
             throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " could not be found.", false);
         }
         VmdkFileInfo vmdkFileInfo1 = vmdksFileInfo[fileName];
         VmdkFileInfo vmdkFileInfo2 = vmdksFileInfo[vmdkFileInfo1.DataFileName];
         if (vmdkFileInfo1.Size + vmdkFileInfo2.Size <= 0UL)
         {
             throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " does not have a valid configuration.", false);
         }
         string str     = this._VimUtilsGetVolumeName(location) + this._Context.JobInfoWrapper.VmName + "/";
         string name    = vmdkFileInfo1.Name;
         string target1 = str + name;
         this._Logger.Information(string.Format("Pre-stage folder vmdk-s \"{0}\" moving to destination folder \"{1}\"", (object)vmdkFileInfo1.FullName, (object)target1));
         location.MoveFilesByFullName(vmdkFileInfo1.FullName, target1, this._Context.JobInfoWrapper.VmName, true, this._Context.ESXHost.ClientCtx);
         string dataFileName = vmdkFileInfo1.DataFileName;
         string target2      = str + dataFileName;
         this._Logger.Information(string.Format("Pre-stage folder vmdk-s \"{0}\" moving to destination folder \"{1}\"", (object)vmdkFileInfo2.FullName, (object)target2));
         location.MoveFilesByFullName(vmdkFileInfo2.FullName, target2, this._Context.JobInfoWrapper.VmName, true, this._Context.ESXHost.ClientCtx);
         this._Logger.Information(string.Format("Pre-stage Disk found and trying to add \"{0}\"", (object)target1));
         disk.File = target1;
         this._AddVirtualDisk(replicaVm, disk);
     }
     catch (Exception ex)
     {
         this._Logger.Error("Exception thrown on attaching a disk" + ex.ToString());
         throw ex;
     }
 }
Esempio n. 17
0
        public void Invoke()
        {
            this._Context.Logger.Information("Creating and configuring the replica vm.");
            this._Context.SetHighAndLowLevelState("PROVISIONING", "ConfiguringVm");
            if (this._Context.JobInfoWrapper.SourceVolumes.Length == 0)
            {
                throw new OculiServiceServiceException(0, "There are no source volumes specified");
            }
            this._ESXHost  = this._Context.ESXHost.HelperESXHost();
            this._HelperVm = this._Context.ESXHost.HelperVm();
            this._HelperSCSIControllers = this._HelperVm.GetScsiControllersInfo();
            this._ValidateHelperForJob();
            this._CheckStopping();
            this._CreateVmName();
            this._CheckStopping();
            IVimVm vm = this._CreateVm();

            this._Context.Logger.Verbose(" vm \"" + this._Context.JobInfoWrapper.VmName + "\" created");
            this._Context.JobInfoWrapper.VmUuid = new Guid(vm.Uuid);
            this._Context.Logger.Verbose("Saving the  VM's GUID: " + vm.Uuid);
            this._Context.SetLowLevelState("ConfiguringVm");
            this._CreateNewDisks(vm);
            this._Context.Logger.Information("Creation and configuration of replica vm is complete.");
        }
 protected virtual void _AddVirtualDisk(IVimVm replicaVm, VmDiskInfo disk)
 {
     new AddVirtualDiskOp(replicaVm, this._Context.ESXHost.VC_Vim, disk, this._Context.Logger, 120).Run();
 }
 protected virtual void _CreateVirtualDisks(IVimVm replicaVm, VmDiskInfo[] vmDiskInfo, VimClientlContext ctx)
 {
     replicaVm.CreateVirtualDisks(vmDiskInfo, ctx);
 }
Esempio n. 20
0
 protected virtual void _StopVm(IVimVm replicaVm, IVimService vc, ILogger logger, int maxRetryTime)
 {
     new StopVmOp(replicaVm, vc, logger, maxRetryTime).Run();
 }
Esempio n. 21
0
 protected virtual void _AddScsiController(IVimVm replicaVm, int[] scsiBusNumbers)
 {
     replicaVm.AddScsiCtrl(scsiBusNumbers, this._ScsiControllerType(), this._Context.ESXHost.ClientCtx);
 }
Esempio n. 22
0
 protected virtual void _CreateNewDisks(IVimVm replica)
 {
     this._Context.Invoker.AddDisksToExistingVmESX(replica);
 }
Esempio n. 23
0
 protected virtual ScsiControllerInfo[] _GetScsiControllersInfo(IVimVm replicaVm)
 {
     return(replicaVm.GetScsiControllersInfo());
 }
Esempio n. 24
0
 protected virtual VmdkProperties[] _GetVMDKInfo(IVimVm vm)
 {
     return(vm.GetVMDKInfo());
 }