예제 #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);
        }
예제 #2
0
        public MigrationVMDetail(MigrationVM SourceVM, VAPIConnection VClient)
        {
            var config = new NLog.Config.LoggingConfiguration();

            // Targets where to log to: File and Console
            var logfile = new NLog.Targets.FileTarget("logfile")
            {
                FileName = "MToolVapiClient.log", Layout = "${longdate} | ${level:uppercase=true:padding=-5:fixedLength=true} | ${logger:padding=-35:fixedLength=true} | ${message}"
            };
            var logconsole = new NLog.Targets.ConsoleTarget("logconsole")
            {
                Layout = "${longdate} | ${level:uppercase=true:padding=-5:fixedLength=true} | ${logger:padding=-35:fixedLength=true} | ${message}"
            };

            // Rules for mapping loggers to targets
            config.AddRule(LogLevel.Trace, LogLevel.Fatal, logconsole);
            config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);

            // Apply config
            NLog.LogManager.Configuration = config;

            try
            {
                SourceVirtualMachine = (VirtualMachine)VClient.GetViewByName <VirtualMachine>(SourceVM.Name);
                Logger.Info("{0}: Source VM {0} :: {1}", SourceVirtualMachine.Name, SourceVirtualMachine.MoRef);
                SourceHostSystem = (HostSystem)VClient.GetViewByRef <HostSystem>(SourceVirtualMachine.Runtime.Host);
                Logger.Info("{0}: Source Host {1} :: {2}", SourceVirtualMachine.Name, SourceHostSystem.Name, SourceHostSystem.MoRef);

                var cluster = VClient.GetViewByRef <ClusterComputeResource>(SourceHostSystem.Parent);
                if (cluster is ClusterComputeResource)
                {
                    SourceClusterComputeResource = (ClusterComputeResource)cluster;
                }
                else if (cluster is ComputeResource)
                {
                    SourceComputeResource = (ComputeResource)VClient.GetViewByRef <ComputeResource>(SourceHostSystem.Parent);
                }
                Logger.Info("{0}: Source Cluster {1} :: {2}", SourceVirtualMachine.Name, SourceClusterComputeResource?.Name, SourceClusterComputeResource?.MoRef);

                SourceDatastore = new List <Datastore>();
                foreach (var datastore in SourceVirtualMachine.Datastore)
                {
                    var datastoreView = (Datastore)VClient.GetViewByRef <Datastore>(datastore);
                    Logger.Info("{0}: Source Datastore {1} :: {2}", SourceVirtualMachine.Name, datastoreView.Name, datastoreView.MoRef);
                    SourceDatastore.Add(datastoreView);
                    var datastoreParent = VClient.GetViewByRef <StoragePod>(datastoreView.Parent);
                    if (datastoreParent is StoragePod)
                    {
                        SourceStoragePod = (StoragePod)datastoreParent;
                        Logger.Info("{0}: Source StoragePod {1} :: {2}", SourceVirtualMachine.Name, SourceStoragePod.Name, SourceStoragePod.MoRef);
                    }
                    Logger.Info("{0}: Total VM Datastores = {1}", SourceVirtualMachine.Name, SourceDatastore.Count());
                }

                EntityViewBase temporaryViewObject;
                temporaryViewObject = VClient.GetViewByName <ClusterComputeResource>(SourceVM.DestinationCompute);
                if (temporaryViewObject != null)
                {
                    DestinationClusterComputeResource = (ClusterComputeResource)temporaryViewObject;
                }
                else
                {
                    temporaryViewObject = VClient.GetViewByName <HostSystem>(SourceVM.DestinationCompute);
                    if (temporaryViewObject != null)
                    {
                        DestinationIsComputeCluster       = false;
                        DestinationHostSystem             = (HostSystem)temporaryViewObject;
                        DestinationClusterComputeResource = (ClusterComputeResource)VClient.GetViewByRef <ClusterComputeResource>(DestinationHostSystem.Parent);
                    }
                }
                Logger.Info("{0}: Destination Cluster {1} :: {2}", SourceVirtualMachine.Name, DestinationClusterComputeResource.Name, DestinationClusterComputeResource.MoRef);
                Logger.Info("{0}: Destination HostSystem {1} :: {2}", SourceVirtualMachine.Name, DestinationHostSystem?.Name, DestinationHostSystem?.MoRef);

                temporaryViewObject = VClient.GetViewByName <StoragePod>(SourceVM.DestinationStorage);
                if (temporaryViewObject != null)
                {
                    DestinationStoragePod = (StoragePod)VClient.GetViewByName <StoragePod>(SourceVM.DestinationStorage);
                    Logger.Info("{0}: Destination StoragePod {1} :: {2}", SourceVirtualMachine.Name, DestinationStoragePod.Name, DestinationStoragePod.MoRef);
                }
                else
                {
                    DestinationDatastore = (Datastore)VClient.GetViewByName <Datastore>(SourceVM.DestinationStorage);
                    Logger.Info("{0}: Destination Datastore {1} :: {2}", SourceVirtualMachine.Name, DestinationDatastore.Name, DestinationDatastore.MoRef);
                }
            }
            catch (Exception e)
            {
                Logger.Error("EXCEPTION: {0}", e.Message);
                Logger.Error("EXCEPTION: {0}", e.StackTrace);
            }
        }