Ejemplo n.º 1
0
        public string MigrateVirtualMachine(MigrationVM virtualMachine)
        {
            MigrationVMDetail vmDetail = new MigrationVMDetail(virtualMachine, this);

            if (!vmDetail.Verified)
            {
                Logger.Error("{0}: Skipped: Verify Failed", virtualMachine.Name);
                return("skipped-verifyfailed");
            }

            try {
                VirtualMachineRelocateSpec vmRelocSpec = GetRelocationSpec(vmDetail);
                if (vmRelocSpec != null)
                {
                    Logger.Info("==========================================");
                    Logger.Info("Migrating {0} to {1}{2}", virtualMachine.Name, vmDetail.DestinationClusterComputeResource?.Name, vmDetail.DestinationHostSystem?.Name);
                    Logger.Info("Migrating {0} storage to {1}{2}", virtualMachine.Name, vmDetail.DestinationDatastore?.Name, vmDetail.DestinationStoragePod?.Name);
                    if (vmRelocSpec.DeviceChange?.Count() > 0)
                    {
                        foreach (var deviceChange in vmRelocSpec.DeviceChange)
                        {
                            Logger.Info("Migrating networking to {@0}", deviceChange);
                        }
                    }
                    Logger.Info("==========================================");

                    return(((VirtualMachine)GetViewByName <VirtualMachine>(virtualMachine.Name))
                           .RelocateVM_Task(vmRelocSpec, VirtualMachineMovePriority.highPriority).ToString());
                }
            }
            catch (Exception e)
            {
                Logger.Error("EXCEPTION: {0}", e.Message);
                Logger.Error("EXCEPTION: {0}", e.StackTrace);
                throw;
            }

            Logger.Trace("return null {@value}", vmDetail);
            return(null);
        }
