Skip to content

Commit

Permalink
Merge pull request #3 from paulRbr/0.10.4
Browse files Browse the repository at this point in the history
Release 0.10.4 in sync with terraform's version
  • Loading branch information
paulRbr authored Sep 10, 2017
2 parents 3b3f88f + a0374df commit 624ef39
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 52 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ install:
script:
# Basic run
- make
- ./test.sh
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM hashicorp/terraform:0.10.4

RUN apk add --update make bash

WORKDIR /opt/terraform
COPY . .

VOLUME [ /opt/terraform/providers ]
VOLUME [ /opt/terraform/modules ]
26 changes: 11 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ------------------
# TERRAFORM-MAKEFILE
# v0.1.0
# v0.10.4
# ------------------
#
# This Makefile is maintained on Github.com.
Expand All @@ -11,7 +11,7 @@
##
# TERRAFORM INSTALL
##
version ?= "0.10.3"
version ?= "0.10.4"
os ?= $(shell uname|tr A-Z a-z)
ifeq ($(shell uname -m),x86_64)
arch ?= "amd64"
Expand All @@ -33,11 +33,6 @@ env ?= ""
##
# INTERNAL VARIABLES
##
ifneq ("$(provider)", "")
wd ?= providers/$(provider)/$(env)
else
wd ?= "."
endif
ifeq ("$(shell which terraform)", "")
install ?= "true"
endif
Expand All @@ -51,37 +46,38 @@ endif
.PHONY: install
install: ## make install # Install terraform and dependencies
ifeq ($(install),"true")
@wget -O /usr/bin/terraform.zip https://releases.hashicorp.com/terraform/0.10.3/terraform_$(version)_$(os)_$(arch).zip
@wget -O /usr/bin/terraform.zip https://releases.hashicorp.com/terraform/$(version)/terraform_$(version)_$(os)_$(arch).zip
@unzip -d /usr/bin /usr/bin/terraform.zip && rm /usr/bin/terraform.zip
endif
@terraform --version
@wd=$(wd) ./terraform.sh init
@bash terraform.sh init

.PHONY: lint
lint: ## make lint # Rewrites config to canonical format
@terraform fmt -diff=true $(opts)

.PHONY: validate
validate: ## make validate # Basic syntax check
@wd=$(wd) ./terraform.sh validate $(opts)
@bash terraform.sh validate $(opts)

.PHONY: list
list: ## make list # List infra resources
@wd=$(wd) ./terraform.sh show $(opts)
@bash terraform.sh show $(opts)

.PHONY: dry-run
dry-run: ## make dry-run # Dry run resources changes
@wd=$(wd) ./terraform.sh plan $(opts)
dry-run: pass ## make dry-run # Dry run resources changes
@bash terraform.sh plan $(opts)

.PHONY: run
run: ## make run # Execute resources changes
@wd=$(wd) ./terraform.sh apply $(opts)
@bash terraform.sh apply $(opts)

.PHONY: destroy
destroy: ## make destroy # Destroy resources
@wd=$(wd) ./terraform.sh destroy $(opts)
@bash terraform.sh destroy $(opts)

help:
@printf "\033[32mTerraform-makefile v$(version)\033[0m\n"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help
51 changes: 40 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,72 @@
# Makefile for Terraform users

