-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtapestry.sh
executable file
·1127 lines (1030 loc) · 33.4 KB
/
tapestry.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
TAPESTRY_SELF=$0
TAPESTRY_DOCKER_SUDO=
TAPESTRY_DRY_RUN=
################################################################################
# NAME
# tapestry-run
#
# SYNOPSIS
# tapestry-run [-h] [-o OUTPUT] [-v] command args...
#
# DESCRIPTION
# Runs a given command unless the program is in dry-run mode. Redirects
# stdout to OUTPUT if given.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print the command before running it always
#
# -o OUTPUT
# The path to save the stdout of the command (default no redirection)
#
tapestry-run() {
local opt_output opt_verbose opt OPTIND OPTARG
opt_verbose=
while getopts "o:hv" opt; do
case "$opt" in
(o) opt_output=$OPTARG;;
(v) opt_verbose=1;;
(h) tapestry-usage -n $LINENO;;
esac
done
shift $(($OPTIND-1))
if [ -n "$opt_verbose" ] || [ -n "$TAPESTRY_DRY_RUN" ]; then
if [ -n "$opt_output" ]; then
printf $'RUN:' >&2
printf $' %q' "$@" >&2
printf $' > %s\n' "$opt_output" >&2
else
printf $'RUN:' >&2
printf $' %q' "$@" >&2
printf $'\n' >&2
fi
fi
if [ -z "$TAPESTRY_DRY_RUN" ]; then
if [ -n "$opt_output" ]; then
"$@" > "$opt_output"
else
"$@"
fi
fi
}
################################################################################
# NAME
# tapestry-extract
#
# SYNOPSIS
# tapestry-extract [-h] [-l] [-v] -f ARCHIVE [-d DIRECTORY]
#
# DESCRIPTION
# Extract the ARCHIVE and optionally write the output to DIRECTORY
# (defaults to current working directory).
#
# OPTIONS
# -h
# Print this help message
#
# -l
# Print all files extracted
#
# -v
# Print commands before running them
#
# -f ARCHIVE
# The path to an archive to extract with some recognized file extension
#
# -d DIRECTORY
# The directory to output to (default current working directory)
#
tapestry-extract() {
local opt_archive opt_directory opt_list opt_verbose opt OPTIND OPTARG IFS \
parts n
opt_archive=
opt_directory=
opt_list=
opt_verbose=
while getopts "f:d:hvl" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(l) opt_list=1;;
(v) opt_verbose=1;;
(f) opt_archive=$OPTARG;;
(d) opt_directory=$OPTARG;;
esac
done
shift $(($OPTIND-1))
if [ -z "$opt_archive" ]; then
tapestry-usage -n $LINENO -e "Missing required argument: ARCHIVE"
fi
IFS=$'.'
parts=( $opt_archive )
n=${#parts[@]}
while [ $n -gt 0 ]; do
n=$(($n - 1))
IFS=$'.'
set -- "${parts[@]:$n:100}"
case "$*" in
(tar.gz)
IFS=$' '
if ! [ -d "$opt_directory" ]; then
mkdir "$opt_directory"
fi
tapestry-run ${opt_verbose:+-v} tar \
x${opt_list:+v}f "$opt_archive" \
${opt_directory:+-C "$opt_directory"}
return
;;
esac
done
tapestry-usage -n $LINENO -e "Unrecognized extension for $opt_archive"
}
################################################################################
# NAME
# tapestry-download
#
# SYNOPSIS
# tapestry-download [-h] [-v] [-p] -u URL -o PATH
#
# DESCRIPTION
# Download URL and store it to PATH. Tries to download using a few
# different executables, depending on which are installed and available.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before running them
#
# -p
# Print progress while downloading (verbose mode)
#
# -u URL
# The URL to download
#
# -o PATH
# The path to save the downloaded file in
#
tapestry-download() {
local opt_url opt_path opt_verbose opt_progress opt OPTIND OPTARG
opt_url=
opt_path=
opt_verbose=
opt_no_progress=1
while getopts "u:o:hvp" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(u) opt_url=$OPTARG;;
(o) opt_path=$OPTARG;;
(v) opt_verbose=1;;
(p) opt_no_progress=;;
esac
done
shift $(($OPTIND-1))
if [ -z "$opt_url" ] || [ -z "$opt_path" ]; then
printf $'Expected url and path\n' >&2
printf $' Got url = %s\n' "$opt_url" >&2
printf $' Got path = %s\n' "$opt_path" >&2
exit 1
fi
if hash curl &>/dev/null; then
tapestry-run ${opt_verbose:+-v} \
curl ${opt_no_progress:+-s} -L "$opt_url" -o "$opt_path"
elif hash wget &>/dev/null; then
tapestry-run ${opt_verbose:+-v} \
wget ${opt_no_progress:+-q} -O "$opt_path" "$opt_url"
else
printf $'Cannot download file from web: no valid executables.\n' >&2
printf $'Please download the following URL and save it at the\n' >&2
printf $'following path\n\n' >&2
printf $'URL = %s\n\n' "$opt_url" >&2
printf $'PATH = %s\n\n' "$opt_path" >&2
exit 1
fi
}
################################################################################
# NAME
# tapestry-do-depend
#
# SYNOPSIS
# ./tapestry.sh depend [-h] [-v] [-p]
#
# DESCRIPTION
# Downloads any dependencies needed to build the Tapestry web server.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before executing them
#
# -p
# Show progress while downloading files
#
tapestry-do-depend() {
local opt_verbose opt_progress opt OPTIND OPTARG has_git has_docker \
has_docker_perm has_docker_swarm
opt_verbose=
opt_progress=
while getopts ":hvp" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(p) opt_progress=1;;
(\?) tapestry-usage -n $LINENO -e "Unexpected option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
has_git=1
if ! which git &>/dev/null; then
printf $'Error: git not installed\n' >&2
printf $' Fix: sudo apt-get install git\n\n' >&2
has_git=
fi
has_docker=1
if ! which docker &>/dev/null; then
printf $'Error: docker not installed\n' >&2
printf $' Fix: sudo apt-get install docker.io\n\n' >&2
has_docker=
fi
has_docker_perm=1
if ! ${TAPESTRY_DOCKER_SUDO:+sudo} docker ps &>/dev/null; then
printf $'Error: docker not accessible from current user\n' >&2
printf $' Fix: Rerun command with -s: ./tapestry.sh -s depend\n' >&2
printf $' Alt: sudo gpasswd -a $USER docker && newgrp docker\n' >&2
printf $' NOTE: https://askubuntu.com/a/477554\n\n' >&2
has_docker_perm=
fi
has_docker_swarm=1
if ! ${TAPESTRY_DOCKER_SUDO:+sudo} docker node ls &>/dev/null; then
printf $'Error: docker swarm not initialized\n' >&2
printf $' Fix: %sdocker swarm init\n\n' \
"${TAPESTRY_DOCKER_SUDO:+sudo }" >&2
has_docker_swarm=
fi
if ! [ "$has_git$has_docker$has_docker_perm$has_docker_swarm" = 1111 ]; then
printf $'Please fix errors before rerunning this command.\n' >&2
exit 1
fi
if ! [ -e tapestry/pbnj/.git ]; then
tapestry-run ${opt_verbose:+-v} git submodule update --init
else
tapestry-run ${opt_verbose:+-v} git submodule update
fi
if ! [ -f tapestry/ispc-v1.9.1-linux.tar.gz ]; then
tapestry-download \
-u http://sourceforge.net/projects/ispcmirror/files/v1.9.1/ispc-v1.9.1-linux.tar.gz/download \
-o tapestry/ispc-v1.9.1-linux.tar.gz \
${opt_verbose:+-v} \
${opt_progress:+-p}
fi
if ! [ -f tapestry/embree-2.16.4.x86_64.linux.tar.gz ]; then
tapestry-download \
-u https://github.com/embree/embree/releases/download/v2.16.4/embree-2.16.4.x86_64.linux.tar.gz \
-o tapestry/embree-2.16.4.x86_64.linux.tar.gz \
${opt_verbose:+-v} \
${opt_progress:+-p}
fi
if ! [ -f tapestry/tbb2017_20161128oss_lin.tgz ]; then
tapestry-download \
-u https://github.com/01org/tbb/releases/download/2017_U3/tbb2017_20161128oss_lin.tgz \
-o tapestry/tbb2017_20161128oss_lin.tgz \
${opt_verbose:+-v} \
${opt_progress:+-p}
fi
}
################################################################################
# NAME
# tapestry-do-build
#
# SYNOPSIS
# ./tapestry.sh build [-h] [-j JOBS] [-t TAG]
#
# DESCRIPTION
# Builds the Tapestry docker image and compiles using JOBS simultaneous
# processes for the Makefiles, and stores to the Docker tag TAG.
#
# OPTIONS
# -h
# Print this help message
#
# -j JOBS
# The number of simultaneous jobs to run in the Docker image (default
# no simultaneous jobs)
#
# -t TAG
# The tag to save the built image under (default "tapestry_tapestry")
#
# -m
# Enable minification of JavaScript code
#
tapestry-do-build() {
local opt_parallel opt_tag opt_verbose opt_minify opt OPTIND OPTARG
opt_parallel=
opt_tag=tapestry_tapestry
opt_verbose=
opt_minify=
while getopts ":j:t:hvm" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(j) opt_parallel=$OPTARG;;
(t) opt_tag=$OPTARG;;
(v) opt_verbose=1;;
(m) opt_minify=1;;
(\?) tapestry-usage -n $LINENO -e "unexpected option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
# create overlay network if doesn't exist
tapestry-run ${opt_verbose:+-v}
${TAPESTRY_DOCKER_SUDO:+sudo} docker network ls \
| grep -q "tapestry_network"
if [ $? -eq 1 ]; then
tapestry-run ${opt_verbose:+v}
${TAPESTRY_DOCKER_SUDO:+sudo} docker network create \
-d overlay tapestry_network
fi;
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker build \
${opt_parallel:+--build-arg build_parallel="-j $opt_parallel"} \
${opt_minify:+--build-arg minifyjs=1} \
${opt_tag:+-t "$opt_tag"} \
tapestry
if [ $? -ne 0 ]; then
echo "Error: Could not build the image";
return 1;
fi
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker build \
--file tapestry/Dockerfile.nginx \
--tag tapestry_nginx \
tapestry
}
################################################################################
# NAME
# tapestry-do-stop
#
# SYNOPSIS
# ./tapestry.sh stop [-h] [-v] [-n NAME]
#
# DESCRIPTION
# Stops the Tapestry service and the NGINX caching service
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before running them
#
# -n NAME
# The name of the Docker service that was assigned (default "tapestry")
#
tapestry-do-stop() {
local opt_verbose opt_name opt OPTIND OPTARG
opt_verbose=
opt_name=tapestry
while getopts ":n:hv" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(n) opt_name=$OPTARG;;
(\?) tapestry-usage -n $LINENO -e "unexpected option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service rm "$opt_name"
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service ls | grep -q tapestry_nginx
if [ $? -eq 0 ]; then
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service rm tapestry_nginx
fi
}
################################################################################
# NAME
# tapestry-do-run
#
# SYNOPSIS
# ./tapestry.sh run [-h] [-v] -c CONFIGS -d DATA [-p PORT] [-n NAME] [-t
# TAG]
#
# DESCRIPTION
# Create and start the Tapestry Docker service, using the CONFIGS and DATA
# directories in the Docker containers. The containers run the image
# provided by TAG and the service will be called NAME. Listens on PORT
# which should be accessible from different machines, provided the firewall
# allows it.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before running them
#
# -k
# Enable distributed caching
#
# -c CONFIGS
# The directory that contains Tapestry configuration files
#
# -d DATA
# The directory that contains Tapestry data files
#
# -a APP
# A directory containing a custom Tapestry application/plugin
#
# -p PORT
# The port to access the Tapestry service at (default "8080")
#
# -n NAME
# The name of the Docker service to be created (default "tapestry")
#
# -t TAG
# The tag of the previously built Docker image (default
# "tapestry_tapestry")
#
tapestry-do-run() {
local opt_verbose opt_config opt_data opt_port opt_name opt_tag \
opt_app_dir opt_enable_cache opt OPTIND OPTARG
opt_verbose=
opt_config=
opt_data=
opt_port=8080
opt_name=tapestry
opt_tag=tapestry_tapestry
opt_app_dir=
opt_enable_cache=
opt_disable_cache=1
while getopts ":c:d:p:n:t:a:hvk" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(c) opt_config=$(realpath "$OPTARG");;
(d) opt_data=$(realpath "$OPTARG");;
(p) opt_port=$OPTARG;;
(n) opt_name=$OPTARG;;
(t) opt_tag=$OPTARG;;
(a) opt_app_dir=$(realpath "$OPTARG");;
(k) opt_enable_cache=1;;
(\?) tapestry-usage -n $LINENO -e "unexpected option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
if [ -z "$opt_config" ] || [ -z "$opt_data" ]; then
tapestry-usage -n $LINENO -e "expected config and data directories"
exit 1
fi
if [[ $opt_enable_cache = 1 ]]; then
unset opt_disable_cache
fi
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service create \
--replicas 1 \
--name "$opt_name" \
--mount type=bind,src="$opt_config",dst=/config \
--mount type=bind,src="$opt_data",dst=/data \
--network tapestry_network \
${opt_disable_cache:+--publish "$opt_port":9010/tcp} \
${opt_app_dir:+--mount type=bind,src="$opt_app_dir",dst=/app} \
${opt_app_dir:+--env APP_DIR=/app} \
"$opt_tag"
if [[ $opt_enable_cache = 1 ]]; then
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service create \
--mode global \
--name tapestry_nginx \
--publish "$opt_port":80/tcp \
--network tapestry_network \
tapestry_nginx
fi
}
################################################################################
# NAME
# tapestry-do-shell
#
# SYNOPSIS
# ./tapestry.sh shell [-h] [-v] [-n NAME]
#
# DESCRIPTION
# Open a shell in the first container of the service
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before executing them
#
# -n NAME
# The name of the service to check (default "tapestry")
#
tapestry-do-shell() {
local opt_verbose opt_name opt OPTIND OPTARG IFS lines first id id2
opt_verbose=
opt_name=tapestry
while getopts ":n:hv" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(n) opt_name=$OPTARG;;
(\?) tapestry-usage -n $LINENO -e "Unexpected option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
IFS=$'\n'
lines=( $(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service ps \
"$opt_name") )
IFS=$' '
first=( ${lines[1]} )
id=${first[0]}
id2=$(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker inspect \
--format "{{.Status.ContainerStatus.ContainerID}}" "$id")
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker exec -it "$id2" /bin/bash
}
################################################################################
# NAME
# tapestry-do-logs
#
# SYNOPSIS
# ./tapestry.sh logs [-h] [-v] [-n NAME]
#
# DESCRIPTION
# Fetch and print any logs from the most recent service with the given
# NAME.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before executing them
#
# -n NAME
# The name of the service to check (default "tapestry")
#
tapestry-do-logs() {
local opt_verbose opt_name opt OPTIND OPTARG IFS lines first id id2
opt_verbose=
opt_name=tapestry
while getopts ":n:hv" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(n) opt_name=$OPTARG;;
(\?) tapestry-usage -n $LINENO -e "Unexpected option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
IFS=$'\n'
lines=( $(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service ps \
"$opt_name") )
IFS=$' '
first=( ${lines[1]} )
id=${first[0]}
id2=$(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker inspect \
--format "{{.Status.ContainerStatus.ContainerID}}" "$id")
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker logs "$id2"
}
################################################################################
# NAME
# tapestry-do-examples
#
# SYNOPSIS
# ./tapestry.sh examples [-h] [-v] [-p]
#
# DESCRIPTION
# Download the example files to use with Tapestry.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Run in verbose mode
#
# -p
# Show progress while downloading files
#
tapestry-do-examples() {
local opt_verbose opt_progress opt OPTIND OPTARG
opt_verbose=
opt_progress=
while getopts ":hvp" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(p) opt_progress=1;;
(\?) tapestry-usage -n $LINENO -e "Unknown option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
if ! [ -e tapestry_examples.tar.gz ]; then
tapestry-download \
-u http://seelab.eecs.utk.edu/tapestry/tapestry_examples.tar.gz \
-o tapestry_examples.tar.gz \
${opt_verbose:+-v} \
${opt_progress:+-p}
fi
if ! [ -e examples ]; then
tapestry-extract \
-f tapestry_examples.tar.gz \
-d examples \
${opt_verbose:+-v}
fi
}
################################################################################
# NAME
# tapestry-do-autoscale
#
# SYNOPSIS
# ./tapestry.sh autoscale [-h] [-v] [-M MAX_CPU] [-m MIN_CPU] [-l
# MIN_CONTAINERS] [-c COOLDOWN] [-i INTERVAL] [-t TRIGGER] [-n NAME]
#
# DESCRIPTION
# Monitor the Docker service NAME and scale the number of replicas up or
# down based on MIN_CPU and MAX_CPU. If the cpu goes above MAX_CPU or below
# MIN_CPU for TRIGGER consecutive checks (spaced INTERVAL seconds apart),
# increase or decrease the number of replicas by 1, then sleep for COOLDOWN
# seconds. The number of replicas will not go below MIN_CONTAINERS, but if
# the number starts below MIN_CONTAINERS, it will only be increased when
# the cpu checks occur.
#
# OPTIONS
#
# -h
# Print this help message
#
# -v
# Print commands before executing them
#
# -M MAX_CPU
# The maximum total CPU usage in percent of the machine used by the
# Docker services (default "10")
#
# -m MIN_CPU
# The minimum total CPU usage in percent of the machine used by the
# Docker services (default "5")
#
# -l MIN_CONTAINERS
# The lower bound on the number of replicas that will be scaled down by
# this script (default "1")
#
# -c COOLDOWN
# The number of seconds to wait after scaling up or down the number of
# replicas (default "5")
#
# -i INTERVAL
# The number of seconds to wait between each check (default "2")
#
# -t TRIGGER
# The number of consecutive too-high or too-low checks before scaling
# the service (default "1")
#
# -n NAME
# The name of the service to monitor and autoscale (default "tapestry")
#
tapestry-do-autoscale() {
local opt_verbose opt_max_cpu opt_min_cpu opt_min_containers opt_cooldown \
opt_interval opt_trigger_threshold opt_name opt OPTIND OPTARG \
scale_up_counter scale_down_counter cpu replicas lines line IFS parts \
ids id containers replicas
opt_verbose=
opt_max_cpu=1000 # in hundredths of a percent
opt_min_cpu=500 # in hundredths of a percent
opt_min_containers=1
opt_cooldown=5 # in seconds
opt_interval=2 # in seconds
opt_trigger_threshold=1 # in number of intervals
opt_name=tapestry
while getopts ":hvM:m:l:c:i:t:n:" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(M) opt_max_cpu=${OPTARG}00;;
(m) opt_min_cpu=${OPTARG}00;;
(l) opt_min_containers=$OPTARG;;
(c) opt_cooldown=$OPTARG;;
(i) opt_interval=$OPTARG;;
(t) opt_trigger_threshold=$OPTARG;;
(n) opt_name=$OPTARG;;
(\?) tapestry-usage -n $LINENO -e "Unknown option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
scale_up_counter=0
scale_down_counter=0
${TAPESTRY_DOCKER_SUDO:+sudo} true # open sudo session if necessary
printf $'Monitoring %s...\n' "$opt_name"
while sleep "$opt_interval"; do
# Get container IDs for Tapestry nodes
IFS=$'\n'
lines=( $(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service ps \
"$opt_name" --no-resolve) )
IFS=$' '
parts=( ${lines[0]} )
[ "${parts[0]}" = ID ] || printf $'Incorrect format: ID\n' >&2
[ "${parts[1]}" = NAME ] || printf $'Incorrect format: NAME\n' >&2
[ "${parts[2]}" = IMAGE ] || printf $'Incorrect format: IMAGE\n' >&2
[ "${parts[3]}" = NODE ] || printf $'Incorrect format: NODE\n' >&2
[ "${parts[4]}" = DESIRED ] || printf $'Incorrect format: DESIRED\n' >&2
[ "${parts[5]}" = STATE ] || printf $'Incorrect format: STATE\n' >&2
[ "${parts[6]}" = CURRENT ] || printf $'Incorrect format: CURRENT\n' >&2
[ "${parts[7]}" = STATE ] || printf $'Incorrect format: STATE\n' >&2
[ "${parts[8]}" = ERROR ] || printf $'Incorrect format: ERROR\n' >&2
ids=()
for line in "${lines[@]:1:1000}"; do # skip header line
IFS=$' '
parts=( $line )
ids+=( "${parts[0]}" )
done
# Get actual container IDs for Tapestry instances
IFS=$'\n'
lines=( $(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker ps \
--no-trunc --format "{{.Names}}\t{{.ID}}") )
containers=()
for line in "${lines[@]}"; do
IFS=$'\t'
parts=( $line )
for id in "${ids[@]}"; do
case "${parts[0]}" in
($opt_name.*.$id)
containers+=( "${parts[1]}" )
break
;;
esac
done
done
# Get stats for the Tapestry instances
IFS=$'\n'
lines=( $(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker stats \
--no-stream "${containers[@]}") )
IFS=$' '
parts=( ${lines[0]} )
[ "${parts[0]}" = CONTAINER ] || printf $'Incorrect format: CONTAINER\n' >&2
[ "${parts[1]}" = CPU ] || printf $'Incorrect format: CPU\n' >&2
[ "${parts[2]}" = % ] || printf $'Incorrect format: %\n' >&2
[ "${parts[3]}" = MEM ] || printf $'Incorrect format: MEM\n' >&2
[ "${parts[4]}" = USAGE ] || printf $'Incorrect format: USAGE\n' >&2
[ "${parts[5]}" = / ] || printf $'Incorrect format: /\n' >&2
[ "${parts[6]}" = LIMIT ] || printf $'Incorrect format: LIMIT\n' >&2
[ "${parts[7]}" = MEM ] || printf $'Incorrect format: MEM\n' >&2
[ "${parts[8]}" = % ] || printf $'Incorrect format: %\n' >&2
[ "${parts[9]}" = NET ] || printf $'Incorrect format: NET\n' >&2
[ "${parts[10]}" = I/O ] || printf $'Incorrect format: I/O\n' >&2
[ "${parts[11]}" = BLOCK ] || printf $'Incorrect format: BLOCK\n' >&2
[ "${parts[12]}" = I/O ] || printf $'Incorrect format: I/O\n' >&2
[ "${parts[13]}" = PIDS ] || printf $'Incorrect format: PIDS\n' >&2
cpu=0
for line in "${lines[@]:1:1000}"; do # skip header line
IFS=$' '
parts=( $line )
# Parse CPU values and compute with them in units of hundredths of
# a percent
case "${parts[1]}" in
(?.??%) cpu=$(($cpu + ${parts[1]:0:1}${parts[1]:2:2}));;
(??.??%) cpu=$(($cpu + ${parts[1]:0:2}${parts[1]:3:2}));;
(???.??%) cpu=$(($cpu + ${parts[1]:0:3}${parts[1]:4:2}));;
(????.??%) cpu=$(($cpu + ${parts[1]:0:4}${parts[1]:5:2}));;
(*) printf $'Bad CPU value from docker: %s\n' "${parts[1]}" >&2;;
esac
done
if [ "$cpu" -gt "$opt_max_cpu" ]; then
scale_up_counter=$(($scale_up_counter + 1))
scale_down_counter=0
fi
if [ "$cpu" -lt "$opt_min_cpu" ]; then
scale_down_counter=$(($scale_down_counter + 1))
scale_up_counter=0
fi
if [ "$cpu" -gt "$opt_min_cpu" ] && [ "$cpu" -lt "$opt_max_cpu" ]; then
scale_up_counter=0
scale_down_counter=0
continue
fi
# Determine the current number of replicas
IFS=$'\n'
lines=( $(tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service ls \
--filter name="$opt_name") )
IFS=$' '
parts=( ${lines[0]} )
[ "${parts[0]}" = ID ] || printf $'Incorrect format: ID\n' >&2
[ "${parts[1]}" = NAME ] || printf $'Incorrect format: NAME\n' >&2
[ "${parts[2]}" = REPLICAS ] || printf $'Incorrect format: REPLICAS\n' >&2
[ "${parts[3]}" = IMAGE ] || printf $'Incorrect format: IMAGE\n' >&2
[ "${parts[4]}" = COMMAND ] || printf $'Incorrect format: COMMAND\n' >&2
# Should only get one line, but could have more
for line in "${lines[@]:1:1000}"; do # skip header line
IFS=$' '
parts=( $line )
replicas=${parts[2]%%/*} # i.e. with 3/4, grab the 3 part
done
if [ "$scale_up_counter" -gt "$opt_trigger_threshold" ]; then
printf $'[%s] Scaling up to %s\n' "$(date)" "$(($replicas + 1))"
tapestry-run ${opt_verbose:+-v} -o /dev/null \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service scale \
"$opt_name=$(($replicas + 1))"
scale_up_counter=0
scale_down_counter=0
sleep "$opt_cooldown"
fi
if [ "$scale_down_counter" -gt "$opt_trigger_threshold" ] &&
[ "$replicas" -gt "$opt_min_containers" ]; then
printf $'[%s] Scaling down to %s\n' "$(date)" "$(($replicas - 1))"
tapestry-run ${opt_verbose:+-v} -o /dev/null \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service scale \
"$opt_name=$(($replicas - 1))"
scale_up_counter=0
scale_down_counter=0
sleep "$opt_cooldown"
fi
done
}
################################################################################
# NAME
# tapestry-do-scale
#
# SYNOPSIS
# ./tapestry.sh scale [-h] [-v] [-n NAME] REPLICAS
#
# DESCRIPTION
# Scale the number of replicas of the service NAME to REPLICAS.
#
# OPTIONS
# -h
# Print this help message
#
# -v
# Print commands before running them
#
# -n NAME
# The name of the service to scale (default "tapestry")
#
tapestry-do-scale() {
local opt_verbose opt_name opt_replicas opt OPTIND OPTARG
opt_verbose=
opt_name=tapestry
while getopts ":n:hv" opt; do
case "$opt" in
(h) tapestry-usage -n $LINENO;;
(v) opt_verbose=1;;
(n) opt_name=$OPTARG;;
(\?) tapestry-usage -n $LINENO -e "Unknown option: -$OPTARG";;
esac
done
shift $(($OPTIND-1))
opt_replicas=$1; shift
if ! [ "$opt_replicas" -eq "$opt_replicas" ] 2>/dev/null; then
tapestry-usage -n $LINENO -e "REPLICAS must be numeric"
fi
if [ "$opt_replicas" -lt 0 ]; then
tapestry-usage -n $LINENO -e "REPLICAS must be a positive number"
fi
tapestry-run ${opt_verbose:+-v} \
${TAPESTRY_DOCKER_SUDO:+sudo} docker service scale \
"$opt_name=$opt_replicas"
}
################################################################################
# NAME
# tapestry-do-cache-report
#
# SYNOPSIS
# ./tapestry.sh cache_report
#
# DESCRIPTION
# Gives a report on the cache's hits and misses
#
tapestry-do-cache-report() {
local container_id
container_id=$(docker ps | grep tapestry_nginx | awk '{print $1}');
docker exec -t $container_id /bin/bash -c "awk '{print $1}' \
/var/log/nginx/cache.log \
| cut -d '[' -f 1 \
| cut -d '-' -f 2 \
| sort | uniq -c | sort -r";
}
################################################################################
# NAME
# tapestry-usage
#
# SYNOPSIS
# tapestry-usage [-h] [-e] [-n LINENO] [message]
#
# DESCRIPTION
# Display the usage of the program and optionally display MESSAGE. Exits
# with a non-zero exit code (if -e is passed).
#