Esempio n. 1
0
        private async Task ExecuteAsync(Configurations.Client.Instance instance, Execution execution, CancellationToken cancellationToken)
        {
            var template = await this.apiClient.GetTemplateAsync(execution.TemplateId, cancellationToken);

            var variables = execution.VariableSetId == default
                ? new VariableSet()
                : await GetVariablesAsync(execution.VariableSetId, execution.VariableOverrides, cancellationToken);

            // Builtin variables
            variables.Values.Add(nameof(execution.Instance.Tenant), execution.Instance.Tenant);
            variables.Values.Add(nameof(execution.Instance.Environment), execution.Instance.Environment);

            var i = 1;

            foreach (var step in template.Steps.OrderBy(x => x.Order))
            {
                var logs = new List <string> {
                    $"Starting step {step.Order}: '{step.Name}'"
                };

                var command = string.IsNullOrWhiteSpace(step.Command)
                    ? ""
                    : Substitute(step.Command, variables.Values);

                var folder = Substitute(step.WorkingFolder, variables.Values);
                folder = Path.GetFullPath(folder, Path.GetFullPath(this.instanceOptions?.Value?.DefaultFolder ?? "."));
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                    logs.Add($"Folder: '{folder}' created.");
                }
                logs.Add($"Executing at folder: '{folder}'");
                var result = step.Type switch
                {
                    Template.StepType.Archive => await UnpackArchiveAsync(step.FileName, folder, variables.Values),
                    Template.StepType.CMD => await this.runner.RunCMDAsync(folder, command),
                    Template.StepType.PShell => await this.runner.RunPowerShellAsync(folder, command),
                    Template.StepType.Docker => await this.runner.RunDockerAsync(folder, command),
                    Template.StepType.DockerCompose => await this.runner.RunComposeAsync(folder, command),
                    _ => throw new NotImplementedException(),
                };

                if (result.isSuccessed)
                {
                    logs.AddRange(result.logs);
                }
                else
                {
                    await this.logSender.LogError(execution.Id, $"Step '{step.Name}' failed.", step.Order, true, logs : result.logs.ToList(), cancellationToken : cancellationToken);

                    break;
                }

                logs.Add($"Step {step.Order} - '{step.Name}' finished");

                await this.logSender.LogStep(execution.Id, step.Order, i == template.Steps.Count, logs, cancellationToken : cancellationToken);

                i++;
            }
        }
Esempio n. 2
0
 public async Task ExecuteAsync(Configurations.Client.Instance instance, long executionId, CancellationToken cancellationToken)
 {
     try
     {
         var execution = await this.apiClient.GetExecutionAsync(executionId, cancellationToken).ConfigureAwait(false);
         await ExecuteAsync(instance, execution, cancellationToken);
     }
     catch (Exception e)
     {
         await this.logSender.LogError(executionId, "Failed to execute", isLastStep : true, error : e, cancellationToken : cancellationToken);
     }
 }