From a84397895a4def350d48226ba623d9f3f3881028 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Mon, 6 Jul 2015 19:41:17 -0700 Subject: [PATCH 1/2] initial pass at config plugin arguments refactor --- dokku | 8 +- plugins/00_dokku-standard/commands | 12 +-- plugins/20_events/commands | 8 +- plugins/apps/commands | 6 +- plugins/backup/commands | 4 +- plugins/config/commands | 117 +++++++++++++++-------------- plugins/docker-options/commands | 8 +- plugins/domains/commands | 8 +- plugins/nginx-vhosts/commands | 4 +- plugins/ps/commands | 16 ++-- plugins/shell/commands | 2 +- tests/unit/config.bats | 44 ++++++++++- 12 files changed, 143 insertions(+), 94 deletions(-) diff --git a/dokku b/dokku index 56e06f169..c8232cb15 100755 --- a/dokku +++ b/dokku @@ -208,10 +208,10 @@ case "$1" in echo "Options:" cat< [-t] Show the last logs for an application (-t follows) - run Run a command in the environment of an application - url Show the first URL for an application (compatibility) - urls Show all URLs for an application - version Print dokku's version + ls Pretty listing of deployed applications and containers + logs [-t] Show the last logs for an application (-t follows) + run Run a command in the environment of an application + url Show the first URL for an application (compatibility) + urls Show all URLs for an application + version Print dokku's version EOF ;; diff --git a/plugins/20_events/commands b/plugins/20_events/commands index 62f0ea475..ad96119e5 100755 --- a/plugins/20_events/commands +++ b/plugins/20_events/commands @@ -40,10 +40,10 @@ case "$1" in help | events:help) cat && cat< Create a new app - apps:destroy Permanently destroy an app + apps List your apps + apps:create Create a new app + apps:destroy Permanently destroy an app EOF ;; diff --git a/plugins/backup/commands b/plugins/backup/commands index 3eacdde2a..8d44e1438 100755 --- a/plugins/backup/commands +++ b/plugins/backup/commands @@ -87,8 +87,8 @@ case "$1" in help | backup:help) cat && cat< $ENV_FILE_TEMP if ! cmp -s $ENV_FILE $ENV_FILE_TEMP; then cp -f $ENV_FILE_TEMP $ENV_FILE @@ -46,14 +48,36 @@ config_write() { rm -f $ENV_FILE_TEMP } +parse_config_args() { + case "$2" in + --global) + ENV_FILE="$DOKKU_ROOT/ENV" + DOKKU_CONFIG_TYPE="global" + DOKKU_CONFIG_RESTART=false + ;; + --no-restart) + ARGS=("$@") && shift 2 + DOKKU_CONFIG_RESTART=false dokku ${ARGS[0]} "$@" + exit 0 + ;; + *) + ENV_FILE="$DOKKU_ROOT/$2/ENV" + DOKKU_CONFIG_TYPE="app" + + [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + verify_app_name "$2" + APP="$2" + ;; + esac +} + case "$1" in config) - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - APP="$2" - + parse_config_args "$@" config_create - [[ ! -s $ENV_FILE ]] && echo "$APP has no config vars" && exit 1 + + [[ "$APP" ]] && DOKKU_CONFIG_TYPE=$APP + [[ ! -s $ENV_FILE ]] && echo "no config vars for $DOKKU_CONFIG_TYPE" && exit 1 VARS=$(grep -Eo "export ([a-zA-Z_][a-zA-Z0-9_]*=.*)" $ENV_FILE | cut -d" " -f2-) @@ -64,14 +88,13 @@ case "$1" in fi done - dokku_log_info2_quiet "$APP config vars" + + dokku_log_info2_quiet "$DOKKU_CONFIG_TYPE config vars" config_styled_hash "$VARS" ;; config:get) - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - APP="$2" + parse_config_args "$@" if [[ -z $3 ]]; then echo "Usage: dokku config:get APP KEY" @@ -90,26 +113,7 @@ case "$1" in ;; config:set) - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - APP="$2" - - if [[ -z "${*:3}" ]]; then - echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]" - echo "Must specify KEY and VALUE to set." - exit 1 - fi - - shift 2 - dokku config:set-norestart $APP "$@" - dokku_log_info1 "Restarting app $APP" - dokku ps:restart $APP - ;; - - config:set-norestart) - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - APP="$2" + parse_config_args "$@" if [[ -z "${*:3}" ]]; then echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]" @@ -153,29 +157,15 @@ ${var}" config_write "$ENV_TEMP" fi + + if [[ "$DOKKU_CONFIG_RESTART" == "true" ]]; then + dokku_log_info1 "Restarting app $APP" + dokku ps:restart $APP + fi ;; config:unset) - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - APP="$2" - - if [[ -z $3 ]]; then - echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]" - echo "Must specify KEY to unset." - exit 1 - fi - - shift 2 - dokku config:unset-norestart $APP "$@" - dokku_log_info1 "Restarting app $APP" - dokku ps:restart $APP - ;; - - config:unset-norestart) - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - APP="$2" + parse_config_args "$@" if [[ -z $3 ]]; then echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]" @@ -193,14 +183,31 @@ ${var}" config_write "$ENV_TEMP" done + + if [[ "$DOKKU_CONFIG_RESTART" == "true" ]]; then + dokku_log_info1 "Restarting app $APP" + dokku ps:restart $APP + fi + ;; + + config:set-norestart) + dokku_log_info2 "$1 is deprecated as of v0.3.22" + shift 1 + dokku config:set --no-restart "$@" + ;; + + config:unset-norestart) + dokku_log_info2 "$1 is deprecated as of v0.3.22" + shift 1 + dokku config:unset --no-restart "$@" ;; help | config:help) cat && cat< Display the config vars for an app - config:get KEY Display a config value for an app - config:set KEY1=VALUE1 [KEY2=VALUE2 ...] Set one or more config vars - config:unset KEY1 [KEY2 ...] Unset one or more config vars + config (|--global) Display all global or app-specific config vars + config:get (|--global) KEY Display a global or app-specific config value + config:set (|--global) KEY1=VALUE1 [KEY2=VALUE2 ...] Set one or more config vars + config:unset (|--global) KEY1 [KEY2 ...] Unset one or more config vars EOF ;; diff --git a/plugins/docker-options/commands b/plugins/docker-options/commands index d3eb5a9eb..377fcfd5d 100755 --- a/plugins/docker-options/commands +++ b/plugins/docker-options/commands @@ -141,10 +141,10 @@ case "$1" in # Display usage help help) cat && cat< Display apps docker options for all phases - docker-options Display apps docker options for phase (comma seperated phase list) - docker-options:add OPTION Add docker option to app for phase (comma seperated phase list) - docker-options:remove OPTION Remove docker option from app for phase (comma seperated phase list) + docker-options Display apps docker options for all phases + docker-options Display apps docker options for phase (comma seperated phase list) + docker-options:add OPTION Add docker option to app for phase (comma seperated phase list) + docker-options:remove OPTION Remove docker option from app for phase (comma seperated phase list) EOF ;; diff --git a/plugins/domains/commands b/plugins/domains/commands index 5de0f426d..4b2297bdf 100755 --- a/plugins/domains/commands +++ b/plugins/domains/commands @@ -133,10 +133,10 @@ case "$1" in help | domains:help) cat && cat< List custom domains for app - domains:add DOMAIN Add a custom domain to app - domains:clear Clear all custom domains for app - domains:remove DOMAIN Remove a custom domain from app + domains List custom domains for app + domains:add DOMAIN Add a custom domain to app + domains:clear Clear all custom domains for app + domains:remove DOMAIN Remove a custom domain from app EOF ;; diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index 126c096cd..abb9a94de 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -167,8 +167,8 @@ EOF help | nginx:help) cat && cat< Imports a tarball from stdin; should contain server.crt and server.key - nginx:build-config (Re)builds nginx config for given app + nginx:import-ssl Imports a tarball from stdin; should contain server.crt and server.key + nginx:build-config (Re)builds nginx config for given app EOF ;; diff --git a/plugins/ps/commands b/plugins/ps/commands index 92e28d1cc..f5b824643 100755 --- a/plugins/ps/commands +++ b/plugins/ps/commands @@ -105,14 +105,14 @@ case "$1" in help | ps:help) cat && cat< List processes running in app container(s) - ps:scale = [=] Set how many processes of a given process to run - ps:start Start app container(s) - ps:stop Stop app container(s) - ps:rebuild Rebuild an app - ps:rebuildall Rebuild all apps - ps:restart Restart app container(s) - ps:restartall Restart all deployed app containers + ps List processes running in app container(s) + ps:scale = [=] Set how many processes of a given process to run + ps:start Start app container(s) + ps:stop Stop app container(s) + ps:rebuild Rebuild an app + ps:rebuildall Rebuild all apps + ps:restart Restart app container(s) + ps:restartall Restart all deployed app containers EOF ;; diff --git a/plugins/shell/commands b/plugins/shell/commands index c6dca6ffe..3b1d6d60b 100755 --- a/plugins/shell/commands +++ b/plugins/shell/commands @@ -48,7 +48,7 @@ case "$1" in help | shell:help) cat && cat< $DOKKU_ROOT/ENV" create_app } teardown() { destroy_app - rm -f "$DOKKU_ROOT/ENV" + [[ -f $DOKKU_ROOT/ENV.bak ]] && mv -f $DOKKU_ROOT/ENV.bak $DOKKU_ROOT/ENV +} + +@test "(config) config:set --global" { + run ssh dokku@dokku.me config:set --global test_var=true test_var2=\"hello world\" + echo "output: "$output + echo "status: "$status + assert_success +} + +@test "(config) config:get --global" { + run ssh dokku@dokku.me config:set --global test_var=true test_var2=\"hello world\" test_var3=\"with\\nnewline\" + echo "output: "$output + echo "status: "$status + assert_success + run dokku config:get --global test_var2 + echo "output: "$output + echo "status: "$status + assert_output 'hello world' + run dokku config:get --global test_var3 + echo "output: "$output + echo "status: "$status + assert_output 'with\nnewline' +} + +@test "(config) config:unset --global" { + run ssh dokku@dokku.me config:set --global test_var=true test_var2=\"hello world\" + echo "output: "$output + echo "status: "$status + assert_success + run dokku config:get --global test_var + echo "output: "$output + echo "status: "$status + assert_success + run dokku config:unset --global test_var + echo "output: "$output + echo "status: "$status + assert_success + run dokku config:get --global test_var + echo "output: "$output + echo "status: "$status + assert_output "" } @test "(config) config:set" { From 710c6f4e8839f3f499df85a19d41fd40be98bcfc Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Tue, 7 Jul 2015 12:31:57 -0700 Subject: [PATCH 2/2] don't run dokku again on --* args in config plugin --- plugins/config/commands | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/plugins/config/commands b/plugins/config/commands index 8d0d60f34..fd672833e 100755 --- a/plugins/config/commands +++ b/plugins/config/commands @@ -2,8 +2,6 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x source "$(dirname $0)/../common/functions" -DOKKU_CONFIG_RESTART=${DOKKU_CONFIG_RESTART:=true} - config_create () { [ -f $ENV_FILE ] || { touch $ENV_FILE @@ -24,7 +22,7 @@ config_styled_hash () { while read -r word; do KEY=$(echo $word | cut -d"=" -f1) - VALUE=$(echo $word | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//") + VALUE=$(echo $word | cut -d"=" -f2- | sed -e "s/^'//" -e "s/'$//" -e "s/\$$//g") num_zeros=$((${#longest} - ${#KEY})) zeros=" " @@ -56,19 +54,26 @@ parse_config_args() { DOKKU_CONFIG_RESTART=false ;; --no-restart) - ARGS=("$@") && shift 2 - DOKKU_CONFIG_RESTART=false dokku ${ARGS[0]} "$@" - exit 0 - ;; - *) - ENV_FILE="$DOKKU_ROOT/$2/ENV" + APP="$3" + ENV_FILE="$DOKKU_ROOT/$APP/ENV" + DOKKU_CONFIG_RESTART=false DOKKU_CONFIG_TYPE="app" + set -- "${@:1:1}" "${@:3}" + esac - [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 + APP=${APP:="$2"} + ENV_FILE=${ENV_FILE:="$DOKKU_ROOT/$APP/ENV"} + DOKKU_CONFIG_TYPE=${DOKKU_CONFIG_TYPE:="app"} + DOKKU_CONFIG_RESTART=${DOKKU_CONFIG_RESTART:=true} + + if [[ "$DOKKU_CONFIG_TYPE" = "app" ]]; then + if [[ -z $APP ]]; then + echo "Please specify an app to run the command on" >&2 && exit 1 + else verify_app_name "$2" - APP="$2" - ;; - esac + fi + fi + export APP ENV_FILE DOKKU_CONFIG_TYPE DOKKU_CONFIG_RESTART } case "$1" in @@ -114,6 +119,7 @@ case "$1" in config:set) parse_config_args "$@" + [[ "$2" = "--no-restart" ]] && set -- "${@:1:1}" "${@:3}" if [[ -z "${*:3}" ]]; then echo "Usage: dokku config:set APP KEY1=VALUE1 [KEY2=VALUE2 ...]" @@ -166,6 +172,7 @@ ${var}" config:unset) parse_config_args "$@" + [[ "$2" = "--no-restart" ]] && set -- "${@:1:1}" "${@:3}" if [[ -z $3 ]]; then echo "Usage: dokku config:unset APP KEY1 [KEY2 ...]"