Skip to content

Commit 4c7b17c

Browse files
rootroot
authored andcommitted
test
1 parent 40fcd74 commit 4c7b17c

File tree

18 files changed

+440
-28
lines changed

18 files changed

+440
-28
lines changed

.terrahub.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ template:
1515
provider:
1616
google: {}
1717
locals:
18-
google_project_id: terrahub-123456
18+
google_project_id: terrahub-demo-f2754a99
1919
google_billing_account: 123456-ABCDEF-ZYXWVU

README.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ Your output should be similar to the one below:
114114
Run the following command in terminal:
115115
```shell
116116
terrahub component -t google_storage_bucket -n demo_storage_bucket \
117-
&& terrahub component -t google_cloudfunctions_function -n demo_function -o ../demo_storage_bucket
117+
&& terrahub component -t google_cloudfunctions_function -n demo_function -o ../demo_storage_bucket \
118+
&& terrahub component -t google_storage_bucket -n demo_www -o ../demo_storage_bucket
118119
```
119120

120121
Your output should be similar to the one below:
@@ -142,7 +143,7 @@ Your output should be similar to the one below:
142143
Run the following command in terminal:
143144
```shell
144145
terrahub configure -i demo_storage_bucket -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/demo_storage_bucket/terraform.tfstate'
145-
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.name='demo_storage_bucket_${local.project["code"]}'
146+
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.name='demo_storage_bucket_a123456b'
146147
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.location='US'
147148
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.project='${local.google_project_id}'
148149
terrahub configure -i demo_storage_bucket -c component.template.variable -D -y
@@ -161,7 +162,7 @@ terrahub configure -i demo_function -c component.template.terraform.backend.loca
161162
terrahub configure -i demo_function -c component.template.data.terraform_remote_state.storage.backend='local'
162163
terrahub configure -i demo_function -c component.template.data.terraform_remote_state.storage.config.path='/tmp/.terrahub/local_backend/demo_storage_bucket/terraform.tfstate'
163164
terrahub configure -i demo_function -c component.template.resource.google_storage_bucket_object.demo_object.name='demo.zip'
164-
terrahub configure -i demo_function -c component.template.resource.google_storage_bucket_object.demo_object.bucket='${data.terraform_remote_state.storage.thub_id}'
165+
terrahub configure -i demo_function -c component.template.resource.google_storage_bucket_object.demo_object.bucket='${data.terraform_remote_state.storage.thub_id}/deploy/demo_function'
165166
terrahub configure -i demo_function -c component.template.resource.google_storage_bucket_object.demo_object.source='./demo.zip'
166167
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.depends_on[0]='google_storage_bucket_object.demo_object'
167168
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.name='demofunction${local.project["code"]}'
@@ -179,18 +180,25 @@ terrahub configure -i demo_function -c component.template.output -D -y
179180
terrahub configure -i demo_function -c component.template.output.id.value='${google_cloudfunctions_function.demo_function.id}'
180181
terrahub configure -i demo_function -c component.template.output.trigger_url.value='${google_cloudfunctions_function.demo_function.https_trigger_url}'
181182
terrahub configure -i demo_function -c build.env.variables.THUB_FUNCTION_ZIP='demo.zip'
182-
terrahub configure -i demo_function -c build.env.variables.THUB_BUILD_PATH='..'
183183
terrahub configure -i demo_function -c build.env.variables.THUB_FUNCTION_TXT='demo.txt'
184-
terrahub configure -i demo_function -c build.env.variables.THUB_BUILD_OK='true'
184+
terrahub configure -i demo_function -c build.env.variables.COMPONENT_NAME='demo_function'
185+
terrahub configure -i demo_function -c build.env.variables.OBJECT_NAME='demo_object'
186+
terrahub configure -i demo_function -c build.env.variables.THUB_BUILD_PATH='..'
187+
terrahub configure -i demo_function -c build.env.variables.THUB_BUILD_OK='false'
188+
terrahub configure -i demo_function -c build.env.variables.THUB_BUCKET_PATH='gs://demo_storage_bucket_a123456b'
189+
terrahub configure -i demo_function -c build.env.variables.THUB_BUCKET_KEY: 'deploy/demo_function'
185190
terrahub configure -i demo_function -c build.phases.pre_build.commands[0]='echo "BUILD: Running pre_build step"'
186-
terrahub configure -i demo_function -c build.phases.pre_build.commands[1]='if [ ! -e "$THUB_FUNCTION_TXT" ]; then touch "$THUB_FUNCTION_TXT"; fi'
191+
terrahub configure -i demo_function -c build.phases.pre_build.commands[1]='./scripts/download.sh $THUB_FUNCTION_TXT $THUB_BUCKET_PATH/$THUB_BUCKET_KEY/$THUB_FUNCTION_TXT'
192+
terrahub configure -i demo_function -c build.phases.pre_build.commands[2]='./scripts/compare.sh $THUB_FUNCTION_TXT $THUB_BUILD_PATH/*.js'
187193
terrahub configure -i demo_function -c build.phases.pre_build.finally[0]='echo "BUILD: pre_build step successful"'
188194
terrahub configure -i demo_function -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
189-
terrahub configure -i demo_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 demo_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 demo_function -c component.template.resource.google_storage_bucket_object.demo_object.name=$(date +%s).zip; fi'
195+
terrahub configure -i demo_function -c build.phases.build.commands[1]='./scripts/build.sh $COMPONENT_NAME $OBJECT_NAME $THUB_BUCKET_KEY/'
191196
terrahub configure -i demo_function -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
192197
terrahub configure -i demo_function -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
193-
terrahub configure -i demo_function -c build.phases.post_build.commands[1]='echo $(stat -c %y "${THUB_BUILD_PATH}/index.js") > "$THUB_FUNCTION_TXT"'
198+
terrahub configure -i demo_function -c build.phases.post_build.commands[1]='./scripts/shasum.sh $THUB_FUNCTION_TXT'
199+
terrahub configure -i demo_function -c build.phases.post_build.commands[2]='./scripts/zip.sh $THUB_FUNCTION_ZIP $THUB_BUILD_PATH/*.js'
200+
terrahub configure -i demo_function -c build.phases.post_build.commands[3]='./scripts/upload.sh $THUB_FUNCTION_TXT $THUB_BUCKET_PATH/$THUB_BUCKET_KEY/$THUB_FUNCTION_TXT'
201+
terrahub configure -i demo_function -c build.phases.post_build.commands[4]='rm -f .terrahub_build.env $THUB_FUNCTION_TXT'
194202
terrahub configure -i demo_function -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
195203
```
196204

