Skip to content

Commit

Permalink
feat: add pre and post execute variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nehemming committed Aug 23, 2021
1 parent e97fb44 commit aa71760
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 51 deletions.
2 changes: 2 additions & 0 deletions pkg/builtin/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (runType) Prepare(ctx context.Context, capComm *rocket.CapComm, task rocket

// Setup command
cmd := exec.Command(commandLine.ProgramPath, commandLine.Args...)

// get the environment variables in the correct for for the exec command.
cmd.Env = capComm.GetExecEnv()

// Check not cancelled
Expand Down
17 changes: 13 additions & 4 deletions pkg/rocket/capcomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,19 @@ func (capComm *CapComm) Log() loggee.Logger {

// ExportVariable exports the passed variable to all capComm's sharing the same parent as the receiver.
func (capComm *CapComm) ExportVariable(key, value string) *CapComm {
// Save the value in the local and export lists
capComm.variables.Set(key, value)
capComm.exportTo.Set(key, value)
return capComm
}

// SetLocalVariable sets a local capComm's variable.
func (capComm *CapComm) SetLocalVariable(key, value string) *CapComm {
// Save the value in the local and export lists
capComm.variables.Set(key, value)
return capComm
}

// ExportVariables exports a list of variables if set in the capComm to aa parent. If the variable is not found
// the exported name will be checked against the params and exported.
func (capComm *CapComm) ExportVariables(exports Exports) {
Expand All @@ -251,8 +260,10 @@ func (capComm *CapComm) ExportVariables(exports Exports) {
}

func (capComm *CapComm) getTemplateVariables() map[string]string {
// use all exported variables
m := capComm.variables.All()

// include any local variables not already exported
for k, v := range capComm.exportTo.All() {
if _, ok := m[k]; !ok {
m[k] = v
Expand Down Expand Up @@ -283,7 +294,7 @@ func (capComm *CapComm) WithMission(mission *Mission) *CapComm {
}

// MergeBasicEnvMap adds environment variables into an unsealed CapComm.
func (capComm *CapComm) MergeBasicEnvMap(env EnvMap) *CapComm {
func (capComm *CapComm) MergeBasicEnvMap(env VarMap) *CapComm {
capComm.mustNotBeSealed()

if env == nil {
Expand Down Expand Up @@ -631,9 +642,7 @@ func (capComm *CapComm) MergeParams(ctx context.Context, params []Param) error {
}

// MergeTemplateEnvs adds params into an unsealed CapComm instance.
func (capComm *CapComm) MergeTemplateEnvs(ctx context.Context, env EnvMap) error {
capComm.mustNotBeSealed()

func (capComm *CapComm) MergeTemplateEnvs(ctx context.Context, env VarMap) error {
// Envs need to be expanded prior to merging
expanded := make(map[string]string)

Expand Down
22 changes: 11 additions & 11 deletions pkg/rocket/capcomm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func TestWithMission(t *testing.T) {
func TestMergeBasicEnvMap(t *testing.T) {
capComm := NewCapComm(testMissionFile, stdlog.New())

envMap := make(EnvMap)
envMap := make(VarMap)

capComm.MergeBasicEnvMap(nil)

Expand Down Expand Up @@ -453,7 +453,7 @@ func TestAddFileResource(t *testing.T) { //nolint -- keep as one test
}

func TestAddFileResourceExpansion(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["something"] = "io"
ctx := context.Background()
capComm := NewCapComm(testMissionFile, stdlog.New()).MergeBasicEnvMap(envMap)
Expand Down Expand Up @@ -730,7 +730,7 @@ func TestAttachRedirectOutputExpand(t *testing.T) {
os.Setenv("TEST_ENV_CAPCOMM", "99")
defer os.Unsetenv("TEST_ENV_CAPCOMM")

envMap := make(EnvMap)
envMap := make(VarMap)
envMap["something"] = "here"

ctx := context.Background()
Expand Down Expand Up @@ -765,7 +765,7 @@ func TestAttachRedirectErrorExpand(t *testing.T) {
os.Setenv("TEST_ENV_CAPCOMM", "99")
defer os.Unsetenv("TEST_ENV_CAPCOMM")

envMap := make(EnvMap)
envMap := make(VarMap)
envMap["something"] = "here"

ctx := context.Background()
Expand Down Expand Up @@ -837,7 +837,7 @@ func TestAttachRedirectInExpand(t *testing.T) {
}

func TestAttachRedirectInExpandNotSet(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["something"] = "here"

ctx := context.Background()
Expand Down Expand Up @@ -917,7 +917,7 @@ func TestMergeParams(t *testing.T) {
}

func TestMergeParamsWithFile(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["name"] = "io"

ctx := context.Background()
Expand All @@ -940,7 +940,7 @@ func TestMergeParamsWithFile(t *testing.T) {
}

func TestMergeParamsWithOptionalFile(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["name"] = "notaconfig"

ctx := context.Background()
Expand All @@ -964,7 +964,7 @@ func TestMergeParamsWithOptionalFile(t *testing.T) {
}

func TestMergeParamsWithSkipTemplate(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["name"] = "notaconfig"

ctx := context.Background()
Expand All @@ -988,7 +988,7 @@ func TestMergeParamsWithSkipTemplate(t *testing.T) {
}

func TestMergeParamsWithParam(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["name"] = "config"

ctx := context.Background()
Expand Down Expand Up @@ -1033,7 +1033,7 @@ func TestMergeTemplateEnvs(t *testing.T) {
ctx := context.Background()
capComm := NewCapComm(testMissionFile, stdlog.New())

envMap := make(EnvMap)
envMap := make(VarMap)

capComm.MergeBasicEnvMap(nil)

Expand Down Expand Up @@ -1073,7 +1073,7 @@ func TestGetExecEnvNoOSInherit(t *testing.T) {
}

func TestGetExecEnv(t *testing.T) {
envMap := make(EnvMap)
envMap := make(VarMap)
envMap["TEST_ENV_CAPCOMM"] = "99"

execEnv := newCapCommFromEnvironment(getTestMissionFile(), stdlog.New()).
Expand Down
31 changes: 21 additions & 10 deletions pkg/rocket/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ type (

// BasicEnv is a map of additional environment variables
// They are not template expanded
BasicEnv EnvMap `mapstructure:"basicEnv"`
BasicEnv VarMap `mapstructure:"basicEnv"`

// Env is a map of additional environment variables
// These are subject to template expansion after the params have been expanded
Env EnvMap `mapstructure:"env"`
Env VarMap `mapstructure:"env"`

// Must is a slice of params that must be defined prior to the mission starting
// Iif any are missing the mission will fail.
Expand Down Expand Up @@ -133,8 +133,8 @@ type (
Value string `mapstructure:"value"`
}

// EnvMap is a map of environment variables to their values.
EnvMap map[string]string
// VarMap is a map of variables to their values.
VarMap map[string]string

// Stage is a collection of tasks that can share a common set of parameters.
// All tasks within a stage are executed sequently.
Expand All @@ -151,7 +151,7 @@ type (

// BasicEnv is a map of additional environment variables
// They are not template expanded
BasicEnv EnvMap `mapstructure:"basicEnv"`
BasicEnv VarMap `mapstructure:"basicEnv"`

// Condition if present is evaluated prior to running a stage. If the condition template expression evaluates to true/yes/1 the
// stage will be run. If the template is blank or non true value the stage will not be run and the step will be skipped.
Expand All @@ -162,7 +162,7 @@ type (

// Env is a map of additional environment variables
// These are subject to template expansion after the params have been expanded.
Env EnvMap `mapstructure:"env"`
Env VarMap `mapstructure:"env"`

// Filter is an optional filter on the stage.
// If the filter criteria are not met the stage will not be executed.
Expand Down Expand Up @@ -201,7 +201,7 @@ type (

// BasicEnv is a map of additional environment variables.
// They are not template expanded.
BasicEnv EnvMap `mapstructure:"basicEnv"`
BasicEnv VarMap `mapstructure:"basicEnv"`

// Concurrent is a list of tasks to execute concurrently.
Concurrent Tasks `mapstructure:"concurrent"`
Expand All @@ -221,7 +221,7 @@ type (

// Env is a map of additional environment variables.
// These are subject to template expansion after the params have been expanded.
Env EnvMap `mapstructure:"env"`
Env VarMap `mapstructure:"env"`

// Export is a list of variables to export. This list can be used by try and group task types
// to export their variables (output from sub tasks) to their parent stage or task.
Expand Down Expand Up @@ -249,8 +249,19 @@ 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 and Env variables are template expanded during the preparation phase of a mission. That means their
// values are calculated prior to any task running in any stage.
// If values need to be calculated before or after a run use pre or post variaBLES
Params Params `mapstructure:"params"`

// PostVars are variable evaluated after a task has run
// Post variable are automatically exported to the parent task/stage.
PostVars VarMap `mapstructure:"postvars"`

// PreVars are variables calculated immediately prior to a run. They are are not exported to the parent context.
// If the variable needs to be used by other tasks it should be explicitly exported (See Export above).
PreVars VarMap `mapstructure:"prevars"`

// Try is a list of tasks to try.
Try Tasks `mapstructure:"try"`

Expand Down Expand Up @@ -368,8 +379,8 @@ func (l *Include) Validate() error {
}

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

0 comments on commit aa71760

Please sign in to comment.