forked from jeffutter/dokku-postgresql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands
executable file
·224 lines (182 loc) · 6.83 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
#set -e;
APP=$2
OLDHOME=$HOME
HOME="$DOKKU_ROOT/.postgresql"
PGADMIN=postgres
postgresql_database="${APP}"
db_image=asaushkin/postgresql
check_app() {
if [[ -z "$APP" ]]; then
echo "You must specify an app name"
exit 1
fi
}
check_container() {
if [[ -z "$id" ]]; then
echo "Postgresql container not started cannot continue. Start with dokku postgresql:start"
exit 1
fi
}
postgres_password() {
cat "$DOKKU_ROOT/.postgresql/admin_pw"
}
appuser_password() {
cat "$DOKKU_ROOT/.postgresql/pass_$APP"
}
# $1 - dbname (if none, then postgres with root privs)
# $2 - sql code
# $3 - psql parameters
execute() {
local dbhost=localhost
local dbport=5432
if [ -z "$1" -o x"$1" = x"postgres" ]; then
local dbname=postgres
local dbuser=postgres
local dbpass=$(postgres_password)
else
local dbname="$APP"
local dbuser="$APP"
local dbpass=$(appuser_password)
fi
if [ ! -z "$2" ]; then
PGPASSWORD=$dbpass psql $3 -h $dbhost -p $dbport -U $dbuser -d $dbname <<SQL
$2
SQL
else
PGPASSWORD=$dbpass psql $3 -h $dbhost -p $dbport -U $dbuser -d $dbname
fi
}
url() {
echo "DATABASE_URL=\"postgres://${APP}:$(appuser_password)@${ip}:5432/${APP}\""
}
id=$(docker ps | grep "$db_image" | awk '{print $1}')
ip=$(ifconfig docker0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
case "$1" in
postgresql:psql)
check_container
if [ -z "$APP" ]; then
execute postgres
else
if [[ ! -r "$DOKKU_ROOT/.postgresql/pass_$APP" ]]; then
echo "ERROR: Database $APP does not exist or not properly initialized"
exit 1
fi
execute "$APP"
fi
;;
postgresql:create)
check_container
check_app
# Check already initiaized db
if [ -r "$DOKKU_ROOT/.postgresql/pass_$APP" ]; then
echo ERROR: Database ${postgresql_database} for application ${APP} already exists. Try \"dokku postgresql:delete "$APP"\" first.
exit 1
fi
if [[ $(execute postgres "select count(*) from pg_roles where rolname = '${APP}'" '-At') -eq 0 ]]; then
apg -M NCL -m 16 -x 16 -n 1 > "$DOKKU_ROOT/.postgresql/pass_$APP"
execute postgres "create user \"${APP}\" with password '$(appuser_password)'" '-X'
else
echo WARN: User "$APP" already exists in the PostgreSQL instance, skipping creation.
fi
if [[ $(execute postgres "select count(*) from pg_database where datname = '${APP}'" '-At') -eq 0 ]]; then
execute postgres "create database \"${postgresql_database}\" with owner \"${APP}\"" '-X'
execute postgres "alter database \"${postgresql_database}\" set timezone to 'UTC'" '-X'
else
echo WARN: Database "$APP" already exists in the PostgreSQL instance, skipping creation.
fi
if [[ -d "$DOKKU_ROOT/$APP" ]]; then
dokku config:set "$APP" $(url)
else
url
fi
;;
postgresql:url)
check_container
check_app
url
;;
postgresql:delete)
check_container
check_app
execute postgres "drop database \"${APP}\"" "-X"
execute postgres "drop user \"${APP}\"" "-X"
if [[ -d "$DOKKU_ROOT/$APP" ]]; then
dokku config:unset "$APP" DATABASE_URL
fi
rm "$DOKKU_ROOT/.postgresql/pass_$APP"
;;
postgresql:dblist)
check_container
execute postgres '\list'
;;
postgresql:start)
if [[ "$id" != "" ]]; then
echo "Postgresql container already running with ID: ${id}"
else
docker run -p 5432:5432 -d \
-v "$DOKKU_ROOT/.postgresql/data":"/var/lib/postgresql/9.3/main" \
-v "$DOKKU_ROOT/.postgresql/etc":"/etc/postgresql/9.3/main" \
"$db_image" bash -c "su postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf'"
fi
;;
postgresql:stop)
check_container
docker stop ${id}
;;
postgresql:status)
if [[ "$id" != "" ]]; then
echo "Postgresql container running with ID: ${id}"
else
echo "Postgresql container not running"
fi
;;
postgresql:bash)
check_container
docker exec -it $id bash
;;
postgresql:tsize)
check_container
check_app
execute $APP "
select table_schema, table_name,
pg_size_pretty( pg_relation_size( quote_ident( table_schema ) || '.' || quote_ident( table_name ) ) ) as size,
pg_size_pretty( pg_total_relation_size( quote_ident( table_schema ) || '.' || quote_ident( table_name ) ) ) as total_size
from information_schema.tables
where table_type = 'BASE TABLE' and table_schema not in ('information_schema', 'pg_catalog')
order by pg_relation_size( quote_ident( table_schema ) || '.' || quote_ident( table_name ) ) desc, table_schema, table_name"
;;
postgresql:dbsize)
check_container
execute postgres "SELECT datname, pg_size_pretty(pg_database_size(datname)) db_size FROM pg_database ORDER BY db_size"
;;
postgresql:activity)
check_container
execute postgres "select * from pg_stat_activity"
;;
postgresql:kill)
check_container
if [ ! -z "$2" ]; then
execute postgres "select pg_terminate_backend('$2')"
else
echo "Usage: dokku postgresql:kill <id>"
fi
;;
help)
cat && cat<<EOF
postgresql:psql <app> Launch a postgresql console for a given app or as admin user (without appname)
postgresql:create <app> Create a Postgresql database
postgresql:delete <app> Delete specified Postgresql database
postgresql:start Start the Postgresql docker container if it isn't running
postgresql:stop Stop the Postgresql docker container
postgresql:bash Enter into bash on the PostgreSQL docker container
postgresql:status Shows status of Postgresql
postgresql:dblist List all databases
postgresql:dbsize Size all databases
postgresql:tsize <app> Size all user tables in database <app>
postgresql:activity Show current processes in the postgresql instance
postgresql:kill <id> Terminate a backend process
postgresql:url <app> Show an application DATABASE_URL
EOF
;;
esac