Пример #1
0
        public async Task <Vm> Start(string id)
        {
            await Connect();

            Vm vm = _vmCache[id];

            _logger.LogDebug($"Starting vm {vm.Name}");
            if (vm.State != VmPowerState.Running)
            {
                ManagedObjectReference task = await _vim.PowerOnVM_TaskAsync(vm.AsVim(), null);

                TaskInfo info = await WaitForVimTask(task);

                vm.State = (info.state == TaskInfoState.success)
                    ? VmPowerState.Running
                    : vm.State;
                if (vm.State != VmPowerState.Running)
                {
                    throw new Exception(info.error.localizedMessage);
                }

                //apply guestinfo for annotations
                await ReconfigureVm(id, "guest", "", "");
            }

            _vmCache.TryUpdate(vm.Id, vm, vm);
            return(vm);
        }
Пример #2
0
        public async Task <string> PowerOnVm(Guid uuid)
        {
            _logger.LogDebug($"Power on vm {uuid} requested");

            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);

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

            try
            {
                task = await _client.PowerOnVM_TaskAsync(vmReference, null);

                // TaskInfo info = await WaitForVimTask(task);
                // if (info.state == TaskInfoState.success) {
                //     state = "started";
                // }
                // else
                // {
                //     throw new Exception(info.error.localizedMessage);
                // }

                state = "poweron submitted";
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, $"Failed to send power on " + uuid);
                state = "poweron error";
            }

            state = "poweron submitted";

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

            return(state);
        }