Skip to content

Commit

Permalink
Merge pull request #290 from apex/add-infra
Browse files Browse the repository at this point in the history
add infra command
  • Loading branch information
tj committed Mar 1, 2016
2 parents 3d1304a + 318b125 commit 7436597
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
44 changes: 44 additions & 0 deletions cmd/apex/infra/infra.go
Original file line number Diff line number Diff line change
@@ -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...)
}
11 changes: 6 additions & 5 deletions cmd/apex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
61 changes: 61 additions & 0 deletions infra/infra.go
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 7436597

Please sign in to comment.