This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdocweb
executable file
·1511 lines (1306 loc) · 35.3 KB
/
docweb
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
#!/bin/bash -i
# Copyright 2016-2020 Faraz Fallahi <fffaraz@gmail.com>
# docweb is not responsible for misuse or for any damage that you may cause!
# You agree that you use this software at your own risk.
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ $# -lt 1 ]; then
echo "Usage: docweb COMMAND [ARGS]"
exit 1
fi
if [ "$1" == "rm" ]; then
# Remove stopped containers
stopped_containers=$(docker ps -q -f status=exited)
[ ! -z "$stopped_containers" ] && docker rm $stopped_containers
# Remove orphaned images
dangling_images=$(docker images -q -f dangling=true)
[ ! -z "$dangling_images" ] && docker rmi $dangling_images
# Remove orphaned volumes
dangling_volumes=$(docker volume ls -q -f dangling=true)
[ ! -z "$dangling_volumes" ] && docker volume rm $dangling_volumes
exit 0
fi
if [ "$1" == "rm:all" ]; then
$0 rm
# Remove all containers and volumes
containers=$(docker ps -a -q)
[ ! -z "$containers" ] && docker stop $containers
[ ! -z "$containers" ] && docker rm $containers
volumes=$(docker volume ls -q)
[ ! -z "$volumes" ] && docker volume rm $volumes
$0 rm
exit 0
# TODO: docker system prune
# https://github.com/RobLoach/docker-clean
fi
if [ "$1" == "rm:img" ]; then
# Remove all images
docker rmi $(docker images -q)
exit 0
fi
if [ "$1" == "build" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb build IMAGE"
exit 1
fi
if [ ! -d /opt/dockerweb/$1 ]; then
echo "Directory /opt/dockerweb/$1 doesn't exist"
exit 1
fi
cd /opt/dockerweb/$1
docker build --no-cache -t fffaraz/$1:latest --rm=true -f Dockerfile .
cd ~-
exit 0
fi
if [ "$1" == "build:all" ] || [ "$1" == "build:core" ]; then
set -euxo pipefail
$0 build proxy/base
$0 build proxy
$0 build balancer
$0 build web/php7nginx/base
$0 build web/php7nginx
$0 build web/php7apache
$0 build app/phpmyadmin
echo DONE
exit 0
fi
if [ "$1" == "stats" ]; then
shift
echo
uname -a
echo
docweb hostname
hostname --all-fqdns
hostname --all-ip-addresses
echo
lsb_release -a
echo
/usr/lib/update-notifier/apt-check --human-readable
echo
apt list --upgradable
echo
uptime
echo
w -i
echo
free -h
echo
vmstat
echo
df -h | grep --color=never -e Filesystem -e '^/dev/'
echo
df -i | grep --color=never -e Filesystem -e '^/dev/'
echo
iostat
echo
ss -s
#docker system df
#smem -tw
#smem -tu
echo
echo --------------------------------------------------------------------------------
echo
docker stats --no-stream $(docker ps --format={{.Names}})
# alias ds='while true; do TEXT=$(docker stats --no-stream $(docker ps --format={{.Names}})); sleep 0.1; clear; echo "$TEXT"; done'
# https://www.datadoghq.com/blog/how-to-collect-docker-metrics/
if [ "$1" == "-f" ]; then
#cat /etc/lsb-release
#cat /proc/cpuinfo
#cat /proc/meminfo
#cat /proc/mdstat
#cat cat /etc/os-release
#cat cat /etc/lsb-release
#dmesg | grep Memory
#lsof +D /home
#dstat -tdD total 60
echo
echo --------------------------------------------------------------------------------
echo
echo Disk Usage:
echo
#du -sm /home/*
du -sh /home/*
echo
echo --------------------------------------------------------------------------------
echo
echo Number of Files:
echo
for i in $(find /home/* -maxdepth 0 -type d); do
echo -n $i" : "
find $i -type f | wc -l
done
fi
echo
exit 0
fi
if [ "$1" == "ps" ]; then
shift
if [ $# -lt 1 ]; then
docker ps -a -s --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Size}}"
exit 0
fi
docker exec "$1" ps -o user,comm,pid,ppid,pgid,time,vsz,rss,args
exit 0
fi
if [ "$1" == "status" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb status NAME"
exit 1
fi
# user,group,comm,pid,ppid,pgid,etime,nice,rgroup,ruser,time,tty,vsz,stat,rss,args
RESULT=$(docker exec "$1" ps -axo user,comm,pid,ppid,pgid,time,vsz,rss,args | grep -v "ps -axo ")
echo "$RESULT" | grep -v "php-fpm: pool webuser"
echo "$RESULT" | grep "php-fpm: pool webuser"
# killall -9 php
exit 0
fi
if [ "$1" == "run" ]; then
shift
RUNARGS="-d --restart=always"
if [ "$1" == "--debug" ]; then
shift
RUNARGS="-it --rm"
fi
if [ "$1" == "--bash" ]; then
shift
RUNARGS="-it --rm --entrypoint /bin/bash"
fi
HTTPPORT=""
if [ "$1" == "--direct" ]; then
shift
HTTPPORT="-p $1:80"
echo "Exposing HTTP Port $1"
shift
fi
SSHPORT=""
if [ $# -eq 4 ]; then
SSHPORT="-e WEBUSER_PASSWORD=$3 -p $4:22"
echo "Exposing SSH Port $4"
fi
if [ $# -lt 2 ]; then
echo "Usage: docweb run [--debug|--bash] [--direct PORT] IMAGE NAME [WEBUSER_PASSWORD SSH_PORT]"
exit 1
fi
if [[ "$(docker images -q fffaraz/web/$1:latest 2> /dev/null)" == "" ]]; then
echo "Docker image fffaraz/web/$1:latest doesn't exist"
exit 1
fi
if [ "$2" == "proxy" ]; then
echo "The name 'proxy' is reserved for the reverse proxy server container"
exit 1
fi
ISRUNNING=$(docker inspect --format={{.State.Running}} $2 2> /dev/null)
if [ $? -eq 0 ]; then
echo "Stopping the running instance ..."
docker stop $2
docker rm $2
fi
mkdir -p /home/$2
docker run $RUNARGS \
--name=$2 \
--hostname=$2 \
--net=isolated_nw \
--volume=/home/$2:/home/webuser \
-w=/home/webuser \
--memory=512m \
$HTTPPORT \
$SSHPORT \
fffaraz/web/$1:latest
# --ulimit <type>=<soft limit>[:<hard limit>]
# --ulimit nproc=64:64
# --memory-swap=512m
# --kernel-memory=8M
# --cap-drop=all --cap-add=NET_BIND_SERVICE
exit 0
fi
if [ "$1" == "log" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb log NAME"
exit 1
fi
docker logs -f $1
exit 0
fi
if [ "$1" == "stop" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb stop NAME"
exit 1
fi
docker stop --time 86400 $1
#docker kill $1
#docker kill --signal TERM $1
docker rm $1
exit 0
fi
if [ "$1" == "exec" ]; then
shift
RUNASROOT=0
if [ "$1" = "--root" ]; then
shift
RUNASROOT=1
fi
if [ $# -lt 1 ]; then
echo "Usage: docweb exec [--root] NAME"
exit 1
fi
if [ $RUNASROOT -eq 1 ]; then
docker exec -it -e COLUMNS=$COLUMNS -e LINES=$LINES -e TERM=$TERM $1 /bin/bash -c 'cd /home/webuser/www; exec /bin/bash -i -l'
else
docker exec -it --user webuser -e COLUMNS=$COLUMNS -e LINES=$LINES -e TERM=$TERM $1 /bin/bash -c 'cd /home/webuser/www; exec /bin/bash -i -l'
fi
exit 0
fi
if [ "$1" == "clean" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb clean NAME"
exit 1
fi
if [ ! -d /home/$1 ]; then
echo "Directory /home/$1 doesn't exist"
exit 1
fi
rm -rf /home/$1/tmp
rm -rf /home/$1/log
exit 0
fi
if [ "$1" == "update" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb update NAME [args]"
exit 1
fi
docker exec -it $1 /script_update.sh $@
exit 0
fi
if [ "$1" == "backup" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb backup NAME"
exit 1
fi
if [ ! -d /home/$1 ]; then
echo "Directory /home/$1 doesn't exist"
exit 1
fi
if [ "$1" == "backup" ]; then
echo "Can't backup the backup directory!"
exit 1
fi
mkdir -p /home/backup
FILEPATH=/home/backup/$1_$(date +%Y-%m-%d_%H-%M-%S).tar.gz
# TODO: pigz
tar --exclude=$1/backup --exclude=$1/log --exclude=$1/tmp --directory=/home -czf $FILEPATH $1
ls -lh $FILEPATH
#gzip -v -t $FILEPATH
exit 0
fi
if [ "$1" == "log:nginx" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb log:nginx NAME [--access | --error]"
exit 1
fi
if [ ! -d /home/$1 ]; then
echo "Directory /home/$1 doesn't exist"
exit 1
fi
if [ $# -eq 2 ] && [ "$2" == "--access" ]; then
tail -f /home/$1/log/nginx/access.log
elif [ $# -eq 2 ] && [ "$2" == "--error" ]; then
tail -f /home/$1/log/nginx/error.log
else
tail -f /home/$1/log/nginx/*.log
fi
exit 0
fi
if [ "$1" == "log:php" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb log:php NAME [--access | --error | --mail]"
exit 1
fi
if [ ! -d /home/$1 ]; then
echo "Directory /home/$1 doesn't exist"
exit 1
fi
if [ $# -eq 2 ] && [ "$2" == "--access" ]; then
tail -f /home/$1/log/php/access.log
elif [ $# -eq 2 ] && [ "$2" == "--error" ]; then
tail -f /home/$1/log/php/error.log
elif [ $# -eq 2 ] && [ "$2" == "--mail" ]; then
tail -f /home/$1/log/php/mail.log
else
tail -f /home/$1/log/php/*.log
fi
exit 0
fi
if [ "$1" == "proxy:run" ]; then
shift
# docweb proxy:run [--debug] [SERVER_NAME]
ISRUNNING=$(docker inspect --format={{.State.Running}} proxy 2> /dev/null)
if [ $? -eq 0 ]; then
echo "Stopping the running instance ..."
docker stop proxy
docker rm proxy
fi
RUNARG="-d --restart=always"
if [ "$1" = "--debug" ]; then
shift
RUNARG="-it --rm"
fi
mkdir -p /home/proxy
mkdir -p /home/proxy/ssl/etc
mkdir -p /home/proxy/ssl/lib
mkdir -p /home/proxy/ssl/log
mkdir -p /home/proxy/ssl/default
docker run $RUNARG \
--name=proxy \
--hostname=$(docweb hostname) \
--net=isolated_nw \
--memory=512m \
-p 80:80 -p 443:443 \
-v /home/proxy:/home/webuser \
-v /home/proxy/ssl/etc:/etc/letsencrypt \
-v /home/proxy/ssl/lib:/var/lib/letsencrypt \
-v /home/proxy/ssl/log:/var/log/letsencrypt \
-v /home/proxy/ssl/default:/opt/nginx/conf/cert \
-e CERTEMAIL="fffaraz@gmail.com" \
fffaraz/proxy:latest \
$@
exit 0
fi
if [ "$1" == "proxy:update" ]; then
shift
docker exec -i proxy /script_update.sh $@
exit 0
fi
if [ "$1" == "balancer:run" ]; then
shift
ISRUNNING=$(docker inspect --format={{.State.Running}} balancer 2> /dev/null)
if [ $? -eq 0 ]; then
echo "Stopping the running instance ..."
docker stop balancer
docker rm balancer
fi
RUNARG="-d --restart=always"
if [ "$1" = "--debug" ]; then
shift
RUNARG="-it --rm"
fi
mkdir -p /home/balancer
docker run $RUNARG \
--name=balancer \
--net=isolated_nw \
--memory=512m \
-v /home/balancer:/home/webuser \
fffaraz/balancer:latest
exit 0
fi
if [ "$1" == "balancer:update" ]; then
shift
docker exec -i balancer /script_update.sh $@
exit 0
fi
if [ "$1" == "proxy:log" ]; then
shift
if [ $# -eq 1 ] && [ "$1" == "--trafic" ]; then
tail -f /home/proxy/log/nginx/trafic.log
elif [ $# -eq 1 ] && [ "$1" == "--error" ]; then
tail -f /home/proxy/log/nginx/error.log
else
tail -f /home/proxy/log/nginx/*.log
fi
exit 0
fi
if [ "$1" == "proxy:status" ]; then
shift
# https://github.com/lebinh/ngxtop
# https://github.com/allinurl/goaccess
exit 0
fi
if [ "$1" == "hostname" ]; then
shift
H1=$(hostname --fqdn)
H2=$(hostnamectl --static)
[ ${#H1} -ge ${#H2} ] && echo $H1 || echo $H2
exit 0
fi
if [ "$1" == "ip" ]; then
shift
IPV4=$(dig +short myip.opendns.com @208.67.222.222 a) || IPV4=""
echo "Your IPv4 address is: $IPV4"
IPV6=$(dig +short myip.opendns.com @2620:0:ccc::2 aaaa) || IPV6=""
echo "Your IPv6 address is: $IPV6"
exit 0
fi
if [ "$1" == "microdns:run" ]; then
shift
DNSTTL="-ttl 3600"
if [ "$1" = "--ttl" ]; then
shift
DNSTTL="-ttl $1"
shift
fi
DNSLOG=""
if [ "$1" = "--log" ]; then
shift
DNSLOG="-log"
fi
#curl -s http://whatismyip.akamai.com/
IPV4=$(dig +short myip.opendns.com @208.67.222.222 a) || IPV4=""
echo "Your IPv4 address is: $IPV4"
IPV6=$(dig +short myip.opendns.com @2620:0:ccc::2 aaaa) || IPV6=""
echo "Your IPv6 address is: $IPV6"
[ ! -z "$IPV4" ] && IPV4="-ipv4 $IPV4"
[ ! -z "$IPV6" ] && IPV6="-ipv6 $IPV6"
ISRUNNING=$(docker inspect --format={{.State.Running}} microdns 2> /dev/null)
[ $? -eq 0 ] && docker rm -f microdns
mkdir -p /home/microdns
[ ! -f /home/microdns/dns.conf ] && echo "# domain. IPv4 IPv6" > /home/microdns/dns.conf
docker run -d --restart=always \
--name microdns -p 53:53 -p 53:53/udp \
--memory 16m \
-v /home/microdns:/home \
fffaraz/microdns:latest \
$IPV4 $IPV6 $DNSTTL $DNSLOG -conf /home/dns.conf
exit 0
fi
if [ "$1" == "microdns:log" ]; then
docker logs -f --tail 1000 microdns
exit 0
fi
if [ "$1" == "bind:run" ]; then
shift
ISRUNNING=$(docker inspect --format={{.State.Running}} bind 2> /dev/null)
[ $? -eq 0 ] && docker rm -f bind
mkdir -p /home/bind
docker run -d --restart=always \
--name bind --memory 64m \
--hostname=$(docweb hostname) \
-p 53:53 -p 53:53/udp \
-v /home/bind:/data \
fffaraz/bind
exit 0
fi
if [ "$1" == "bind:webmin" ]; then
shift
if [ $# -lt 2 ]; then
echo "Usage: docweb bind:webmin ROOT_PASSWORD WEBMIN_ENABLED"
exit 1
fi
ISRUNNING=$(docker inspect --format={{.State.Running}} bindwebmin 2> /dev/null)
[ $? -eq 0 ] && docker rm -f bindwebmin
mkdir -p /home/bindwebmin
docker run -d --restart=always \
--name bindwebmin --memory 128m \
--hostname=$(docweb hostname) \
-p 53:53 -p 53:53/udp -p 90:10000 \
-v /home/bindwebmin:/data \
-e ROOT_PASSWORD="$1" \
-e WEBMIN_ENABLED="$2" \
fffaraz/bindwebmin
exit 0
fi
if [ "$1" == "coredns:run" ]; then
shift
# https://github.com/miekg/coredns
exit 0
fi
if [ "$1" == "pma:run" ]; then
shift
RUNARG="-d --restart=always"
if [ "$1" = "--debug" ]; then
shift
RUNARG="-it --rm"
fi
HTTPPORT=""
if [ "$1" = "--direct" ]; then
shift
HTTPPORT="-p $1:80"
echo "Exposing HTTP Port $1"
shift
fi
if [ $# -lt 2 ]; then
echo "Usage: docweb pma:run [--debug] [--direct PORT] MYSQLSERVER MYSQLROOTPASSWORD [pma status] [CaptchaPublic CaptchaPrivate]"
# TODO: controluser controlpass pmadb
exit 1
fi
ISRUNNING=$(docker inspect --format={{.State.Running}} ${1}_pma 2> /dev/null)
[ $? -eq 0 ] && docker rm -f ${1}_pma
PHPMYADMIN=pma
PHPSTATUS=status
if [ $# -eq 4 ]; then
PHPMYADMIN=$3
PHPSTATUS=$4
fi
CaptchaPublic=
CaptchaPrivate=
if [ $# -eq 6 ]; then
CaptchaPublic=$5
CaptchaPrivate=$6
fi
docker run $RUNARG \
--name=${1}_pma \
--net=isolated_nw \
--memory=512m \
$HTTPPORT \
-e MYSQL_SERVER="$1" \
-e MYSQL_ROOT_PASSWORD="$2" \
-e PHPMYADMIN="$PHPMYADMIN" \
-e PHPSTATUS="$PHPSTATUS" \
-e CaptchaPublic="$CaptchaPublic" \
-e CaptchaPrivate="$CaptchaPrivate" \
fffaraz/app/phpmyadmin:latest
exit 0
fi
if [ "$1" == "glances:run" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb glances:run HASH"
exit 1
fi
killall glances
#mkdir -p /etc/glances
#echo -n $1 > /etc/glances/glances.pwd
mkdir -p ~/.config/glances
echo -n $1 > ~/.config/glances/glances.pwd
if [ $# -gt 1 ]; then
nohup glances -w -B 172.17.0.1 --disable-plugin $2 --password &
else
nohup glances -w -B 172.17.0.1 --password &
fi
exit 0
fi
if [ "$1" == "redis:run" ]; then
shift
RUNARGS="-d --restart=always"
if [ "$1" == "--debug" ]; then
shift
RUNARGS="-it --rm"
fi
REDISPORT=""
if [ "$1" = "--direct" ]; then
shift
REDISPORT="-p $1:6379"
echo "Exposing Redis Port $1"
shift
fi
if [ $# -lt 2 ]; then
echo "Usage: docweb redis:run [--debug] [--direct PORT] NAME PASSWORD"
exit 1
fi
ISRUNNING=$(docker inspect --format={{.State.Running}} $1 2> /dev/null)
if [ $? -eq 0 ]; then
echo "Stopping the running instance ..."
docker stop $1
docker rm $1
fi
mkdir -p /home/$1/data
# /myredis/conf/redis.conf:/usr/local/etc/redis/redis.conf
docker run $RUNARGS \
--name="$1" \
--hostname="$1" \
--net=isolated_nw \
--memory=512m \
--volume=/home/$1/data:/data \
$REDISPORT \
redis:alpine redis-server --appendonly yes --requirepass "$2"
exit 0
fi
if [ "$1" == "postgres:run" ]; then
shift
RUNARGS="-d --restart=always"
if [ "$1" == "--debug" ]; then
shift
RUNARGS="-it --rm"
fi
if [ $# -lt 2 ]; then
echo "Usage: docweb postgres:run [--debug] NAME POSTGRES_ROOT_PASSWORD"
exit 1
fi
ISRUNNING=$(docker inspect --format={{.State.Running}} $1 2> /dev/null)
if [ $? -eq 0 ]; then
echo "Stopping the running instance ..."
docker stop $1
docker rm $1
fi
mkdir -p /home/$1/log
mkdir -p /home/$1/data
mkdir -p /home/$1/backup
mkdir -p /home/$1/entrypoint
docker run $RUNARGS \
--name="$1" \
--hostname="$1" \
--net=isolated_nw \
--memory=512m \
-v /home/$1/backup:/backup \
-v /home/$1/log:/var/log/postgresql \
-v /home/$1/data:/var/lib/postgresql/data \
-v /home/$1/entrypoint:/docker-entrypoint-initdb.d \
-e POSTGRES_PASSWORD="$2" \
postgres:10 #latest
exit 0
fi
if [ "$1" == "postgres:listdbs" ] || [ "$1" == "postgres:listdb" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb postgres:listdbs NAME"
exit 1
fi
docker exec -it $1 sh -c 'export COLUMNS=100; psql -U postgres -l'
exit 0
fi
if [ "$1" == "postgres:backup" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb postgres:backup NAME [DATABASE]"
exit 1
fi
PSQLSERVER=$1
shift
ISRUNNING=$(docker inspect --format={{.State.Running}} $PSQLSERVER 2> /dev/null)
if [ $? -ne 0 ]; then
echo "PostgreSQL instance $PSQLSERVER is not running."
exit 1
fi
mkdir -p /home/$PSQLSERVER/backup
if [ $# -lt 1 ]; then
docker exec -u postgres $PSQLSERVER pg_dumpall -U postgres -c > /home/$PSQLSERVER/backup/dump_$(date +%Y-%m-%d_%H-%M-%S).sql
else
docker exec -u postgres $PSQLSERVER pg_dump -U postgres -c $1 > /home/$PSQLSERVER/backup/dump_$1_$(date +%Y-%m-%d_%H-%M-%S).sql
fi
exit 0
fi
if [ "$1" == "postgres:clean" ]; then
shift
if [ $# -lt 1 ]; then
echo "Usage: docweb postgres:clean NAME [DATABASE]"
exit 1
fi
PSQLSERVER=$1
shift
ISRUNNING=$(docker inspect --format={{.State.Running}} $PSQLSERVER 2> /dev/null)
if [ $? -ne 0 ]; then
echo "PostgreSQL instance $PSQLSERVER is not running."
exit 1
fi
if [ $# -lt 1 ]; then
echo "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" | docker exec -i $PSQLSERVER psql -U postgres
else
echo "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" | docker exec -i $PSQLSERVER psql -U postgres -d $1
fi
exit 0
fi
if [ "$1" == "postgres:restore" ]; then
shift
if [ $# -lt 2 ]; then
echo "Usage: docweb postgres:restore NAME FILENAME [DATABASE]"
exit 1
fi
PSQLSERVER=$1
shift
ISRUNNING=$(docker inspect --format={{.State.Running}} $PSQLSERVER 2> /dev/null)
if [ $? -ne 0 ]; then
echo "PostgreSQL instance $PSQLSERVER is not running."
exit 1
fi
if [ $# -lt 2 ]; then
cat /home/$PSQLSERVER/backup/$1 | docker exec -i $PSQLSERVER psql -U postgres
else
cat /home/$PSQLSERVER/backup/$1 | docker exec -i $PSQLSERVER psql -U postgres -d $2
fi
exit 0
fi
if [ "$1" == "mysql:run" ]; then
shift
RUNARGS="-d --restart=always"
if [ "$1" == "--debug" ]; then
shift
RUNARGS="-it --rm"
fi
MYSQLPORT=""
if [ "$1" = "--direct" ]; then
shift
MYSQLPORT="-p $1:3306"
echo "Exposing MySQL Port $1"
shift
fi
MEMORY=512
if [ "$1" == "--memory" ]; then
shift
MEMORY=$1
shift
fi
SKIPGRANT=""
if [ "$1" == "--skip-grant" ]; then
shift
SKIPGRANT="mysqld_safe --skip-grant-tables"
fi
SKIPNET=""
if [ "$1" == "--skip-net" ]; then
shift
SKIPNET="--skip-networking"
fi
if [ $# -lt 2 ]; then
echo "Usage: docweb mysql:run [--debug] [--direct PORT] [--memory M] [--skip-grant] [--skip-net] NAME MYSQL_ROOT_PASSWORD [mysql|mariadb|mysql/mysql-server]"
exit 1
fi
ISRUNNING=$(docker inspect --format={{.State.Running}} $1 2> /dev/null)
if [ $? -eq 0 ]; then
echo "Stopping the running instance ..."
docker stop --time 86400 $1
docker rm $1
fi
# https://hub.docker.com/_/mysql/
# https://hub.docker.com/_/mariadb/
# https://hub.docker.com/r/mysql/mysql-server/
# https://hub.docker.com/_/percona/
# https://hub.docker.com/r/percona/percona-server/
# https://github.com/mysql/mysql-sys
DBSERVER="mariadb"
[[ $# -eq 3 ]] && DBSERVER="$3"
echo "DBSERVER: $DBSERVER"
rm -rf /home/$1/conf.d
rm -rf /home/$1/log
mkdir -p /home/$1/backup
mkdir -p /home/$1/conf.d
mkdir -p /home/$1/datadir
mkdir -p /home/$1/entrypoint
mkdir -p /home/$1/log
chown -R 999:999 /home/$1/log
cat > /home/$1/conf.d/global.cnf <<'EOL'
[mysqld]
default_storage_engine=InnoDB
skip_name_resolve=1
general_log=0
general_log_file=/var/log/mysql/general.log
innodb_fast_shutdown=0
innodb_file_per_table=1
innodb_lock_wait_timeout=120
local_infile=0
log_error=/var/log/mysql/error.log
log_warnings=2
long_query_time=1
max_allowed_packet=1G
max_connect_errors=1000000
max_connections=500
open_files_limit=100000
performance_schema=0
skip_external_locking=1
slow_query_log=1
slow_query_log_file=/var/log/mysql/slow.log
log_queries_not_using_indexes=0
character_set_server=utf8
character_set_filesystem=utf8
collation_server=utf8_unicode_ci
# symbolic-links=0
# character_set_results=utf8
# character_set_client_handshake=utf8
# character_set_connection=utf8
# collation_connection=utf8_unicode_ci
# sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
# skip-innodb_doublewrite
# innodb_doublewrite = 0
[client]
default-character-set=utf8
[mysqldump]
quick
max_allowed_packet=1G
[mysql]
default-character-set=utf8
no-auto-rehash
EOL
echo "
[mysqld]
binlog_cache_size=4K
binlog_stmt_cache_size=4K
innodb_buffer_pool_instances=1
innodb_buffer_pool_size=$((MEMORY/4))M
# innodb_log_file_size * innodb_log_files_in_group should be equals to 1/4 of buffer pool size
# innodb_log_file_size=8M
innodb_ft_cache_size=4M
innodb_ft_total_cache_size=32M
innodb_sort_buffer_size=8M
join_buffer_size=4M
key_buffer_size=256K
max_heap_table_size=32M
query_cache_limit=512K
query_cache_min_res_unit=2K
query_cache_size=32M
query_cache_type=1
read_buffer_size=1M
read_rnd_buffer_size=1M
sort_buffer_size=4M
table_definition_cache=1K
table_open_cache=2K
thread_cache_size=16
tmp_table_size=32M
" > /home/$1/conf.d/limits.cnf
# echo "
# [client]
# user=root
# password=$2
# host=127.0.0.1
# " > /home/$1/conf.d/credentials.cnf
docker run $RUNARGS \
--name="$1" \
--hostname="$1" \
--net=isolated_nw \