Exemple #1
0
        private STT.Task <string> RunTask(TaskEntity taskToExecute, ResultEntity resultEntity, CancellationToken ct)
        {
            STT.Task <string> task = null;
            switch (taskToExecute.ApiUrl)
            {
            case "stackstorm":     // _stackStormService
            {
                switch (taskToExecute.Action)
                {
                case TaskAction.guest_file_read:
                {
                    task = STT.Task.Run(() => _stackStormService.GuestReadFile(resultEntity.InputString));
                    break;
                }

                case TaskAction.guest_file_upload_content:
                {
                    task = STT.Task.Run(() => _stackStormService.GuestFileUploadContent(resultEntity.InputString));
                    break;
                }

                case TaskAction.guest_process_run:
                {
                    task = STT.Task.Run(() => _stackStormService.GuestCommand(resultEntity.InputString));
                    break;
                }

                case TaskAction.guest_process_run_fast:
                {
                    task = STT.Task.Run(() => _stackStormService.GuestCommandFast(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_hw_power_on:
                {
                    task = STT.Task.Run(() => _stackStormService.VmPowerOn(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_hw_power_off:
                {
                    task = STT.Task.Run(() => _stackStormService.VmPowerOff(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_create_from_template:
                {
                    task = STT.Task.Run(() => _stackStormService.CreateVmFromTemplate(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_hw_remove:
                {
                    task = STT.Task.Run(() => _stackStormService.VmRemove(resultEntity.InputString));
                    break;
                }

                default:
                {
                    var message = $"Stackstorm Action {taskToExecute.Action} has not been implemented.";
                    _logger.LogError(message);
                    resultEntity.Status     = Data.TaskStatus.failed;
                    resultEntity.StatusDate = DateTime.UtcNow;
                    break;
                }
                }
                break;
            }

            case "vm":     // _playerVmService
            {
                switch (taskToExecute.Action)
                {
                case TaskAction.guest_file_write:
                default:
                {
                    var message = $"Player VM API Action {taskToExecute.Action} has not been implemented.";
                    _logger.LogError(message);
                    resultEntity.Status     = Data.TaskStatus.failed;
                    resultEntity.StatusDate = DateTime.UtcNow;
                    break;
                }
                }
                break;
            }

            case "player":
            case "caster":
            default:
            {
                var message = $"API ({taskToExecute.ApiUrl}) is not currently implemented";
                _logger.LogError(message);
                throw new NotImplementedException(message);
            }
            }

            return(task);
        }
Exemple #2
0
        private async Task <Data.TaskStatus> ExecuteStackstormTaskAsync(DispatchTaskEntity dispatchTaskToExecute, List <DispatchTaskResultEntity> resultEntityList, CancellationToken ct)
        {
            var overallStatus = Data.TaskStatus.succeeded;

            foreach (var resultEntity in resultEntityList)
            {
                Task <string> task = null;
                resultEntity.InputString = resultEntity.InputString.Replace("VmGuid", "Moid").Replace("{moid}", resultEntity.VmId.ToString());
                resultEntity.VmName      = _stackStormService.GetVmName((Guid)resultEntity.VmId);
                switch (dispatchTaskToExecute.Action)
                {
                case TaskAction.guest_file_read:
                {
                    task = Task.Run(() => _stackStormService.GuestReadFile(resultEntity.InputString));
                    break;
                }

                case TaskAction.guest_process_run:
                {
                    task = Task.Run(() => _stackStormService.GuestCommand(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_hw_power_on:
                {
                    task = Task.Run(() => _stackStormService.VmPowerOn(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_hw_power_off:
                {
                    task = Task.Run(() => _stackStormService.VmPowerOff(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_create_from_template:
                {
                    task = Task.Run(() => _stackStormService.CreateVmFromTemplate(resultEntity.InputString));
                    break;
                }

                case TaskAction.vm_hw_remove:
                {
                    task = Task.Run(() => _stackStormService.VmRemove(resultEntity.InputString));
                    break;
                }

                default:
                {
                    var message = $"Task Action {dispatchTaskToExecute.Action} has not been implemented.";
                    _logger.LogError(message);
                    resultEntity.Status     = Data.TaskStatus.failed;
                    resultEntity.StatusDate = DateTime.UtcNow;
                    break;
                }
                }

                if (task.Wait(TimeSpan.FromSeconds(resultEntity.ExpirationSeconds)))
                {
                    resultEntity.ActualOutput = task.Result.ToString();
                    resultEntity.Status       = ProcessDispatchTaskResult(resultEntity, ct);
                }
                else
                {
                    resultEntity.ActualOutput = task.Result.ToString();
                    resultEntity.Status       = Data.TaskStatus.expired;
                }
                resultEntity.StatusDate = DateTime.UtcNow;
                if (resultEntity.Status != Data.TaskStatus.succeeded)
                {
                    if (overallStatus != Data.TaskStatus.failed)
                    {
                        overallStatus = resultEntity.Status;
                    }
                }
            }
            await _context.SaveChangesAsync();

            return(overallStatus);
        }