forked from FNNDSC/ChRIS_ultron_backEnd
-
Notifications
You must be signed in to change notification settings - Fork 8
/
deploy.sh
executable file
·356 lines (334 loc) · 14.4 KB
/
deploy.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
#!/bin/bash
#
# NAME
#
# deploy.sh
#
# SYNPOSIS
#
# deploy.sh [-h]
# [-O <swarm|kubernetes>] \
# [-N <namespace>] \
# [-T <host|nfs>] \
# [-P <nfsServerIp>] \
# [-S <storeBase>] \
# [-D <storageBase>] \
# [up|down]
#
# DESC
#
# 'deploy.sh' script will depending on the argument deploy the ChRIS set
# of services in production or tear down the system.
#
# TYPICAL CASES:
#
# Deploy ChRIS services into a single-machine Swarm cluster:
#
# deploy.sh up
#
#
# Deploy ChRIS services into a single-machine Kubernetes cluster:
#
# deploy.sh -O kubernetes up
#
#
# Deploy ChRIS services into a multi-machine Kubernetes cluster:
#
# deploy.sh -O kubernetes -T nfs -P <nfsServerIp> -S <storeBase> -D <storageBase> up
#
# ARGS
#
#
# -h
#
# Optional print usage help.
#
# -i
#
# Optional initialize ChRIS with the chris user account and copy plugins. Useful
# to initialize ChRIS the first time the deployment is made.
#
# -O <swarm|kubernetes>
#
# Explicitly set the orchestrator. Default is swarm.
#
# -N <namespace>
#
# Explicitly set the kubernetes namespace to <namespace>. Default is chris.
# Not used for swarm.
#
# -T <host|nfs>
#
# Explicitly set the storage type for the STOREBASE dir and DBs. Default is host.
# Note: The nfs storage type is not implemented for swarm orchestrator yet.
#
# -P <nfsServerIp>
#
# Set the IP address of the NFS server. Required when storage type is set to 'nfs'.
# Not used for 'host' storage type.
#
# -S <storeBase>
#
# Explicitly set the STOREBASE dir to <storeBase>. This is the remote ChRIS
# filesystem where pfcon and plugins share data (usually externally mounted NFS).
# Must be specified or the shell environment variable STOREBASE must be set when
# using nfs storage type.
#
# -D <storageBase>
#
# Explicitly set the ChRIS persistent storage directory to <storageBase>. This
# is a directory containing a few subdirectories used to persist ChRIS services'
# databases. The subdirectories are 'chrisDB', 'storeDB', 'queueDB' and 'swiftDB'.
# Must be specified or the shell environment variable STORAGEBASE must be set when
# using nfs storage type. The directory tree must already exist on the NFS server.
#
# [up|down] (optional, default = 'up')
#
# Denotes whether to fire up or tear down the production set of services.
#
#
source ./decorate.sh
source ./cparse.sh
declare -i STEP=0
declare -i b_initializeChris=0
ORCHESTRATOR=swarm
NAMESPACE=chris
STORAGE_TYPE=host
HERE=$(pwd)
print_usage () {
echo "Usage: ./deploy.sh [-h] [-i] [-O <swarm|kubernetes>] [-N <namespace>] [-T <host|nfs>]
[-P <nfsServerIp>] [-S <storeBase>] [-D <storageBase>] [up|down]"
exit 1
}
while getopts ":hiO:N:T:P:S:D:" opt; do
case $opt in
h) print_usage
;;
i) b_initializeChris=1
;;
O) ORCHESTRATOR=$OPTARG
if ! [[ "$ORCHESTRATOR" =~ ^(swarm|kubernetes)$ ]]; then
echo "Invalid value for option -- O"
print_usage
fi
;;
N) NAMESPACE=$OPTARG
;;
T) STORAGE_TYPE=$OPTARG
if ! [[ "$STORAGE_TYPE" =~ ^(host|nfs)$ ]]; then
echo "Invalid value for option -- T"
print_usage
fi
;;
P) NFS_SERVER=$OPTARG
;;
S) STOREBASE=$OPTARG
;;
D) STORAGEBASE=$OPTARG
;;
\?) echo "Invalid option -- $OPTARG"
print_usage
;;
:) echo "Option requires an argument -- $OPTARG"
print_usage
;;
esac
done
shift $(($OPTIND - 1))
if [[ $STORAGE_TYPE == nfs ]]; then
if [[ $ORCHESTRATOR == swarm ]]; then
echo -e "Sorry, nfs storage type is not supported for swarm orchestrator yet" | ./boxes.sh
exit 1
fi
if [ -z ${NFS_SERVER+x} ]; then
echo "-P <NFS_SERVER> (the NFS server ip address) must be specified or the shell
environment variable NFS_SERVER must be set when using nfs storage type"
print_usage
fi
if [ -z ${STOREBASE+x} ]; then
echo "-S <storeBase> must be specified or the shell environment variable STOREBASE
must be set when using nfs storage type"
print_usage
fi
if [ -z ${STORAGEBASE+x} ]; then
echo "-D <storageBase> must be specified or the shell environment variable
STORAGEBASE must be set when using nfs storage type"
print_usage
fi
fi
COMMAND=up
if (( $# == 1 )) ; then
COMMAND=$1
if ! [[ "$COMMAND" =~ ^(up|down)$ ]]; then
echo "Invalid value $COMMAND"
print_usage
fi
fi
title -d 1 "Setting global exports..."
if [[ $STORAGE_TYPE == host ]]; then
if [ -z ${STOREBASE+x} ]; then
if [[ ! -d CHRIS_REMOTE_FS ]] ; then
mkdir CHRIS_REMOTE_FS
fi
STOREBASE=$HERE/CHRIS_REMOTE_FS
else
if [[ ! -d $STOREBASE ]] ; then
mkdir -p $STOREBASE
fi
fi
if [[ $ORCHESTRATOR == kubernetes ]]; then
if [ -z ${STORAGEBASE+x} ]; then
if [[ ! -d CHRIS_STORAGE_FS ]] ; then
mkdir -p CHRIS_STORAGE_FS/chrisDB
mkdir CHRIS_STORAGE_FS/storeDB
mkdir CHRIS_STORAGE_FS/queueDB
mkdir CHRIS_STORAGE_FS/swiftDB
fi
STORAGEBASE=$HERE/CHRIS_STORAGE_FS
else
if [[ ! -d $STORAGEBASE ]] ; then
mkdir -p $STORAGEBASE/chrisDB
mkdir $STORAGEBASE/storeDB
mkdir $STORAGEBASE/queueDB
mkdir $STORAGEBASE/swiftDB
fi
fi
fi
fi
echo -e "ORCHESTRATOR=$ORCHESTRATOR" | ./boxes.sh
echo -e "exporting STORAGE_TYPE=$STORAGE_TYPE" | ./boxes.sh
export STORAGE_TYPE=$STORAGE_TYPE
if [[ $STORAGE_TYPE == nfs ]]; then
echo -e "exporting NFS_SERVER=$NFS_SERVER" | ./boxes.sh
export NFS_SERVER=$NFS_SERVER
fi
echo -e "exporting STOREBASE=$STOREBASE" | ./boxes.sh
export STOREBASE=$STOREBASE
if [[ $ORCHESTRATOR == kubernetes ]]; then
echo -e "exporting NAMESPACE=$NAMESPACE" | ./boxes.sh
export NAMESPACE=$NAMESPACE
echo -e "exporting STORAGEBASE=$STORAGEBASE" | ./boxes.sh
export STORAGEBASE=$STORAGEBASE
fi
windowBottom
if [[ "$COMMAND" == 'up' ]]; then
title -d 1 "Starting ChRIS production deployment on $ORCHESTRATOR"
if [[ $ORCHESTRATOR == swarm ]]; then
echo "docker stack deploy -c swarm/prod/docker-compose.yml chris_stack" | ./boxes.sh ${LightCyan}
docker stack deploy -c swarm/prod/docker-compose.yml chris_stack
elif [[ $ORCHESTRATOR == kubernetes ]]; then
echo "kubectl create namespace $NAMESPACE" | ./boxes.sh ${LightCyan}
namespace=$(kubectl get namespaces $NAMESPACE --no-headers -o custom-columns=:metadata.name 2> /dev/null)
if [ -z "$namespace" ]; then
kubectl create namespace $NAMESPACE
else
echo "$NAMESPACE namespace already exists, skipping creation"
fi
if [[ $STORAGE_TYPE == host ]]; then
echo "kubectl kustomize kubernetes/prod/overlays/host | envsubst | kubectl apply -f -" | ./boxes.sh ${LightCyan}
kubectl kustomize kubernetes/prod/overlays/host | envsubst | kubectl apply -f -
else
echo "kubectl kustomize kubernetes/prod/overlays/nfs | envsubst | kubectl apply -f -" | ./boxes.sh ${LightCyan}
kubectl kustomize kubernetes/prod/overlays/nfs | envsubst | kubectl apply -f -
fi
fi
windowBottom
if (( b_initializeChris )); then
title -d 1 "Waiting for ChRIS containers to start running on $ORCHESTRATOR"
echo "This might take a few minutes... please be patient." | ./boxes.sh ${Yellow}
for i in {1..50}; do
sleep 5
if [[ $ORCHESTRATOR == swarm ]]; then
chris=$(docker ps -f name=chris.1. -q)
elif [[ $ORCHESTRATOR == kubernetes ]]; then
chris=$(kubectl get pods -n $NAMESPACE --selector="app=chris,env=production" --field-selector=status.phase=Running --output=jsonpath='{.items[*].metadata.name}')
fi
if [ -n "$chris" ]; then
echo "" | ./boxes.sh
echo "Success: chris container is running on $ORCHESTRATOR" | ./boxes.sh ${Green}
echo "" | ./boxes.sh
break
fi
done
if [ -z "$chris" ]; then
echo "" | ./boxes.sh
echo "Error: couldn't start chris container on $ORCHESTRATOR" | ./boxes.sh ${Red}
echo "" | ./boxes.sh
exit 1
fi
windowBottom
title -d 1 "Waiting until CUBE is ready to accept connections..."
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec $chris sh -c 'while ! curl -sSf http://localhost:8000/api/v1/users/ 2> /dev/null; do sleep 5; done;'
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec $chris -c chris -n $NAMESPACE -- sh -c 'while ! curl -sSf http://localhost:8000/api/v1/users/ 2> /dev/null; do sleep 5; done;'
fi
windowBottom
title -d 1 "Creating superuser chris in ChRIS store"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec -it $chris_store sh -c 'python manage.py createsuperuser --username chris --email [email protected]'
elif [[ $ORCHESTRATOR == kubernetes ]]; then
chris_store=$(kubectl get pods -n $NAMESPACE --selector="app=chris-store,env=production" --output=jsonpath='{.items[*].metadata.name}')
kubectl exec -it $chris_store -n $NAMESPACE -- sh -c 'python manage.py createsuperuser --username chris --email [email protected]'
fi
windowBottom
title -d 1 "Creating superuser chris in CUBE"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec -it $chris sh -c 'python manage.py createsuperuser --username chris --email [email protected]'
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec -it $chris -c chris -n $NAMESPACE -- sh -c 'python manage.py createsuperuser --username chris --email [email protected]'
fi
windowBottom
title -d 1 "Uploading the plugin fnndsc/pl-dircopy"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec $chris_store python plugins/services/manager.py add pl-dircopy chris https://github.com/FNNDSC/pl-dircopy fnndsc/pl-dircopy --descriptorstring "$(docker run --rm fnndsc/pl-dircopy dircopy --json 2> /dev/null)"
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec $chris_store -n $NAMESPACE -- python plugins/services/manager.py add pl-dircopy chris https://github.com/FNNDSC/pl-dircopy fnndsc/pl-dircopy --descriptorstring "$(docker run --rm fnndsc/pl-dircopy dircopy --json 2> /dev/null)"
fi
windowBottom
title -d 1 "Uploading the plugin fnndsc/pl-topologicalcopy"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec $chris_store python plugins/services/manager.py add pl-topologicalcopy chris https://github.com/FNNDSC/pl-topologicalcopy fnndsc/pl-topologicalcopy --descriptorstring "$(docker run --rm fnndsc/pl-topologicalcopy topologicalcopy --json 2> /dev/null)"
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec $chris_store -n $NAMESPACE -- python plugins/services/manager.py add pl-topologicalcopy chris https://github.com/FNNDSC/pl-topologicalcopy fnndsc/pl-topologicalcopy --descriptorstring "$(docker run --rm fnndsc/pl-topologicalcopy topologicalcopy --json 2> /dev/null)"
fi
windowBottom
title -d 1 "Adding host compute environment"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec $chris python plugins/services/manager.py add host "http://pfcon.remote:5005/api/v1/" --description "Remote compute"
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec $chris -c chris -n $NAMESPACE -- python plugins/services/manager.py add host "http://pfcon.${NAMESPACE}:5005/api/v1/" --description "Remote compute"
fi
windowBottom
title -d 1 "Registering pl-dircopy from store to CUBE"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec $chris python plugins/services/manager.py register host --pluginname pl-dircopy
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec $chris -c chris -n $NAMESPACE -- python plugins/services/manager.py register host --pluginname pl-dircopy
fi
windowBottom
title -d 1 "Registering pl-topologicalcopy from store to CUBE"
if [[ $ORCHESTRATOR == swarm ]]; then
docker exec $chris python plugins/services/manager.py register host --pluginname pl-topologicalcopy
elif [[ $ORCHESTRATOR == kubernetes ]]; then
kubectl exec $chris -c chris -n $NAMESPACE -- python plugins/services/manager.py register host --pluginname pl-topologicalcopy
fi
windowBottom
fi
fi
if [[ "$COMMAND" == 'down' ]]; then
title -d 1 "Destroying ChRIS production deployment on $ORCHESTRATOR"
if [[ $ORCHESTRATOR == swarm ]]; then
echo "docker stack rm chris_stack" | ./boxes.sh ${LightCyan}
docker stack rm chris_stack
elif [[ $ORCHESTRATOR == kubernetes ]]; then
if [[ $STORAGE_TYPE == host ]]; then
echo "kubectl kustomize kubernetes/prod/overlays/host | envsubst | kubectl delete -f -" | ./boxes.sh ${LightCyan}
kubectl kustomize kubernetes/prod/overlays/host | envsubst | kubectl delete -f -
else
echo "kubectl kustomize kubernetes/prod/overlays/nfs | envsubst | kubectl delete -f -" | ./boxes.sh ${LightCyan}
kubectl kustomize kubernetes/prod/overlays/nfs | envsubst | kubectl delete -f -
fi
fi
windowBottom
fi