Skip to content

Commit f270866

Browse files
committed
Add username/database arguments to link/unlink
1 parent 5075639 commit f270866

File tree

5 files changed

+108
-19
lines changed

5 files changed

+108
-19
lines changed

commands

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ case "$1" in
122122
$PLUGIN_COMMAND_PREFIX:expose <name> [port], Expose a $PLUGIN_SERVICE service on custom port if provided (random port otherwise)
123123
$PLUGIN_COMMAND_PREFIX:import <name> < <file>, Import a dump into the $PLUGIN_SERVICE service database
124124
$PLUGIN_COMMAND_PREFIX:info <name>, Print the connection information
125-
$PLUGIN_COMMAND_PREFIX:link <name> <app>, Link the $PLUGIN_SERVICE service to the app
125+
$PLUGIN_COMMAND_PREFIX:link <name> <app> [--user user] [--database database], Link the $PLUGIN_SERVICE service to the app
126126
$PLUGIN_COMMAND_PREFIX:list, List all $PLUGIN_SERVICE services
127127
$PLUGIN_COMMAND_PREFIX:logs <name> [-t], Print the most recent log(s) for this service
128128
$PLUGIN_COMMAND_PREFIX:promote <name> <app>, Promote service <name> as ${PLUGIN_DEFAULT_ALIAS}_URL in <app>
129129
$PLUGIN_COMMAND_PREFIX:restart <name>, Graceful shutdown and restart of the $PLUGIN_SERVICE service container
130130
$PLUGIN_COMMAND_PREFIX:start <name>, Start a previously stopped $PLUGIN_SERVICE service
131131
$PLUGIN_COMMAND_PREFIX:stop <name>, Stop a running $PLUGIN_SERVICE service
132132
$PLUGIN_COMMAND_PREFIX:unexpose <name>, Unexpose a previously exposed $PLUGIN_SERVICE service
133-
$PLUGIN_COMMAND_PREFIX:unlink <name> <app>, Unlink the $PLUGIN_SERVICE service from the app
133+
$PLUGIN_COMMAND_PREFIX:unlink <name> <app> [--user user] [--database database], Unlink the $PLUGIN_SERVICE service from the app
134134
help_content
135135
}
136136

