private void WaitForTaskHandler(object sender, PropertyFilterUpdateEventArgs args)
 {
     foreach (PropertyChange change in args.FilterUpdate.objectSet[0].changeSet)
     {
         System.Console.WriteLine("Change: name->{0}, op->{1}, val->{2}", change.name, change.op, change.val);
         if (change.name == "info.error")
         {
             if ((change.op == PropertyChangeOp.assign ||
                  change.op == PropertyChangeOp.add) &&
                 change.val != null)
             {
                 // Signal we are done...
                 _done.Set();
             }
         }
         else if (change.name == "info.state")
         {
             if (change.op == PropertyChangeOp.assign ||
                 change.op == PropertyChangeOp.add)
             {
                 TaskInfoState tis = (TaskInfoState)change.val;
                 if (tis != TaskInfoState.queued && tis != TaskInfoState.running)
                 {
                     // Signal we are done...
                     _done.Set();
                 }
             }
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Waits for the task represented by the specified <see cref="VSphereManagedObject" /> to reach a completed state.
        /// </summary>
        /// <param name="taskObject">The <see cref="VSphereManagedObject" /> representing the task.</param>
        /// <param name="timeout">The length of time to wait for the task to complete.</param>
        /// <returns>A <see cref="VSphereTaskResult" /> representing the result of the task.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="taskObject" /> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout" /> is zero or negative.</exception>
        public VSphereTaskResult WaitForTaskCompletion(VSphereManagedObject taskObject, TimeSpan timeout)
        {
            if (taskObject == null)
            {
                throw new ArgumentNullException(nameof(taskObject));
            }

            if (timeout <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), "Timeout must be greater than zero.");
            }

            VSpherePropertySpec getTaskState = new VSpherePropertySpec(VSphereManagedObjectType.Task, "info.state", "info.result", "info.error");

            VSphereManagedObject task = RetrieveObject(taskObject, getTaskState);

            for (int i = 0; i < (int)timeout.TotalSeconds; i++)
            {
                TaskInfoState taskState = (TaskInfoState)task.Properties["info.state"];
                if (taskState == TaskInfoState.success || taskState == TaskInfoState.error)
                {
                    return(new VSphereTaskResult(task));
                }
                else
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    task = RetrieveObject(taskObject, getTaskState);
                }
            }
            return(new VSphereTaskResult(task));
        }
Beispiel #3
0
 internal Task(IVimService vimService, ManagedObjectReference managedObject, string description, DateTime?completeTime, TaskInfoState state)
     : base(vimService, managedObject)
 {
     this._logger       = ((VCService)vimService).Logger;
     this._description  = description;
     this._completeTime = completeTime;
     this._state        = state;
 }
Beispiel #4
0
        public TaskInfoState ReconfigVM(VirtualMachineConfigSpec spec)
        {
            ManagedObjectReference morTask = _vimService.ReconfigVM_Task(_morThis, spec);
            MyVMTask      myTask           = new MyVMTask(_vimService, _pc, morTask);
            TaskInfo      ti    = null;
            TaskInfoState state = myTask.Wait(3000, -1, out ti);

            return(state);
        }
Beispiel #5
0
        public Tuple <TaskInfoState, TaskInfo> WaitForTask(ManagedObjectReference taskMoref)
        {
            var taskResult = svcUtil.WaitForValues(taskMoref,
                                                   new string[] { "info.state", "info.result" },
                                                   new string[] { "state" },
                                                   new object[][] { new object[] { TaskInfoState.success, TaskInfoState.error } });

            ObjectContent[] objTaskInfo = svcUtil.GetObjectProperties(svcConn._sic.propertyCollector, taskMoref, new String[] { "info" });
            TaskInfo        tInfo       = (TaskInfo)objTaskInfo[0].propSet[0].val;
            TaskInfoState   taskState   = (TaskInfoState)taskResult[0];

            return(new Tuple <TaskInfoState, TaskInfo>(taskState, tInfo));
        }