@@ -199,6 +207,19 @@ Your output should be similar to the one below:
199207
✅ Done
200208
```
201209

210+
## Customize TerraHub Component for Frontend
211+
212+
Run the following command in terminal:
213+
```shell
214+
terrahub configure -i demo_www -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/demo_www/terraform.tfstate'
215+
216+
```
217+
218+
Your output should be similar to the one below:
219+
```
220+
✅ Done
221+
```
222+
202223
## Visualize TerraHub Components
203224

204225
Run the following command in terminal:
@@ -218,7 +239,8 @@ Project: demo-terraform-automation-google
218239

219240
Run the following command in terminal:
220241
```shell
221-
terrahub build -i demo_function
242+
terrahub run -a -y -i demo_storage
243+
terrahub build -i demo_function,demo_www
222244
terrahub run -a -y
223245
```
224246

demo_function/.terrahub.yml

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ component:
2222
entry_point: helloGET
2323
google_storage_bucket_object:
2424
demo_object:
25-
name: demo.zip
25+
name: deploy/demo_function/1551778012.zip
2626
bucket: '${data.terraform_remote_state.storage.thub_id}'
2727
source: ./demo.zip
2828
terraform:
@@ -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: demo_function
49+
OBJECT_NAME: demo_object
50+
THUB_BUILD_PATH: ..
51+
THUB_BUILD_OK: 'false'
52+
THUB_BUCKET_PATH: 'gs://demo_storage_bucket_7b3c5d2c'
53+
THUB_BUCKET_KEY: deploy/demo_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-
demo_function -c
68-
component.template.resource.google_storage_bucket_object.demo_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"'

demo_function/demo.zip

233 Bytes
Binary file not shown.

demo_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

demo_function/scripts/compare.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
# sed -i 's/"${var.lambda_publish}"/"${var.lambda_publish}"\nsource_code_hash = "${base64sha256("'${THUB_SHA}'")}"/g' ./main.tf
43+
else
44+
echo 'Build is NOT required.'
45+
fi

demo_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

demo_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

demo_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

demo_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

0 commit comments

Comments
 (0)