Skip to content

Commit

Permalink
bt: add tf-in-automation flag, if set, it will use a separate TF_DATA…
Browse files Browse the repository at this point in the history
…_DIR per workspace
  • Loading branch information
DavidGamba committed Nov 13, 2024
1 parent 69a97f3 commit 69c2945
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bt/stack/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func BuildCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt
opt.Bool("serial", false)
opt.Bool("show", false, opt.Description("Show Terraform plan"))
opt.Bool("lock", false, opt.Description("Run 'terraform providers lock' after init"))
opt.Bool("tf-in-automation", false, opt.Description(`Determine if we are running in automation.
It will use a separate TF_DATA_DIR per workspace.`), opt.GetEnv("TF_IN_AUTOMATION"), opt.GetEnv("BT_IN_AUTOMATION"))
opt.String("profile", "default", opt.Description("BT Terraform Profile to use"), opt.GetEnv(cfg.Config.TerraformProfileEnvVar))
opt.Int("parallelism", 10*runtime.GOMAXPROCS(0), opt.Description("Pass through to Terraform -parallelism flag"))
opt.Int("stack-parallelism", runtime.GOMAXPROCS(0), opt.Description("Max number of stack components to run in parallel"))
Expand Down Expand Up @@ -89,6 +91,7 @@ func BuildRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
nopt.Bool("no-checks", opt.Value("no-checks").(bool))
nopt.Bool("show", opt.Value("show").(bool))
nopt.Bool("lock", opt.Value("lock").(bool))
nopt.Bool("tf-in-automation", opt.Value("tf-in-automation").(bool))
nopt.String("profile", opt.Value("profile").(string))
nopt.String("color", opt.Value("color").(string))
nopt.String("ws", ws)
Expand Down
4 changes: 4 additions & 0 deletions bt/stack/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func InitCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt
opt.Bool("lock", false, opt.Description("Run 'terraform providers lock' after init"))
opt.String("profile", "default", opt.Description("BT Terraform Profile to use"), opt.GetEnv(cfg.Config.TerraformProfileEnvVar))
opt.Int("stack-parallelism", 1, opt.Description("Max number of stack components to run in parallel"))
opt.Bool("tf-in-automation", false, opt.Description(`Determine if we are running in automation.
It will use a separate TF_DATA_DIR per workspace.`), opt.GetEnv("TF_IN_AUTOMATION"), opt.GetEnv("BT_IN_AUTOMATION"))

return opt
}
Expand Down Expand Up @@ -59,8 +61,10 @@ func InitRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
nopt := getoptions.New()
nopt.Bool("dry-run", opt.Value("dry-run").(bool))
nopt.Bool("ignore-cache", opt.Value("ignore-cache").(bool))
nopt.Bool("tf-in-automation", opt.Value("tf-in-automation").(bool))
nopt.String("profile", opt.Value("profile").(string))
nopt.String("color", opt.Value("color").(string))
nopt.String("ws", ws)

return terraform.InitRun(ctx, nopt, args)
}
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func applyCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt

