From 59d285f2f17373b310409892022e4352214c0808 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 10 Oct 2018 00:14:08 -0400 Subject: [PATCH] feat: add ability to upgrade service image and image-version --- README.md | 1 + common-functions | 23 +++++++++++++++++++++++ functions | 5 ++--- subcommands/upgrade | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100755 subcommands/upgrade diff --git a/README.md b/README.md index 83a4cdd..df20416 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ postgres:start Start a previously stopped postgres service postgres:stop Stop a running postgres service postgres:unexpose Unexpose a previously exposed postgres service postgres:unlink Unlink the postgres service from the app +postgres:upgrade Upgrade service to the specified version ``` ## usage diff --git a/common-functions b/common-functions index 5ff4442..bbbff15 100755 --- a/common-functions +++ b/common-functions @@ -273,6 +273,18 @@ service_backup_unset_encryption() { rm -rf "$SERVICE_BACKUP_ENCRYPTION_ROOT" } +service_container_rm() { + declare desc="Stops a service and removes the running container" + declare SERVICE="$1" + local SERVICE_NAME="$(get_service_name "$SERVICE")" + + service_stop "$SERVICE" + dokku_log_info2_quiet "Removing container" + if ! docker rm "$SERVICE_NAME" > /dev/null 2>&1; then + dokku_log_fail "Unable to stop container for service $SERVICE" + fi +} + service_enter() { declare desc="enters running app container of specified proc type" declare SERVICE="$1" && shift 1 @@ -300,6 +312,17 @@ service_exposed_ports() { done } +service_image_exists() { + declare desc="Checks if the current image exists" + local IMAGE="$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" + + if [[ "$(docker images -q "$IMAGE" 2> /dev/null)" == "" ]]; then + return 0 + fi + + return 1 +} + service_info() { declare desc="Retrieves information about a given service" declare SERVICE="$1" INFO_FLAG="$2" diff --git a/functions b/functions index da9ddcf..7a9ba73 100755 --- a/functions +++ b/functions @@ -29,7 +29,7 @@ service_create() { service_parse_args "${@:2}" - if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " ; then + if ! service_image_exists "$SERVICE"; then if [[ "$PLUGIN_DISABLE_PULL" == "true" ]]; then dokku_log_warn "${PLUGIN_DISABLE_PULL_VARIABLE} environment variable detected. Not running pull command." 1>&2 dokku_log_warn " docker pull ${IMAGE}" 1>&2 @@ -130,14 +130,13 @@ service_start() { 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) local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")" if [[ -n $PREVIOUS_ID ]]; then docker start "$PREVIOUS_ID" > /dev/null service_port_unpause "$SERVICE" dokku_log_info2 "Container started" - elif $IMAGE_EXISTS && [[ -n "$PASSWORD" ]]; then + elif service_image_exists "$SERVICE" && [[ -n "$PASSWORD" ]]; then service_create_container "$SERVICE" else dokku_log_verbose_quiet "Neither container nor valid configuration exists for $SERVICE" diff --git a/subcommands/upgrade b/subcommands/upgrade new file mode 100755 index 0000000..a518733 --- /dev/null +++ b/subcommands/upgrade @@ -0,0 +1,42 @@ +#!/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-upgrade-cmd() { + #E you can upgrade an existing service to a new image or image-version + #E dokku $PLUGIN_COMMAND_PREFIX:upgrade lolipop + #A service, service to run command against + #F -c|--custom-env "USER=alpha;HOST=beta", semi-colon delimited environment variables to start the service with + #F -i|--image IMAGE, the image name to start the service with + #F -i|--image-version IMAGE_VERSION, the image version to start the service with + declare desc="upgrade service to the specified versions" + local cmd="$PLUGIN_COMMAND_PREFIX:upgrade" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1 + declare SERVICE="$1" CLONE_FLAGS_LIST="${@:2}" + 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" + + 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") + + service_parse_args "${@:2}" + + if ! service_image_exists "$SERVICE"; then + dokku_log_fail "Unable to proceed with upgrade, image ${PLUGIN_IMAGE}:${PLUGIN_IMAGE_VERSION} does not exist" + fi + + dokku_log_info2 "Stopping $SERVICE" + service_container_rm "$NEW_SERVICE" + dokku_log_info2 "Upgrading $SERVICE @ $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" + service_start "$NEW_SERVICE" "${@:2}" + dokku_log_info2 "Done" +} + +service-upgrade-cmd "$@"