Ejemplo n.º 2
0
        private VirtualMachineRelocateSpec GetRelocationSpec(MigrationVMDetail vmDetail)
        {
            Logger.Info("{0}: Setting up RelocationSpec", vmDetail.SourceVirtualMachine.Name);

            try
            {
                PlacementSpec placementSpec = new PlacementSpec();
                placementSpec.Vm            = vmDetail.SourceVirtualMachine.MoRef;
                placementSpec.Priority      = VirtualMachineMovePriority.highPriority;
                placementSpec.PlacementType = "relocate";
                placementSpec.RelocateSpec  = new VirtualMachineRelocateSpec();

                if (vmDetail.MigrateCompute)
                {
                    if (vmDetail.DestinationIsComputeCluster)
                    {
                        //placementSpec.Hosts = vmDetail.DestinationClusterComputeResource.Host;
                        vmDetail.DestinationHostSystem  = (HostSystem)GetViewByRef <HostSystem>(vmDetail.DestinationClusterComputeResource.RecommendHostsForVm(vmDetail.SourceVirtualMachine.MoRef, vmDetail.DestinationClusterComputeResource.ResourcePool).FirstOrDefault().Host);
                        placementSpec.RelocateSpec.Host = vmDetail.DestinationHostSystem.MoRef;
                        placementSpec.RelocateSpec.Pool = vmDetail.DestinationClusterComputeResource.ResourcePool;
                        Logger.Trace("{0}: Recomended Host {1}", vmDetail.SourceVirtualMachine.Name, placementSpec.RelocateSpec.Host.ToString());
                    }
                    else
                    {
                        placementSpec.RelocateSpec.Host = vmDetail.DestinationHostSystem.MoRef;
                        placementSpec.RelocateSpec.Pool = vmDetail.DestinationClusterComputeResource.ResourcePool;
                    }

                    var networkDeviceConfigSpecs = UpdateVMNetworkDevices(vmDetail.SourceVirtualMachine, placementSpec.RelocateSpec.Host);
                    foreach (var ndc in networkDeviceConfigSpecs)
                    {
                        Logger.Info("{0}: Network Device Config: {1}", vmDetail.SourceVirtualMachine.Name, ndc.Device.Backing);
                    }

                    placementSpec.RelocateSpec.DeviceChange = networkDeviceConfigSpecs.ToArray();
                    Logger.Trace("{value0}: placmentSpec: {@value1}", vmDetail.SourceVirtualMachine.Name, placementSpec);
                }

                if (vmDetail.MigrateStorage)
                {
                    if (vmDetail.DestinationStoragePod != null)
                    {
                        StoragePlacementSpec storagePlacementSpec = new StoragePlacementSpec
                        {
                            Vm               = vmDetail.SourceVirtualMachine.MoRef,
                            Type             = "relocate",
                            Priority         = VirtualMachineMovePriority.highPriority,
                            PodSelectionSpec = new StorageDrsPodSelectionSpec
                            {
                                StoragePod = vmDetail.DestinationStoragePod.MoRef
                            }
                        };
                        var storageRM     = new StorageResourceManager(vClient, vClient.ServiceContent.StorageResourceManager);
                        var storageResult = storageRM.RecommendDatastores(storagePlacementSpec);
                        Logger.Trace("{value0}: storageResult: {@value1}", vmDetail.SourceVirtualMachine.Name, storageResult);
                        Logger.Trace("{0}: StoragePlacementResult: Setting placementSpec.RelocateSpec.Datastore to {@value1}", vmDetail.SourceVirtualMachine.Name, ((StoragePlacementAction)storageResult.Recommendations.FirstOrDefault().Action.FirstOrDefault()).Destination);
                        placementSpec.RelocateSpec.Datastore = ((StoragePlacementAction)storageResult.Recommendations.FirstOrDefault().Action.FirstOrDefault()).Destination;

                        //Logger.Trace("{0}: Setting placementSpec.StoragePods to {1}", vmDetail.SourceVirtualMachine.Name, vmDetail.DestinationStoragePod.MoRef);
                        //placementSpec.StoragePods = new[] { vmDetail.DestinationStoragePod.MoRef };
                    }
                    else
                    {
                        Logger.Trace("{0}: Setting placementSpec.RelocateSpec.Datastore to {1}", vmDetail.SourceVirtualMachine.Name, vmDetail.DestinationDatastore.MoRef);
                        placementSpec.RelocateSpec.Datastore = vmDetail.DestinationDatastore.MoRef;
                    }
                }

                //PlacementResult placementResult = vmDetail.DestinationClusterComputeResource.PlaceVm(placementSpec);
                //if (placementResult.DrsFault == null)
                //{
                //    var recommendedRelocSpec = (VirtualMachineRelocateSpec)((PlacementAction)placementResult.Recommendations[0].Action[0]).RelocateSpec;
                //    Logger.Info("{value0}: recommendedRelocSpec: {@value1}", vmDetail.SourceVirtualMachine.Name, recommendedRelocSpec);
                //    placementSpec.RelocateSpec.Host = recommendedRelocSpec.Host;
                //    Logger.Info("{0}: Recomended Host {1}", vmDetail.SourceVirtualMachine.Name, recommendedRelocSpec.Host.ToString());
                //    placementSpec.RelocateSpec.Datastore = recommendedRelocSpec.Datastore;
                //    Logger.Info("{0}: Recomended Datastore {1}", vmDetail.SourceVirtualMachine.Name, recommendedRelocSpec.Datastore.ToString());
                //    Logger.Trace("{value0}: Final RelocationSpec: {@value1}", vmDetail.SourceVirtualMachine.Name, placementSpec.RelocateSpec);
                //    return placementSpec.RelocateSpec;

                //}
                //else
                //{
                //    Logger.Error("{0}: DRS FAULT: {1}", vmDetail.SourceVirtualMachine.Name, placementResult.DrsFault.Reason);
                //    foreach (var faultsByVm in placementResult.DrsFault.FaultsByVm)
                //        foreach (var fault in faultsByVm.Fault)
                //            Logger.Error("{0}: FaultsByVm: {1}", vmDetail.SourceVirtualMachine.Name, fault.LocalizedMessage);


                return(placementSpec.RelocateSpec);

                //}
            }
            catch (Exception e)
            {
                Logger.Error("EXCEPTION: {0}", vmDetail.SourceVirtualMachine.Name);
                Logger.Error("EXCEPTION: {0}", e.Message);
                Logger.Error("EXCEPTION: {0}", e.StackTrace);
                throw;
            }
        }