Skip to content

Commit 82ecff4

Browse files
samples: properly catch errors (#197)
* Fixed issues in some cases when user setting up environment. * fix: use bash trap Co-authored-by: Alexander Fenster <[email protected]>
1 parent 753cc1f commit 82ecff4

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

retail/interactive-tutorials/user_environment_setup.sh

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
32
# Copyright 2022 Google Inc. All Rights Reserved.
43
#
54
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,28 +13,44 @@
1413
# See the License for the specific language governing permissions and
1514
# limitations under the License.
1615

16+
success() {
17+
echo "========================================="
18+
echo "The Google Cloud setup is completed."
19+
echo "Please proceed with the Tutorial steps"
20+
echo "========================================="
21+
exit 0
22+
}
23+
24+
failure() {
25+
echo "========================================="
26+
echo "The Google Cloud setup was not completed."
27+
echo "Please fix the errors above!"
28+
echo "========================================="
29+
exit 1
30+
}
31+
32+
# catch any error that happened during execution
33+
trap 'failure' ERR
34+
1735
# set the Google Cloud Project ID
1836
project_id=$1
1937
echo "Project ID: $project_id"
2038
gcloud config set project "$project_id"
2139

2240
timestamp=$(date +%s)
23-
2441
service_account_id="service-acc-$timestamp"
2542
echo "Service Account: $service_account_id"
26-
2743
# create service account (your service-acc-$timestamp)
2844
gcloud iam service-accounts create "$service_account_id"
2945

3046
# assign necessary roles to your new service account
3147
for role in {retail.admin,editor,bigquery.admin}
32-
do
48+
do
3349
gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}"
3450
done
3551

3652
echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account"
3753
sleep 60
38-
3954
# upload your service account key file
4055
service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com"
4156
gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email"
@@ -44,10 +59,5 @@ gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_e
4459
gcloud auth activate-service-account --key-file ~/key.json
4560

4661
# install needed Google client libraries
47-
cd ~/cloudshell_open/nodejs-retail/samples || exit
62+
cd ~/cloudshell_open/nodejs-retail/samples
4863
npm install
49-
50-
echo "======================================="
51-
echo "The Google Cloud setup is completed."
52-
echo "Please proceed with the Tutorial steps"
53-
echo "======================================="

0 commit comments

Comments
 (0)