From 1f4884a44a1f7f77b2c37e72dd129cb244bd6605 Mon Sep 17 00:00:00 2001 From: Tan Jay Jun Date: Sat, 12 Jan 2019 20:50:45 +0800 Subject: [PATCH] Add postgres:reset command --- README.md | 4 ++++ common-functions | 20 ++++++++++++++++++++ functions | 11 +++++++++++ subcommands/destroy | 17 ++--------------- subcommands/reset | 25 +++++++++++++++++++++++++ tests/service_reset.bats | 18 ++++++++++++++++++ 6 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 subcommands/reset create mode 100644 tests/service_reset.bats diff --git a/README.md b/README.md index 33939b2..11964ad 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ postgres:list List all postgres services postgres:logs [-t] Print the most recent log(s) for this service postgres:promote Promote service as DATABASE_URL in postgres:restart Graceful shutdown and restart of the postgres service container +postgres:reset Delete all data in the postgres service postgres:start Start a previously stopped postgres service postgres:stop Stop a running postgres service postgres:unexpose Unexpose a previously exposed postgres service @@ -152,6 +153,9 @@ dokku postgres:import lolipop < database.dump # you can clone an existing database to a new one dokku postgres:clone lolipop new_database +# you can reset a database to delete all its data +dokku postgres:reset lolipop + # finally, you can destroy the container dokku postgres:destroy lolipop ``` diff --git a/common-functions b/common-functions index d126f3d..344e35a 100755 --- a/common-functions +++ b/common-functions @@ -83,6 +83,26 @@ is_implemented_command() { return 0 } +prompt_confirmation() { + declare desc="Prompts user to confirm destructive action" + declare MESSAGE="$1" SERVICE="$2" FORCE_FLAG="$3" + + if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then + DOKKU_APPS_FORCE_DELETE=1 + fi + if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then + dokku_log_warn "WARNING: Potentially Destructive Action" + dokku_log_warn "$MESSAGE" + dokku_log_warn "To proceed, type \"$SERVICE\"" + echo "" + + read -rp "> " service_name + if [[ "$service_name" != "$SERVICE" ]]; then + dokku_log_fail "Confirmation did not match $SERVICE. Aborted." + fi + fi +} + remove_from_links_file() { declare desc="Removes an app from the service link file" declare SERVICE="$1" APP="$2" diff --git a/functions b/functions index 4abc96f..2c4a8cc 100755 --- a/functions +++ b/functions @@ -117,6 +117,17 @@ service_import() { docker exec -i "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_restore -h localhost -cO --if-exists -d "$DATABASE_NAME" -U postgres -w } +service_reset() { + local SERVICE="$1" + local SERVICE_NAME="$(get_service_name "$SERVICE")" + local DATABASE_NAME="$(get_database_name "$SERVICE")" + + dokku_log_info2_quiet "Deleting all data in $SERVICE" + docker exec "$SERVICE_NAME" su - postgres -c "dropdb $DATABASE_NAME" + docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME" + dokku_log_info2 "All $SERVICE data deleted" +} + service_start() { local SERVICE="$1" local QUIET="$2" diff --git a/subcommands/destroy b/subcommands/destroy index 8854f6d..4077535 100755 --- a/subcommands/destroy +++ b/subcommands/destroy @@ -21,21 +21,8 @@ service-destroy-cmd() { [[ -s "$LINKS_FILE" ]] && dokku_log_fail "Cannot delete linked service" - if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then - DOKKU_APPS_FORCE_DELETE=1 - fi - if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then - dokku_log_warn "WARNING: Potentially Destructive Action" - dokku_log_warn "This command will destroy $SERVICE $PLUGIN_SERVICE service." - dokku_log_warn "To proceed, type \"$SERVICE\"" - echo "" - - read -rp "> " service_name - if [[ "$service_name" != "$SERVICE" ]]; then - dokku_log_warn "Confirmation did not match $SERVICE. Aborted." - exit 1 - fi - fi + local message="This command will destroy $SERVICE $PLUGIN_SERVICE service." + prompt_confirmation "$message" "$SERVICE" "$FORCE_FLAG" dokku_log_info2_quiet "Deleting $SERVICE" service_backup_unschedule "$SERVICE" diff --git a/subcommands/reset b/subcommands/reset new file mode 100644 index 0000000..a2e499e --- /dev/null +++ b/subcommands/reset @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config" +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_BASE_PATH/common/functions" +source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions" + +service-reset-cmd() { + #E delete all data in $PLUGIN_COMMAND_PREFIX service named lolipop + #E dokku $PLUGIN_COMMAND_PREFIX:reset lolipop + #A service, service to run command against + #F -f|--force, force delete without asking for confirmation + declare desc="delete all data in $PLUGIN_SERVICE service" + local cmd="$PLUGIN_COMMAND_PREFIX:reset" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 + declare SERVICE="$1" FORCE_FLAG="$2" + + [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" + verify_service_name "$SERVICE" + + local message="This command will delete all data in $SERVICE $PLUGIN_SERVICE service." + prompt_confirmation "$message" "$SERVICE" "$FORCE_FLAG" + + service_reset "$SERVICE" +} + +service-reset-cmd "$@" diff --git a/tests/service_reset.bats b/tests/service_reset.bats new file mode 100644 index 0000000..b7a4fab --- /dev/null +++ b/tests/service_reset.bats @@ -0,0 +1,18 @@ +#!/usr/bin/env bats +load test_helper + +@test "($PLUGIN_COMMAND_PREFIX:reset) success with --force" { + dokku "$PLUGIN_COMMAND_PREFIX:create" l + run dokku --force "$PLUGIN_COMMAND_PREFIX:reset" l + assert_contains "${lines[*]}" "All l data deleted" +} + +@test "($PLUGIN_COMMAND_PREFIX:reset) error when there are no arguments" { + run dokku "$PLUGIN_COMMAND_PREFIX:reset" + assert_contains "${lines[*]}" "Please specify a name for the service" +} + +@test "($PLUGIN_COMMAND_PREFIX:reset) error when service does not exist" { + run dokku "$PLUGIN_COMMAND_PREFIX:reset" not_existing_service + assert_contains "${lines[*]}" "service not_existing_service does not exist" +}