diff --git a/cmd/apex/infra/infra.go b/cmd/apex/infra/infra.go new file mode 100644 index 0000000..c99fb0a --- /dev/null +++ b/cmd/apex/infra/infra.go @@ -0,0 +1,44 @@ +// Package infra proxies Terraform commands. +package infra + +import ( + "github.com/spf13/cobra" + + "github.com/apex/apex/cmd/apex/root" + "github.com/apex/apex/infra" +) + +var infraEnvironment string + +// example output. +const example = ` View change plan + $ apex infra plan + + Apply changes + $ apex infra apply` + +// Command config. +var Command = &cobra.Command{ + Use: "infra", + Short: "Infrastructure management", + Example: example, + RunE: run, +} + +// Initialize. +func init() { + root.Register(Command) +} + +// Run command. +func run(c *cobra.Command, args []string) error { + if err := root.Project.LoadFunctions(); err != nil { + return err + } + + p := &infra.Proxy{ + Project: root.Project, + } + + return p.Run(args...) +} diff --git a/cmd/apex/main.go b/cmd/apex/main.go index 10ea95e..d399a23 100644 --- a/cmd/apex/main.go +++ b/cmd/apex/main.go @@ -7,15 +7,16 @@ import ( "github.com/apex/apex/cmd/apex/root" // commands + _ "github.com/apex/apex/cmd/apex/build" + _ "github.com/apex/apex/cmd/apex/delete" _ "github.com/apex/apex/cmd/apex/deploy" + _ "github.com/apex/apex/cmd/apex/docs" + _ "github.com/apex/apex/cmd/apex/infra" _ "github.com/apex/apex/cmd/apex/invoke" - _ "github.com/apex/apex/cmd/apex/delete" - _ "github.com/apex/apex/cmd/apex/rollback" - _ "github.com/apex/apex/cmd/apex/build" _ "github.com/apex/apex/cmd/apex/list" - _ "github.com/apex/apex/cmd/apex/metrics" _ "github.com/apex/apex/cmd/apex/logs" - _ "github.com/apex/apex/cmd/apex/docs" + _ "github.com/apex/apex/cmd/apex/metrics" + _ "github.com/apex/apex/cmd/apex/rollback" _ "github.com/apex/apex/cmd/apex/upgrade" _ "github.com/apex/apex/cmd/apex/version" diff --git a/infra/infra.go b/infra/infra.go new file mode 100644 index 0000000..e5b08aa --- /dev/null +++ b/infra/infra.go @@ -0,0 +1,61 @@ +// Package infra proxies Terraform commands. +package infra + +import ( + "fmt" + "os" + "os/exec" + + "github.com/apex/log" + + "github.com/apex/apex/project" +) + +// Proxy is a wrapper around Terraform commands. +type Proxy struct { + Project *project.Project +} + +// Run terraform command in infrastructure directory. +func (p *Proxy) Run(args ...string) error { + if p.shouldInjectVars(args) { + args = append(args, p.functionVars()...) + } + + log.WithFields(log.Fields{ + "args": args, + }).Debug("terraform") + + cmd := exec.Command("terraform", args...) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = "infrastructure" + + return cmd.Run() +} + +// functionVars returns the function ARN's as terraform -var arguments. +func (p *Proxy) functionVars() (args []string) { + for _, fn := range p.Project.Functions { + config, err := fn.GetConfig() + if err != nil { + log.Debugf("can't fetch function config: %s", err.Error()) + continue + } + + args = append(args, "-var") + args = append(args, fmt.Sprintf("apex_function_%s=%s", fn.Name, *config.Configuration.FunctionArn)) + } + + return args +} + +// shouldInjectVars checks if the command accepts -var flags. +func (p *Proxy) shouldInjectVars(args []string) bool { + if len(args) == 0 { + return false + } + + return args[0] == "plan" || args[0] == "apply" +}