-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcommands
executable file
·267 lines (249 loc) · 8.84 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
set -e;
# Check if name is specified
if [[ $1 == mariadb:* ]]; then
if [ -z $2 ] && [ $1 != mariadb:list ]; then
echo "You must specify an app name"
exit 1
else
APP="$2"
# Check if app exists with the same name
if [[ -d "$DOKKU_ROOT/$APP" ]]; then
APP_EXISTS=true
else
APP_EXISTS=false
fi
fi
fi
# Create .mariadb directory if does not exist
if [[ ! -d $DOKKU_ROOT/.mariadb ]]; then
mkdir -p $DOKKU_ROOT/.mariadb
chown -R dokku: $DOKKU_ROOT/.mariadb
fi
case "$1" in
mariadb:create)
DB_IMAGE=mariadb/$APP
# Check if DB container is installed
IMAGE=$(docker images | grep "kloadut/mariadb " | awk '{print $3}')
if [[ -z $IMAGE ]]; then
echo "MariaDB image not found... Did you run 'dokku plugins-install' ?"
exit 1
fi
# Check if an existing DB volume exists
if [[ -f "$DOKKU_ROOT/.mariadb/volume_$APP" ]]; then
VOLUME="`cat $DOKKU_ROOT/.mariadb/volume_$APP`:/opt/mysql"
echo
echo "-----> Reusing mariadb/$APP database"
else
VOLUME="/opt/mysql"
# Generate a random password for DB user
DB_PASSWORD=$(< /dev/urandom tr -dc A-Za-z0-9 | head -c 16)
echo $DB_PASSWORD > "$DOKKU_ROOT/.mariadb/pwd_$APP"
chown dokku: "$DOKKU_ROOT/.mariadb/pwd_$APP"
chmod 700 "$DOKKU_ROOT/.mariadb/pwd_$APP"
fi
# Stop existing container with the same persistent DB
ID=$(docker ps | grep "$DB_IMAGE":latest | awk '{print $1}')
if [[ ! -z "$ID" ]]; then
docker stop $ID > /dev/null
fi
# Fork DB image
ID=$(docker run -d kloadut/mariadb /bin/bash "exit 0")
docker wait $ID > /dev/null
IMAGE=$(docker commit $ID)
docker tag -f $IMAGE $DB_IMAGE
# Launch container
ID=$(docker run -v $VOLUME -p 172.17.42.1::3306 -d $DB_IMAGE /usr/bin/start_mariadb.sh $DB_PASSWORD)
sleep 4
# Store persistent volume
if [[ ! -f "$DOKKU_ROOT/.mariadb/volume_$APP" ]]; then
VOLUME_PATH=$(docker inspect $ID | grep /var/lib/docker/vfs/dir/ | awk '{print $2}' | sed -e's/"//g')
if [[ -z $VOLUME_PATH ]]; then
echo "Your docker version is too old, please update it"
exit 1
fi
echo $VOLUME_PATH > "$DOKKU_ROOT/.mariadb/volume_$APP"
fi
# Write port for further usage
PORT=$(docker port $ID 3306 | sed 's/172.17.42.1://')
echo $PORT > "$DOKKU_ROOT/.mariadb/port_$APP"
# Link to a potential existing app
dokku mariadb:link $APP $APP
echo
echo "-----> MariaDB container created: $DB_IMAGE"
sleep 1
dokku mariadb:info $APP
;;
mariadb:delete)
DB_IMAGE=mariadb/$APP
ID=$(docker ps -a | grep "$DB_IMAGE":latest | awk '{print $1}')
# Stop the container
if [[ ! -z $ID ]]; then
docker kill $ID > /dev/null
sleep 1
docker rm -v $ID > /dev/null
sleep 1
fi
# Remove image
IMAGE=$(docker images | grep "$DB_IMAGE " | awk '{print $1}')
if [[ ! -z $IMAGE ]]; then
docker rmi $IMAGE > /dev/null
fi
# Remove container port storage
if [[ -f "$DOKKU_ROOT/.mariadb/port_$APP" ]]; then
rm -f "$DOKKU_ROOT/.mariadb/port_$APP"
fi
# Remove container root password
if [[ -f "$DOKKU_ROOT/.mariadb/pwd_$APP" ]]; then
rm -f "$DOKKU_ROOT/.mariadb/pwd_$APP"
fi
# Remove persistent volume
if [[ -f "$DOKKU_ROOT/.mariadb/volume_$APP" ]]; then
VOLUME="$(cat $DOKKU_ROOT/.mariadb/volume_$APP)"
sleep 4
if [[ -d "$VOLUME" ]]; then
echo "-----> Persistant Storage cannot be deleted !"
echo "---------> Linkfile 'DOKKU_ROOT/.mariadb/volume_$APP' won't be deleted"
exit 1
fi
rm -f "$DOKKU_ROOT/.mariadb/volume_$APP"
fi
echo
echo "-----> MariaDB container deleted: $DB_IMAGE"
;;
mariadb:info)
DB_IMAGE=mariadb/$APP
if [[ ! -f "$DOKKU_ROOT/.mariadb/pwd_$APP" ]]; then
echo "Unknown (or too old) MariaDB container"
exit 1
fi
echo
echo " Host: 172.17.42.1"
echo " Port: $(cat "$DOKKU_ROOT/.mariadb/port_$APP")"
echo " User: 'root'"
echo " Password: '$(cat "$DOKKU_ROOT/.mariadb/pwd_$APP")'"
echo " Database: 'db'"
echo
;;
mariadb:link)
if $APP_EXISTS; then
# Check argument
if [[ -z $3 ]]; then
echo "Linking db to $APP"
DB_IMAGE="mariadb/$APP"
DB_PASSWORD=$(cat "$DOKKU_ROOT/.mariadb/pwd_$APP")
PORT=$(cat "$DOKKU_ROOT/.mariadb/port_$APP")
# Link database using dokku command
dokku config:set $APP "DATABASE_URL=mysql2://root:[email protected]:$PORT/db"
#All DB-variables as ENV
dokku config:set $APP "DB_HOST=172.17.42.1" "DB_PASS=$DB_PASSWORD" "DB_NAME=db" "DB_PORT=$PORT" "DB_USER=root"
exit 1
fi
DB_IMAGE="mariadb/$3"
if [[ ! -f "$DOKKU_ROOT/.mariadb/pwd_$3" ]]; then
echo "Database is not initialized correctly"
exit 1
fi
DB_PASSWORD=$(cat "$DOKKU_ROOT/.mariadb/pwd_$3")
PORT=$(cat "$DOKKU_ROOT/.mariadb/port_$3")
# Link database using dokku command
dokku config:set $APP "DATABASE_URL=mysql2://root:[email protected]:$PORT/db"
#All DB-variables as ENV
dokku config:set $APP "DB_HOST=172.17.42.1" "DB_PASSWORD=$DB_PASSWORD" "DB_NAME=db" "DB_PORT=$PORT" "DB_USER=root"
echo
echo "-----> $APP linked to $DB_IMAGE database"
fi
;;
mariadb:console)
if $APP_EXISTS; then
# Check argument
if [[ -z $2 ]]; then
echo "You must specify a database name"
exit 1
fi
DB_IMAGE="mariadb/$2"
if [[ ! -f "$DOKKU_ROOT/.mariadb/pwd_$2" ]]; then
echo "Database is not initialized correctly"
exit 1
fi
DB_PASSWORD=$(cat "$DOKKU_ROOT/.mariadb/pwd_$2")
PORT=$(cat "$DOKKU_ROOT/.mariadb/port_$2")
# Open database using mysql-client
mysql --host=172.17.42.1 --user=root --port="$PORT" --password="$DB_PASSWORD" db
fi
;;
mariadb:dump)
if $APP_EXISTS; then
# Check argument
if [[ -z $2 ]]; then
echo "You must specify a database name"
exit 1
fi
DB_IMAGE="mariadb/$2"
if [[ ! -f "$DOKKU_ROOT/.mariadb/pwd_$2" ]]; then
echo "Database is not initialized correctly"
exit 1
fi
DB_PASSWORD=$(cat "$DOKKU_ROOT/.mariadb/pwd_$2")
PORT=$(cat "$DOKKU_ROOT/.mariadb/port_$2")
# Check if user gave a filename
if [[ -z $3 ]]; then
echo "Dumping to $2-dump$(date +%s).sql"
mysqldump --host=172.17.42.1 --user=root --port="$PORT" --password="$DB_PASSWORD" db > "$2-dump$(date +%s).sql"
exit 1
fi
mysqldump --host=172.17.42.1 --user=root --port="$PORT" --password="$DB_PASSWORD" db > $3
fi
;;
mariadb:dumpraw)
if $APP_EXISTS; then
# Check argument
if [[ -z $2 ]]; then
echo "You must specify a database name"
exit 1
fi
DB_IMAGE="mariadb/$2"
if [[ ! -f "$DOKKU_ROOT/.mariadb/pwd_$2" ]]; then
echo "Database is not initialized correctly"
exit 1
fi
DB_PASSWORD=$(cat "$DOKKU_ROOT/.mariadb/pwd_$2")
PORT=$(cat "$DOKKU_ROOT/.mariadb/port_$2")
# Check if user gave a filename
if [[ -z $3 ]]; then
mysqldump --lock-tables=false --host=172.17.42.1 --user=root --port="$PORT" --password="$DB_PASSWORD" db
exit 1
fi
mysqldump --lock-tables=false --host=172.17.42.1 --user=root --port="$PORT" --password="$DB_PASSWORD" db > $3
fi
;;
mariadb:list)
CONTAINERS=$(ls $DOKKU_ROOT/.mariadb/volume* 2> /dev/null | sed -e 's/_/ /' | awk '{print $2}')
if [[ -z $CONTAINERS ]]; then
echo "There are no MariaDB containers created."
else
echo "MariaDB containers:"
for CONTAINER in $CONTAINERS; do
echo " - $CONTAINER"
done
fi
;;
mariadb:logs)
DB_IMAGE=mariadb/$APP
ID=$(docker ps -a | grep "$DB_IMAGE" | awk '{print $1}')
docker logs $ID | tail -n 100
;;
help)
cat && cat<<EOF
mariadb:create <app> Create a MariaDB container
mariadb:delete <app> Delete specified MariaDB container
mariadb:info <app> Display database informations
mariadb:link <app> <db> Link an app to a MariaDB database
mariadb:console <app> Open mysql-console to MariaDB container
mariadb:dump <app> <file> Dump default db database into file
mariadb:dumpraw <app> Dump default db database to std out
mariadb:logs <app> Display last logs from MariaDB container
mariadb:list Display list of MariaDB containers
EOF
;;
esac