-
Notifications
You must be signed in to change notification settings - Fork 1
/
nimble.sh
executable file
·1178 lines (906 loc) · 29.9 KB
/
nimble.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
#!/bin/bash
# A Nimble Docker Environment
# Copyright (C) 2017 John Romberger [email protected]
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Is the command ./_nimble/nimble.sh or nimble
if [[ $0 == \.* ]]; then
command=$0
else
command=`basename "$0"`
fi
script="$0"
source="$PWD/_nimble/nimble.sh"
tld="$(<_conf/tld.conf)"
if [[ -z "$tld" ]]; then
tld="local"
fi
# Check that we're running in docker root directory
if [[ ! -f $source ]]; then
echo "This command must be run from the docker root directory. Please navigate there to use Nimble."
exit 1
fi
is_root(){
# if script is found, we're ok. if not... ut oh
if [[ -f $source ]]; then
return 0
fi
return 1
}
is_mac(){
# mac forwards loopback instead of using nat
if [ "$(uname)" == "Darwin" ]; then
return 0
fi
return 1
}
is_cygwin(){
# check for mingw windows for handling of directories
if [[ "$(uname -s)" = MINGW* ]]; then
return 0
fi
return 1
}
# If file is not source, check that file is the same as source
# If not, call source, and replace this file
if ! [[ $command = "./_nimble/nimble.sh" ]]; then
if ! cmp -s "$script" "$source"; then
$source $@
$source localize
# Exit with source exit code
exit $?
fi
fi
nimble_root="$PWD"
project_root="$nimble_root"
site_root="$project_root/sites"
template_root="$project_root/_templates"
images_root="$project_root/images"
certs_root=$project_root/_conf/certs
argies="$@"
template=""
# directories in the VM may be different than in your command line
# i should make this nicer
nimble_root_command_line="$nimble_root"
nimble_root_vm="$nimble_root"
images_root_command_line="$images_root"
images_root_vm="$images_root"
site_root_command_line="$site_root"
site_root_vm="$site_root"
help(){
echo "Setup:"
echo "Usage: $command setup"
echo "Sets up your environment"
echo "Create new project:"
echo "Usage: $command create \$project"
echo "Init project:"
echo "Usage: $command init \$project"
echo "Install WP DB:"
echo "Usage: $command install \$project"
echo "Add Site to Hosts:"
echo "Usage: $command hosts \$project"
echo "Remove Site from Hosts:"
echo "Usage: $command rmhosts \$project"
echo "Delete all docker containers"
echo "Usage: $command clear"
echo "Delete all docker containers AND IMAGES!"
echo "Usage: $command clear all"
echo "Clean old docker volumes"
echo "Usage: $command clean volumes"
echo "Delete a project"
echo "Usage: $command delete \$project"
echo "Delete all projects"
echo "Usage: $command delete all"
}
if is_cygwin; then
nimble_root_command_line=${nimble_root_command_line//"/mnt/f/"/"f:/"}
nimble_root_command_line=${nimble_root_command_line//"/mnt/F/"/"F:/"}
nimble_root_command_line=${nimble_root_command_line//"/mnt/c/"/"c:/"}
nimble_root_command_line=${nimble_root_command_line//"/mnt/C/"/"C:/"}
nimble_root_command_line=${nimble_root_command_line//"/f/"/"f:/"}
nimble_root_command_line=${nimble_root_command_line//"/F/"/"F:/"}
nimble_root_command_line=${nimble_root_command_line//"/c/"/"c:/"}
nimble_root_command_line=${nimble_root_command_line//"/C/"/"C:/"}
images_root_command_line=${images_root_command_line//"/mnt/f/"/"f:/"}
images_root_command_line=${images_root_command_line//"/mnt/F/"/"F:/"}
images_root_command_line=${images_root_command_line//"/mnt/c/"/"c:/"}
images_root_command_line=${images_root_command_line//"/mnt/C/"/"C:/"}
images_root_command_line=${images_root_command_line//"/f/"/"f:/"}
images_root_command_line=${images_root_command_line//"/F/"/"F:/"}
images_root_command_line=${images_root_command_line//"/c/"/"c:/"}
images_root_command_line=${images_root_command_line//"/C/"/"C:/"}
site_root_command_line=${site_root_command_line//"/mnt/f/"/"f:/"}
site_root_command_line=${site_root_command_line//"/mnt/F/"/"F:/"}
site_root_command_line=${site_root_command_line//"/mnt/c/"/"c:/"}
site_root_command_line=${site_root_command_line//"/mnt/C/"/"C:/"}
site_root_command_line=${site_root_command_line//"/f/"/"f:/"}
site_root_command_line=${site_root_command_line//"/F/"/"F:/"}
site_root_command_line=${site_root_command_line//"/C/"/"C:/"}
else
if ! is_mac; then
# bash on ubuntu for windows .. hopefully not a dual boot
nimble_root_vm=${nimble_root_vm//"/mnt/f/"/"/f/"}
nimble_root_vm=${nimble_root_vm//"/mnt/F/"/"/F/"}
nimble_root_vm=${nimble_root_vm//"/mnt/c/"/"/c/"}
nimble_root_vm=${nimble_root_vm//"/mnt/C/"/"/C/"}
images_root_vm=${images_root_vm//"/mnt/f/"/"/f/"}
images_root_vm=${images_root_vm//"/mnt/F/"/"/F/"}
images_root_vm=${images_root_vm//"/mnt/c/"/"/c/"}
images_root_vm=${images_root_vm//"/mnt/C/"/"/C/"}
site_root_vm=${site_root_vm//"/mnt/f/"/"/f/"}
site_root_vm=${site_root_vm//"/mnt/F/"/"/F/"}
site_root_vm=${site_root_vm//"/mnt/c/"/"/c/"}
site_root_vm=${site_root_vm//"/mnt/C/"/"/C/"}
fi
fi
debug() {
echo "nimble_root_command_line=${nimble_root_command_line}"
echo "images_root_command_line=${images_root_command_line}"
echo "site_root_command_line=${site_root_command_line}"
echo "nimble_root_vm=${nimble_root_vm}"
echo "images_root_vm=${images_root_vm}"
echo "site_root_vm=${site_root_vm}"
}
# nice yes/no function
confirm() {
local prompt
local default
local REPLY
# http://djm.me/ask
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question - use /dev/tty in case stdin is redirected from somewhere else
read -p "$1 [$prompt] " REPLY </dev/tty
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
# nice variable confirmation function based off of the above
# first argument is a return variable
ask() {
local required=0
local REPLY
local prompt=$2
while true; do
if [ -z "$prompt" ]; then
prompt="Warning: There is no text for this question. Sorry!!!"
fi
if [ -z "$3" ]; then
default="" # no default
elif [ "$3" == "--required" ]; then
required=1
default=""
else
default="$3"
fi
if [ "$4" == "--required" ]; then
required=1
fi
# Ask the question - use /dev/tty in case stdin is redirected from somewhere else
read -p "$prompt [$default]: " REPLY </dev/tty
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
if [ ! -z "$REPLY" ] || [ $required -eq 0 ]; then
eval "$1='$REPLY'"
return 1
fi
done
}
args(){
#!/bin/bash
# Use -gt 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use -gt 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it such
# as in the --default example).
# note: if this is set to -gt 0 the /etc/hosts part is not recognized ( may be a bug )
local i=1
while [[ i -le "$#" ]]
do
local key="${!i}"
local value_index=$(($i+1))
local value="${!value_index}"
case $key in
-t|--template)
template="$value"
set -- "${@:1:i-1}" "${@:i+2}"
;;
*)
# unknown option
;;
esac
i=$(($i+1))
done
argies="$@"
}
# this is the magic right here
wp(){
# requires project name
if [[ -z "$1" ]]; then
echo "You need to provide the project to wp into! e.g., nimble wp myproject"
return
elif [ "$1" == "all" ]; then
echo "'all' is not a valid name for a project! Unfortunately, it conflicts with \`delete all\`. Try a new name!"
return
fi
local project=$1
local inner_dir="/var/www/html"
bashitup "$project" "cd $inner_dir && wp ${*:2}"
}
localize(){
mkdir ~/bin >& /dev/null
rm ~/bin/nimble >& /dev/null
ln -s "$source" ~/bin/nimble
chmod +x ~/bin/nimble
return $?
}
fetch_repo(){
if [[ -z "$1" ]]; then
echo "Please enter a repo name like 'owner/repo'"
exit 1
fi
if [[ -z "$2" ]]; then
echo "Please enter a directory"
exit 1
fi
mkdir -p "$2"
git clone "https://github.com/$1.git" "$2"
}
get_template(){
if [[ -z "$1" ]]; then
echo "Please select a template"
exit 1
fi
local template_dir="$template_root/$1"
if [ ! -d "$template_dir" ]; then
echo "Template $1 does not exist, fetching..."
fetch_repo "$1" "$template_dir"
fi
if [ ! -f "$template_dir/template.yml" ]; then
echo "Invalid Template! Please check the git repo name"
fi
}
has_hook(){
local project="$1"
local hook="$2"
if [[ -z "$project" ]]; then
echo "has_hook requires a project!"
return 1
fi
if [[ -z "$hook" ]]; then
echo "has_hook requires a hook (obvs)!"
return 1
fi
if [ -f "$site_root/$project/hooks/$hook.sh" ]; then
return 0
else
if [[ -z "$template" ]]; then
if [[ -f "$site_root/$project/template.conf" ]]; then
local project_template="$(<$site_root/$project/template.conf)"
fi
else
local project_template="$template"
fi
if [[ ! -z "$project_template" ]]; then
if [[ -f "$template_root/$project_template/hooks/$hook.sh" ]]; then
return 0
fi
fi
fi
return 1
}
do_hook(){
local project="$1"
local hook="$2"
if [[ -z "$project" ]]; then
echo "do_hook requires a project!"
return 1
fi
if [[ -z "$hook" ]]; then
echo "do_hook requires a hook (obvs)!"
return 1
fi
if [ -f "$site_root/$project/hooks/$hook.sh" ]; then
source "$site_root/$project/hooks/$hook.sh"
else
# need a verbose feature
# echo "Could not find a local hook. Using Template hook"
if [[ -z "$template" ]]; then
if [[ -f "$site_root/$project/template.conf" ]]; then
local project_template="$(<$site_root/$project/template.conf)"
fi
else
local project_template="$template"
fi
if [[ ! -z "$project_template" ]]; then
if [[ -f "$template_root/$project_template/hooks/$hook.sh" ]]; then
source "$template_root/$project_template/hooks/$hook.sh"
else
# Need a verbose option
: # echo "Could not find project template hook"
fi
else
# Need a verbose option
: # echo "No template found"
fi
fi
}
create_front(){
#
# ugh I should redo this
#
local oldtemplate=template
template="johnrom/nimble-nginx-proxy"
local project="_front"
local project_name="${project//_/}"
local site_dir="$site_root/$project"
local template_dir="$template_root/$template"
# adding project name in case they differ
do_hook "$project" "before-create" "$project_name"
get_template "$template"
# create directories
#
if [ -d "$site_dir" ]; then
echo "Error: Folder already exists for project: $project. Exiting!"
exit 1
fi
echo "creating project directory: $project"
mkdir -p $site_dir
echo "$template" > "$site_dir/template.conf"
# update dev config
#
echo "Adding .yml file"
local dev_template=$(<$template_dir/template.yml)
echo "$dev_template" > "$site_dir/$project_name.yml"
if [[ -d "$template_dir/conf" ]]; then
cp -R "$template_dir/conf" "$site_dir/conf"
fi
if [[ -d "$template_dir/hooks" ]]; then
cp -R "$template_dir/hooks" "$site_dir/hooks"
fi
# adding project name in case they differ
do_hook "$project" "after-create" "$project_name"
template="$oldtemplate"
}
create(){
# requires project name
if [[ -z "$1" ]]; then
help
return
elif [ "$1" == "all" ]; then
echo "'all' is not a valid name for a project! Unfortunately, it conflicts with \`delete all\`. Try a new name!"
return
fi
local project="$1"
local site_dir="$site_root/$project"
local www_dir="$site_root/$project/www"
# adding project name in case they differ
do_hook "$project" "before-create" "$project_name"
default_template="$(<_conf/default-template.conf)"
while [[ -z "$template" ]]; do
default_template=${default_template-"johnrom/nimble-wp-template"}
ask template "Please choose a template:" "$default_template" --required
done
local template_dir="$template_root/$template"
get_template "$template"
if [ ! -f "$template_dir/template.yml" ]; then
echo "Invalid Template! Please check the git repo name"
exit 1
fi
# create directories
#
if [ -d "$site_dir" ]; then
echo "Error: Folder already exists for project: $project. Exiting!"
exit 1
fi
echo "creating project directory: $project"
mkdir -p $www_dir
echo "$template" > "$site_dir/template.conf"
cd $project_root
# adding project name in case they differ
do_hook "$project" "create" "$project_name"
# update dev config
#
echo "Adding .yml file"
local dev_template=$(<$template_dir/template.yml)
dev_template=${dev_template//PROJECT/$project}
dev_template=${dev_template//TLD/$tld}
echo "$dev_template" > "$site_dir/$project.yml"
if confirm "Do you want this project kept in git?" Y; then
git add -f "$site_dir/template.conf"
git add -f "$site_dir/$project.yml"
else
echo "$certs_root/$project.$tld.crt" >> .gitignore
echo "$certs_root/$project.$tld.key" >> .gitignore
fi
# adding project name in case they differ
do_hook "$project" "after-create" "$project_name"
# starting docker-compose in detached mode
up
init $project
if has_hook "$project" install; then
if confirm "Do you want to run the initial setup?" Y; then
install "$project"
fi
fi
}
setup(){
localize
if [ ! -f "$site_root/_front/front.yml" ]; then
create_front
fi
}
up(){
echo "emptying docker-compose.yml"
> docker-compose.yml
echo "emptying docker-common.yml"
> docker-common.yml
local project
local valid=0
if [[ -z "$1" ]]; then
eval "$(env)"
else
eval "$(env $1)"
fi
for project in $(ls -d $site_root/*)
do
local project_raw_name="$(basename $project)"
local project_name=${project_raw_name//"_"/""}
echo "Processing project: $project_name"
if [[ ! -f $project/$project_name.yml ]]; then
echo "Warning: $project does not have a valid .yml file. Skipping!"
else
if [[ -f "$project/template.conf" ]]; then
# I am tired
local project_template="$(<$project/template.conf)"
get_template "$project_template"
fi
# docker does not like relative directories
local this_template=$(<$project/$project_name.yml)
this_template=${this_template//SITEROOT/"$site_root_vm/$project_raw_name"}
this_template=${this_template//NIMBLE/"$nimble_root_vm"}
this_template=${this_template//NIMCMD/"$nimble_root_command_line"}
this_template=${this_template//IMAGES/"$images_root_command_line"}
this_template=${this_template//COMMON/"$nimble_root_command_line/docker-common.yml"}
echo "$this_template" >> "docker-compose.yml"
valid=1
do_hook "$project_name" "before-up"
fi
done
echo "Processing Common Template"
for owner in $(ls -d $template_root/*)
do
local owner_name="$(basename $owner)"
for template_directory in $(ls -d $template_root/$owner_name/*)
do
local repo_name="$(basename $template_directory)"
echo "Processing template: $owner_name/$repo_name"
if [[ ! -f "$template_directory/common.yml" ]]; then
echo "Warning: $owner_name/$repo_name does not have a valid common file. Skipping!"
else
# quick hack because docker for windows does not like relative directories
local this_template=$(<"$template_directory/common.yml")
this_template=${this_template//NIMBLE/"$nimble_root"}
this_template=${this_template//IMAGES/"$images_root_command_line"}
if is_cygwin; then
this_template=${this_template//"/mnt/f/"/"/f/"}
this_template=${this_template//"/mnt/c/"/"/f/"}
fi
echo "$this_template" >> "$nimble_root/docker-common.yml"
fi
done
done
if [[ $valid = 1 ]]; then
echo "Common templates assembled. Starting docker-compose in detached mode"
if [ -z "$1" ] || ! [ "$1" = "attach" ]; then
local command="docker-compose up -d"
else
local command="docker-compose up"
fi
$command
else
echo "Did not find any valid projects! Did you run setup?: nimble setup"
fi
}
down(){
echo "stopping docker-compose"
docker-compose down >& /dev/null
echo "emptying docker-compose.yml"
> docker-compose.yml
echo "removing old cachegrind files"
local directory
for directory in $(ls "$site_root"); do
if [ -d $site_root/"$directory" ]; then
rm $site_root/"$directory"/cachegrind/* >& /dev/null
fi
done
return 0
}
update(){
echo "making sure containers are up"
up >& /dev/null
echo "stopping containers without emptying docker-compose"
docker-compose down >& /dev/null
echo "rebuilding local images"
docker-compose build
echo "updating containers"
docker-compose pull
up
}
restart(){
down
up
}
install(){
do_hook "$1" install
}
run(){
do_hook "$1" run "$@"
}
migrate() {
local project=$1
local inner_dir="/var/www/html"
local url="$project.$tld"
local remote
if [[ -z $project ]];
then
echo "Usage: nimble migrate \$project"
exit 1
fi
while true; do
# Ask the question - use /dev/tty in case stdin is redirected from somewhere else
read -p "Remote Domain [empty to continue, use www THEN non-www versions]: " remote </dev/tty
# Default?
if [[ -z "$remote" ]]; then
return 0
fi
echo $remote
echo $url
echo "docker exec -i $project /bin/bash -c wp search-replace $remote $url"
docker exec -i "$project" /bin/bash -c "wp search-replace $remote $url"
done
}
cert() {
if [[ -z "$1" ]]; then
help
return
fi
local project=$1
openssl req \
-newkey rsa:2048 -nodes \
-subj "//C=US\ST=Pennsylvania\L=Philadelphia\O=MYORGANIZATION\CN=$project.$tld" \
-keyout $PWD/_conf/certs/"$project.$tld.key" \
-x509 -days 365 -out $PWD/_conf/certs/"$project.$tld.crt"
}
npm_install() {
if confirm "Do you want to install NPM? It could take a while..." N; then
# Install NPM Dependencies
#
echo "Installing NPM Dependencies"
npm install
fi
}
init() {
if [[ -z "$1" ]]; then
help
return 1
elif [ "$1" == "all" ]; then
echo "'all' is not a valid name for a project! Unfortunately, it conflicts with other things. Try a new name!"
return 1
fi
local project="$1"
local root="$nimble_root"
local dir="$site_root/$project/www"
local inner_dir="/var/www/html"
if confirm "Do you want to add this project to your hosts?" Y; then
hosts $project
fi
echo "Doing Init for $project"
do_hook "$project" "init"
if confirm "Do you want to generate certificates for this site?" Y; then
cert $project
fi
echo "Restarting containers"
restart
}
clone() {
if [[ -z "$1" ]]; then
help
return
elif [ "$1" == "all" ]; then
echo "'all' is not a valid name for a project! Unfortunately, it conflicts with other things. Try a new name!"
return
fi
local project="$1"
local dir="$site_root/$project/www"
cd $dir
if [[ -z "$2" ]]; then
# Pull a repo
ask repo "Repo URL (https://github.com/user/$project.git)" "" --required
else
local repo="$2"
fi
# initialize
git init
# set remote
git remote add -t \* -f origin "$repo"
# checkout main
git checkout -f main
cd $nimble_root
}
env() {
local profile
if [ -z "$1" ]; then
profile="profiler_enable=0"
elif [ "$1" == "profile" ]; then
profile="profiler_enable=1"
elif [ "$1" == "trigger" ]; then
profile="profiler_enable_trigger=1"
fi
# run me before docker-compose up -d in order to set your env variable
# for PHP to hook up to local xdebug listeners using remote_host
# with docker beta we are just using the default docker network
local ip="host.docker.internal"
echo "export XDEBUG_CONFIG=\"remote_host=$ip remote_autostart=1 $profile\""
echo "# to use this function to set up xdebug, use \`eval \$($command env \$machine_name)\`"
echo "# \$machine_name is usually \`default\`, and is an optional parameter"
}
hosts() {
# requires project name
if [[ -z "$1" ]]; then
help
return
fi
local project=$1
# check Git Bash hosts location
local file="/c/Windows/System32/drivers/etc/hosts"
if [ ! -f $file ]; then
# check Ubuntu for Windows Bash hosts location
local file="/mnt/c/Windows/System32/drivers/etc/hosts"
if [ ! -f $file ]; then
# check Mac hosts location
local file="/private/etc/hosts"
if [ ! -f $file ]; then
echo "Cannot find hosts file"
return
fi
fi
fi
local ip="10.0.75.2"
# mac forwards loopback instead of using nat
if [ "$(uname)" == "Darwin" ]; then
local ip="127.0.0.1"
if [ $EUID != 0 ]; then
sudo "$0" hosts "$@"
return $?
fi
fi
rmhosts $project
# Editing Hosts
#
echo "Adding $project.$tld to hosts file at $ip"
echo "Adding phpmyadmin.$project.$tld to hosts file at $ip"
echo "Adding webgrind.$project.$tld to hosts file at $ip"
x=$(tail -c 1 "$file")
if [ "$x" != "" ]
then
echo "" >> $file
fi
echo "$ip $project.$tld" >> "$file"
echo "$ip phpmyadmin.$project.$tld" >> "$file"
echo "$ip webgrind.$project.$tld" >> "$file"
}
rmhosts() {
# requires project name
if [[ -z "$1" ]]; then
help
return
fi
local file="/c/Windows/System32/drivers/etc/hosts"
if [ ! -f $file ]; then
local file="/mnt/c/Windows/System32/drivers/etc/hosts"
if [ ! -f $file ]; then
local file="/private/etc/hosts"
if [ ! -f $file ]; then
echo "Cannot find hosts file"
return
fi
fi
fi
local project=$1
local tab="$(printf '\t') "
echo "Removing $project from $file"
grep -vE "\s((phpmyadmin|webgrind)\.)?$project\.$tld" "$file" > hosts.tmp && cat hosts.tmp > "$file"
rm hosts.tmp
}
clear() {
echo "Removing docker containers"
docker rm -f $(docker ps -a -q)
if [ "$1" == "all" ]; then
echo "Removing docker images"
docker rmi -f $(docker images -q)
fi
}
clean() {
if [ "$1" == "volumes" ]; then
echo "Cleaning old volumes"
echo "Making sure docker is running so we register dangling volumes properly"
docker-compose up -d
if [ -z "$(docker volume ls -qf dangling=true)" ]; then
echo "There are no orphaned docker volumes to clean"
else
echo "Verify the following volumes for deletion:"
docker volume ls -qf dangling=true
if confirm "Do all of these volumes look correct?! If any look like they are still necessary, don't proceed." N; then
docker volume rm $(docker volume ls -qf dangling=true)
fi
fi
fi
}
delete_one() {
local project="$1"
local dir="$site_root/$project/www"
# Shut down docker-compose