Skip to content

Commit f2b99af

Browse files
committed
another set of bug fixing
1 parent 8e33d46 commit f2b99af

File tree

14 files changed

+393
-26
lines changed

14 files changed

+393
-26
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ Run the following command in terminal:
212212
```shell
213213
terrahub component -t google_storage_bucket -n google_storage \
214214
&& terrahub component -t google_cloudfunctions_function -n google_function -o ../google_storage \
215-
&& terrahub component -t google_storage_bucket -n static_website -d ./www/.terrahub \
216-
&& terrahub component -t google_storage_bucket_iam_member -n iam_object_viewer -d ./www/.terrahub -o ../static_website
215+
&& terrahub component -t google_storage_bucket -n static_website \
216+
&& terrahub component -t google_storage_bucket_iam_member -n iam_object_viewer -o ../static_website
217217
```
218218

219219
Your output should be similar to the one below:
@@ -305,22 +305,22 @@ terrahub configure -i static_website -c component.template.resource.google_stora
305305
terrahub configure -i static_website -c component.template.variable -D -y
306306
terrahub configure -i static_website -c build.env.variables.THUB_ENV='dev'
307307
terrahub configure -i static_website -c build.env.variables.THUB_INDEX_FILE='www.txt'
308-
terrahub configure -i static_website -c build.env.variables.THUB_S3_PATH="gs://${STORAGE_BUCKET}_website"
308+
terrahub configure -i static_website -c build.env.variables.THUB_GS_PATH="gs://${STORAGE_BUCKET}_website"
309309
terrahub configure -i static_website -c build.env.variables.THUB_ROBOTS='../../robots.dev.txt'
310310
terrahub configure -i static_website -c build.env.variables.THUB_BUILD_PATH='../../build'
311311
terrahub configure -i static_website -c build.env.variables.THUB_SOURCE_PATH='../../assets ../../static/fonts ../../static/img ../../views'
312312
terrahub configure -i static_website -c build.env.variables.THUB_BUILD_OK='false'
313313
terrahub configure -i static_website -c build.env.variables.THUB_MAX_AGE='600'
314314
terrahub configure -i static_website -c build.phases.pre_build.commands[0]='echo "BUILD: Running pre_build step"'
315-
terrahub configure -i static_website -c build.phases.pre_build.commands[1]='./scripts/download.sh $THUB_INDEX_FILE $THUB_S3_PATH/$THUB_INDEX_FILE'
315+
terrahub configure -i static_website -c build.phases.pre_build.commands[1]='./scripts/download.sh $THUB_INDEX_FILE $THUB_GS_PATH/$THUB_INDEX_FILE'
316316
terrahub configure -i static_website -c build.phases.pre_build.commands[2]='./scripts/compare.sh $THUB_INDEX_FILE $THUB_SOURCE_PATH'
317317
terrahub configure -i static_website -c build.phases.pre_build.finally[0]='echo "BUILD: pre_build step successful"'
318318
terrahub configure -i static_website -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
319319
terrahub configure -i static_website -c build.phases.build.commands[1]='../../bin/compile.sh'
320320
terrahub configure -i static_website -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
321321
terrahub configure -i static_website -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
322322
terrahub configure -i static_website -c build.phases.post_build.commands[1]='./scripts/shasum.sh $THUB_BUILD_PATH/$THUB_INDEX_FILE'
323-
terrahub configure -i static_website -c build.phases.post_build.commands[2]='./scripts/upload.sh $THUB_BUILD_PATH $THUB_S3_PATH --cache-control max-age=$THUB_MAX_AGE'
323+
terrahub configure -i static_website -c build.phases.post_build.commands[2]='./scripts/upload.sh $THUB_BUILD_PATH $THUB_GS_PATH --cache-control max-age=$THUB_MAX_AGE'
324324
terrahub configure -i static_website -c build.phases.post_build.commands[3]='rm -f .terrahub_build.env $THUB_INDEX_FILE'
325325
terrahub configure -i static_website -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
326326
```
@@ -360,8 +360,8 @@ Your output should be similar to the one below:
360360
Project: demo-terraform-automation-gcp
361361
├─ google_storage [path: ./google_storage]
362362
│ └─ google_function [path: ./google_function]
363-
└─ static_website [path: ./www/.terrahub/static_website]
364-
└─ iam_object_viewer [path: ./www/.terrahub/iam_object_viewer]
363+
└─ static_website [path: ./static_website]
364+
└─ iam_object_viewer [path: ./iam_object_viewer]
365365
```
366366

367367
## Run TerraHub Automation

google_function/scripts/compare.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fi
3535

