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

consolidate the multiple terraform deployments at src/core/* into a single deployment at src/terraform #268

Merged
merged 31 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ed47498
Prelim movements and cleanup.
Jun 8, 2021
987a1fb
Large conglomerate template in place.
Jun 15, 2021
699b3c2
Manually working single deployment.
Jun 16, 2021
f50e212
hub and spoke working
glennmusa Jun 22, 2021
5015de6
add remote access
glennmusa Jun 22, 2021
181a8af
add a vars sample for local deployment
glennmusa Jun 22, 2021
fde53dd
remove core
glennmusa Jun 22, 2021
b955c7b
support multiple subs and tags
glennmusa Jun 23, 2021
d39599a
working apply
glennmusa Jun 24, 2021
4017623
working destroy and clean
glennmusa Jun 24, 2021
f9622e4
add minimum vars sample
glennmusa Jun 24, 2021
b1f22aa
peering to mlz because dynamic providers
glennmusa Jun 24, 2021
7291d89
update docs
glennmusa Jun 24, 2021
a2aac25
remove frontend references
glennmusa Jun 24, 2021
0d50cae
Fork from new automation for testing, build in outputs, tier3 templat…
Jun 26, 2021
96aa299
Untested, needs debugging and additional outputs.
Jun 27, 2021
a32ffcf
Initial post testing.
Jun 28, 2021
39eb237
Documentation Update. Minor edits.
Jun 28, 2021
9c6d842
Doc and minor fixes.
Jun 28, 2021
4ad6263
Merge branch 'main' into singledeployment_deconflict
glennmusa Jun 28, 2021
87019d6
set keyvault properties
glennmusa Jun 28, 2021
798a11b
Adjust docs, adjust templates, and code
Jun 28, 2021
7603428
workaround failing az ad sp show queries
glennmusa Jun 28, 2021
fd29856
bump to 2.63
glennmusa Jun 28, 2021
1f1b070
fix merges
glennmusa Jun 29, 2021
d2e80c3
Merge branch 'singledeployment_addtier3' into singledeployment_deconf…
glennmusa Jun 29, 2021
b734847
catch flags and notset params
glennmusa Jun 29, 2021
483a926
use tfvars.json
glennmusa Jun 29, 2021
72c3b50
remove dead code
glennmusa Jun 29, 2021
1dbc9f3
Merge branch 'glennmusa/removefrontend' into singledeployment
glennmusa Jun 29, 2021
2854519
Merge remote-tracking branch 'origin/main' into singledeployment
brooke-hamilton Jun 29, 2021
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
5 changes: 0 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "src/front"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
18 changes: 5 additions & 13 deletions .github/workflows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ For more on workflows: <https://docs.github.com/en/actions/reference/workflow-sy

1. Checks out the .devcontainer from a private container registry for common tools

1. Recursively validates and lints all the terraform referenced at src/core
1. Recursively validates and lints all the terraform referenced at src/terraform

## Configuration Prerequisites

1. MLZ Setup

To apply terraform at all, locally, or from this automation, `src/scripts/config/create_mlz_configuration_resources.sh` must be run to create the storage accounts to store Terraform state and create the Service Principal with authorization to deploy resources into the configured subscription(s).
To apply terraform at all, locally, or from this automation, `src/scripts/config/create_required_resources.sh` must be run to create the storage accounts to store Terraform state and create the Service Principal with authorization to deploy resources into the configured subscription(s).

See the root README's [Configure the Terraform Backend](#../..//README.md/#Configure-the-Terraform-Backend) on how to do this.

Expand All @@ -46,12 +46,8 @@ For more on workflows: <https://docs.github.com/en/actions/reference/workflow-sy

File Name | Value
------------ | -------------
mlz.config | An MLZ Configuration file that comes from create_mlz_configuration_resources.sh
globals.tfvars | Global MLZ terraform values
saca-hub.tfvars | SACA Hub MLZ terraform values
tier-0.tfvars | Tier 0 MLZ terraform values
tier-1.tfvars | Tier 1 MLZ terraform values
tier-2.tfvars | Tier 2 MLZ terraform values
mlz.config | An MLZ Configuration file that comes from create_required_resources.sh
mlz.tfvars | MLZ terraform values

Running this from your local machine, you can provide these files yourself, but, today, for automation these files are stored in an Azure Storage Account and retrieved at workflow execution time. See [build/get_vars.sh](../../build/get_vars.sh) to see how we retrieve

Expand All @@ -60,11 +56,7 @@ For more on workflows: <https://docs.github.com/en/actions/reference/workflow-sy

# pulls down these files:
vars/mlz.config
vars/globals.tfvars
vars/saca-hub.tfvars
vars/tier-0.tfvars
vars/tier-1.tfvars
vars/tier-2.tfvars
vars/mlz.tfvars
```

1. Secret store and minimally scoped Service Principal
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/apply-and-destroy-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,13 @@ jobs:
cd src/build
./apply_tf.sh \
vars/mlz.config \
vars/globals.tfvars \
vars/saca-hub.tfvars \
vars/tier-0.tfvars \
vars/tier-1.tfvars \
vars/tier-2.tfvars \
vars/mlz.tfvars \
n

- name: destroy terraform
run : |
cd src/build
./destroy_tf.sh \
vars/mlz.config \
vars/globals.tfvars \
vars/saca-hub.tfvars \
vars/tier-0.tfvars \
vars/tier-1.tfvars \
vars/tier-2.tfvars \
vars/mlz.tfvars \
n
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*.tfvars
*.terraform
*.tfstate
*.tfstate.backup
*.tfstate.*
terraform-provider-azurerm_v*
terraform-provider-random_v*
*.terraform.lock.hcl
Expand Down
3 changes: 0 additions & 3 deletions src/.dockerignore

This file was deleted.

100 changes: 0 additions & 100 deletions src/Dockerfile

This file was deleted.

18 changes: 5 additions & 13 deletions src/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,23 @@ There's an [optional argument to display terraform output](#Optionally-display-T
```shell
usage() {
echo "apply_tf.sh: Automation that calls apply terraform given a MLZ configuration and some tfvars"
error_log "usage: apply_tf.sh <mlz config> <globals.tfvars> <saca.tfvars> <tier0.tfvars> <tier1.tfvars> <tier2.tfvars> <display terraform output (y/n)>"
error_log "usage: apply_tf.sh <mlz config> <mlz.tfvars> <display terraform output (y/n)>"
}
```

```shell
# assuming src/scripts/config/create_mlz_configuration_resources.sh has been run before...
# assuming src/scripts/config/create_required_resources.sh has been run before...
./apply_tf.sh \
./path-to/mlz.config \
./path-to/globals.tfvars \
./path-to/saca-hub.tfvars \
./path-to/tier-0.tfvars \
./path-to/tier-1.tfvars \
./path-to/tier-2.tfvars \
./path-to/mlz.tfvars
y
```

```shell
# assuming src/scripts/config/create_mlz_configuration_resources.sh has been run before...
# assuming src/scripts/config/create_required_resources.sh has been run before...
./destroy_tf.sh \
./path-to/mlz.config \
./path-to/globals.tfvars \
./path-to/saca-hub.tfvars \
./path-to/tier-0.tfvars \
./path-to/tier-1.tfvars \
./path-to/tier-2.tfvars \
./path-to/mlz.tfvars \
y
```

Expand Down
63 changes: 27 additions & 36 deletions src/build/apply_tf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# SC1090: Can't follow non-constant source. Use a directive to specify location.
# SC2154: "var is referenced but not assigned". These values come from an external file.
#
# Automation that calls apply terraform given a MLZ configuration and some globals.tfvars
# Automation that calls apply terraform given a MLZ configuration

set -e

Expand All @@ -17,45 +17,39 @@ error_log() {

usage() {
echo "apply_tf.sh: Automation that calls apply terraform given a MLZ configuration and some tfvars"
error_log "usage: apply_tf.sh <mlz config> <globals.tfvars> <saca.tfvars> <tier0.tfvars> <tier1.tfvars> <tier2.tfvars> <display terraform output (y/n)>"
error_log "usage: apply_tf.sh <mlz config> <mlz.tfvars> <display terraform output (y/n)>"
}

if [[ "$#" -lt 6 ]]; then
if [[ "$#" -lt 2 ]]; then
usage
exit 1
fi

# take some valid, well known, mlz_config and vars as input
mlz_config=$1
globals=$2
saca_vars=$3
tier0_vars=$4
tier1_vars=$5
tier2_vars=$6
display_tf_output=${7:-n}
# take some valid, well known, mlz_config and tfvars as input
mlz_config=$(realpath "${1}")
mlz_tfvars=$(realpath "${2}")
display_tf_output=${3:-n}

# reference paths
this_script_path=$(realpath "${BASH_SOURCE%/*}")
src_dir=$(dirname "${this_script_path}")
core_path="${src_dir}/core/"
scripts_path="${src_dir}/scripts/"
terraform_dir="${src_dir}/terraform/"
scripts_dir="${src_dir}/scripts/"

# apply function
apply() {
name=$1
tier_sub=$2
path=$3
vars=$4
sub_id=$1
tf_dir=$2
vars=$3

# generate config.vars based on MLZ Config and Terraform module
. "${scripts_path}/config/generate_vars.sh" \
. "${scripts_dir}/config/generate_vars.sh" \
"${mlz_config}" \
"${tier_sub}" \
"${name}" \
"${path}"
"${sub_id}" \
"${tf_dir}"

# remove any existing terraform initialzation
rm -rf "${path}/.terraform"
rm -rf "${tf_dir}/.terraform"

# copy input vars to temporary file
input_vars=$(realpath "${vars}")
Expand All @@ -64,26 +58,26 @@ apply() {
touch "${temp_vars}"
cp "${input_vars}" "${temp_vars}"

# remove any configuration tfvars and subtitute it with input vars
tf_vars="${path}/$(basename "${vars}")"
# remove any tfvars and subtitute it with input vars
tf_vars="${tf_dir}/$(basename "${vars}")"
rm -f "${tf_vars}"
touch "${tf_vars}"
cp "${temp_vars}" "${tf_vars}"
rm -f "${temp_vars}"

# set the target subscription
az account set \
--subscription "${tier_sub}" \
--subscription "${sub_id}" \
--output none

# attempt to apply $max_attempts times before giving up waiting between attempts
# attempt to apply $max_attempts times before giving up
# (race conditions, transient errors etc.)
apply_success="false"
attempts=1
max_attempts=5

apply_command="${scripts_path}/terraform/apply_terraform.sh ${globals} ${path} ${tf_vars} y"
destroy_command="${scripts_path}/terraform/destroy_terraform.sh ${globals} ${path} ${tf_vars} y"
apply_command="${scripts_dir}/terraform/apply_terraform.sh ${tf_dir} ${tf_vars} y"
destroy_command="${scripts_dir}/terraform/destroy_terraform.sh ${tf_dir} ${tf_vars} y"

if [[ $display_tf_output == "n" ]]; then
apply_command+=" &>/dev/null"
Expand All @@ -92,24 +86,24 @@ apply() {

while [ $apply_success == "false" ]
do
echo "INFO: applying ${name} (${attempts}/${max_attempts})..."
echo "INFO: applying Terraform at ${tf_dir} (${attempts}/${max_attempts})..."

if ! eval "$apply_command";
then
# if we fail, run terraform destroy and try again
error_log "ERROR: failed to apply ${name} (${attempts}/${max_attempts}). Trying some manual clean-up and Terraform destroy..."
error_log "ERROR: failed to apply ${tf_dir} (${attempts}/${max_attempts}). Trying some manual clean-up and Terraform destroy..."
eval "$destroy_command"

((attempts++))

if [[ $attempts -gt $max_attempts ]]; then
error_log "ERROR: failed ${max_attempts} times to apply ${name}. Exiting."
error_log "ERROR: failed ${max_attempts} times to apply ${tf_dir}. Exiting."
exit 1
fi
else
# if we succeed meet the base case
apply_success="true"
echo "INFO: finished applying ${name}!"
echo "INFO: finished applying ${tf_dir}!"
fi
done
}
Expand All @@ -118,7 +112,4 @@ apply() {
. "${mlz_config}"

# call apply()
apply "saca-hub" "${mlz_saca_subid}" "${core_path}/saca-hub" "${saca_vars}"
apply "tier-0" "${mlz_tier0_subid}" "${core_path}/tier-0" "${tier0_vars}"
apply "tier-1" "${mlz_tier1_subid}" "${core_path}/tier-1" "${tier1_vars}"
apply "tier-2" "${mlz_tier2_subid}" "${core_path}/tier-2" "${tier2_vars}"
apply "${mlz_saca_subid}" "${terraform_dir}/mlz" "${mlz_tfvars}"
Loading