Revamp link/unlink commands

Previously we were exporting `MONGO_URL` via the docker-args* hooks.
This seems to confuse our users (since the env var is not displayed
when calling `dokku config`) and in some cases it also seems that the
env var is not correctly set.
Another problem is if several services are linked to the same app and
if they are exporting `MONGO_URL` as well. Then we don’t know what
will be set.

To resolve theses issues, this patch changes the way we manage the env
vars. We use standard dokku commands (`config` and `docker-options`) so
config is set on the linked application and can be reviewed by the user
easily.
We also handle the case where `MONGO_URL` is already set on the
linked application. When it’s the case, we automatically generate
another env var based on the following pattern: DOKKU_<service
name>_<random unused color>_URL. For example, this can give:
DOKKU_MONGO_BLACK_URL.

Since naming is now handled automatically, the `alias` command has been
removed. If the user wants to set a different env var on her app, it’s
just a matter of using `dokku config:set` and pasting the wanted value.

IP in DSN has been removed in favor of host name exported by docker in
the container. This is more robust and simpler since the IP can change
but the name will remain the same if the service container restarts for
instance.

With all those changes, a new command has been introduced: `promote`.
The goal of this command is to easily set a service as the primary one
when several are linked to an app. (see README for an example)
This commit is contained in:
Loïc Guitaut
2015-09-22 18:13:57 +02:00
parent 9038ffc684
commit 5eed4378aa
18 changed files with 284 additions and 119 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
tests/dokku
tests/fixtures
tests/bin/plugn

View File

