-
Notifications
You must be signed in to change notification settings - Fork 9
/
deploy-gke.bash
executable file
·227 lines (192 loc) · 7.54 KB
/
deploy-gke.bash
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
#!/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
#
# 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.
# TODO: initial steps
GREEN="\e[32m"
RED="\e[31m"
RESET="\e[0m"
PROJECT=${PROJECT}
# Service names have "_" prefix and suffix to allow full service name matching
# using a substring search on the merged list.
SERVICE_NAMES=("_ic_" "_hydraic_" "_icdemo_")
ENV=""
unset PROMPT
unset BYPASS_BUILD
unset CONFIG_ONLY
unset DB_IP
unset DOMAIN_IC
print_usage() {
echo -e ${RED?}'Usage: deploy [-c] [-b] [-f] [-h] [-i] [-p project_id] [-P database_ip] [-d domain_for_ic] [service_name service_name ...]'${RESET?}
echo -e ${RED?}' -b \t bypass build of services'${RESET?}
echo -e ${RED?}' -c \t config generation only'${RESET?}
echo -e ${RED?}' -h \t show this help usage'${RESET?}
echo -e ${RED?}' -i \t interactive prompts to proceed between steps'${RESET?}
echo -e ${RED?}' -p \t GCP project_id to deploy to'${RESET?}
echo -e ${RED?}' -P \t CloudSQL instance private ip'${RESET?}
echo -e ${RED?}' -d \t domain for ic'${RESET?}
echo
echo -e ${RED?}' service names: '"${SERVICE_NAMES[@]//_}"${RESET?}
echo
echo -e ${RED?}' all flags must be provided before service names'${RESET?}
}
echo -e ${GREEN?}'Args: '"$@"${RESET?}
ARGS=("$@")
DEPLOY=()
unset SKIP_ARG
for arg in "${ARGS[@]}"; do
if [[ "${SKIP_ARG}" != "" ]]; then
# previous was a flag and this is that flag's string argument
unset SKIP_ARG
elif [[ $arg == -* ]]; then
# arg is a flag
if [[ "${#DEPLOY[@]}" != "0" ]]; then
echo -e ${RED?}'Flag "'$arg'" must be specified before any service names'${RESET?}
exit 1
elif [[ "$arg" == "-p" || "$arg" == "-P" || "$arg" == "-d" ]]; then
SKIP_ARG='true'
fi
elif [[ "${SERVICE_NAMES[@]}" =~ '_'$arg'_' ]]; then
DEPLOY+=('_'$arg'_')
else
echo -e ${RED?}'Service "'$arg'" is not a valid service name'${RESET?}
exit 1
fi
done
if [[ "${#DEPLOY[@]}" == "0" ]]; then
# When no services are specified to deploy, then deploy all of them.
DEPLOY=("${SERVICE_NAMES[@]}")
fi
while getopts ':bchip:P:d:' flag; do
case "${flag}" in
b) BYPASS_BUILD='true' ;;
c) CONFIG_ONLY='true' ;;
h) print_usage
exit 1 ;;
i) PROMPT='true' ;;
p) PROJECT="${OPTARG}" ;;
P) DB_IP="${OPTARG}" ;;
d) DOMAIN_IC="${OPTARG}" ;;
*) echo -e ${RED?}'Unknown flag: -'${flag}${RESET?}
print_usage
exit 1 ;;
esac
done
if [[ "${PROJECT}" == "" ]]; then
echo -e ${RED?}'Must provide a project via $PROJECT or -p project'${RESET?}
print_usage
exit 1
fi
if [[ "${DB_IP}" == "" ]]; then
echo -e ${RED?}'Must provide the private ip of database via -P ip'${RESET?}
print_usage
exit 1
fi
if [[ "${DOMAIN_IC}" == "" ]]; then
echo -e ${RED?}'Must provide the domain of ic via -d your.domain.ic'${RESET?}
print_usage
exit 1
fi
deploy_service() {
if [[ "${DEPLOY[@]}" =~ "_$1_" && -z $2 ]]; then
return
fi
echo -e ${GREEN?}'SKIP service "'$1'"'${RESET?}
false
}
# You need to loging to gcloud and createa a project using gcloud cli.
# Export the id of your project to environment variable PROJECT.
echo -e ${GREEN?}'Starting deployment to project: '${PROJECT?}${RESET?}
gcloud config set project ${PROJECT?}
# Generate the config files
if [[ "${IC_CONFIG}" == "" ]]; then
echo -e ${GREEN?}'Generating the default IC config files.'${RESET?}
echo -e ${GREEN?}'To use your own configs instead, set environment variable "IC_CONFIG" to point to your config folders.'${RESET?}
else
echo -e ${GREEN?}'Using the provided IC_CONFIG files at: '${IC_CONFIG?}${RESET?}
fi
if [[ "$PROMPT" != "" ]]; then
echo -e ${GREEN?}'PRESS ENTER TO CONTINUE...'${RESET?}
read
fi
# Create IC and DAM config directories if they do not exist.
mkdir -p ./deploy/config/ic/
if [[ "${IC_CONFIG}" == "" ]]; then
cp -R ./deploy/config/ic-gke-template/* ./deploy/config/ic/
else
cp -R $IC_CONFIG/* ./deploy/config/ic/
fi
sed -i 's/${DOMAIN_FOR_IC}/'${DOMAIN_IC?}'/g' ./deploy/config/ic/config_master_main_latest.json
sed -i 's/${DOMAIN_FOR_IC}/'${DOMAIN_IC?}'/g' ./deploy/config/ic/secrets_master_main_latest.json
mkdir -p ./deploy/build-gke/
cp -R ./deploy/build-gke-templates/* ./deploy/build-gke/
sed -i 's/${YOUR_PROJECT_ID}/'${PROJECT?}'/g' ./deploy/build-gke/hydra-ic/k8s.yaml
sed -i 's/${YOUR_PROJECT_ID}/'${PROJECT?}'/g' ./deploy/build-gke/ic/k8s.yaml
sed -i 's/${YOUR_PROJECT_ID}/'${PROJECT?}'/g' ./deploy/build-gke/icdemo/k8s.yaml
sed -i 's/${PRIVATE_IP_OF_CLOUDSQL}/'${DB_IP?}'/g' ./deploy/build-gke/hydra-ic/k8s.yaml
sed -i 's/${PRIVATE_IP_OF_CLOUDSQL}/'${DB_IP?}'/g' ./deploy/build-gke/ic/k8s.yaml
sed -i 's/${PRIVATE_IP_OF_CLOUDSQL}/'${DB_IP?}'/g' ./deploy/build-gke/icdemo/k8s.yaml
sed -i 's/${DOMAIN_FOR_IC}/'${DOMAIN_IC?}'/g' ./deploy/build-gke/hydra-ic/k8s.yaml
sed -i 's/${DOMAIN_FOR_IC}/'${DOMAIN_IC?}'/g' ./deploy/build-gke/ic/k8s.yaml
sed -i 's/${DOMAIN_FOR_IC}/'${DOMAIN_IC?}'/g' ./deploy/build-gke/icdemo/k8s.yaml
sed -i 's/${DOMAIN_FOR_IC}/'${DOMAIN_IC?}'/g' ./deploy/build-gke/ingress.yaml
sed -i 's/build-gke-templates/build-gke/g' ./deploy/build-gke/ic/Dockerfile
sed -i 's/build-gke-templates/build-gke/g' ./deploy/build-gke/ic/cloudbuild.yaml
sed -i 's/build-gke-templates/build-gke/g' ./deploy/build-gke/hydra-ic/Dockerfile
sed -i 's/build-gke-templates/build-gke/g' ./deploy/build-gke/hydra-ic/cloudbuild.yaml
if [[ "${CONFIG_ONLY}" != "" ]]; then
echo -e ${GREEN?}'CONFIG_ONLY flag is set. Skipping all other steps.'${RESET?}
exit 0
fi
# Build the Hydra, IC, ICDEMO images
if [[ "${BYPASS_BUILD}" == "" ]]; then
echo -e ${GREEN?}'Building Docker images for the services.'${RESET?}
if [[ "$PROMPT" != "" ]]; then
echo -e ${GREEN?}'PRESS ENTER TO CONTINUE...'${RESET?}
read
fi
if deploy_service "hydraic" $BYPASS_BUILD; then
echo -e ${GREEN?}'Building Hydra IC Docker Image.'${RESET?}
gcloud builds submit --project=${PROJECT?} --config deploy/build-gke/hydra-ic/cloudbuild.yaml .
fi
if deploy_service "ic" $BYPASS_BUILD; then
echo -e ${GREEN?}'Building IC Docker Image.'${RESET?}
gcloud builds submit --project=${PROJECT?} --config deploy/build-gke/ic/cloudbuild.yaml .
fi
if deploy_service "icdemo" $BYPASS_BUILD; then
echo -e ${GREEN?}'Building ICDEMO Docker Image.'${RESET?}
gcloud builds submit --project=${PROJECT?} --config deploy/build-gke/icdemo/cloudbuild.yaml .
fi
else
echo -e ${GREEN?}'BYPASS_BUILD flag is set. Bypassing the building of Docker images for the services.'${RESET?}
fi
# Deploy Services
echo -e ${GREEN?}'Deploying services to the GCP project.'${RESET?}
if [[ "${PROMPT}" != "" ]]; then
echo -e ${GREEN?}'PRESS ENTER TO CONTINUE...'${RESET?}
read
fi
if deploy_service "hydraic"; then
echo -e ${GREEN?}'Deploy Hydra IC.'${RESET?}
kubectl apply -f deploy/build-gke/hydra-ic/k8s.yaml
fi
if deploy_service "ic"; then
echo -e ${GREEN?}'Deploy IC.'${RESET?}
kubectl apply -f deploy/build-gke/ic/k8s.yaml
fi
if deploy_service "icdemo"; then
echo -e ${GREEN?}'Deploy ICDEMO.'${RESET?}
kubectl apply -f deploy/build-gke/icdemo/k8s.yaml
fi
kubectl apply -f deploy/build-gke/ingress.yaml
echo -e ${GREEN?}'=== DEPLOY COMPLETE, But it may need 10 min to effect ==='${RESET?}