Skip to content

Commit

Permalink
Merge branch 'master' into mbarrien/no-apply-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking authored Oct 11, 2018
2 parents 7a2cd50 + f21da10 commit bea5067
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
before:
hooks:
- make clean
- make packr

builds:
- binary: fogg
env:
Expand Down
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ all: test install
setup:
go get github.com/rakyll/gotest
go install github.com/rakyll/gotest
go get -u github.com/gobuffalo/packr/...
go install github.com/gobuffalo/packr/packr

lint: ## run the fast go linters
gometalinter --vendor --fast ./...
Expand All @@ -17,11 +19,15 @@ lint-slow: ## run all linters, even the slow ones
gometalinter --vendor --deadline 120s ./...

packr: ## run the packr tool to generate our static files
packr
packr clean -v
packr -v

release: packr
release: ## run a release
goreleaser release --rm-dist

release-snapshot: ## run a release
goreleaser release --rm-dist --snapshot

build: packr ## build the binary
go build ${LDFLAGS} .

Expand All @@ -37,4 +43,10 @@ install: packr ## install the fogg binary in $GOPATH/bin
help: ## display help for this makefile
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: build coverage test install lint lint-slow packr release help
clean: ## clean the repo
rm fogg 2>/dev/null || true
go clean
rm -rf dist
packr clean