common-functions

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ service_info() {
196196
declare desc="Retrieves information about a given service"
197197
declare SERVICE="$1" INFO_FLAG="$2"
198198
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
199-
local SERVICE_URL=$(service_url "$SERVICE")
199+
local PASSWORD=$(cat "$SERVICE_ROOT/auth/postgres")
200+
local SERVICE_URL=$(service_url "$SERVICE" postgres "$PASSWORD" "$SERVICE")
200201
local PORT_FILE="$SERVICE_ROOT/PORT"
201202
local SERVICE_CONTAINER_ID="$(cat "$SERVICE_ROOT/ID")"
202203
local flag key valid_flags
@@ -235,9 +236,13 @@ service_link() {
235236
declare desc="Links a service to an application"
236237
declare SERVICE="$1" APP="$2"
237238
update_plugin_scheme_for_app "$APP"
238-
local SERVICE_URL=$(service_url "$SERVICE")
239-
local SERVICE_NAME="$(get_service_name "$SERVICE")"
240239
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
240+
check_auth_migration "$SERVICE"
241+
local USER=${3:-postgres}
242+
local DATABASE=${4:-$(get_database_name "$SERVICE")}
243+
local PASSWORD=$(cat "$SERVICE_ROOT/auth/$USER")
244+
local SERVICE_URL=$(service_url "$SERVICE" "$USER" "$PASSWORD" "$DATABASE")
245+
local SERVICE_NAME="$(get_service_name "$SERVICE")"
241246
local EXISTING_CONFIG=$(config_all "$APP")
242247
local LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1) || true
243248
local DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
@@ -447,8 +452,12 @@ service_stop() {
447452
service_unlink() {
448453
declare desc="Unlinks an application from a service"
449454
declare SERVICE="$1" APP="$2"
455+
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
456+
local USER=${3:-postgres}
457+
local PASSWORD=$(cat "$SERVICE_ROOT/auth/$USER")
458+
local DATABASE=${4:-$(get_database_name "$SERVICE")}
450459
update_plugin_scheme_for_app "$APP"
451-
local SERVICE_URL=$(service_url "$SERVICE")
460+
local SERVICE_URL=$(service_url "$SERVICE" "$USER" "$PASSWORD" "$DATABASE")
452461
local SERVICE_NAME="$(get_service_name "$SERVICE")"
453462
local EXISTING_CONFIG=$(config_all "$APP")
454463
local SERVICE_ALIAS=$(service_alias "$SERVICE")
@@ -490,3 +499,37 @@ verify_service_name() {
490499
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist"
491500
return 0
492501
}
502+
503+
verify_user_name() {
504+
declare desc="Verifies that a user exists"
505+
declare SERVICE="$1" USER="$2"
506+
[[ ! -n "$SERVICE" ]] && dokku_log_fail "(verify_user_name) SERVICE must not be null"
507+
[[ ! -n "$USER" ]] && dokku_log_fail "(verify_user_name) SERVICE must not be null"
508+
[[ ! -f "$PLUGIN_DATA_ROOT/$SERVICE/auth/$USER" ]] && dokku_log_fail "$PLUGIN_SERVICE user $USER for service $SERVICE does not exist"
509+
return 0
510+
}
511+
512+
verify_database_name() {
513+
declare desc="Verifies that a database exists"
514+
declare SERVICE="$1"
515+
declare DATABASE="$2"
516+
[[ ! -n "$SERVICE" ]] && dokku_log_fail "(verify_service_name) SERVICE must not be null"
517+
[[ ! -n "$DATABASE" ]] && dokku_log_fail "(verify_service_name) DATABASE must not be null"
518+
[[ ! -f "$PLUGIN_DATA_ROOT/$SERVICE/databases/$DATABASE" ]] && dokku_log_fail "$PLUGIN_SERVICE database $DATABASE for service $SERVICE does not exist"
519+
return 0
520+
}
521+
522+
check_auth_migration() {
523+
declare desc="Check whether root credentials need to be migrated into multi-user format and do so if needed"
524+
declare SERVICE="$1"
525+
[[ ! -n "$SERVICE" ]] && dokku_log_fail "(verify_service_name) SERVICE must not be null"
526+
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
527+
if [ -f "$SERVICE_ROOT/PASSWORD" ]; then
528+
dokku_log_verbose_quiet "Migrating root user to multi-user format"
529+
local AUTH_DIR="$SERVICE_ROOT/auth"
530+
mkdir -p "$AUTH_DIR"
531+
chmod 750 "$AUTH_DIR"
532+
mv "$SERVICE_ROOT/PASSWORD" "$AUTH_DIR/postgres"
533+
touch "$SERVICE_ROOT/databases/$SERVICE"
534+
fi
535+
}

functions

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ service_create() {
2727
chmod 750 "$SERVICE_ROOT/auth"
2828
touch "$LINKS_FILE"
2929
password=$(openssl rand -hex 16)
30-
echo "$password" > "$SERVICE_ROOT/auth/PASSWORD"
31-
chmod 640 "$SERVICE_ROOT/PASSWORD"
30+
echo "$password" > "$SERVICE_ROOT/auth/postgres"
31+
chmod 640 "$SERVICE_ROOT/auth/postgres"
3232

3333
if [[ -n $POSTGRES_CUSTOM_ENV ]]; then
3434
echo "$POSTGRES_CUSTOM_ENV" | tr ';' "\n" > "$SERVICE_ROOT/ENV"
@@ -44,12 +44,14 @@ database_create() {
4444
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
4545
[[ -z "$NAME" ]] && dokku_log_fail "Please specify a name for the database"
4646

47+
check_auth_migration "$SERVICE"
48+
4749
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
4850
local SERVICE_NAME="$(get_service_name "$SERVICE")"
4951

5052
dokku_log_verbose_quiet "Creating user"
5153
password=$(openssl rand -hex 16)
52-
if docker exec "$SERVICE_NAME" su - postgres -c "psql -c \"CREATE USER $NAME WITH PASSWORD '$password';\"" 2> /dev/null; then
54+
if docker exec "$SERVICE_NAME" su - postgres -c "psql -c \"CREATE USER $NAME WITH PASSWORD '$password';\"" > /dev/null 2>&1; then
5355
echo "$password" > "$SERVICE_ROOT/auth/$NAME"
5456
chmod 640 "$SERVICE_ROOT/auth/$NAME"
5557
else
@@ -69,7 +71,7 @@ service_create_container() {
6971
local SERVICE="$1"
7072
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
7173
local SERVICE_NAME="$(get_service_name "$SERVICE")"
72-
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
74+
local PASSWORD="$(cat "$SERVICE_ROOT/auth/postgres")"
7375
local PREVIOUS_ID
7476

7577
ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_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")
@@ -99,7 +101,7 @@ service_export() {
99101
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
100102
local SERVICE_NAME="$(get_service_name "$SERVICE")"
101103
local DATABASE_NAME="$(get_database_name "$SERVICE")"
102-
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
104+
local PASSWORD="$(cat "$SERVICE_ROOT/auth/postgres")"
103105

104106
[[ -n $SSH_TTY ]] && stty -opost
105107
docker exec "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_dump -Fc --no-acl --no-owner -h localhost -U postgres -w "$DATABASE_NAME"
@@ -113,7 +115,7 @@ service_import() {
113115
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
114116
SERVICE_NAME="$(get_service_name "$SERVICE")"
115117
DATABASE_NAME="$(get_database_name "$SERVICE")"
116-
PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
118+
PASSWORD="$(cat "$SERVICE_ROOT/auth/postgres")"
117119

118120
if [[ -t 0 ]]; then
119121
dokku_log_fail "No data provided on stdin."
@@ -135,7 +137,7 @@ service_start() {
135137
dokku_log_info1_quiet "Starting container"
136138
local PREVIOUS_ID=$(docker ps -f status=exited | grep -e "$SERVICE_NAME$" | awk '{print $1}') || true
137139
local IMAGE_EXISTS=$(docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " && true)
138-
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
140+
local PASSWORD="$(cat "$SERVICE_ROOT/auth/postgres")"
139141

140142
if [[ -n $PREVIOUS_ID ]]; then
141143
docker start "$PREVIOUS_ID" > /dev/null
@@ -152,8 +154,9 @@ service_url() {
152154
local SERVICE="$1"
153155
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
154156

155-
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
156-
local DATABASE_NAME="$(get_database_name "$SERVICE")"
157+
local USER="$2" # postgres
158+
local PASSWORD="$3"
159+
local DATABASE_NAME="$4"
157160
local SERVICE_ALIAS="$(service_alias "$SERVICE")"
158-
echo "$PLUGIN_SCHEME://postgres:$PASSWORD@$SERVICE_ALIAS:${PLUGIN_DATASTORE_PORTS[0]}/$DATABASE_NAME"
161+
echo "$PLUGIN_SCHEME://$USER:$PASSWORD@$SERVICE_ALIAS:${PLUGIN_DATASTORE_PORTS[0]}/$DATABASE_NAME"
159162
}

subcommands/link

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,35 @@ source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"
77
postgres-link-cmd() {
88
declare desc="link the $PLUGIN_SERVICE service to the app"
99
local cmd="$PLUGIN_COMMAND_PREFIX:link" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
10-
declare SERVICE="$1" APP="$2"
10+
11+
local USER=''
12+
13+
local next_index=1; local skip=false; local args=("$@"); local positional=()
14+
for arg in "$@"; do
15+
$skip && skip=false && local next_index=$(( next_index + 1 )) && continue
16+
case "$arg" in
17+
--user)
18+
USER=${args[$next_index]}; skip=true
19+
;;
20+
--database)
21+
DATABASE=${args[$next_index]}; skip=true
22+
;;
23+
*)
24+
positional+=("$arg")
25+
esac
26+
local next_index=$(( next_index + 1 ))
27+
done
28+
29+
declare SERVICE="${positional[0]}" APP="${positional[1]}"
1130
APP=${APP:="$DOKKU_APP_NAME"}
1231

1332
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
1433
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
1534
verify_app_name "$APP"
1635
verify_service_name "$SERVICE"
17-
service_link "$SERVICE" "$APP"
36+
[[ -z "$USER" ]] || verify_user_name "$SERVICE" "$USER"
37+
[[ -z "$DATABASE" ]] || verify_database_name "$SERVICE" "$DATABASE"
38+
service_link "$SERVICE" "$APP" "$USER" "$DATABASE"
1839
}
1940

2041
postgres-link-cmd "$@"

subcommands/unlink

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,33 @@ postgres-unlink-cmd() {
1010
declare SERVICE="$1" APP="$2"
1111
APP=${APP:="$DOKKU_APP_NAME"}
1212

13+
local USER=''
14+
15+
local next_index=1; local skip=false; local args=("$@"); local positional=()
16+
for arg in "$@"; do
17+
$skip && skip=false && local next_index=$(( next_index + 1 )) && continue
18+
case "$arg" in
19+
--user)
20+
USER=${args[$next_index]}; skip=true
21+
;;
22+
--database)
23+
DATABASE=${args[$next_index]}; skip=true
24+
;;
25+
*)
26+
positional+=("$arg")
27+
esac
28+
local next_index=$(( next_index + 1 ))
29+
done
30+
31+
declare SERVICE="${positional[0]}" APP="${positional[1]}"
32+
1333
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
1434
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
1535
verify_app_name "$APP"
1636
verify_service_name "$SERVICE"
17-
service_unlink "$SERVICE" "$APP"
37+
[[ -z "$DATABASE" ]] || verify_database_name "$SERVICE" "$DATABASE"
38+
[[ -z "$USER" ]] || verify_user_name "$SERVICE" "$USER"
39+
service_unlink "$SERVICE" "$APP" "$USER" "$DATABASE"
1840
}
1941

2042
postgres-unlink-cmd "$@"

0 commit comments

Comments
 (0)