Skip to content

Commit 6b6cebd

Browse files
authored
Merge pull request #12 from TerraHubCorp/dev
Transform HCL into TerraHub config with template
2 parents 72e54ae + c4deee7 commit 6b6cebd

File tree

10 files changed

+254
-45
lines changed

10 files changed

+254
-45
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ Manual Setup (set values in double quotes and run the following command in termi
1616
```shell
1717
export GOOGLE_CLOUD_PROJECT="" ## e.g. terrahub-123456
1818
export GOOGLE_APPLICATION_CREDENTIALS="" ## e.g. ${HOME}/.config/gcloud/terraform.json
19-
export BILLING_ID="" ## e.g. 123456-ABCDEF-ZYXWVU
20-
export PROJECT_NAME="" ## e.g. TerraHub
21-
export IAM_NAME="" ## e.g. terraform
22-
export IAM_DESC="" ## e.g. terraform service account
19+
export BILLING_ID="" ## e.g. 123456-ABCDEF-ZYXWVU
20+
export PROJECT_NAME="" ## e.g. TerraHub
21+
export IAM_NAME="" ## e.g. terraform
22+
export IAM_DESC="" ## e.g. terraform service account
23+
export STORAGE_BUCKET="" ## e.g. terrahub_bucket_123456
2324
```
2425
### Setup BILLING_ID Programmatically
2526

@@ -142,8 +143,9 @@ Your output should be similar to the one below:
142143
Run the following command in terminal:
143144
```shell
144145
terrahub configure -i google_storage -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_storage/terraform.tfstate'
145-
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.name='demo_terraform_automation_${local.project["code"]}'
146+
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.name="${STORAGE_BUCKET}"
146147
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.location='US'
148+
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.force_destroy='true'
147149
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.project='${local.google_project_id}'
148150
terrahub configure -i google_storage -c component.template.variable -D -y
149151
```
@@ -179,18 +181,25 @@ terrahub configure -i google_function -c component.template.output -D -y
179181
terrahub configure -i google_function -c component.template.output.id.value='${google_cloudfunctions_function.google_function.id}'
180182
terrahub configure -i google_function -c component.template.output.trigger_url.value='${google_cloudfunctions_function.google_function.https_trigger_url}'
181183
terrahub configure -i google_function -c build.env.variables.THUB_FUNCTION_ZIP='demo.zip'
182-
terrahub configure -i google_function -c build.env.variables.THUB_BUILD_PATH='..'
183184
terrahub configure -i google_function -c build.env.variables.THUB_FUNCTION_TXT='demo.txt'
184-
terrahub configure -i google_function -c build.env.variables.THUB_BUILD_OK='true'
185+
terrahub configure -i google_function -c build.env.variables.COMPONENT_NAME='google_function'
186+
terrahub configure -i google_function -c build.env.variables.OBJECT_NAME='google_storage_object'
187+
terrahub configure -i google_function -c build.env.variables.THUB_BUILD_PATH='..'
188+
terrahub configure -i google_function -c build.env.variables.THUB_BUILD_OK='false'
189+
terrahub configure -i google_function -c build.env.variables.THUB_BUCKET_PATH="gs://${STORAGE_BUCKET}"
190+
terrahub configure -i google_function -c build.env.variables.THUB_BUCKET_KEY='deploy/google_function'
185191
terrahub configure -i google_function -c build.phases.pre_build.commands[0]='echo "BUILD: Running pre_build step"'
186-
terrahub configure -i google_function -c build.phases.pre_build.commands[1]='if [ ! -e "$THUB_FUNCTION_TXT" ]; then touch "$THUB_FUNCTION_TXT"; fi'
192+
terrahub configure -i google_function -c build.phases.pre_build.commands[1]='./scripts/download.sh $THUB_FUNCTION_TXT $THUB_BUCKET_PATH/$THUB_BUCKET_KEY/$THUB_FUNCTION_TXT'
193+
terrahub configure -i google_function -c build.phases.pre_build.commands[2]='./scripts/compare.sh $THUB_FUNCTION_TXT $THUB_BUILD_PATH/*.js'
187194
terrahub configure -i google_function -c build.phases.pre_build.finally[0]='echo "BUILD: pre_build step successful"'
188195
terrahub configure -i google_function -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
189-
terrahub configure -i google_function -c build.phases.build.commands[1]='if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y "${THUB_BUILD_PATH}/index.js")" ]; then zip -j ${THUB_FUNCTION_ZIP} ${THUB_BUILD_PATH}/index.js; fi'
190-
terrahub configure -i google_function -c build.phases.build.commands[2]='if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y "${THUB_BUILD_PATH}/index.js")" ]; then terrahub configure -i google_function -c component.template.resource.google_storage_bucket_object.google_storage_object.name=$(date +%s).zip; fi'
196+
terrahub configure -i google_function -c build.phases.build.commands[1]='./scripts/build.sh $COMPONENT_NAME $OBJECT_NAME $THUB_BUCKET_KEY/'
191197
terrahub configure -i google_function -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
192198
terrahub configure -i google_function -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
193-
terrahub configure -i google_function -c build.phases.post_build.commands[1]='echo $(stat -c %y "${THUB_BUILD_PATH}/index.js") > "$THUB_FUNCTION_TXT"'
199+
terrahub configure -i google_function -c build.phases.post_build.commands[1]='./scripts/shasum.sh $THUB_FUNCTION_TXT'
200+
terrahub configure -i google_function -c build.phases.post_build.commands[2]='./scripts/zip.sh $THUB_FUNCTION_ZIP $THUB_BUILD_PATH/*.js'
201+
terrahub configure -i google_function -c build.phases.post_build.commands[3]='./scripts/upload.sh $THUB_FUNCTION_TXT $THUB_BUCKET_PATH/$THUB_BUCKET_KEY/$THUB_FUNCTION_TXT'
202+
terrahub configure -i google_function -c build.phases.post_build.commands[4]='rm -f .terrahub_build.env $THUB_FUNCTION_TXT'
194203
terrahub configure -i google_function -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
195204
```
196205

@@ -218,6 +227,7 @@ Project: demo-terraform-automation-google
218227

219228
Run the following command in terminal:
220229
```shell
230+
terrahub run -a -y -i google_storage
221231
terrahub build -i google_function
222232
terrahub run -a -y
223233
```
@@ -231,4 +241,4 @@ Your output should be similar to the one below:
231241
Run the following command in terminal:
232242
```
233243
curl https://us-central1-terrahub-123456.cloudfunctions.net/demofunctionxxxxxxxx
234-
```
244+
```

google_function/.terrahub.yml

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@ component:
55
dependsOn:
66
- ../google_storage
77
template:
8+
terraform:
9+
backend:
10+
local:
11+
path: /tmp/.terrahub/local_backend/google_function/terraform.tfstate
12+
data:
13+
terraform_remote_state:
14+
storage:
15+
backend: local
16+
config:
17+
path: /tmp/.terrahub/local_backend/google_storage/terraform.tfstate
818
resource:
19+
google_storage_bucket_object:
20+
google_storage_object:
21+
name: demo.zip
22+
bucket: '${data.terraform_remote_state.storage.thub_id}'
23+
source: ./demo.zip
924
google_cloudfunctions_function:
1025
google_function:
11-
name: 'demofunction${local.project["code"]}'
1226
depends_on:
1327
- google_storage_bucket_object.google_storage_object
28+
name: 'demofunction${local.project["code"]}'
1429
region: us-central1
1530
runtime: nodejs8
1631
description: My demo function
@@ -20,21 +35,6 @@ component:
2035
trigger_http: true
2136
timeout: 60
2237
entry_point: helloGET
23-
google_storage_bucket_object:
24-
google_storage_object:
25-
name: demo.zip
26-
bucket: '${data.terraform_remote_state.storage.thub_id}'
27-
source: ./demo.zip
28-
terraform:
29-
backend:
30-
local:
31-
path: /tmp/.terrahub/local_backend/google_function/terraform.tfstate
32-
data:
33-
terraform_remote_state:
34-
storage:
35-
backend: local
36-
config:
37-
path: /tmp/.terrahub/local_backend/google_storage/terraform.tfstate
3838
output:
3939
id:
4040
value: '${google_cloudfunctions_function.google_function.id}'
@@ -44,36 +44,37 @@ build:
4444
env:
4545
variables:
4646
THUB_FUNCTION_ZIP: demo.zip
47-
THUB_BUILD_PATH: ..
4847
THUB_FUNCTION_TXT: demo.txt
49-
THUB_BUILD_OK: true
48+
COMPONENT_NAME: google_function
49+
OBJECT_NAME: google_storage_object
50+
THUB_BUILD_PATH: ..
51+
THUB_BUILD_OK: false
52+
THUB_BUCKET_PATH: 'gs://terrahub_bucket_123456'
53+
THUB_BUCKET_KEY: deploy/google_function
5054
phases:
5155
pre_build:
5256
commands:
5357
- 'echo "BUILD: Running pre_build step"'
54-
- 'if [ ! -e "$THUB_FUNCTION_TXT" ]; then touch "$THUB_FUNCTION_TXT"; fi'
58+
- >-
59+
./scripts/download.sh $THUB_FUNCTION_TXT
60+
$THUB_BUCKET_PATH/$THUB_BUCKET_KEY/$THUB_FUNCTION_TXT
61+
- ./scripts/compare.sh $THUB_FUNCTION_TXT $THUB_BUILD_PATH/*.js
5562
finally:
5663
- 'echo "BUILD: pre_build step successful"'
5764
build:
5865
commands:
5966
- 'echo "BUILD: Running build step"'
60-
- >-
61-
if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y
62-
"${THUB_BUILD_PATH}/index.js")" ]; then zip -j ${THUB_FUNCTION_ZIP}
63-
${THUB_BUILD_PATH}/index.js; fi
64-
- >-
65-
if [ "$(head -n 1 "$THUB_FUNCTION_TXT")" != "$(stat -c %y
66-
"${THUB_BUILD_PATH}/index.js")" ]; then terrahub configure -i
67-
google_function -c
68-
component.template.resource.google_storage_bucket_object.google_storage_object.name=$(date
69-
+%s).zip; fi
67+
- ./scripts/build.sh $COMPONENT_NAME $OBJECT_NAME $THUB_BUCKET_KEY/
7068
finally:
7169
- 'echo "BUILD: build step successful"'
7270
post_build:
7371
commands:
7472
- 'echo "BUILD: Running post_build step"'
73+
- ./scripts/shasum.sh $THUB_FUNCTION_TXT
74+
- ./scripts/zip.sh $THUB_FUNCTION_ZIP $THUB_BUILD_PATH/*.js
7575
- >-
76-
echo $(stat -c %y "${THUB_BUILD_PATH}/index.js") >
77-
"$THUB_FUNCTION_TXT"
76+
./scripts/upload.sh $THUB_FUNCTION_TXT
77+
$THUB_BUCKET_PATH/$THUB_BUCKET_KEY/$THUB_FUNCTION_TXT
78+
- rm -f .terrahub_build.env $THUB_FUNCTION_TXT
7879
finally:
7980
- 'echo "BUILD: post_build step successful"'

google_function/scripts/build.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
## Component name
4+
COMPONENT_NAME=${1}
5+
if [ -z "${COMPONENT_NAME}" ]; then
6+
echo >&2 'ERROR: COMPONENT_NAME variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## Object name
11+
OBJECT_NAME=${2}
12+
if [ -z "${OBJECT_NAME}" ]; then
13+
echo >&2 'ERROR: OBJECT_NAME variable is empty. Aborting...'
14+
exit 1
15+
fi
16+
17+
## Bucket key
18+
THUB_BUCKET_KEY=${3}
19+
if [ -z "${THUB_BUCKET_KEY}" ]; then
20+
echo >&2 'ERROR: THUB_BUCKET_KEY variable is empty. Aborting...'
21+
exit 1
22+
fi
23+
24+
## Setup environmental variables
25+
[ -f .terrahub_build.env ] && . .terrahub_build.env
26+
27+
## Checking if THUB_BUILD_OK is true
28+
if [ "$THUB_BUILD_OK" == "true" ]; then
29+
OBJECT_IN_COMPONENT='component.template.resource.google_storage_bucket_object.'${OBJECT_NAME}'.name'
30+
terrahub configure -i ${COMPONENT_NAME} -c ${OBJECT_IN_COMPONENT}=${THUB_BUCKET_KEY}$(date +%s).zip
31+
echo 'Build was executed'
32+
else
33+
echo 'Build was NOT executed'
34+
fi

google_function/scripts/compare.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
## Source path
4+
THUB_SRC=${1}
5+
if [ -z "${THUB_SRC}" ]; then
6+
echo >&2 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## Source files for build process
11+
THUB_COMPARE_PATH="${@:2}"
12+
if [ -z "${THUB_COMPARE_PATH}" ]; then
13+
echo 'ERROR: THUB_COMPARE_PATH variable is empty. Aborting...'
14+
exit 1
15+
fi
16+
17+
## Setup environmental variables
18+
[ -f .terrahub_build.env ] && . .terrahub_build.env
19+
20+
## Compare SHA256 sums from THUB_SRC file with files in THUB_COMPARE_PATH
21+
echo "THUB_BUILD_OK='${THUB_BUILD_OK}' ==> Comparing SHA256 sums."
22+
if [ "$(uname)" == "Darwin" ]; then
23+
THUB_SHA=$(find -s ${THUB_COMPARE_PATH} -path **/node_modules -prune -o -type f -exec shasum -a 256 {} \; | sort -k 2 | shasum -a 256 | cut -f 1 -d " ")
24+
else
25+
THUB_SHA=$(find ${THUB_COMPARE_PATH} -path **/node_modules -prune -o -type f -exec shasum -a 256 {} \; | sort -k 2 | shasum -a 256 | cut -f 1 -d " ")
26+
fi
27+
28+
## Checking if needs to skip SHA256 sums compare
29+
echo "export THUB_SHA=\"${THUB_SHA}\"" >> .terrahub_build.env
30+
echo "INFO: Current SHA256 => ${THUB_SHA}"
31+
if [ "${THUB_BUILD_OK}" == "true" ]; then
32+
echo "INFO: THUB_BUILD_OK='${THUB_BUILD_OK}' ==> Skipping comparing SHA256 sums."
33+
exit 0
34+
fi
35+
36+
## Checking if the project requires to be built
37+
THUB_COMPARE=$(head -n 1 ${THUB_SRC})
38+
echo "INFO: S3 Object SHA256 => ${THUB_COMPARE}"
39+
if [ "${THUB_SHA}" != "${THUB_COMPARE}" ]; then
40+
echo 'Build is required!'
41+
echo 'export THUB_BUILD_OK="true"' >> .terrahub_build.env
42+
fi