3636
## Checking if the project requires to be built
3737
THUB_COMPARE=$(head -n 1 ${THUB_SRC})
38-
echo "INFO: S3 Object SHA256 => ${THUB_COMPARE}"
38+
echo "INFO: Google Storage Object SHA256 => ${THUB_COMPARE}"
3939
if [ "${THUB_SHA}" != "${THUB_COMPARE}" ]; then
4040
echo 'Build is required!'
4141
echo 'export THUB_BUILD_OK="true"' >> .terrahub_build.env

google_function/scripts/download.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ if [ -z "${THUB_SRC}" ]; then
77
exit 1
88
fi
99

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...'
10+
## Google Storage bucket name
11+
THUB_GS_PATH=${2-${THUB_GS_PATH}}
12+
if [ -z "${THUB_GS_PATH}" ]; then
13+
echo >&2 'ERROR: THUB_GS_PATH variable is empty. Aborting...'
1414
exit 1
1515
fi
1616

1717
## Clean environmental variables
1818
> .terrahub_build.env
1919

20-
## Checking if THUB_S3_PATH file exists in S3
20+
## Checking if THUB_GS_PATH file exists in Google Storage
2121
gsutil --version > /dev/null 2>&1 || { echo >&2 'gsutil is missing. Aborting...'; exit 1; }
22-
THUB_CHECK_TYPE=$(gsutil ls ${THUB_S3_PATH} || echo "")
22+
THUB_CHECK_TYPE=$(gsutil ls ${THUB_GS_PATH} || echo "")
2323
if [ -z "${THUB_CHECK_TYPE}" ]; then
24-
echo "INFO: ${THUB_S3_PATH} does NOT exist ==> First execution."
24+
echo "INFO: ${THUB_GS_PATH} does NOT exist ==> First execution."
2525
echo 'export THUB_BUILD_OK="true"' >> .terrahub_build.env
2626
exit 0
2727
fi
2828

29-
## Downloading from S3
30-
echo 'Downloading THUB_SRC from THUB_S3_PATH'
29+
## Downloading from Google Storage
30+
echo 'Downloading THUB_SRC from THUB_GS_PATH'
3131
if [[ $THUB_CHECK_TYPE = *" PRE "* ]]; then
32-
gsutil rsync ${THUB_S3_PATH} ${THUB_SRC}
32+
gsutil rsync ${THUB_GS_PATH} ${THUB_SRC}
3333
else
34-
gsutil cp ${THUB_S3_PATH} ${THUB_SRC}
34+
gsutil cp ${THUB_GS_PATH} ${THUB_SRC}
3535
fi

google_function/scripts/upload.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ if [ -z "${THUB_SRC}" ]; then
77
exit 1
88
fi
99

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...'
10+
## Google Storage bucket name
11+
THUB_GS_PATH=${2-${THUB_GS_PATH}}
12+
if [ -z "${THUB_GS_PATH}" ]; then
13+
echo >&2 'ERROR: THUB_GS_PATH variable is empty. Aborting...'
1414
exit 1
1515
fi
1616

@@ -19,12 +19,12 @@ fi
1919

2020
## Checking if THUB_BUILD_OK is true
2121
if [ "${THUB_BUILD_OK}" == "true" ]; then
22-
## Sync THUB_SRC to THUB_S3_PATH
22+
## Sync THUB_SRC to THUB_GS_PATH
2323
gsutil --version > /dev/null 2>&1 || { echo >&2 'gsutil is missing. Aborting...'; exit 1; }
2424
if [[ -d "${THUB_SRC}" ]]; then
25-
gsutil rsync ${THUB_SRC} ${THUB_S3_PATH}
25+
gsutil rsync ${THUB_SRC} ${THUB_GS_PATH}
2626
elif [[ -f "${THUB_SRC}" ]]; then
27-
gsutil cp ${THUB_SRC} ${THUB_S3_PATH}
27+
gsutil cp ${THUB_SRC} ${THUB_GS_PATH}
2828
else
2929
echo >&2 "ERROR: ${THUB_SRC} is not valid"
3030
exit 1

google_storage/scripts/compare.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
## Source path
4+
THUB_SRC=${1}
5+
if [ -z "${THUB_SRC}" ]; then
6+
echo '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: Google Storage 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+
else
43+
echo 'Build is NOT required.'
44+
fi

google_storage/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 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## Google Storage bucket name
11+
THUB_GS_PATH=${2-${THUB_GS_PATH}}
12+
if [ -z "${THUB_GS_PATH}" ]; then
13+
echo 'ERROR: THUB_GS_PATH variable is empty. Aborting...'
14+
exit 1
15+
fi
16+
17+
## Clean environmental variables
18+
> .terrahub_build.env
19+
20+
## Checking if THUB_GS_PATH file exists in Google Storage
21+
gsutil --version > /dev/null 2>&1 || { echo >&2 'gsutil is missing. Aborting...'; exit 1; }
22+
THUB_CHECK_TYPE=$(gsutil ls ${THUB_GS_PATH}|| echo "")
23+
if [ -z "${THUB_CHECK_TYPE}" ]; then
24+
echo "INFO: ${THUB_GS_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 Google Storage
30+
echo 'INFO: Downloading THUB_SRC from THUB_GS_PATH'
31+
if [[ $THUB_CHECK_TYPE = *" PRE "* ]]; then
32+
gsutil rsync -d -r ${THUB_GS_PATH} ${THUB_SRC}
33+
else
34+
gsutil cp ${THUB_GS_PATH} ${THUB_SRC}
35+
fi

