Skip to content

Commit e56e6cd

Browse files
authored
fix (samples) user environment setup script. (#534)
1 parent 24a1278 commit e56e6cd

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

retail/interactive-tutorials/user_environment_setup.sh

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,83 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# set the Google Cloud Project ID
18-
{
19-
project_id=$1
20-
echo "Project ID: $project_id"
21-
gcloud config set project "$project_id"
22-
23-
} && {
24-
25-
timestamp=$(date +%s)
26-
27-
service_account_id="service-acc-$timestamp"
28-
echo "Service Account: $service_account_id"
17+
failure() {
18+
echo "========================================="
19+
echo "The Google Cloud setup was not completed."
20+
echo "Please fix the errors above!"
21+
echo "========================================="
22+
exit 0
23+
}
2924

30-
# create service account (your service-acc-$timestamp)
31-
gcloud iam service-accounts create "$service_account_id"
25+
# catch any error that happened during execution
26+
trap 'failure' ERR
3227

33-
} && {
28+
# set the Google Cloud Project ID
3429

35-
# assign necessary roles to your new service account
36-
for role in {retail.admin,editor,bigquery.admin}
30+
project_id=$1
31+
echo "Project ID: $project_id"
32+
gcloud config set project "$project_id"
33+
34+
email=$(gcloud auth list --filter="status:ACTIVE account:$project_id.iam.gserviceaccount.com" --format="value(account)")
35+
echo $email
36+
37+
# Check if user has service account active
38+
if [ -z "$email" ]
39+
then
40+
# Create a new service account
41+
timestamp=$(date +%s)
42+
43+
service_account_id="service-acc-$timestamp"
44+
echo "Service Account: $service_account_id"
45+
46+
# create service account (your service-acc-$timestamp)
47+
gcloud iam service-accounts create "$service_account_id"
48+
else
49+
service_account_id="${email%@*}"
50+
# Log out of service account
51+
gcloud auth revoke
52+
fi
53+
echo "$service_account_id"
54+
55+
editor=$(gcloud projects get-iam-policy $project_id \
56+
--flatten="bindings[].members" \
57+
--format='table(bindings.role)' \
58+
--filter="bindings.members:$service_account_id ROLE=roles/editor")
59+
60+
retail_admin=$(gcloud projects get-iam-policy $project_id \
61+
--flatten="bindings[].members" \
62+
--format='table(bindings.role)' \
63+
--filter="bindings.members:$service_account_id ROLE=roles/retail.admin")
64+
65+
66+
# assign necessary roles to your new service account
67+
# Check if any of the needed roles is missing
68+
if [ -z "$editor" ] || [ -z "$retail_admin" ]
69+
then
70+
# Assign necessary roles to your new service account.
71+
for role in {retail.admin,editor}
3772
do
38-
gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}"
39-
done
73+
gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}"
74+
done
75+
echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account"
76+
sleep 60
77+
fi
4078

41-
} && {
79+
# upload your service account key file
80+
service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com"
81+
gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email"
4282

43-
echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account"
44-
sleep 60
83+
# activate the service account using the key
84+
gcloud auth activate-service-account --key-file ~/key.json
4585

46-
# upload your service account key file
47-
service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com"
48-
gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email"
4986

50-
# activate the service account using the key
51-
gcloud auth activate-service-account --key-file ~/key.json
87+
# install needed Google client libraries
88+
cd ~/cloudshell_open/java-retail/samples/interactive-tutorials || exit
89+
mvn clean install -DskipTests
5290

53-
} && {
5491

55-
# install needed Google client libraries
56-
cd ~/cloudshell_open/java-retail/samples/interactive-tutorials || exit
57-
mvn clean install -DskipTests
58-
59-
} && {
60-
61-
# Print success message
62-
echo "========================================"
63-
echo "The Google Cloud setup is completed."
64-
echo "Please proceed with the Tutorial steps"
65-
echo "========================================"
66-
67-
} || {
68-
69-
# Print error message
70-
echo "========================================"
71-
echo "The Google Cloud setup was not completed."
72-
echo "Please fix the errors above!"
73-
echo "========================================"
74-
}
92+
# Print success message
93+
echo "========================================"
94+
echo "The Google Cloud setup is completed."
95+
echo "Please proceed with the Tutorial steps"
96+
echo "========================================"

0 commit comments

Comments
 (0)