forked from dokku/dokku-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
executable file
·352 lines (299 loc) · 10.8 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(dirname "$0")/config"
get_random_ports() {
local iterations="${1:-1}"
for (( i=0; i < iterations; i++ )); do
local port=$RANDOM
local quit=0
while [ "$quit" -ne 1 ]; do
netstat -an | grep $port > /dev/null
if [ $? -gt 0 ]; then
quit=1
else
port=$((port + 1))
fi
done
echo $port
done
}
get_container_ip() {
docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$1"
}
verify_service_name() {
local SERVICE="$1"
[[ ! -n "$SERVICE" ]] && dokku_log_fail "(verify_service_name) SERVICE must not be null"
[[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist"
return 0
}
service_alias() {
local SERVICE="$1"
local SERVICE_NAME=$(get_service_name "$SERVICE")
echo "$SERVICE_NAME" | tr ._ -
}
service_info() {
local SERVICE="$1"
local SERVICE_URL=$(service_url "$SERVICE")
echo " DSN: $SERVICE_URL"
}
service_list() {
local SERVICES=$(ls "$PLUGIN_DATA_ROOT" 2> /dev/null)
if [[ -z $SERVICES ]]; then
dokku_log_warn "There are no $PLUGIN_SERVICE services"
else
LIST="NAME,VERSION,STATUS,EXPOSED PORTS,LINKS\n"
for SERVICE in $SERVICES; do
LIST+="$SERVICE,$(service_version "$SERVICE"),$(service_status "$SERVICE"),$(service_exposed_ports "$SERVICE"),$(service_linked_apps "$SERVICE")\n"
done
printf "%b" "$LIST" | column -t -s,
fi
}
service_exposed_ports() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
[[ ! -f $PORT_FILE ]] && echo '-' && return 0
local PORTS=($(cat "$PORT_FILE"))
for (( i=0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++ )); do
echo -n "${PLUGIN_DATASTORE_PORTS[i]}->${PORTS[i]} "
done
}
service_link() {
local APP="$2"
local SERVICE="$1"
local SERVICE_URL=$(service_url "$SERVICE")
local SERVICE_NAME=$(get_service_name "$SERVICE")
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXISTING_CONFIG=$(dokku config "$APP")
local LINK=$(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1) || true
local DEFAULT_ALIAS=$(echo "$EXISTING_CONFIG" | grep "${PLUGIN_DEFAULT_ALIAS}_URL") || true
local SERVICE_ALIAS=$(service_alias "$SERVICE")
local LINKS_FILE="$SERVICE_ROOT/LINKS"
[[ -n $LINK ]] && dokku_log_fail "Already linked as $LINK"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$LINKS_FILE"
echo "$APP" >> "$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
local ALIAS="$PLUGIN_DEFAULT_ALIAS"
if [[ -n $DEFAULT_ALIAS ]]; then
ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
fi
dokku docker-options:add "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS"
dokku config:set "$APP" "${ALIAS}_URL=$SERVICE_URL"
}
service_alternative_alias() {
local EXISTING_CONFIG="$1"
local COLORS=(AQUA BLACK BLUE FUCHSIA GRAY GREEN LIME MAROON NAVY OLIVE PURPLE RED SILVER TEAL WHITE YELLOW)
local ALIAS;
while [[ -z $ALIAS ]]; do
local IDX=$((RANDOM % ${#COLORS[*]}))
local COLOR=${COLORS[IDX]}
ALIAS="${PLUGIN_ALT_ALIAS}_${COLOR}"
local IN_USE=$(echo "$EXISTING_CONFIG" | grep "${ALIAS}_URL")
if [[ -n $IN_USE ]]; then
unset ALIAS
fi
done
echo "$ALIAS"
}
service_logs() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID=$(cat "$SERVICE_ROOT/ID")
if [[ $2 == "-t" ]]; then
DOKKU_LOGS_ARGS="--follow"
else
DOKKU_LOGS_ARGS="--tail 100"
fi
docker logs $DOKKU_LOGS_ARGS "$ID"
}
service_set_alias() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ALIAS_FILE="$SERVICE_ROOT/ALIAS"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$ALIAS_FILE"
echo "$2" > "$ALIAS_FILE"
}
service_status() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local ID="$(cat "$SERVICE_ROOT/ID")"
is_container_status "$ID" "Dead" && echo "dead" && return 0
is_container_status "$ID" "OOMKilled" && echo "oomkilled" && return 0
is_container_status "$ID" "Paused" && echo "paused" && return 0
is_container_status "$ID" "Restarting" && echo "restarting" && return 0
is_container_status "$ID" "Running" && echo "running" && return 0
echo "stopped" && return 0
}
service_port_expose() {
service_start "$1"
service_port_unpause "$1" "true" "${@:2}"
}
service_port_pause() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local EXPOSED_NAME="$(get_service_name "$SERVICE").ambassador"
local PORT_FILE="$SERVICE_ROOT/PORT"
local LOG_FAIL="$2"
if [[ "$LOG_FAIL" == "true" ]]; then
[[ ! -f "$PORT_FILE" ]] && dokku_log_fail "Service not exposed"
else
[[ ! -f "$PORT_FILE" ]] && return 0
fi
docker stop "$EXPOSED_NAME" > /dev/null
docker rm "$EXPOSED_NAME" > /dev/null
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE unexposed"
fi
}
service_port_unexpose() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PORT_FILE="$SERVICE_ROOT/PORT"
service_port_pause "$SERVICE" "true"
rm -rf "$PORT_FILE"
}
service_port_unpause() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME=$(get_service_name "$SERVICE")
local EXPOSED_NAME="${SERVICE_NAME}.ambassador"
local PORT_FILE="$SERVICE_ROOT/PORT"
local LOG_FAIL="$2"
local PORTS=(${@:3})
PORTS=(${PORTS[@]:-$(get_random_ports ${#PLUGIN_DATASTORE_PORTS[@]})})
local ID=$(cat "$SERVICE_ROOT/ID")
[[ "${#PORTS[@]}" != "${#PLUGIN_DATASTORE_PORTS[@]}" ]] && dokku_log_fail "${#PLUGIN_DATASTORE_PORTS[@]} ports to be exposed need to be provided"
if [[ "$LOG_FAIL" == "true" ]]; then
[[ -f "$PORT_FILE" ]] && PORTS=($(cat "$PORT_FILE")) && dokku_log_fail "Service $SERVICE already exposed on port(s) ${PORTS[*]}"
else
[[ ! -f "$PORT_FILE" ]] && return 0
PORTS=($(cat "$PORT_FILE"))
fi
echo "${PORTS[@]}" > "$PORT_FILE"
docker run -d --link "$SERVICE_NAME:mysql" --name "$EXPOSED_NAME" $(docker_ports_options "${PORTS[@]}") --restart always --label dokku=ambassador --label dokku.ambassador=mysql svendowideit/ambassador > /dev/null
if [[ "$LOG_FAIL" == "true" ]]; then
dokku_log_info1 "Service $SERVICE exposed on port(s) ${PORTS[*]}"
fi
}
docker_ports_options() {
local PORTS=("$@")
for (( i=0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++ )); do
echo -n "-p ${PORTS[i]}:${PLUGIN_DATASTORE_PORTS[i]} "
done
}
service_start() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local SERVICE_NAME=$(get_service_name "$SERVICE")
local ID=$(docker ps -f status=running | grep "$SERVICE_NAME" | awk '{print $1}') || true
[[ -n $ID ]] && dokku_log_warn "Service is already started" && return 0
dokku_log_info1_quiet "Starting container"
local PREVIOUS_ID=$(docker ps -f status=exited | grep "$SERVICE_NAME" | awk '{print $1}') || true
if [[ -n $PREVIOUS_ID ]]; then
docker start "$PREVIOUS_ID" > /dev/null
service_port_unpause "$SERVICE"
dokku_log_info2 "Container started"
else
dokku_log_verbose_quiet "No container exists for $SERVICE"
fi
}
service_stop() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE";
local SERVICE_NAME=$(get_service_name "$SERVICE")
local ID=$(docker ps -f status=running | grep "$SERVICE_NAME" | awk '{print $1}') || true
[[ -z $ID ]] && dokku_log_warn "Service is already stopped" && return 0
if [[ -n $ID ]]; then
dokku_log_info1_quiet "Stopping container"
docker stop "$SERVICE_NAME" > /dev/null
service_port_pause "$SERVICE"
dokku_log_info2 "Container stopped"
else
dokku_log_verbose_quiet "No container exists for $SERVICE"
fi
}
service_unlink() {
local APP="$2"
local SERVICE="$1"
local SERVICE_URL=$(service_url "$SERVICE")
local SERVICE_NAME=$(get_service_name "$SERVICE")
local EXISTING_CONFIG=$(dokku config "$APP")
local SERVICE_ALIAS=$(service_alias "$SERVICE")
local LINK=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
[[ -z ${LINK[*]} ]] && dokku_log_fail "Not linked to app $APP"
remove_from_links_file "$SERVICE" "$APP"
dokku docker-options:remove "$APP" build,deploy,run "--link $SERVICE_NAME:$SERVICE_ALIAS"
dokku config:unset "$APP" "${LINK[*]}"
}
service_url() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local PASSWORD="$(cat "$SERVICE_ROOT/PASSWORD")"
local SERVICE_ALIAS="$(service_alias "$SERVICE")"
echo "$PLUGIN_SCHEME://mysql:$PASSWORD@$SERVICE_ALIAS:${PLUGIN_DATASTORE_PORTS[0]}/$SERVICE"
}
is_container_status () {
local CID=$1
local TEMPLATE="{{.State.$2}}"
local CONTAINER_STATUS=$(docker inspect -f "$TEMPLATE" "$CID" || true)
if [[ "$CONTAINER_STATUS" == "true" ]]; then
return 0
else
return 1
fi
}
get_service_name() {
local SERVICE="$1"
echo "dokku.${PLUGIN_COMMAND_PREFIX}.$SERVICE"
}
service_version() {
local SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
docker inspect -f '{{.Config.Image}}' "$SERVICE_NAME"
}
get_url_from_config() {
local EXISTING_CONFIG="$1"
local CONFIG_VAR="$2"
echo "$EXISTING_CONFIG" | grep "$CONFIG_VAR" | sed "s/$CONFIG_VAR:\s*//"
}
promote() {
local SERVICE="$1"
local APP="$2"
local PLUGIN_DEFAULT_CONFIG_VAR="${PLUGIN_DEFAULT_ALIAS}_URL"
local EXISTING_CONFIG=$(dokku config "$APP")
local SERVICE_URL=$(service_url "$SERVICE")
local CONFIG_VARS=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
local PREVIOUS_DEFAULT_URL=$(get_url_from_config "$EXISTING_CONFIG" "$PLUGIN_DEFAULT_CONFIG_VAR")
[[ -z ${CONFIG_VARS[*]} ]] && dokku_log_fail "Not linked to app $APP"
[[ ${CONFIG_VARS[*]} =~ $PLUGIN_DEFAULT_CONFIG_VAR ]] && dokku_log_fail "Service $1 already promoted as $PLUGIN_DEFAULT_CONFIG_VAR"
local NEW_CONFIG_VARS=""
if [[ -n $PREVIOUS_DEFAULT_URL ]]; then
local PREVIOUS_ALIAS=$(echo "$EXISTING_CONFIG" | grep "$PREVIOUS_DEFAULT_URL" | grep -v "$PLUGIN_DEFAULT_CONFIG_VAR") || true
if [[ -z $PREVIOUS_ALIAS ]]; then
local ALIAS=$(service_alternative_alias "$EXISTING_CONFIG")
NEW_CONFIG_VARS+="${ALIAS}_URL=$PREVIOUS_DEFAULT_URL "
fi
fi
local PROMOTE_URL=$(get_url_from_config "$EXISTING_CONFIG" "${CONFIG_VARS[0]}")
NEW_CONFIG_VARS+="$PLUGIN_DEFAULT_CONFIG_VAR=$PROMOTE_URL"
dokku config:set "$APP" $NEW_CONFIG_VARS
}
remove_from_links_file() {
local SERVICE="$1"
local APP="$2"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
touch "$LINKS_FILE"
sed -i "/^$APP\$/d" "$LINKS_FILE"
sort "$LINKS_FILE" -u -o "$LINKS_FILE"
}
service_linked_apps() {
local SERVICE="$1"
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
local LINKS_FILE="$SERVICE_ROOT/LINKS"
[[ -z $(< "$LINKS_FILE") ]] && echo '-' && return 0
tr '\n' ' ' < "$LINKS_FILE"
}