From bb7d44f39e2e5a77634ae864c95afc09fe03756f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Tue, 29 Sep 2015 20:06:53 +0200 Subject: [PATCH] Remove linked app from links file when destroying app --- .gitignore | 1 + functions | 19 +++++++++++++------ pre-delete | 16 ++++++++++++++++ tests/bin/sudo | 2 ++ tests/hook_pre_delete.bats | 20 ++++++++++++++++++++ tests/setup.sh | 9 +++++++++ tests/test_helper.bash | 1 + 7 files changed, 62 insertions(+), 6 deletions(-) create mode 100755 pre-delete create mode 100755 tests/bin/sudo create mode 100755 tests/hook_pre_delete.bats diff --git a/.gitignore b/.gitignore index c604a48..a722348 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ tests/dokku tests/fixtures +tests/bin/plugn diff --git a/functions b/functions index 9280610..cec929e 100755 --- a/functions +++ b/functions @@ -257,18 +257,13 @@ service_unlink() { local SERVICE="$1" local SERVICE_URL=$(service_url "$SERVICE") local SERVICE_NAME=$(get_service_name "$SERVICE") - local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" local EXISTING_CONFIG=$(dokku config "$APP") local DATABASE_NAME=$(get_database_name "$SERVICE") local SERVICE_ALIAS=$(service_alias "$SERVICE") local LINK=($(echo "$EXISTING_CONFIG" | grep "$PLUGIN_SCHEME://.*/$DATABASE_NAME" | cut -d: -f1)) || true - local LINKS_FILE="$SERVICE_ROOT/LINKS" [[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP" - mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory" - touch "$LINKS_FILE" - sed -i "/^$APP\$/d" "$LINKS_FILE" - sort "$LINKS_FILE" -u -o "$LINKS_FILE" + remove_from_links_file "$SERVICE" "$APP" dokku docker-options:remove "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS" dokku config:unset "$APP" "${LINK[*]}" @@ -343,3 +338,15 @@ promote() { NEW_CONFIG_VARS+="$PLUGIN_DEFAULT_CONFIG_VAR=$PROMOTE_URL" dokku config:set "$APP" $NEW_CONFIG_VARS } + +remove_from_links_file() { + local SERVICE="$1" + local APP="$2" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + local LINKS_FILE="$SERVICE_ROOT/LINKS" + + mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory" + touch "$LINKS_FILE" + sed -i "/^$APP\$/d" "$LINKS_FILE" + sort "$LINKS_FILE" -u -o "$LINKS_FILE" +} diff --git a/pre-delete b/pre-delete new file mode 100755 index 0000000..22897dc --- /dev/null +++ b/pre-delete @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x + +PLUGIN_BASE_PATH="$PLUGIN_PATH" +if [[ -n $DOKKU_API_VERSION ]]; then + PLUGIN_BASE_PATH="$PLUGIN_ENABLED_PATH" +fi +source "$PLUGIN_BASE_PATH/common/functions" +source "$(dirname "$0")/functions" +source "$(dirname "$0")/config" + +APP="$1" +for SERVICE in "$PLUGIN_DATA_ROOT"/*; do + remove_from_links_file "$(basename "$SERVICE")" "$APP" +done +exit 0 diff --git a/tests/bin/sudo b/tests/bin/sudo new file mode 100755 index 0000000..742e13d --- /dev/null +++ b/tests/bin/sudo @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +exit 0 diff --git a/tests/hook_pre_delete.bats b/tests/hook_pre_delete.bats new file mode 100755 index 0000000..62b79f4 --- /dev/null +++ b/tests/hook_pre_delete.bats @@ -0,0 +1,20 @@ +#!/usr/bin/env bats +load test_helper + +setup() { + dokku apps:create my_app >&2 + dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2 + dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app >&2 +} + +teardown() { + dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app >&2 + dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2 + rm "$DOKKU_ROOT/my_app" -rf +} + +@test "($PLUGIN_COMMAND_PREFIX:hook:pre-delete) removes app from links file when destroying app" { + [[ -n $(< "$PLUGIN_DATA_ROOT/l/LINKS") ]] + dokku --force apps:destroy my_app + [[ -z $(< "$PLUGIN_DATA_ROOT/l/LINKS") ]] +} diff --git a/tests/setup.sh b/tests/setup.sh index a29514f..e5e0e5e 100644 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -1,6 +1,8 @@ #!/usr/bin/env bash source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/test_helper.bash" +BIN_STUBS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/bin" + if [[ ! -d $DOKKU_ROOT ]]; then git clone https://github.com/progrium/dokku.git $DOKKU_ROOT > /dev/null fi @@ -13,3 +15,10 @@ cd - rm -rf $DOKKU_ROOT/plugins/service mkdir -p $DOKKU_ROOT/plugins/service find ./ -maxdepth 1 -type f -exec cp '{}' $DOKKU_ROOT/plugins/service \; + +if [[ ! -f $BIN_STUBS/plugn ]]; then + wget -O- "$PLUGN_URL" | tar xzf - -C "$BIN_STUBS" + plugn init + ln -s "$DOKKU_ROOT"/plugins/* "$DOKKU_ROOT"/plugins/available + ln -s "$DOKKU_ROOT"/plugins/* "$DOKKU_ROOT"/plugins/enabled +fi diff --git a/tests/test_helper.bash b/tests/test_helper.bash index 5b7aede..f728ad9 100644 --- a/tests/test_helper.bash +++ b/tests/test_helper.bash @@ -10,6 +10,7 @@ export PLUGIN_AVAILABLE_PATH="$PLUGIN_PATH" export PLUGIN_CORE_AVAILABLE_PATH="$PLUGIN_PATH" export POSTGRES_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/fixtures" export PLUGIN_DATA_ROOT="$POSTGRES_ROOT" +export PLUGN_URL="https://github.com/progrium/plugn/releases/download/v0.1.0/plugn_0.1.0_linux_x86_64.tgz" mkdir -p "$PLUGIN_DATA_ROOT" rm -rf "${PLUGIN_DATA_ROOT:?}"/*