This repository provides a Makefile to give you a simple interface for Terraform.
This repository provides a Makefile to give you a simple interface for [Terraform](https://www.terraform.io/).

## Why?

- Simplify your CLI terraform runs
- Don't Repeat Yourself while typing terraform commands
- Easier adoption for people that are not used to Terraform
- Document common usage
- Unique entrypoint script for credentials management
- Unique entrypoint script for credentials management (only for AWS, Azure, DigitalOcean, Google and Scaleway for now)
- either passing ENV variables. E.g. `<PROVIDER>_<ENV>_SECRET` will be mapped to `DIGITALOCEAN_TOKEN` if `provider=do` is provided as variable
- either using [`pass`](https://www.passwordstore.org/) as local secret manager. E.g. password `terraform/<provider>/<env>/secret` will be mapped to `DIGITALOCEAN_TOKEN` if `provider=do` is provided as variable

## Installation

Simply download the `Makefile` and the `terraform.sh` files in your terraform configuration directory.

wget -O Makefile https://raw.githubusercontent.com/paulRbr/terraform-makefile/master/Makefile
wget -O terraform.sh https://raw.githubusercontent.com/paulRbr/terraform-makefile/master/terraform.sh
wget -N https://raw.githubusercontent.com/paulRbr/terraform-makefile/master/{Makefile,terraform.sh}

## Convention

This makefile assumes your terraform configuration files are stored as such:

```
providers/
├── aws
│   ├── prod
│   │   └── empty.tf
│   └── test
│   └── empty.tf
├── do
│   └── prod
│   └── empty.tf
├── google
│   ├── prod
│   │   └── empty.tf
│   └── test
│   └── empty.tf
└── scaleway
└── prod
└── empty.tf
```

I.e. `providers/<provider>/<env>/*.tf`

## Commands

This is the list of commands made available
List of commands made available

~~~bash
> make
Terraform-makefile v0.10.4
destroy make destroy # Destroy resources
dry-run make dry-run # Dry run resources changes
install make install # Install terraform and dependencies
lint make lint # Rewrites config to canonical format
list make list # List infra resources
run make run # Execute resources changes
validate make validate # Basic syntax check
~~~

## Variables

This is the explanation of variables that can be passed to commands:
Details of the variables that can be passed to commands:


| Name | Default | Description | Example |
| --------- | ------- | ----------- | ------- |
| `provider`| - | Name of the cloud provider to target | If you have an terraform file in `provider/aws/production/production.tf` you will be able to `make run provider=aws env=production` |
| `env` | - | Name of the environment you want to use | If you have an terraform file in `provider/google/production/production.tf` you will be able to `make run provider=google env=production` |
| `args` | - | Add terraform understandable arguments | `make dry-run args='-no-color'` |
| Name | Default | Values | Description | Example |
| --------- | ------- | ------ | ----------- | ------- |
| `provider`| - | `aws`<br/>`azure`<br/>`do`<br/>`google`<br/>`scaleway` | Name of the cloud provider to target | With your terraform file in `provider/aws/production/production.tf` you will be able to `make dry-run provider=aws env=production` |
| `env` | - | `String` | Name of the environment you want to use | With a terraform file in `provider/google/production/production.tf` you will be able to `make dry-run provider=google env=production` |
| `args` | - | `String` | Add terraform understandable arguments | `make dry-run args='-no-color'` |
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.10.4
68 changes: 42 additions & 26 deletions terraform.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ------------------
# TERRAFORM-MAKEFILE
# v0.1.0
# v0.10.4
# ------------------
#
# This Makefile is maintained on Github.com.
Expand All @@ -10,36 +10,52 @@
#
#!/bin/bash -e

key="$(echo "${provider}" | tr '[:lower:]' '[:upper:]')_$(echo "${env}" | tr '[:lower:]' '[:upper:]')_KEY"
secret="$(echo "${provider}" | tr '[:lower:]' '[:upper:]')_$(echo "${env}" | tr '[:lower:]' '[:upper:]')_SECRET"
valid_identifier()
{
echo "$1" | tr '[:lower:]' '[:upper:]' | tr -cs '[:alpha:]\n' '_'
}

key="$(valid_identifier "${provider}")_$(valid_identifier "${env}")_KEY"
secret="$(valid_identifier "${provider}")_$(valid_identifier "${env}")_SECRET"

if (which pass >/dev/null 2>&1); then
pass_key="$(pass "terraform/${provider}/${env}/access_key")"
pass_secret="$(pass "terraform/${provider}/${env}/secret")"
pass_key="$(pass "terraform/${provider}/${env}/access_key")"
pass_secret="$(pass "terraform/${provider}/${env}/secret")"

declare "${key}"="${pass_key}"
declare "${secret}"="${pass_secret}"
declare "${key}"="${pass_key}"
declare "${secret}"="${pass_secret}"
fi

case $provider in
aws)
declare "AWS_ACCESS_KEY_ID=${!key}"
declare "AWS_SECRET_ACCESS_KEY=${!secret}"
;;
azurerm)
declare "ARM_CLIENT_ID=${!key}"
declare "ARM_CLIENT_SECRET=${!secret}"
:;;
"do")
declare "DIGITALOCEAN_TOKEN=${!secret}"
:;;
google)
declare "GOOGLE_CREDENTIALS=${!secret}"
:;;
scaleway)
declare "SCALEWAY_ORGANIZATION=${!key}"
declare "SCALEWAY_TOKEN=${!secret}"
:;;
aws)
if [ -z "${AWS_ACCESS_KEY_ID}" ]; then
declare -x "AWS_ACCESS_KEY_ID=${!key}"
declare -x "AWS_SECRET_ACCESS_KEY=${!secret}"
fi
;;
azurerm)
if [ -z "${ARM_CLIENT_ID}" ]; then
declare -x "ARM_CLIENT_ID=${!key}"
declare -x "ARM_CLIENT_SECRET=${!secret}"
fi
;;
"do")
if [ -z "${DIGITALOCEAN_TOKEN}" ]; then
declare -x "DIGITALOCEAN_TOKEN=${!secret}"
fi
;;
google)
if [ -z "${GOOGLE_CREDENTIALS}" ]; then
declare -x "GOOGLE_CREDENTIALS=${!secret}"
fi
;;
scaleway)
if [ -z "${SCALEWAY_ORGANIZATION}" ]; then
declare -x "SCALEWAY_ORGANIZATION=${!key}"
declare -x "SCALEWAY_TOKEN=${!secret}"
fi
;;
esac

cd "${wd}" && terraform $@
cd "providers/${provider}/${env}"
terraform "$@"
11 changes: 11 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -e

echo "Checking difference between README.md and make help output..."
readme_help="$(awk '/> make/{f=1;next} /~~~/{f=0} f' README.md)"
make_help="$(make | sed 's,\x1B\[[0-9;]*[a-zA-Z],,g')"
diff <(echo "$readme_help") <(echo "$make_help") || (printf "\033[31mFAILED!\033[0m\n" && exit 1)
printf "\033[32mOK\033[0m\n"

echo "Checking version..."
[ "$(git grep $(cat VERSION) | wc -l)" -eq 6 ] || (printf "\033[31mFAILED!\033[0m\n" && exit 1)
printf "\033[32mOK\033[0m\n"

0 comments on commit 624ef39

Please sign in to comment.