forked from dokku/dokku-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
executable file
·208 lines (175 loc) · 8.93 KB
/
functions
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
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common-functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
if [[ -f "$PLUGIN_AVAILABLE_PATH/docker-options/functions" ]]; then
source "$PLUGIN_AVAILABLE_PATH/docker-options/functions"
fi
# non-generic functions
service_create() {
local SERVICE="$1"
local DATABASE=$SERVICE
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already exists"
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS";
if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " ; then
docker pull "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" || dokku_log_fail "$PLUGIN_SERVICE image $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION pull failed"
fi
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
mkdir -p "$SERVICE_ROOT/data" || dokku_log_fail "Unable to create service data directory"
mkdir -p "$SERVICE_ROOT/config" || dokku_log_fail "Unable to create service config directory"
touch "$LINKS_FILE"
echo -e "[mysqld]\nperformance_schema = 0" > "$SERVICE_ROOT/config/disable_performance_schema.cnf"
rootpassword=$(openssl rand -hex 8)
echo "$rootpassword" > "$SERVICE_ROOT/ROOTPASSWORD"
chmod 640 "$SERVICE_ROOT/ROOTPASSWORD"
if [[ -n $MYSQL_CUSTOM_ENV ]]; then
echo "$MYSQL_CUSTOM_ENV" | tr ';' "\n" > "$SERVICE_ROOT/ENV"
else
echo "" > "$SERVICE_ROOT/ENV"
fi
service_create_container "$SERVICE"
}
service_create_container() {
local SERVICE="$1"
local DATABASE=$SERVICE
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ROOTPASSWORD="$(cat "$SERVICE_ROOT/ROOTPASSWORD")"
local DB_LINKS_FILE="$SERVICE_ROOT/dbs/$DATABASE/LINKS"
mkdir -p "$SERVICE_ROOT/dbs" || dokku_log_fail "Unable to create directory to store database data"
mkdir -p "$SERVICE_ROOT/dbs/$DATABASE" || dokku_log_fail "Unable to create directory to store db specific data"
touch "$DB_LINKS_FILE"
db_user=$(openssl rand -hex 8)
password=$(openssl rand -hex 8)
echo "$db_user" > "$SERVICE_ROOT/dbs/$DATABASE/USER"
echo "$password" > "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD"
chmod 640 "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD"
local PASSWORD="$(cat "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD")"
local MYSQL_USER="$(cat "$SERVICE_ROOT/dbs/$DATABASE/USER")"
ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT/data:/var/lib/mysql" -v "$SERVICE_ROOT/config:/etc/mysql/conf.d" -e "MYSQL_ROOT_PASSWORD=$ROOTPASSWORD" -e "MYSQL_USER=$MYSQL_USER" -e "MYSQL_PASSWORD=$PASSWORD" -e "MYSQL_DATABASE=$DATABASE" --env-file="$SERVICE_ROOT/ENV" -d --restart always --label dokku=service --label dokku.service=mysql "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION")
echo "$ID" > "$SERVICE_ROOT/ID"
dokku_log_verbose_quiet "Waiting for container to be ready"
docker run --rm --link "$SERVICE_NAME:$PLUGIN_COMMAND_PREFIX" dokkupaas/wait:0.2 -p "$PLUGIN_DATASTORE_WAIT_PORT" > /dev/null
dokku_log_info2 "$PLUGIN_SERVICE container created: $SERVICE"
# change database encoding
docker exec $ID bash -c "mysql -u root -p$ROOTPASSWORD -e 'ALTER DATABASE \`$SERVICE\` CHARACTER SET utf8 COLLATE utf8_unicode_ci;'"
# http://dev.mysql.com/doc/refman/5.6/en/mysql-tzinfo-to-sql.html
docker exec $ID bash -c "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p$rootpassword mysql"
service_info "$SERVICE"
}
service_extend() {
local SERVICE="$1"
local DATABASE="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
[[ ! -d "$SERVICE_ROOT/dbs/$DATABASE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already has database $DATABASE configured"
mkdir -p "$SERVICE_ROOT/dbs/$DATABASE" || dokku_log_fail "Unable to create directory to store db specific data"
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
[[ -z "$DATABASE" ]] && dokku_log_fail "Please specify a database name"
[[ -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE doesn't exist"
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS"
if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " ; then
docker pull "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION" || dokku_log_fail "$PLUGIN_SERVICE image $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION pull failed"
fi
db_user=$(openssl rand -hex 8)
password=$(openssl rand -hex 8)
echo "$db_user" > "$SERVICE_ROOT/dbs/$DATABASE/USER"
echo "$password" > "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD"
chmod 640 "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD"
service_extend_db "$SERVICE" "$DATABASE"
}
service_extend_db() {
local SERVICE="$1"
local DATABASE="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ROOTPASSWORD="$(cat "$SERVICE_ROOT/ROOTPASSWORD")"
local PASSWORD="$(cat "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD")"
local MYSQL_USER="$(cat "$SERVICE_ROOT/dbs/$DATABASE/USER")"
# create database and grant permissions
docker exec "$SERVICE_NAME" mysql -u root -p"$ROOTPASSWORD" -e "CREATE DATABASE \`$DATABASE\` CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
docker exec "$SERVICE_NAME" mysql -u root -p"$ROOTPASSWORD" -e "GRANT ALL ON \`$DATABASE\`.* to $MYSQL_USER identified by '$PASSWORD';"
service_info "$SERVICE"
}
service_drop_db() {
local SERVICE="$1"
local DATABASE="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ROOTPASSWORD="$(cat "$SERVICE_ROOT/ROOTPASSWORD")"
local PASSWORD="$(cat "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD")"
local MYSQL_USER="$(cat "$SERVICE_ROOT/dbs/$DATABASE/USER")"
local DB_LINKS_FILE="$SERVICE_ROOT/dbs/$DATABASE/LINKS"
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
[[ -z "$DATABASE" ]] && dokku_log_fail "Please specify a database name"
# create database and grant permissions
docker exec "$SERVICE_NAME" mysql -u root -p"$ROOTPASSWORD" -e "DROP DATABASE \`$DATABASE\`;"
docker exec "$SERVICE_NAME" mysql -u root -p"$ROOTPASSWORD" -e "DELETE FROM mysql.user where User='$MYSQL_USER';"
rm -rf "$SERVICE_ROOT/dbs/$DATABASE"
echo "Database $DATABASE on service $SERVICE destroyed!"
}
service_export() {
local SERVICE="$1"
local DATABASE="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local PASSWORD="$(cat "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD")"
local MYSQL_USER="$(cat "$SERVICE_ROOT/dbs/$DATABASE/USER")"
[[ -n $SSH_TTY ]] && stty -opost
docker exec "$SERVICE_NAME" mysqldump --user="$MYSQL_USER" --password="$PASSWORD" "$SERVICE"
status=$?
[[ -n $SSH_TTY ]] && stty opost
exit $status
}
service_import() {
local SERVICE="$1"
local DATABASE="$2"
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
SERVICE_NAME="$(get_service_name "$SERVICE")"
local PASSWORD="$(cat "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD")"
local MYSQL_USER="$(cat "$SERVICE_ROOT/dbs/$DATABASE/USER")"
if [[ -t 0 ]]; then
dokku_log_fail "No data provided on stdin."
fi
docker exec -i "$SERVICE_NAME" mysql --user="$MYSQL_USER" --password="$PASSWORD" "$SERVICE"
}
service_start() {
local SERVICE="$1"
local QUIET="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local ID=$(docker ps -f status=running | grep -e "$SERVICE_NAME$" | awk '{print $1}') || true
if [[ -n $ID ]]; then
[[ -z $QUIET ]] && dokku_log_warn "Service is already started"
return 0
fi
dokku_log_info1_quiet "Starting container"
local PREVIOUS_ID=$(docker ps -f status=exited | grep -e "$SERVICE_NAME$" | awk '{print $1}') || true
local IMAGE_EXISTS=$(docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " && true)
local ROOTPASSWORD="$(cat "$SERVICE_ROOT/ROOTPASSWORD")"
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
if [[ -n $PREVIOUS_ID ]]; then
docker start "$PREVIOUS_ID" > /dev/null
service_port_unpause "$SERVICE"
dokku_log_info2 "Container started"
elif $IMAGE_EXISTS && [[ -n "$ROOTPASSWORD" ]] && [[ -n "$PASSWORD" ]]; then
service_create_container "$SERVICE"
else
dokku_log_verbose_quiet "Neither container nor valid configuration exists for $SERVICE"
fi
}
service_url() {
local SERVICE="$1"
local DATABASE="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_ALIAS="$(service_alias "$SERVICE")"
local PASSWORD="$(cat "$SERVICE_ROOT/dbs/$DATABASE/PASSWORD")"
local USER="$(cat "$SERVICE_ROOT/dbs/$DATABASE/USER")"
echo "$PLUGIN_SCHEME://$USER:$PASSWORD@$SERVICE_ALIAS:${PLUGIN_DATASTORE_PORTS[0]}/$DATABASE"
}
update_plugin_scheme_for_app() {
local APP="$1"
local MYSQL_DATABASE_SCHEME=$(config_get "$APP" MYSQL_DATABASE_SCHEME)
PLUGIN_SCHEME=${MYSQL_DATABASE_SCHEME:-$PLUGIN_SCHEME}
}