Skip to content

Commit 220afea

Browse files
LeoDiazLmcascone
andauthored
Terraform splitting (#26)
* File location moving * Fixing terraform.tfvars * BIG changes in generate_bitops_config * Moving more files out of generators * DB Snap lookup fix * Big bump to DB things * Bumping engine_version for postgres to 11.17 * Dupe var fix * Moving EFS * Moved RDS module * Fixes in vars references * Eliminating deps from R53 to Certs * Moved ELB * cert for elb fix * Fixing Aurora module calling index * Fixes and moving EKS * Dupe removal * Fixing some cert issue in ELB - adding outputs * Tyiding things up - testing efs sg issue fix * env_merger debugging * EFS Access only from VPC to NFS port * Tidy up * Huge movement - EC2 to module, generator completely removed * Code cleanup --------- Co-authored-by: Max Cascone <[email protected]>
1 parent 610ce99 commit 220afea

File tree

87 files changed

+1530
-1274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1530
-1274
lines changed

.github/workflows/unit_tests.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Unit Tests
2+
3+
on:
4+
- push
5+
- workflow_dispatch
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
Unit-Tests:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Setup BATS
15+
uses: mig4/setup-bats@v1
16+
17+
- name: checkout
18+
uses: actions/checkout@v2
19+
20+
- name: unit tests
21+
run: bats $GITHUB_WORKSPACE/operations/_scripts/deploy/tests/.

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local.sh
2+
*.env
3+
4+
# Terraform
5+
*.terraform.lock*
6+
.terraform*
7+
8+
# BitOps
9+
bitops.config.yaml
10+
11+
# BitOps GHAs
12+
provider.tf
13+
terraform.tfvars
14+
15+
terraform-*
16+
operations/deployment/terraform/inventory.yaml
17+
18+
*.pem
19+
20+
.vscode/

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,17 @@ The following inputs can be used as `step.with` keys
197197
|------------------|---------|------------------------------------|
198198
| `aws_postgres_enable` | Boolean | Set to `true` to enable a postgres database. |
199199
| `aws_postgres_engine` | String | Which Database engine to use. Default is `aurora-postgresql`.|
200-
| `aws_postgres_engine_version` | String | Specify Postgres version. More information [here](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Updates.20180305.html). Default is `11.13`. |
200+
| `aws_postgres_engine_version` | String | Specify Postgres version. More information [here](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Updates.20180305.html). Default is `11.17`. |
201201
| `aws_postgres_database_group_family` | String | Specify aws database group family. Default is `aurora-postgresql11`. See [this](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/rds/create-db-parameter-group.html).|
202202
| `aws_postgres_instance_class` | String | Define the size of the instances in the DB cluster. Default is `db.t3.medium`. |
203203
| `aws_postgres_security_group_name` | String | The name of the Postgres security group. Defaults to `SG for ${aws_resource_identifier} - PG`. |
204204
| `aws_postgres_subnets` | String | Specify which subnets to use as a list of strings. Example: `i-1234,i-5678,i-9101`. |
205-
| `aws_postgres_database_name` | String | Specify a database name. Will be created if it does not exist. Default is `root`. |
205+
| `aws_postgres_cluster_name` | String | Specify a cluster name. Will be created if it does not exist. Defaults to `aws_resource_identifier`. |
206+
| `aws_postgres_database_name` | String | Specify a database name. Will be created if it does not exist. Defaults to `aws_resource_identifier`. |
206207
| `aws_postgres_database_port` | String | Specify a listening port for the database. Default is `5432`.|
208+
| `aws_postgres_restore_snapshot` | String | Restore a snapshot to the DB. Should be set only once. Changes in this value will destroy and recreate the database completely. |
209+
| `aws_postgres_snapshot_name` | String | Specify a database name. Will be created if it does not exist. Won't overwrite. |
210+
| `aws_postgres_snapshot_overwrite` | Boolean | Set to true to overwrite the snapshot. |
207211
| `aws_postgres_database_protection` | Boolean | Protects the database from deletion. Default is `false`.|
208212
| `aws_postgres_database_final_snapshot` | Boolean | Creates a snapshot before deletion. If a string is passed, it will be used as snapsthot name. Defaults to `false`.|
209213
<hr/>

action.yaml

+30-47
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,24 @@ inputs:
274274
aws_postgres_subnets:
275275
description: 'Specify which subnets to use as a list of strings. Example: `i-1234,i-5678,i-9101`'
276276
required: false
277+
aws_postgres_cluster_name:
278+
description: 'Specify a cluster name. Will be created if it does not exist'
279+
required: false
277280
aws_postgres_database_name:
278281
description: 'Specify a database name. Will be created if it does not exist'
279282
required: false
280283
aws_postgres_database_port:
281284
description: 'Postgres database port'
282285
required: false
286+
aws_postgres_restore_snapshot:
287+
description: 'Restore a snapshot to the DB. Should be used only once. Changes in this value will destroy and recreate the database completely.'
288+
required: false
289+
aws_postgres_snapshot_name:
290+
description: 'Takes a snapshot of the cluster using that name. If none definded, no snapshot will be made. If snap already exists, no new one will be created.'
291+
required: false
292+
aws_postgres_snapshot_overwrite:
293+
description: 'If the snapshot name is the same as an existing one, will destroy and create a new one.'
294+
required: false
283295
aws_postgres_database_protection:
284296
description: 'Protects the database from deletion.'
285297
required: false
@@ -492,8 +504,12 @@ runs:
492504
AWS_POSTGRES_INSTANCE_CLASS: ${{ inputs.aws_postgres_instance_class }}
493505
AWS_POSTGRES_SECURITY_GROUP_NAME: ${{ inputs.aws_postgres_security_group_name }}
494506
AWS_POSTGRES_SUBNETS: ${{ inputs.aws_postgres_subnets }}
507+
AWS_POSTGRES_CLUSTER_NAME: ${{ inputs.aws_postgres_cluster_name }}
495508
AWS_POSTGRES_DATABASE_NAME: ${{ inputs.aws_postgres_database_name }}
496509
AWS_POSTGRES_DATABASE_PORT: ${{ inputs.aws_postgres_database_port}}
510+
AWS_POSTGRES_RESTORE_SNAPSHOT: ${{ inputs.aws_postgres_restore_snapshot }}
511+
AWS_POSTGRES_SNAPSHOT_NAME: ${{ inputs.aws_postgres_snapshot_name }}
512+
AWS_POSTGRES_SNAPSHOT_OVERWRITE: ${{ inputs.aws_postgres_snapshot_overwrite }}
497513
AWS_POSTGRES_DATABASE_PROTECTION: ${{ inputs.aws_postgres_database_protection }}
498514
AWS_POSTGRES_DATABASE_FINAL_SNAPSHOT: ${{ inputs.aws_postgres_database_final_snapshot }}
499515

@@ -535,53 +551,20 @@ runs:
535551
$GITHUB_ACTION_PATH/operations/_scripts/deploy/export_vars.sh
536552
echo "Finished executions - Now to print results"
537553
538-
# output results to GitHub
539-
- if: ${{ success() && steps.deploy.outputs.vm_url != '' }}
540-
name: Print result created
541-
shell: bash
542-
run: |
543-
echo "## VM Created! :rocket:" >> $GITHUB_STEP_SUMMARY
544-
echo " ${{ steps.deploy.outputs.vm_url }}" >> $GITHUB_STEP_SUMMARY
545-
- if: ${{ success() && steps.deploy.outputs.vm_url == '' && inputs.bitops_code_only == 'true' && inputs.bitops_code_store == 'true' }}
546-
name: Print code generated and archived
554+
# always output results to GitHub Summary UI
555+
- name: Generate Summary Output
556+
if: ${{ always() }}
547557
shell: bash
548-
run: |
549-
echo "## BitOps Code generated. :tada: " >> $GITHUB_STEP_SUMMARY
550-
echo "Download the code artifact. Will be there for 5 days." >> $GITHUB_STEP_SUMMARY
551-
echo "Keep in mind that for creation, EFS should be created before EC2."
552-
echo "While destroying, EC2 should be destroyed before EFS. (Due to resources being in use)."
553-
echo "You can change that in the bitops.config.yaml file, or regenerate the code with destroy set."
554-
- if: ${{ success() && steps.deploy.outputs.vm_url == '' && inputs.bitops_code_only == 'true' && inputs.bitops_code_store != 'true' }}
555-
name: Print code built not archived
556-
shell: bash
557-
run: |
558-
echo "## BitOps Code generated. :tada: " >> $GITHUB_STEP_SUMMARY
559-
- if: ${{ success() && steps.deploy.outputs.vm_url == '' && inputs.tf_stack_destroy != 'true' && inputs.bitops_code_only != 'true' }}
560-
name: Print result deploy finished but no URL.
561-
shell: bash
562-
run: |
563-
echo "## Deploy finished! But no URL found. :thinking: " >> $GITHUB_STEP_SUMMARY
564-
echo "If expecting an URL, please check the logs for possible errors." >> $GITHUB_STEP_SUMMARY
565-
echo "If you consider this is a bug in the Github Action, please submit an issue to our repo." >> $GITHUB_STEP_SUMMARY
566-
- if: ${{ success() && steps.deploy.outputs.vm_url == '' && inputs.tf_stack_destroy == 'true' && inputs.tf_state_bucket_destroy != 'true' }}
567-
name: Print result destroyed no bucket
568-
shell: bash
569-
run: |
570-
echo "## VM Destroyed! :boom:" >> $GITHUB_STEP_SUMMARY
571-
echo "Infrastructure should be gone now!" >> $GITHUB_STEP_SUMMARY
572-
- if: ${{ success() && steps.deploy.outputs.vm_url == '' && inputs.tf_stack_destroy == 'true' && inputs.tf_state_bucket_destroy == 'true' }}
573-
name: Print result destroyed with bucket
574-
shell: bash
575-
run: |
576-
echo "## VM Destroyed! :boom:" >> $GITHUB_STEP_SUMMARY
577-
echo "Buckets and infrastructure should be gone now!" >> $GITHUB_STEP_SUMMARY
578-
- if: ${{ failure() }}
579-
name: Print error result
580-
shell: bash
581-
run: |
582-
echo "## Workflow failed to run :fire:" >> $GITHUB_STEP_SUMMARY
583-
echo "Please check the logs for possible errors." >> $GITHUB_STEP_SUMMARY
584-
echo "If you consider this is a bug in the Github Action, please submit an issue to our repo." >> $GITHUB_STEP_SUMMARY
558+
env:
559+
SUCCESS: ${{ job.status }} # success, failure, cancelled
560+
URL_OUTPUT: ${{ steps.deploy.outputs.vm_url }}
561+
BITOPS_CODE_ONLY: ${{ inputs.bitops_code_only }}
562+
BITOPS_CODE_STORE: ${{ inputs.bitops_code_store }}
563+
TF_STACK_DESTROY: ${{ inputs.tf_stack_destroy }}
564+
TF_STATE_BUCKET_DESTROY: ${{ inputs.tf_state_bucket_destroy }}
565+
run: $GITHUB_ACTION_PATH/operations/_scripts/deploy/summary.sh
566+
567+
# upload generated artifacts to GitHub if enabled
585568
- if: ${{ inputs.bitops_code_store == 'true' }}
586569
name: Archive production artifacts
587570
uses: actions/upload-artifact@v3
@@ -590,4 +573,4 @@ runs:
590573
retention-days: 5
591574
path: |
592575
${{ github.action_path }}/operations/generated_code
593-
!${{ github.action_path }}/operations/generated_code/**/ghs.env
576+
!${{ github.action_path }}/operations/generated_code/**/ghs.env

operations/_scripts/deploy/deploy.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ if [ -s "$GITHUB_WORKSPACE/$ENV_REPO" ] && [ -n "$ENV_REPO" ]; then
8787
cp "$GITHUB_WORKSPACE/$ENV_REPO" "${GITHUB_ACTION_PATH}/operations/deployment/env-files/repo.env"
8888
fi
8989

90+
cp -r "$GITHUB_ACTION_PATH/operations" /opt/bitops_deployment/generated_code
91+
9092
if [[ $(alpha_only "$BITOPS_SKIP_RUN") == true ]]; then
9193
echo "BitOps skip run is set to true. Reached end of the line."
9294
exit 0
@@ -98,7 +100,6 @@ for i in $(env | grep BITOPS_); do
98100
BITOPS_EXTRA_ENV_VARS="${BITOPS_EXTRA_ENV_VARS} -e ${i}"
99101
done
100102

101-
echo "BITOPS_EXTRA_ENV_VARS: $BITOPS_EXTRA_ENV_VARS"
102103

103104
echo "::group::BitOps Excecution"
104105
echo "Running BitOps for env: $BITOPS_ENVIRONMENT"

operations/_scripts/deploy/summary.sh

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC2086
3+
4+
### coming into this we have env vars:
5+
# SUCCESS=${{ job.status }} # success, cancelled, failure
6+
# URL_OUTPUT=${{ steps.deploy.outputs.vm_url }}
7+
# BITOPS_CODE_ONLY
8+
# BITOPS_CODE_STORE
9+
# TF_STACK_DESTROY
10+
# TF_STATE_BUCKET_DESTROY
11+
12+
# Create an error code mechanism so we don't have to check the actual static text,
13+
# just which case we fell into
14+
15+
# 0 - success
16+
# 1 - failure
17+
# 2 - failure, no URL # invalid case
18+
# 3 - failure, no URL, no code generated # invalid case
19+
# 4 - success, no URL
20+
# 5 - success, code generated, not archived
21+
# 6 - success, code generated, archived
22+
# 7 - success, code generated, archived, but no URL found # invalid case
23+
# 8 - success, destroy buckets and infrastructure
24+
# 9 - success, destroy infrastructure
25+
# 10 - cancelled
26+
27+
SUMMARY_CODE=0
28+
29+
if [[ $SUCCESS == 'success' ]]; then
30+
if [[ $URL_OUTPUT != '' ]]; then
31+
result_string="## Deploy Complete! :rocket:
32+
$URL_OUTPUT"
33+
34+
elif [[ $BITOPS_CODE_ONLY == 'true' ]]; then
35+
if [[ $BITOPS_CODE_STORE == 'true' ]]; then
36+
SUMMARY_CODE=6
37+
result_string="## BitOps Code generated. :tada:
38+
Download the code artifact. Will be there for 5 days.
39+
Keep in mind that for creation, EFS should be created before EC2.
40+
While destroying, EC2 should be destroyed before EFS. (Due to resources being in use).
41+
You can change that in the bitops.config.yaml file, or regenerate the code with destroy set."
42+
else
43+
SUMMARY_CODE=5
44+
result_string="## BitOps Code generated. :tada:"
45+
fi
46+
47+
elif [[ $TF_STACK_DESTROY == 'true' ]]; then
48+
if [[ $TF_STATE_BUCKET_DESTROY != 'true' ]]; then
49+
SUMMARY_CODE=9
50+
result_string="## VM Destroyed! :boom:
51+
Infrastructure should be gone now!"
52+
else
53+
SUMMARY_CODE=8
54+
result_string="## VM Destroyed! :boom:
55+
Buckets and infrastructure should be gone now!"
56+
fi
57+
58+
elif [[ $TF_STACK_DESTROY != 'true' && $BITOPS_CODE_ONLY != 'true' ]]; then
59+
SUMMARY_CODE=4
60+
result_string="## Deploy finished! But no URL found. :thinking:
61+
If expecting a URL, please check the logs for possible errors.
62+
If you consider this is a bug in the Github Action, please submit an issue to our repo."
63+
fi
64+
elif [[ $SUCCESS == 'cancelled' ]]; then
65+
SUMMARY_CODE=10
66+
result_string="## Workflow cancelled :warning:"
67+
68+
else
69+
SUMMARY_CODE=1
70+
result_string="## Workflow failed to run :fire:
71+
Please check the logs for possible errors.
72+
If you consider this is a bug in the Github Action, please submit an issue to our repo."
73+
fi
74+
75+
echo "$result_string" >> $GITHUB_STEP_SUMMARY
76+
echo "SUMMARY_CODE=$SUMMARY_CODE" >> $GITHUB_OUTPUT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bats
2+
3+
# see here for summary_codes
4+
# this runs once before all tests
5+
setup_file() {
6+
export file_under_test=$GITHUB_WORKSPACE/operations/_scripts/deploy/summary.sh
7+
export GITHUB_STEP_SUMMARY=$(mktemp ./test.XXX)
8+
export GITHUB_OUTPUT=$(mktemp ./test.XXX)
9+
}
10+
11+
# this runs once after all tests
12+
teardown_file() {
13+
rm -f $GITHUB_STEP_SUMMARY $GITHUB_OUTPUT
14+
}
15+
16+
function runTest() {
17+
# Run the script
18+
source $file_under_test
19+
20+
# take the passed-in expected result code
21+
expected_result=$1
22+
23+
# Compare the results
24+
# SUMMARY_CODE is set in summary script as an output
25+
source $GITHUB_OUTPUT
26+
[[ "$SUMMARY_CODE" = "$expected_result" ]]
27+
}
28+
29+
@test "SUCCESS is true, URL_OUTPUT is not empty" {
30+
# Set the environment variables
31+
export SUCCESS='success'
32+
export URL_OUTPUT='example.com'
33+
34+
# Run the test and pass in the expected result code
35+
runTest 0
36+
}
37+
38+
@test "SUCCESS is true, URL_OUTPUT is empty, BITOPS_CODE_ONLY is true, BITOPS_CODE_STORE is true" {
39+
export SUCCESS='success'
40+
export URL_OUTPUT=''
41+
export BITOPS_CODE_ONLY='true'
42+
export BITOPS_CODE_STORE='true'
43+
44+
runTest 6
45+
}
46+
47+
@test "SUCCESS is true, URL_OUTPUT is empty, BITOPS_CODE_ONLY is true, BITOPS_CODE_STORE is false" {
48+
export SUCCESS='success'
49+
export URL_OUTPUT=''
50+
export BITOPS_CODE_ONLY='true'
51+
export BITOPS_CODE_STORE='false'
52+
53+
runTest 5
54+
}
55+
56+
@test "SUCCESS is true, URL_OUTPUT is empty, TF_STACK_DESTROY is true, TF_STATE_BUCKET_DESTROY is false" {
57+
export SUCCESS='success'
58+
export URL_OUTPUT=''
59+
export BITOPS_CODE_ONLY='false'
60+
export TF_STACK_DESTROY='true'
61+
export TF_STATE_BUCKET_DESTROY='false'
62+
63+
runTest 9
64+
}
65+
66+
@test "SUCCESS is true, URL_OUTPUT is empty, TF_STACK_DESTROY is true, TF_STATE_BUCKET_DESTROY is true" {
67+
export SUCCESS='success'
68+
export URL_OUTPUT=''
69+
export BITOPS_CODE_ONLY='false'
70+
export TF_STACK_DESTROY='true'
71+
export TF_STATE_BUCKET_DESTROY='true'
72+
73+
runTest 8
74+
}
75+
76+
@test "SUCCESS is true, URL_OUTPUT is empty, BITOPS_CODE_ONLY is false, TF_STACK_DESTROY is false" {
77+
export SUCCESS='success'
78+
export URL_OUTPUT=''
79+
export BITOPS_CODE_ONLY='false'
80+
export TF_STACK_DESTROY='false'
81+
export BITOPS_CODE_STORE='false'
82+
83+
runTest 4
84+
}
85+
86+
@test "SUCCESS is false" {
87+
export SUCCESS='false'
88+
89+
runTest 1
90+
91+
}
92+
93+
@test "SUCCESS is 'cancelled'" {
94+
export SUCCESS='cancelled'
95+
96+
runTest 10
97+
}

0 commit comments

Comments
 (0)