forked from ohardy/dokku-psql
-
Notifications
You must be signed in to change notification settings - Fork 9
/
commands
executable file
·159 lines (141 loc) · 5.23 KB
/
commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/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"
set +e
if [[ $1 == psql:* ]]; then
export_psql_image
export_psql_root
load_container_id
fi
case "$1" in
psql:admin_console)
check_container
has_tty && TTY_OPTS="-t"
docker exec -i $TTY_OPTS "$PSQL_CONTAINER_NAME" env TERM="$TERM" psql -h localhost -U postgres
;;
psql:console)
check_container; check_app; verify_app_name "$APP"; check_exists
DATABASE=$(cat "$PSQL_ROOT/db_$APP")
PASSWORD=$(cat "$PSQL_ROOT/pass_$APP")
has_tty && TTY_OPTS="-t"
docker exec -i $TTY_OPTS "$PSQL_CONTAINER_NAME" env TERM="$TERM" PGPASSWORD="$PASSWORD" psql -h localhost -U "$DATABASE" "$DATABASE"
;;
psql:url)
check_container; check_app; verify_app_name "$APP"; check_exists
db_url "$APP"
;;
psql:create)
check_container; check_app; verify_app_name "$APP"; check_already_exists
PASSWORD=$(openssl rand -hex 32)
DATABASE=$(database_name "$APP")
echo "$DATABASE" > "$PSQL_ROOT/db_$APP"
echo "$PASSWORD" > "$PSQL_ROOT/pass_$APP"
chmod 600 "$PSQL_ROOT/db_$APP" "$PSQL_ROOT/pass_$APP"
dokku_log_info1 "Creating database $APP"
docker exec "$PSQL_CONTAINER_NAME" su - postgres -c "psql --command \"CREATE USER $DATABASE WITH PASSWORD '$PASSWORD';\";\
createdb -E utf8 -O $DATABASE $DATABASE;\
psql --command \"GRANT ALL PRIVILEGES ON DATABASE $DATABASE TO $DATABASE;\"" > /dev/null
set_link_and_env_for "$APP"
;;
psql:delete)
check_container; check_app; check_exists
DATABASE=$(cat "$PSQL_ROOT/db_$APP")
dokku_log_info1 "Deleting database $APP"
docker exec "$PSQL_CONTAINER_NAME" su - postgres -c "dropdb $DATABASE; dropuser $DATABASE" > /dev/null
rm -f "$PSQL_ROOT/db_$APP" "$PSQL_ROOT/pass_$APP"
if [[ -d "$DOKKU_ROOT/$APP" ]]; then
unset_link_and_env_for "$APP"
fi
;;
psql:list)
check_container
docker exec -i "$PSQL_CONTAINER_NAME" env TERM="$TERM" psql -h localhost -U postgres -c '\l'
;;
psql:start)
if [[ -n "$PSQL_ID" ]]; then
echo "Postgresql container already running with ID: $PSQL_ID"
exit 1
fi
OLD_ID=$(docker ps -f status=exited | grep "$PSQL_CONTAINER_NAME" | awk '{print $1}')
if [[ -n $OLD_ID ]]; then
dokku_log_info1 "Restarting previously stopped Postgresql container $OLD_ID"
docker start "$OLD_ID" > /dev/null
exit 0
fi
dokku_log_info1 "Starting Postgresql server"
docker run $(bind_ip_options) -d -v "$PSQL_ROOT/data:/var/lib/postgresql/data" --name "$PSQL_CONTAINER_NAME" "$PSQL_IMAGE" > /dev/null
;;
psql:restart)
dokku psql:stop
dokku psql:start
;;
psql:stop)
check_container
dokku_log_info1 "Stopping Postgresql server"
docker stop "$PSQL_CONTAINER_NAME" > /dev/null
;;
psql:dump)
check_container; check_app; verify_app_name "$APP"; check_exists
DATABASE=$(cat "$PSQL_ROOT/db_$APP")
PASSWORD=$(cat "$PSQL_ROOT/pass_$APP")
docker exec "$PSQL_CONTAINER_NAME" env PGPASSWORD="$PASSWORD" pg_dump -Fc --no-acl --no-owner -h localhost -U "$DATABASE" -w "$DATABASE"
;;
psql:restore)
check_container; check_app; verify_app_name "$APP"; check_exists
DATABASE=$(cat "$PSQL_ROOT/db_$APP")
PASSWORD=$(cat "$PSQL_ROOT/pass_$APP")
if [[ -t 0 ]]; then
echo "No data provided on stdin." && exit 1
fi
docker exec -i "$PSQL_CONTAINER_NAME" env PGPASSWORD="$PASSWORD" pg_restore -h localhost -cO -d "$DATABASE" -U "$DATABASE" -w
;;
psql:status)
[[ -n "$PSQL_ID" ]] && echo "Postgresql container running with ID: $PSQL_ID" && exit 0
echo "Postgresql container not running"
;;
psql:link)
check_container; check_app; verify_app_name "$APP"; check_exists
TARGET_APP="$3"
[[ -z $TARGET_APP ]] && echo "Target app is missing" && exit 1
verify_app_name "$TARGET_APP"
set_link_and_env_for "$APP" "$TARGET_APP"
;;
psql:unlink)
check_app; verify_app_name "$APP"
unset_link_and_env_for "$APP"
;;
psql:version)
echo -n "v"
cat "$(dirname "$0")/VERSION"
;;
help)
HELP=$(cat<<EOF
psql:console <app>, Launch a postgresql console for <app>
psql:url <app>, Get DATABASE_URL for <app>
psql:create <app>, Create a Postgresql database for <app>
psql:delete <app>, Delete Postgresql database for <app>
psql:link <source> <target>, Link <source> app DB to <target> app
psql:unlink <app>, Remove DB config for <app>
psql:dump <app> > <filename.dump>, Dump <app> database to PG dump format
psql:restore <app> < <filename.*>, Restore database to <app> from any non-plain-text format exported by pg_dump
psql:admin_console, Launch a postgresql console as admin user
psql:restart, Restart the Postgresql docker container
psql:start, Start the Postgresql docker container if it isn't running
psql:stop, Stop the Postgresql docker container
psql:status, Shows status of Postgresql
psql:list, List all databases
psql:version, Output version of plugin
EOF
)
if [[ -n $DOKKU_API_VERSION ]]; then
echo "$HELP"
else
cat && echo "$HELP"
fi
;;
esac