Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into ryanking/sicc-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking committed Jun 29, 2018
2 parents c4750f4 + 57217e0 commit 84396f0
Show file tree
Hide file tree
Showing 23 changed files with 3,344 additions and 18 deletions.
15 changes: 14 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "github.com/hashicorp/hcl"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
69 changes: 57 additions & 12 deletions apply/apply.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apply

import (
"bytes"
"fmt"
"io"
"log"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/chanzuckerberg/fogg/templates"
"github.com/chanzuckerberg/fogg/util"
"github.com/gobuffalo/packr"
"github.com/hashicorp/hcl/hcl/printer"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -44,7 +46,10 @@ func Apply(fs afero.Fs, configFile string, tmp *templates.T, siccMode bool) erro
return e
}

// TODO modules
e = applyModules(fs, p.Modules, &tmp.Module)
if e != nil {
return e
}

return nil
}
Expand Down Expand Up @@ -77,6 +82,22 @@ func applyAccounts(fs afero.Fs, p *plan.Plan, accountBox *packr.Box) (e error) {
return nil
}

func applyModules(fs afero.Fs, p map[string]plan.Module, moduleBox *packr.Box) error {
var e error
for module, modulePlan := range p {
path := fmt.Sprintf("%s/modules/%s", rootPath, module)
e = fs.MkdirAll(path, 0755)
if e != nil {
return e
}
e = applyTree(moduleBox, afero.NewBasePathFs(fs, path), modulePlan.SiccMode, modulePlan)
if e != nil {
return e
}
}
return nil
}

