Example #1
0
        public async Task <string> PowerOffVm(Guid uuid)
        {
            ManagedObjectReference vmReference = null;
            ManagedObjectReference task;
            string state = null;

            vmReference = await GetVm(uuid);

            if (vmReference == null)
            {
                _logger.LogDebug($"Could not get vm reference");
                return(state);
            }

            state = await GetPowerState(uuid);

            _logger.LogDebug($"Power off vm {uuid} requested");

            if (state == "off")
            {
                state = "already off";
                _logger.LogDebug($"Returning state: {state}");
                return(state);
            }

            try
            {
                task = await _client.PowerOffVM_TaskAsync(vmReference);

                // TaskInfo info = await WaitForVimTask(task);
                // if (info.state == TaskInfoState.success) {
                //     State = VmPowerState.off;
                //     state = "stopped";
                // }
                // else
                // {
                //     throw new Exception(info.error.localizedMessage);
                // }
                state = "poweroff submitted";
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, $"Failed to send power off " + uuid);
                state = "poweroff error";
            }

            _logger.LogDebug($"Returning state: {state}");

            return(state);
        }
Example #2
0
        public async Task <Vm> Stop(string id)
        {
            await Connect();

            Vm vm = _vmCache[id];

            _logger.LogDebug($"Stopping vm {vm.Name}");
            if (vm.State == VmPowerState.Running)
            {
                ManagedObjectReference task = await _vim.PowerOffVM_TaskAsync(vm.AsVim());

                TaskInfo info = await WaitForVimTask(task);

                vm.State = (info.state == TaskInfoState.success)
                    ? VmPowerState.Off
                    : vm.State;
                if (vm.State == VmPowerState.Running)
                {
                    throw new Exception(info.error.localizedMessage);
                }
            }
            _vmCache.TryUpdate(vm.Id, vm, vm);
            return(vm);
        }