From 9ca2b977631dabb8b3b265b142dccd47fd019664 Mon Sep 17 00:00:00 2001 From: Chris Connelly Date: Wed, 7 Feb 2024 21:17:46 +0000 Subject: [PATCH] fix: switch to new naming convention This also separates the terraform environment logic from `deploy.sh` to make it easier to work with terraform locally (e.g. to tear down infrastructure you created with the wrong naming convention...). --- README.md | 17 ++++++++++++++--- deploy.sh | 18 ++++-------------- terraform-env.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 17 deletions(-) create mode 100755 terraform-env.sh diff --git a/README.md b/README.md index 7232588..73f3971 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,22 @@ # do-foundations Foundations for DigitalOcean-based services -This repository defines a `foundation` service whose purpose is to provide the necessary interfaces to seamlessly deploy other services to DigitalOcean. +This repository defines a `do-foundations` service whose purpose is to provide the necessary interfaces to seamlessly deploy other services to DigitalOcean. -In particular, an `` instance of `foundation` provides: +In particular, an `` instance of `do-foundations` provides: -- A `-foundation-terraform` DigitalOcean Spaces Object Storage bucket for Terraform state. +- A `do-foundations--terraform` DigitalOcean Spaces Object Storage bucket for Terraform state. ## Deployment +### Automatic deployment + The service is continuously deployed by GitHub Actions. ### Manual deployment +Prefer to make changes via PR and continuous deployment, but manual deployment is possible if necessary. + #### Prerequisites ##### Tools @@ -59,3 +63,10 @@ The service is continuously deployed by GitHub Actions. ```sh ./deploy.sh '' ``` + +You can alternatively use the `terraform-env.sh` script to set up environment variables for working directly with Terraform: + +```sh +eval "$(./terraform-env.sh do-foundations '')" +terraform ... +``` diff --git a/deploy.sh b/deploy.sh index 7b0a57f..c246ff5 100755 --- a/deploy.sh +++ b/deploy.sh @@ -7,7 +7,7 @@ function usage { exit 1 } -service=foundation +service=do-foundations [[ $# -ge 1 ]] || usage environment=$1 @@ -16,8 +16,7 @@ shift echo "Deploying $service-$environment... " >&2 echo >&2 -stateBucket="$environment-$service-terraform" -stateKey="$service/$environment.tfstate" +eval "$(./terraform-env.sh "$service" "$environment")" echo -n "- Creating $stateBucket... " >&2 if ! result="$(aws s3api create-bucket --bucket "$stateBucket" 2>&1)" ; then @@ -32,17 +31,8 @@ if ! result="$(aws s3api create-bucket --bucket "$stateBucket" 2>&1)" ; then fi echo 'done' >&2 -tfCliArgsInit=( - "-backend-config=region=${AWS_REGION:-"$(aws configure get region)"}" - "-backend-config=bucket=$stateBucket" - "-backend-config=key=$stateKey" -) - -export TF_CLI_ARGS=-input=false -export TF_CLI_ARGS_init="${tfCliArgsInit[@]}" - -echo -n "- Initialising terraform with backend s3://$stateBucket/$stateKey... " >&2 -if ! result="$(terraform init -reconfigure)"; then +echo -n "- Initialising terraform... " >&2 +if ! result="$(terraform init)"; then echo 'failed' >&2 echo >&2 echo "$result" >&2 diff --git a/terraform-env.sh b/terraform-env.sh new file mode 100755 index 0000000..cadfe8e --- /dev/null +++ b/terraform-env.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +set -euo pipefail + +function usage { + echo "Usage: $0 " >&2 + exit 1 +} + +[[ $# -ge 1 ]] || usage +service="$1" +shift + +[[ $# -ge 1 ]] || usage +environment="$1" +shift + + +stateBucket="$service-$environment-terraform" +stateKey="$service/$environment.tfstate" + +tfCliArgs=( + '-input=false' +) + +tfCliArgsInit=( + ${tfCliArgs[@]} + "-backend-config=region=${AWS_REGION:-"$(aws configure get region)"}" + "-backend-config=bucket=$stateBucket" + "-backend-config=key=$stateKey" + '-lockfile=readonly' + '-reconfigure' +) + +tfCliArgsPlan=( + ${tfCliArgs[@]} +) + +tfCliArgsApply=( + ${tfCliArgsPlan[@]} + '-auto-approve' +) + +echo "export TF_CLI_ARGS_init='${tfCliArgsInit[@]}'" +echo "export TF_CLI_ARGS_plan='${tfCliArgsPlan[@]}'" +echo "export TF_CLI_ARGS_apply='${tfCliArgsApply[@]}'" +echo "stateBucket=$stateBucket"