From fdb90b85a351685a8ca1bb5ac4dda786a3098996 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 24 Apr 2018 00:27:28 -0400 Subject: [PATCH] refactor: move unimplemented command detection into config file --- LICENSE.txt | 2 +- common-functions | 20 ++++++++++++++++++-- config | 1 + functions | 2 +- help-functions | 17 +++-------------- subcommands/backup | 1 + subcommands/backup-auth | 1 + subcommands/backup-deauth | 1 + subcommands/backup-schedule | 1 + subcommands/backup-set-encryption | 1 + subcommands/backup-unschedule | 1 + subcommands/backup-unset-encryption | 1 + subcommands/clone | 8 +++++++- subcommands/connect | 3 ++- subcommands/destroy | 4 ++-- subcommands/export | 1 + subcommands/import | 1 + 17 files changed, 44 insertions(+), 22 deletions(-) diff --git a/LICENSE.txt b/LICENSE.txt index 0946d65..b10f8e5 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (C) 2015 Jose Diaz-Gonzalez +Copyright (C) 2018 Jose Diaz-Gonzalez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/common-functions b/common-functions index 2f14abb..2e1ff12 100755 --- a/common-functions +++ b/common-functions @@ -67,6 +67,22 @@ is_container_status() { return 1 } +is_implemented_command() { + declare desc="return true if value ($1) is in list (all other arguments)" + declare CMD="$1" + CMD="$(echo "$CMD" | cut -d ':' -f2)" + + if [[ ${#PLUGIN_UNIMPLEMENTED_SUBCOMMANDS[@]} -eq 0 ]]; then + return 0 + fi + + local e + for e in "${PLUGIN_UNIMPLEMENTED_SUBCOMMANDS[@]}"; do + [[ "$e" == "$CMD" ]] && return 1 + done + return 0 +} + remove_from_links_file() { declare desc="Removes an app from the service link file" declare SERVICE="$1" APP="$2" @@ -598,10 +614,10 @@ service_stop() { [[ -z $ID ]] && dokku_log_warn "Service is already stopped" && return 0 if [[ -n $ID ]]; then - dokku_log_info1_quiet "Stopping container" + dokku_log_info2_quiet "Stopping container" docker stop "$SERVICE_NAME" > /dev/null service_port_pause "$SERVICE" - dokku_log_info2 "Container stopped" + dokku_log_verbose_quiet "Container stopped" else dokku_log_verbose_quiet "No container exists for $SERVICE" fi diff --git a/config b/config index ceedf3a..64b5736 100755 --- a/config +++ b/config @@ -4,6 +4,7 @@ export REDIS_IMAGE_VERSION=${REDIS_IMAGE_VERSION:="4.0.8"} export REDIS_ROOT=${REDIS_ROOT:="/var/lib/dokku/services/redis"} export REDIS_HOST_ROOT=${REDIS_HOST_ROOT:=$REDIS_ROOT} +export PLUGIN_UNIMPLEMENTED_SUBCOMMANDS=() export PLUGIN_COMMAND_PREFIX="redis" export PLUGIN_CONFIG_ROOT=${PLUGIN_CONFIG_ROOT:="$DOKKU_LIB_ROOT/config/$PLUGIN_COMMAND_PREFIX"} export PLUGIN_DATA_ROOT=$REDIS_ROOT diff --git a/functions b/functions index 11f48c8..64def9f 100755 --- a/functions +++ b/functions @@ -120,7 +120,7 @@ service_start() { return 0 fi - dokku_log_info1_quiet "Starting container" + dokku_log_info2_quiet "Starting container" local PREVIOUS_ID=$(docker ps -f status=exited | grep -e "$SERVICE_NAME$" | awk '{print $1}') || true local IMAGE_EXISTS=$(docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " && true) diff --git a/help-functions b/help-functions index 159bd97..f87209c 100755 --- a/help-functions +++ b/help-functions @@ -1,6 +1,7 @@ #!/usr/bin/env bash +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config" set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x - +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common-functions" export SUBCOMMAND_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/subcommands" fn-help() { @@ -83,7 +84,7 @@ fn-help-contents-subcommand() { cat "$SUBCOMMAND_ROOT/$SUBCOMMAND" > "$UNCLEAN_FILE" fn-help-subcommand-sanitize "$UNCLEAN_FILE" "$CLEAN_FILE" - if [[ "$(fn-help-is-subcommand-unimplemented "$CLEAN_FILE")" == true ]]; then + if ! is_implemented_command "$SUBCOMMAND"; then return 1 fi @@ -138,18 +139,6 @@ fn-help-contents-subcommand() { return 0 } -fn-help-is-subcommand-unimplemented() { - declare FUNC_FILE="$1" - local UNIMPLEMENTED - - UNIMPLEMENTED="$(grep "Not yet implemented" "$FUNC_FILE" | head -1 || true)" - if [[ -n "$UNIMPLEMENTED" ]]; then - echo true - else - echo false - fi -} - fn-help-list-example() { # shellcheck disable=SC2034 declare desc="return $PLUGIN_COMMAND_PREFIX plugin help content" diff --git a/subcommands/backup b/subcommands/backup index a35a71a..ff52626 100755 --- a/subcommands/backup +++ b/subcommands/backup @@ -13,6 +13,7 @@ service-backup-cmd() { declare desc="creates a backup of the $PLUGIN_SERVICE service to an existing s3 bucket" local cmd="$PLUGIN_COMMAND_PREFIX:backup" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" BUCKET_NAME="$2" USE_IAM_OPTIONAL_FLAG="$3" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ -z "$BUCKET_NAME" ]] && dokku_log_fail "Please specify an aws bucket for the backup" diff --git a/subcommands/backup-auth b/subcommands/backup-auth index 1e7de4b..3bf97a1 100755 --- a/subcommands/backup-auth +++ b/subcommands/backup-auth @@ -22,6 +22,7 @@ service-backup-auth-cmd() { declare desc="sets up authentication for backups on the $PLUGIN_SERVICE service" local cmd="$PLUGIN_COMMAND_PREFIX:backup-auth" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" AWS_ACCESS_KEY_ID="$2" AWS_SECRET_ACCESS_KEY="$3" AWS_DEFAULT_REGION="$4" AWS_SIGNATURE_VERSION="$5" ENDPOINT_URL="$6" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ -z "$AWS_ACCESS_KEY_ID" ]] && dokku_log_fail "Please specify an aws access key id" diff --git a/subcommands/backup-deauth b/subcommands/backup-deauth index 3eb79cf..a8f3fac 100755 --- a/subcommands/backup-deauth +++ b/subcommands/backup-deauth @@ -11,6 +11,7 @@ service-backup-deauth-cmd() { declare desc="removes backup authentication for the $PLUGIN_SERVICE service" local cmd="$PLUGIN_COMMAND_PREFIX:backup-deauth" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" diff --git a/subcommands/backup-schedule b/subcommands/backup-schedule index 8e89877..a95c478 100755 --- a/subcommands/backup-schedule +++ b/subcommands/backup-schedule @@ -17,6 +17,7 @@ service-backup-schedule-cmd() { declare desc="schedules a backup of the $PLUGIN_SERVICE service" local cmd="$PLUGIN_COMMAND_PREFIX:backup-schedule" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" SCHEDULE="$2" BUCKET_NAME="$3" USE_IAM_OPTIONAL_FLAG="$4" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ -z "$SCHEDULE" ]] && dokku_log_fail "Please specify a schedule for the backup" diff --git a/subcommands/backup-set-encryption b/subcommands/backup-set-encryption index a3b3711..175d716 100755 --- a/subcommands/backup-set-encryption +++ b/subcommands/backup-set-encryption @@ -12,6 +12,7 @@ service-backup-set-encryption-cmd() { declare desc="sets encryption for all future backups of $PLUGIN_SERVICE service" local cmd="$PLUGIN_COMMAND_PREFIX:backup-set-encryption" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" ENCRYPTION_KEY="$2" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ -z "$ENCRYPTION_KEY" ]] && dokku_log_fail "Please specify a GPG encryption key" diff --git a/subcommands/backup-unschedule b/subcommands/backup-unschedule index d3f3b15..73d902b 100755 --- a/subcommands/backup-unschedule +++ b/subcommands/backup-unschedule @@ -11,6 +11,7 @@ service-backup-unschedule-cmd() { declare desc="unschedules the backup of the $PLUGIN_SERVICE service" local cmd="$PLUGIN_COMMAND_PREFIX:backup-unschedule" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" diff --git a/subcommands/backup-unset-encryption b/subcommands/backup-unset-encryption index fcd8a3f..619772c 100755 --- a/subcommands/backup-unset-encryption +++ b/subcommands/backup-unset-encryption @@ -11,6 +11,7 @@ service-backup-unset-encryption-cmd() { declare desc="unsets encryption for future backups of the $PLUGIN_SERVICE service" local cmd="$PLUGIN_COMMAND_PREFIX:backup-unset-encryption" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" diff --git a/subcommands/clone b/subcommands/clone index 82510ca..04a369e 100755 --- a/subcommands/clone +++ b/subcommands/clone @@ -12,18 +12,24 @@ service-clone-cmd() { declare desc="create container then copy data from into " local cmd="$PLUGIN_COMMAND_PREFIX:clone" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" NEW_SERVICE="$2" CLONE_FLAGS_LIST="${@:3}" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ -z "$NEW_SERVICE" ]] && dokku_log_fail "Please specify a name for the new service" verify_service_name "$SERVICE" + local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE" + local ID="$(cat "$SERVICE_ROOT/ID")" + is_container_status "$ID" "Running" || dokku_log_fail "Service ${SERVICE} container is not running" + PLUGIN_IMAGE=$(service_version "$SERVICE" | grep -o "^.*:" | sed -r "s/://g") PLUGIN_IMAGE_VERSION=$(service_version "$SERVICE" | grep -o ":.*$" | sed -r "s/://g") + dokku_log_info2 "Cloning $SERVICE to $NEW_SERVICE" service_create "$NEW_SERVICE" "${@:3}" dokku_log_info1 "Copying data from $SERVICE to $NEW_SERVICE" service_export "$SERVICE" | service_import "$NEW_SERVICE" > /dev/null 2>&1 || true - dokku_log_info1 "Done" + dokku_log_info2 "Done" } service-clone-cmd "$@" diff --git a/subcommands/connect b/subcommands/connect index da0a508..10bf500 100755 --- a/subcommands/connect +++ b/subcommands/connect @@ -8,9 +8,10 @@ service-connect-cmd() { #E connect to the service via the $PLUGIN_COMMAND_PREFIX connection tool #E dokku $PLUGIN_COMMAND_PREFIX:connect lolipop #A service, service to run command against - declare desc="connect via redis-cli to a $PLUGIN_SERVICE service" + declare desc="connect to the service via the $PLUGIN_COMMAND_PREFIX connection tool" local cmd="$PLUGIN_COMMAND_PREFIX:connect" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" diff --git a/subcommands/destroy b/subcommands/destroy index a022b9b..a031f48 100755 --- a/subcommands/destroy +++ b/subcommands/destroy @@ -37,7 +37,7 @@ service-destroy-cmd() { fi fi - dokku_log_info1 "Deleting $SERVICE" + dokku_log_info2_quiet "Deleting $SERVICE" if [[ -n $(docker ps -aq -f name="$SERVICE_NAME") ]]; then dokku_log_verbose_quiet "Deleting container data" service_stop "$SERVICE" @@ -51,7 +51,7 @@ service-destroy-cmd() { fi dokku_log_verbose_quiet "Removing data" - docker run --rm -v "$SERVICE_HOST_ROOT/data:/data" -v "$SERVICE_HOST_ROOT/config:/config" "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" chmod 777 -R /config /data + docker run --rm -v "$SERVICE_HOST_ROOT/data:/data" -v "$SERVICE_HOST_ROOT/config:/config" busybox chmod 777 -R /config /data rm -rf "$SERVICE_ROOT" dokku_log_info2 "$PLUGIN_SERVICE container deleted: $SERVICE" diff --git a/subcommands/export b/subcommands/export index b8da419..84ebec3 100755 --- a/subcommands/export +++ b/subcommands/export @@ -13,6 +13,7 @@ service-export-cmd() { declare desc="export a dump of the $PLUGIN_SERVICE service database" local cmd="$PLUGIN_COMMAND_PREFIX:export" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE" diff --git a/subcommands/import b/subcommands/import index e12edd0..28f85d9 100755 --- a/subcommands/import +++ b/subcommands/import @@ -11,6 +11,7 @@ service-import-cmd() { declare desc="import a dump into the $PLUGIN_SERVICE service database" local cmd="$PLUGIN_COMMAND_PREFIX:import" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 declare SERVICE="$1" + is_implemented_command "$cmd" || dokku_log_fail "Not yet implemented" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" verify_service_name "$SERVICE"