-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·177 lines (151 loc) · 4.45 KB
/
main.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
#!/bin/bash
### Environment variables
INSTANCES="${INSTANCES}" # Comma-separated list of Cloud SQL instances
PROJECT_ID="${PROJECT_ID}"
REGION="${REGION:-asia-northeast1}"
START_SCHEDULE="${START_SCHEDULE:-0 9 * * 1-5}" # 9:00 AM, Monday to Friday
STOP_SCHEDULE="${STOP_SCHEDULE:-0 18 * * 1-5}" # 6:00 PM, Monday to Friday
TIMEZONE="${TIMEZONE:-Asia/Tokyo}"
### Usage
usage() {
echo "Usage: $0 [deploy|delete]"
echo " deploy: Deploy Cloud Pub/Sub, Cloud Function, and Cloud Scheduler."
echo " delete: Delete Cloud Pub/Sub, Cloud Function, and Cloud Scheduler."
exit 1
}
### Validate the required environment variables
validate_envs() {
if [ -z "${PROJECT_ID}" ]; then
echo "PROJECT_ID is not set."
exit 1
fi
if [ -z "${INSTANCES}" ]; then
echo "INSTANCES is not set."
exit 1
fi
local regex="^[a-zA-Z0-9_-]+(,[a-zA-Z0-9_-]+)*$"
if [[ ! "${INSTANCES}" =~ ${regex} ]]; then
echo "Invalid INSTANCES format: ${INSTANCES}"
exit 1
fi
}
### Deploy Cloud Pub/Sub
deploy_pubsub() {
echo "Creating Pub/Sub topic, 'cloud-sql-scheduler'..."
gcloud pubsub topics create "cloud-sql-scheduler" \
--project="${PROJECT_ID}" \
|| echo "Failed to create Pub/Sub topic, 'cloud-sql-scheduler'."
}
### Delete Cloud Pub/Sub
delete_pubsub() {
echo "Deleting Pub/Sub topic, 'cloud-sql-scheduler'..."
gcloud pubsub topics delete "cloud-sql-scheduler" \
--project="${PROJECT_ID}" \
|| echo "Failed to delete Pub/Sub topic, 'cloud-sql-scheduler'."
}
### Deploy Cloud Function
deploy_function() {
echo "Creating a Cloud Function, 'cloud-sql-scheduler'..."
gcloud functions deploy "cloud-sql-scheduler" \
--runtime="go122" \
--trigger-topic="cloud-sql-scheduler" \
--entry-point="ProcessPubSub" \
--project="${PROJECT_ID}" \
--region="${REGION}" \
--set-env-vars="TIMEZONE=${TIMEZONE}" \
--gen2 \
|| echo "Failed to create Cloud Function, 'cloud-sql-scheduler'."
}
### Delete Cloud Function
delete_function() {
echo "Deleting Cloud Function, 'cloud-sql-scheduler'..."
gcloud functions delete "cloud-sql-scheduler" \
--project="${PROJECT_ID}" \
--region="${REGION}" \
--quiet \
|| echo "Failed to delete Cloud Function, 'cloud-sql-scheduler'."
}
### Deploy Cloud Scheduler
deploy_scheduler() {
local action="$1"
local schedule=""
if [ "${action}" == "start" ]; then
schedule="${START_SCHEDULE}"
elif [ "${action}" == "stop" ]; then
schedule="${STOP_SCHEDULE}"
fi
echo "Creating a Cloud Scheduler job, '${action}-cloud-sql'..."
gcloud scheduler jobs create pubsub "${action}-cloud-sql" --location="${REGION}"\
--schedule="${schedule}" \
--time-zone="${TIMEZONE}" \
--topic="cloud-sql-scheduler" \
--location="${REGION}" \
--message-body="{
\"Action\":\"${action}\",
\"Instance\":\"${INSTANCES}\",
\"Project\":\"${PROJECT_ID}\"
}" \
--project="${PROJECT_ID}" \
|| echo "Failed to create Cloud Scheduler job, '${action}-cloud-sql'."
}
### Delete Cloud Scheduler
delete_scheduler() {
local action="$1"
echo "Deleting Cloud Scheduler job, '${action}-cloud-sql'..."
gcloud scheduler jobs delete "${action}-cloud-sql" \
--project="${PROJECT_ID}" \
--location="${REGION}" \
--quiet \
|| echo "Failed to delete Cloud Scheduler job, '${action}-cloud-sql'."
}
### Check if the Cloud SQL Admin API is enabled
check_api() {
local api="sqladmin.googleapis.com"
local status
status=$(gcloud services list \
--project="${PROJECT_ID}" \
--enabled \
--format="value(config.name)" \
--filter="config.name:${api}")
echo "Checking if the Cloud SQL Admin API is enabled..."
if [ "${status}" != "${api}" ]; then
echo "Enabling the Cloud SQL Admin API..."
gcloud services enable "${api}" \
--project="${PROJECT_ID}" \
|| echo "Failed to enable the Cloud SQL Admin API."
else
echo "The Cloud SQL Admin API is already enabled."
fi
}
### Deploy Instances
deploy() {
check_api
deploy_pubsub
deploy_function
deploy_scheduler "start"
deploy_scheduler "stop"
}
### Delete Instances
delete() {
delete_scheduler "start"
delete_scheduler "stop"
delete_function
delete_pubsub
}
### Main
# Check if there are any arguments
if [ "$#" -ne 1 ]; then
usage
fi
# Validate the required environment variables
validate_envs
# Check if the action is valid
if [ "$1" == "deploy" ]; then
deploy
elif [ "$1" == "delete" ]; then
delete
else
echo "Invalid action: $1"
usage
exit 1
fi