Skip to content

Commit 5075639

Browse files
committed
Add initial create-database command
1 parent 3038d57 commit 5075639

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

commands

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ case "$1" in
4343
"$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/subcommands/create" "$@"
4444
;;
4545

46+
$PLUGIN_COMMAND_PREFIX:create-database)
47+
"$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/subcommands/create-database" "$@"
48+
;;
49+
4650
$PLUGIN_COMMAND_PREFIX:destroy)
4751
"$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/subcommands/destroy" "$@"
4852
;;
@@ -112,6 +116,7 @@ case "$1" in
112116
$PLUGIN_COMMAND_PREFIX:clone <name> <new-name>, Create container <new-name> then copy data from <name> into <new-name>
113117
$PLUGIN_COMMAND_PREFIX:connect <name>, Connect via psql to a $PLUGIN_SERVICE service
114118
$PLUGIN_COMMAND_PREFIX:create <name>, Create a $PLUGIN_SERVICE service
119+
$PLUGIN_COMMAND_PREFIX:create-database <name> <db>, Create a $PLUGIN_SERVICE database in the specified service
115120
$PLUGIN_COMMAND_PREFIX:destroy <name>, Delete the $PLUGIN_SERVICE service and stop its container if there are no links left
116121
$PLUGIN_COMMAND_PREFIX:export <name>, Export a dump of the $PLUGIN_SERVICE service database
117122
$PLUGIN_COMMAND_PREFIX:expose <name> [port], Expose a $PLUGIN_SERVICE service on custom port if provided (random port otherwise)

functions

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ service_create() {
2222
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
2323
mkdir -p "$SERVICE_ROOT/data" || dokku_log_fail "Unable to create service data directory"
2424
mkdir -p "$SERVICE_ROOT/config" || dokku_log_fail "Unable to create service config directory"
25+
mkdir -p "$SERVICE_ROOT/databases" || dokku_log_fail "Unable to create service database directory"
26+
mkdir -p "$SERVICE_ROOT/auth" || dokku_log_fail "Unable to create service auth directory"
27+
chmod 750 "$SERVICE_ROOT/auth"
2528
touch "$LINKS_FILE"
2629
password=$(openssl rand -hex 16)
27-
echo "$password" > "$SERVICE_ROOT/PASSWORD"
30+
echo "$password" > "$SERVICE_ROOT/auth/PASSWORD"
2831
chmod 640 "$SERVICE_ROOT/PASSWORD"
2932

3033
if [[ -n $POSTGRES_CUSTOM_ENV ]]; then
@@ -35,6 +38,33 @@ service_create() {
3538
service_create_container "$SERVICE"
3639
}
3740

41+
database_create() {
42+
local SERVICE="$1"
43+
local NAME="$2"
44+
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
45+
[[ -z "$NAME" ]] && dokku_log_fail "Please specify a name for the database"
46+
47+
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
48+
local SERVICE_NAME="$(get_service_name "$SERVICE")"
49+
50+
dokku_log_verbose_quiet "Creating user"
51+
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
53+
echo "$password" > "$SERVICE_ROOT/auth/$NAME"
54+
chmod 640 "$SERVICE_ROOT/auth/$NAME"
55+
else
56+
echo 'Already exists'
57+
fi
58+
59+
dokku_log_verbose_quiet "Creating database"
60+
if docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $NAME" 2> /dev/null; then
61+
touch "$SERVICE_ROOT/databases/$NAME"
62+
else
63+
echo 'Already exists'
64+
fi
65+
docker exec "$SERVICE_NAME" su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE $NAME TO $NAME;\"" > /dev/null
66+
}
67+
3868
service_create_container() {
3969
local SERVICE="$1"
4070
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"

subcommands/create-database

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config"
3+
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
4+
source "$PLUGIN_BASE_PATH/common/functions"
5+
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"
6+
7+
postgres-create-database-cmd() {
8+
declare desc="create a $PLUGIN_SERVICE database"
9+
local cmd="$PLUGIN_COMMAND_PREFIX:create-database" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
10+
declare SERVICE="$1" NAME="$2"
11+
12+
database_create "$SERVICE" "$NAME"
13+
}
14+
15+
postgres-create-database-cmd "$@"

0 commit comments

Comments
 (0)