Skip to content
Open
152 changes: 64 additions & 88 deletions common-functions

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -eo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common-functions"
source "$PLUGIN_BASE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
if [[ -f "$PLUGIN_AVAILABLE_PATH/docker-options/functions" ]]; then
source "$PLUGIN_AVAILABLE_PATH/docker-options/functions"
fi
Expand All @@ -26,7 +27,6 @@ service_create() {
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a valid name for the service"
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already exists"
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
LINKS_FILE="$SERVICE_ROOT/LINKS"

service_parse_args "${@:2}"

Expand All @@ -42,15 +42,13 @@ service_create() {

mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
mkdir -p "$SERVICE_ROOT/data" || dokku_log_fail "Unable to create service data directory"
touch "$LINKS_FILE"

PASSWORD=$(openssl rand -hex 16)
if [[ -n "$SERVICE_PASSWORD" ]]; then
PASSWORD="$SERVICE_PASSWORD"
dokku_log_warn "Specified password may not be as secure as the auto-generated password"
fi
echo "$PASSWORD" >"$SERVICE_ROOT/PASSWORD"
chmod 640 "$SERVICE_ROOT/PASSWORD"
fn-plugin-property-write "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "PASSWORD" "$PASSWORD"

[[ -n "$SERVICE_CUSTOM_ENV" ]] && POSTGRES_CUSTOM_ENV="$SERVICE_CUSTOM_ENV"
if [[ -n $POSTGRES_CUSTOM_ENV ]]; then
Expand All @@ -73,7 +71,8 @@ service_create_container() {
local PREVIOUS_ID

ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_HOST_ROOT/data:/var/lib/postgresql/data" -e "POSTGRES_PASSWORD=$PASSWORD" --env-file="$SERVICE_ROOT/ENV" -d --restart always --label dokku=service --label dokku.service=postgres "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION")
echo "$ID" >"$SERVICE_ROOT/ID"
fn-plugin-property-write "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "ID" "$ID"


dokku_log_verbose_quiet "Waiting for container to be ready"
docker run --rm --link "$SERVICE_NAME:$PLUGIN_COMMAND_PREFIX" "$PLUGIN_WAIT_IMAGE" -p "$PLUGIN_DATASTORE_WAIT_PORT" >/dev/null
Expand Down Expand Up @@ -129,9 +128,9 @@ service_start() {
local ID=$(docker ps -aq --no-trunc --filter "status=running" --filter "name=^/$SERVICE_NAME$" --format '{{ .ID }}') || true
if [[ -n $ID ]]; then
[[ -z $QUIET ]] && dokku_log_warn "Service is already started"
if [[ ! -f "$SERVICE_ROOT/ID" ]] || [[ "$(cat "$SERVICE_ROOT/ID")" != "$ID" ]]; then
if fn-plugin-property-exists "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "ID" || [[ "$(fn-plugin-property-get "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "ID")" != "$ID" ]]; then
[[ -z $QUIET ]] && dokku_log_warn "Updating local container ID"
echo "$ID" >"$SERVICE_ROOT/ID"
fn-plugin-property-write "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "ID" "$ID"
fi
return 0
fi
Expand Down
29 changes: 29 additions & 0 deletions install
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"

set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

Expand All @@ -25,9 +27,36 @@ plugin-install() {
mkdir -p "$PLUGIN_DATA_ROOT" || echo "Failed to create $PLUGIN_SERVICE data directory"
chown dokku:dokku "$PLUGIN_DATA_ROOT"

fn-plugin-property-setup "$PLUGIN_COMMAND_PREFIX"
mkdir -p "$PLUGIN_CONFIG_ROOT" || echo "Failed to create $PLUGIN_SERVICE config directory"
chown dokku:dokku "$PLUGIN_CONFIG_ROOT"

pushd "$PLUGIN_DATA_ROOT"
for SERVICE in *; do
if [ -d "$SERVICE" ]; then
mkdir -p "$PLUGIN_CONFIG_ROOT"/"$SERVICE/"
pushd "$SERVICE"
for PROP in *; do
if [ ! -d "$PROP" ] && [[ "$PROP" != "ENV" ]]; then
mv "$PROP" "$PLUGIN_CONFIG_ROOT"/"$SERVICE/"
fi
done
popd
if [ -d "$SERVICE"/backup ]; then
pushd "$SERVICE"/backup
for BACKUPPROP in *; do
mv "$BACKUPPROP" "BACKUP_$BACKUPPROP"
mv "BACKUP_$BACKUPPROP" "$PLUGIN_CONFIG_ROOT/$SERVICE/"
done
popd
fi
chown dokku:dokku "$PLUGIN_CONFIG_ROOT"/"$SERVICE"/
chown -R dokku:dokku "$PLUGIN_CONFIG_ROOT"/"$SERVICE"/*
chmod 600 "$PLUGIN_CONFIG_ROOT"/"$SERVICE"/*
fi
done
popd

rm -f "/etc/sudoers.d/dokku-${PLUGIN_COMMAND_PREFIX}*"
_SUDOERS_FILE="/etc/sudoers.d/dokku-${PLUGIN_COMMAND_PREFIX}"

Expand Down
5 changes: 3 additions & 2 deletions subcommands/clone
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_BASE_PATH/common/functions"
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"


service-clone-cmd() {
#E you can clone an existing service to a new one
Expand All @@ -25,8 +27,7 @@ service-clone-cmd() {
[[ -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")"
local ID=$(fn-plugin-property-get "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "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")
Expand Down
5 changes: 3 additions & 2 deletions subcommands/destroy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_BASE_PATH/common/functions"
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"

service-destroy-cmd() {
#E destroy the service, it's data, and the running container
Expand All @@ -18,11 +19,10 @@ service-destroy-cmd() {
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a valid name for the service"
verify_service_name "$SERVICE"
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
LINKS_FILE="$SERVICE_ROOT/LINKS"
SERVICE_HOST_ROOT="$PLUGIN_DATA_HOST_ROOT/$SERVICE"
SERVICE_NAME="$(get_service_name "$SERVICE")"

[[ -s "$LINKS_FILE" ]] && dokku_log_fail "Cannot delete linked service"
[[ $(fn-plugin-property-get "$PLUGIN_COMMAND_PREFIX" "$SERVICE" "LINKS" "") != "" ]] && dokku_log_fail "Cannot delete linked service"

if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then
DOKKU_APPS_FORCE_DELETE=1
Expand All @@ -47,6 +47,7 @@ service-destroy-cmd() {
dokku_log_verbose_quiet "Removing data"
docker run --rm -v "$SERVICE_HOST_ROOT/data:/data" -v "$SERVICE_HOST_ROOT/$PLUGIN_CONFIG_SUFFIX:/config" "$PLUGIN_BUSYBOX_IMAGE" chmod 777 -R /config /data
rm -rf "$SERVICE_ROOT"
fn-plugin-property-destroy "$PLUGIN_COMMAND_PREFIX" "$SERVICE"

dokku_log_info2 "$PLUGIN_SERVICE container deleted: $SERVICE"
}
Expand Down
4 changes: 2 additions & 2 deletions tests/hook_pre_delete.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ teardown() {
}

@test "($PLUGIN_COMMAND_PREFIX:hook:pre-delete) removes app from links file when destroying app" {
[[ -n $(< "$PLUGIN_DATA_ROOT/l/LINKS") ]]
sudo [ -n $(< "$PLUGIN_CONFIG_ROOT/l/LINKS") ]
dokku --force apps:destroy my_app
[[ -z $(< "$PLUGIN_DATA_ROOT/l/LINKS") ]]
sudo [ -z $(< "$PLUGIN_CONFIG_ROOT/l/LINKS") ]
}
2 changes: 1 addition & 1 deletion tests/service_clone.bats
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ teardown() {

@test "($PLUGIN_COMMAND_PREFIX:clone) success" {
run dokku "$PLUGIN_COMMAND_PREFIX:clone" l new_service
[[ -f $PLUGIN_DATA_ROOT/new_service/ID ]]
[[ -f $PLUGIN_CONFIG_ROOT/new_service/ID ]]
assert_contains "${lines[*]}" "Copying data from l to new_service"
assert_contains "${lines[*]}" "Done"
assert_success
Expand Down
6 changes: 3 additions & 3 deletions tests/service_info.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ teardown() {

@test "($PLUGIN_COMMAND_PREFIX:info) success" {
run dokku "$PLUGIN_COMMAND_PREFIX:info" l
local password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
local password="$(sudo cat "$PLUGIN_CONFIG_ROOT/l/PASSWORD")"
assert_contains "${lines[*]}" "postgres://postgres:$password@dokku-postgres-l:5432/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
local password="$(sudo cat "$PLUGIN_DATA_ROOT/test_with_underscores/PASSWORD")"
local password="$(sudo cat "$PLUGIN_CONFIG_ROOT/test_with_underscores/PASSWORD")"
assert_contains "${lines[*]}" "postgres://postgres:$password@dokku-postgres-test-with-underscores:5432/test_with_underscores"
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" test_with_underscores
}

@test "($PLUGIN_COMMAND_PREFIX:info) success with flag" {
run dokku "$PLUGIN_COMMAND_PREFIX:info" l --dsn
local password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
local password="$(sudo cat "$PLUGIN_CONFIG_ROOT/l/PASSWORD")"
assert_output "postgres://postgres:$password@dokku-postgres-l:5432/l"

run dokku "$PLUGIN_COMMAND_PREFIX:info" l --config-dir
Expand Down
6 changes: 3 additions & 3 deletions tests/service_link.bats
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ teardown() {
echo "output: $output"
echo "status: $status"
url=$(dokku config:get my_app DATABASE_URL)
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
password="$(sudo cat "$PLUGIN_CONFIG_ROOT/l/PASSWORD")"
assert_contains "$url" "postgres://postgres:$password@dokku-postgres-l:5432/l"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
Expand Down Expand Up @@ -95,7 +95,7 @@ teardown() {
dokku config:set my_app POSTGRES_DATABASE_SCHEME=postgres2
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
url=$(dokku config:get my_app DATABASE_URL)
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
password="$(sudo cat "$PLUGIN_CONFIG_ROOT/l/PASSWORD")"
assert_contains "$url" "postgres2://postgres:$password@dokku-postgres-l:5432/l"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
Expand All @@ -112,7 +112,7 @@ teardown() {
@test "($PLUGIN_COMMAND_PREFIX:link) uses a specified config url when alias is specified" {
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app --alias "ALIAS"
url=$(dokku config:get my_app ALIAS_URL)
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
password="$(sudo cat "$PLUGIN_CONFIG_ROOT/l/PASSWORD")"
assert_contains "$url" "postgres://postgres:$password@dokku-postgres-l:5432/l"
assert_success
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
Expand Down
6 changes: 3 additions & 3 deletions tests/service_promote.bats
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ teardown() {
}

@test "($PLUGIN_COMMAND_PREFIX:promote) changes DATABASE_URL" {
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
password="$(sudo cat "$PLUGIN_CONFIG_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="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
password="$(sudo cat "$PLUGIN_CONFIG_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_"
}
@test "($PLUGIN_COMMAND_PREFIX:promote) uses POSTGRES_DATABASE_SCHEME variable" {
password="$(sudo cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
password="$(sudo cat "$PLUGIN_CONFIG_ROOT/l/PASSWORD")"
dokku config:set my_app "POSTGRES_DATABASE_SCHEME=postgres2" "DATABASE_URL=postgres://u:p@host:5432/db" "DOKKU_POSTGRES_BLUE_URL=postgres2://postgres:$password@dokku-postgres-l:5432/l"
dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
url=$(dokku config:get my_app DATABASE_URL)
Expand Down