Skip to content

Commit

Permalink
fix: ensure cloned/renamed apps have a link to linked services
Browse files Browse the repository at this point in the history
Previously they would just 'disappear', resulting in broken applications until the app was relinked to the service in question.

Closes dokku/dokku-redis#142
  • Loading branch information
josegonzalez committed Sep 13, 2021
1 parent e0c5f79 commit 14754aa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
25 changes: 22 additions & 3 deletions common-functions
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_AVAILABLE_PATH/config/functions"

add_to_links_file() {
declare desc="add an app to the service link file"
declare SERVICE="$1" APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"

touch "$LINKS_FILE"
sed -i.bak "/^$APP\$/d" "$LINKS_FILE" && rm "$LINKS_FILE.bak"
echo "$APP" >>"$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
}

docker_ports_options() {
declare desc="export a list of exposed ports"
declare PORTS=("$@")
Expand Down Expand Up @@ -61,6 +73,15 @@ get_url_from_config() {
echo "$EXISTING_CONFIG" | grep "$CONFIG_VAR" | sed "s/$CONFIG_VAR:\s*//" | xargs
}

in_links_file() {
declare desc="check if a service LINKS file contains an app"
declare SERVICE="$1" APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"

grep -qE "^$APP\$" "$LINKS_FILE"
}

is_container_status() {
declare desc="return 0 or 1 depending upon whether a given container has a certain status"
declare CID="$1" STATUS="$2"
Expand Down Expand Up @@ -501,9 +522,7 @@ service_link() {

[[ -n $LINK ]] && dokku_log_fail "Already linked as $LINK"
plugn trigger service-action pre-link "$SERVICE" "$APP"
touch "$LINKS_FILE"
echo "$APP" >>"$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
add_to_links_file "$SERVICE" "$APP"

if declare -f -F add_passed_docker_option >/dev/null; then
# shellcheck disable=SC2034
Expand Down
19 changes: 19 additions & 0 deletions post-app-clone-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common-functions"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

plugin-post-app-clone-setup() {
declare OLD_APP_NAME="$1" NEW_APP_NAME="$2"

local SERVICES=$(ls "$PLUGIN_DATA_ROOT" 2>/dev/null)
for SERVICE in $SERVICES; do
if in_links_file "$SERVICE" "$OLD_APP_NAME"; then
add_to_links_file "$SERVICE" "$NEW_APP_NAME"
fi
done
}

plugin-post-app-clone-setup "$@"
19 changes: 19 additions & 0 deletions post-app-rename-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common-functions"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

plugin-post-app-rename-setup() {
declare OLD_APP_NAME="$1" NEW_APP_NAME="$2"

local SERVICES=$(ls "$PLUGIN_DATA_ROOT" 2>/dev/null)
for SERVICE in $SERVICES; do
if in_links_file "$SERVICE" "$OLD_APP_NAME"; then
add_to_links_file "$SERVICE" "$NEW_APP_NAME"
fi
done
}

plugin-post-app-rename-setup "$@"

0 comments on commit 14754aa

Please sign in to comment.