forked from nahakiole/cloudrexx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcx
executable file
·1936 lines (1807 loc) · 77.9 KB
/
cx
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
@GOTO WIN \
2>/dev/null
#!/bin/bash
# vim: syn=sh
# Wrapper for bash 3
if [ "${BASH_VERSION:0:1}" -lt 4 ]; then
command="cd /root && ./cx --proxy-host-dir='$(pwd -P)' $@"
# Special case: user wants to start interactive shell to wrapper container
if [[ "$@" == "env shell --wrapper" ]]; then
command="cd /root && bash"
fi
# Pass everything through our wrapper
docker run --rm -ti --name clx-win-wrapper --user=$(id -u):$(stat -c '%g' /var/run/docker.sock) -v "$(pwd -P)/":/root -v /var/run/docker.sock:/var/run/docker.sock cloudrexx/ubuntu bash -c "$command"
exit 0
fi
# URL to download this script from
DOWNLOAD_URL="https://raw.githubusercontent.com/Cloudrexx/cloudrexx/master/cx"
# current command, sub-command, sub-sub-command, etc.:
COMMANDS=()
# list of arguments, including default values
declare -A ARGUMENTS
# list of arguments set by user:
declare -A FORCED_ARGUMENTS
# list of default environment configs:
declare -A DEFAULT_CONFIG
# this env's config:
declare -A CONFIG
# list of commands which are handled completely within this script:
INTERNAL_COMMANDS=("envs" "env" "debug")
# proxy image
PROXY_IMAGE="cloudrexx/web:proxy"
# temp files:
declare -A TEMP_FILES
trap clean_temp_files EXIT
function clean_temp_files {
for tempfile in "${!TEMP_FILES[@]}"; do
rm -f "${TEMP_FILES[$tempfile]}"
done
}
#######################
# Input param cleanup #
#######################
# default values
DEFAULT_CONFIG=(
["port"]=80
["hostname"]=""
["php-version"]=""
["db-image"]="mariadb:10.1"
["php-image"]="cloudrexx/web:PHP<php-version>-with-mysql"
["db-name"]="dev"
["db-pass"]="123456"
["db-user"]="root"
["db-host"]="db"
)
ARGUMENTS=(
["port"]=80
["ssl-port"]=443
["php-image"]="cloudrexx/web:PHP<php-version>-with-mysql"
["db-image"]="mariadb:10.1"
["source-repo"]="https://github.com/Cloudrexx/cloudrexx.git"
["source-branch"]="master"
["scripts-repo"]="https://github.com/Cloudrexx/scripts.git"
["scripts-branch"]="master"
["root-domain"]="lvh.me"
)
# iterate over all arguments to separate commands and arguments
for arg in "$@"
do
if [[ "$arg" == --* ]]; then
arg="${arg:2:${#arg}-2}"
if [[ "$arg" = *=* ]]; then
IFS='='; read -r key value <<< "$arg"
ARGUMENTS[$key]=$value
FORCED_ARGUMENTS[$key]=$value
else
ARGUMENTS[$arg]=true
fi
else
COMMANDS+=("$arg")
fi
done
##################
# "help" command #
##################
function cmd_help {
case "$1" in
envs)
case "$2" in
up)
echo "Starts the multi-vhost environment.
Synopsis:
./cx envs up [<options>]
Available options are:
--certs-dir=<dir> Directory to get SSL certificates from.
--ssl-port=<port> Port for HTTPS connections. Defaults to 443.
--port=<port> Port on which the vhosts are available."
;;
down)
echo "Stops the multi-vhost environment.
Synopsis:
./cx envs down"
;;
restart)
echo "Restarts the multi-vhost environment.
Synopsis:
./cx envs restart"
;;
status)
echo "Shows the status of the multi-vhost environment.
Synopsis:
./cx envs status"
;;
info)
echo "Shows the status of all vhosts' containers.
Synopsis:
./cx envs info"
;;
debug)
echo "Shows the logs of the nginx proxy.
Synopsis:
./cx envs debug"
;;
list)
echo "Shows a list of all attached environments.
Synopsis:
./cx envs list [<options>]
Available options are:
--dir=<directory> Shows the name of the environment(s) tied to a directory.
--all Also lists environments which are not attached to the proxy.
--real Shows docker-compose's project name instead of environment's name"
;;
locate)
echo "Shows the working directory of an environment.
Synopsis:
./cx envs locate <environment> [<options>]
Available options are:
--real Expects environment name to be docker-compose's project name"
;;
shell)
echo "This command opens an interactive shell of the NGINX proxy docker container.
Synopsis
./cx envs shell"
;;
exec)
echo "Executes the given subcommand for 'env' on each environment.
Synopsis
./cx envs exec <command> [<options>]
Available options are:
--silent Does not output current environment's name
--real Shows environment name as docker-compose's project name"
;;
*)
echo "This command manages the environment for multiple Cloudrexx installations on one host.
Synopsis
./cx envs <subcommand> [<options>]
Available subcommands are:
up Starts the docker containers
down Stops the docker containers
restart Restarts the docker containers
status Tells whether the containers are running
info Alias for 'status'
shell Interactive shell of the proxy container
debug Shows NGINX logs
list Lists all environments that are up
locate Show an environments working directory
For more info about a subcommand type ./cx help envs <subcommand>";
;;
esac
;;
env)
case "$2" in
init)
echo "This command (re-)initializes a Cloudrexx installation and its environment.
Basically it does the following:
- Checks out main source repository
- Checks out helper scripts repository
- Generates docker configuration (docker-compose.yml)
- Starts necessary docker containers
- Loads database
Synopsis
./cx env init [<options>]
Available options are:
--yes Answer all questions with yes. Use with care!
--silent Do not output non-necessary messages
--scale=<int> Sets the number of PHP-Containers to spawn. Useful for testing.
--skip-source Skips GIT clone for source code repository
--source-repo=<repoUrl> GIT-URL for the source code repository. Default is https://github.com/Cloudrexx/cloudrexx
--source-branch=<branchName> GIT branch to check out of the source code repository. Default is master.
--skip-scripts Skips GIT clone for scripts repository
--scripts-dir=<dir> Locale directory to use as scripts directory instead of cloning from GIT repository
--scripts-repo=<repoUrl> GIT-URL for the scripts repository. Default is https://github.com/Cloudrexx/scripts
--scripts-branch=<branchName> GIT branch to check out of the scripts repository. Default is master.
--skip-database Does not initialize database.
In addition all options of ./cx env config can be used, see ./cx help env config."
;;
up)
echo "This command starts the environment for your installation.
Synopsis
./cx env up"
;;
down)
echo "This command stops the environment for your installation.
Synopsis
./cx env down [<options>]
Available options are:
--purge Drops docker volumes associated with this environment. WARNING: You may loose data!"
;;
restart)
echo "This command restarts the environment for your installation.
Synopsis
./cx env restart"
;;
status)
echo "This command shows if the environment for your installation is up and running.
Synopsis
./cx env status"
;;
info)
echo "This command shows the status and configuration of the environment for your installation.
Synopsis
./cx env info"
;;
update)
echo "This command updates your installation.
Synopsis
./cx env update [<options>]
Available options are:
--yes Answer all questions with yes. Use with care!
--silent Do not output non-necessary messages
--db If set without setting --git or --docker only database is updated, implies --force
--git If set without setting --db or --docker only GIT is updated
--docker If set without setting --db or --git only docker images are updated
--force Forces reset of the database even if dump has not changed
--reset Resets local GIT clone to origin's state
--drop-sessions Skips restoring user sessions
--drop-users Skips restoring users, implies --drop-sessions"
;;
config)
echo "This command configures your installation.
Default behavior:
- If configuration does not yet exist, use default values (or arguments, if set)
- If configuration exists, only change values in arguments
The option --interactive forces the command to ask for each value
Synopsis
./cx env config [<options>]
Available options are:
--show Displays current configuration and exits.
--yes Answer all questions with yes / default. Use with care!
--silent Do not output non-necessary messages
--interactive Ask for each value instead of using default behavior
--force-default Forces default (/automatic) configuration
--db-host=<hostname> Sets database host to <hostname>
--db-name=<dbname> Sets database name to <dbname>
--db-user=<username> Sets database user to <username>
--db-pass=<password> Sets database password to <password>
--php-image=<dockerImageName> Name of the docker image to use. Default is 'cloudrexx/web:PHP<php_version>-with-mysql'.
--db-image=<dockerImageName> Name of the docker image to use. Default is 'mariadb'.
--php-version=<php-version> Sets PHP version to <php-version> (example: 7.2), default is based on Cloudrexx version
--hostname=<hostname> Sets installation hostname to <hostname>, defaults to parent directory name
--port=<port> Sets installation port to <port>, defaults to 80"
;;
shell)
echo "This command opens an interactive shell of the PHP docker container for this environment.
Synopsis
./cx env shell [<options>]
Available options are:
--root Opens an interactive root shell on the web container
--wrapper Opens an interactive shell of the Windows wrapper container instead (Windows only)"
;;
exec)
echo "Allows to execute code (Bash/SQL) on containers of this environment.
Synopsis
./cx env exec [<options>] <code>
Available options are:
--root Execute Bash as root
--sql Execute SQL on database container instead of Bash on web container.
--sql-file Execute SQL from a file on database container instead of Bash on web container."
;;
component)
case "$3" in
list)
echo "Lists additional components.
Synopsis
./cx env component list <options>
Available options are:
--directories List the component's directories
--raw Does not shiny-print the output"
;;
add)
echo "Adds an additional component.
Synopsis
./cx env component add <componentType> <componentName> <gitRepository> (<options>)
Argument description:
componentType One of 'core', 'core_module', 'module', 'theme', 'lib'
componentName Name of the component
gitRepository GIT remote URL
options See options below
Available options:
--skip-source Assume GIT clone is already done. <gitRepository> is optional when using this option.
--skip-db Assume database is already updated and component active."
;;
remove)
echo "Removes an additional component.
Synopsis
./cx env component remove <componentType> <componentName>
Argument description:
componentType One of 'core', 'core_module', 'module', 'theme', 'lib'
componentName Name of the component"
;;
update)
echo "Updates an additional component. Add a Data.sql or a OldStructureWithTestData.sql and a Migration.sql file to the Data directory of your component.
Synopsis
./cx env component update <componentType> <componentName> (<options>)
Argument description:
componentType One of 'core', 'core_module', 'module', 'theme', 'lib'
componentName Name of the component
options See options below
Available options:
--db Reset component database
--git Update/pull GIT repository"
;;
*)
echo "Manages additional components.
Synopsis
./cx env component <subcommand> <arguments>
Available subcommands are:
list Lists registered additional components
add Adds an additional component
remove Removes a registered component
update Updates a registered component"
;;
esac
;;
remote)
case "$3" in
set)
echo "Links this installation to a remote site
Synopsis
./cx env remote set <remote> (--snapshot-repo=<repo_url>)
Parameter explanations:
<remote> Name of the remote site to use
<repo_url> URL of the snapshot repository to use. <site> can be used as a placeholder for <remote> in the URL."
;;
unset)
echo "Removes link to a remote site
Synopsis
./cx env remote unset"
;;
show)
echo "Shows to which remote site this installation is linked
Synopsis
./cx env remote show"
;;
pull)
echo "Pulls and installs newest snapshot from linked remote site
Synopsis
./cx env remote pull <arguments>
Available arguments:
(none yet, more to come)"
;;
*)
echo "Manages link to a remote site
Synopsis
./cx env remote <subcommand> <arguments>
Available subcommands are:
set Sets the remote to use
unset Removes link to remote
show Shows which remote is currently linked
pull Pulls and installs newest snapshot from remote"
;;
esac
;;
*)
echo "This command manages a Cloudrexx installation and its environment.
Synopsis
./cx env <subcommand> [<options>]
Available subcommands are:
init (Re-)Initializes the environment
up Starts the necessary docker containers
down Stops the necessary docker containers
restart Restarts the necessary docker containers
status Checks if all necessary docker containers are running
info Calls 'status' and prints environment information
update Updates docker images and GIT and reloads database
config Configures Cloudrexx (config/configuration.php)
shell Interactive shell of the current environment's web container
component Manages additional components
remote Manages remote installations
exec Executes code (Bash/SQL) for the current environment
For more info about a subcommand type ./cx help env <subcommand>";
;;
esac
;;
help)
echo "Shows this help"
;;
debug)
echo "Shows debug output. By default it outputs the last fatal error.
Synopsis
./cx debug [<options>]
Available options are:
--web Shows the log of the web docker container (Apache)
--request Shows the log of the last request
--stack Shows the stack trace of the last failed request
--clear-usercache Clears the user cache
--install-x-debug Installs xDebug inside the web container
--follow Follows the log"
;;
*)
echo "Executes commands for Cloudrexx installations.
Synopsis
./cx <command> [<subcommand> [...]] [<cmd_options>] [<cmd_arguments>]
The following commands are available:
envs Manages development environments
env Manages a development environment
help Shows this help
debug Returns the last fatal error"
REGEX="(\S+) - ?(.*)"
HELP_LIST="$(phpexec | grep $'^\t' 2> /dev/null)"
spacepad=" "
while read -r line; do
[[ "$line" =~ $REGEX ]]
echo " ${BASH_REMATCH[1]}${spacepad:${#BASH_REMATCH[1]}}${BASH_REMATCH[2]}"
done <<< "$HELP_LIST"
echo "
For more info about a command type ./cx help <command>"
;;
esac
}
##################
# "envs" command #
##################
function cmd_envs {
case "$1" in
up)
if ! isPortFree ${ARGUMENTS["port"]}; then
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "TCP port ${ARGUMENTS["port"]} is in use. Stop any service running on this port and try again."
fi
exit 1
fi
local environments env_dirs
ARGUMENTS["real"]=true
ARGUMENTS["all"]=true
IFS=$'\n'
set -f
environments=($(cmd_envs list))
set +f
# TODO: Only list environments on the same port
if [ "${#environments[@]}" -gt "0" ]; then
if [[ ${ARGUMENTS["yes"]} != true ]]; then
echo "The following environments are up. Shall they be adopted?"
for environment in "${environments[@]}"; do
echo " $environment"
done
read -p "Adopt environments? [Yn]? " -n 1 -r
echo ""
fi
if [[ ${ARGUMENTS["yes"]} ]] || [[ "$REPLY" == "" ]] || [[ "$REPLY" =~ ^[Yy]$ ]]; then
for environment in "${environments[@]}"; do
env_dirs+=($(cmd_envs locate "$environment"))
ARGUMENTS["name"]=$environment
cmd_env down
done
else
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Aborting"
fi
exit
fi
fi
dockerexec network create "<proxy-network>"
if [[ "${ARGUMENTS["certs-dir"]}" != "" ]]; then
dockerexec run --restart=always --name "<proxy-container>" --net "nginx-proxy" -d -p "${ARGUMENTS["port"]}:80" -p "${ARGUMENTS["ssl-port"]}":443 -v "${ARGUMENTS["certs-dir"]}":/etc/nginx/certs:ro -v /var/run/docker.sock:/tmp/docker.sock:ro "$PROXY_IMAGE"
else
dockerexec run --restart=always --name "<proxy-container>" --net "nginx-proxy" -d -p "${ARGUMENTS["port"]}:80" -v /var/run/docker.sock:/tmp/docker.sock:ro "$PROXY_IMAGE"
fi
for env_dir in "${env_dirs[@]}"; do
(
cd "$env_dir"
cmd_env config
cmd_env up
)
done
;;
down)
local environments env_dir
ARGUMENTS["real"]=true
IFS=$'\n'
set -f
environments=($(cmd_envs list))
set +f
if [ "${#environments[@]}" -gt "0" ]; then
if [[ ${ARGUMENTS["yes"]} != true ]]; then
echo "The following environments are still up. Shall they be shut down?"
for environment in "${environments[@]}"; do
echo " $environment"
done
read -p "Shut environments down? [Yn]? " -n 1 -r
echo ""
fi
if [[ ${ARGUMENTS["yes"]} ]] || [[ "$REPLY" == "" ]] || [[ "$REPLY" =~ ^[Yy]$ ]]; then
if [ "${#environments[@]}" -eq "1" ]; then
ARGUMENTS["real"]=true
env_dir=$(cmd_envs locate "${environments[0]}")
fi
for environment in "${environments[@]}"; do
ARGUMENTS["name"]=$environment
cmd_env down
done
else
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Aborting"
fi
exit
fi
fi
dockerexec stop "<proxy-container>"
dockerexec rm "<proxy-container>"
dockerexec network rm "<proxy-network>"
if [ "${#environments[@]}" -eq "1" ]; then
if [[ ${ARGUMENTS["yes"]} != true ]]; then
echo "Should the environment \"${environments[0]}\" be restarted as a standalone environment?"
read -p "Reconfigure and start environment? [Yn]? " -n 1 -r
echo ""
fi
if [[ ${ARGUMENTS["yes"]} ]] || [[ "$REPLY" == "" ]] || [[ "$REPLY" =~ ^[Yy]$ ]]; then
(
cd "$env_dir"
cmd_env config
cmd_env up
)
fi
fi
;;
restart)
cmd_envs down
cmd_envs up
;;
status)
dockerexec ps -f "name=<proxy-container>"
;;
info)
dockerexec ps -f "name=<container-prefix>*"
;;
debug)
dockerexec logs -f "<proxy-container>"
;;
shell)
dockerexec exec -ti "<proxy-container>" bash
;;
list)
local containerlist
if [[ "${ARGUMENTS["dir"]}" != "" ]]; then
containerlist=$(
while read -r container; do
if [[ "$container" == "" ]]; then
:
elif [[ "$(docker inspect "$container" | grep "${ARGUMENTS["dir"]}:")" != "" ]]; then
echo "$container"
fi
done <<< "$(docker ps --format '{{.Names}}')"
)
elif [[ ${ARGUMENTS["all"]} ]]; then
containerlist=$(dockerexec ps -f "name=<container-prefix>" --format "{{.Names}}" | grep web)
else
containerlist=$(dockerexec ps -f "name=<container-prefix>" -f "network=<proxy-network>" --format "{{.Names}}" | grep web)
fi
if ! [[ ${ARGUMENTS["real"]} ]]; then
containerlist="${containerlist//"clxenv"/}"
containerlist="${containerlist//"${ARGUMENTS["root-domain"]//./}_web_1"/}"
fi
containerlist="${containerlist//"_web_1"/}"
if [[ "$containerlist" == "" ]]; then
return 1
fi
echo "$containerlist"
;;
locate)
local mountline environment
environment=$2
if [[ ${ARGUMENTS["real"]} != true ]]; then
environment="clxenv$2${ARGUMENTS["root-domain"]//./}"
fi
mountline=$(docker inspect "${environment}_web_1" | grep ":/var/www/html:")
REGEX="\"([^:]+):"
[[ "$mountline" =~ $REGEX ]]
echo "${BASH_REMATCH[1]}"
;;
exec)
ARGUMENTS["real"]=true
envs="$(cmd_envs list)"
if [[ "$envs" == "" ]]; then
echo "No env up"
return
fi
shift
# TODO: Allow interactive decision whether to execute command on this env
first=true
ORIG_ARGS=("${ORIG_ARGS[@]:2}")
while read env; do
if "$first"; then
first=false
else
echo ""
fi
prevPwd="$(pwd -P)"
cd "$(cmd_envs locate "$env")" || continue
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Execute env command for $env"
fi
./cx env "${ORIG_ARGS[@]}"
cd "$prevPwd"
done <<< "$envs"
;;
*)
(>&2 echo "No such subcommand")
;;
esac
}
#################
# "env" command #
#################
function cmd_env {
case "$1" in
init)
if [[ ${ARGUMENTS["yes"]} != true ]]; then
echo "This will install Cloudrexx in the current directory ($(get_real_pwd))"
read -p "Continue [Yn]? " -n 1 -r
echo ""
if [[ "$REPLY" != "" ]] && ! [[ "$REPLY" =~ ^[Yy]$ ]]; then
return
fi
fi
if [[ ${ARGUMENTS["skip-source"]} != true || ${ARGUMENTS["skip-scripts"]} != true ]] && [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Checking out source and scripts repository"
fi
if [[ ${ARGUMENTS["skip-source"]} != true ]]; then
# We do not simply clone because ./cx already exists
git init -q
git remote add origin "${ARGUMENTS["source-repo"]}"
if [[ ${ARGUMENTS["silent"]} == true ]]; then
git fetch --tags -q
else
git fetch --tags
fi
git checkout -t -f "origin/${ARGUMENTS["source-branch"]}"
# assume ./tmp/* and ./config/* are unchanged
git ls-files -z tmp/ | xargs -0 git update-index --assume-unchanged
git ls-files -z config/ | xargs -0 git update-index --assume-unchanged
fi
if [[ ${ARGUMENTS["skip-scripts"]} != true ]]; then
if [[ ${ARGUMENTS["scripts-dir"]} != "" ]]; then
ln -s "${ARGUMENTS["scripts-dir"]}" "_meta"
else
if [[ ${ARGUMENTS["silent"]} == true ]]; then
git clone -q "${ARGUMENTS["scripts-repo"]}" --branch="${ARGUMENTS["scripts-branch"]}" _meta
else
git clone "${ARGUMENTS["scripts-repo"]}" --branch="${ARGUMENTS["scripts-branch"]}" _meta
fi
fi
fi
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Configuring the environment"
fi
ARGUMENTS["force-default"]=true && cmd_env config
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Loading docker containers"
fi
local result
result="$(cmd_env up)"
if [[ "$?" != "0" ]]; then
echo "$result"
echo "In order to retry, please run ./cx env init --skip-scripts --skip-source"
exit
fi
if [[ ${ARGUMENTS["skip-database"]} != true ]]; then
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Loading database"
fi
ARGUMENTS["db"]=true && ARGUMENTS["drop-users"]=true && cmd_env update
fi
ARGUMENTS["force-default"]=false && cmd_env config
echo "$result"
;;
up)
get_config_array
if [[ ${CONFIG["is-vhost"]} != true ]]; then
if ! isPortFree "${CONFIG["port"]}"; then
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "TCP port ${CONFIG["port"]} is in use. Stop any service running on this port and try again."
fi
exit 1
fi
fi
ARGUMENTS["dir"]="$(get_real_pwd)"
envname="$(cmd_envs list)"
if [[ "$?" == "0" ]]; then
echo "There's already an environment up and running (\"$envname\") for this directory."
exit 2
fi
[ -d ./tmp/db ] || mkdir -p ./tmp/db
if [[ ${ARGUMENTS["silent"]} != true ]]; then
dockercomposeexec up -d
echo "Waiting for database server to come up..."
else
dockercomposeexec up -d &> /dev/null
fi
dockerexec exec "<db-container>" bash -c "while ! echo | mysql -u${CONFIG["db-user"]} -p${CONFIG["db-pass"]} &> /dev/null; do sleep 1; done"
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Applying configuration to new environment"
fi
phpexec config --force
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Environment successfully initialized. It is available at http://${CONFIG["hostname"]}/"
if [[ ${CONFIG["is-vhost"]} == true ]]; then
echo "phpMyAdmin is available at http://phpma.${CONFIG["hostname"]}/"
echo "MailHog is available at http://mail.${CONFIG["hostname"]}/"
else
echo "phpMyAdmin is available at http://${CONFIG["hostname"]}:8234/"
echo "MailHog is available at http://${CONFIG["hostname"]}:8025/"
fi
if ! which cx > /dev/null; then
echo ""
echo "cx could not be found in your path. Run the following command to add it:"
echo "sudo cp $(get_real_pwd)/cx /usr/local/bin"
fi
fi
exit 0
;;
down)
local environments
if [[ "${ARGUMENTS["name"]}" != "" ]]; then
environments=(${ARGUMENTS["name"]})
else
ARGUMENTS["dir"]="$(get_real_pwd)"
ARGUMENTS["real"]=true
IFS=$'\n'
set -f
environments=($(cmd_envs list))
set +f
fi
get_config_array
environments+=("clxenv$(getenvname)")
environments=($(tr ' ' '\n' <<< "${environments[@]}" | sort -u | tr '\n' ' '))
for environment in "${environments[@]}"; do
echo "Shutting down environment $environment"
if [[ "${ARGUMENTS["purge"]}" == true ]]; then
docker-compose --project-name "$environment" down -v
else
docker-compose --project-name "$environment" down
fi
done
;;
restart)
cmd_env down
cmd_env up
;;
status)
if [[ ${ARGUMENTS["q"]} == true ]]; then
exit $(test -n "$(dockercomposeexec ps -q)")
else
dockercomposeexec ps
fi
;;
info)
cmd_env status
ARGUMENTS["show"]=true && cmd_env config
;;
update)
STRUCTMD5=$(/usr/bin/md5sum installer/data/contrexx_dump_structure.sql)
DATAMD5=$(/usr/bin/md5sum installer/data/contrexx_dump_data.sql)
DB_UPDATE_NECESSARY=false
get_config_array
if [[ ${ARGUMENTS["db"]} != true && ${ARGUMENTS["docker"]} != true ]] || [[ ${ARGUMENTS["git"]} = true ]]; then
if [[ ${ARGUMENTS["reset"]} == true ]]; then
if [[ ${ARGUMENTS["yes"]} != true ]]; then
echo "This will remove all local changes. Are you sure?"
read -p "Continue [Ny]? " -n 1 -r
echo ""
if ! [[ "$REPLY" =~ ^[Yy]$ ]]; then
return
fi
fi
git fetch
git reset --hard
git clean -ffdx
set_config_array
else
git stash
git pull
git stash pop
fi
STRUCTMD5NEW=$(/usr/bin/md5sum installer/data/contrexx_dump_structure.sql)
DATAMD5NEW=$(/usr/bin/md5sum installer/data/contrexx_dump_data.sql)
if [[ "$STRUCTMD5NEW" != "$STRUCTMD5" || "$DATAMD5NEW" != "$DATAMD5" ]]; then
DB_UPDATE_NECESSARY=true
else
DB_UPDATE_NECESSARY=false
fi
(cd "_meta"; git stash && git pull && git stash pop)
cmd_env config
fi
if [[ ${ARGUMENTS["db"]} != true && ${ARGUMENTS["git"]} != true ]] || [[ ${ARGUMENTS["docker"]} = true ]]; then
dockercomposeexec pull
dockercomposeexec restart
fi
if [[ ${ARGUMENTS["git"]} = true || ${ARGUMENTS["docker"]} = true ]] && [[ ${ARGUMENTS["db"]} != true ]]; then
if [[ $DB_UPDATE_NECESSARY = true && ${ARGUMENTS["silent"]} != true ]]; then
echo "DB update would be necessary, you may want to run ./cx env update --db"
fi
return
fi
if [[ ${ARGUMENTS["force"]} != true && ${ARGUMENTS["db"]} != true && $DB_UPDATE_NECESSARY != true && ${ARGUMENTS["silent"]} != true ]]; then
echo "DB update not necessary, run ./cx env update --db --force if you want to force db reload"
return
fi
# create login file
mysql_create_tempfile
if [[ ${ARGUMENTS["drop-users"]} != true ]]; then
TEMP_FILES["user-backup"]="$(mktemp)"
mysql_dump --no-create-info --skip-triggers "${CONFIG["db-name"]}" contrexx_access_users contrexx_access_user_profile > "${TEMP_FILES["user-backup"]}"
mysql_dump --no-create-info --skip-triggers -w 'attribute_id=0' "${CONFIG["db-name"]}" contrexx_access_user_attribute_value >> "${TEMP_FILES["user-backup"]}"
if [[ ${ARGUMENTS["drop-sessions"]} != true ]]; then
TEMP_FILES["session-backup"]="$(mktemp)"
mysql_dump --no-create-info --skip-triggers "${CONFIG["db-name"]}" contrexx_sessions > "${TEMP_FILES["session-backup"]}"
fi
fi
# empty database if exists
# from https://stackoverflow.com/questions/12403662/how-to-remove-all-mysql-tables-from-the-command-line-without-drop-database-permi
mysql_exec "
SET FOREIGN_KEY_CHECKS = 0;
SET GROUP_CONCAT_MAX_LEN=32768;
SET @tables = NULL;
SELECT GROUP_CONCAT('\`', table_name, '\`') INTO @tables
FROM information_schema.tables
WHERE table_schema = (SELECT DATABASE()) AND table_name LIKE '${CONFIG["db-prefix"]}%';
SELECT IFNULL(@tables,'dummy') INTO @tables;
SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @views = NULL;
SELECT GROUP_CONCAT('\`', TABLE_NAME, '\`') INTO @views
FROM information_schema.views
WHERE table_schema = (SELECT DATABASE()) AND table_name LIKE '${CONFIG["db-prefix"]}%';
SELECT IFNULL(@views,'dummy') INTO @views;
SET @views = CONCAT('DROP VIEW IF EXISTS ', @views);
PREPARE stmt FROM @views;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;"
# create db if it does not exist
mysql_exec "CREATE DATABASE IF NOT EXISTS ${CONFIG["db-name"]} COLLATE utf8_unicode_ci"
# load db
mysql_load_named "DB structure" "installer/data/contrexx_dump_structure.sql"
mysql_load_named "DB dump" "installer/data/contrexx_dump_data.sql"
if [[ ${ARGUMENTS["drop-users"]} != true ]]; then
mysql_load_named "Restore users" "${TEMP_FILES["user-backup"]}"
if [[ ${ARGUMENTS["drop-sessions"]} != true ]]; then
mysql_load_named "Restore sessions" "${TEMP_FILES["session-backup"]}"
fi
fi
mysql_drop_tempfile
cmd_env remote pull
mysql_create_tempfile
# TODO: Check if there's at least one admin, make the user's ID dynamic
EXISTING_CX_USER=$(mysql_exec "SELECT id FROM contrexx_access_users WHERE id = 1")
if [[ "$EXISTING_CX_USER" == "" ]]; then
if [[ ${ARGUMENTS["yes"]} != true ]]; then
echo "There is no user, will create a default user. Ok?"
read -p "Continue [Yn]? " -n 1 -r
echo ""
if [[ "$REPLY" =~ ^[Nn]$ ]]; then
mysql_drop_tempfile
return
fi
fi
# there is no user and we decided to create one
declare -A ClxCredentials
ClxCredentials["user"]=""
ClxCredentials["mail"]="[email protected]"
ClxCredentials["pass"]="123456"
if [[ ${ARGUMENTS["yes"]} != true ]]; then
for key in "${!ClxCredentials[@]}"; do
read -r -p "Enter Cloudrexx $key [${ClxCredentials[$key]}]: " newvalue
if [[ "$newvalue" != "" ]]; then
ClxCredentials[$key]="$newvalue"
fi
done
fi
mysql_exec "INSERT INTO contrexx_access_users (id, is_admin, username, password, regdate, expiration, validity, last_auth, last_activity, email, email_access, frontend_lang_id, backend_lang_id, active, profile_access, restore_key, restore_key_time, u2u_active, auth_token) VALUES (1,1,'${ClxCredentials["user"]}',MD5('${ClxCredentials["pass"]}'),0,0,0,0,0,'${ClxCredentials["mail"]}','nobody', 0,0,1,'members_only','',0,'0', '');INSERT INTO contrexx_access_user_profile (user_id, gender, title, firstname, lastname, company, address, city, zip, country, phone_office, phone_private, phone_mobile, phone_fax, birthday, website, profession, interests, signature, picture) VALUES (1,'gender_undefined',2,'CMS','System Benutzer','','','','',0,'','','','','','','','','','');"
if [[ ${ARGUMENTS["silent"]} != true ]]; then
echo "Created the user \"${ClxCredentials["user"]}\" with e-mail \"${ClxCredentials["mail"]}\" and password \"${ClxCredentials["pass"]}\""
fi
fi
mysql_drop_tempfile
while read -r line; do
if [[ "$line" == "" ]]; then
continue
fi
IFS=" "
local component=( $line )
echo "Update ${component[0]} ${component[1]}"
cmd_env component update "${component[0]}" "${component[1]}"
done <<< $(ARGUMENTS["raw"]=true && cmd_env component list)
clearUsercache
;;
config)
get_config_array
if [[ ${ARGUMENTS["show"]} = true ]]; then
OUTPUT="$(echo -e "KEY\tVALUE")"
OUTPUT+=$'\n'
for value in "${!CONFIG[@]}"; do
OUTPUT+="$(echo -e "$value\t${CONFIG[$value]}")"
OUTPUT+=$'\n'
done
echo "$OUTPUT" | column -t -s $'\t'
return
fi
declare -A CONFIG_OVERRIDE