Revamp link/unlink commands

Previously we were exporting `DATABASE_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 `DATABASE_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 `DATABASE_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_POSTGRES_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 ad0da307d5
commit 63047297ea
12 changed files with 217 additions and 114 deletions

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,5 @@ 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: postgres://postgres:$password@172.17.0.34:5432/l"
assert_contains "${lines[*]}" "DSN: postgres://postgres:$password@dokku-postgres-l:5432/l"
}

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 DATABASE_URL"
}
@test "($PLUGIN_COMMAND_PREFIX:link) exports DATABASE_URL to app" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
url=$(dokku config:get my_app DATABASE_URL)
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
assert_contains "$url" "postgres://postgres:$password@dokku-postgres-l:5432/l"
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
}
@test "($PLUGIN_COMMAND_PREFIX:link) generates an alternate config url when DATABASE_URL already in use" {
dokku config:set my_app DATABASE_URL=postgres://user:pass@host:5432/db
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
run dokku config my_app
assert_contains "${lines[*]}" "DOKKU_POSTGRES_"
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.postgres.l:dokku-postgres-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 DATABASE_URL"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) changes DATABASE_URL" {
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
dokku config:set my_app "DATABASE_URL=postgres://u:p@host:5432/db" "DOKKU_POSTGRES_BLUE_URL=postgres://postgres:$password@dokku-postgres-l:5432/l"
dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
url=$(dokku config:get my_app DATABASE_URL)
assert_equal "$url" "postgres://postgres:$password@dokku-postgres-l:5432/l"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) creates new config url when needed" {
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
dokku config:set my_app "DATABASE_URL=postgres://u:p@host:5432/db" "DOKKU_POSTGRES_BLUE_URL=postgres://postgres:$password@dokku-postgres-l:5432/l"
dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
run dokku config my_app
assert_contains "${lines[*]}" "DOKKU_POSTGRES_"
}

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 DATABASE_URL)
assert_equal "$config" ""
}