google_storage/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 '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 "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_storage/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 'ERROR: THUB_SRC variable is empty. Aborting...'
7+
exit 1
8+
fi
9+
10+
## Google Storage bucket name
11+
THUB_GS_PATH=${2-${THUB_GS_PATH}}
12+
if [ -z "${THUB_GS_PATH}" ]; then
13+
echo 'ERROR: THUB_GS_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_GS_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 -d -r ${THUB_SRC} ${THUB_GS_PATH}
26+
elif [[ -f "${THUB_SRC}" ]]; then
27+
gsutil cp ${THUB_SRC} ${THUB_GS_PATH}
28+
else
29+
echo "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

iam_object_viewer/.terrahub.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
component:
2+
name: iam_object_viewer
3+
mapping:
4+
- .
5+
dependsOn:
6+
- ../static_website
7+
template:
8+
resource:
9+
google_storage_bucket_iam_member:
10+
iam_object_viewer:
11+
role: roles/storage.objectViewer
12+
bucket: terrahub_123456_website
13+
member: allUsers
14+
output:
15+
etag:
16+
value: '${google_storage_bucket_iam_member.iam_object_viewer.etag}'
17+
terraform:
18+
backend:
19+
local:
20+
path: /tmp/.terrahub/local_backend/iam_object_viewer/terraform.tfstate
21+
data:
22+
terraform_remote_state:
23+
storage:
24+
backend: local
25+
config:
26+
path: /tmp/.terrahub/local_backend/static_website/terraform.tfstate

static_website/.terrahub.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
component:
2+
name: static_website
3+
mapping:
4+
- .
5+
template:
6+
resource:
7+
google_storage_bucket:
8+
static_website:
9+
name: terrahub_123456_website
10+
location: US
11+
force_destroy: true
12+
project: '${local.google_project_id}'
13+
website:
14+
main_page_suffix: index.html
15+
not_found_page: /404.html
16+
output:
17+
id:
18+
value: '${google_storage_bucket.static_website.id}'
19+
thub_id:
20+
value: '${google_storage_bucket.static_website.id}'
21+
self_link:
22+
value: '${google_storage_bucket.static_website.self_link}'
23+
project:
24+
value: '${google_storage_bucket.static_website.project}'
25+
url:
26+
value: '${google_storage_bucket.static_website.url}'
27+
terraform:
28+
backend:
29+
local:
30+
path: /tmp/.terrahub/local_backend/static_website/terraform.tfstate
31+
build:
32+
env:
33+
variables:
34+
THUB_ENV: dev
35+
THUB_INDEX_FILE: www.txt
36+
THUB_GS_PATH: 'gs://terrahub_123456_website'
37+
THUB_ROBOTS: ../../robots.dev.txt
38+
THUB_BUILD_PATH: ../../build
39+
THUB_SOURCE_PATH: ../../assets ../../static/fonts ../../static/img ../../views
40+
THUB_BUILD_OK: false
41+
THUB_MAX_AGE: 600
42+
phases:
43+
pre_build:
44+
commands:
45+
- 'echo "BUILD: Running pre_build step"'
46+
- ./scripts/download.sh $THUB_INDEX_FILE $THUB_GS_PATH/$THUB_INDEX_FILE
47+
- ./scripts/compare.sh $THUB_INDEX_FILE $THUB_SOURCE_PATH
48+
finally:
49+
- 'echo "BUILD: pre_build step successful"'
50+
build:
51+
commands:
52+
- 'echo "BUILD: Running build step"'
53+
- ../../bin/compile.sh
54+
finally:
55+
- 'echo "BUILD: build step successful"'
56+
post_build:
57+
commands:
58+
- 'echo "BUILD: Running post_build step"'
59+
- ./scripts/shasum.sh $THUB_BUILD_PATH/$THUB_INDEX_FILE
60+
- >-
61+
./scripts/upload.sh $THUB_BUILD_PATH $THUB_GS_PATH --cache-control
62+
max-age=$THUB_MAX_AGE
63+
- rm -f .terrahub_build.env $THUB_INDEX_FILE
64+
finally:
65+
- 'echo "BUILD: post_build step successful"'

0 commit comments

Comments
 (0)