google_function/scripts/download.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
## Source path
4+
THUB_SRC=${1}
5+
if [ -z "${THUB_SRC}" ]; then
6+
echo >&2 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## S3 bucket name
11+
THUB_S3_PATH=${2-${THUB_S3_PATH}}
12+
if [ -z "${THUB_S3_PATH}" ]; then
13+
echo >&2 'ERROR: THUB_S3_PATH variable is empty. Aborting...'
14+
exit 1
15+
fi
16+
17+
## Clean environmental variables
18+
> .terrahub_build.env
19+
20+
## Checking if THUB_S3_PATH file exists in S3
21+
gsutil --version > /dev/null 2>&1 || { echo >&2 'gsutil is missing. Aborting...'; exit 1; }
22+
THUB_CHECK_TYPE=$(gsutil ls ${THUB_S3_PATH} || echo "")
23+
if [ -z "${THUB_CHECK_TYPE}" ]; then
24+
echo "INFO: ${THUB_S3_PATH} does NOT exist ==> First execution."
25+
echo 'export THUB_BUILD_OK="true"' >> .terrahub_build.env
26+
exit 0
27+
fi
28+
29+
## Downloading from S3
30+
echo 'Downloading THUB_SRC from THUB_S3_PATH'
31+
if [[ $THUB_CHECK_TYPE = *" PRE "* ]]; then
32+
gsutil rsync ${THUB_S3_PATH} ${THUB_SRC}
33+
else
34+
gsutil cp ${THUB_S3_PATH} ${THUB_SRC}
35+
fi

