Skip to content

Commit

Permalink
feat: add onfail stage & tasks and support task refs
Browse files Browse the repository at this point in the history
  • Loading branch information
nehemming committed Aug 23, 2021
1 parent 7b838a7 commit 674afd6
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 46 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"mitchellh",
"nehemming",
"nolint",
"onfail",
"runbook",
"testdata",
"yamlformatter"
Expand Down
2 changes: 1 addition & 1 deletion pkg/rocket/assemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

// Assemble locates a blueprint from the assembly sources, loads the runbook and builds the assembly following the blueprint.
func (mc *missionControl) Assemble(ctx context.Context, blueprintName string, sources []string, runbook string, params []Param) error {
func (mc *missionControl) Assemble(ctx context.Context, blueprintName string, sources []string, runbook string, params Params) error {
// blueprint, if not abs need to search sources to locate
// once blueprint found extract it
blueprint, blueprintLocation, err := mc.searchSources(ctx, blueprintName, sources, mc.missionLog())
Expand Down
87 changes: 80 additions & 7 deletions pkg/rocket/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type (
// Mission name, defsaults to the config file name
Name string `mapstructure:"name"`

// Description is a free text description of the mission.
Description string `mapstructure:"description"`

// Additional contains any additional parameters specified in the
// configuration. They are included in the template data set
// but will be overridden by any other duplicate keys
Expand All @@ -56,7 +59,7 @@ type (
// Params is a collection of parameters that can be used within
// the child stages. Parameters are template expanded and can use
// Environment variables defined in Env
Params []Param `mapstructure:"params"`
Params Params `mapstructure:"params"`

// Sequences specify a list of stages to run.
// If no sequences are provides all stages are run in the order they are defined.
Expand All @@ -66,7 +69,10 @@ type (
// Stages represents the stages of the mission
// If no sequences are included in the file all stages are executed in ordinal order.
// If a sequence is included in the mission file the launch mission call must specify the sequence to use.
Stages []Stage `mapstructure:"stages"`
Stages Stages `mapstructure:"stages"`

// OnFail is a stage that is executed if the mission fails.
OnFail *Stage `mapstructure:"onfail"`

// Version of the mission definition
Version string `mapstructure:"version"`
Expand All @@ -76,6 +82,15 @@ type (
// The list is checked prior too processing the activities own set of param definitions.
MustHaveParams []string

// Stages is a slice of stages.
Stages []*Stage

// Tasks is a slice of tasks
Tasks []Task

// Params is a slice of params
Params []Param

// Param is an expandible parameter.
Param struct {
// Name is the name of the parameter.
Expand Down Expand Up @@ -125,6 +140,12 @@ type (
// If it is not provided it default to the ordinal ID of the stage within the mission
Name string `mapstructure:"name"`

// Stage is impmented by another named stage.
Ref string `mapstructure:"ref"`

// Description is a free text description of the stage.
Description string `mapstructure:"description"`

// BasicEnv is a map of additional environment variables
// They are not template expanded
BasicEnv EnvMap `mapstructure:"basicEnv"`
Expand Down Expand Up @@ -152,14 +173,17 @@ type (
// Params is a collection of parameters that can be used within
// the child stages. Parameters are template expanded and can use
// Environment variables defined in Env
Params []Param `mapstructure:"params"`
Params Params `mapstructure:"params"`

// Tasks is a collection of one or more tasks to complete
// Tasks are executed sequentally
Tasks []Task `mapstructure:"tasks"`
Tasks Tasks `mapstructure:"tasks"`

// OnFail is a task that is executed if the stage fails.
OnFail *Task `mapstructure:"onfail"`

// Try to run the stage but if it fails do no abort the whole run
Try bool `mapstructure:"try"`
Try string `mapstructure:"try"`
}

// Task is an activity that is executed.
Expand All @@ -172,6 +196,12 @@ type (
// If it is not provided it default to the ordinal ID of the task within the stage
Name string `mapstructure:"name"`

// Task is impmented by another named task in the same stage.
Ref string `mapstructure:"ref"`

// Description is a free text description of the task.
Description string `mapstructure:"description"`

// Definition contains the additional data required to process the task type
Definition map[string]interface{} `mapstructure:",remain"`

Expand Down Expand Up @@ -202,10 +232,10 @@ type (
// Params is a collection of parameters that can be used within
// the child stages. Parameters are template expanded and can use
// Environment variables defined in Env
Params []Param `mapstructure:"params"`
Params Params `mapstructure:"params"`

// Try to run the task but if it fails do no abort the whole run
Try bool `mapstructure:"try"`
Try string `mapstructure:"try"`
}

// Filter restricts running an activity
Expand Down Expand Up @@ -324,3 +354,46 @@ func (l *Include) Validate() error {

return nil
}

// Copy copies an environment map.
func (em EnvMap) Copy() EnvMap {
c := make(EnvMap)
for k, v := range em {
c[k] = v
}
return c
}

// Copy copies the must have params.
func (params MustHaveParams) Copy() MustHaveParams {
c := make(MustHaveParams, len(params))
copy(c, params)
return c
}

// Copy the params.
func (params Params) Copy() Params {
c := make(Params, len(params))
copy(c, params)
return c
}

// Copy the tasks.
func (tasks Tasks) Copy() Tasks {
c := make(Tasks, len(tasks))
copy(c, tasks)
return c
}

// ToMap converts the task slice to a map using the task name as the key.
func (tasks Tasks) ToMap() TaskMap {
c := make(TaskMap)

for _, v := range tasks {
if v.Name != "" {
c[v.Name] = &v
}
}

return c
}
Loading

0 comments on commit 674afd6

Please sign in to comment.