-
Notifications
You must be signed in to change notification settings - Fork 36
/
cbf2.sh
executable file
·539 lines (398 loc) · 12.9 KB
/
cbf2.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
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
#!/bin/bash
###############################################################################
#
# Welcome to CBF 2.0.
#
# The goal of the project is to make my life easier when it comes to managing
# Pentaho projects' lifecycle.
#
# Author: Pedro Alves
# License: Whatever... Apache2 if I have to pick one
#
###############################################################################
VERSION=1.1
# Lists the clients and starts / delets
BASEDIR=$(dirname $0)
source "$BASEDIR/utils.sh"
cd $BASEDIR
# We're going to do a few things here;
# 1. Show the core images
# 2. Show the core container based on the previous images
# 3. Show the project images
# 4. Show the projects based on the previous images
#
# Each selection will have it's own option; On top of that we'll have the
# following:
# 1. Add new core images
# 2. Create new project
DOCKER_CONTAINERS=()
DOCKER_STATUS=()
DOCKER_RESTARTPOLICY=()
DOCKER_IDS=()
DOCKER_IMAGES=()
# Get list of files
echo
echo "--------------------------------------------------------------"
echo "--------------------------------------------------------------"
echo "------ CBF2 - Community Build Framework 2 -------"
echo "------ Version: $VERSION -------"
echo "------ Author: Pedro Alves ([email protected]) -------"
echo "--------------------------------------------------------------"
echo "--------------------------------------------------------------"
echo
echo Core Images available:
echo ----------------------
echo
# 1. Search for what we have
IMAGES=$( docker images | egrep '^baserver' | cut -d' ' -f 1 )
IFS=$'\n';
n=-1
for image in $IMAGES
do
((n++))
echo " [$n] $image"
BUILD[$n]=$image
TYPE[$n]="IMAGE"
done;
echo
echo Core containers available:
echo --------------------------
echo
RUNNING_CONTAINERS=$( docker ps -a -f "name=baserver" --format "{{.ID}}XX{{.Names}}XX{{.Status}}XX{{.Image}}" | grep -v 'pdu-' )
for container in $RUNNING_CONTAINERS
do
((n++))
IFS='XX' read -a ENTRY <<< "$container"
if [[ ${ENTRY[4]} =~ "Up " ]]; then
DOCKER_STATUS[$n]="Running"
else
DOCKER_STATUS[$n]="Stopped"
fi
DOCKER_RESTARTPOLICY[$n]="Restart: "$( docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}" ${ENTRY[2]})
DOCKER_CONTAINERS[$n]=${ENTRY[2]}
DOCKER_IDS[$n]=${ENTRY[0]}
DOCKER_IMAGES[$n]=${ENTRY[6]}
TYPE[$n]="CONTAINER"
echo " [$n] (${DOCKER_STATUS[$n]}): ${ENTRY[2]} (${DOCKER_RESTARTPOLICY[$n]}) "
done
echo
echo Project images available:
echo -------------------------
echo
# 1. Search for what we have
IMAGES=$( docker images | egrep 'pdu-' | cut -d' ' -f 1 )
IFS=$'\n';
for image in $IMAGES
do
((n++))
echo " [$n] $image"
BUILD[$n]=$image
TYPE[$n]="IMAGE"
done;
echo
echo Project containers available:
echo -----------------------------
echo
RUNNING_CONTAINERS=$( docker ps -a -f "name=pdu" --format "{{.ID}}XX{{.Names}}XX{{.Status}}XX{{.Image}}" )
for container in $RUNNING_CONTAINERS
do
((n++))
IFS='XX' read -a ENTRY <<< "$container"
if [[ ${ENTRY[4]} =~ "Up " ]]; then
DOCKER_STATUS[$n]="Running"
else
DOCKER_STATUS[$n]="Stopped"
fi
DOCKER_RESTARTPOLICY[$n]="Restart: "$( docker inspect -f "{{ .HostConfig.RestartPolicy.Name }}" ${ENTRY[2]})
DOCKER_CONTAINERS[$n]=${ENTRY[2]}
DOCKER_IDS[$n]=${ENTRY[0]}
DOCKER_IMAGES[$n]=${ENTRY[6]}
TYPE[$n]="CONTAINER"
echo " [$n] (${DOCKER_STATUS[$n]}): ${ENTRY[2]} (${DOCKER_RESTARTPOLICY[$n]}) "
done
echo
read -e -p "> Select an entry number, [A] to add new image or [C] to create new project or [N] to create a new project structure: " choice
choice=$( tr '[:lower:]' '[:upper:]' <<< "$choice" )
if [ -z $choice ]
then
echo You have to make a selection
exit 1
fi
# Add a new image
if [ $choice == "A" ]; then
source "$BASEDIR/softwareToCoreImages.sh"
fi
# Create project
if [ $choice == "C" ]; then
source "$BASEDIR/buildProjectImages.sh"
fi
# New project structure
if [ $choice == "N" ]; then
source "$BASEDIR/buildProjectStructure.sh"
fi
if ! [ "$choice" -eq "$choice" ] || [ "$choice" -lt 0 ] || [ "$choice" -gt "$n" ] 2>/dev/null
then
echo Invalid choice: $choice
exit 1;
else
if [ ${TYPE[$choice]} == "IMAGE" ]
then
# Action over the images
build=${BUILD[$choice]}
echo
echo You selected the image $build
echo
read -e -p "> What do you want to do? (L)aunch a new container, (D)elete the image or (I)nspect before launch)? [L]: " operation
operation=${operation:-L}
operation=$( tr '[:lower:]' '[:upper:]' <<< "$operation" )
if ! [ $operation == "L" ] && ! [ $operation == "D" ] && ! [ $operation == "I" ]
then
echo Invalid selection
exit 1;
fi
# Are we deleting it?
if [ $operation == "D" ]
then
docker rmi $build
echo Removed successfully
exit 0
fi
# Are we launching it?
if [ $operation == "L" ]
then
read -e -p "Do you want to start the image $build in debug mode? [y/N]: " -n 1 DEBUG
DEBUG=${DEBUG:-"n"}
# Check for docker volumes
projectName=$(echo $build | sed -E -e 's/pdu-(.*)-baserver.*/\1/ ' )
if [ $projectName ] && [ -f $BASEDIR/projects/$projectName/config/setPorts.sh ]
then
source "$BASEDIR/projects/$projectName/config/setPorts.sh"
fi
source "$BASEDIR/setPorts.sh"
if ! [ -z $projectName ] && [ -f $BASEDIR/projects/$projectName/config/dockerVolumes.sh ]
then
source "$BASEDIR/projects/$projectName/config/dockerVolumes.sh"
volumeList=""
for volume in "${VOLUMES[@]}" ; do
pathHost=${volume%%:*}
pathContainer=${volume#*:}
volumeList+=" -v $pathHost:$pathContainer"
done
fi
# Allow to specify a network for docker
DOCKER_NETWORK_OPT=""
if [ ! -z ${CBF2_DOCKER_NETWORK+x} ]
then
DOCKER_NETWORK_OPT="--net=${CBF2_DOCKER_NETWORK}"
fi
if [ $DEBUG == "y" ] || [ $DEBUG == "Y" ]
then
eval "docker run $exposePorts $DOCKER_NETWORK_OPT -p $debugPort:8044 --name $build-debug --hostname $build-debug -e DEBUG=true $volumeList $build"
else
eval "docker run $exposePorts $DOCKER_NETWORK_OPT --name $build --hostname $build $volumeList $build"
fi
fi
# Are we inspecting it? it?
if [ $operation == "I" ]
then
docker run --entrypoint bash -i -t --rm $build
exit 0
fi
else
# Action over the containers
dockerId=${DOCKER_IMAGES[$choice]}
dockerImage=${DOCKER_CONTAINERS[$choice]}
echo
echo "You selected the container $dockerImage"
echo
echo
# Now, different options depending on the status
if [[ ${DOCKER_STATUS[$choice]} == "Running" ]]
then
echo "The container is running; Possible operations:"
echo
echo " S: Stop it"
echo " R: Restart it"
echo " A: Attach to it"
echo " L: See the Logs"
echo " P: See exposed ports"
if [[ $dockerImage =~ ^pdu ]]; then
echo " E: Export the solution"
echo " I: Import the solution"
fi
echo " F: Flag restart to it"
echo
read -e -p "What do you want to do? [A]: " operation
operation=${operation:-A}
operation=$( tr '[:lower:]' '[:upper:]' <<< "$operation" )
if ! [ $operation == "S" ] && ! [ $operation == "R" ] && ! [ $operation == "A" ] && ! [ $operation == "E" ] && ! [ $operation == "I" ] && ! [ $operation == "L" ] && ! [ $operation == "P" ] && ! [ $operation == "F" ]
then
echo Invalid selection
exit 1;
fi
if [ $operation == "S" ]; then
echo Stopping...
docker stop -t 10 $dockerImage
echo $dockerImage stopped successfully
exit 0
fi
if [ $operation == "R" ]; then
echo Restarting...
docker restart $dockerImage
echo $dockerImage restarted successfully
exit 0
fi
if [ $operation == "A" ]; then
docker exec -it $dockerImage bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash"
echo Done
exit 0
fi
if [ $operation == "L" ]; then
docker logs --tail 500 -f $dockerImage
echo Done
exit 0
fi
if [ $operation == "P" ]; then
echo "[Container] -> [Host]"
docker port $dockerImage
echo Done
exit 0
fi
if [ $operation == "E" ]; then
read -e -p "Username for the export operation? [admin]: " user
user=${user:-admin}
read -e -p "Password for the export operation? [password]: " password
password=${password:-password}
project=$(echo $dockerImage | sed -E -e 's/pdu-(.*)-baserver.*/\1/ ' )
serverDir=ee;
if [[ $dockerImage =~ ce ]]; then
serverDir=ce;
fi
echo "Exporting $dockerImage to project $project."
echo "Please note that by design CBF2 only exports the folders in public that are already part of the project. You'll need to manually create the directory if you add a top level one."
echo
pushd projects/$project/solution/public/ > /dev/null
DIRS=$( ls -d -1 */ )
IFS=$'\n';
for dir in $DIRS
do
dir=$( echo $dir | sed -E -e 's/\/$//')
echo Exporting public/$dir...
docker exec $dockerImage bash -c "cd /pentaho/*server* ; ./import-export.sh --export -a http://127.0.0.1:8080/pentaho -u $user -p $password -w true -fp /pentaho/export.zip -f /public/$dir"
docker cp $dockerImage:/pentaho/export.zip .
unzip -o export.zip > /dev/null
rm export.zip
done;
echo
popd > /dev/null
fi
if [ $operation == "I" ]; then
read -e -p "Username for the import operation? [admin]: " user
user=${user:-admin}
read -e -p "Password for the import operation? [password]: " password
password=${password:-password}
project=$(echo $dockerImage | sed -E -e 's/pdu-(.*)-baserver.*/\1/ ' )
serverDir=ee;
if [[ $dockerImage =~ ce ]]; then
serverDir=ce;
fi
echo "Importing project $project to $dockerImage..."
# Zipping the files
pushd projects/$project/solution/ > /dev/null
zip -r import.zip public -x "*.DS_Store"
# Sending it and importing
docker cp import.zip $dockerImage:/pentaho/
docker exec $dockerImage bash -c "cd /pentaho/*server* ; ./import-export.sh --import -a http://127.0.0.1:8080/pentaho -u $user -p $password -fp /pentaho/import.zip -r true --permission=true -f / -c UTF-8 -o true > /dev/null "
rm import.zip
popd > /dev/null
fi
if [ $operation == "F" ]; then
echo
echo "Select a flag for restart policy:"
echo
echo " N: Not automatically restart the container"
echo " O: Restart the container if it exits due to an error, which manifests as a non-zero exit code"
echo " A: Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted"
echo " U: Always restart the container if it stops, unless it is stopped manually or otherwise "
echo
read -e -p "Type the flag? [N]: " operation
operation=${operation:-N}
if ! [ $operation == "N" ] && ! [ $operation == "O" ] && ! [ $operation == "A" ] && ! [ $operation == "U" ]
then
echo Invalid selection
exit 1;
fi
if [ $operation == "N" ]; then
operation="no"
elif [ $operation == "O" ]; then
operation="on-failure"
elif [ $operation == "A" ]; then
operation="always"
else
operation="unless-stopped"
fi
docker update --restart $operation $dockerImage
echo Done
exit 0
fi
else
# Stopped
echo "The container is stopped; Possible operations:"
echo " S: Start it"
echo " D: Delete it"
echo " F: Flag restart to it"
echo
read -e -p "What do you want to do? [S]: " operation
operation=${operation:-S}
operation=$( tr '[:lower:]' '[:upper:]' <<< "$operation" )
if ! [ $operation == "S" ] && ! [ $operation == "D" ] && ! [ $operation == "F" ]
then
echo Invalid selection
exit 1;
fi
if [ $operation == "S" ]; then
echo Starting...
docker start $dockerImage
echo $dockerImage started successfully
exit 0
fi
if [ $operation == "D" ]; then
docker rm $dockerImage
echo Done
exit 0
fi
if [ $operation == "F" ]; then
echo
echo "Select a flag for restart policy:"
echo
echo " N: Not automatically restart the container"
echo " O: Restart the container if it exits due to an error, which manifests as a non-zero exit code"
echo " A: Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted"
echo " U: Always restart the container if it stops, unless it is stopped manually or otherwise "
echo
read -e -p "Type the flag? [N]: " operation
operation=${operation:-N}
if ! [ $operation == "N" ] && ! [ $operation == "O" ] && ! [ $operation == "A" ] && ! [ $operation == "U" ]
then
echo Invalid selection
exit 1;
fi
if [ $operation == "N" ]; then
operation="no"
elif [ $operation == "O" ]; then
operation="on-failure"
elif [ $operation == "A" ]; then
operation="always"
else
operation="unless-stopped"
fi
docker update --restart $operation $dockerImage
echo Done
exit 0
fi
fi
fi
echo all good so far
fi
cd $BASEDIR
exit 0