Skip to content

Commit ac16b00

Browse files
authored
Merge pull request #19 from TerraHubCorp/dev
Another set of fixes and improvements
2 parents 358a3e5 + 62dbcd7 commit ac16b00

File tree

19 files changed

+492
-92
lines changed

19 files changed

+492
-92
lines changed

README.md

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ corresponding terraform configurations:
66

77
| GCP Resource | Terraform Resource | Link to TerraHub Config |
88
|-----------------------|--------------------|-------------------------|
9-
| Cloud Function | google_cloudfunctions_function | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_function/.terrahub.yml#L24 |
10-
| Cloud Storage | google_storage_bucket | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_storage/.terrahub.yml#L7 |
11-
| Static Website | google_storage_bucket_object | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_function/.terrahub.yml#L19 |
12-
| IAM Policy | iam_member_object_viewer | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_storage/.terrahub.yml#L7 |
9+
| Cloud Function | google_cloudfunctions_function | [google_function/.terrahub.yml#L24](https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_function/.terrahub.yml#L24) |
10+
| Cloud Storage | google_storage_bucket | [google_storage/.terrahub.yml#L7](https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_storage/.terrahub.yml#L7) |
11+
| Static Website | google_storage_bucket_object | [static_website/.terrahub.yml#L8](https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/static_website/.terrahub.yml#L8) |
12+
| IAM Policy | google_storage_bucket_iam_member | [iam_object_viewer/.terrahub.yml#L9](https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/iam_object_viewer/.terrahub.yml#L9) |
1313

1414
Follow below instructions to try this out in your own Google Cloud account.
1515

@@ -193,7 +193,7 @@ Your output should be similar to the one below:
193193
The source code of Google Function is stored in `index.js`:
194194

195195
```shell
196-
cat 'exports.helloGET = (req, res) => { res.send("Hello World!\n"); };' > index.js
196+
echo 'exports.helloGET = (req, res) => { res.send("Hello World!\n"); };' > index.js
197197
```
198198

199199
The source code of Static Website is cloned from another public repository:
@@ -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 google_static_website -d ./www/.terrahub \
216-
&& terrahub component -t google_storage_bucket_iam_member -n iam_member_object_viewer -d ./www/.terrahub -o ../google_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:
@@ -295,34 +295,34 @@ Your output should be similar to the one below:
295295

296296
Run the following commands in terminal:
297297
```shell
298-
terrahub configure -i google_static_website -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_static_website/terraform.tfstate'
299-
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.name="${STORAGE_BUCKET}_website"
300-
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.location='US'
301-
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.force_destroy='true'
302-
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.project='${local.google_project_id}'
303-
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.website.main_page_suffix='index.html'
304-
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.website.not_found_page='/404.html'
305-
terrahub configure -i google_static_website -c component.template.variable -D -y
306-
terrahub configure -i google_static_website -c build.env.variables.THUB_ENV='dev'
307-
terrahub configure -i google_static_website -c build.env.variables.THUB_INDEX_FILE='www.txt'
308-
terrahub configure -i google_static_website -c build.env.variables.THUB_S3_PATH="gs://${STORAGE_BUCKET}_website"
309-
terrahub configure -i google_static_website -c build.env.variables.THUB_ROBOTS='../../robots.dev.txt'
310-
terrahub configure -i google_static_website -c build.env.variables.THUB_BUILD_PATH='../../build'
311-
terrahub configure -i google_static_website -c build.env.variables.THUB_SOURCE_PATH='../../assets ../../static/fonts ../../static/img ../../views'
312-
terrahub configure -i google_static_website -c build.env.variables.THUB_BUILD_OK='false'
313-
terrahub configure -i google_static_website -c build.env.variables.THUB_MAX_AGE='600'
314-
terrahub configure -i google_static_website -c build.phases.pre_build.commands[0]='echo "BUILD: Running pre_build step"'
315-
terrahub configure -i google_static_website -c build.phases.pre_build.commands[1]='./scripts/download.sh $THUB_INDEX_FILE $THUB_S3_PATH/$THUB_INDEX_FILE'
316-
terrahub configure -i google_static_website -c build.phases.pre_build.commands[2]='./scripts/compare.sh $THUB_INDEX_FILE $THUB_SOURCE_PATH'
317-
terrahub configure -i google_static_website -c build.phases.pre_build.finally[0]='echo "BUILD: pre_build step successful"'
318-
terrahub configure -i google_static_website -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
319-
terrahub configure -i google_static_website -c build.phases.build.commands[1]='../../bin/compile.sh'
320-
terrahub configure -i google_static_website -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
321-
terrahub configure -i google_static_website -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
322-
terrahub configure -i google_static_website -c build.phases.post_build.commands[1]='./scripts/shasum.sh $THUB_BUILD_PATH/$THUB_INDEX_FILE'
323-
terrahub configure -i google_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'
324-
terrahub configure -i google_static_website -c build.phases.post_build.commands[3]='rm -f .terrahub_build.env $THUB_INDEX_FILE'
325-
terrahub configure -i google_static_website -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
298+
terrahub configure -i static_website -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/static_website/terraform.tfstate'
299+
terrahub configure -i static_website -c component.template.resource.google_storage_bucket.static_website.name="${STORAGE_BUCKET}_website"
300+
terrahub configure -i static_website -c component.template.resource.google_storage_bucket.static_website.location='US'
301+
terrahub configure -i static_website -c component.template.resource.google_storage_bucket.static_website.force_destroy='true'
302+
terrahub configure -i static_website -c component.template.resource.google_storage_bucket.static_website.project='${local.google_project_id}'
303+
terrahub configure -i static_website -c component.template.resource.google_storage_bucket.static_website.website.main_page_suffix='index.html'
304+
terrahub configure -i static_website -c component.template.resource.google_storage_bucket.static_website.website.not_found_page='/404.html'
305+
terrahub configure -i static_website -c component.template.variable -D -y
306+
terrahub configure -i static_website -c build.env.variables.THUB_ENV='dev'
307+
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_GS_PATH="gs://${STORAGE_BUCKET}_website"
309+
terrahub configure -i static_website -c build.env.variables.THUB_ROBOTS='../www/robots.dev.txt'
310+
terrahub configure -i static_website -c build.env.variables.THUB_BUILD_PATH='../www/build'
311+
terrahub configure -i static_website -c build.env.variables.THUB_SOURCE_PATH='../www/assets ../www/static/fonts ../www/static/img ../www/views'
312+
terrahub configure -i static_website -c build.env.variables.THUB_BUILD_OK='false'
313+
terrahub configure -i static_website -c build.env.variables.THUB_MAX_AGE='600'
314+
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_GS_PATH/$THUB_INDEX_FILE'
316+
terrahub configure -i static_website -c build.phases.pre_build.commands[2]='./scripts/compare.sh $THUB_INDEX_FILE $THUB_SOURCE_PATH'
317+
terrahub configure -i static_website -c build.phases.pre_build.finally[0]='echo "BUILD: pre_build step successful"'
318+
terrahub configure -i static_website -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
319+
terrahub configure -i static_website -c build.phases.build.commands[1]='../www/bin/compile.sh'
320+
terrahub configure -i static_website -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
321+
terrahub configure -i static_website -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
322+
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_GS_PATH --cache-control max-age=$THUB_MAX_AGE'
324+
terrahub configure -i static_website -c build.phases.post_build.commands[3]='rm -f .terrahub_build.env $THUB_INDEX_FILE'
325+
terrahub configure -i static_website -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
326326
```
327327

328328
Your output should be similar to the one below:
@@ -334,13 +334,13 @@ Your output should be similar to the one below:
334334

335335
Run the following commands in terminal:
336336
```shell
337-
terrahub configure -i iam_member_object_viewer -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/iam_member_object_viewer/terraform.tfstate'
338-
terrahub configure -i iam_member_object_viewer -c component.template.data.terraform_remote_state.storage.backend='local'
339-
terrahub configure -i iam_member_object_viewer -c component.template.data.terraform_remote_state.storage.config.path='/tmp/.terrahub/local_backend/google_static_website/terraform.tfstate'
340-
terrahub configure -i iam_member_object_viewer -c component.template.resource.google_storage_bucket_iam_member.iam_member_object_viewer.bucket="${STORAGE_BUCKET}_website"
341-
terrahub configure -i iam_member_object_viewer -c component.template.resource.google_storage_bucket_iam_member.iam_member_object_viewer.role="roles/storage.objectViewer"
342-
terrahub configure -i iam_member_object_viewer -c component.template.resource.google_storage_bucket_iam_member.iam_member_object_viewer.member="allUsers"
343-
terrahub configure -i iam_member_object_viewer -c component.template.variable -D -y
337+
terrahub configure -i iam_object_viewer -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/iam_object_viewer/terraform.tfstate'
338+
terrahub configure -i iam_object_viewer -c component.template.data.terraform_remote_state.storage.backend='local'
339+
terrahub configure -i iam_object_viewer -c component.template.data.terraform_remote_state.storage.config.path='/tmp/.terrahub/local_backend/static_website/terraform.tfstate'
340+
terrahub configure -i iam_object_viewer -c component.template.resource.google_storage_bucket_iam_member.iam_object_viewer.bucket="${STORAGE_BUCKET}_website"
341+
terrahub configure -i iam_object_viewer -c component.template.resource.google_storage_bucket_iam_member.iam_object_viewer.role="roles/storage.objectViewer"
342+
terrahub configure -i iam_object_viewer -c component.template.resource.google_storage_bucket_iam_member.iam_object_viewer.member="allUsers"
343+
terrahub configure -i iam_object_viewer -c component.template.variable -D -y
344344
```
345345

346346
Your output should be similar to the one below:
@@ -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-
└─ google_static_website [path: ./www/.terrahub/google_static_website]
364-
└─ iam_member_object_viewer [path: ./www/.terrahub/iam_member_object_viewer]
363+
└─ static_website [path: ./static_website]
364+
└─ iam_object_viewer [path: ./iam_object_viewer]
365365
```
366366

367367
## Run TerraHub Automation
@@ -370,7 +370,7 @@ Project: demo-terraform-automation-gcp
370370

371371
Run the following command in terminal:
372372
```shell
373-
terrahub run -y -a -i google_storage,google_static_website
373+
terrahub run -y -a -i google_storage,static_website
374374
```
375375

376376
Your output should be similar to the one below:
@@ -382,13 +382,17 @@ Your output should be similar to the one below:
382382
Run the following command in terminal:
383383

384384
```shell
385-
terrahub build -i google_function,google_static_website
385+
terrahub build -i google_function,static_website
386386
```
387387

388388
Your output should be similar to the one below:
389389
```
390390
```
391391

392+
> NOTE: If you get an error saying `ServiceException: 401 Anonymous caller
393+
does not have storage.objects.get access to [***]`, run `gsutil config` and
394+
setup default project id.
395+
392396
### Run TerraHub Automation
393397

394398
Run the following command in terminal:

bin/deploy.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
gcloud --version > /dev/null 2>&1 || { echo >&2 'gcloud is missing. aborting...'; exit 1; }
4+
npm --version > /dev/null 2>&1 || { echo >&2 "npm is missing. aborting..."; exit 1; }
5+
export NODE_PATH="$(npm root -g)"
6+
7+
if [ -z "${BRANCH_FROM}" ]; then BRANCH_FROM = "dev"; fi
8+
if [ -z "${BRANCH_TO}" ]; then BRANCH_TO = "dev"; fi
9+
if [ "${BRANCH_TO}" != "dev" ]; then THUB_ENV="-e ${BRANCH_TO}"; fi
10+
if [ "${THUB_STATE}" == "approved" ]; then THUB_APPLY="-a"; fi
11+
12+
git --version > /dev/null 2>&1 || { echo >&2 "git is missing. aborting..."; exit 1; }
13+
git checkout $BRANCH_TO
14+
git checkout $BRANCH_FROM
15+
16+
jq --version > /dev/null 2>&1 || { echo >&2 'jq is missing. aborting...'; exit 1; }
17+
GOOGLE_CLOUD_PROJECT="$(gcloud config list --format=json | jq '.core.project')"
18+
BILLING_ID="$(gcloud beta billing accounts list --format=json | jq '.[0].name[16:]')"
19+
GOOGLE_APPLICATION_CREDENTIALS="${HOME}/.config/gcloud/${GOOGLE_CLOUD_PROJECT}.json"
20+
SERVICE_ACCOUNT="terraform@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"
21+
gcloud iam service-accounts keys create ${GOOGLE_APPLICATION_CREDENTIALS} --iam-account=${SERVICE_ACCOUNT}
22+
23+
terrahub --version > /dev/null 2>&1 || { echo >&2 "terrahub is missing. aborting..."; exit 1; }
24+
terrahub configure -c template.locals.google_project_id="${GOOGLE_CLOUD_PROJECT}"
25+
terrahub configure -c template.locals.google_billing_account="${BILLING_ID}"
26+
27+
terrahub run -y -b ${THUB_APPLY} ${THUB_ENV}
28+
echo "Execution successful: from ${BRANCH_FROM} into ${BRANCH_TO}"

google_function/.terrahub.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,12 @@ 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
188
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
249
google_cloudfunctions_function:
2510
google_function:
11+
name: 'demofunction${local.project["code"]}'
2612
depends_on:
2713
- google_storage_bucket_object.google_storage_object
28-
name: 'demofunction${local.project["code"]}'
2914
region: us-central1
3015
runtime: nodejs8
3116
description: My demo function
@@ -35,6 +20,21 @@ component:
3520
trigger_http: true
3621
timeout: 60
3722
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}'

google_function/scripts/build.sh

100644100755
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/bin/bash
22

3-
## Component name
4-
COMPONENT_NAME=${1}
5-
if [ -z "${COMPONENT_NAME}" ]; then
6-
echo >&2 'ERROR: COMPONENT_NAME variable is empty. Aborting...'
3+
## TerraHub Component
4+
THUB_COMPONENT=${1}
5+
if [ -z "${THUB_COMPONENT}" ]; then
6+
echo >&2 'ERROR: THUB_COMPONENT variable is empty. Aborting...'
77
exit 1
88
fi
99

10-
## Object name
11-
OBJECT_NAME=${2}
12-
if [ -z "${OBJECT_NAME}" ]; then
13-
echo >&2 'ERROR: OBJECT_NAME variable is empty. Aborting...'
10+
## Google Storage object name
11+
THUB_OBJECT=${2}
12+
if [ -z "${THUB_OBJECT}" ]; then
13+
echo >&2 'ERROR: THUB_OBJECT variable is empty. Aborting...'
1414
exit 1
1515
fi
1616

17-
## Bucket key
17+
## Google Storage bucket key
1818
THUB_BUCKET_KEY=${3}
1919
if [ -z "${THUB_BUCKET_KEY}" ]; then
2020
echo >&2 'ERROR: THUB_BUCKET_KEY variable is empty. Aborting...'
@@ -26,8 +26,9 @@ fi
2626

2727
## Checking if THUB_BUILD_OK is true
2828
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
29+
THUB_OBJECT_KEY='component.template.resource.google_storage_bucket_object.'${THUB_OBJECT}'.name'
30+
terrahub --version > /dev/null 2>&1 || { echo >&2 'terrahub is missing. Aborting...'; exit 1; }
31+
terrahub configure -i ${THUB_COMPONENT} -c ${THUB_OBJECT_KEY}=${THUB_BUCKET_KEY}$(date +%s).zip
3132
echo 'Build was executed'
3233
else
3334
echo 'Build was NOT executed'

google_function/scripts/compare.sh

100644100755
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

100644100755
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/shasum.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)