diff options
| author | Melodi <62381002+Melodi17@users.noreply.github.com> | 2026-09-13 13:22:20 +1000 |
|---|---|---|
| committer | Melodi <62381002+Melodi17@users.noreply.github.com> | 2026-09-13 13:22:20 +1000 |
| commit | df0737e55afeb5c01eb39909605970d1d3a73ffc (patch) | |
| tree | 09823eaa310c60eefd2420dc305acae45c81900f /src/marlin.core/models | |
| parent | ee69ce25b2fba871920260028bb10c2d6b62b26f (diff) | |
refactor: cleanup; feat: connection to agent
Diffstat (limited to 'src/marlin.core/models')
34 files changed, 495 insertions, 120 deletions
diff --git a/src/marlin.core/models/Agent.cs b/src/marlin.core/models/Agent.cs deleted file mode 100644 index 2903131..0000000 --- a/src/marlin.core/models/Agent.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace marlin.core.models; - -public class Agent -{ - public Guid Id { get; set; } - - public string Name { get; set; } - public int MaxConcurrentJobs { get; set; } - public DateTime LastSeen { get; set; } - - public CapabilitySet Capabilities { get; set; } -}
\ No newline at end of file diff --git a/src/marlin.core/models/AgentStatus.cs b/src/marlin.core/models/AgentStatus.cs new file mode 100644 index 0000000..393062f --- /dev/null +++ b/src/marlin.core/models/AgentStatus.cs @@ -0,0 +1,4 @@ +namespace marlin.core.models; + +public record AgentStatus(CapabilitySet CapabilitySet); +public record AgentConfiguration(string Name, int MaxConcurrentJobs);
\ No newline at end of file diff --git a/src/marlin.core/models/CapabilitySet.cs b/src/marlin.core/models/CapabilitySet.cs index ac7da46..553d746 100644 --- a/src/marlin.core/models/CapabilitySet.cs +++ b/src/marlin.core/models/CapabilitySet.cs @@ -3,4 +3,11 @@ namespace marlin.core.models; public class CapabilitySet { public Dictionary<string, string> Items { get; set; } = new(); +} + +public static class KnownCapabilities +{ + public const string CoreCount = "cores"; + public const string Memory = "memory"; + public const string Architecture = "architecture"; }
\ No newline at end of file diff --git a/src/marlin.core/models/DispatchJob.cs b/src/marlin.core/models/DispatchJob.cs new file mode 100644 index 0000000..4070447 --- /dev/null +++ b/src/marlin.core/models/DispatchJob.cs @@ -0,0 +1,9 @@ +namespace marlin.core.models; + +public record DispatchJob( + Guid Id, + string Token, + string Vcs, + string Ref, + string Commit, + string Recipe); diff --git a/src/marlin.core/models/Job.cs b/src/marlin.core/models/Job.cs deleted file mode 100644 index a8c7c5d..0000000 --- a/src/marlin.core/models/Job.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace marlin.core.models; - -public class Job -{ - public Guid Id { get; set; } - - public Guid SourceId { get; set; } - public VersionControlSource Source { get; set; } - public string Ref { get; set; } - public string Commit { get; set; } - - public string CreatedReason { get; set; } - public DateTime CreatedAt { get; set; } - public JobStatus Status { get; set; } - public ICollection<JobStatusUpdate> StatusUpdates { get; set; } - - public Guid? AgentId { get; set; } - public Agent? Agent { get; set; } - - public string Recipe { get; set; } - // plan -} - -public class JobStatusUpdate -{ - public Guid Id { get; set; } - - public Guid JobId { get; set; } - public Job Job { get; set; } - - public DateTime At { get; set; } - public JobStatus Status { get; set; } -} - -public enum JobStatus -{ - Pending, - Active, - Cancelled, - Success, - Failed -}
\ No newline at end of file diff --git a/src/marlin.core/models/SecretKind.cs b/src/marlin.core/models/SecretKind.cs deleted file mode 100644 index 02652d5..0000000 --- a/src/marlin.core/models/SecretKind.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace marlin.web.Models; - -public static class SecretKind -{ - public const string HTTPToken = "http-token"; - public const string SSHKey = "ssh-key"; -}
\ No newline at end of file diff --git a/src/marlin.core/models/VersionControlSource.cs b/src/marlin.core/models/VersionControlSource.cs deleted file mode 100644 index daa8d51..0000000 --- a/src/marlin.core/models/VersionControlSource.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace marlin.core.models; - -public class VersionControlSource -{ - public Guid Id { get; set; } - - public string UpstreamUrl { get; set; } - public string MainBranch { get; set; } - public string BranchFilter { get; set; } - public int MinPollingInterval { get; set; } - - public Guid? AccessSecretId { get; set; } - - public ICollection<VersionControlledBranch> Branches { get; set; } -}
\ No newline at end of file diff --git a/src/marlin.core/models/VersionControlledBlob.cs b/src/marlin.core/models/VersionControlledBlob.cs deleted file mode 100644 index 03915be..0000000 --- a/src/marlin.core/models/VersionControlledBlob.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace marlin.core.models; - -public class VersionControlledBlob -{ - public Guid Id { get; set; } - - public Guid SourceId { get; set; } - public VersionControlSource Source { get; set; } - - public Guid CommitId { get; set; } - public VersionControlledCommit Commit { get; set; } - - public string Path { get; set; } - public byte[] Content { get; set; } -}
\ No newline at end of file diff --git a/src/marlin.core/models/VersionControlledBranch.cs b/src/marlin.core/models/VersionControlledBranch.cs deleted file mode 100644 index 601f527..0000000 --- a/src/marlin.core/models/VersionControlledBranch.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace marlin.core.models; - -public class VersionControlledBranch -{ - public Guid Id { get; set; } - - public Guid SourceId { get; set; } - public VersionControlSource Source { get; set; } - - public Guid CommitId { get; set; } - public VersionControlledCommit Commit { get; set; } - - public string Name { get; set; } -}
\ No newline at end of file diff --git a/src/marlin.core/models/VersionControlledCommit.cs b/src/marlin.core/models/VersionControlledCommit.cs deleted file mode 100644 index a7b5e9e..0000000 --- a/src/marlin.core/models/VersionControlledCommit.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace marlin.core.models; - -public class VersionControlledCommit -{ - public Guid Id { get; set; } - - public Guid SourceId { get; set; } - public VersionControlSource Source { get; set; } - public bool BlobsFetched { get; set; } - - public string Hash { get; set; } - - public ICollection<VersionControlledBranch> Branches { get; set; } - public ICollection<VersionControlledBlob> Blobs { get; set; } -}
\ No newline at end of file diff --git a/src/marlin.core/models/meta/AbstractStep.cs b/src/marlin.core/models/meta/AbstractStep.cs new file mode 100644 index 0000000..7d10643 --- /dev/null +++ b/src/marlin.core/models/meta/AbstractStep.cs @@ -0,0 +1,16 @@ +namespace marlin.core.models.meta; + +public abstract class AbstractStep +{ + protected AbstractStep(string id) + { + this.Id = id; + } + + public string Id { get; } + public string? Name { get; set; } = null; + public string? Run { get; set; } = null; + public Dictionary<string, string> With { get; set; } = new(); + public FailBehavior? Fail { get; set; } = null; + public abstract IEnumerable<AbstractStep> GetAncestorsAndThis(Workflow workflow); +} diff --git a/src/marlin.core/models/meta/Artifact.cs b/src/marlin.core/models/meta/Artifact.cs new file mode 100644 index 0000000..dce25a6 --- /dev/null +++ b/src/marlin.core/models/meta/Artifact.cs @@ -0,0 +1,26 @@ +namespace marlin.core.models.meta; + +public class Artifact +{ + public Artifact(string id) + { + this.Id = id; + } + + public string Id { get; } + public string Path { get; set; } + public string Type { get; set; } + public ArtifactHandle Handle { get; set; } = ArtifactHandle.Defer; + + public bool RequiresOrchestratorUpload() + { + return this.Type?.StartsWith("test/") == true || this.Type?.StartsWith("logs/") == true; + } +} + +public enum ArtifactHandle +{ + Immediate, + Step, + Defer +}
\ No newline at end of file diff --git a/src/marlin.core/models/meta/ArtifactHandler.cs b/src/marlin.core/models/meta/ArtifactHandler.cs new file mode 100644 index 0000000..51b6519 --- /dev/null +++ b/src/marlin.core/models/meta/ArtifactHandler.cs @@ -0,0 +1,12 @@ +namespace marlin.core.models.meta; + +public class ArtifactHandler +{ + public ArtifactHandler(string id) + { + this.Id = id; + } + + public string Id { get; } + public ArtifactHandlerStep? Handle { get; set; } +} diff --git a/src/marlin.core/models/meta/ArtifactHandlerStep.cs b/src/marlin.core/models/meta/ArtifactHandlerStep.cs new file mode 100644 index 0000000..bfcc9b9 --- /dev/null +++ b/src/marlin.core/models/meta/ArtifactHandlerStep.cs @@ -0,0 +1,14 @@ +namespace marlin.core.models.meta; + +public class ArtifactHandlerStep : AbstractStep +{ + public ArtifactHandlerStep(string id) : base(id) + { + } + + public CleanupStep? Cleanup { get; set; } = null; + public override IEnumerable<AbstractStep> GetAncestorsAndThis(Workflow workflow) + { + yield return this; + } +} diff --git a/src/marlin.core/models/meta/CleanupAfter.cs b/src/marlin.core/models/meta/CleanupAfter.cs new file mode 100644 index 0000000..0ca8f5a --- /dev/null +++ b/src/marlin.core/models/meta/CleanupAfter.cs @@ -0,0 +1,7 @@ +namespace marlin.core.models.meta; + +public enum CleanupAfter +{ + Step, + Recipe +}
\ No newline at end of file diff --git a/src/marlin.core/models/meta/CleanupStep.cs b/src/marlin.core/models/meta/CleanupStep.cs new file mode 100644 index 0000000..b2d895b --- /dev/null +++ b/src/marlin.core/models/meta/CleanupStep.cs @@ -0,0 +1,14 @@ +namespace marlin.core.models.meta; + +public class CleanupStep : AbstractStep +{ + public CleanupStep(string id) : base(id) + { + } + + public CleanupAfter After { get; set; } + public override IEnumerable<AbstractStep> GetAncestorsAndThis(Workflow workflow) + { + yield return this; + } +}
\ No newline at end of file diff --git a/src/marlin.core/models/meta/FailBehavior.cs b/src/marlin.core/models/meta/FailBehavior.cs new file mode 100644 index 0000000..6084f5d --- /dev/null +++ b/src/marlin.core/models/meta/FailBehavior.cs @@ -0,0 +1,8 @@ +namespace marlin.core.models.meta; + +public enum FailBehavior +{ + Continue, + Stop, + StopAfter +}
\ No newline at end of file diff --git a/src/marlin.core/models/meta/Recipe.cs b/src/marlin.core/models/meta/Recipe.cs new file mode 100644 index 0000000..33ed814 --- /dev/null +++ b/src/marlin.core/models/meta/Recipe.cs @@ -0,0 +1,36 @@ +namespace marlin.core.models.meta; + +public class Recipe +{ + public Recipe(string id) + { + this.Id = id; + } + + public string Id { get; } + public string Name { get; set; } + + public string? RunsOnId { get; set; } + public List<string> RequireIds { get; set; } = []; + public Dictionary<string, RecipeStep> Steps { get; set; } = new(); + public Dictionary<string, string> Parameters { get; set; } = new(); + public Dictionary<string, string> Environment { get; set; } = new(); + + public RequirementSet? GetRunsOn(Workflow workflow) => + this.RunsOnId != null ? workflow.Requirements[this.RunsOnId] : null; + + public IEnumerable<Recipe> GetRequires(Workflow workflow) => + this.RequireIds.Select(id => workflow.Recipes[id]); + + public Dictionary<string, string> CreateArguments() + { + Dictionary<string, string> args = new(); + foreach (var parameter in this.Parameters) + args.Add(parameter.Key, parameter.Value); + + foreach (var environment in this.Environment) + args.Add($"env.{environment.Key}", environment.Value); + + return args; + } +} diff --git a/src/marlin.core/models/meta/RecipeStep.cs b/src/marlin.core/models/meta/RecipeStep.cs new file mode 100644 index 0000000..afe77cf --- /dev/null +++ b/src/marlin.core/models/meta/RecipeStep.cs @@ -0,0 +1,28 @@ +namespace marlin.core.models.meta; + +public class RecipeStep : AbstractStep +{ + public RecipeStep(string id) : base(id) + { + } + + public string? BaseId { get; set; } + public CleanupStep? Cleanup { get; set; } + public Dictionary<string, Artifact> Artifacts { get; set; } = new(); + public List<string> Ingest { get; set; } = new(); + + public RecipeStep? GetBase(Workflow workflow) => + this.BaseId != null ? workflow.Steps[this.BaseId] : null; + + public override IEnumerable<AbstractStep> GetAncestorsAndThis(Workflow workflow) + { + yield return this; + + var step = this.GetBase(workflow); + while (step != null) + { + yield return step; + step = step.GetBase(workflow); + } + } +} diff --git a/src/marlin.core/models/meta/RequirementSet.cs b/src/marlin.core/models/meta/RequirementSet.cs new file mode 100644 index 0000000..13a3daa --- /dev/null +++ b/src/marlin.core/models/meta/RequirementSet.cs @@ -0,0 +1,13 @@ +namespace marlin.core.models.meta; + +public class RequirementSet +{ + public RequirementSet(string id) + { + this.Id = id; + } + + public string Id { get; } + public string? Base { get; set; } + public Dictionary<string, string> Has { get; set; } = new(); +} diff --git a/src/marlin.core/models/meta/Workflow.cs b/src/marlin.core/models/meta/Workflow.cs new file mode 100644 index 0000000..d7a200a --- /dev/null +++ b/src/marlin.core/models/meta/Workflow.cs @@ -0,0 +1,9 @@ +namespace marlin.core.models.meta; + +public class Workflow +{ + public Dictionary<string, Recipe> Recipes { get; set; } = new(); + public Dictionary<string, RecipeStep> Steps { get; set; } = new(); + public Dictionary<string, ArtifactHandler> ArtifactHandlers { get; set; } = new(); + public Dictionary<string, RequirementSet> Requirements { get; set; } = new(); +} diff --git a/src/marlin.core/models/meta/dto/ArtifactDto.cs b/src/marlin.core/models/meta/dto/ArtifactDto.cs new file mode 100644 index 0000000..aa2db76 --- /dev/null +++ b/src/marlin.core/models/meta/dto/ArtifactDto.cs @@ -0,0 +1,8 @@ +namespace marlin.core.models.meta.dto; + +public sealed record ArtifactDto +{ + public string? Path { get; init; } + public string? As { get; init; } + public ArtifactHandle? Handle { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/ArtifactHandlerDto.cs b/src/marlin.core/models/meta/dto/ArtifactHandlerDto.cs new file mode 100644 index 0000000..0e51a52 --- /dev/null +++ b/src/marlin.core/models/meta/dto/ArtifactHandlerDto.cs @@ -0,0 +1,6 @@ +namespace marlin.core.models.meta.dto; + +public sealed record ArtifactHandlerDto +{ + public StepDtoBase? Handle { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/CleanupStepDto.cs b/src/marlin.core/models/meta/dto/CleanupStepDto.cs new file mode 100644 index 0000000..606619b --- /dev/null +++ b/src/marlin.core/models/meta/dto/CleanupStepDto.cs @@ -0,0 +1,6 @@ +namespace marlin.core.models.meta.dto; + +public sealed record CleanupStepDto : StepDtoBase +{ + public CleanupAfter? After { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/RecipeDto.cs b/src/marlin.core/models/meta/dto/RecipeDto.cs new file mode 100644 index 0000000..5e311b9 --- /dev/null +++ b/src/marlin.core/models/meta/dto/RecipeDto.cs @@ -0,0 +1,11 @@ +namespace marlin.core.models.meta.dto; + +public sealed record RecipeDto +{ + public string? Name { get; init; } + public string? RunsOn { get; init; } + public List<string>? Requires { get; init; } + public Dictionary<string, StepDto>? Steps { get; init; } + public Dictionary<string, string>? Parameters { get; init; } + public Dictionary<string, string>? Environment { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/RequirementSetDto.cs b/src/marlin.core/models/meta/dto/RequirementSetDto.cs new file mode 100644 index 0000000..165ed66 --- /dev/null +++ b/src/marlin.core/models/meta/dto/RequirementSetDto.cs @@ -0,0 +1,7 @@ +namespace marlin.core.models.meta.dto; + +public sealed record RequirementSetDto +{ + public string? Base { get; init; } + public Dictionary<string, string>? Has { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/StepDto.cs b/src/marlin.core/models/meta/dto/StepDto.cs new file mode 100644 index 0000000..57e4092 --- /dev/null +++ b/src/marlin.core/models/meta/dto/StepDto.cs @@ -0,0 +1,9 @@ +namespace marlin.core.models.meta.dto; + +public sealed record StepDto : StepDtoBase +{ + public string? Base { get; init; } + public CleanupStepDto? Cleanup { get; init; } + public Dictionary<string, ArtifactDto>? Artifacts { get; init; } + public List<string>? Ingest { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/StepDtoBase.cs b/src/marlin.core/models/meta/dto/StepDtoBase.cs new file mode 100644 index 0000000..6f4e36e --- /dev/null +++ b/src/marlin.core/models/meta/dto/StepDtoBase.cs @@ -0,0 +1,9 @@ +namespace marlin.core.models.meta.dto; + +public record StepDtoBase +{ + public string? Name { get; init; } + public string? Run { get; init; } + public Dictionary<string, string>? With { get; init; } + public FailBehavior? Fail { get; init; } +} diff --git a/src/marlin.core/models/meta/dto/WorkflowFileDto.cs b/src/marlin.core/models/meta/dto/WorkflowFileDto.cs new file mode 100644 index 0000000..10997d6 --- /dev/null +++ b/src/marlin.core/models/meta/dto/WorkflowFileDto.cs @@ -0,0 +1,9 @@ +namespace marlin.core.models.meta.dto; + +public sealed record WorkflowFileDto +{ + public Dictionary<string, RecipeDto>? Recipes { get; init; } + public Dictionary<string, StepDto>? Steps { get; init; } + public Dictionary<string, RequirementSetDto>? Requirements { get; init; } + public Dictionary<string, ArtifactHandlerDto>? ArtifactHandlers { get; init; } +} diff --git a/src/marlin.core/models/plan/CommandResult.cs b/src/marlin.core/models/plan/CommandResult.cs new file mode 100644 index 0000000..3385e84 --- /dev/null +++ b/src/marlin.core/models/plan/CommandResult.cs @@ -0,0 +1,17 @@ +namespace marlin.core.models.plan; + +[Flags] +public enum CommandResult : byte +{ + Success = 1, + Failure = 2, + Unconditional = Success | Failure, +} + +public static class CommandResultExtensions +{ + public static bool Matches(this CommandResult self, CommandResult contains) + { + return (self & contains) != 0; + } +}
\ No newline at end of file diff --git a/src/marlin.core/models/plan/CommandResultSelector.cs b/src/marlin.core/models/plan/CommandResultSelector.cs new file mode 100644 index 0000000..92c41c7 --- /dev/null +++ b/src/marlin.core/models/plan/CommandResultSelector.cs @@ -0,0 +1,13 @@ +namespace marlin.core.models.plan; + +public class CommandResultSelector +{ + public ExecutionCommand Command { get; } + public CommandResult Result { get; } + + public CommandResultSelector(ExecutionCommand command, CommandResult result) + { + this.Command = command; + this.Result = result; + } +}
\ No newline at end of file diff --git a/src/marlin.core/models/plan/CommandRole.cs b/src/marlin.core/models/plan/CommandRole.cs new file mode 100644 index 0000000..8005532 --- /dev/null +++ b/src/marlin.core/models/plan/CommandRole.cs @@ -0,0 +1,8 @@ +namespace marlin.core.models.plan; + +public enum CommandRole +{ + Step = 0, + ArtifactHandler = 1, + Cleanup = 2 +}
\ No newline at end of file diff --git a/src/marlin.core/models/plan/ExecutionCommand.cs b/src/marlin.core/models/plan/ExecutionCommand.cs new file mode 100644 index 0000000..124da63 --- /dev/null +++ b/src/marlin.core/models/plan/ExecutionCommand.cs @@ -0,0 +1,15 @@ +namespace marlin.core.models.plan; + +using meta; + +public class ExecutionCommand +{ + public required string Id; + public required string Name; + public required string Command; + public required CommandRole Role; + public required bool Deferred; + public CommandResultSelector? ResultSelector; + + public required FailBehavior FailBehavior; +}
\ No newline at end of file diff --git a/src/marlin.core/models/plan/ExecutionPlan.cs b/src/marlin.core/models/plan/ExecutionPlan.cs new file mode 100644 index 0000000..24c40e4 --- /dev/null +++ b/src/marlin.core/models/plan/ExecutionPlan.cs @@ -0,0 +1,174 @@ +namespace marlin.core.models.plan; + +using meta; +using util; + +public class ExecutionPlan +{ + public IEnumerable<ExecutionCommand> Components => [..this._commands, ..this._deferredArtifactHandler, ..this._deferredCleanup]; + + private readonly List<ExecutionCommand> _commands; + private readonly List<ExecutionCommand> _deferredArtifactHandler; + private readonly List<ExecutionCommand> _deferredCleanup; + + public ExecutionPlan() + { + this._commands = new(); + this._deferredArtifactHandler = new(); + this._deferredCleanup = new(); + } + + private void AddCommand(ExecutionCommand command) + { + if (!command.Deferred) + this._commands.Add(command); + + else + { + switch (command.Role) + { + case CommandRole.Cleanup: + this._deferredCleanup.Add(command); + break; + + case CommandRole.ArtifactHandler: + this._deferredArtifactHandler.Add(command); + break; + + default: + throw new InvalidOperationException($"Cannot defer {command.Role}"); + } + } + } + + public static ExecutionPlan Create(Workflow workflow, Recipe recipe) + { + ExecutionPlan plan = new(); + + AddToExecutionPlan(plan, workflow, recipe, null); + + return plan; + } + + private static ExecutionCommand? AddToExecutionPlan(ExecutionPlan plan, Workflow workflow, Recipe recipe, ExecutionCommand? previousCommand) + { + foreach (var dependentRecipe in recipe.GetRequires(workflow)) + previousCommand = AddToExecutionPlan(plan, workflow, dependentRecipe, previousCommand); + + Dictionary<string, string> recipeArgs = recipe.CreateArguments(); + + foreach (var step in recipe.Steps.Values) + { + var command = CreateCommand(step, recipe, workflow, false, recipeArgs, out var commandArgs); + if (previousCommand != null) + { + command.ResultSelector = new CommandResultSelector(previousCommand, + previousCommand.FailBehavior switch + { + FailBehavior.Continue => CommandResult.Unconditional, + FailBehavior.Stop => CommandResult.Success, + FailBehavior.StopAfter => CommandResult.Success, + + _ => throw new ArgumentOutOfRangeException() + }); + } + + plan.AddCommand(command); + + var cleanupCommand = CreateCleanupCommand(step, command, recipe, workflow, commandArgs); + if (cleanupCommand != null) + plan.AddCommand(cleanupCommand); + + previousCommand = command; + } + + return previousCommand; + } + + private static ExecutionCommand CreateCommand( + AbstractStep step, + Recipe recipe, + Workflow workflow, + bool defer, + Dictionary<string, string> baseArgs, + out Dictionary<string, string> createdArgs) + { + string? name = null; + string? run = null; + FailBehavior? fail = null; + + Dictionary<string, string> args = new(baseArgs); + + foreach (var stepHierarchy in step.GetAncestorsAndThis(workflow)) + { + name ??= stepHierarchy.Name; + run ??= stepHierarchy.Run; + fail ??= stepHierarchy.Fail; + + foreach (var kvp in stepHierarchy.With) + args.TryAdd(kvp.Key, kvp.Value); + } + + name ??= step.Id; + fail ??= FailBehavior.Stop; + + if (run == null) + throw new Exception($"No run found for {recipe.Name}.{name}"); + + run = VariableExpander.Expand(run, args); + + ExecutionCommand stepCommand = new ExecutionCommand + { + Id = step.Id, + Name = name, + Command = run, + Role = step switch + { + ArtifactHandlerStep => CommandRole.ArtifactHandler, + RecipeStep => CommandRole.Step, + CleanupStep => CommandRole.Cleanup, + _ => throw new ArgumentOutOfRangeException() + }, + FailBehavior = fail.Value, + Deferred = defer + }; + + createdArgs = args; + return stepCommand; + } + + private static ExecutionCommand? CreateCleanupCommand(RecipeStep parentStep, + ExecutionCommand parentCommand, + Recipe recipe, + Workflow workflow, + Dictionary<string, string> baseArgs) + { + var cleanupStep = GetCleanup(parentStep, workflow); + if (cleanupStep == null) + return null; + + var cleanupCommand = CreateCommand(cleanupStep, recipe, workflow, + cleanupStep.After == CleanupAfter.Recipe, baseArgs, out _); + + cleanupCommand.ResultSelector = new CommandResultSelector(parentCommand, + parentCommand.FailBehavior switch + { + FailBehavior.Stop => CommandResult.Success, + FailBehavior.StopAfter => CommandResult.Unconditional, + FailBehavior.Continue => CommandResult.Unconditional, + + _ => throw new ArgumentOutOfRangeException() + }); + + return cleanupCommand; + } + + private static CleanupStep? GetCleanup(RecipeStep step, Workflow workflow) + { + return step + .GetAncestorsAndThis(workflow) + .OfType<RecipeStep>() + .Select(x => x.Cleanup) + .FirstOrDefault(x => x != null); + } +} |