@@ -22,7 +22,6 @@ dokku plugin:install https://github.com/dokku/dokku-mongo.git mongo
## commands
```
mongo:alias <name> <alias> Set an alias for the docker link
mongo:clone <name> <new-name> Create container <new-name> then copy data from <name> into <new-name>
mongo:connect <name> Connect via telnet to a mongo service
mongo:create <name> Create a mongo service
@@ -34,6 +33,7 @@ mongo:info <name> Print the connection information
mongo:link <name> <app> Link the mongo service to the app
mongo:list List all mongo services
mongo:logs <name> [-t] Print the most recent log(s) for this service
mongo:promote <name> <app> Promote service <name> as MONGO_URL in <app>
mongo:restart <name> Graceful shutdown and restart of the mongo service container
mongo:start <name> Start a previously stopped mongo service
mongo:stop <name> Stop a running mongo service
@@ -57,8 +57,6 @@ dokku mongo:create lolipop
# get connection information as follows
dokku mongo:info lolipop
# lets assume the ip of our mongo service is 172.17.0.1
# a mongo service can be linked to a
# container this will use native docker
# links via the docker-options plugin
@@ -68,24 +66,42 @@ dokku mongo:link lolipop playground
# the above will expose the following environment variables
#
# MONGO_URL=mongo://l:PASSWORD@172.17.0.1:27017/lolipop
# MONGO_NAME=/lolipop/DATABASE
# MONGO_PORT=tcp://172.17.0.1:27017
# MONGO_PORT_27017_TCP=tcp://172.17.0.1:27017
# MONGO_PORT_27017_TCP_PROTO=tcp
# MONGO_PORT_27017_TCP_PORT=27017
# MONGO_PORT_27017_TCP_ADDR=172.17.0.1
# DOKKU_MONGO_LOLIPOP_NAME=/lolipop/DATABASE
# DOKKU_MONGO_LOLIPOP_PORT=tcp://172.17.0.1:27017
# DOKKU_MONGO_LOLIPOP_PORT_27017_TCP=tcp://172.17.0.1:27017
# DOKKU_MONGO_LOLIPOP_PORT_27017_TCP_PROTO=tcp
# DOKKU_MONGO_LOLIPOP_PORT_27017_TCP_PORT=27017
# DOKKU_MONGO_LOLIPOP_PORT_27017_TCP_ADDR=172.17.0.1
#
# and the following will be set on the linked application by default
#
# MONGO_URL=mongodb://lolipop:SOME_PASSWORD@dokku-mongo-lolipop:27017/lolipop
#
# NOTE: the host exposed here only works internally in docker containers. If
# you want your container to be reachable from outside, you should use `expose`.
# you can examine the environment variables
# using our 'playground' app's env command
dokku run playground env
# another service can be linked to your app
dokku mongo:link other_service playground
# you can customize the environment
# variables through a custom docker link alias
dokku mongo:alias lolipop MONGO_DATABASE
# since DATABASE_URL is already in use, another environment variable will be
# generated automatically
#
# DOKKU_MONGO_BLUE_URL=mongodb://other_service:ANOTHER_PASSWORD@dokku-mongo-other-service:27017/other_service
# you can then promote the new service to be the primary one
# NOTE: this will restart your app
dokku mongo:promote other_service playground
# this will replace MONGO_URL with the url from other_service and generate
# another environment variable to hold the previous value if necessary.
# you could end up with the following for example:
#
# MONGO_URL=mongodb://other_service:ANOTHER_PASSWORD@dokku-mongo-other-service:27017/other_service
# DOKKU_MONGO_BLUE_URL=mongodb://other_service:ANOTHER_PASSWORD@dokku-mongo-other-service:27017/other_service
# DOKKU_MONGO_SILVER_URL=mongodb://lolipop:SOME_PASSWORD@dokku-mongo-lolipop:27017/lolipop
# you can also unlink a mongo service
# NOTE: this will restart your app
# NOTE: this will restart your app and unset related environment variables
dokku mongo:unlink lolipop playground
# you can tail logs for a particular service

View File

@@ -16,13 +16,6 @@ if [[ $1 == $PLUGIN_COMMAND_PREFIX:* ]]; then
fi
case "$1" in
$PLUGIN_COMMAND_PREFIX:alias)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ -z $3 ]] && dokku_log_fail "Please specify an alias for the service"
verify_service_name "$2"
service_set_alias "$2" "$3"
;;
$PLUGIN_COMMAND_PREFIX:create)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ ! -d "$PLUGIN_DATA_ROOT/$2" ]] || dokku_log_fail "$PLUGIN_SERVICE service $2 already exists"
@@ -205,9 +198,16 @@ case "$1" in
service_port_unexpose "$2"
;;
$PLUGIN_COMMAND_PREFIX:promote)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ -z $3 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_service_name "$2"
verify_app_name "$3"
promote "$2" "$3"
;;
help | $PLUGIN_COMMAND_PREFIX:help)
HELP=$(cat<<EOF
$PLUGIN_COMMAND_PREFIX:alias <name> <alias>, Set an alias for the docker link
$PLUGIN_COMMAND_PREFIX:create <name>, Create a $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:destroy <name>, Delete the $PLUGIN_SERVICE service and stop its container if there are no links left
$PLUGIN_COMMAND_PREFIX:link <name> <app>, Link the $PLUGIN_SERVICE service to the app
@@ -224,6 +224,7 @@ case "$1" in
$PLUGIN_COMMAND_PREFIX:unexpose <name>, Unexpose a previously exposed $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:start <name>, Start a previously stopped $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:stop <name>, Stop a running $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:promote <name> <app>, Promote service <name> as ${PLUGIN_DEFAULT_ALIAS}_URL in <app>
EOF
)
if [[ -n $DOKKU_API_VERSION ]]; then

1
config
View File

@@ -7,6 +7,7 @@ export PLUGIN_COMMAND_PREFIX="mongo"
export PLUGIN_DATA_ROOT=$MONGO_ROOT
export PLUGIN_DATASTORE_PORTS=(27017 27018 27019 28017)
export PLUGIN_DEFAULT_ALIAS="MONGO"
export PLUGIN_ALT_ALIAS="DOKKU_MONGO"
export PLUGIN_IMAGE=$MONGO_IMAGE
export PLUGIN_IMAGE_VERSION=$MONGO_IMAGE_VERSION
export PLUGIN_SCHEME="mongodb"

View File

@@ -1 +0,0 @@
docker-args-deploy

View File

@@ -1,31 +0,0 @@
#!/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"
STDIN=$(cat)
APP="$1"
output=""
for i in $PLUGIN_DATA_ROOT/*; do
[[ -d $i ]] || continue
SERVICE=$(echo "$i" | cut -d'/' -f 7)
LINKS_FILE="$PLUGIN_DATA_ROOT/$SERVICE/LINKS"
ALIAS="$(service_alias "$SERVICE")"
SERVICE_URL="$(service_url "$SERVICE")"
if [[ -f "$LINKS_FILE" ]]; then
while read line; do
if [[ "$line" == "$APP" ]]; then
output="$output --link dokku.mongo.$SERVICE:$ALIAS --env ${ALIAS}_URL=$SERVICE_URL"
break
fi
done < "$LINKS_FILE"
fi
done
echo "$STDIN$output"

View File

@@ -1 +0,0 @@
docker-args-deploy

107
functions
View File

@@ -32,14 +32,8 @@ verify_service_name() {
service_alias() {
local SERVICE="$1"
local ALIAS_FILE="$PLUGIN_DATA_ROOT/$SERVICE/ALIAS"
verify_service_name "$1"
if [[ -f "$ALIAS_FILE" ]]; then
cat "$ALIAS_FILE"
else
echo "$PLUGIN_DEFAULT_ALIAS"
fi
local SERVICE_NAME=$(get_service_name "$SERVICE")
echo "$SERVICE_NAME" | tr ._ -
}
service_info() {
@@ -77,16 +71,44 @@ service_exposed_ports() {
service_link() {
local APP="$2"
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 LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1) || true
local DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
local SERVICE_ALIAS=$(service_alias "$SERVICE")
local LINKS_FILE="$SERVICE_ROOT/LINKS"
[[ -n $LINK ]] && dokku_log_fail "Already linked as $LINK"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$LINKS_FILE"
echo "$APP" >> "$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
dokku_log_info1 "Restarting app $APP"
dokku ps:restart "$APP"
local ALIAS="$PLUGIN_DEFAULT_ALIAS"
if [[ -n $DEFAULT_ALIAS ]]; then
ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
fi
dokku docker-options:add "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS"
dokku config:set "$APP" "${ALIAS}_URL=$SERVICE_URL"
}
service_alternative_alias() {
local EXISTING_CONFIG="$1"
local COLORS=(AQUA BLACK BLUE FUCHSIA GRAY GREEN LIME MAROON NAVY OLIVE PURPLE RED SILVER TEAL WHITE YELLOW)
local ALIAS;
while [[ -z $ALIAS ]]; do
local IDX=$((RANDOM % ${#COLORS[*]}))
local COLOR=${COLORS[IDX]}
ALIAS="${PLUGIN_ALT_ALIAS}_${COLOR}"
local IN_USE=$(echo "$EXISTING_CONFIG" | grep "${ALIAS}_URL")
if [[ -n $IN_USE ]]; then
unset ALIAS
fi
done
echo "$ALIAS"
}
service_logs() {
@@ -232,25 +254,25 @@ service_stop() {
service_unlink() {
local APP="$2"
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
local SERVICE_URL=$(service_url "$SERVICE")
local SERVICE_NAME=$(get_service_name "$SERVICE")
local EXISTING_CONFIG=$(dokku config "$APP")
local SERVICE_ALIAS=$(service_alias "$SERVICE")
local LINK=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
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"
[[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP"
remove_from_links_file "$SERVICE" "$APP"
dokku_log_info1 "Restarting app $APP"
dokku ps:restart "$APP"
dokku docker-options:remove "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS"
dokku config:unset "$APP" "${LINK[*]}"
}
service_url() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
local IP="$(get_container_ip "$ID")"
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
echo "$PLUGIN_SCHEME://$SERVICE:$PASSWORD@$IP:${PLUGIN_DATASTORE_PORTS[0]}/$SERVICE"
local SERVICE_ALIAS="$(service_alias "$SERVICE")"
echo "$PLUGIN_SCHEME://$SERVICE:$PASSWORD@$SERVICE_ALIAS:${PLUGIN_DATASTORE_PORTS[0]}/$SERVICE"
}
is_container_status () {
@@ -275,3 +297,46 @@ service_version() {
local SERVICE_NAME="$(get_service_name "$SERVICE")"
docker inspect -f '{{.Config.Image}}' "$SERVICE_NAME"
}
get_url_from_config() {
local EXISTING_CONFIG="$1"
local CONFIG_VAR="$2"
echo "$EXISTING_CONFIG" | grep "$CONFIG_VAR" | sed "s/$CONFIG_VAR:\s*//"
}
promote() {
local SERVICE="$1"
local APP="$2"
local PLUGIN_DEFAULT_CONFIG_VAR="${PLUGIN_DEFAULT_ALIAS}_URL"
local EXISTING_CONFIG=$(dokku config "$APP")
local SERVICE_URL=$(service_url "$SERVICE")
local CONFIG_VARS=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
local PREVIOUS_DEFAULT_URL=$(get_url_from_config "$EXISTING_CONFIG" "$PLUGIN_DEFAULT_CONFIG_VAR")
[[ -z ${CONFIG_VARS[*]} ]] && dokku_log_fail "Not linked to app $APP"
[[ ${CONFIG_VARS[*]} =~ $PLUGIN_DEFAULT_CONFIG_VAR ]] && dokku_log_fail "Service $1 already promoted as $PLUGIN_DEFAULT_CONFIG_VAR"
local NEW_CONFIG_VARS=""
if [[ -n $PREVIOUS_DEFAULT_URL ]]; then
local PREVIOUS_ALIAS=$(echo "$EXISTING_CONFIG" | grep "$PREVIOUS_DEFAULT_URL" | grep -v "$PLUGIN_DEFAULT_CONFIG_VAR") || true
if [[ -z $PREVIOUS_ALIAS ]]; then
local ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
NEW_CONFIG_VARS+="${ALIAS}_URL=$PREVIOUS_DEFAULT_URL "
fi
fi
local PROMOTE_URL=$(get_url_from_config "$EXISTING_CONFIG" "${CONFIG_VARS[0]}")
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"
}

16
pre-delete Executable file
View File

@@ -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

2
tests/bin/sudo Executable file
View File

@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exit 0

20
tests/hook_pre_delete.bats Executable file
View File

@@ -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") ]]
}

View File

@@ -1,32 +0,0 @@
#!/usr/bin/env bats
load test_helper
setup() {
dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2
}
teardown() {
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2
}
@test "($PLUGIN_COMMAND_PREFIX:alias) error when there are no arguments" {
run dokku "$PLUGIN_COMMAND_PREFIX:alias"
assert_contains "${lines[*]}" "Please specify a name for the service"
}
@test "($PLUGIN_COMMAND_PREFIX:alias) error when alias is missing" {
run dokku "$PLUGIN_COMMAND_PREFIX:alias" l
assert_contains "${lines[*]}" "Please specify an alias for the service"
}
@test "($PLUGIN_COMMAND_PREFIX:alias) error when service does not exist" {
run dokku "$PLUGIN_COMMAND_PREFIX:alias" not_existing_service MY_ALIAS
assert_contains "${lines[*]}" "service not_existing_service does not exist"
}
@test "($PLUGIN_COMMAND_PREFIX:alias) success" {
run dokku "$PLUGIN_COMMAND_PREFIX:alias" l MY_ALIAS
new_alias=$(cat "$PLUGIN_DATA_ROOT/l/ALIAS")
[[ $new_alias == "MY_ALIAS" ]]
}

View File

@@ -22,5 +22,13 @@ teardown() {
@test "($PLUGIN_COMMAND_PREFIX:info) success" {
run dokku "$PLUGIN_COMMAND_PREFIX:info" l
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "${lines[*]}" "DSN: mongodb://l:$password@172.17.0.34:27017/l"
assert_contains "${lines[*]}" "DSN: mongodb://l:$password@dokku-mongo-l:27017/l"
}
@test "($PLUGIN_COMMAND_PREFIX:info) replaces underscores by dash in hostname" {
dokku "$PLUGIN_COMMAND_PREFIX:create" test_with_underscores
run dokku "$PLUGIN_COMMAND_PREFIX:info" test_with_underscores
password="$(cat "$PLUGIN_DATA_ROOT/test_with_underscores/PASSWORD")"
assert_contains "${lines[*]}" "DSN: mongodb://test_with_underscores:$password@dokku-mongo-test-with-underscores:27017/test_with_underscores"
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" test_with_underscores
}

View File

@@ -31,8 +31,31 @@ teardown() {
assert_contains "${lines[*]}" "service not_existing_service does not exist"
}
@test "($PLUGIN_COMMAND_PREFIX:link) success" {
@test "($PLUGIN_COMMAND_PREFIX:link) error when the service is already linked to app" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
run dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
links=$(cat "$PLUGIN_DATA_ROOT/l/LINKS")
assert_equal "$links" "my_app"
assert_contains "${lines[*]}" "Already linked as MONGO_URL"
}
@test "($PLUGIN_COMMAND_PREFIX:link) exports MONGO_URL to app" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
url=$(dokku config:get my_app MONGO_URL)
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "$url" "mongodb://l:$password@dokku-mongo-l:27017/l"
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
}
@test "($PLUGIN_COMMAND_PREFIX:link) generates an alternate config url when MONGO_URL already in use" {
dokku config:set my_app MONGO_URL=mongodb://user:pass@host:27017/db
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
run dokku config my_app
assert_contains "${lines[*]}" "DOKKU_MONGO_"
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
}
@test "($PLUGIN_COMMAND_PREFIX:link) links to app with docker-options" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
run dokku docker-options my_app
assert_contains "${lines[*]}" "--link dokku.mongo.l:dokku-mongo-l"
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
}

55
tests/service_promote.bats Executable file
View File

@@ -0,0 +1,55 @@
#!/usr/bin/env bats
load test_helper
setup() {
dokku "$PLUGIN_COMMAND_PREFIX:create" l >&2
dokku apps:create my_app >&2
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
}
teardown() {
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l >&2
rm "$DOKKU_ROOT/my_app" -rf
}
@test "($PLUGIN_COMMAND_PREFIX:promote) error when there are no arguments" {
run dokku "$PLUGIN_COMMAND_PREFIX:promote"
assert_contains "${lines[*]}" "Please specify a name for the service"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) error when the app argument is missing" {
run dokku "$PLUGIN_COMMAND_PREFIX:promote" l
assert_contains "${lines[*]}" "Please specify an app to run the command on"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) error when the app does not exist" {
run dokku "$PLUGIN_COMMAND_PREFIX:promote" l not_existing_app
assert_contains "${lines[*]}" "App not_existing_app does not exist"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) error when the service does not exist" {
run dokku "$PLUGIN_COMMAND_PREFIX:promote" not_existing_service my_app
assert_contains "${lines[*]}" "service not_existing_service does not exist"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) error when the service is already promoted" {
run dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
assert_contains "${lines[*]}" "already promoted as MONGO_URL"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) changes MONGO_URL" {
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
dokku config:set my_app "MONGO_URL=mongodb://u:p@host:27017/db" "DOKKU_MONGO_BLUE_URL=mongodb://l:$password@dokku-mongo-l:27017/l"
dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
url=$(dokku config:get my_app MONGO_URL)
assert_equal "$url" "mongodb://l:$password@dokku-mongo-l:27017/l"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) creates new config url when needed" {
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
dokku config:set my_app "MONGO_URL=mongodb://u:p@host:27017/db" "DOKKU_MONGO_BLUE_URL=mongodb://l:$password@dokku-mongo-l:27017/l"
dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
run dokku config my_app
assert_contains "${lines[*]}" "DOKKU_MONGO_"
}

View File

@@ -31,9 +31,21 @@ teardown() {
assert_contains "${lines[*]}" "service not_existing_service does not exist"
}
@test "($PLUGIN_COMMAND_PREFIX:unlink) success" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app >&2
@test "($PLUGIN_COMMAND_PREFIX:unlink) error when service not linked to app" {
run dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
links=$(cat "$PLUGIN_DATA_ROOT/l/LINKS")
assert_equal "$links" ""
assert_contains "${lines[*]}" "Not linked to app my_app"
}
@test "($PLUGIN_COMMAND_PREFIX:unlink) removes link from docker-options" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app >&2
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
options=$(dokku docker-options my_app)
assert_equal "$options" ""
}
@test "($PLUGIN_COMMAND_PREFIX:unlink) unsets config url from app" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app >&2
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
config=$(dokku config:get my_app MONGO_URL)
assert_equal "$config" ""
}

View File

@@ -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

View File

@@ -10,6 +10,7 @@ export PLUGIN_AVAILABLE_PATH="$PLUGIN_PATH"
export PLUGIN_CORE_AVAILABLE_PATH="$PLUGIN_PATH"
export MONGO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/fixtures"
export PLUGIN_DATA_ROOT="$MONGO_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:?}"/*