Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply modules #13

Merged
merged 7 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func Apply(fs afero.Fs, configFile string, tmp *templates.T) error {
return e
}

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

return nil
}
Expand Down Expand Up @@ -77,6 +80,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)
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
10 changes: 5 additions & 5 deletions plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type account struct {
TerraformVersion string
}

type module struct {
type Module struct {
TerraformVersion string
}

Expand Down Expand Up @@ -65,7 +65,7 @@ type Plan struct {
Accounts map[string]account
Envs map[string]Env
Global Component
Modules map[string]module
Modules map[string]Module
Version string
}

Expand Down Expand Up @@ -205,10 +205,10 @@ func buildAccounts(c *config.Config) map[string]account {
return accountPlans
}

func buildModules(c *config.Config) map[string]module {
modulePlans := make(map[string]module, len(c.Modules))
func buildModules(c *config.Config) 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)
modulePlans[name] = modulePlan
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"),
}