google_function/scripts/shasum.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
## Source path
4+
THUB_SRC=${1}
5+
if [ -z "${THUB_SRC}" ]; then
6+
echo >&2 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## Setup environmental variables
11+
[ -f .terrahub_build.env ] && . .terrahub_build.env
12+
13+
## Checking if THUB_BUILD_OK is true
14+
if [ "$THUB_BUILD_OK" == "true" ]; then
15+
## Checking if SHA256 sums exists
16+
if [ -z "${THUB_SHA}" ]; then
17+
echo >&2 'ERROR: THUB_SHA variable is empty. Aborting...'
18+
exit 1
19+
fi
20+
21+
## Write current SHA256 to THUB_SRC
22+
echo "${THUB_SHA}" > ${THUB_SRC}
23+
else
24+
echo 'Build was NOT executed ==> SHA256 will NOT be updated.'
25+
fi

google_function/scripts/upload.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
## Source path
4+
THUB_SRC=${1}
5+
if [ -z "${THUB_SRC}" ]; then
6+
echo >&2 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## S3 bucket name
11+
THUB_S3_PATH=${2-${THUB_S3_PATH}}
12+
if [ -z "${THUB_S3_PATH}" ]; then
13+
echo >&2 'ERROR: THUB_S3_PATH variable is empty. Aborting...'
14+
exit 1
15+
fi
16+
17+
## Setup environmental variables
18+
[ -f .terrahub_build.env ] && . .terrahub_build.env
19+
20+
## Checking if THUB_BUILD_OK is true
21+
if [ "${THUB_BUILD_OK}" == "true" ]; then
22+
## Sync THUB_SRC to THUB_S3_PATH
23+
gsutil --version > /dev/null 2>&1 || { echo >&2 'gsutil is missing. Aborting...'; exit 1; }
24+
if [[ -d "${THUB_SRC}" ]]; then
25+
gsutil rsync ${THUB_SRC} ${THUB_S3_PATH}
26+
elif [[ -f "${THUB_SRC}" ]]; then
27+
gsutil cp ${THUB_SRC} ${THUB_S3_PATH}
28+
else
29+
echo >&2 "ERROR: ${THUB_SRC} is not valid"
30+
exit 1
31+
fi
32+
else
33+
echo 'Build was NOT executed ==> Files will NOT be uploaded.'
34+
fi

