This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathenterprise.sh
executable file
·456 lines (363 loc) · 16.3 KB
/
enterprise.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/bin/bash
###########################################################################
#
# Copyright 2020 Google LLC
#
# 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.
#
###########################################################################
setup_sql() {
echo ""
echo "----------------------------------------"
echo "Set Up Cloud SQL Database"
echo "----------------------------------------"
echo ""
echo " - https://cloud.google.com/endpoints/docs/openapi/enable-api"
echo " - https://cloud.google.com/sql/docs/mysql/create-instance"
echo " - https://cloud.google.com/sdk/gcloud/reference/sql/databases/create"
echo " - https://cloud.google.com/sdk/gcloud/reference/sql/users/create"
echo ""
gcloud services enable sqladmin.googleapis.com
gcloud services enable sql-component.googleapis.com
gcloud services enable serviceusage.googleapis.com
values=$(gcloud sql instances list --filter="name=$STARTHINKER_UI_PRODUCTION_DATABASE_NAME" --format="value(name)" --verbosity=none)
if [ -z "${values}" ]; then
gcloud sql instances create $STARTHINKER_UI_PRODUCTION_DATABASE_NAME --database-version=POSTGRES_9_6 --cpu=2 --memory=7680MiB --region=$STARTHINKER_REGION
else
echo "Instance already exists."
fi
values=$(gcloud sql databases list --instance=$STARTHINKER_UI_PRODUCTION_DATABASE_NAME --filter="name=$STARTHINKER_UI_PRODUCTION_DATABASE_NAME" --format="value(name)" --verbosity=none)
if [ -z "${values}" ]; then
gcloud sql databases create $STARTHINKER_UI_PRODUCTION_DATABASE_NAME --instance=$STARTHINKER_UI_PRODUCTION_DATABASE_NAME
else
echo "Database already exists."
fi
gcloud sql users create $STARTHINKER_UI_PRODUCTION_DATABASE_USER --host=% --instance=$STARTHINKER_UI_PRODUCTION_DATABASE_NAME --password=$STARTHINKER_UI_PRODUCTION_DATABASE_PASSWORD
echo "Done"
}
start_proxy() {
echo ""
echo "----------------------------------------"
echo "Start Cloud Proxy"
echo "----------------------------------------"
echo ""
echo " - ${STARTHINKER_ROOT}/starthinker_assets/cloud_sql_proxy -instances=$STARTHINKER_PROJECT:$STARTHINKER_REGION:$STARTHINKER_UI_PRODUCTION_DATABASE_NAME=tcp:5432"
echo " - https://cloud.google.com/sql/docs/mysql/connect-admin-proxy"
echo ""
"${STARTHINKER_ROOT}/starthinker_assets/cloud_sql_proxy" -instances="$STARTHINKER_PROJECT:$STARTHINKER_REGION:$STARTHINKER_UI_PRODUCTION_DATABASE_NAME"=tcp:5432 &
sleep 15
echo "Done"
}
stop_proxy() {
echo ""
echo "----------------------------------------"
echo "Stop Cloud Proxy - ${STARTHINKER_ROOT}/starthinker_assets/cloud_sql_proxy"
echo "----------------------------------------"
echo ""
kill -9 $(jobs -p "${STARTHINKER_ROOT}/starthinker_assets/cloud_sql_proxy")
echo "Done"
}
migrate_database_proxy() {
echo ""
echo "----------------------------------------"
echo "Migrate Database"
echo "----------------------------------------"
echo ""
echo " - https://docs.djangoproject.com/en/4.0/topics/migrations/"
echo ""
start_proxy;
export STARTHINKER_UI_DOMAIN="${STARTHINKER_UI_PRODUCTION_DOMAIN}";
export STARTHINKER_UI_SECRET="${STARTHINKER_UI_PRODUCTION_SECRET}";
export STARTHINKER_UI_DATABASE_ENGINE="${STARTHINKER_UI_PRODUCTION_DATABASE_ENGINE}";
export STARTHINKER_UI_DATABASE_HOST="${STARTHINKER_UI_PRODUCTION_DATABASE_HOST}";
export STARTHINKER_UI_DATABASE_PORT="${STARTHINKER_UI_PRODUCTION_DATABASE_PORT}";
export STARTHINKER_UI_DATABASE_NAME="${STARTHINKER_UI_PRODUCTION_DATABASE_NAME}";
export STARTHINKER_UI_DATABASE_USER="${STARTHINKER_UI_PRODUCTION_DATABASE_USER}";
export STARTHINKER_UI_DATABASE_PASSWORD="${STARTHINKER_UI_PRODUCTION_DATABASE_PASSWORD}";
source "${STARTHINKER_ROOT}/starthinker_virtualenv/bin/activate"
python3 "${STARTHINKER_ROOT}/starthinker_ui/manage.py" makemigrations;
python3 "${STARTHINKER_ROOT}/starthinker_ui/manage.py" migrate;
deactivate
stop_proxy;
echo "Done"
}
deploy_appengine() {
echo ""
echo "----------------------------------------"
echo "Deploy AppEngine"
echo "----------------------------------------"
echo ""
echo " - https://cloud.google.com/endpoints/docs/openapi/enable-api"
echo " - https://cloud.google.com/build/docs/securing-builds/configure-access-for-cloud-build-service-account"
echo " - https://cloud.google.com/sdk/gcloud/reference/app/create"
echo " - https://cloud.google.com/sdk/gcloud/reference/app/deploy"
echo ""
gcloud services enable cloudbuild.googleapis.com
gcloud services enable appengine.googleapis.com
sleep 10
gcloud projects add-iam-policy-binding "${STARTHINKER_PROJECT}" \
--member=serviceAccount:$(get_cloudbuild_email) \
--role='roles/appengine.appAdmin'
# create recipe scripts python file for App Engine ( buffers scripts avoiding complex disk lookup )
source "${STARTHINKER_ROOT}/starthinker_assets/production.sh";
python3 "${STARTHINKER_ROOT}/starthinker_ui/recipe/scripts.py";
values=$(gcloud app describe --format="value(name)" --verbosity=none)
if [ -z "${values}" ]; then
gcloud app create --region "${STARTHINKER_REGION}"
fi
gcloud app deploy $STARTHINKER_ROOT/app.yaml --stop-previous-version --service-account=$(get_service_email)
gcloud app deploy $STARTHINKER_ROOT/cron.yaml --stop-previous-version --service-account=$(get_service_email)
# delete the recipe scripts python file for App Engine ( easy to forget and waste time debugging )
rm "${STARTHINKER_ROOT}/starthinker_ui/recipe/scripts_lookup.py"*
deactivate
echo "Done"
}
configure_yaml() {
deploy_Type=$1
echo ""
echo "----------------------------------------"
echo "Configure StarThinker App"
echo "----------------------------------------"
echo ""
echo " - $STARTHINKER_CONFIG"
echo " - https://cloud.google.com/appengine/docs/standard/python/config/appref"
echo " - https://cloud.google.com/appengine/docs/standard/python3/runtime#python-3.7"
echo ""
appengine_client=$(cat "$STARTHINKER_CLIENT_WEB" | tr '\n' ' ')
if [ -z "${STARTHINKER_UI_PRODUCTION_DOMAIN}" ]; then
appengine_domain=$(gcloud app browse --no-launch-browser)
else
appengine_domain=$STARTHINKER_UI_PRODUCTION_DOMAIN;
fi
appengine_database_engine="${STARTHINKER_UI_PRODUCTION_DATABASE_ENGINE}"
appengine_database_host="/cloudsql/$STARTHINKER_PROJECT:$STARTHINKER_REGION:$STARTHINKER_UI_PRODUCTION_DATABASE_NAME"
appengine_database_port="${STARTHINKER_UI_PRODUCTION_DATABASE_PORT}"
bash -c "cat > $STARTHINKER_ROOT/app.yaml" << EOL
runtime: python37
env: standard
entrypoint: gunicorn -b :\$PORT starthinker_ui.ui.wsgi
instance_class: F4_HIGHMEM
runtime_config:
python_version: 37
beta_settings:
cloud_sql_instances: $STARTHINKER_PROJECT:$STARTHINKER_REGION:$STARTHINKER_UI_PRODUCTION_DATABASE_NAME
env_variables:
STARTHINKER_SCALE: '1'
STARTHINKER_DEVELOPMENT: '$STARTHINKER_DEVELOPMENT'
STARTHINKER_WORKER_MAX: '$STARTHINKER_WORKER_MAX'
STARTHINKER_WORKER_JOBS: '$STARTHINKER_WORKER_JOBS'
STARTHINKER_ANALYTICS: '$STARTHINKER_ANALYTICS'
STARTHINKER_PROJECT: '$STARTHINKER_PROJECT'
STARTHINKER_ZONE: '$STARTHINKER_ZONE'
STARTHINKER_CLIENT: '$appengine_client'
STARTHINKER_SERVICE: 'DEFAULT'
STARTHINKER_UI_DOMAIN: '$appengine_domain'
STARTHINKER_UI_SECRET: '$STARTHINKER_UI_PRODUCTION_SECRET'
STARTHINKER_UI_DATABASE_ENGINE: '$appengine_database_engine'
STARTHINKER_UI_DATABASE_HOST: '$appengine_database_host'
STARTHINKER_UI_DATABASE_PORT: '$appengine_database_port'
STARTHINKER_UI_DATABASE_NAME: '$STARTHINKER_UI_PRODUCTION_DATABASE_NAME'
STARTHINKER_UI_DATABASE_USER: '$STARTHINKER_UI_PRODUCTION_DATABASE_USER'
STARTHINKER_UI_DATABASE_PASSWORD: '$STARTHINKER_UI_PRODUCTION_DATABASE_PASSWORD'
handlers:
- url: /static
static_dir: starthinker_ui/static
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
EOL
echo "Done"
}
setup_appengine() {
echo ""
echo "----------------------------------------"
echo "Setup Enterprise UI Instance On App Engine"
echo "----------------------------------------"
echo ""
echo "This will create a StarThinker UI instance in Google Cloud project $STARTHINKER_PROJECT."
echo ""
setup_credentials_service;
setup_database;
save_config;
gcloud services enable doubleclickbidmanager.googleapis.com
gcloud services enable storage-api.googleapis.com
gcloud services enable bigquery-json.googleapis.com
gcloud services enable dfareporting.googleapis.com
gcloud services enable drive.googleapis.com
gcloud services enable sheets.googleapis.com
gcloud services enable doubleclicksearch.googleapis.com
gcloud services enable secretmanager.googleapis.com
install_proxy; # first so it install dependencies
install_virtualenv; # second because pip is here
install_requirements; # second because pip is here
setup_sql;
migrate_database_proxy;
setup_credentials_ui;
save_config;
generate_translations;
configure_yaml $deploy_Type;
deploy_appengine;
echo ""
echo "----------------------------------------"
echo ""
echo "Access Your StarThinker UI At: $(gcloud app browse --no-launch-browser)"
echo ""
if [[ !$STARTHINKER_GSUITE ]]; then
echo ""
echo "----------------------------------------"
echo "The application may show a security warning: https://github.com/google/starthinker/raw/master/tutorials/images/verification.png"
echo "The warning is only showed when logging in."
echo "Bypass it by clicking Advnaced and Go To StarThinker."
echo ""
echo "To remove the above warning:"
echo " A. Verify your domian and switch to internal application."
echo " B. Enroll in gSuite and switch to internal application."
echo " C. Submit your application for verification to Google ( 6+ weeks )."
echo ""
fi
echo ""
echo "----------------------------------------"
echo "HIGHY SUGGESTED IAP SECURITY: https://console.cloud.google.com/security/iap?project=$STARTHINKER_PROJECT"
echo "This allows you to conrol access to the UI on a per user basis."
echo " 1. Enable IAP."
echo " 2. Add members with access to the role: Cloud IAP > IAP-secured Web App User"
echo " 3. Or use a firewall: https://cloud.google.com/appengine/docs/standard/python/application-security"
echo ""
echo ""
echo "Done"
}
migrate_database_enterprise() {
echo ""
echo "----------------------------------------"
echo "Migrate Database To Latest Version"
echo "----------------------------------------"
echo ""
setup_credentials_service;
setup_database;
save_config;
install_proxy; # first so it install dependencies
install_virtualenv; # second because pip is here
install_requirements; # second because pip is here
setup_sql;
migrate_database_proxy;
echo "Done"
echo ""
}
setup_cloud_function() {
gcloud services enable cloudbuild.googleapis.com
gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudscheduler.googleapis.com
gcloud services enable appengine.googleapis.com
echo ""
echo "------------------------------------------------------------------------------"
echo ""
echo "Cloud Function Setup"
echo ""
echo "------------------------------------------------------------------------------"
echo ""
echo " - A sample cloud scheduler job is deploying: https://console.cloud.google.com/cloudscheduler"
echo " - It will run a simple hello world task: https://github.com/google/starthinker/tree/master/starthinker/task/hello"
echo " - Invoke the hello world task using the parameters file: https://github.com/google/starthinker/blob/master/scripts/hello.json"
echo " - Extend the sample with scripts from: https://github.com/google/starthinker/tree/master/scripts"
echo ' - Convert a script to a recipe, replaces {"field":{...}} with values: https://github.com/google/starthinker/blob/master/tutorials/deploy_commandline.md#run-a-script'
echo " - If you need service cerdentials see: https://github.com/google/starthinker/blob/master/tutorials/cloud_service.md"
echo " - If you need user cerdentials see: https://github.com/google/starthinker/blob/master/tutorials/deploy_commandline.md#optional-setup-user-credentials"
echo " - A single cloud function can run all StarThinker recipes, simply pass a different JSON in the request."
echo ""
echo "Setting up cloud function..."
echo ""
service=$(get_service_email);
gcloud projects add-iam-policy-binding "${STARTHINKER_PROJECT}" \
--member=serviceAccount:$(get_service_email) \
--role=roles/cloudfunctions.invoker
# BUFFER_SCALE=1 minimizes memory ( should it be lower still )?
gcloud alpha functions deploy starthinker --entry-point run --runtime python39 \
--trigger-http \
--security-level secure-always \
--service-account "${service}" \
--memory 4096MB \
--source "$STARTHINKER_ROOT/cloud_function" \
--set-env-vars "BUFFER_SCALE=1, UI_SERVICE=DEFAULT" \
--timeout 540s --quiet
echo ""
echo "Deploying sample scheduled job..."
echo ""
# 0 3 * * * = 3 AM Daily (can be adjusted by user from UI)
trigger=$(gcloud functions describe starthinker --format 'value(httpsTrigger.url)')
gcloud scheduler jobs create http starthinker_sample --schedule "0 3 * * *" --uri "${trigger}" --http-method POST --message-body='{ "setup":{ "id":"${STARTHINKER_PROJECT}" }, "tasks":[ { "hello":{ "auth":"user", "say":"Hello World" }} ] }' --oidc-token-audience "${trigger}" --oidc-service-account-email "${service}" --time-zone "Etc/UTC" --location=$STARTHINKER_REGION
echo ""
echo "View the function at: https://pantheon.corp.google.com/functions/list"
echo "Run the scheduled task at: https://console.cloud.google.com/cloudscheduler"
echo "The function trigger URL is: ${trigger}"
}
setup_enterprise() {
echo ""
echo "------------------------------------------------------------------------------"
echo ""
echo "Enterprise Setup"
echo ""
echo "------------------------------------------------------------------------------"
echo ""
echo "StartThinker provides a Django powered browser UI for multi user enterprise teams."
echo "The UI will also require a deployment of worker machines to run jobs remotely."
echo ""
echo "- All deployments are re-entrant, you can run them multiple times."
echo "- Set up the UI first, you will probably leave the domain blank."
echo "- Workers deployments will create cloud instances. You can delete them and run the script again."
echo "- A Cloud SQL Postgres Database will be created for the UI and workers."
echo "- You need to manually delete and/or secure worker instances and database."
echo ""
echo "To edit deployment parameters use this script or from the command line edit:"
echo "- ${STARTHINKER_CONFIG}"
echo ""
enterprise_done=0
enterprise_options=("Deploy UI & Workers" "Deploy UI" "Deploy Workers" "Change Domain" "Change Database" "Migrate Database" "Start Datbase Proxy")
while (( !enterprise_done ))
do
echo "----------------------------------------------------------------------"
echo "Enterprise Menu"
echo "----------------------------------------------------------------------"
echo ""
PS3='Your Choice ( q = Quit ): '
select enterprise_option in "${enterprise_options[@]}"; do
case $REPLY in
1) setup_worker; setup_appengine; break ;;
2) setup_appengine; break ;;
3) setup_worker; break ;;
4) setup_domain; save_config; break ;;
5) setup_database; save_config; break ;;
6) migrate_database_enterprise; break ;;
7) start_proxy; break ;;
q) enterprise_done=1; break;;
*) echo "What's that?" ;;
esac
done
echo ""
done
}
if [ "$1" = "--instance" ];then
shift;
if [ -d "${PWD}/install" ]; then
THIS_DIR=$PWD
source ${THIS_DIR}/install/config.sh
source ${THIS_DIR}/install/worker.sh
setup_enterprise;
else
echo ""
echo "Directory starthinker not found."
echo "This utility must be run from the StarThinker directory containing the install folder."
echo "Please change directories and try again."
echo ""
fi
fi