From 86f917db79b7bb618abdc9900af46f5264856113 Mon Sep 17 00:00:00 2001 From: Adel Qalieh Date: Fri, 20 Oct 2017 23:25:58 -0400 Subject: [PATCH 1/7] Add configuration option to disable automatic app creation Fixes #2808 --- docs/configuration/environment-variables.md | 3 ++- plugins/git/functions | 17 ++++++++++++++--- tests/unit/20_build-env.bats | 12 ++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index 373601ebc..65191546a 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -104,6 +104,7 @@ The following list config variables have special meaning and can be set in a var | `DOKKU_DEPLOY_BRANCH` | `master` | `dokku config:set` | Branch to deploy by default. | | `DOKKU_DISABLE_PROXY` | none | `dokku proxy:disable`
`dokku proxy:enable` | Disables the proxy in front of your application, resulting in publicly routing the docker container. | | `DOKKU_DISABLE_ANSI_PREFIX_REMOVAL` | none | `dokku config:set`
`/etc/environment`
`~dokku/.dokkurc`
`~dokku/.dokkurc/*` | Disables removal of the ANSI prefix during deploys. Can be used in cases where the client deployer does not understand ansi escape codes. | +| `DOKKU_DISABLE_APP_AUTOCREATION` | none | `dokku config:set` | Disables automatic creation of a non-existent app on deploy. | | `DOKKU_DOCKER_STOP_TIMEOUT` | `10` | `dokku config:set` | Configurable grace period given to the `docker stop` command. If a container has not stopped by this time, a `kill -9` signal or equivalent is sent in order to force-terminate the container. Both the `ps:stop` and `apps:destroy` commands _also_ respect this value. If not specified, the docker defaults for the [docker stop command](https://docs.docker.com/engine/reference/commandline/stop/) will be used.| | `DOKKU_DOCKERFILE_CMD` | dockerfile cmd | `dokku config:set` | | | `DOKKU_DOCKERFILE_CACHE_BUILD` | none | `dokku config:set` | | @@ -119,4 +120,4 @@ The following list config variables have special meaning and can be set in a var | `DOKKU_SKIP_DEPLOY` | | `dokku config:set` | | | `DOKKU_SYSTEM_GROUP` | `dokku` | `/etc/environment`
`~dokku/.dokkurc`
`~dokku/.dokkurc/*` | System group to chown files as. | | `DOKKU_SYSTEM_USER` | `dokku` | `/etc/environment`
`~dokku/.dokkurc`
`~dokku/.dokkurc/*` | System user to chown files as. | -| `DOKKU_WAIT_TO_RETIRE` | `60` | `dokku config:set` | After a successful deploy, the grace period given to old containers before they are stopped/terminated. This is useful for ensuring completion of long-running http connections. | +| `DOKKU_WAIT_TO_RETIRE` | `60` | `dokku config:set` | After a successful deploy, the grace period given to old containers before they are stopped/terminated. This is useful for ensuring completion of long-running http connections. | \ No newline at end of file diff --git a/plugins/git/functions b/plugins/git/functions index 0ae67bd7d..41b05180d 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -23,8 +23,10 @@ use_git_worktree() { git_build_app_repo() { declare desc="builds local git app repo for app" - verify_app_name "$1" - local APP="$1"; local REV="$2" + declare APP="$1"; local REV="$2" + local DOKKU_GLOBAL_DISABLE_AUTOCREATE + + verify_app_name "$APP" # clean up after ourselves local GIT_BUILD_APP_REPO_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku_git.XXXX") @@ -33,7 +35,16 @@ git_build_app_repo() { local TMP_TAG="dokku/$REV" chmod 755 "$GIT_BUILD_APP_REPO_TMP_WORK_DIR" unset GIT_DIR GIT_WORK_TREE - [[ ! -d "$DOKKU_ROOT/$APP" ]] && apps_create "$APP" + + DOKKU_GLOBAL_DISABLE_AUTOCREATE="$(config_get --global DOKKU_DISABLE_APP_AUTOCREATION)" + if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then + if [[ "$DOKKU_GLOBAL_DISABLE_AUTOCREATE" == true ]]; then + dokku_log_warn "Invalid app name '$APP' specified." + dokku_log_fail "Check the name of the app or create an app with 'dokku apps:create ${APP}'" + else + apps_create "$APP" + fi + fi if use_git_worktree; then # git worktree - this method uses git worktree which was introduced in git 2.5 diff --git a/tests/unit/20_build-env.bats b/tests/unit/20_build-env.bats index dc7affbe8..9253627de 100644 --- a/tests/unit/20_build-env.bats +++ b/tests/unit/20_build-env.bats @@ -70,3 +70,15 @@ teardown() { echo "status: "$status assert_output "herokuish" } + +@test "(build-env) app autocreate disabled" { + run dokku config:set --no-restart $TEST_APP DOKKU_DISABLE_APP_AUTOCREATION='true' + echo "output: "$output + echo "status: "$status + assert_success + + run deploy_app + echo "output: "$output + echo "status: "$status + assert_failure +} From db7cc94939753e924868cca00de0d1a0f798ccf8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 4 Nov 2017 01:27:52 -0400 Subject: [PATCH 2/7] fix: ensure config_get capture exits 0 --- plugins/git/functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/functions b/plugins/git/functions index 41b05180d..21fc999ae 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -36,7 +36,7 @@ git_build_app_repo() { chmod 755 "$GIT_BUILD_APP_REPO_TMP_WORK_DIR" unset GIT_DIR GIT_WORK_TREE - DOKKU_GLOBAL_DISABLE_AUTOCREATE="$(config_get --global DOKKU_DISABLE_APP_AUTOCREATION)" + DOKKU_GLOBAL_DISABLE_AUTOCREATE="$(config_get --global DOKKU_DISABLE_APP_AUTOCREATION || true)" if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then if [[ "$DOKKU_GLOBAL_DISABLE_AUTOCREATE" == true ]]; then dokku_log_warn "Invalid app name '$APP' specified." From b5ecdfc0c58b0c73131514f2238acfc66a68cf4e Mon Sep 17 00:00:00 2001 From: Adel Qalieh Date: Sat, 4 Nov 2017 14:37:37 -0400 Subject: [PATCH 3/7] fix: set global config key in tests --- tests/unit/20_build-env.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/20_build-env.bats b/tests/unit/20_build-env.bats index 9253627de..13bc6a235 100644 --- a/tests/unit/20_build-env.bats +++ b/tests/unit/20_build-env.bats @@ -72,7 +72,7 @@ teardown() { } @test "(build-env) app autocreate disabled" { - run dokku config:set --no-restart $TEST_APP DOKKU_DISABLE_APP_AUTOCREATION='true' + run dokku config:set --no-restart --global DOKKU_DISABLE_APP_AUTOCREATION='true' echo "output: "$output echo "status: "$status assert_success From cc0687ec0fc80c0119fb4cbf938c417cb98f42c8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 5 Nov 2017 11:59:29 -0500 Subject: [PATCH 4/7] fix: compare the env var to string true, not boolean true --- plugins/git/functions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/git/functions b/plugins/git/functions index 21fc999ae..5c2c09d46 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -38,7 +38,7 @@ git_build_app_repo() { DOKKU_GLOBAL_DISABLE_AUTOCREATE="$(config_get --global DOKKU_DISABLE_APP_AUTOCREATION || true)" if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - if [[ "$DOKKU_GLOBAL_DISABLE_AUTOCREATE" == true ]]; then + if [[ "$DOKKU_GLOBAL_DISABLE_AUTOCREATE" == "true" ]]; then dokku_log_warn "Invalid app name '$APP' specified." dokku_log_fail "Check the name of the app or create an app with 'dokku apps:create ${APP}'" else From 40b0bdd6e8006f131d45bfb2a249bf2f42a20d0b Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 5 Mar 2018 23:47:10 -0500 Subject: [PATCH 5/7] fix: ensure we call apps_create at all proper locations --- plugins/apps/functions | 17 ++++++++++++++++- plugins/common/functions | 14 ++++++++++++++ plugins/git/functions | 28 +++++----------------------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/plugins/apps/functions b/plugins/apps/functions index 97eafdd00..1bfbf49a2 100755 --- a/plugins/apps/functions +++ b/plugins/apps/functions @@ -11,7 +11,7 @@ apps_create() { [[ -d "$DOKKU_ROOT/$APP" ]] && dokku_log_fail "Name is already taken" mkdir -p "$DOKKU_ROOT/$APP" - echo "Creating $APP... done" + dokku_log_info1_quiet "Creating $APP... done" plugn trigger post-create "$APP" } @@ -58,3 +58,18 @@ apps_exists() { [[ -d "$DOKKU_ROOT/$APP" ]] || dokku_log_fail "App does not exist" dokku_log_info1 "App already exists" } + +apps_maybe_create() { + declare desc="creates an app dir if allowed" + declare APP="$1" + + if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then + DOKKU_GLOBAL_DISABLE_AUTOCREATE="$(config_get --global DOKKU_DISABLE_APP_AUTOCREATION || true)" + if [[ "$DOKKU_GLOBAL_DISABLE_AUTOCREATE" == "true" ]]; then + dokku_log_warn "App auto-creation disabled." + dokku_log_fail " ! Re-enable app auto-creation or create an app with 'dokku apps:create ${APP}'" + else + suppress_output apps_create "$APP" + fi + fi +} \ No newline at end of file diff --git a/plugins/common/functions b/plugins/common/functions index 82f82f077..f1ba349d9 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -1186,3 +1186,17 @@ release_app_deploy_lock() { flock -u "$APP_DEPLOY_LOCK_FD" && rm -f "$APP_DEPLOY_LOCK_FILE" &> /dev/null } + +suppress_output() { + declare desc="suppress all output from a given command unless there is an error" + local TMP_COMMAND_OUTPUT + TMP_COMMAND_OUTPUT=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX") + trap 'rm -rf "$TMP_COMMAND_OUTPUT" > /dev/null' RETURN INT TERM EXIT + + "$@" > "$TMP_COMMAND_OUTPUT" 2>&1 || { + local exit_code="$?" + cat "$TMP_COMMAND_OUTPUT" + return "$exit_code" + } + return 0 +} \ No newline at end of file diff --git a/plugins/git/functions b/plugins/git/functions index 5c2c09d46..e1189448e 100755 --- a/plugins/git/functions +++ b/plugins/git/functions @@ -36,15 +36,7 @@ git_build_app_repo() { chmod 755 "$GIT_BUILD_APP_REPO_TMP_WORK_DIR" unset GIT_DIR GIT_WORK_TREE - DOKKU_GLOBAL_DISABLE_AUTOCREATE="$(config_get --global DOKKU_DISABLE_APP_AUTOCREATION || true)" - if [[ ! -d "$DOKKU_ROOT/$APP" ]]; then - if [[ "$DOKKU_GLOBAL_DISABLE_AUTOCREATE" == "true" ]]; then - dokku_log_warn "Invalid app name '$APP' specified." - dokku_log_fail "Check the name of the app or create an app with 'dokku apps:create ${APP}'" - else - apps_create "$APP" - fi - fi + [[ ! -d "$DOKKU_ROOT/$APP" ]] && apps_maybe_create "$APP" if use_git_worktree; then # git worktree - this method uses git worktree which was introduced in git 2.5 @@ -107,20 +99,6 @@ git_trigger_build() { fi } -suppress_output() { - declare desc="suppress all output from a given command unless there is an error" - local TMP_COMMAND_OUTPUT - TMP_COMMAND_OUTPUT=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX") - trap 'rm -rf "$TMP_COMMAND_OUTPUT" > /dev/null' RETURN INT TERM EXIT - - "$@" > "$TMP_COMMAND_OUTPUT" 2>&1 || { - local exit_code="$?" - cat "$TMP_COMMAND_OUTPUT" - return "$exit_code" - } - return 0 -} - git_deploy_branch() { declare desc="retrieve the deploy branch for a given application" local cmd="git-hook" @@ -207,6 +185,8 @@ git_upload_pack_cmd() { declare APP="$2" APP="$(echo "$APP" | perl -pe 's/(? /dev/null local PRERECEIVE_HOOK="$APP_PATH/hooks/pre-receive" cat > "$PRERECEIVE_HOOK" < Date: Tue, 6 Mar 2018 00:27:48 -0500 Subject: [PATCH 6/7] fix: unset global --- tests/unit/20_build-env.bats | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/20_build-env.bats b/tests/unit/20_build-env.bats index 13bc6a235..c66b5d99a 100644 --- a/tests/unit/20_build-env.bats +++ b/tests/unit/20_build-env.bats @@ -81,4 +81,5 @@ teardown() { echo "output: "$output echo "status: "$status assert_failure + run dokku config:unset --no-restart --global DOKKU_DISABLE_APP_AUTOCREATION } From 5eb95dd6ab2b77d01a85798cd87797a5e43738d8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 6 Mar 2018 01:04:24 -0500 Subject: [PATCH 7/7] fix: correct tests by moving to correct file --- tests/unit/10_apps.bats | 13 +++++++++++++ tests/unit/20_build-env.bats | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/unit/10_apps.bats b/tests/unit/10_apps.bats index 5fb722f6b..aa4052802 100644 --- a/tests/unit/10_apps.bats +++ b/tests/unit/10_apps.bats @@ -53,6 +53,19 @@ teardown () { destroy_app } +@test "(apps) app autocreate disabled" { + run dokku config:set --no-restart --global DOKKU_DISABLE_APP_AUTOCREATION='true' + echo "output: "$output + echo "status: "$status + assert_success + + run deploy_app + echo "output: "$output + echo "status: "$status + assert_failure + run dokku config:unset --no-restart --global DOKKU_DISABLE_APP_AUTOCREATION +} + @test "(apps) apps:destroy" { create_app run bash -c "dokku --force apps:destroy $TEST_APP" diff --git a/tests/unit/20_build-env.bats b/tests/unit/20_build-env.bats index c66b5d99a..dc7affbe8 100644 --- a/tests/unit/20_build-env.bats +++ b/tests/unit/20_build-env.bats @@ -70,16 +70,3 @@ teardown() { echo "status: "$status assert_output "herokuish" } - -@test "(build-env) app autocreate disabled" { - run dokku config:set --no-restart --global DOKKU_DISABLE_APP_AUTOCREATION='true' - echo "output: "$output - echo "status: "$status - assert_success - - run deploy_app - echo "output: "$output - echo "status: "$status - assert_failure - run dokku config:unset --no-restart --global DOKKU_DISABLE_APP_AUTOCREATION -}