From f00d84c40fc1aac2db40e3ecf1795082e3c78e62 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 13 May 2018 00:47:43 -0400 Subject: [PATCH 1/9] Fixed mixup between older and newer approach. --- cli/commands/help | 2 +- commands/help | 22 ---------------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100755 commands/help diff --git a/cli/commands/help b/cli/commands/help index 563e362..32e1691 100755 --- a/cli/commands/help +++ b/cli/commands/help @@ -7,7 +7,7 @@ ${YELLOW}WPLib Box ${GREEN}CLI${RESET} usage: box ${GREEN}database${RESET} [] - Manage databases. box ${GREEN}container${RESET} [] - Manage Docker containers. -box ${GREEN}start${RESET} - Start Docker containers found in project.json's services object. +box ${GREEN}startup${RESET} - Start Docker containers found in project.json's services object. box ${GREEN}restart${RESET} - Restart Docker containers found in project.json's services object. box ${GREEN}shutdown${RESET} - Shutdown all Docker containers. diff --git a/commands/help b/commands/help deleted file mode 100755 index 7817aa7..0000000 --- a/commands/help +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -cat <] - Manage databases. -box ${GREEN}container${RESET} [] - Manage Docker containers. - -box ${GREEN}startup${RESET} - Start Docker containers found in project.json's services object. -box ${GREEN}restart${RESET} - Restart Docker containers found in project.json's services object. -box ${GREEN}shutdown${RESET} - Shutdown all Docker containers. - -box ${GREEN}status${RESET} - Output status of WPLib Box's standard containers. -box ${GREEN}version${RESET} - Display the current version of this WPLib Box VM. -box ${GREEN}self-update${RESET} - Update this CLI tool to the latest version for this Box version. - -box ${GREEN}test${RESET} - Update this CLI tool to the latest version for this Box version. - -box [${GREEN}help${RESET}] - Display the above information. -EOF - From 83d0072d130efd491e95b6da45f57dbeb96582cc Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 13 May 2018 00:48:53 -0400 Subject: [PATCH 2/9] Added command line switch --no-backup to `box database import` command. --- cli/commands/database | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/commands/database b/cli/commands/database index d280a41..dd7ba6a 100755 --- a/cli/commands/database +++ b/cli/commands/database @@ -156,11 +156,12 @@ function database_import() { fi + if [ ! $(has_arg --no-backup) ]; then database_backup "${basefile}.bak" - if [ $? -ne 0 ]; then output "Error when backing up to ${basefile}.bak" return 4 + fi fi output "Dropping any prior tables" From 422635fe144d18f9ac6a424e76d161c071e6f1fb Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 13 May 2018 01:27:10 -0400 Subject: [PATCH 3/9] Added a --no-droptables argument. --- cli/commands/database | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cli/commands/database b/cli/commands/database index dd7ba6a..5a14476 100755 --- a/cli/commands/database +++ b/cli/commands/database @@ -164,17 +164,21 @@ function database_import() { fi fi - output "Dropping any prior tables" + if [ ! $(has_arg --no-droptables) ]; then - if [ ! $(has_arg --dryrun) ]; then - box container exec ${database} mysql -h ${DB_HOST_IP} -u wordpress -pwordpress -Nse "show tables;" wordpress 2>&1 | grep -v "Warning:" > /tmp/mysql-table-list.txt - cat /tmp/mysql-table-list.txt | while read table - do + output "Dropping any prior tables" + + if [ ! $(has_arg --dryrun) ]; then + box container exec ${database} mysql -h ${DB_HOST_IP} -u wordpress -pwordpress -Nse "show tables;" wordpress 2>&1 | grep -v "Warning:" > /tmp/mysql-table-list.txt + cat /tmp/mysql-table-list.txt | while read table + do box container exec ${database} mysql -h ${DB_HOST_IP} -u wordpress -pwordpress -e "drop table $table" wordpress 2>&1 | grep -v "Warning:" - done - rm /tmp/mysql-table-list.txt + done + rm /tmp/mysql-table-list.txt + fi + output "Tables dropped" + fi - output "Tables dropped" if [ "" != "$(cat "${importfilepath}" | grep "utf8mb4_unicode_520_ci")" ]; then # From 731e89d7bdc594a56ed07d9e65a138df7fbb4311 Mon Sep 17 00:00:00 2001 From: MickMake Date: Mon, 21 May 2018 14:22:51 +1000 Subject: [PATCH 4/9] Path issue. --- bin/composer | 2 +- bin/php | 2 +- bin/wp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/composer b/bin/composer index 7bd6d22..a7d4f6b 100755 --- a/bin/composer +++ b/bin/composer @@ -22,7 +22,7 @@ check_image() SCRIPT="`basename $0`" REQVERS="`echo $SCRIPT | sed -r 's/composer-([0-9]*\.[0-9]*)/\1/'`" -EXEC="/usr/local/bin/composer-$REQVERS" +EXEC="/opt/box/bin/composer-$REQVERS" if [ -x "$EXEC" ] diff --git a/bin/php b/bin/php index 52bafea..303d0ff 100755 --- a/bin/php +++ b/bin/php @@ -22,7 +22,7 @@ check_image() SCRIPT="`basename $0`" REQVERS="`echo $SCRIPT | sed -r 's/php-([0-9]*\.[0-9]*)/\1/'`" -EXEC="/usr/local/bin/php-$REQVERS" +EXEC="/opt/box/bin/php-$REQVERS" if [ -x "$EXEC" ] diff --git a/bin/wp b/bin/wp index e7c02fe..2b0d717 100755 --- a/bin/wp +++ b/bin/wp @@ -22,7 +22,7 @@ check_image() SCRIPT="`basename $0`" REQVERS="`echo $SCRIPT | sed -r 's/wp-([0-9]*\.[0-9]*)/\1/'`" -EXEC="/usr/local/bin/wp-$REQVERS" +EXEC="/opt/box/bin/wp-$REQVERS" if [ -x "$EXEC" ] From bca40fc71c038009f66693b7b6a1a71d0f2c3638 Mon Sep 17 00:00:00 2001 From: MickMake Date: Mon, 21 May 2018 14:23:19 +1000 Subject: [PATCH 5/9] SUDO issue resolved. --- cli/commands/self-update | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/commands/self-update b/cli/commands/self-update index bf1cacd..d09521a 100755 --- a/cli/commands/self-update +++ b/cli/commands/self-update @@ -56,4 +56,4 @@ EOF chmod a+x /tmp/self-update.$$ -exec bash /tmp/self-update.$$ +exec sudo /tmp/self-update.$$ From 6daf3792038cf520ab4467338598bb2964060277 Mon Sep 17 00:00:00 2001 From: MickMake Date: Mon, 21 May 2018 14:23:33 +1000 Subject: [PATCH 6/9] Fix spurious shutdown issues. --- cli/commands/shutdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/commands/shutdown b/cli/commands/shutdown index 68e9779..487a243 100755 --- a/cli/commands/shutdown +++ b/cli/commands/shutdown @@ -1,5 +1,5 @@ #!/usr/bin/env bash source "${WPLIB_BOX_INCLUDES_DIR}/read-json" -box container stop $database $webserver $processvm $kvstore $cache $sqladmin $webadmin -# box container shutdown +# box container stop $database $webserver $processvm $kvstore $cache $sqladmin $webadmin +box container shutdown From 38334204b7e5e5eeab0a192cfedf95b2fd0b8143 Mon Sep 17 00:00:00 2001 From: MickMake Date: Mon, 21 May 2018 14:23:53 +1000 Subject: [PATCH 7/9] Native mysql command. --- bin/mysql | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 bin/mysql diff --git a/bin/mysql b/bin/mysql new file mode 100755 index 0000000..4fd8d33 --- /dev/null +++ b/bin/mysql @@ -0,0 +1,25 @@ +#!/bin/bash + +check_image() +{ + # VERSION="`docker container ls -af label=container.class="database" -f "status=running" --format='wplib/{{.Label "container.name"}}:{{.Label "container.version"}}' 2>/dev/null`" + VERSION="`docker container ls -af label=container.class="database" -f "status=running" --format='{{.Label "container.version"}}' 2>/dev/null`" + + if [ "$VERSION" == "" ] + then + echo "WPLib: No MySQL database running." + return 1 + fi + + MAJORVERSION="`docker container ls -af label=container.class="database" -f "status=running" --format='{{.Label "container.majorversion"}}' 2>/dev/null`" + IP="`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql-${VERSION}`" + + return 0 +} + + +if check_image +then + exec box container shell wplib/mysql:${MAJORVERSION} mysql -h ${IP} "$@" +fi + From 28940b2472c49c46b5b90a745f28481830988f37 Mon Sep 17 00:00:00 2001 From: MickMake Date: Mon, 21 May 2018 14:24:09 +1000 Subject: [PATCH 8/9] Fixed TTY issue. --- cli/commands/container | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cli/commands/container b/cli/commands/container index b765740..464918c 100755 --- a/cli/commands/container +++ b/cli/commands/container @@ -312,7 +312,15 @@ d_shell() error_cni else # echo "$ECHO_PREFIX ${GREEN}Shell to container: ${CYAN}${IMAGE}${RESET}" 1>&2 - docker run --name ${CONTAINER_NAME}-shell --rm -i -t ${IMAGE_NETWORK} ${IMAGE_ARGS} ${IMAGE_VOLUMES} ${IMAGE} "$@" + if [ -t 0 ] + then + # TTY allocated. + docker run --name ${CONTAINER_NAME}-shell --rm -i -t ${IMAGE_NETWORK} ${IMAGE_ARGS} ${IMAGE_VOLUMES} ${IMAGE} "$@" + else + # TTY not allocated. + docker run --name ${CONTAINER_NAME}-exec --rm -i ${IMAGE_NETWORK} ${IMAGE_ARGS} ${IMAGE_VOLUMES} ${IMAGE} "$@" + fi + if [ "$?" == "1" ] then error_ccbi From 50c5f94062dc29147691593fee9e3262e472d87a Mon Sep 17 00:00:00 2001 From: MickMake Date: Mon, 21 May 2018 14:24:16 +1000 Subject: [PATCH 9/9] Command completion. --- etc/box-completion.bash | 159 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 etc/box-completion.bash diff --git a/etc/box-completion.bash b/etc/box-completion.bash new file mode 100644 index 0000000..a900c2c --- /dev/null +++ b/etc/box-completion.bash @@ -0,0 +1,159 @@ +#/usr/bin/env bash + +################################################################################ +# Command completion for 'box' +_box() +{ + local cur=${COMP_WORDS[COMP_CWORD]} + local prev=${COMP_WORDS[COMP_CWORD-1]} + + + case "${COMP_WORDS[1]}" in + 'container') + # /opt/box/bin/box container help + _box_container + return 0 + ;; + + 'database') + COMPREPLY=($(compgen -W "backup import chunk unchunk credentials dbname username password" -- $cur)) + return 0 + ;; + + 'startup') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + + 'restart') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + + 'shutdown') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + + 'status') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + + 'shell') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + + 'version') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + + 'self-update') + COMPREPLY=($(compgen -W "$(git --git-dir=/opt/box/.git tag)" -- $cur)) + return 0 + ;; + + 'test') + COMPREPLY=($(compgen -W "$(find /opt/box/cli/tests -maxdepth 1 -type f -printf '%f\n')" -- $cur)) + return 0 + ;; + + 'help') + COMPREPLY=($(compgen -W "" -- $cur)) + return 0 + ;; + esac + + COMPREPLY=($(compgen -W "database container startup restart shutdown status shell version self-update test help" -- $cur)) +} +complete -F _box box + + + +################################################################################ +# Command completion for 'box container' +_box_container() +{ + local cur=${COMP_WORDS[COMP_CWORD]} + local prev=${COMP_WORDS[COMP_CWORD-1]} + + case "$prev" in + 'stop'|'restart') + _box_container_running + return 0 + ;; + + 'start'|'restart'|'rm'|'clean'|'refresh') + _box_container_stopped + return 0 + ;; + + 'ls'|'inspect'|'log') + _box_container_all + return 0 + ;; + + 'pull'|'install') + _box_container_dockerhub + return 0 + ;; + esac + + COMPREPLY=($(compgen -W "install ls start stop rm clean refresh update show shutdown reallyclean inspect log pull" -- $cur)) + return 0 +} + +_box_container_running() +{ + local cur=${COMP_WORDS[COMP_CWORD]} + + COMPREPLY=($(compgen -W "$(docker container ls -af label=container.project=wplib -f status=running --format='{{.Image}}')" -- $cur)) + return 0 +} + + +_box_container_stopped() +{ + local cur=${COMP_WORDS[COMP_CWORD]} + + COMPREPLY=($(compgen -W "$(docker container ls -af label=container.project=wplib -f status=created -f status=exited --format='{{.Image}}')" -- $cur)) + return 0 +} + + +_box_container_all() +{ + local cur=${COMP_WORDS[COMP_CWORD]} + + COMPREPLY=($(compgen -W "$(docker container ls -af label=container.project=wplib --format='{{.Image}}')" -- $cur)) + return 0 +} + + +_box_container_dockerhub() +{ + local IMAGES + local IMAGE_NAME + local VERSIONS + local IMAGE_VERSION + local REPLY + local cur=${COMP_WORDS[COMP_CWORD]} + + IMAGES="`jq -r '.results|.[]|.name' /opt/box/etc/respositories.json`" + for IMAGE_NAME in $IMAGES + do + VERSIONS="`jq -r '.results|.[]|.name' /opt/box/etc/images/${IMAGE_NAME}.json`" + for IMAGE_VERSION in $VERSIONS + do + REPLY="$REPLY wplib/${IMAGE_NAME}:$IMAGE_VERSION" + done + + done + + COMPREPLY=($(compgen -W "$REPLY" -- $cur)) + return 0 +} + +