From f293e9026d70fc8e335a14f2d8c218b6073892a6 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 11 Jan 2015 22:36:12 -0500 Subject: [PATCH 01/19] Better dokku-client bash formatting --- contrib/dokku_client.sh | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index b7fdaafd1..b1b9741a2 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -1,13 +1,11 @@ -if [ -z "$DOKKU_HOST" ] -then +if [[ ! -z $DOKKU_HOST ]]; then function dokku { appname=$(git remote -v 2>/dev/null | grep dokku | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) - if [ "$?" != "0" ] - then + if [[ "$?" != "0" ]]; then donotshift="YES" fi - if [ "$1" = "create" ] - then + + if [[ "$1" = "create" ]]; then appname=$(echo "print(elfs.GenName())" | lua -l elfs) if git remote add dokku dokku@$DOKKU_HOST:$appname then @@ -18,15 +16,16 @@ then return fi git push dokku master - else - if [ -z "$donotshift" ] - then - ssh dokku@$DOKKU_HOST $* - exit - fi - verb=$1 - shift - ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ + return $? fi + + if [[ -z "$donotshift" ]]; then + ssh dokku@$DOKKU_HOST $* + exit + fi + + verb=$1 + shift + ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ } fi From cd65704c5e8fe734583357fa0d678ec19c47b1f2 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 11 Jan 2015 22:36:50 -0500 Subject: [PATCH 02/19] Force usage of bash to run command --- contrib/dokku_client.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index b1b9741a2..8a18aa69b 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + if [[ ! -z $DOKKU_HOST ]]; then function dokku { appname=$(git remote -v 2>/dev/null | grep dokku | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) From 34895630fd0f363dc52194676ed4cf93ec0be864 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 11 Jan 2015 22:38:07 -0500 Subject: [PATCH 03/19] Add DOKKU_TRACE support to turn on command tracing --- contrib/dokku_client.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 8a18aa69b..3681697b8 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x if [[ ! -z $DOKKU_HOST ]]; then function dokku { From 0af71abe1213329a31d0d41f7ec8a64454bb5a14 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:22:10 -0500 Subject: [PATCH 04/19] Exit with exit code from ssh command --- contrib/dokku_client.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 3681697b8..d7b8bae35 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -24,7 +24,7 @@ if [[ ! -z $DOKKU_HOST ]]; then if [[ -z "$donotshift" ]]; then ssh dokku@$DOKKU_HOST $* - exit + exit $? fi verb=$1 From 3e05a8c8ac223807e7e6828157ca41d9fa7fc388 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:25:15 -0500 Subject: [PATCH 05/19] formatting --- contrib/dokku_client.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index d7b8bae35..56650e78c 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -19,7 +19,7 @@ if [[ ! -z $DOKKU_HOST ]]; then return fi git push dokku master - return $? + return $? fi if [[ -z "$donotshift" ]]; then From aec4543b616435bd822ce403b397479e3c17719a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:25:46 -0500 Subject: [PATCH 06/19] Properly retrieve and parse the app name from git remote --- contrib/dokku_client.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 56650e78c..1a268aa96 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -3,8 +3,17 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x if [[ ! -z $DOKKU_HOST ]]; then function dokku { - appname=$(git remote -v 2>/dev/null | grep dokku | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) - if [[ "$?" != "0" ]]; then + appname="" + if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then + set +e + appname=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) + set -e + else + echo "This is not a git repository" + exit 1 + fi + + if [[ "$appname" != "" ]]; then donotshift="YES" fi From 1eb01db8f9a3c8e2a2745b162679ae5fb76c946d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:27:06 -0500 Subject: [PATCH 07/19] Add ability to alias `dokku_client.sh` as `dokku` command locally --- contrib/dokku_client.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 1a268aa96..6e55414cd 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -2,7 +2,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x if [[ ! -z $DOKKU_HOST ]]; then - function dokku { + function _dokku { appname="" if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then set +e @@ -40,4 +40,9 @@ if [[ ! -z $DOKKU_HOST ]]; then shift ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ } + + if [[ "$0" == "dokku" ]] || [[ "$0" == *dokku_client.sh ]]; then + _dokku $@ + exit $? + fi fi From d8fa69c5f4d79901bd1bae00ddd52e37bfcb973d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:27:26 -0500 Subject: [PATCH 08/19] formatting --- contrib/dokku_client.sh | 76 ++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 6e55414cd..efc91ba83 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -2,47 +2,47 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x if [[ ! -z $DOKKU_HOST ]]; then - function _dokku { - appname="" - if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then - set +e - appname=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) - set -e - else - echo "This is not a git repository" - exit 1 - fi + function _dokku { + appname="" + if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then + set +e + appname=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) + set -e + else + echo "This is not a git repository" + exit 1 + fi - if [[ "$appname" != "" ]]; then - donotshift="YES" - fi + if [[ "$appname" != "" ]]; then + donotshift="YES" + fi - if [[ "$1" = "create" ]]; then - appname=$(echo "print(elfs.GenName())" | lua -l elfs) - if git remote add dokku dokku@$DOKKU_HOST:$appname - then - echo "-----> Dokku remote added at $DOKKU_HOST" - echo "-----> Application name is $appname" - else - echo "! Dokku remote not added! Do you already have a dokku remote?" - return - fi - git push dokku master - return $? - fi + if [[ "$1" = "create" ]]; then + appname=$(echo "print(elfs.GenName())" | lua -l elfs) + if git remote add dokku dokku@$DOKKU_HOST:$appname + then + echo "-----> Dokku remote added at $DOKKU_HOST" + echo "-----> Application name is $appname" + else + echo "! Dokku remote not added! Do you already have a dokku remote?" + return + fi + git push dokku master + return $? + fi - if [[ -z "$donotshift" ]]; then - ssh dokku@$DOKKU_HOST $* - exit $? - fi + if [[ -z "$donotshift" ]]; then + ssh dokku@$DOKKU_HOST $* + exit $? + fi - verb=$1 - shift - ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ - } + verb=$1 + shift + ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ + } - if [[ "$0" == "dokku" ]] || [[ "$0" == *dokku_client.sh ]]; then - _dokku $@ - exit $? - fi + if [[ "$0" == "dokku" ]] || [[ "$0" == *dokku_client.sh ]]; then + _dokku $@ + exit $? + fi fi From 7f99fcb844bad95cb4bb95c6fb40ec4ce621387c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:31:18 -0500 Subject: [PATCH 09/19] Use double-square brackets in if test --- contrib/dokku_client.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index efc91ba83..deefc046a 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -4,7 +4,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x if [[ ! -z $DOKKU_HOST ]]; then function _dokku { appname="" - if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then + if [[ -d .git ]] || git rev-parse --git-dir > /dev/null 2>&1; then set +e appname=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d':' 2>/dev/null) set -e From 45120c5e431900857d05b753a8a1c32b49ea265c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 04:32:07 -0500 Subject: [PATCH 10/19] Pull DOKKU_HOST from dokku remote host if not otherwise set --- contrib/dokku_client.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index deefc046a..c7be9e289 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -1,6 +1,12 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +if [[ -z $DOKKU_HOST ]]; then + if [[ -d .git ]] || git rev-parse --git-dir > /dev/null 2>&1; then + DOKKU_HOST=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d '@' | cut -f1 -d':' 2>/dev/null) + fi +fi + if [[ ! -z $DOKKU_HOST ]]; then function _dokku { appname="" From dc64759a97d9265b2d049e52722712d3639e08d5 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 14 Jan 2015 12:45:46 -0500 Subject: [PATCH 11/19] Use doublequotes to prevent whitespace problems --- contrib/dokku_client.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index c7be9e289..fe4b77152 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -38,17 +38,17 @@ if [[ ! -z $DOKKU_HOST ]]; then fi if [[ -z "$donotshift" ]]; then - ssh dokku@$DOKKU_HOST $* + ssh dokku@$DOKKU_HOST "$*" exit $? fi verb=$1 shift - ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ + ssh dokku@$DOKKU_HOST "$verb" "$appname" "$@" } if [[ "$0" == "dokku" ]] || [[ "$0" == *dokku_client.sh ]]; then - _dokku $@ + _dokku "$@" exit $? fi fi From e35493e248f4848ece84ef97c66c8024af4ea5de Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 16 Jan 2015 17:07:51 -0500 Subject: [PATCH 12/19] unquote variables --- contrib/dokku_client.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index fe4b77152..f89684cf3 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -38,13 +38,13 @@ if [[ ! -z $DOKKU_HOST ]]; then fi if [[ -z "$donotshift" ]]; then - ssh dokku@$DOKKU_HOST "$*" + ssh dokku@$DOKKU_HOST $* exit $? fi verb=$1 shift - ssh dokku@$DOKKU_HOST "$verb" "$appname" "$@" + ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ } if [[ "$0" == "dokku" ]] || [[ "$0" == *dokku_client.sh ]]; then From 55c2b0fd62ff01b688fdb384652a4fa653eca08b Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 16 Jan 2015 14:57:49 -0800 Subject: [PATCH 13/19] allow force override --- plugins/apps/commands | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/plugins/apps/commands b/plugins/apps/commands index d15121083..28e977744 100755 --- a/plugins/apps/commands +++ b/plugins/apps/commands @@ -20,17 +20,19 @@ case "$1" in [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 [[ ! -d "$DOKKU_ROOT/$2" ]] && echo "App $2 does not exist" && exit 1 [[ "$2" == "tls" ]] && echo "Unable to destroy tls directory" && exit 1 - APP="$2"; IMAGE="dokku/$APP"; + APP="$2"; IMAGE="dokku/$APP"; FORCE="$3" - echo " ! WARNING: Potentially Destructive Action" - echo " ! This command will destroy $APP (including all add-ons)." - echo " ! To proceed, type \"$APP\"" - echo "" + if [[ "$FORCE" != "force" ]]; then + echo " ! WARNING: Potentially Destructive Action" + echo " ! This command will destroy $APP (including all add-ons)." + echo " ! To proceed, type \"$APP\"" + echo "" - read -p "> " app_name - if [[ "$app_name" != "$APP" ]]; then - echo " ! Confirmation did not match $APP. Aborted." - exit 1 + read -p "> " app_name + if [[ "$app_name" != "$APP" ]]; then + echo " ! Confirmation did not match $APP. Aborted." + exit 1 + fi fi echo "Destroying $APP (including all add-ons)" From fda3a08c83888647484c3fb4dedcb5f89a4efec9 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 16 Jan 2015 14:58:41 -0800 Subject: [PATCH 14/19] shellcheck dokku_client with specific exclusion --- tests.mk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests.mk b/tests.mk index 7e8ab05a4..0bab93efb 100644 --- a/tests.mk +++ b/tests.mk @@ -56,7 +56,8 @@ lint: # SC2143: Instead of [ -n $(foo | grep bar) ], use foo | grep -q bar - https://github.com/koalaman/shellcheck/wiki/SC2143 # SC2001: See if you can use ${variable//search/replace} instead. - https://github.com/koalaman/shellcheck/wiki/SC2001 @echo linting... - @$(QUIET) find . -not -path '*/\.*' | xargs file | egrep "shell|bash" | awk '{ print $$1 }' | sed 's/://g' | xargs shellcheck -e SC2034,SC2086,SC2143,SC2001 + @$(QUIET) shellcheck -e SC2029 ./contrib/dokku_client.sh + @$(QUIET) find . -not -path '*/\.*' | xargs file | egrep "shell|bash" | awk '{ print $$1 }' | sed 's/://g' | grep -v dokku_client.sh | xargs shellcheck -e SC2034,SC2086,SC2143,SC2001 unit-tests: @echo running unit tests... From c7bd7119b8d947c9ffee23032d0f2b4f350fa4c1 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 16 Jan 2015 14:59:01 -0800 Subject: [PATCH 15/19] assert on intermediate steps --- tests/unit/config.bats | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/config.bats b/tests/unit/config.bats index 389a31069..2699c7619 100644 --- a/tests/unit/config.bats +++ b/tests/unit/config.bats @@ -21,6 +21,7 @@ teardown() { run ssh dokku@dokku.me config:set $TEST_APP test_var=true test_var2=\"hello world\" echo "output: "$output echo "status: "$status + assert_success run dokku config:get $TEST_APP test_var2 echo "output: "$output echo "status: "$status @@ -31,12 +32,15 @@ teardown() { run ssh dokku@dokku.me config:set $TEST_APP test_var=true test_var2=\"hello world\" echo "output: "$output echo "status: "$status + assert_success run dokku config:get $TEST_APP test_var echo "output: "$output echo "status: "$status + assert_success run dokku config:unset $TEST_APP test_var echo "output: "$output echo "status: "$status + assert_success run dokku config:get $TEST_APP test_var echo "output: "$output echo "status: "$status From cb39110904ef9ef2f8976ad0be8a672892105298 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 16 Jan 2015 15:18:24 -0800 Subject: [PATCH 16/19] new assertion for a specific exit status --- tests/unit/test_helper.bash | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index 37db58b5c..8afbd838d 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -79,6 +79,10 @@ assert() { fi } +assert_exit_status() { + assert_equal "$status" "$1" +} + # dokku functions create_app() { dokku apps:create $TEST_APP From 161111aab583dee2fd2976dbfbc430acf9445c69 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 16 Jan 2015 15:19:14 -0800 Subject: [PATCH 17/19] lintball client. add tests. add help message --- contrib/dokku_client.sh | 19 +++-- tests/unit/client.bats | 166 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 tests/unit/client.bats diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index f89684cf3..0dc886183 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -1,9 +1,17 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +function client_help_msg() { + echo "==> Configure the DOKKU_HOST environment variable or run $0 from a repository with a git remote named dokku" + echo "--> i.e. git remote add dokku dokku@:" + exit 20 # exit with specific status. only used in units tests for now +} + if [[ -z $DOKKU_HOST ]]; then if [[ -d .git ]] || git rev-parse --git-dir > /dev/null 2>&1; then - DOKKU_HOST=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d '@' | cut -f1 -d':' 2>/dev/null) + DOKKU_HOST=$(git remote -v 2>/dev/null | grep -Ei "^dokku" | head -n 1 | cut -f1 -d' ' | cut -f2 -d '@' | cut -f1 -d':' 2>/dev/null || true) + else + client_help_msg fi fi @@ -16,7 +24,6 @@ if [[ ! -z $DOKKU_HOST ]]; then set -e else echo "This is not a git repository" - exit 1 fi if [[ "$appname" != "" ]]; then @@ -25,7 +32,7 @@ if [[ ! -z $DOKKU_HOST ]]; then if [[ "$1" = "create" ]]; then appname=$(echo "print(elfs.GenName())" | lua -l elfs) - if git remote add dokku dokku@$DOKKU_HOST:$appname + if git remote add dokku "dokku@$DOKKU_HOST:$appname" then echo "-----> Dokku remote added at $DOKKU_HOST" echo "-----> Application name is $appname" @@ -38,17 +45,19 @@ if [[ ! -z $DOKKU_HOST ]]; then fi if [[ -z "$donotshift" ]]; then - ssh dokku@$DOKKU_HOST $* + ssh "dokku@$DOKKU_HOST" "$*" exit $? fi verb=$1 shift - ssh dokku@$DOKKU_HOST "$verb" "$appname" $@ + ssh "dokku@$DOKKU_HOST" "$verb" "$appname" "$@" } if [[ "$0" == "dokku" ]] || [[ "$0" == *dokku_client.sh ]]; then _dokku "$@" exit $? fi +else + client_help_msg fi diff --git a/tests/unit/client.bats b/tests/unit/client.bats new file mode 100644 index 000000000..2a6a253e7 --- /dev/null +++ b/tests/unit/client.bats @@ -0,0 +1,166 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + export DOKKU_HOST=dokku.me + create_app +} + +teardown() { + destroy_app + unset DOKKU_HOST +} + +@test "dokku client (unconfigured DOKKU_HOST)" { + unset DOKKU_HOST + run ./contrib/dokku_client.sh apps + echo "output: "$output + echo "status: "$status + assert_exit_status 20 +} + +@test "dokku client (no args should print help)" { + run /bin/bash -c "./contrib/dokku_client.sh | head -1 | grep -q 'dokku COMMAND '" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (apps:create)" { + destroy_app + run ./contrib/dokku_client.sh apps:create $TEST_APP + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (apps:destroy)" { + run ./contrib/dokku_client.sh apps:destroy $TEST_APP force + echo "output: "$output + echo "status: "$status + assert_success + create_app # prevent teardown() failure +} + +@test "dokku client (config:set)" { + run ./contrib/dokku_client.sh config:set $TEST_APP test_var=true test_var2=\"hello world\" + echo "output: "$output + echo "status: "$status + assert_success + run /bin/bash -c "./contrib/dokku_client.sh config:get $TEST_APP test_var2 | grep -q 'hello world'" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (config:unset)" { + run ./contrib/dokku_client.sh config:set $TEST_APP test_var=true test_var2=\"hello world\" + echo "output: "$output + echo "status: "$status + assert_success + run ./contrib/dokku_client.sh config:get $TEST_APP test_var + echo "output: "$output + echo "status: "$status + assert_success + run ./contrib/dokku_client.sh config:unset $TEST_APP test_var + echo "output: "$output + echo "status: "$status + assert_success + run /bin/bash -c "./contrib/dokku_client.sh config:get $TEST_APP test_var | grep test_var" + echo "output: "$output + echo "status: "$status + assert_failure +} + +@test "dokku client (domains)" { + run bash -c "./contrib/dokku_client.sh domains $TEST_APP | grep -q ${TEST_APP}.dokku.me" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (domains:add)" { + run ./contrib/dokku_client.sh domains:add $TEST_APP www.test.app.dokku.me + echo "output: "$output + echo "status: "$status + assert_success + run ./contrib/dokku_client.sh domains:add $TEST_APP test.app.dokku.me + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (domains:remove)" { + run ./contrib/dokku_client.sh domains:add $TEST_APP test.app.dokku.me + echo "output: "$output + echo "status: "$status + assert_success + run ./contrib/dokku_client.sh domains:remove $TEST_APP test.app.dokku.me + echo "output: "$output + echo "status: "$status + refute_line "test.app.dokku.me" +} + +@test "dokku client (domains:clear)" { + run ./contrib/dokku_client.sh domains:add $TEST_APP test.app.dokku.me + echo "output: "$output + echo "status: "$status + assert_success + run ./contrib/dokku_client.sh domains:clear $TEST_APP + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (ps)" { + # CI support: 'Ah. I just spoke with our Docker expert -- + # looks like docker exec is built to work with docker-under-libcontainer, + # but we're using docker-under-lxc. I don't have an estimated time for the fix, sorry + skip "circleci does not support docker exec at the moment." + deploy_app + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh ps $TEST_APP | grep -q 'node web.js'" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (ps:start)" { + deploy_app + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh ps:stop $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh ps:start $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -q --no-trunc | grep -q $(< $DOKKU_ROOT/$TEST_APP/CONTAINER)" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "dokku client (ps:stop)" { + deploy_app + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh ps:stop $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -q --no-trunc | grep -q $(< $DOKKU_ROOT/$TEST_APP/CONTAINER)" + echo "output: "$output + echo "status: "$status + assert_failure +} + +@test "dokku client (ps:restart)" { + deploy_app + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh ps:restart $TEST_APP" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -q --no-trunc | grep -q $(< $DOKKU_ROOT/$TEST_APP/CONTAINER)" + echo "output: "$output + echo "status: "$status + assert_success +} From 666f2d7434f62b057268c809b5447ec49fbf7f09 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Fri, 16 Jan 2015 17:59:51 -0800 Subject: [PATCH 18/19] add random app generator in bash. support `create` and `delete` verbs. tested on bash 4.3.18 and 3.2.53 --- contrib/dokku_client.sh | 58 +++++++++++++++++++++++++++++++++---- tests/unit/client.bats | 12 ++++++++ tests/unit/test_helper.bash | 13 +++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 0dc886183..86312c4db 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -1,6 +1,32 @@ #!/usr/bin/env bash set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +MOVES=(ABLE ABNORMA AGAIN AIREXPL ANG ANGER ASAIL ATTACK AURORA AWL BAN BAND BARE BEAT BEATED BELLY BIND BITE BLOC BLOOD BODY BOOK BREATH BUMP CAST CHAM CLAMP CLAP CLAW CLEAR CLI CLIP CLOUD CONTRO CONVY COOLHIT CRASH CRY CUT DESCRI D-FIGHT DIG DITCH DIV DOZ DRE DUL DU-PIN DYE EARTH EDU EG-BOMB EGG ELEGY ELE-HIT EMBODY EMPLI ENGL ERUPT EVENS EXPLOR EYES FALL FAST F-CAR F-DANCE FEARS F-FIGHT FIGHT FIR FIRE FIREHIT FLAME FLAP FLASH FLEW FORCE FRA FREEZE FROG G-BIRD GENKISS GIFT G-KISS G-MOUSE GRADE GROW HAMMER HARD HAT HATE H-BOMB HELL-R HEMP HINT HIT HU HUNT HYPNOSI INHA IRO IRONBAR IR-WING J-GUN KEE KICK KNIF KNIFE KNOCK LEVEL LIGH LIGHHIT LIGHT LIVE L-WALL MAD MAJUS MEL MELO MESS MILK MIMI MISS MIXING MOVE MUD NI-BED NOISY NOONLI NULL N-WAVE PAT PEACE PIN PLAN PLANE POIS POL POWDE POWE POWER PRIZE PROTECT PROUD RAGE RECOR REFLAC REFREC REGR RELIV RENEW R-FIGHT RING RKICK ROCK ROUND RUS RUSH SAND SAW SCISSOR SCRA SCRIPT SEEN SERVER SHADOW SHELL SHINE SHO SIGHT SIN SMALL SMELT SMOK SNAKE SNO SNOW SOU SO-WAVE SPAR SPEC SPID S-PIN SPRA STAM STARE STEA STONE STORM STRU STRUG STUDEN SUBS SUCID SUN-LIG SUNRIS SUPLY S-WAVE TAILS TANGL TASTE TELLI THANK TONKICK TOOTH TORL TRAIN TRIKICK TUNGE VOLT WA-GUN WATCH WAVE W-BOMB WFALL WFING WHIP WHIRL WIND WOLF WOOD WOR YUJA) +NAMES=(SEED GRASS FLOWE SHAD CABR SNAKE GOLD COW GUIKI PEDAL DELAN B-FLY BIDE KEYU FORK LAP PIGE PIJIA CAML LAT BIRD BABOO VIV ABOKE PIKAQ RYE SAN BREAD LIDEL LIDE PIP PIKEX ROK JUGEN PUD BUDE ZHIB GELU GRAS FLOW LAFUL ATH BALA CORN MOLUF DESP DAKED MIMI BOLUX KODA GELUD MONK SUMOY GEDI WENDI NILEM NILE NILEC KEZI YONGL HUDE WANLI GELI GUAIL MADAQ WUCI WUCI MUJEF JELLY SICIB GELU NELUO BOLI JIALE YED YEDE CLO SCARE AOCO DEDE DEDEI BAWU JIUG BADEB BADEB HOLE BALUX GES FANT QUAR YIHE SWAB SLIPP CLU DEPOS BILIY YUANO SOME NO YELA EMPT ZECUN XIAHE BOLEL DEJI MACID XIHON XITO LUCK MENJI GELU DECI XIDE DASAJ DONGN RICUL MINXI BALIY ZENDA LUZEL HELE5 0FENB KAIL JIAND CARP JINDE LAPU MUDE YIFU LINLI SANDI HUSI JINC OUMU OUMUX CAP KUIZA PUD TIAO FRMAN CLAU SPARK DRAGO BOLIU GUAIL MIYOU MIY QIAOK BEIL MUKEI RIDED MADAM BAGEP CROC ALIGE OUDAL OUD DADA HEHE YEDEA NUXI NUXIN ROUY ALIAD STICK QIANG LAAND PIQI PI PUPI DEKE DEKEJ NADI NADIO MALI PEA ELECT FLOWE MAL MALI HUSHU NILEE YUZI POPOZ DUZI HEBA XIAN SHAN YEYEA WUY LUO KEFE HULA CROW YADEH MOW ANNAN SUONI KYLI HULU HUDEL YEHE GULAE YEHE BLU GELAN BOAT NIP POIT HELAK XINL BEAR LINB MAGEH MAGEJ WULI YIDE RIVE FISH AOGU DELIE MANTE KONMU DELU HELU HUAN HUMA DONGF JINCA HEDE DEFU LIBY JIAPA MEJI HELE BUHU MILK HABI THUN GARD DON YANGQ SANAQ BANQ LUJ PHIX SIEI EGG) + +function random_number() { + [[ -n "$1" ]] && RANGE="$1" + if [[ -n "$RANGE" ]];then + number=$RANDOM + let "number %= $RANGE" + else + number=$RANDOM + fi + echo $number +} + +function random_name() { + NUM1=$(random_number ${#MOVES[@]}) + NUM2=$(random_number ${#MOVES[@]}) + NUM3=$(random_number ${#NAMES[@]}) + + UPPER_APPNAME="${MOVES[${NUM1}]}-${MOVES[${NUM2}]}-${NAMES[${NUM3}]}" + + [[ "$BASH_VERSION" =~ 4.* ]] && lower_appname=${UPPER_APPNAME,,} + [[ -z "$lower_appname" ]] && lower_appname=$(echo $UPPER_APPNAME | tr '[:upper:]' '[:lower:]') + echo "$lower_appname" +} + function client_help_msg() { echo "==> Configure the DOKKU_HOST environment variable or run $0 from a repository with a git remote named dokku" echo "--> i.e. git remote add dokku dokku@:" @@ -26,12 +52,34 @@ if [[ ! -z $DOKKU_HOST ]]; then echo "This is not a git repository" fi - if [[ "$appname" != "" ]]; then - donotshift="YES" + if [[ "$appname" != "" ]] && [[ -n "$*" ]]; then + case "$1" in + apps*|backup*|help|plugins*|ps:restartall|trace|version) + true + ;; + delete) + verb="apps:destroy" + donotshift="YES" + ;; + *) + donotshift="YES" + ;; + esac fi if [[ "$1" = "create" ]]; then - appname=$(echo "print(elfs.GenName())" | lua -l elfs) + appname=$(random_name) + counter=0 + while ssh "dokku@$DOKKU_HOST" apps 2>/dev/null| grep -q "$appname"; do + if [[ $counter -ge 100 ]];then + echo "Error: could not reasonably generate a new app name. try cleaning up some apps..." + ssh "dokku@$DOKKU_HOST" apps + exit 1 + else + appname=$(random_name) + counter=$((counter+1)) + fi + done if git remote add dokku "dokku@$DOKKU_HOST:$appname" then echo "-----> Dokku remote added at $DOKKU_HOST" @@ -45,11 +93,11 @@ if [[ ! -z $DOKKU_HOST ]]; then fi if [[ -z "$donotshift" ]]; then - ssh "dokku@$DOKKU_HOST" "$*" + ssh "dokku@$DOKKU_HOST" "$@" exit $? fi - verb=$1 + [[ -z "$verb" ]] && verb=$1 shift ssh "dokku@$DOKKU_HOST" "$verb" "$appname" "$@" } diff --git a/tests/unit/client.bats b/tests/unit/client.bats index 2a6a253e7..76ca83d74 100644 --- a/tests/unit/client.bats +++ b/tests/unit/client.bats @@ -164,3 +164,15 @@ teardown() { echo "status: "$status assert_success } + +@test "dokku client (create/delete)" { + setup_client_repo + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh create" + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh delete force" + echo "output: "$output + echo "status: "$status + assert_success +} diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash index 8afbd838d..935e38ee9 100644 --- a/tests/unit/test_helper.bash +++ b/tests/unit/test_helper.bash @@ -107,6 +107,19 @@ deploy_app() { git push target master || destroy_app } +setup_client_repo() { + TMP=$(mktemp -d -t "$TARGET.XXXXX") + rmdir $TMP && cp -r ./tests/apps/nodejs-express $TMP + cd $TMP + git init + git config user.email "robot@example.com" + git config user.name "Test Robot" + + [[ -f gitignore ]] && mv gitignore .gitignore + git add . + git commit -m 'initial commit' +} + setup_test_tls() { TLS="/home/dokku/$TEST_APP/tls" mkdir -p $TLS From 2ff2ca55c1910a31effb0985eb70988f289fdc59 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Sat, 17 Jan 2015 16:24:50 -0800 Subject: [PATCH 19/19] drop support for create/delete in favor of standard apps:create and apps:destroy --- contrib/dokku_client.sh | 11 ++++++----- tests/unit/client.bats | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index 86312c4db..b899a9af1 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -54,12 +54,13 @@ if [[ ! -z $DOKKU_HOST ]]; then if [[ "$appname" != "" ]] && [[ -n "$*" ]]; then case "$1" in - apps*|backup*|help|plugins*|ps:restartall|trace|version) + apps|backup*|help|plugins*|ps:restartall|trace|version) true ;; - delete) - verb="apps:destroy" - donotshift="YES" + apps:destroy) + if [[ -z "$2" ]] || [[ "$2" == "force" ]];then + donotshift="YES" + fi ;; *) donotshift="YES" @@ -67,7 +68,7 @@ if [[ ! -z $DOKKU_HOST ]]; then esac fi - if [[ "$1" = "create" ]]; then + if [[ "$1" = "apps:create" ]] && [[ -z "$2" ]]; then appname=$(random_name) counter=0 while ssh "dokku@$DOKKU_HOST" apps 2>/dev/null| grep -q "$appname"; do diff --git a/tests/unit/client.bats b/tests/unit/client.bats index 76ca83d74..4ddb42969 100644 --- a/tests/unit/client.bats +++ b/tests/unit/client.bats @@ -165,13 +165,13 @@ teardown() { assert_success } -@test "dokku client (create/delete)" { +@test "dokku client (apps:create AND apps:destroy)" { setup_client_repo - run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh create" + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh apps:create" echo "output: "$output echo "status: "$status assert_success - run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh delete force" + run bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh apps:destroy force" echo "output: "$output echo "status: "$status assert_success