Skip to content

Commit

Permalink
Merge pull request #2 from digital-society-coop/update-naming
Browse files Browse the repository at this point in the history
Switch to new naming convention
  • Loading branch information
connec authored Feb 8, 2024
2 parents 47d0234 + 9ca2b97 commit 313d171
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 `<environment>` instance of `foundation` provides:
In particular, an `<environment>` instance of `do-foundations` provides:

- A `<environment>-foundation-terraform` DigitalOcean Spaces Object Storage bucket for Terraform state.
- A `do-foundations-<environment>-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
Expand Down Expand Up @@ -59,3 +63,10 @@ The service is continuously deployed by GitHub Actions.
```sh
./deploy.sh '<env>'
```
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 '<env>')"
terraform ...
```
18 changes: 4 additions & 14 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function usage {
exit 1
}

service=foundation
service=do-foundations

[[ $# -ge 1 ]] || usage
environment=$1
Expand All @@ -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
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions terraform-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

set -euo pipefail

function usage {
echo "Usage: $0 <service> <env>" >&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"

0 comments on commit 313d171

Please sign in to comment.