google_function/scripts/zip.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
## Source path
4+
THUB_SRC=${1}
5+
if [ -z "${THUB_SRC}" ]; then
6+
echo >&2 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## Source files or folders for zip process
11+
THUB_ZIP_PATH="${@:2}"
12+
if [ -z "${THUB_ZIP_PATH}" ]; then
13+
echo >&2 'ERROR: THUB_ZIP_PATH variable is empty. Aborting...'
14+
exit 1
15+
fi
16+
17+
## Setup environmental variables
18+
[ -f .terrahub_build.env ] && . .terrahub_build.env
19+
20+
## Checking if THUB_BUILD_OK is true
21+
if [ "$THUB_BUILD_OK" == "true" ]; then
22+
zip --version > /dev/null 2>&1 || { echo >&2 'zip is missing. Aborting...'; exit 1; }
23+
zip -j ${THUB_SRC} ${THUB_ZIP_PATH}
24+
echo 'Build was executed ==> zip file was created.'
25+
else
26+
echo 'Build was NOT executed ==> zip file was NOT created.'
27+
fi

google_storage/.terrahub.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ component:
66
resource:
77
google_storage_bucket:
88
google_storage:
9-
name: 'demo_terraform_automation_${local.project["code"]}'
9+
name: terrahub_bucket_123456
1010
location: US
11+
force_destroy: true
1112
project: '${local.google_project_id}'
1213
output:
1314
id:

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exports.helloGET = (req, res) => {
2-
res.send('Hello World!');
2+
res.send("Hello World!\n");
33
};

0 commit comments

Comments
 (0)