func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
automation := opt.Value("tf-in-automation").(bool)
parallelism := opt.Value("parallelism").(int)
ws := opt.Value("ws").(string)
profile := opt.Value("profile").(string)
Expand Down Expand Up @@ -73,6 +74,9 @@ func applyRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
}
cmd = append(cmd, args...)
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
if ws != "" && automation {
dataDir = fmt.Sprintf("%s-%s", dataDir, ws)
}
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
if ws != "" {
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestApply(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.String("ws", "dev")
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestApply(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.String("ws", "dev")
Expand Down Expand Up @@ -125,6 +127,7 @@ func TestApply(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "dev")
opt.String("ws", "dev")
Expand Down Expand Up @@ -165,6 +168,7 @@ func TestApply(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "prod")
opt.String("ws", "prod")
Expand Down
1 change: 1 addition & 0 deletions bt/terraform/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestBuild(t *testing.T) {
opt.Bool("apply", false)
opt.Bool("show", false)
opt.Bool("lock", false)
opt.Bool("tf-in-automation", false)
opt.StringSlice("var", 1, 1)
opt.StringSlice("var-file", 1, 1)
opt.StringSlice("target", 1, 99)
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func postChecksRun(ctx context.Context, opt *getoptions.GetOpt, args []string) e
varFiles := opt.Value("var-file").([]string)
ws := opt.Value("ws").(string)
ignoreCache := opt.Value("ignore-cache").(bool)
automation := opt.Value("tf-in-automation").(bool)
nc := opt.Value("no-checks").(bool)
if nc {
Logger.Printf("WARNING: no-checks flag passed. Skipping post-apply checks.\n")
Expand Down Expand Up @@ -320,6 +321,9 @@ func postChecksRun(ctx context.Context, opt *getoptions.GetOpt, args []string) e
}

dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
if ws != "" && automation {
dataDir = fmt.Sprintf("%s-%s", dataDir, ws)
}
Logger.Printf("export %s\n", dataDir)

for _, cmd := range cfg.TFProfile[cfg.Profile(profile)].PostApplyChecks.Commands {
Expand Down
22 changes: 22 additions & 0 deletions bt/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,27 @@ func InitRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
profile := opt.Value("profile").(string)
color := opt.Value("color").(string)
ignoreCache := opt.Value("ignore-cache").(bool)
automation := opt.Value("tf-in-automation").(bool)
ws := opt.Value("ws").(string)

cfg := config.ConfigFromContext(ctx)
dir := DirFromContext(ctx)
LogConfig(cfg, profile)
os.Setenv("CONFIG_ROOT", cfg.ConfigRoot)

ws, err := updateWSIfSelected(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile), ws)
if err != nil {
return err
}

if cfg.TFProfile[cfg.Profile(profile)].Workspaces.Enabled {
if automation && !workspaceSelected(cfg.Config.DefaultTerraformProfile, profile) {
if ws == "" {
return fmt.Errorf("running in workspace mode in automation but no workspace selected or --ws given")
}
}
}

lockFile := ".terraform.lock.hcl"
initFile := ".tf.init"
files, modified, err := fsmodtime.Target(os.DirFS(dir), []string{initFile}, []string{lockFile})
Expand Down Expand Up @@ -67,6 +82,13 @@ func InitRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
}
cmd = append(cmd, args...)
dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
if ws != "" {
wsEnv := fmt.Sprintf("TF_WORKSPACE=%s", ws)
Logger.Printf("export %s\n", wsEnv)
if automation {
dataDir = fmt.Sprintf("%s-%s", dataDir, ws)
}
}
Logger.Printf("export %s\n", dataDir)
err = run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun).Run()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions bt/terraform/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("ignore-cache", false)
opt.Bool("tf-in-automation", false)
opt.String("profile", "default")
opt.String("color", "auto")
opt.String("ws", "")
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down Expand Up @@ -84,8 +86,10 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("ignore-cache", false)
opt.Bool("tf-in-automation", false)
opt.String("profile", "default")
opt.String("color", "auto")
opt.String("ws", "")
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down Expand Up @@ -124,8 +128,10 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("ignore-cache", false)
opt.Bool("tf-in-automation", false)
opt.String("profile", "dev")
opt.String("color", "auto")
opt.String("ws", "")
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down Expand Up @@ -163,8 +169,10 @@ func TestInit(t *testing.T) {
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("ignore-cache", false)
opt.Bool("tf-in-automation", false)
opt.String("profile", "prod")
opt.String("color", "auto")
opt.String("ws", "")
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
destroy := opt.Value("destroy").(bool)
detailedExitcode := opt.Value("detailed-exitcode").(bool)
ignoreCache := opt.Value("ignore-cache").(bool)
automation := opt.Value("tf-in-automation").(bool)
parallelism := opt.Value("parallelism").(int)
varFiles := opt.Value("var-file").([]string)
variables := opt.Value("var").([]string)
Expand Down Expand Up @@ -189,6 +190,9 @@ func planRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
cmd = append(cmd, args...)

dataDir := fmt.Sprintf("TF_DATA_DIR=%s", getDataDir(cfg.Config.DefaultTerraformProfile, cfg.Profile(profile)))
if ws != "" && automation {
dataDir = fmt.Sprintf("%s-%s", dataDir, ws)
}
Logger.Printf("export %s\n", dataDir)
ri := run.CMDCtx(ctx, cmd...).Stdin().Log().Env(dataDir).Dir(dir).DryRun(dryRun)
if ws != "" {
Expand Down
4 changes: 4 additions & 0 deletions bt/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestPlan(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.Bool("destroy", false)
Expand Down Expand Up @@ -92,6 +93,7 @@ func TestPlan(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "default")
opt.Bool("destroy", false)
Expand Down Expand Up @@ -138,6 +140,7 @@ func TestPlan(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "dev")
opt.Bool("destroy", false)
Expand Down Expand Up @@ -184,6 +187,7 @@ func TestPlan(t *testing.T) {
ctx = run.ContextWithRunInfo(ctx, mock)
opt := getoptions.New()
opt.Bool("dry-run", false)
opt.Bool("tf-in-automation", false)
opt.Int("parallelism", 10)
opt.String("profile", "prod")
opt.Bool("destroy", false)
Expand Down
2 changes: 2 additions & 0 deletions bt/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewCommand(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetO

opt := parent.NewCommand("terraform", "terraform related tasks")
opt.String("profile", "default", opt.Description("BT Terraform Profile to use"), opt.GetEnv(cfg.Config.TerraformProfileEnvVar))
opt.Bool("tf-in-automation", false, opt.Description(`Determine if we are running in automation.
It will use a separate TF_DATA_DIR per workspace.`), opt.GetEnv("TF_IN_AUTOMATION"), opt.GetEnv("BT_IN_AUTOMATION"))

wss, err := validWorkspaces(cfg, opt.Value("profile").(string))
if err != nil {
Expand Down

0 comments on commit 69c2945

Please sign in to comment.