func applyEnvs(fs afero.Fs, p *plan.Plan, envBox *packr.Box, componentBox *packr.Box) (e error) {
for env, envPlan := range p.Envs {
path := fmt.Sprintf("%s/envs/%s", rootPath, env)
Expand Down Expand Up @@ -107,39 +128,63 @@ func applyTree(source *packr.Box, dest afero.Fs, siccMode bool, subst interface{
return source.Walk(func(path string, sourceFile packr.File) error {
extension := filepath.Ext(path)
target := getTargetPath(path, siccMode)
targetExtension := filepath.Ext(target)
if extension == ".tmpl" {

err := applyTemplate(sourceFile, dest, target, subst)
if err != nil {
return errors.Wrap(err, "unable to apply template")
e = applyTemplate(sourceFile, dest, target, subst)
if e != nil {
return errors.Wrap(e, "unable to apply template")
}

// if dest.endswith('.tf'):
// subprocess.call(['terraform', 'fmt', dest])
} else if extension == ".touch" {
touchFile(dest, target)
// if dest.endswith('.tf'):
// subprocess.call(['terraform', 'fmt', dest])

e = touchFile(dest, target)
if e != nil {
return e
}

} else if extension == ".create" {
createFile(dest, target, sourceFile)
// if dest.endswith('.tf'):
// subprocess.call(['terraform', 'fmt', dest])

e = createFile(dest, target, sourceFile)
if e != nil {
return errors.Wrapf(e, "unable to create file %s", target)
}

} else {

log.Printf("copying %s", path)
e = afero.WriteReader(dest, path, sourceFile)
if e != nil {
return errors.Wrap(e, "unable to copy file")
}

}

if !siccMode && target == "fogg.tf" {
log.Println("removing sicc.tf")
dest.Remove("sicc.tf")
}

if targetExtension == ".tf" {
e = fmtHcl(dest, target)
if e != nil {
return errors.Wrap(e, "unable to format HCL")
}
}
return nil
})
}

func fmtHcl(fs afero.Fs, path string) error {
in, e := afero.ReadFile(fs, path)
if e != nil {
return e
}
out, e := printer.Format(in)
if e != nil {
return e
}
return afero.WriteReader(fs, path, bytes.NewReader(out))
}

func touchFile(dest afero.Fs, path string) error {
Expand Down
19 changes: 19 additions & 0 deletions apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ func TestGetTargetPath(t *testing.T) {
}
}

func TestFmtHcl(t *testing.T) {
before := `foo { bar = "bam"}`
after := `foo {
bar = "bam"
}
`
fs := afero.NewMemMapFs()
in := strings.NewReader(before)
e := afero.WriteReader(fs, "foo.tf", in)
assert.Nil(t, e)
e = fmtHcl(fs, "foo.tf")
assert.Nil(t, e)
out, e := afero.ReadFile(fs, "foo.tf")
assert.Nil(t, e)
assert.NotNil(t, out)
s := string(out)
assert.Equal(t, after, s)
}

func readFile(fs afero.Fs, path string) (string, error) {
f, e := fs.Open(path)
if e != nil {
Expand Down
12 changes: 7 additions & 5 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type account struct {
SiccMode bool
}

type module struct {
type Module struct {
SiccMode bool
TerraformVersion string
}

Expand Down Expand Up @@ -68,7 +69,7 @@ type Plan struct {
Accounts map[string]account
Envs map[string]Env
Global Component
Modules map[string]module
Modules map[string]Module
SiccMode bool
Version string
}
Expand Down Expand Up @@ -210,12 +211,13 @@ func buildAccounts(c *config.Config, siccMode bool) map[string]account {
return accountPlans
}

func buildModules(c *config.Config, siccMode bool) map[string]module {
modulePlans := make(map[string]module, len(c.Modules))
func buildModules(c *config.Config, siccMode bool) map[string]Module {
modulePlans := make(map[string]Module, len(c.Modules))
for name, conf := range c.Modules {
modulePlan := module{}
modulePlan := Module{}

modulePlan.TerraformVersion = resolveRequired(c.Defaults.TerraformVersion, conf.TerraformVersion)
modulePlan.SiccMode = siccMode
modulePlans[name] = modulePlan
}
return modulePlans
Expand Down
46 changes: 46 additions & 0 deletions templates/module/Makefile.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Auto-generated by sicc. Do not edit
# Make improvements in sicc, so that everyone can benefit.

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)
# 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)

docker_base = \
docker run -it --rm -e HOME=/home -v $$HOME/.aws:/home/.aws -v $(REPO_ROOT):/repo \
-e GIT_SSH_COMMAND='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
-e TF_PLUGIN_CACHE_DIR="/repo/.terraform.d/plugin-cache" -e TF="$(TF)" \
-w /repo/$(REPO_RELATIVE_PATH) $(TF_VARS) $$($(REPO_ROOT)/scripts/docker-ssh-mount.sh)
docker_terraform = $(docker_base) hashicorp/terraform:{{ .TerraformVersion }}
docker_sh = $(docker_base) --entrypoint='/bin/sh' hashicorp/terraform:{{ .TerraformVersion }}

all: fmt lint doc

fmt:
@$(docker_sh) -c "for f in $(TF); do printf '.'; terraform fmt $$f; done"; \
echo

lint: lint-tf

lint-tf:
@$(docker_sh) -c "for f in $(TF); do printf '.'; terraform fmt --check=true --diff=true $$f || exit $$? ; done"; \
echo

readme:
bash .update-readme.sh update

docs: readme

check-docs:
@bash .update-readme.sh check; \
if [ ! $$? -eq 0 ]; then \
echo "Docs are out of date, run \`make docs\`"; \
fi

clean:

test:

.PHONY: all check-doc clean docs fmt lint lint-tf readme test
2 changes: 2 additions & 0 deletions templates/module/README.md.create
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- START -->
<!-- END -->
Empty file added templates/module/main.tf.touch
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions templates/module/sicc.tf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Auto-generated by sicc. Do not edit
# Make improvements in sicc, so that everyone can benefit.

terraform {
required_version = "~>{{ .TerraformVersion }}"
}
Empty file.
2 changes: 2 additions & 0 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type T struct {
Component packr.Box
Env packr.Box
Global packr.Box
Module packr.Box
Repo packr.Box
}

Expand All @@ -15,5 +16,6 @@ var Templates = &T{
Component: packr.NewBox("component"),
Env: packr.NewBox("env"),
Global: packr.NewBox("global"),
Module: packr.NewBox("module"),
Repo: packr.NewBox("repo"),
}
Loading

0 comments on commit 84396f0

Please sign in to comment.