Skip to content

Commit

Permalink
Merge pull request #1 from rackspace-infrastructure-automation/circleci
Browse files Browse the repository at this point in the history
Adding CircleCI.
  • Loading branch information
stevengorrell authored Aug 3, 2018
2 parents 628fe74 + 4f8a3eb commit 2dae593
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 3 deletions.
25 changes: 25 additions & 0 deletions .circleci/bin/apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

set -e

WORKING_DIR=$(pwd)
WORKSPACE_DIR="$WORKING_DIR/workspace"
LAYERS_DIR="$WORKING_DIR/layers"

if [ -f "$WORKSPACE_DIR/changed_layers" ]; then
LAYERS=$(cat "$WORKSPACE_DIR/changed_layers" | sort -n)
else
LAYERS=$(find "$LAYERS_DIR"/* -type d -maxdepth 0 -exec basename '{}' \; | sort -n)
fi

for LAYER in $LAYERS; do
# for debugging, show that these files exist
ls -la "$WORKSPACE_DIR/.terraform.$LAYER.tar.gz"
ls -la "$WORKSPACE_DIR/terraform.$LAYER.plan"

# uncache .terraform for the apply
(cd "$LAYERS_DIR/$LAYER" && tar xzf "$WORKSPACE_DIR/.terraform.$LAYER.tar.gz")

echo "terraform apply $LAYER"
(cd "$LAYERS_DIR/$LAYER" && terraform apply -input=false -no-color "$WORKSPACE_DIR/terraform.$LAYER.plan")
done
32 changes: 32 additions & 0 deletions .circleci/bin/check_master.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

set -e

# standard paths
WORKING_DIR=$(pwd)
WORKSPACE_DIR="$WORKING_DIR/workspace"
LAYERS_DIR="$WORKING_DIR/layers"
LAYERS=$(find "$LAYERS_DIR"/* -type d -maxdepth 0 -exec basename '{}' \; | sort -n)

# be sure we know about the latest remote refs
git fetch origin
MASTER_REF=$(git rev-parse remotes/origin/master)

# in the last hundred commits, is one of the parents in the current master?
git log --pretty=format:'%H' -n 100 | grep -q "$MASTER_REF"
UPTODATE=$?

if [ $UPTODATE -ne 0 ]
then
echo "Your branch is not up to date. Exiting."
fi

if [ -f "$WORKSPACE_DIR/changed_layers" ]; then
CHANGED_LAYERS=$(cat "$WORKSPACE_DIR/changed_layers")
else
CHANGED_LAYERS=$(git diff --name-only "$MASTER_REF" -- "$LAYERS_DIR" | awk -F "/" '{print $2}' | sort -n | uniq)
echo $CHANGED_LAYERS > "$WORKSPACE_DIR/changed_layers"
fi

echo "Changed layers: "
echo $CHANGED_LAYERS
24 changes: 24 additions & 0 deletions .circleci/bin/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

set -e

WORKING_DIR=$(pwd)
WORKSPACE_DIR="$WORKING_DIR/workspace"
LAYERS_DIR="$WORKING_DIR/layers"

if [ -f "$WORKSPACE_DIR/changed_layers" ]; then
LAYERS=$(cat "$WORKSPACE_DIR/changed_layers" | sort -nr)
else
LAYERS=$(find "$LAYERS_DIR"/* -type d -maxdepth 0 -exec basename '{}' \; | sort -nr)
fi

for LAYER in $LAYERS; do
# for debugging, show that these files exist
ls -la "$LAYERS_DIR/$LAYER/terraform.tfstate"

# uncache .terraform for the destroy
(cd "$LAYERS_DIR/$LAYER" && tar xzf "$WORKSPACE_DIR/.terraform.$LAYER.tar.gz" || echo "Did not find a cached .terraform directory")

echo "terraform destroy $LAYER"
(cd "$LAYERS_DIR/$LAYER" && terraform destroy -refresh=false -auto-approve)
done
27 changes: 27 additions & 0 deletions .circleci/bin/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

set -e

WORKING_DIR=$(pwd)
LAYERS_DIR="$WORKING_DIR/layers"
LAYERS=$(find "$LAYERS_DIR"/* -type d -maxdepth 0 -exec basename '{}' \; | sort -n)

OVERALL_RETURN=0
for LAYER in $LAYERS; do
echo "terraform fmt $LAYER"

LINT_OUTPUT=$(cd "$LAYERS_DIR/$LAYER" && terraform fmt -check=true -write=false -diff=false -list=true)
LINT_RETURN=$?

if [ $LINT_RETURN -ne 0 ]
then
echo "Linting failed in $LAYER, please run terraform fmt"
echo $LINT_OUTPUT
OVERALL_RETURN=1
fi
done

if [ $OVERALL_RETURN -ne 0 ]
then
exit $OVERALL_RETURN
fi
28 changes: 28 additions & 0 deletions .circleci/bin/plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

set -e

WORKING_DIR=$(pwd)
WORKSPACE_DIR="$WORKING_DIR/workspace"
LAYERS_DIR="$WORKING_DIR/layers"

if [ -f "$WORKSPACE_DIR/changed_layers" ]; then
LAYERS=$(cat "$WORKSPACE_DIR/changed_layers")
else
LAYERS=$(find "$LAYERS_DIR"/* -type d -maxdepth 0 -exec basename '{}' \; | sort -n)
fi

for LAYER in $LAYERS; do
echo "terraform init $LAYER"
(cd "$LAYERS_DIR/$LAYER" && terraform init -input=false -no-color)

# cache .terraform during the plan
(cd "$LAYERS_DIR/$LAYER" && tar -czf "$WORKSPACE_DIR/.terraform.$LAYER.tar.gz" .terraform)

echo "terraform plan $LAYER"
(cd "$LAYERS_DIR/$LAYER" && terraform plan -no-color -input=false -out="$WORKSPACE_DIR/terraform.$LAYER.plan" | tee "$WORKSPACE_DIR/full_plan_output.log" | grep -v "Refreshing state" )

# for debugging, show these files exist
ls -la "$WORKSPACE_DIR/.terraform.$LAYER.tar.gz"
ls -la "$WORKSPACE_DIR/terraform.$LAYER.plan"
done
28 changes: 28 additions & 0 deletions .circleci/bin/validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

set -e

WORKING_DIR=$(pwd)
WORKSPACE_DIR="$WORKING_DIR/workspace"
LAYERS_DIR="$WORKING_DIR/layers"
LAYERS=$(find "$LAYERS_DIR"/* -type d -maxdepth 0 -exec basename '{}' \; | sort -n)

OVERALL_RETURN=0
for LAYER in $LAYERS; do
echo "terraform validate $LAYER"

VALIDATE_OUTPUT=$(cd "$LAYERS_DIR/$LAYER" && terraform validate -input=false -check-variables=false -no-color .)
VALIDATE_RETURN=$?

if [ $VALIDATE_RETURN -ne 0 ]
then
echo "Validate failed in $LAYER, please run terraform validate"
echo $VALIDATE_OUTPUT
OVERALL_RETURN=1
fi
done

if [ $OVERALL_RETURN -ne 0 ]
then
exit $OVERALL_RETURN
fi
40 changes: 40 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: 2
jobs:
test:
docker:
- image: hashicorp/terraform:0.11.7
steps:
- checkout:
path: ~/module
- run: mkdir -p ~/workspace
- run: cp -pr ~/module/.circleci/bin ~/bin
- run: mv ~/module/tests/ ~/layers/ # Move tests into layers directory
- run:
name: lint tests
command: cd ~ && ~/bin/lint.sh
- run: mkdir -p ~/example_lint/layers/ && mv ~/module/examples/ ~/example_lint/layers/
- run:
name: lint examples
command: cd ~/example_lint && ~/bin/lint.sh
- run: mkdir -p ~/module_lint/layers/ && cp -pr ~/module/ ~/module_lint/layers/module/
- run:
name: lint module
command: cd ~/module_lint && ~/bin/lint.sh
- run:
name: plan
command: cd ~ && ~/bin/plan.sh
- run:
name: apply
command: cd ~ && ~/bin/apply.sh
- run:
name: destroy
command: cd ~ && ~/bin/destroy.sh # must succeed or we have something to clean up manually

workflows:
version: 2
build_and_test:
jobs:
- test:
filters:
branches:
ignore: master
15 changes: 13 additions & 2 deletions examples/s3.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
provider "aws" {
version = "~> 1.2"
region = "us-west-2"
}

resource "random_string" "s3_rstring" {
length = 18
upper = false
special = false
}

module "s3" {
source = "path/to/module"
source = "[email protected]:rackspace-infrastructure-automation/aws-terraform-s3//?ref=v0.0.1"

bucket_name = "<bucket_name>"
bucket_name = "${random_string.s3_rstring.result}-example-s3-bucket"

bucket_acl = "bucket-owner-full-control"

Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ locals {
disabled = "${list()}"
}

nc_ia_transitions = "${var.noncurrent_version_transition_ia_days > 0 ? "ia_enbled": "disabled"}"
nc_ia_transitions = "${var.noncurrent_version_transition_ia_days > 0 ? "ia_enabled": "disabled"}"
nc_glacier_transitions = "${var.noncurrent_version_transition_glacier_days > 0 ? "glacier_enabled":"disabled"}"

nc_transitions = "${concat(local.noncurrent_version_transition[local.nc_ia_transitions], local.noncurrent_version_transition[local.nc_glacier_transitions])}"
Expand Down
49 changes: 49 additions & 0 deletions tests/test1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
provider "aws" {
version = "~> 1.2"
region = "us-west-2"
}

resource "random_string" "s3_rstring" {
length = 18
upper = false
special = false
}

module "s3" {
source = "../../module"

bucket_name = "${random_string.s3_rstring.result}-example-s3-bucket"

bucket_acl = "bucket-owner-full-control"

bucket_logging = false

bucket_tags = {
RightSaid = "Fred"
LeftSaid = "George"
}

environment = "Development"

lifecycle_enabled = true

noncurrent_version_expiration_days = "425"

noncurrent_version_transition_glacier_days = "60"

noncurrent_version_transition_ia_days = "30"

object_expiration_days = "425"

transition_to_glacier_days = "60"

transition_to_ia_days = "30"

versioning = true

website = true

website_error = "error.html"

website_index = "index.html"
}

0 comments on commit 2dae593

Please sign in to comment.