This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config
executable file
·145 lines (97 loc) · 3.03 KB
/
config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
SA_ACCOUNT_NAME="habdash-sa"
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SA_KEY_FILE="$SCRIPT_DIR/work/sa_key.json"
SA_ACCOUNT="$SA_ACCOUNT_NAME@$1.iam.gserviceaccount.com"
#create work dir
mkdir -p $SCRIPT_DIR/work
#take command line input
if [ -z "$1" ]; then
echo "Project name wasn't passed. Exiting."
exit 1
fi
#set project
if ! gcloud projects list | grep -q $1 ; then
echo ""
echo "No such project exists"
exit 1
fi
#output current account
current_account=$(gcloud config list account --format "value(core.account)")
echo "You are logged in as $current_account"
#login into admin
if [[ "$current_account" == *"gserviceaccount.com"* ]]; then
echo "You are loged in as service account"
echo "User account is required"
echo "Please login as proper user:"
gcloud auth login
fi
#set project
echo ""
echo "Setting project to $1"
gcloud config set project $1
echo "Checking that required APIs are enabled"
#enable iam api
if ! gcloud services list | grep -q "iam.googleapis.com" ; then
echo "Enabling IAM API"
gcloud services enable iam.googleapis.com
fi
#enable cloud resource manager
if ! gcloud services list | grep -q "cloudresourcemanager.googleapis.com" ; then
echo "Enabling cloud resrouce manager"
gcloud services enable cloudresourcemanager.googleapis.com
fi
#remove service account if exists
if gcloud iam service-accounts describe $SA_ACCOUNT; then
echo ""
echo "There is already a service account with the name '$SA_ACCOUNT_NAME'"
echo "Removing"
gcloud iam service-accounts delete $SA_ACCOUNT
rm $SA_KEY_FILE
else
echo ""
fi
#gen new service account
echo "Creating new service account"
if gcloud iam service-accounts create $SA_ACCOUNT_NAME \
--display-name="$SA_ACCOUNT_NAME" \
--description="Main service account for the habdash project" ; then
echo "Done"
else
echo "Couldn't create a service account"
fi
echo ""
#gen key for the service account
echo "Generating auth key for the service account"
if gcloud iam service-accounts keys create $SA_KEY_FILE \
--iam-account=$SA_ACCOUNT ; then
echo "Service account key was saved at $SA_KEY_FILE"
else
echo "Failed to generate a keyfile"
fi
#grant required permissions
echo ""
echo "Granting permissions to the service account"
#Actions Admin
if ! gcloud projects add-iam-policy-binding $1 \
--member="serviceAccount:$SA_ACCOUNT" \
--role=roles/actions.Admin ; then
exit 1
fi
#Service Usage Admin
if ! gcloud projects add-iam-policy-binding $1 \
--member="serviceAccount:$SA_ACCOUNT" \
--role=roles/serviceusage.serviceUsageAdmin ; then
exit 1
fi
#Service Secret manager accessor
if ! gcloud projects add-iam-policy-binding $1 \
--member="serviceAccount:$SA_ACCOUNT" \
--role=roles/secretmanager.secretAccessor ; then
exit 1
fi
#Activating the service account
echo ""
echo "Activating the service account"
gcloud auth activate-service-account \
--key-file=$SA_KEY_FILE --project=$1