Skip to content

Commit

Permalink
bt: add stack init
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Nov 13, 2024
1 parent fcfd3c4 commit 69a97f3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
86 changes: 86 additions & 0 deletions bt/stack/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package stack

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/DavidGamba/dgtools/bt/config"
sconfig "github.com/DavidGamba/dgtools/bt/stack/config"
"github.com/DavidGamba/dgtools/bt/terraform"
"github.com/DavidGamba/go-getoptions"
)

func InitCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt {
cfg := config.ConfigFromContext(ctx)

opt := parent.NewCommand("init", "Runs terraform init on each component of the stack")
opt.SetCommandFn(InitRun)
opt.Bool("dry-run", false)
opt.Bool("ignore-cache", false, opt.Description("Ignore the cache and re-run the plan"), opt.Alias("ic"))
opt.Bool("serial", false)
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"))

return opt
}

func InitRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
id := opt.Value("id").(string)
serial := opt.Value("serial").(bool)
stackParallelism := opt.Value("stack-parallelism").(int)

if id == "" {
fmt.Fprintf(os.Stderr, "ERROR: missing stack id\n")
fmt.Fprint(os.Stderr, opt.Help(getoptions.HelpSynopsis))
return getoptions.ErrorHelpCalled
}

cfg := sconfig.ConfigFromContext(ctx)

wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}

wsFn := func(component, dir, ws string, variables []string) getoptions.CommandFn {
return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
ctx = terraform.NewComponentContext(ctx, fmt.Sprintf("%s:%s", component, ws))
ctx = terraform.NewStackContext(ctx, true)
d := filepath.Join(cfg.ConfigRoot, dir)
d, err = filepath.Rel(wd, d)
if err != nil {
return fmt.Errorf("failed to get relative path: %w", err)
}
ctx = terraform.NewDirContext(ctx, d)

nopt := getoptions.New()
nopt.Bool("dry-run", opt.Value("dry-run").(bool))
nopt.Bool("ignore-cache", opt.Value("ignore-cache").(bool))
nopt.String("profile", opt.Value("profile").(string))
nopt.String("color", opt.Value("color").(string))

return terraform.InitRun(ctx, nopt, args)
}
}

g, err := generateDAG(opt, id, cfg, true, wsFn)
if err != nil {
return err
}
g.SetMaxParallel(stackParallelism)
Logger.Printf("stack parallelism: %d\n", stackParallelism)

if serial {
g.SetSerial()
}

err = g.Run(ctx, opt, args)
if err != nil {
return fmt.Errorf("failed to run graph: %w", err)
}

return nil
}
1 change: 1 addition & 0 deletions bt/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewCommand(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetO
ConfigCMD(ctx, opt)
GraphCMD(ctx, opt)
BuildCMD(ctx, opt)
InitCMD(ctx, opt)
MirrorCMD(ctx, opt)
return opt
}
2 changes: 1 addition & 1 deletion bt/terraform/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func BuildRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error
}

tm := dag.NewTaskMap()
tm.Add("init", initRun)
tm.Add("init", InitRun)
if lock {
tm.Add("lock", lockFn)
}
Expand Down
4 changes: 2 additions & 2 deletions bt/terraform/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func initCMD(ctx context.Context, parent *getoptions.GetOpt) *getoptions.GetOpt
opt := parent.NewCommand("init", "")
opt.Bool("dry-run", false)
opt.Bool("ignore-cache", false, opt.Description("Ignore the cache and re-run the init"), opt.Alias("ic"))
opt.SetCommandFn(initRun)
opt.SetCommandFn(InitRun)
return opt
}

func initRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
func InitRun(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
dryRun := opt.Value("dry-run").(bool)
profile := opt.Value("profile").(string)
color := opt.Value("color").(string)
Expand Down
8 changes: 4 additions & 4 deletions bt/terraform/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestInit(t *testing.T) {
opt.Bool("ignore-cache", false)
opt.String("profile", "default")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestInit(t *testing.T) {
opt.Bool("ignore-cache", false)
opt.String("profile", "default")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestInit(t *testing.T) {
opt.Bool("ignore-cache", false)
opt.String("profile", "dev")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestInit(t *testing.T) {
opt.Bool("ignore-cache", false)
opt.String("profile", "prod")
opt.String("color", "auto")
err := initRun(ctx, opt, []string{})
err := InitRun(ctx, opt, []string{})
if err != nil {
t.Errorf("TestInit error: %s", err)
}
Expand Down

0 comments on commit 69a97f3

Please sign in to comment.