-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcfd3c4
commit 69a97f3
Showing
5 changed files
with
94 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters