Пример #1
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));
        }
Пример #2
0
        /// <summary>
        /// Runs a guest process in the specified virtual machine.
        /// </summary>
        /// <param name="vmObject">The <see cref="VSphereManagedObject" /> representing the virtual machine.</param>
        /// <param name="fileName">The file name of the application to run.</param>
        /// <param name="arguments">The command-line arguments to pass to the application when the process starts.</param>
        /// <param name="credential">The credential to run the process as.</param>
        /// <param name="waitForExit">if set to <c>true</c> wait for the process to exit before returning.</param>
        /// <returns>The ID of the guest process that was started.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="vmObject" /> is null.
        /// <para>or</para>
        /// <paramref name="credential" /> is null.
        /// </exception>
        public long RunVirtualMachineGuestProcess(VSphereManagedObject vmObject, string fileName, string arguments, NetworkCredential credential, bool waitForExit)
        {
            if (vmObject == null)
            {
                throw new ArgumentNullException(nameof(vmObject));
            }

            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            VSpherePropertySpec  spec = new VSpherePropertySpec(VSphereManagedObjectType.GuestOperationsManager, "processManager");
            VSphereManagedObject guestOperationsManager = RetrieveObject(new VSphereManagedObject(_serviceContent.guestOperationsManager), spec);
            var processManager = (ManagedObjectReference)guestOperationsManager.Properties["processManager"];

            NamePasswordAuthentication auth = new NamePasswordAuthentication
            {
                username           = BuildUserName(credential),
                password           = credential.Password,
                interactiveSession = true
            };
            GuestProgramSpec guestProgram = new GuestProgramSpec
            {
                programPath = fileName,
                arguments   = arguments
            };

            long processId = _vimService.StartProgramInGuest(processManager, vmObject.ManagedObjectReference, auth, guestProgram);

            if (waitForExit)
            {
                while (true)
                {
                    var processInfo = _vimService.ListProcessesInGuest(processManager, vmObject.ManagedObjectReference, auth, new[] { processId }).FirstOrDefault();
                    if (processInfo?.exitCodeSpecified == true)
                    {
                        break;
                    }
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }

            return(processId);
        }
Пример #3
0
        /// <summary>
        /// Retrieves a set of vSphere managed objects using the specified <see cref="VSphereSelectionSpec" /> and <see cref="VSpherePropertySpec" />.
        /// </summary>
        /// <param name="startingObject">The <see cref="VSphereManagedObject" /> from which the query should start.</param>
        /// <param name="selectionSpec">The <see cref="VSphereSelectionSpec" /> that specifies which objects will be queried.</param>
        /// <param name="propertySpec">The <see cref="VSpherePropertySpec" /> that specifies which object properties will be retrieved.</param>
        /// <returns>A collection of <see cref="VSphereManagedObject" />.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="startingObject" /> is null.
        /// <para>or</para>
        /// <paramref name="selectionSpec" /> is null.
        /// <para>or</para>
        /// <paramref name="propertySpec" /> is null.
        /// </exception>
        public IEnumerable <VSphereManagedObject> RetrieveObjects(VSphereManagedObject startingObject, VSphereSelectionSpec selectionSpec, VSpherePropertySpec propertySpec)
        {
            if (propertySpec == null)
            {
                throw new ArgumentNullException(nameof(propertySpec));
            }

            return(RetrieveObjects(startingObject, selectionSpec, new[] { propertySpec }));
        }
Пример #4
0
        /// <summary>
        /// Retrieves a vSphere managed object with updated property values using the specified <see cref="VSpherePropertySpec" />.
        /// </summary>
        /// <param name="managedObject">The <see cref="VSphereManagedObject" /> to retrieve.</param>
        /// <param name="propertySpec">The <see cref="VSpherePropertySpec" /> that specifies which object properties will be retrieved.</param>
        /// <returns>A <see cref="VSphereManagedObject" />.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="managedObject" /> is null.
        /// <para>or</para>
        /// <paramref name="propertySpec" /> is null.
        /// </exception>
        public VSphereManagedObject RetrieveObject(VSphereManagedObject managedObject, VSpherePropertySpec propertySpec)
        {
            if (managedObject == null)
            {
                throw new ArgumentNullException(nameof(managedObject));
            }

            if (propertySpec == null)
            {
                throw new ArgumentNullException(nameof(propertySpec));
            }

            ObjectSpec objectSpec = new ObjectSpec
            {
                obj = managedObject.ManagedObjectReference
            };

            PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec
            {
                objectSet = new[] { objectSpec },
                propSet   = new[] { propertySpec.PropertySpec }
            };

            var result = _vimService.RetrieveProperties(_serviceContent.propertyCollector, new[] { propertyFilterSpec });

            return(new VSphereManagedObject(result.Single()));
        }
Пример #5
0
 /// <summary>
 /// Retrieves a set of vSphere managed objects using the specified <see cref="VSphereSelectionSpec" /> and <see cref="VSpherePropertySpec" />.
 /// </summary>
 /// <param name="selectionSpec">The <see cref="VSphereSelectionSpec" /> that specifies which objects will be queried.</param>
 /// <param name="propertySpec">The <see cref="VSpherePropertySpec" /> that specifies which object properties will be retrieved.</param>
 /// <returns>A collection of <see cref="VSphereManagedObject" />.</returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="selectionSpec" /> is null.
 /// <para>or</para>
 /// <paramref name="propertySpec" /> is null.
 /// </exception>
 public IEnumerable <VSphereManagedObject> RetrieveObjects(VSphereSelectionSpec selectionSpec, VSpherePropertySpec propertySpec)
 {
     return(RetrieveObjects(new VSphereManagedObject(_serviceContent.rootFolder), selectionSpec, propertySpec));
 }