-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgcp_notification_func.sh
executable file
·98 lines (81 loc) · 3.18 KB
/
gcp_notification_func.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
################################################################################
############# GCP Notification Function ##############
################################################################################
#!/bin/bash
#Help Funtion showing script usage
Help()
{
echo "Bash Script for utilising GCP notification functionality"
echo ""
echo "Basic Usage, using default parameters: ./gcp_notification_func "
echo "Usage: ./gcp_notification_func [--b|--e|--t|--tr|-g|--bu|--sT|--mT|--h]"
echo ""
echo "Options:"
echo "$1 GCP Storage Bucket name"
echo "$2 GCP PubSub Topic name"
echo "$3 GCP PubSub Subscription name"
echo "$4 Path to source directory for notification function"
echo "$5 Destination email that will be notified"
echo "$6 Less secure source email where results will be sent from"
echo "$7 Password for SMTP client for source email"
echo "$8 GCP Cloud Function Name"
echo "$9 GCP Cloud Function runtime version"
echo ""
exit
}
#import environment variables from secrets script that stores sensitive GCP values
source config/secrets.sh
#############################################################
##### secrets.sh ####
#!/usr/bin/env bash
# export PROJECT_ID=""
# export BUCKET=""
# export JOB_DIR=""
# export REGION=""
# export TOPIC=""
# export SUBSCRIPTION=""
# export SOURCE_DIR=""
# export TOMAIL=""
# export FROMMAIL=""
# export EMAIL_PASS=""
#############################################################
### Only need to run this script once before calling gcp_training script ###
echo ""
echo "Notification Function Arguments"
echo "#########################"
echo 'Function Name: '$FUNCTION_NAME
echo 'BUCKET NAME: ' $BUCKET
echo 'TOPIC: ' $TOPIC
echo 'SUBSCRIPTION: ' $SUBSCRIPTION
echo 'SOURCE_DIR: ' $SOURCE_DIR
echo 'TOMAIL: ' $TOMAIL
echo 'FROMMAIL: ' $FROMMAIL
echo 'EMAIL_PASS: ' $EMAIL_PASS
echo "#########################"
echo ""
#update any gcloud components
#yes | gcloud components update
#listing existing pubsub topics
gcloud pubsub topics list
#delete topic if already exists
gcloud pubsub topics delete $TOPIC #* may get error if topic doesn't exist but will not halt execution
#create pubsub topic
gcloud pubsub topics create $TOPIC
#delete pubsub subscription if already exists
gcloud pubsub subscriptions delete $SUBSCRIPTION #* may get error if subscription doesn't exist but will not halt execution
#create subscription to topic
gcloud pubsub subscriptions create $SUBSCRIPTION \
--topic=$TOPIC
#list bucket notifications
gsutil notification list $BUCKET_NAME
#delete bucket notifications
gsutil notification delete $BUCKET_NAME #* may get error if notification doesn't exist but will not halt execution
#delete gcloud function if exists (need to enable Cloud Build API)
gcloud functions delete $FUNCTION_NAME #* may get error if function doesn't exist but will not halt execution
#deploy gcloud function
gcloud functions deploy $FUNCTION_NAME \
--source $SOURCE_DIR \
--runtime $RUNTIME_VERSION \
--trigger-topic $TOPIC \
--set-env-vars BUCKET=$BUCKET_NAME,EMAIL_USER=$TOMAIL,FROM_MAIL=$FROMMAIL,EMAIL_PASS=$EMAIL_PASS \
--allow-unauthenticated