-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinstall_to_gcp.sh
More file actions
executable file
·1140 lines (1039 loc) · 55.4 KB
/
Copy pathinstall_to_gcp.sh
File metadata and controls
executable file
·1140 lines (1039 loc) · 55.4 KB
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2023 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
#
# http://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.
#!/bin/bash -u
# Import the environment variables to be used in this installer
source ./env.sh
# Constants
APP_ENGINE_REGION=us-central
BQ_FEED_DATASET=feed_data
BQ_MONITOR_DATASET=monitor_data
CLOUD_COMPOSER_ENV_NAME=process-completion-monitor
CLOUD_COMPOSER_ZONE=us-central1-f
CLOUD_COMPOSER_IMAGE_VERSION=composer-2.0.28-airflow-2.3.3
DAG_ID=completion_monitor
EXPIRATION_TRACKING_TABLE_ID=items_expiration_tracking
ITEM_RESULTS_TABLE_ID=item_results
ITEMS_TABLE=items
KEYRING=cf_secret_keyring
KEYNAME=cf_secret_key
PROCESS_RESULT_TABLE_ID=process_result
PUBSUB_TOPIC_MAILER=mailer-trigger
PUBSUB_TOPIC_CLOUD_BUILD=cloud-build
REGION=us-central1
MAILER_SUBSCRIPTION=push-to-gae-mailer
CLOUD_BUILD_SUBSCRIPTION=push-to-gae-build-reporter
CALCULATE_PRODUCT_CHANGES_FUNCTION_NAME="calculate_product_changes"
FEED_IMPORT_FUNCTION_NAME="import_storage_file_into_big_query"
FEED_RETRY_FUNCTION_NAME="reprocess_feed_file"
TRIGGER_DAG_FUNCTION_NAME="trigger_dag"
FEED_SCHEMA_CONFIG_FILENAME="feed_schema_config.json"
# Constants for Local Inventory Feeds
ARCHIVE_BUCKET_LOCAL="${ARCHIVE_BUCKET}-local"
COMPLETED_FILES_BUCKET_LOCAL="${COMPLETED_FILES_BUCKET}-local"
FEED_BUCKET_LOCAL="${FEED_BUCKET}-local"
LOCK_BUCKET_LOCAL="${LOCK_BUCKET}-local"
RETRIGGER_BUCKET_LOCAL="${RETRIGGER_BUCKET}-local"
TRIGGER_COMPLETION_BUCKET_LOCAL="${TRIGGER_COMPLETION_BUCKET}-local"
UPDATE_BUCKET_LOCAL="${UPDATE_BUCKET}-local"
BQ_FEED_DATASET_LOCAL="${BQ_FEED_DATASET}_local"
BQ_MONITOR_DATASET_LOCAL="${BQ_MONITOR_DATASET}_local"
DAG_ID_LOCAL="${DAG_ID}_local"
FEED_SCHEMA_CONFIG_FILENAME_LOCAL="feed_schema_config_local.json"
if echo "$OSTYPE" | grep -q darwin
then
PUBSUB_TOKEN=$(uuidgen)
GREEN='\x1B[1;32m'
NOCOLOR='\x1B[0m'
HYPERLINK='\x1B]8;;'
else
PUBSUB_TOKEN=$(cat /proc/sys/kernel/random/uuid)
GREEN='\033[1;32m'
NOCOLOR='\033[0m'
HYPERLINK='\033]8;;'
fi
print_green() {
echo -e "${GREEN}$1${NOCOLOR}"
}
# Validate that environment variables are within expected thresholds
if (($EXPIRATION_THRESHOLD < 1 || $EXPIRATION_THRESHOLD > 30)); then
print_green "EXPIRATION_THRESHOLD has an invalid value. Please set this variable to a number between 1 and 30 and try running this script again."
exit 1
fi
TIMEZONE_REGEX="(\+|\-)[0-9]{2}\:[0-9]{2}"
if ! [[ $TIMEZONE_UTC_OFFSET =~ $TIMEZONE_REGEX ]]; then
print_green "TIMEZONE_UTC_OFFSET was not formatted properly. Please specify the value in +/-DD:DD format and try running this script again."
exit 1
fi
USE_LOCAL_INVENTORY_ADS=$(echo "$USE_LOCAL_INVENTORY_ADS" | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')
if ! [[ "${USE_LOCAL_INVENTORY_ADS}" = "True" || "${USE_LOCAL_INVENTORY_ADS}" = "False" ]]; then
print_green "USE_LOCAL_INVENTORY_ADS must be set to a value of True or False. Please set this variable and try running this script again."
exit 1
fi
IS_MCA=$(echo "$IS_MCA" | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')
if ! [[ "${IS_MCA}" = "True" || "${IS_MCA}" = "False" ]]; then
print_green "IS_MCA must be set to a value of True or False. Please set this variable and try running this script again."
exit 1
fi
SHOPTIMIZER_INTEGRATION_ON=$(echo "$SHOPTIMIZER_INTEGRATION_ON" | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')
if ! [[ "${SHOPTIMIZER_API_INTEGRATION_ON}" = "True" || "${SHOPTIMIZER_API_INTEGRATION_ON}" = "False" ]]; then
print_green "SHOPTIMIZER_API_INTEGRATION_ON must be set to a value of True or False. Please set this variable and try running this script again."
exit 1
fi
ENABLE_LOCAL_INVENTORY_FEEDS=$(echo "$ENABLE_LOCAL_INVENTORY_FEEDS" | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}')
if ! [[ "${ENABLE_LOCAL_INVENTORY_FEEDS}" = "True" || "${ENABLE_LOCAL_INVENTORY_FEEDS}" = "False" ]]; then
print_green "ENABLE_LOCAL_INVENTORY_FEEDS must be set to a value of True or False. Please set this variable and try running this script again."
exit 1
fi
# Validate the relationship between feed type and ads type.
if [[ "${USE_LOCAL_INVENTORY_ADS}" = "False" && "${ENABLE_LOCAL_INVENTORY_FEEDS}" = "True" ]]; then
print_green "USE_LOCAL_INVENTORY_ADS must be set to True if ENABLE_LOCAL_INVENTORY_FEEDS is True. Please set these variables correctly and try running this script again."
exit 1
fi
# Authorize with a Google Account.
gcloud auth login
print_green "Feedloader Installation started..."
# Set default project.
gcloud config set project "$GCP_PROJECT"
# Enable all necessary APIs
print_green "Enabling Cloud APIs if necessary..."
REQUIRED_SERVICES=(
appengine.googleapis.com
appengineflex.googleapis.com
bigquerydatatransfer.googleapis.com
bigquery-json.googleapis.com
cloudbuild.googleapis.com
cloudfunctions.googleapis.com
cloudkms.googleapis.com
cloudtasks.googleapis.com
composer.googleapis.com
logging.googleapis.com
monitoring.googleapis.com
pubsub.googleapis.com
run.googleapis.com
serviceusage.googleapis.com
shoppingcontent.googleapis.com
sourcerepo.googleapis.com
storage-api.googleapis.com
storage-component.googleapis.com
)
ENABLED_SERVICES=$(gcloud services list)
for SERVICE in "${REQUIRED_SERVICES[@]}"
do
if echo "$ENABLED_SERVICES" | grep -q "$SERVICE"
then
echo "$SERVICE is already enabled."
else
gcloud services enable "$SERVICE" \
&& echo "$SERVICE has been successfully enabled."
sleep 1
fi
done
# Create Cloud Storage buckets
print_green "Creating Cloud Storage Buckets..."
CreateBuckets() {
local ARCHIVE_BUCKET=$1
local COMPLETED_FILES_BUCKET=$2
local FEED_BUCKET=$3
local LOCK_BUCKET=$4
local MONITOR_BUCKET=$5
local RETRIGGER_BUCKET=$6
local TRIGGER_COMPLETION_BUCKET=$7
local UPDATE_BUCKET=$8
local STORAGE_BUCKETS=(
"$ARCHIVE_BUCKET"
"$COMPLETED_FILES_BUCKET"
"$FEED_BUCKET"
"$LOCK_BUCKET"
"$MONITOR_BUCKET"
"$RETRIGGER_BUCKET"
"$TRIGGER_COMPLETION_BUCKET"
"$UPDATE_BUCKET"
)
for BUCKET in "${STORAGE_BUCKETS[@]}"
do
BUCKET_CREATE_RESULT=$(gcloud storage buckets create --location="$REGION" "$BUCKET" 2>&1)
sleep 1
if [[ $BUCKET_CREATE_RESULT = *"Did you mean to use a gs:// URL"* ]]
then
gcloud storage buckets create --location="$REGION" "gs://$BUCKET"
sleep 1
fi
done
# Use the .json file from here for the bucket lifecycle command below
# (Strip gs:// if it exists and re-add it to ensure valid bucket name)
local ARCHIVE_BUCKET=${ARCHIVE_BUCKET/gs:\/\/}
local ARCHIVE_BUCKET="gs://$ARCHIVE_BUCKET"
gcloud storage buckets update "$ARCHIVE_BUCKET" --lifecycle-file=archive_bucket_lifecycle.json
}
CreateBuckets "$ARCHIVE_BUCKET" "$COMPLETED_FILES_BUCKET" "$FEED_BUCKET" \
"$LOCK_BUCKET" "$MONITOR_BUCKET" "$RETRIGGER_BUCKET" \
"$TRIGGER_COMPLETION_BUCKET" "$UPDATE_BUCKET"
if echo "$ENABLE_LOCAL_INVENTORY_FEEDS"
then
CreateBuckets "$ARCHIVE_BUCKET_LOCAL" "$COMPLETED_FILES_BUCKET_LOCAL" \
"$FEED_BUCKET_LOCAL" "$LOCK_BUCKET_LOCAL" \
"$MONITOR_BUCKET_LOCAL" "$RETRIGGER_BUCKET_LOCAL" \
"$TRIGGER_COMPLETION_BUCKET_LOCAL" "$UPDATE_BUCKET_LOCAL"
fi
# Create service accounts
print_green "Creating service accounts and clearing their keys..."
DeleteAllServiceAccountKeys() {
local SERVICE_ACCOUNT_EMAIL=$1
for KEY in $(gcloud alpha iam service-accounts keys list --iam-account="$SERVICE_ACCOUNT_EMAIL" --managed-by="user" | awk '{if(NR>1)print}' | awk '{print $1;}')
do
gcloud alpha iam service-accounts keys -q delete "$KEY" --iam-account="$SERVICE_ACCOUNT_EMAIL"
sleep 1
done
}
FEED_SERVICE_ACCOUNT=shopping-feed@"$GCP_PROJECT".iam.gserviceaccount.com
if gcloud iam service-accounts list | grep -q "$FEED_SERVICE_ACCOUNT"
then
echo "$FEED_SERVICE_ACCOUNT already exists."
else
gcloud iam service-accounts create shopping-feed --display-name "Shopping Feed"
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:"$FEED_SERVICE_ACCOUNT" \
--role roles/editor \
&& echo "$FEED_SERVICE_ACCOUNT has been successfully created."
fi
DeleteAllServiceAccountKeys "$FEED_SERVICE_ACCOUNT"
MC_SERVICE_ACCOUNT=merchant-center@"$GCP_PROJECT".iam.gserviceaccount.com
if gcloud iam service-accounts list | grep -q "$MC_SERVICE_ACCOUNT"
then
echo "$MC_SERVICE_ACCOUNT already exists."
else
gcloud iam service-accounts create merchant-center --display-name "Merchant Center Service Account"
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:"$MC_SERVICE_ACCOUNT" \
--role roles/editor \
&& echo "$MC_SERVICE_ACCOUNT has been successfully created."
fi
DeleteAllServiceAccountKeys "$MC_SERVICE_ACCOUNT"
# Create BigQuery datasets and tables
print_green "Creating BigQuery datasets and tables..."
CreateBQTables() {
local BQ_FEED_DATASET=$1
local BQ_MONITOR_DATASET=$2
# If NEED_EXPIRATION_TABLE is true, the expiration related tables are created.
# Otherwise, they are not created. This logic is needed because local
# inventory feed does not have expiration.
local NEED_EXPIRATION_TABLE=$3
if bq ls --all | grep -q -w "$BQ_FEED_DATASET"
then
echo "$BQ_FEED_DATASET already exists."
else
bq --location=US mk -d "$BQ_FEED_DATASET"
fi
bq rm -f "$BQ_FEED_DATASET".streaming_items
bq mk -t \
--schema 'item_id:STRING,google_merchant_id:STRING,hashed_content:STRING,import_datetime:DATETIME' \
"$BQ_FEED_DATASET".streaming_items
bq rm -f "$BQ_FEED_DATASET".items_to_delete
bq mk -t --schema 'item_id:STRING,google_merchant_id:STRING' "$BQ_FEED_DATASET".items_to_delete
bq rm -f "$BQ_FEED_DATASET".items_to_upsert
bq mk -t --schema 'item_id:STRING' "$BQ_FEED_DATASET".items_to_upsert
if "${NEED_EXPIRATION_TABLE}"; then
bq rm -f "$BQ_FEED_DATASET".items_expiration_tracking
bq mk -t \
--schema 'item_id:STRING,last_touched_date:DATE' \
"$BQ_FEED_DATASET".items_expiration_tracking
bq rm -f "$BQ_FEED_DATASET".items_to_prevent_expiring
bq mk -t \
--schema 'item_id:STRING' \
"$BQ_FEED_DATASET".items_to_prevent_expiring
fi
if bq ls --all | grep -q -w "$BQ_MONITOR_DATASET"
then
echo "$BQ_MONITOR_DATASET already exists."
else
bq --location US mk -d "$BQ_MONITOR_DATASET"
fi
bq rm -f "$BQ_MONITOR_DATASET"."$PROCESS_RESULT_TABLE_ID"
bq mk -t "$BQ_MONITOR_DATASET"."$PROCESS_RESULT_TABLE_ID" \
channel:STRING,operation:STRING,timestamp:STRING,batch_id:INTEGER,success_count:INTEGER,failure_count:INTEGER,skipped_count:INTEGER
bq rm -f "$BQ_MONITOR_DATASET"."$ITEM_RESULTS_TABLE_ID"
bq mk -t "$BQ_MONITOR_DATASET"."$ITEM_RESULTS_TABLE_ID" \
item_id:STRING,batch_id:INTEGER,channel:STRING,operation:STRING,result:STRING,error:STRING,timestamp:STRING
print_green "Creating scheduled Queries..."
# Remove any pre-existing scheduled queries from the Cloud project
EXISTING_SCHEDULED_QUERIES=$(bq ls --transfer_config --transfer_location=us --project_id="$GCP_PROJECT" | grep projects | awk '{print $1}')
for SCHEDULED_QUERY in $(echo "$EXISTING_SCHEDULED_QUERIES")
do
bq rm -f --transfer_config "$SCHEDULED_QUERY"
sleep 1
done
# Schedule a query to cleanup the streaming_items table periodically
bq query \
--use_legacy_sql=false \
--destination_table="$BQ_FEED_DATASET".streaming_items \
--display_name='Cleanup streaming_items older than 30 days' \
--schedule='every 730 hours' \
--replace=true \
"SELECT
*
FROM
$BQ_FEED_DATASET.streaming_items
WHERE import_datetime > DATETIME_SUB(CURRENT_DATETIME(), INTERVAL 30 DAY)"
# Schedule a query to cleanup the item_results table periodically
bq query \
--use_legacy_sql=false \
--destination_table="$BQ_MONITOR_DATASET".item_results \
--display_name='Cleanup item_results older than 30 days' \
--schedule='every 730 hours' \
--replace=true \
"SELECT
*
FROM
$BQ_MONITOR_DATASET.item_results
WHERE timestamp > FORMAT_DATETIME(\"%Y%m%d\", DATETIME_SUB(CURRENT_DATETIME(), INTERVAL 30 DAY))"
}
CreateBQTables "$BQ_FEED_DATASET" "$BQ_MONITOR_DATASET" true
if echo "$ENABLE_LOCAL_INVENTORY_FEEDS"
then
CreateBQTables "$BQ_FEED_DATASET_LOCAL" "$BQ_MONITOR_DATASET_LOCAL" false
fi
# Create Cloud Pub/Sub topics
print_green "Creating PubSub topics and subscriptions..."
REQUIRED_TOPICS=(
"$PUBSUB_TOPIC_MAILER"
"$PUBSUB_TOPIC_CLOUD_BUILD"
)
EXISTING_TOPICS=$(gcloud pubsub topics list)
for TOPIC in "${REQUIRED_TOPICS[@]}"
do
if echo "$EXISTING_TOPICS" | grep -q "$TOPIC"
then
echo "Pub/Sub topic $TOPIC already exists."
else
gcloud pubsub topics create "$TOPIC" \
&& echo "Pub/Sub topic $TOPIC has been successfully created."
sleep 1
fi
done
if gcloud beta pubsub subscriptions list | grep -q "$MAILER_SUBSCRIPTION"
then
echo "Pub/Sub subscription $MAILER_SUBSCRIPTION already exists."
else
gcloud beta pubsub subscriptions create \
--topic "$PUBSUB_TOPIC_MAILER" \
--push-endpoint https://mailer-dot-"$GCP_PROJECT".appspot.com/pubsub/push?token="$PUBSUB_TOKEN" \
--ack-deadline 600 \
--expiration-period never \
--message-retention-duration 10m \
"$MAILER_SUBSCRIPTION" \
&& echo "Pub/Sub subscription $MAILER_SUBSCRIPTION has been successfully created."
fi
if gcloud beta pubsub subscriptions list | grep -q "$CLOUD_BUILD_SUBSCRIPTION"
then
echo "Pub/Sub subscription $CLOUD_BUILD_SUBSCRIPTION already exists."
else
gcloud beta pubsub subscriptions create \
--topic $PUBSUB_TOPIC_CLOUD_BUILD \
--push-endpoint https://build-reporter-dot-"$GCP_PROJECT".appspot.com/pubsub/push \
--ack-deadline 600 \
--expiration-period never \
--message-retention-duration 10m \
"$CLOUD_BUILD_SUBSCRIPTION" \
&& echo "Pub/Sub subscription $CLOUD_BUILD_SUBSCRIPTION has been successfully created."
fi
# Set up AppEngine
print_green "Setting up AppEngine..."
if ! gcloud app describe &> /dev/null; then
gcloud app create --region="$APP_ENGINE_REGION"
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
gcloud app deploy python-docs-samples/appengine/standard_python3/hello_world --project "$GCP_PROJECT" --quiet
rm -rf ./python-docs-samples
fi
# Set up AppEngine service account
print_green "Creating AppEngine service account keys..."
APP_ENGINE_SERVICE_ACCOUNT_KEYS=(
appengine/uploader/config/gcp_service_account.json
appengine/initiator/config/service_account.json
)
for SERVICE_ACCOUNT_KEY in "${APP_ENGINE_SERVICE_ACCOUNT_KEYS[@]}"
do
if [[ -f "$SERVICE_ACCOUNT_KEY" ]]
then
echo "Service account key $SERVICE_ACCOUNT_KEY already exists. Deleting and recreating it..."
rm "$SERVICE_ACCOUNT_KEY"
fi
gcloud iam service-accounts keys create \
"$SERVICE_ACCOUNT_KEY" \
--iam-account "$FEED_SERVICE_ACCOUNT" \
&& echo "Service account key $SERVICE_ACCOUNT_KEY has been successfully created."
sleep 1
done
MC_SERVICE_ACCOUNT_KEY=appengine/uploader/config/mc_service_account.json
if [[ -f "$MC_SERVICE_ACCOUNT_KEY" ]]
then
echo "Service account key $MC_SERVICE_ACCOUNT_KEY already exists."
rm "$MC_SERVICE_ACCOUNT_KEY"
fi
gcloud iam service-accounts keys create \
"$MC_SERVICE_ACCOUNT_KEY" \
--iam-account merchant-center@"$GCP_PROJECT".iam.gserviceaccount.com \
&& echo "Service account key $MC_SERVICE_ACCOUNT_KEY has been successfully created."
# Create a new Cloud Source Repository for Git
print_green "Creating the Git repository..."
EXISTING_REPOS="$(gcloud source repos list --filter="$SOURCE_REPO")"
if echo "$EXISTING_REPOS" | grep -q -w "$SOURCE_REPO"
then
echo "Cloud Source Repository $SOURCE_REPO already exists."
else
gcloud source repos create "$SOURCE_REPO"
fi
# Set up Cloud Composer
PROJECT_NUMBER=$(gcloud projects list --filter="PROJECT_ID=$GCP_PROJECT" --format="value(PROJECT_NUMBER)")
print_green "Setting up Cloud Composer. This could take a while..."
gcloud components install kubectl
if gcloud beta composer environments list --locations="$REGION" | grep -w "$CLOUD_COMPOSER_ENV_NAME" &> /dev/null
then
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:service-"${PROJECT_NUMBER}"@cloudcomposer-accounts.iam.gserviceaccount.com \
--role roles/composer.ServiceAgentV2Ext
gcloud composer environments delete "$CLOUD_COMPOSER_ENV_NAME" --quiet \
--location "$REGION"
fi
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:service-"${PROJECT_NUMBER}"@cloudcomposer-accounts.iam.gserviceaccount.com \
--role roles/composer.ServiceAgentV2Ext
gcloud composer environments create "$CLOUD_COMPOSER_ENV_NAME" \
--image-version="$CLOUD_COMPOSER_IMAGE_VERSION" \
--airflow-configs=core-default_timezone=Asia/Tokyo \
--location "$REGION" \
--env-variables LOCATION="$REGION",PUBSUB_TOPIC="$PUBSUB_TOPIC_MAILER"
echo "Installing Python libraries in Cloud Composer..."
COMPOSER_UPDATE_RESULT=$(gcloud composer environments update "$CLOUD_COMPOSER_ENV_NAME" \
--location "$REGION" \
--update-pypi-packages-from-file composer/completion_monitor/requirements.txt \
2>&1)
if [[ "$COMPOSER_UPDATE_RESULT" == "ERROR:"* ]];
then
if [[ "$COMPOSER_UPDATE_RESULT" == *"No change in configuration"* ]];
then
echo "INFO: Did not update Composer environment because there is no change in configuration"
else
echo "$COMPOSER_UPDATE_RESULT"
exit 1
fi
else
echo "$COMPOSER_UPDATE_RESULT"
fi
sed -e "s/<DAG_ID>/$DAG_ID/g" \
-e "s/<GCP_PROJECT_ID>/$GCP_PROJECT/g" \
-e "s/<QUEUE_LOCATION>/$REGION/g" \
-e "s/<MONITOR_DATASET_ID>/$BQ_MONITOR_DATASET/g" \
-e "s/<TIMEZONE_UTC_OFFSET>/$TIMEZONE_UTC_OFFSET/g" \
-e "s/<FEED_DATASET_ID>/$BQ_FEED_DATASET/g" \
-e "s/<ITEMS_TABLE_ID>/$ITEMS_TABLE/g" \
-e "s/<EXPIRATION_TRACKING_TABLE_ID>/$EXPIRATION_TRACKING_TABLE_ID/g" \
-e "s/<ITEM_RESULTS_TABLE_ID>/$ITEM_RESULTS_TABLE_ID/g" \
-e "s%<LOCK_BUCKET>%$LOCK_BUCKET%g" \
composer/completion_monitor/config/variables_template.json > composer/completion_monitor/config/variables.json
print_green "Creating service account keys for Cloud Composer..."
AIRFLOW_SERVICE_ACCOUNT_KEY=composer/completion_monitor/config/service_account.json
if [[ -f "$AIRFLOW_SERVICE_ACCOUNT_KEY" ]]
then
echo "Service account key $AIRFLOW_SERVICE_ACCOUNT_KEY already exists."
rm "$AIRFLOW_SERVICE_ACCOUNT_KEY"
fi
gcloud iam service-accounts keys create \
"$AIRFLOW_SERVICE_ACCOUNT_KEY" \
--iam-account "$FEED_SERVICE_ACCOUNT" \
&& echo "Service account key $AIRFLOW_SERVICE_ACCOUNT_KEY has been successfully created."
# Extract Cloud Composer Airflow Webserver ID
AIRFLOW_WEBSERVER_ID=$(gcloud composer environments describe "$CLOUD_COMPOSER_ENV_NAME" --location "$REGION" | grep -E "airflowUri" | awk '{print $NF}')
print_green "Returned Airflow Webserver ID was: $AIRFLOW_WEBSERVER_ID"
# Delete existing policies in case they are being re-created
print_green "Checking what alerting policies are already installed and deleting them first..."
EXISTING_POLICIES="$(gcloud alpha monitoring policies list | grep "name:" | grep -v "conditions" | awk '{printf("%s\n", $2)}')"
for POLICY_ID in $(echo "$EXISTING_POLICIES")
do
gcloud alpha monitoring policies -q delete "$POLICY_ID"
sleep 2
done
# Create log metrics for alerts
print_green "Removing and re-creating log metrics..."
EXISTING_METRICS=$(gcloud logging metrics list)
IMPORT_STORAGE_FILE_ERRORS=ImportStorageFileErrors
if echo "$EXISTING_METRICS" | grep -q -w "$IMPORT_STORAGE_FILE_ERRORS"
then
echo "Metric $IMPORT_STORAGE_FILE_ERRORS already exists. Deleting it and recreating..."
gcloud logging metrics -q delete $IMPORT_STORAGE_FILE_ERRORS
fi
gcloud logging metrics create "$IMPORT_STORAGE_FILE_ERRORS" \
--description="Count of the import_storage_file_into_big_query Cloud Function Errors" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"import_storage_file_into_big_query\" severity>=ERROR" \
&& echo "Metric $IMPORT_STORAGE_FILE_ERRORS has been successfully created."
BQ_LOAD_SUCCESSES=BQLoadSuccesses
if echo "$EXISTING_METRICS" | grep -q -w "$BQ_LOAD_SUCCESSES"
then
echo "Metric $BQ_LOAD_SUCCESSES already exists."
gcloud logging metrics -q delete $BQ_LOAD_SUCCESSES
fi
gcloud logging metrics create "$BQ_LOAD_SUCCESSES" \
--description="Count of the import_storage_file_into_big_query Cloud Function Successes" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"import_storage_file_into_big_query\" \"rows to table\"" \
&& echo "Metric $BQ_LOAD_SUCCESSES has been successfully created."
IMPORT_FUNCTION_TIMEOUT_ERRORS=ImportFunctionTimeouts
if echo "$EXISTING_METRICS" | grep -q -w "$IMPORT_FUNCTION_TIMEOUT_ERRORS"
then
echo "Metric $IMPORT_FUNCTION_TIMEOUT_ERRORS already exists."
gcloud logging metrics -q delete $IMPORT_FUNCTION_TIMEOUT_ERRORS
fi
gcloud logging metrics create "$IMPORT_FUNCTION_TIMEOUT_ERRORS" \
--description="Count of the errors when import_storage_file_into_big_query function times out." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"import_storage_file_into_big_query\" \"Dropping event\"" \
&& echo "Metric $IMPORT_FUNCTION_TIMEOUT_ERRORS has been successfully created."
BQ_LOAD_FAILURES=BQLoadFailures
if echo "$EXISTING_METRICS" | grep -q -w "$BQ_LOAD_FAILURES"
then
echo "Metric $BQ_LOAD_FAILURES already exists."
gcloud logging metrics -q delete $BQ_LOAD_FAILURES
fi
gcloud logging metrics create "$BQ_LOAD_FAILURES" \
--description="Count of the import_storage_file_into_big_query Cloud Function BigQuery load failures" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"import_storage_file_into_big_query\" (\"BigQuery load job failed\" OR \"One or more errors occurred while loading the feed\")" \
&& echo "Metric $BQ_LOAD_FAILURES has been successfully created."
EOF_EXISTS_DURING_IMPORT_ERRORS=EOFExistsErrors
if echo "$EXISTING_METRICS" | grep -q -w "$EOF_EXISTS_DURING_IMPORT_ERRORS"
then
echo "Metric $EOF_EXISTS_DURING_IMPORT_ERRORS already exists."
gcloud logging metrics -q delete $EOF_EXISTS_DURING_IMPORT_ERRORS
fi
gcloud logging metrics create "$EOF_EXISTS_DURING_IMPORT_ERRORS" \
--description="Count of the import_storage_file_into_big_query Cloud Function errors due to existing EOF preventing concurrent runs from starting" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"import_storage_file_into_big_query\" resource.labels.region=\"us-central1\" \"An EOF file was found in bucket\"" \
&& echo "Metric $EOF_EXISTS_DURING_IMPORT_ERRORS has been successfully created."
CALCULATE_PRODUCT_CHANGES_ERRORS=CalculateProductChangesErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CALCULATE_PRODUCT_CHANGES_ERRORS"
then
echo "Metric $CALCULATE_PRODUCT_CHANGES_ERRORS already exists."
gcloud logging metrics -q delete $CALCULATE_PRODUCT_CHANGES_ERRORS
fi
gcloud logging metrics create "$CALCULATE_PRODUCT_CHANGES_ERRORS" \
--description="Count of the calculateProductChanges Cloud Function Errors" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" severity>=ERROR" \
&& echo "Metric $CALCULATE_PRODUCT_CHANGES_ERRORS has been successfully created."
SCHEMA_CONFIG_PARSE_FAILURE_FOR_LOAD=SchemaConfigParseFailureForLoad
if echo "$EXISTING_METRICS" | grep -q -w "$SCHEMA_CONFIG_PARSE_FAILURE_FOR_LOAD"
then
echo "Metric $SCHEMA_CONFIG_PARSE_FAILURE_FOR_LOAD already exists."
gcloud logging metrics -q delete $SCHEMA_CONFIG_PARSE_FAILURE_FOR_LOAD
fi
gcloud logging metrics create "$SCHEMA_CONFIG_PARSE_FAILURE_FOR_LOAD" \
--description="Logs indicating that the schema config file failed to be parsed for creating the items table" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"import_storage_file_into_big_query\" \"Schema is invalid\"" \
&& echo "Metric $SCHEMA_CONFIG_PARSE_FAILURE_FOR_LOAD has been successfully created."
SCHEMA_CONFIG_PARSE_FAILURE_FOR_CALCULATION=SchemaConfigParseFailureForCalculation
if echo "$EXISTING_METRICS" | grep -q -w "$SCHEMA_CONFIG_PARSE_FAILURE_FOR_CALCULATION"
then
echo "Metric $SCHEMA_CONFIG_PARSE_FAILURE_FOR_CALCULATION already exists."
gcloud logging metrics -q delete $SCHEMA_CONFIG_PARSE_FAILURE_FOR_CALCULATION
fi
gcloud logging metrics create "$SCHEMA_CONFIG_PARSE_FAILURE_FOR_CALCULATION" \
--description="Logs indicating that the schema config file failed to be injected into the SQL queries" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Unable to map any columns from the schema config\"" \
&& echo "Metric $SCHEMA_CONFIG_PARSE_FAILURE_FOR_CALCULATION has been successfully created."
DELETES_THRESHOLD_CROSSED=DeletesThresholdCrossed
if echo "$EXISTING_METRICS" | grep -q -w "$DELETES_THRESHOLD_CROSSED"
then
echo "Metric $DELETES_THRESHOLD_CROSSED already exists."
gcloud logging metrics -q delete $DELETES_THRESHOLD_CROSSED
fi
gcloud logging metrics create "$DELETES_THRESHOLD_CROSSED" \
--description="Logs indicating that the delete count crossed the allowed delete threshold amount" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"crossed deletes threshold\"" \
&& echo "Metric $DELETES_THRESHOLD_CROSSED has been successfully created."
UPSERTS_THRESHOLD_CROSSED=UpsertsThresholdCrossed
if echo "$EXISTING_METRICS" | grep -q -w "$UPSERTS_THRESHOLD_CROSSED"
then
echo "Metric $UPSERTS_THRESHOLD_CROSSED already exists."
gcloud logging metrics -q delete $UPSERTS_THRESHOLD_CROSSED
fi
gcloud logging metrics create "$UPSERTS_THRESHOLD_CROSSED" \
--description="Logs indicating that the upsert count crossed the allowed upsert threshold amount" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"crossed upserts threshold\"" \
&& echo "Metric $UPSERTS_THRESHOLD_CROSSED has been successfully created."
COUNT_DELETES_QUERY_FAILURES=CountDeletesQueryFailures
if echo "$EXISTING_METRICS" | grep -q -w "$COUNT_DELETES_QUERY_FAILURES"
then
echo "Metric $COUNT_DELETES_QUERY_FAILURES already exists."
gcloud logging metrics -q delete $COUNT_DELETES_QUERY_FAILURES
fi
gcloud logging metrics create "$COUNT_DELETES_QUERY_FAILURES" \
--description="Count of the COUNT_DELETES_QUERY job errors in calculateProductChanges" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Delete count and publish job failed\"" \
&& echo "Metric $COUNT_DELETES_QUERY_FAILURES has been successfully created."
COUNT_UPSERTS_QUERY_FAILURES=CountUpsertsQueryFailures
if echo "$EXISTING_METRICS" | grep -q -w "$COUNT_UPSERTS_QUERY_FAILURES"
then
echo "Metric $COUNT_UPSERTS_QUERY_FAILURES already exists."
gcloud logging metrics -q delete $COUNT_UPSERTS_QUERY_FAILURES
fi
gcloud logging metrics create "$COUNT_UPSERTS_QUERY_FAILURES" \
--description="Count of the COUNT_UPSERTS_QUERY job errors in calculateProductChanges" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Upsert count and publish job failed\"" \
&& echo "Metric $COUNT_UPSERTS_QUERY_FAILURES has been successfully created."
INVALID_EOF_ERRORS=InvalidEofErrors
if echo "$EXISTING_METRICS" | grep -q -w "$INVALID_EOF_ERRORS"
then
echo "Metric $INVALID_EOF_ERRORS already exists."
gcloud logging metrics -q delete $INVALID_EOF_ERRORS
fi
gcloud logging metrics create "$INVALID_EOF_ERRORS" \
--description="Count of the errors in calculateProductChanges for invalid EOF upload" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"File was not an empty EOF\"" \
&& echo "Metric $INVALID_EOF_ERRORS has been successfully created."
EOF_LOCK_CHECK_ERRORS=EofLockCheckErrors
if echo "$EXISTING_METRICS" | grep -q -w "$EOF_LOCK_CHECK_ERRORS"
then
echo "Metric $EOF_LOCK_CHECK_ERRORS already exists."
gcloud logging metrics -q delete $EOF_LOCK_CHECK_ERRORS
fi
gcloud logging metrics create "$EOF_LOCK_CHECK_ERRORS" \
--description="Count of the errors in calculateProductChanges for EOF.lock check failure" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"EOF.lock check failed\"" \
&& echo "Metric $EOF_LOCK_CHECK_ERRORS has been successfully created."
EOF_LOCKED_ERRORS=EofLockedErrors
if echo "$EXISTING_METRICS" | grep -q -w "$EOF_LOCKED_ERRORS"
then
echo "Metric $EOF_LOCKED_ERRORS already exists."
gcloud logging metrics -q delete $EOF_LOCKED_ERRORS
fi
gcloud logging metrics create "$EOF_LOCKED_ERRORS" \
--description="Count of the errors in calculateProductChanges for locked EOF errors" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"An EOF.lock file was found, which indicates that this function is still running\"" \
&& echo "Metric $EOF_LOCKED_ERRORS has been successfully created."
EOF_LOCK_FAILED_ERRORS=EofLockFailedErrors
if echo "$EXISTING_METRICS" | grep -q -w "$EOF_LOCK_FAILED_ERRORS"
then
echo "Metric $EOF_LOCK_FAILED_ERRORS already exists."
gcloud logging metrics -q delete $EOF_LOCK_FAILED_ERRORS
fi
gcloud logging metrics create "$EOF_LOCK_FAILED_ERRORS" \
--description="Count of the errors in calculateProductChanges for EOF lock failed errors" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"EOF could not be locked! Exiting Function\"" \
&& echo "Metric $EOF_LOCK_FAILED_ERRORS has been successfully created."
EOF_UNLOCK_FAILED_ERRORS=EofUnlockFailedErrors
if echo "$EXISTING_METRICS" | grep -q -w "$EOF_UNLOCK_FAILED_ERRORS"
then
echo "Metric $EOF_UNLOCK_FAILED_ERRORS already exists."
gcloud logging metrics -q delete $EOF_UNLOCK_FAILED_ERRORS
fi
gcloud logging metrics create "$EOF_UNLOCK_FAILED_ERRORS" \
--description="Count of the errors in calculateProductChanges for EOF unlock failed errors" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Subsequent runs will be blocked unless the lock file is removed\"" \
&& echo "Metric $EOF_UNLOCK_FAILED_ERRORS has been successfully created."
NONEXISTENT_TABLE_ERRORS=NonexistentTableErrors
if echo "$EXISTING_METRICS" | grep -q -w "$NONEXISTENT_TABLE_ERRORS"
then
echo "Metric $NONEXISTENT_TABLE_ERRORS already exists."
gcloud logging metrics -q delete $NONEXISTENT_TABLE_ERRORS
fi
gcloud logging metrics create "$NONEXISTENT_TABLE_ERRORS" \
--description="Count of the errors in calculateProductChanges for a nonexistent table" \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"table must exist before\"" \
&& echo "Metric $NONEXISTENT_TABLE_ERRORS has been successfully created."
CALCULATE_DELETES_ERRORS=CalculateDeletesErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CALCULATE_DELETES_ERRORS"
then
echo "Metric $CALCULATE_DELETES_ERRORS already exists."
gcloud logging metrics -q delete $CALCULATE_DELETES_ERRORS
fi
gcloud logging metrics create "$CALCULATE_DELETES_ERRORS" \
--description="Count of the runQueryJob errors in calculateProductChanges for CALCULATE_ITEMS_FOR_DELETION_QUERY." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"CALCULATE_ITEMS_FOR_DELETION_QUERY job failed\"" \
&& echo "Metric $CALCULATE_DELETES_ERRORS has been successfully created."
CALCULATE_UPDATES_ERRORS=CalculateUpdatesErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CALCULATE_UPDATES_ERRORS"
then
echo "Metric $CALCULATE_UPDATES_ERRORS already exists."
gcloud logging metrics -q delete $CALCULATE_UPDATES_ERRORS
fi
gcloud logging metrics create "$CALCULATE_UPDATES_ERRORS" \
--description="Count of the runQueryJob errors in calculateProductChanges for CALCULATE_ITEMS_FOR_UPDATE_QUERY." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"CALCULATE_ITEMS_FOR_UPDATE_QUERY job failed\"" \
&& echo "Metric $CALCULATE_UPDATES_ERRORS has been successfully created."
CALCULATE_INSERTS_ERRORS=CalculateInsertsErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CALCULATE_INSERTS_ERRORS"
then
echo "Metric $CALCULATE_INSERTS_ERRORS already exists."
gcloud logging metrics -q delete $CALCULATE_INSERTS_ERRORS
fi
gcloud logging metrics create "$CALCULATE_INSERTS_ERRORS" \
--description="Count of the runQueryJob errors in calculateProductChanges for CALCULATE_ITEMS_FOR_INSERTION_QUERY." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"CALCULATE_ITEMS_FOR_INSERTION_QUERY job failed.\"" \
&& echo "Metric $CALCULATE_INSERTS_ERRORS has been successfully created."
CHANGES_COUNT_QUERY_ERRORS=ChangesCountQueryErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CHANGES_COUNT_QUERY_ERRORS"
then
echo "Metric $CHANGES_COUNT_QUERY_ERRORS already exists."
gcloud logging metrics -q delete $CHANGES_COUNT_QUERY_ERRORS
fi
gcloud logging metrics create "$CHANGES_COUNT_QUERY_ERRORS" \
--description="Count of the errors in calculateProductChanges when extracting the count of changes." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"could not be extracted from the query result\"" \
&& echo "Metric $CHANGES_COUNT_QUERY_ERRORS has been successfully created."
CREATE_TASK_EMPTY_RESPONSE_ERROR=CreateTaskEmptyResponseErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CREATE_TASK_EMPTY_RESPONSE_ERROR"
then
echo "Metric $CREATE_TASK_EMPTY_RESPONSE_ERROR already exists."
gcloud logging metrics -q delete $CREATE_TASK_EMPTY_RESPONSE_ERROR
fi
gcloud logging metrics create "$CREATE_TASK_EMPTY_RESPONSE_ERROR" \
--description="Count of the Task creation empty responses in calculateProductChanges." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Task creation returned unexpected response\"" \
&& echo "Metric $CREATE_TASK_EMPTY_RESPONSE_ERROR has been successfully created."
CREATE_TASK_EXCEPTION_ERRORS=CreateTaskExceptionErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CREATE_TASK_EXCEPTION_ERRORS"
then
echo "Metric $CREATE_TASK_EXCEPTION_ERRORS already exists."
gcloud logging metrics -q delete $CREATE_TASK_EXCEPTION_ERRORS
fi
gcloud logging metrics create "$CREATE_TASK_EXCEPTION_ERRORS" \
--description="Count of the Task creation exceptions in calculateProductChanges." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Error occurred when creating a Task to start GAE\"" \
&& echo "Metric $CREATE_TASK_EXCEPTION_ERRORS has been successfully created."
ATTEMPTED_FILES_RETRIEVAL_ERRORS=AttemptedFilesRetrievalErrors
if echo "$EXISTING_METRICS" | grep -q -w "$ATTEMPTED_FILES_RETRIEVAL_ERRORS"
then
echo "Metric $ATTEMPTED_FILES_RETRIEVAL_ERRORS already exists."
gcloud logging metrics -q delete $ATTEMPTED_FILES_RETRIEVAL_ERRORS
fi
gcloud logging metrics create "$ATTEMPTED_FILES_RETRIEVAL_ERRORS" \
--description="Count of the attemptedFiles retrieval errors in calculateProductChanges." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"attemptedFiles retrieval failed\"" \
&& echo "Metric $ATTEMPTED_FILES_RETRIEVAL_ERRORS has been successfully created."
IMPORTED_FILES_RETRIEVAL_ERRORS=ImportedFilesRetrievalErrors
if echo "$EXISTING_METRICS" | grep -q -w "$IMPORTED_FILES_RETRIEVAL_ERRORS"
then
echo "Metric $IMPORTED_FILES_RETRIEVAL_ERRORS already exists."
gcloud logging metrics -q delete $IMPORTED_FILES_RETRIEVAL_ERRORS
fi
gcloud logging metrics create "$IMPORTED_FILES_RETRIEVAL_ERRORS" \
--description="Count of the importedFiles retrieval errors in calculateProductChanges." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"importedFiles retrieval failed\"" \
&& echo "Metric $IMPORTED_FILES_RETRIEVAL_ERRORS has been successfully created."
DELETE_IMPORTED_FILENAMES_ERRORS=DeleteImportedFilenamesErrors
if echo "$EXISTING_METRICS" | grep -q -w "$DELETE_IMPORTED_FILENAMES_ERRORS"
then
echo "Metric $DELETE_IMPORTED_FILENAMES_ERRORS already exists."
gcloud logging metrics -q delete $DELETE_IMPORTED_FILENAMES_ERRORS
fi
gcloud logging metrics create "$DELETE_IMPORTED_FILENAMES_ERRORS" \
--description="Count of the failed imported filenames deletes errors in calculateProductChanges." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"One or more errors occurred when deleting from the imported filenames bucket\"" \
&& echo "Metric $DELETE_IMPORTED_FILENAMES_ERRORS has been successfully created."
RETRIEVE_FEEDS_TO_ARCHIVE_ERRORS=RetrieveFeedsToArchiveErrors
if echo "$EXISTING_METRICS" | grep -q -w "$RETRIEVE_FEEDS_TO_ARCHIVE_ERRORS"
then
echo "Metric $RETRIEVE_FEEDS_TO_ARCHIVE_ERRORS already exists."
gcloud logging metrics -q delete $RETRIEVE_FEEDS_TO_ARCHIVE_ERRORS
fi
gcloud logging metrics create "$RETRIEVE_FEEDS_TO_ARCHIVE_ERRORS" \
--description="Count of the errors of retrieving feeds to archive in calculateProductChanges." \
--log-filter="resource.type=\"cloud_function\" resource.labels.function_name=\"calculateProductChanges\" \"Retrieval of feeds to archive failed\"" \
&& echo "Metric $RETRIEVE_FEEDS_TO_ARCHIVE_ERRORS has been successfully created."
CONTENT_API_ITEM_INSERT_ERRORS=ContentApiItemInsertErrors
if echo "$EXISTING_METRICS" | grep -q -w "$CONTENT_API_ITEM_INSERT_ERRORS"
then
echo "Metric $CONTENT_API_ITEM_INSERT_ERRORS already exists."
gcloud logging metrics -q delete $CONTENT_API_ITEM_INSERT_ERRORS
fi
gcloud logging metrics create "$CONTENT_API_ITEM_INSERT_ERRORS" \
--description="Count of the Content API errors for inserting items" \
--log-filter="resource.type=\"gae_app\" \"The item could not be inserted\"" \
&& echo "Metric $CONTENT_API_ITEM_INSERT_ERRORS has been successfully created."
APP_ENGINE_UPLOAD_LOG_ERRORS=AppEngineUploadLogErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_UPLOAD_LOG_ERRORS"
then
echo "Metric $APP_ENGINE_UPLOAD_LOG_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_UPLOAD_LOG_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_UPLOAD_LOG_ERRORS" \
--description="Count of the App Engine errors when writing to the log table fails" \
--log-filter="resource.type=\"gae_app\" \"The result of Content API for Shopping call was not recorded\"" \
&& echo "Metric $APP_ENGINE_UPLOAD_LOG_ERRORS has been successfully created."
APP_ENGINE_MERCHANT_INFO_NOT_FOUND_ERRORS=AppEngineMerchantInfoNotFoundErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_MERCHANT_INFO_NOT_FOUND_ERRORS"
then
echo "Metric $APP_ENGINE_MERCHANT_INFO_NOT_FOUND_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_MERCHANT_INFO_NOT_FOUND_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_MERCHANT_INFO_NOT_FOUND_ERRORS" \
--description="Count of the App Engine errors when merchant-info.json could not be loaded" \
--log-filter="resource.type=\"gae_app\" \"Merchant Info JSON file does not exist\"" \
&& echo "Metric $APP_ENGINE_MERCHANT_INFO_NOT_FOUND_ERRORS has been successfully created."
APP_ENGINE_MERCHANT_INFO_INVALID_ERRORS=AppEngineMerchantInfoInvalidErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_MERCHANT_INFO_INVALID_ERRORS"
then
echo "Metric $APP_ENGINE_MERCHANT_INFO_INVALID_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_MERCHANT_INFO_INVALID_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_MERCHANT_INFO_INVALID_ERRORS" \
--description="Count of the App Engine errors when merchant-info.json could not be parsed" \
--log-filter="resource.type=\"gae_app\" \"Could not parse Merchant center configuration file\"" \
&& echo "Metric $APP_ENGINE_MERCHANT_INFO_INVALID_ERRORS has been successfully created."
APP_ENGINE_TASK_PARSE_ERRORS=AppEngineTaskParseErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_TASK_PARSE_ERRORS"
then
echo "Metric $APP_ENGINE_TASK_PARSE_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_TASK_PARSE_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_TASK_PARSE_ERRORS" \
--description="Count of the App Engine errors when incoming task could not be parsed" \
--log-filter="resource.type=\"gae_app\" \"Error parsing the task JSON\"" \
&& echo "Metric $APP_ENGINE_TASK_PARSE_ERRORS has been successfully created."
APP_ENGINE_RECORD_RESULT_ERRORS=AppEngineRecordResultErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_RECORD_RESULT_ERRORS"
then
echo "Metric $APP_ENGINE_RECORD_RESULT_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_RECORD_RESULT_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_RECORD_RESULT_ERRORS" \
--description="Count of the App Engine errors when API results could not be written to BigQuery" \
--log-filter="resource.type=\"gae_app\" \"The result of Content API for Shopping call was not recorded to BigQuery\"" \
&& echo "Metric $APP_ENGINE_RECORD_RESULT_ERRORS has been successfully created."
APP_ENGINE_RECORD_PER_ITEM_RESULTS_ERRORS=AppEngineRecordPerItemResultsErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_RECORD_PER_ITEM_RESULTS_ERRORS"
then
echo "Metric $APP_ENGINE_RECORD_PER_ITEM_RESULTS_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_RECORD_PER_ITEM_RESULTS_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_RECORD_PER_ITEM_RESULTS_ERRORS" \
--description="Count of the App Engine errors when per item results of the Content API call could not be written to BigQuery" \
--log-filter="resource.type=\"gae_app\" \"The per item results of the Content API for Shopping call were not recorded to BigQuery\"" \
&& echo "Metric $APP_ENGINE_RECORD_PER_ITEM_RESULTS_ERRORS has been successfully created."
APP_ENGINE_TASK_HOSTNAME_ERRORS=AppEngineTaskHostnameErrors
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_TASK_HOSTNAME_ERRORS"
then
echo "Metric $APP_ENGINE_TASK_HOSTNAME_ERRORS already exists."
gcloud logging metrics -q delete $APP_ENGINE_TASK_HOSTNAME_ERRORS
fi
gcloud logging metrics create "$APP_ENGINE_TASK_HOSTNAME_ERRORS" \
--description="Count of the App Engine errors when a Task is created and hostname could not be found" \
--log-filter="resource.type=\"gae_app\" \"Failed to create a task: Failed to resolve hostname\"" \
&& echo "Metric $APP_ENGINE_TASK_HOSTNAME_ERRORS has been successfully created."
APP_ENGINE_PROCESS_BATCH_FAILED=AppEngineProcessBatchFailed
if echo "$EXISTING_METRICS" | grep -q -w "$APP_ENGINE_PROCESS_BATCH_FAILED"
then
echo "Metric $APP_ENGINE_PROCESS_BATCH_FAILED already exists."
gcloud logging metrics -q delete $APP_ENGINE_PROCESS_BATCH_FAILED
fi
gcloud logging metrics create "$APP_ENGINE_PROCESS_BATCH_FAILED" \
--description="Count of the App Engine errors when a batch of items failed to be upserted/deleted and will not be retried" \
--log-filter="resource.type=\"gae_app\" \"failed and will not be retried\"" \
&& echo "Metric $APP_ENGINE_PROCESS_BATCH_FAILED has been successfully created."
# Create Notification Alerts
print_green "Recreating Stackdriver alerts..."
ALERT_POLICIES=(
app-engine-merchant-info-invalid-errors-policy.yaml
app-engine-merchant-info-not-found-errors-policy.yaml
app-engine-process-batch-errors-policy.yaml
app-engine-record-per-item-results-errors-policy.yaml
app-engine-record-result-errors-policy.yaml
app-engine-task-hostname-errors-policy.yaml
app-engine-task-parse-errors-policy.yaml
attempted-files-retrieval-errors-policy.yaml
bq-load-errors-policy.yaml
calculate-changes-function-errors-policy.yaml
calculate-deletes-query-errors-policy.yaml
calculate-inserts-query-errors-policy.yaml
calculate-updates-query-errors-policy.yaml
content-api-call-log-errors-policy.yaml
content-api-item-insert-errors-policy.yaml
count-deletes-query-errors-policy.yaml
count-upserts-query-errors-policy.yaml
create-task-empty-response-errors-policy.yaml
create-task-exception-errors-policy.yaml
delete-imported-filenames-errors-policy.yaml
deletes-threshold-errors-policy.yaml
eof-exists-during-import-errors-policy.yaml
eof-lock-check-errors-policy.yaml
eof-lock-failed-errors-policy.yaml
eof-locked-errors-policy.yaml
eof-unlock-failed-errors-policy.yaml
files-to-archive-retrieval-errors-policy.yaml
get-changes-count-query-errors-policy.yaml
import-function-timeout-errors-policy.yaml
import-storage-file-function-errors-policy.yaml
imported-files-retrieval-errors-policy.yaml
invalid-eof-errors-policy.yaml
nonexistent-table-errors-policy.yaml
schema-config-errors-calc-policy.yaml
schema-config-errors-load-policy.yaml
upserts-threshold-errors-policy.yaml
)
print_green "Recreating alerting policies..."
for POLICY in "${ALERT_POLICIES[@]}"
do
print_green "Recreating alerting policy $POLICY..."
POLICY_RESULT="$(gcloud alpha monitoring policies create --policy-from-file="stackdriver_alerts/$POLICY" 2>&1)"
sleep 2
POLICY_ID=$(echo "$POLICY_RESULT" | sed "s/.*\[\(.*\)\].*/\1/")
for EMAIL in $(echo "$ALERT_EMAILS" | sed "s/,/ /g")
do
NOTIFICATION_RESULT="$(gcloud alpha monitoring channels create --display-name="$EMAIL" --type=email --channel-labels="email_address=$EMAIL" --description="Shopping Alert Notification Channel" 2>&1)"
sleep 2
NOTIFICATION_CHANNEL=$(echo "$NOTIFICATION_RESULT" | sed "s/.*\[\(.*\)\].*/\1/")
gcloud alpha monitoring policies update "$POLICY_ID" --add-notification-channels="$NOTIFICATION_CHANNEL"
sleep 2
done
print_green "Alerting policy with notification channel created for $POLICY"
done
# Setup Service Accounts and grant permissions
print_green "Setting up service account permissions..."
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:"$GCP_PROJECT"@appspot.gserviceaccount.com \
--role roles/editor
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:"$GCP_PROJECT"@appspot.gserviceaccount.com \
--role roles/bigquery.admin
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member serviceAccount:"$GCP_PROJECT"@appspot.gserviceaccount.com \