Beispiel #6
0
        public TaskInfoState RemoveSnapshot(string snapshot, bool removeChildren, bool consolidate)
        {
            ManagedObjectReference morSnapshot = new ManagedObjectReference
            {
                @type = "VirtualMachineSnapshot",
                Value = snapshot
            };
            ManagedObjectReference morTask = _vimService.RemoveSnapshot_Task(morSnapshot, removeChildren, consolidate);
            MyVMTask      myTask           = new MyVMTask(_vimService, _pc, morTask);
            TaskInfo      ti    = null;
            TaskInfoState state = myTask.Wait(3000, -1, out ti);

            return(state);
        }
Beispiel #7
0
        public TaskInfoState CreateSnapshot(string name, string description, bool memory, bool quiesce, out string snapshotOut)
        {
            snapshotOut = string.Empty;
            ManagedObjectReference morTask = _vimService.CreateSnapshot_Task(_morThis, name, description, memory, quiesce);
            MyVMTask      myTask           = new MyVMTask(_vimService, _pc, morTask);
            TaskInfo      ti    = null;
            TaskInfoState state = myTask.Wait(3000, -1, out ti);

            if (state == TaskInfoState.success)
            {
                ManagedObjectReference morSnapshot = (ManagedObjectReference)(ti.result);
                snapshotOut = morSnapshot?.Value;
            }
            return(state);
        }
 public void SetTaskState(TaskInfoState state, object result, LocalizedMethodFault fault)
 {
     try
     {
         SetTaskStateRequestType setTaskStateRequestType = new SetTaskStateRequestType();
         setTaskStateRequestType._this  = (VimApi_65.ManagedObjectReference)VIConvert.ToWsdlType(this.MoRef);
         setTaskStateRequestType.state  = (VimApi_65.TaskInfoState)VIConvert.ToWsdlType(state);
         setTaskStateRequestType.result = result;
         setTaskStateRequestType.fault  = (VimApi_65.LocalizedMethodFault)VIConvert.ToWsdlType(fault);
         this.VimServiceProxy.SetTaskState(new SetTaskStateRequest(setTaskStateRequestType));
     }
     catch (FaultException ex)
     {
         throw FaultUtility.HandleFault(ex);
     }
 }
Beispiel #9
0
 private void displayTasks(ObjectContent[] oContents)
 {
     for (int oci = 0; oci < oContents.Length; ++oci)
     {
         Console.WriteLine("Task");
         DynamicProperty[] dps = oContents[oci].propSet;
         if (dps != null)
         {
             String op = "", name = "", type = "", state = "", error = "";
             for (int dpi = 0; dpi < dps.Length; ++dpi)
             {
                 DynamicProperty dp = dps[dpi];
                 if ("info.entity".Equals(dp.name))
                 {
                     type = ((ManagedObjectReference)dp.val).GetType().ToString();
                 }
                 else if ("info.entityName".Equals(dp.name))
                 {
                     name = ((String)dp.val);
                 }
                 else if ("info.name".Equals(dp.name))
                 {
                     op = ((String)dp.val);
                 }
                 else if ("info.state".Equals(dp.name))
                 {
                     TaskInfoState tis = (TaskInfoState)dp.val;
                     if (TaskInfoState.error.Equals(tis))
                     {
                         state = "-Error";
                     }
                     else if (TaskInfoState.queued.Equals(tis))
                     {
                         state = "-Queued";
                     }
                     else if (TaskInfoState.running.Equals(tis))
                     {
                         state = "-Running";
                     }
                     else if (TaskInfoState.success.Equals(tis))
                     {
                         state = "-Success";
                     }
                 }
                 else if ("info.cancelled".Equals(dp.name))
                 {
                     Boolean b = (Boolean)dp.val;
                     //I need to chk
                     if (b != null)
                     {
                         state += "-Cancelled";
                     }
                 }
                 else if ("info.error".Equals(dp.name))
                 {
                     LocalizedMethodFault mf = (LocalizedMethodFault)dp.val;
                     if (mf != null)
                     {
                         error = mf.localizedMessage;
                     }
                 }
                 else
                 {
                     op = "Got unexpected property: " + dp.name
                          + " Value: " + dp.val.ToString();
                 }
             }
             Console.WriteLine("Operation " + op);
             Console.WriteLine("Name " + name);
             Console.WriteLine("Type " + type);
             Console.WriteLine("State " + state);
             Console.WriteLine("Error " + error);
             Console.WriteLine("======================");
         }
     }
     if (oContents.Length == 0)
     {
         Console.WriteLine("Currently no task running");
     }
 }
