-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from apex/add-infra
add infra command
- Loading branch information
Showing
3 changed files
with
111 additions
and
5 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,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...) | ||
} |
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
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" | ||
} |