This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
194 lines (178 loc) · 5.55 KB
/
deploy.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
###########################################################################
#
# Copyright 2022 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
cat <<END
******** Welcome **********
*
* Google Tag Manager Database Setup
*
***************************
END
read -p "Please enter your Google Cloud Project ID: " project_id
echo "~~~~~~~~ Enabling APIs ~~~~~~~~~~"
gcloud services enable \
cloudbuild.googleapis.com \
cloudfunctions.googleapis.com \
bigquery.googleapis.com \
cloudscheduler.googleapis.com \
appengine.googleapis.com \
tagmanager.googleapis.com \
--async
gcloud app create
exit_setup () {
exit "Exiting Google Tag Manager Database setup. Setup failed."
}
create_service_account () {
gcloud iam service-accounts create $service_account_name \
--display-name=$service_account_name
}
set_service_account_email () {
if [[ service_account_email = "" || $1 < 3 ]]; then
echo "Service account email attempt $1"
sleep 2
retry_attempt=$(( $1 + 1 ))
set_service_account_email "$retry_attempt"
else
echo $service_account_email
fi
}
set_service_account_iam_policy () {
gcloud projects add-iam-policy-binding $project_id \
--member="serviceAccount:$service_account_email" \
--role="roles/editor"
}
service_account_setup () {
read -p "Please enter you desired service account name with no spaces.
This service account will be used by your Cloud Function.
The recommended name is 'gtm-database' : " service_account_name
echo "~~~~~~~~ Creating Service Account ~~~~~~~~~~"
if create_service_account; then
service_account_email="$service_account_name@$project_id.iam.gserviceaccount.com"
if set_service_account_iam_policy; then
echo "Service account created."
else
read -p "Service account creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
service_account_setup
fi
fi
else
read -p "Service account creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
create_service_account
fi
fi
}
service_account_setup
create_cloud_function () {
gcloud functions deploy $function_name \
--project=$project_id \
--runtime=node16 \
--service-account=$service_account_email \
--memory=1GB \
--timeout=4000s \
--trigger-http \
--entry-point=gtmDownloader \
--gen2
}
cloud_function_setup () {
read -p "Please enter your desired Function name. The recommended
function name is 'gtm_downloader': " function_name
cd functions/gtm-downloader
echo "~~~~~~~~ Creating Function ~~~~~~~~~~"
if create_cloud_function; then
cd ../..
echo "Function created."
else
cd ../..
read -p "Function creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
cloud_function_setup
fi
fi
}
cloud_function_setup
echo "~~~~~~~~ Creating BigQuery Dataset ~~~~~~~~~~"
bq mk -d $project_id:gtm_database
echo "~~~~~~~~ Creating BigQuery Tables ~~~~~~~~~~"
cd bigquery/schemas
bq mk -t --time_partitioning_type=DAY \
--schema=./gtm_accounts.json \
$project_id:gtm_database.gtm_account_summaries
bq mk -t --time_partitioning_type=DAY \
--schema=./gtm_containers.json \
$project_id:gtm_database.gtm_containers
bq mk -t --time_partitioning_type=DAY \
--schema=./gtm_tags.json \
$project_id:gtm_database.gtm_tags
bq mk -t --time_partitioning_type=DAY \
--schema=./gtm_variables.json \
$project_id:gtm_database.gtm_variables
bq mk -t --time_partitioning_type=DAY \
--schema=./gtm_built_in_variables.json \
$project_id:gtm_database.gtm_built_in_variables
bq mk -t --time_partitioning_type=DAY \
--schema=./gtm_triggers.json \
$project_id:gtm_database.gtm_triggers
cd ../..
echo "BigQuery tables created."
create_cloud_scheduler () {
gcloud scheduler jobs create http $scheduler_name \
--schedule "0 23 * * *" \
--uri="$function_uri" \
--http-method=GET \
--oidc-service-account-email=$service_account_email \
--oidc-token-audience=$function_uri \
--project=$project_id
}
cloud_scheduler_setup () {
read -p "Please enter your desired Cloud Scheduler name.
The recommended scheduler name is 'gtm_downloader': " scheduler_name
echo "A cloud scheduler will now be created that runs daily at 11 PM."
echo "~~~~~~~~ Creating Cloud Scheduler ~~~~~~~~~~"
function_uri=$(gcloud functions describe $function_name --format="value(httpsTrigger.url)")
echo $function_uri
if gcloud app browse; then
gcloud app create
fi
if create_cloud_scheduler; then
echo "Cloud scheduler created."
else
cd ..
read -p "Schedule job creation failed. Try again? y/n: " exit_response
if [ $exit_response = "n" ]; then
exit_setup
else
cloud_scheduler_setup
fi
fi
}
cloud_scheduler_setup
echo "***************************
*
* Google Tag Manager Database Setup Complete!
*
* You must now grant $service_account_email access to your Google Tag Manager
* Accounts. This will be the email Google Cloud uses to access your Google
* Tag Manager settings.
***************************"