Beispiel #10
0
        public static int MainFindVM(MainSearchVM_In inParam, MainSearchVM_Out outRet)
        {
            VmSummaryInfo vmSummaryInfo = new VmSummaryInfo();

            System.Net.ServicePointManager.Expect100Continue = false;

            MyCertVerify certVerify = new MyCertVerify();

            System.Net.ServicePointManager.ServerCertificateValidationCallback =
                certVerify.RemoteCertificateValidationCallback;

            BasicHttpBinding binding = null;

            if (string.Compare("https", inParam.Protocal, StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
                {
                    AllowCookies           = true,
                    OpenTimeout            = TimeSpan.FromHours(1),
                    ReceiveTimeout         = TimeSpan.FromHours(1),
                    SendTimeout            = TimeSpan.FromHours(1),
                    CloseTimeout           = TimeSpan.FromHours(1),
                    MaxBufferPoolSize      = 1024 * 1024 * 2,
                    MaxReceivedMessageSize = 1024 * 512,
                    MaxBufferSize          = 1024 * 512
                };
            }
            else
            {
                binding = new BasicHttpBinding()
                {
                    AllowCookies           = true,
                    OpenTimeout            = TimeSpan.FromHours(1),
                    ReceiveTimeout         = TimeSpan.FromHours(1),
                    SendTimeout            = TimeSpan.FromHours(1),
                    CloseTimeout           = TimeSpan.FromHours(1),
                    MaxBufferPoolSize      = 1024 * 1024 * 2,
                    MaxReceivedMessageSize = 1024 * 512,
                    MaxBufferSize          = 1024 * 512
                };
            }

            string          sdkUrl = string.Format("{0}://{1}{2}/sdk", inParam.Protocal, inParam.ServerName, 0 == inParam.Port ? "" : ":" + inParam.Port.ToString());
            EndpointAddress epa    = new EndpointAddress(sdkUrl);

            vmSummaryInfo.VmHost.ServerName = inParam.ServerName;
            vmSummaryInfo.VmHost.UserName   = inParam.User;
            vmSummaryInfo.VmHost.Password   = inParam.Password;
            vmSummaryInfo.VmHost.Port       = inParam.Port;


            StringBuilder sb = new StringBuilder();
            StringWriter  sw = new StringWriter(sb);

            using (var vimClient = new VimPortTypeClient(binding, epa))
            {
                //var serviceCertificate = vimClient.ClientCredentials.ServiceCertificate;
                //serviceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
                //serviceCertificate.Authentication.CustomCertificateValidator = new MyX509CertificateValidator(null);
                //serviceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;

                //var clientCertificate = vimClient.ClientCredentials.ClientCertificate;

                try
                {
                    ManagedObjectReference morServiceInstance = new ManagedObjectReference()
                    {
                        @type = "ServiceInstance",
                        Value = "ServiceInstance"
                    };
                    ServiceContent sc = vimClient.RetrieveServiceContent(morServiceInstance);

                    vmSummaryInfo.VmHost.ThumbPrint = StaticHelpers.FormatThumbPrint(certVerify.Thumbprint);

                    sb.AppendFormat("{0}, {1}, {2}, {3}, {4}", epa.Uri, inParam.User, inParam.Password, vmSummaryInfo.VmHost.ThumbPrint, certVerify.Thumbprint);
                    sb.AppendLine();
                    //StaticHelpers.PrintServerAboutInfo(sb, sc.about);
                    sc.about.Dump(nameof(sc.about), sw, false);

                    Console.WriteLine("{0}", sb.ToString());
                    sb.Clear();


                    using (MyCollectionDisposable collectionDisposal = new MyCollectionDisposable())
                    {
                        UserSession userSession = vimClient.Login(sc.sessionManager, inParam.User, inParam.Password, null);
                        collectionDisposal.Add(() => vimClient.Logout(sc.sessionManager));

                        //bool bListAllVMs = false;
                        //if(bListAllVMs)
                        if (inParam.ToListAllVMs)
                        {
                            CListVMs listVMs = new CListVMs(vimClient, sc.propertyCollector);
                            listVMs.list_all_vms(sc);
                        }


                        VimPropCollector pc = new VimPropCollector(vimClient, sc.propertyCollector);

                        VimFinder vimFinder = new VimFinder(vimClient, sc.propertyCollector, sc.rootFolder);

                        List <ManagedObjectReference> morDatacenters = new List <ManagedObjectReference>();
                        vimFinder.FindDatacenters(morDatacenters);

                        foreach (var datacenter in morDatacenters)
                        {
                            try
                            {
                                ManagedObjectReference morVM = (ManagedObjectReference)vimClient.FindByDatastorePath(sc.searchIndex, datacenter, inParam.VmPath);
                                if (null != morVM)
                                {
                                    vmSummaryInfo.VmHost.VmxSpec = "moref=" + morVM.Value;

                                    VirtualMachineConfigInfo virtualMachineConfigInfo = (VirtualMachineConfigInfo)pc.Get(morVM, "config");
                                    //StaticHelpers.PrintMor(sb, morVM);
                                    morVM.Dump(nameof(morVM), sw, false);
                                    //StaticHelpers.PrintVMCI(sb, virtualMachineConfigInfo);
                                    StaticHelpers.PrintVirtualDisksBriefInfo(sb, virtualMachineConfigInfo, vmSummaryInfo.VmHost.CurrentDiskPath);
                                    virtualMachineConfigInfo.Dump(nameof(virtualMachineConfigInfo), sw, false);
                                    Console.WriteLine("{0}", sb.ToString());
                                    sb.Clear();

                                    VimSnapshotInfo si = new VimSnapshotInfo(vimClient, sc.propertyCollector, morVM, sb, vmSummaryInfo.Snapshots);
                                    si.Traverse();
                                    Console.WriteLine("{0}", sb.ToString());
                                    sb.Clear();

                                    //datastore ManagedObjectReference:Datastore[] datastore-983 (datastore_on_30_1T)
                                    var datastore = (ManagedObjectReference[])pc.Get(morVM, "datastore");
                                    if (null != datastore)
                                    {
                                        var datastoreName = from item in datastore
                                                            select new { Datastore = item, DatastoreName = (string)pc.Get(item, "name") };
                                        foreach (var item in datastoreName)
                                        {
                                            sb.AppendLine();
                                            //StaticHelpers.PrintMor(sb, item.Datastore);
                                            item.Datastore.Dump(nameof(item.Datastore), sw, false);
                                            sb.AppendFormat("{0}\t\"{1}\"", nameof(item.DatastoreName), item.DatastoreName);
                                            sb.AppendLine();
                                        }
                                    }
                                    //guest GuestInfo guest
                                    var guest = (GuestInfo)pc.Get(morVM, "guest");
                                    //StaticHelpers.PrintGuestInfo(sb, guest);
                                    guest.Dump(nameof(guest), sw, false);
                                    //layout VirtualMachineFileLayout layout
                                    var layout = (VirtualMachineFileLayout)pc.Get(morVM, "layout");
                                    //StaticHelpers.PrintVirtualMachineFileLayout(sb, layout);
                                    layout.Dump(nameof(layout), sw, false);
                                    //layoutEx VirtualMachineFileLayoutEx layoutEx
                                    var layoutEx = (VirtualMachineFileLayoutEx)pc.Get(morVM, "layoutEx");
                                    //StaticHelpers.PrintVirtualMachineFileLayoutEx(sb, layoutEx);
                                    layoutEx.Dump(nameof(layoutEx), sw, false);
                                    //name string "shuli02-vc60"
                                    var name = (string)pc.Get(morVM, "name");
                                    sb.AppendFormat("{0}\t\"{1}\"", nameof(name), name);
                                    //network ManagedObjectReference:Network[] network-822(VM Network)
                                    var network = (ManagedObjectReference[])pc.Get(morVM, "network");
                                    if (null != network)
                                    {
                                        foreach (var Network in network)
                                        {
                                            //StaticHelpers.PrintMor(sb, Network);
                                            Network.Dump(nameof(Network), sw, false);
                                        }
                                    }

                                    //parent ManagedObjectReference:Folder group-v3 (vm)
                                    var parent = (ManagedObjectReference)pc.Get(morVM, "parent");
                                    //StaticHelpers.PrintMor(sb, parent);
                                    parent.Dump(nameof(parent), sw, false);

                                    //resourceConfig ResourceConfigSpec resourceConfig
                                    var resourceConfig = (ResourceConfigSpec)pc.Get(morVM, "resourceConfig");
                                    //StaticHelpers.PrintResourceConfigSpec(sb, resourceConfig);
                                    resourceConfig.Dump(nameof(resourceConfig), sw, false);

                                    //resourcePool ManagedObjectReference:ResourcePool resgroup-980 (VC)
                                    var resourcePool = (ManagedObjectReference)pc.Get(morVM, "resourcePool");
                                    //StaticHelpers.PrintMor(sb, resourcePool);
                                    resourcePool.Dump(nameof(resourcePool), sw, false);

                                    //runtime VirtualMachineRuntimeInfo runtime
                                    var runtime = (VirtualMachineRuntimeInfo)pc.Get(morVM, "runtime");
                                    //StaticHelpers.PrintVirtualMachineRuntimeInfo(sb, runtime);
                                    runtime.Dump(nameof(runtime), sw, false);

                                    string runtimeHostName = (string)pc.Get(runtime.host, "name");
                                    ManagedObjectReference morRuntimehostParent = (ManagedObjectReference)pc.Get(runtime.host, "parent");
                                    string runtimeHostParentName = (string)pc.Get(morRuntimehostParent, "name");
                                    //StaticHelpers.PrintMor(sb, runtime.host);
                                    runtime.host.Dump(nameof(runtime.host), sw, false);
                                    sb.AppendFormat("{0}\t\"{1}\"", nameof(runtimeHostName), runtimeHostName); sb.AppendLine();
                                    sb.AppendLine();
                                    //StaticHelpers.PrintMor(sb, morRuntimehostParent);
                                    morRuntimehostParent.Dump(nameof(morRuntimehostParent), sw, false);
                                    sb.AppendFormat("{0}\t\"{1}\"", nameof(runtimeHostParentName), runtimeHostParentName); sb.AppendLine();
                                    sb.AppendLine();

                                    //storage VirtualMachineStorageInfo storage
                                    var storage = (VirtualMachineStorageInfo)pc.Get(morVM, "storage");
                                    //StaticHelpers.PrintVirtualMachineStorageInfo(sb, storage);
                                    storage.Dump(nameof(storage), sw, false);
                                    Console.WriteLine("{0}", sb.ToString());
                                    sb.Clear();

                                    //summary VirtualMachineSummary summary
                                    var summary = (VirtualMachineSummary)pc.Get(morVM, "summary");
                                    //StaticHelpers.PrintVirtualMachineSummary(sb, summary);
                                    summary.Dump(nameof(summary), sw, false);


                                    Console.WriteLine("{0}", sb.ToString());
                                    sb.Clear();

                                    /////////////////////////////////////////////////////////////////////////////////////////////
                                    sb.Append('~', 80); sb.AppendLine();
                                    sb.Append('~', 80); sb.AppendLine();
                                    vmSummaryInfo.Dump(nameof(vmSummaryInfo), sw, false);
                                    sb.Append('~', 80); sb.AppendLine();
                                    sb.Append('~', 80); sb.AppendLine();
                                    Console.WriteLine("{0}", sb.ToString());
                                    sb.Clear();

                                    /////////////////////////////////////////////////////////////////////////////////////////////
                                    bool testVmOperation = false;
                                    if (testVmOperation)
                                    {
                                        try
                                        {
                                            MyVMOperation vmop = new MyVMOperation(vimClient, sc.propertyCollector, morVM);

                                            TaskInfoState state = vmop.ConsolidateVMDisks();

                                            string snapshotid1 = null;
                                            state = vmop.CreateSnapshot("test snapshot name 1", "test snapshot description1 ", false, true, out snapshotid1);
                                            VirtualDisk[] VirtualDisks1 = vmop.QuerySnapshotVirtualDisks(snapshotid1);


                                            string snapshotid2 = null;
                                            state = vmop.CreateSnapshot("test snapshot name 2", "test snapshot description2 ", false, true, out snapshotid2);
                                            VirtualDisk[] VirtualDisks2 = vmop.QuerySnapshotVirtualDisks(snapshotid2);



                                            try
                                            {
                                                foreach (var vdisk in VirtualDisks1)
                                                {
                                                    long startOffset = 0;
                                                    long diskSize    = vdisk.capacityInBytes == 0 ? vdisk.capacityInKB * 1024 : vdisk.capacityInBytes;
                                                    while (startOffset < diskSize)
                                                    {
                                                        DiskChangeInfo diskChangeInfo = vmop.QueryChangedDiskAreas(snapshotid2, vdisk, startOffset);
                                                        if (null != diskChangeInfo.changedArea)
                                                        {
                                                            foreach (var changedArea in diskChangeInfo.changedArea)
                                                            {
                                                                changedArea.Dump(nameof(changedArea), sw, false);
                                                            }
                                                            Console.WriteLine("{0}", sb.ToString());
                                                            sb.Clear();
                                                        }
                                                        startOffset = diskChangeInfo.startOffset + diskChangeInfo.length;
                                                    }
                                                }
                                            }
                                            catch (Exception exQueryChangedDiskAreas)
                                            {
                                                StaticHelpers.PrintException(exQueryChangedDiskAreas, 0);
                                            }

                                            state = vmop.RemoveSnapshot(snapshotid2, false, true);
                                            state = vmop.RemoveSnapshot(snapshotid1, false, true);
                                        }
                                        catch (Exception exTestVmOperation)
                                        {
                                            StaticHelpers.PrintException(exTestVmOperation, 0);
                                        }
                                    }

                                    /////////////////////////////////////////////////////////////////////////////////////////////
                                    bool testGustOp = false;
                                    if (testGustOp)
                                    {
                                        try
                                        {
                                            string            guestUser     = "******";
                                            string            guestPassword = "******";
                                            VimGuestOperation vimGuestOp    = new VimGuestOperation(vimClient, sc.propertyCollector, sc.guestOperationsManager, morVM, guestUser, guestPassword);

                                            string[] vars = vimGuestOp.ReadEnvironmentVariableInGuest(new string[] { "path" });

                                            GuestProgramSpec guestProgramSpec = new GuestProgramSpec()
                                            {
                                                programPath      = @"c:\Windows\notepad.exe",
                                                arguments        = "",
                                                workingDirectory = @"c:\",
                                                envVariables     = null
                                            };
                                            long pid = vimGuestOp.StartProgramInGuest(guestProgramSpec);
                                            GuestProcessInfo[] gpi = vimGuestOp.ListProcessesInGuest(new long[] { pid });
                                            vimGuestOp.TerminateProcessInGuest(pid);


                                            string guestFilePath = @"E:\1.vhdx";
                                            string localFile     = @"E:\~temp\1.vhdx";
                                            vimGuestOp.FileTransferFromGuest(guestFilePath, localFile);
                                            string guestFilePath2 = @"e:\111\1.vhdx";
                                            vimGuestOp.FileTransferToGuest(guestFilePath2, localFile, true);
                                        }
                                        catch (Exception exGust)
                                        {
                                            StaticHelpers.PrintException(exGust, 0);
                                        }
                                    }
                                }
                            }
                            catch (Exception exFind)
                            {
                                StaticHelpers.PrintException(exFind, 0);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    StaticHelpers.PrintException(ex, 0);
                }
            }

            return(0);
        }