.PHONY: build clean coverage test install lint lint-slow packr release help
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ brew tap chanzuckerberg/tap
brew install fogg
```

Note– if you installed fogg from homebrew before version 0.15.0, the tap location has changed. Run this, then install as above–

```
brew uninstall fogg
brew untap chanzuckerberg/fogg
```

## Linux, Windows, etc.

Binaries are available on the releases page. Download one for your architecture, put it in your path and make it executable.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.14.0
0.15.3
46 changes: 33 additions & 13 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
dockerImageVersion = "0.2.1"
)

// AWSConfiguration represents aws configuration
type AWSConfiguration struct {
AccountID *int64 `yaml:"account_id"`
AccountName string `yaml:"account_name"`
Expand All @@ -27,13 +28,15 @@ type AWSConfiguration struct {
InfraBucket string `yaml:"infra_bucket"`
}

// Common represents common fields
type Common struct {
DockerImageVersion string `yaml:"docker_image_version"`
PathToRepoRoot string `yaml:"path_to_repo_root"`
TerraformVersion string `yaml:"terraform_version"`
}

type account struct {
// Account is an account
type Account struct {
AWSConfiguration `yaml:",inline"`
Common `yaml:",inline"`

Expand All @@ -44,14 +47,17 @@ type account struct {
TfLint TfLint
}

// Module is a module
type Module struct {
Common `yaml:",inline"`
}

// Component is a component
type Component struct {
AWSConfiguration `yaml:",inline"`
Common `yaml:",inline"`

Accounts map[string]Account // Reference accounts for remote state
Component string
Env string
ExtraVars map[string]string `yaml:"extra_vars"`
Expand All @@ -62,6 +68,7 @@ type Component struct {
TfLint TfLint
}

// Env is an env
type Env struct {
AWSConfiguration `yaml:",inline"`
Common `yaml:",inline"`
Expand Down Expand Up @@ -101,15 +108,17 @@ func (p *Plugins) SetTerraformProvidersPlan(terraformProviders map[string]*plugi
}
}

// Plan represents a set of actions to take
type Plan struct {
Accounts map[string]account
Accounts map[string]Account
Envs map[string]Env
Global Component
Modules map[string]Module
Plugins Plugins
Version string
}

// Eval evaluats a config
func Eval(config *config.Config, verbose bool) (*Plan, error) {
p := &Plan{}
v, e := util.VersionString()
Expand All @@ -120,14 +129,19 @@ func Eval(config *config.Config, verbose bool) (*Plan, error) {
p.Plugins.SetCustomPluginsPlan(config.Plugins.CustomPlugins)
p.Plugins.SetTerraformProvidersPlan(config.Plugins.TerraformProviders)

p.Accounts = buildAccounts(config)
p.Envs = buildEnvs(config)
p.Global = buildGlobal(config)
p.Modules = buildModules(config)
var err error
p.Accounts = p.buildAccounts(config)
p.Envs, err = p.buildEnvs(config)
if err != nil {
return nil, err
}
p.Global = p.buildGlobal(config)
p.Modules = p.buildModules(config)

return p, nil
}

// Print prints a plan
func Print(p *Plan) error {
out, err := yaml.Marshal(p)
if err != nil {
Expand All @@ -137,12 +151,12 @@ func Print(p *Plan) error {
return nil
}

func buildAccounts(c *config.Config) map[string]account {
func (p *Plan) buildAccounts(c *config.Config) map[string]Account {
defaults := c.Defaults

accountPlans := make(map[string]account, len(c.Accounts))
accountPlans := make(map[string]Account, len(c.Accounts))
for name, config := range c.Accounts {
accountPlan := account{}
accountPlan := Account{}
accountPlan.DockerImageVersion = dockerImageVersion

accountPlan.AccountName = name
Expand Down Expand Up @@ -171,7 +185,7 @@ func buildAccounts(c *config.Config) map[string]account {
return accountPlans
}

func buildModules(c *config.Config) map[string]Module {
func (p *Plan) buildModules(c *config.Config) map[string]Module {
modulePlans := make(map[string]Module, len(c.Modules))
for name, conf := range c.Modules {
modulePlan := Module{}
Expand All @@ -190,7 +204,7 @@ func newEnvPlan() Env {
return ep
}

func buildGlobal(conf *config.Config) Component {
func (p *Plan) buildGlobal(conf *config.Config) Component {
// Global just uses defaults because that's the way sicc worked. We should make it directly configurable.
componentPlan := Component{}

Expand Down Expand Up @@ -219,7 +233,8 @@ func buildGlobal(conf *config.Config) Component {
return componentPlan
}

func buildEnvs(conf *config.Config) map[string]Env {
// buildEnvs must be build after accounts
func (p *Plan) buildEnvs(conf *config.Config) (map[string]Env, error) {
envPlans := make(map[string]Env, len(conf.Envs))
defaults := conf.Defaults

Expand Down Expand Up @@ -249,6 +264,11 @@ func buildEnvs(conf *config.Config) map[string]Env {

for componentName, componentConf := range conf.Envs[envName].Components {
componentPlan := Component{}
// reference accounts for remote state
if _, dupe := p.Accounts[componentName]; dupe {
return nil, errs.WrapUser(fmt.Errorf("Component %s can't have same name as account", componentName), "Invalid component name")
}
componentPlan.Accounts = p.Accounts

componentPlan.AccountID = resolveOptionalInt(envPlan.AccountID, componentConf.AccountID)
componentPlan.AWSRegionBackend = resolveRequired(envPlan.AWSRegionBackend, componentConf.AWSRegionBackend)
Expand Down Expand Up @@ -280,7 +300,7 @@ func buildEnvs(conf *config.Config) map[string]Env {

envPlans[envName] = envPlan
}
return envPlans
return envPlans, nil
}

func otherComponentNames(components map[string]*config.Component, thisComponent string) []string {
Expand Down
26 changes: 26 additions & 0 deletions templates/component/fogg.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,29 @@ data "terraform_remote_state" "{{ $component }}" {
}
}
{{ end }}

# remote state for accounts
{{ range $accountName, $account := .Accounts }}
data "terraform_remote_state" "{{ $accountName }}" {
backend = "s3"

config {
bucket = "{{ $account.InfraBucket }}"
key = "terraform/{{ .Project }}/accounts/{{ $account.AccountName }}.tfstate"
region = "{{ $account.AWSRegionBackend }}"
{{ if $account.AWSProfileBackend }}profile = "{{ $account.AWSProfileBackend }}"{{ end }}
}
}
{{ end }}

# map of aws_accounts
variable "aws_accounts" {
type = "map"
default = {
{{ range $accountName, $account := .Accounts }}
{{ if $account.AccountID }}
{{ $accountName }} = {{ $account.AccountID }}
{{ end }}
{{ end }}
}
}
3 changes: 2 additions & 1 deletion templates/repo/scripts/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
TF_VARS := $(patsubst %,-e%,$(filter TF_VAR_%,$(.VARIABLES)))
REPO_ROOT := $(shell git rev-parse --show-toplevel)
REPO_RELATIVE_PATH := $(shell git rev-parse --show-prefix)
AUTO_APPROVE := false
# We need to do this because `terraform fmt` recurses into .terraform/modules
# and wont' accept more than one file at a time.
TF=$(wildcard *.tf)
Expand Down Expand Up @@ -48,7 +49,7 @@ plan: fmt get init ssh-forward

apply: FOGG_DOCKER_FLAGS = -it
apply: fmt get init ssh-forward
$(docker_terraform) apply -auto-approve=false
$(docker_terraform) apply -auto-approve=$(AUTO_APPROVE)

docs:
@echo
Expand Down

0 comments on commit bea5067

Please sign in to comment.