forked from stuartpb/dokku-rethinkdb-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands
executable file
·285 lines (250 loc) · 7.61 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
RETHINKDB_IMAGE=rethinkdb:2.0.3
PENDING_DIR="$DOKKU_ROOT/.rethinkdb/pending-volumes"
# Check if name is specified
if [[ $1 == rethinkdb:* ]]; then
if [[ -z $2 ]]; then
echo "You must specify an app name"
exit 1
else
APP="$2"
if [[ -d "$DOKKU_ROOT/$APP" ]]; then
APP_EXISTS=true
else
APP_EXISTS=false
fi
fi
VOLUME_DIR=""
if [[ -d "$DOKKU_ROOT/$APP/rethinkdb" ]]; then
VOLUME_DIR="$DOKKU_ROOT/$APP/rethinkdb"
elif [[ -d "$PENDING_DIR/$APP" ]]; then
VOLUME_DIR="$PENDING_DIR/$APP"
fi
BIND_FILE="$VOLUME_DIR/PUBLISH"
CONTAINER_NAME="rethinkdb_$APP"
ENV_FILE="$DOKKU_ROOT/$APP/ENV"
fi
config_restart_app() {
local APP="$1";
echo "-----> Releasing $APP ..."
dokku release $APP
echo "-----> Release complete!"
echo "-----> Deploying $APP ..."
dokku deploy $APP
echo "-----> Deploy complete!"
}
docker_get_id() {
local CID=`docker inspect '-f={{.Id}}' "$1" 2> /dev/null || true`
if [[ "$CID" == "<no value>" ]]; then
CID=`docker inspect '-f={{.ID}}' "$1" 2> /dev/null || true`
fi
echo "$CID"
}
docker_inspect_port() {
local NAME PORT DUMP
NAME="$1"
PORT="$2"
DUMP="{{(index (index .NetworkSettings.Ports \"$PORT/tcp\") 0).HostPort}}"
docker inspect "-format=$DUMP" $NAME 2> /dev/null || true
}
bind_port() {
local CONTAINER_PORT="$1"
local HOST_PORT="$2"
if [[ -z HOST_PORT ]]; then
PORT_BINDING="$CONTAINER_PORT"
else
PORT_BINDING="$HOST_PORT:$CONTAINER_PORT"
fi
if [[ -f $BIND_FILE ]]; then
sed -i "/$CONTAINER_PORT\$/ d" $BIND_FILE
fi
echo $PORT_BINDING >>"$BIND_FILE"
}
case "$1" in
rethinkdb:create)
# Check if an existing DB volume exists
if [[ -d $VOLUME_DIR ]]; then
echo "-----> Reusing $CONTAINER_NAME data"
else
echo "-----> Creating new RethinkDB volume for $APP"
if $APP_EXISTS; then
VOLUME_DIR="$DOKKU_ROOT/$APP/rethinkdb"
else
VOLUME_DIR="$PENDING_DIR/$APP"
fi
mkdir -p "$VOLUME_DIR"
chown -R dokku:dokku "$VOLUME_DIR"
# Container will be linked in pre-release hook
fi
if $APP_EXISTS; then
# Container will be launched in pre-deploy hook
dokku rethinkdb:link $APP
config_restart_app $APP
fi
;;
rethinkdb:delete)
# Stop the container
ID=$(docker_get_id "$CONTAINER_NAME")
if [[ ! -z $ID ]]; then
docker kill $ID > /dev/null
docker rm $ID > /dev/null
fi
# Remove persistent volume
if [[ -d "$VOLUME_DIR" ]]; then
rm -rf "$VOLUME_DIR"
fi
if [[ -d "$PENDING_DIR/$APP" ]]; then
rm -rf "$PENDING_DIR/$APP"
fi
if [[ -f $ENV_FILE ]]; then
sed -i '/^export (?:RETHINKDB|RDB)_HOST=$RETHINKDB_LINK_PORT_28015_TCP_ADDR$/ d' $ENV_FILE
sed -i '/^export (?:RETHINKDB|RDB)_PORT=$RETHINKDB_LINK_PORT_28015_TCP_PORT$/ d' $ENV_FILE
fi
echo "-----> RethinkDB container deleted: $CONTAINER_NAME"
if $APP_EXISTS; then
config_restart_app $APP
fi
;;
rethinkdb:link)
if $APP_EXISTS; then
# Add aliases for RETHINKDB variables
if [ -f $ENV_FILE ]; then
sed -i "/^export (?:RETHINKDB|RDB)_(?:HOST|PORT)=/ d" $ENV_FILE
fi
cat >>$ENV_FILE <<"EOF"
export RETHINKDB_HOST=$RETHINKDB_LINK_PORT_28015_TCP_ADDR
export RETHINKDB_PORT=$RETHINKDB_LINK_PORT_28015_TCP_PORT
export RDB_HOST=$RETHINKDB_LINK_PORT_28015_TCP_ADDR
export RDB_PORT=$RETHINKDB_LINK_PORT_28015_TCP_PORT
EOF
else
echo "Cannot link non-existing app"
exit 1
fi
;;
rethinkdb:bind-webui)
if [[ -z $VOLUME_DIR ]]; then
echo "No RethinkDB volume found for $APP"
exit 1
fi
bind_port 8080 "$3"
if $APP_EXISTS; then
dokku deploy $APP
else
dokku rethinkdb:start $app
fi
;;
rethinkdb:bind-cluster)
if [[ -z $VOLUME_DIR ]]; then
echo "No RethinkDB volume found for $APP"
exit 1
fi
bind_port 29015 "$3"
if $APP_EXISTS; then
dokku deploy $APP
else
dokku rethinkdb:start $app
fi
;;
rethinkdb:bind-client-driver)
if [[ -z $VOLUME_DIR ]]; then
echo "No RethinkDB volume found for $APP"
exit 1
fi
bind_port 28015 "$3"
if $APP_EXISTS; then
dokku deploy $APP
else
dokku rethinkdb:start $app
fi
;;
rethinkdb:unbind)
if [[ -z $VOLUME_DIR ]]; then
echo "No RethinkDB volume found for $APP"
exit 1
fi
if [ -f $BIND_FILE ]; then
rm $BIND_FILE
fi
if $APP_EXISTS; then
dokku deploy $APP
else
dokku rethinkdb:start $app
fi
;;
rethinkdb:stop)
ID=$(docker_get_id "$CONTAINER_NAME")
if [[ ! -z "$ID" ]]; then
docker stop $ID > /dev/null
fi
;;
rethinkdb:start)
if [[ -z $VOLUME_DIR ]]; then
echo "No RethinkDB volume found for $APP"
exit 1
fi
dokku rethinkdb:stop $APP
ID=$(docker_get_id "$CONTAINER_NAME")
if [[ ! -z "$ID" ]]; then
docker rm $ID > /dev/null
fi
PORTS=()
DYNAMIC_PORTS=()
if [[ -f "$BIND_FILE" ]]; then
while read line; do
PORTS+=("-p" "$line")
if [[ ! "$line" =~ [0-9]+\:[0-9]+$ ]]; then
DYNAMIC_PORTS+=("$line")
fi
done < $BIND_FILE
fi
VOLUME="$VOLUME_DIR:/rethinkdb"
docker run -v $VOLUME --name=$CONTAINER_NAME \
${PORTS[@]} -d $RETHINKDB_IMAGE
# Save all dynamic port bindings as static bindings
for port in "${DYNAMIC_PORTS[@]}"; do
HOST_PORT=$(docker_inspect_port $CONTAINER_NAME $port)
sed -i "s/^$port\$/$HOST_PORT:$port/" $BIND_FILE
done
dokku rethinkdb:info $APP
;;
rethinkdb:info)
ID=$(docker_get_id "$CONTAINER_NAME")
if [[ -z "$ID" ]]; then
echo "No container found for $APP"
exit 1
else
IP=$(docker inspect -f '{{.NetworkSettings.IPAddress}}' $ID)
echo " Container ID: ${ID:0:12}"
echo " Container IP: ${IP}"
WEBUI_PORT=$(docker_inspect_port $ID 8080)
if [[ ! -z "$WEBUI_PORT" ]]; then
echo " WebUI public port: $WEBUI_PORT"
fi
CLUSTER_PORT=$(docker_inspect_port $ID 29015)
if [[ ! -z "$CLUSTER_PORT" ]]; then
echo " Cluster public port: $CLUSTER_PORT"
fi
fi
;;
rethinkdb:logs)
ID=$(docker_get_id "$CONTAINER_NAME")
docker logs $ID | tail -n 100
;;
help)
cat && cat<<EOF
rethinkdb:create <app> Create a RethinkDB container
rethinkdb:delete <app> Delete specified RethinkDB container
rethinkdb:bind-client-driver <app> [host port] Bind app's RethinkDB container client driver port to host port
rethinkdb:bind-webui <app> [host port] Bind app's RethinkDB container WebUI to host port
rethinkdb:bind-cluster <app> [host port] Bind app's RethinkDB cluster port to host
rethinkdb:unbind <app> Unbind app RethinkDB container host port bindings
rethinkdb:logs <app> Display last logs from RethinkDB container
rethinkdb:info <app> Display RethinkDB container address and ports
rethinkdb:link <app> Set app config to link to RethinkDB container
rethinkdb:start <app> (Re)start app's RethinkDB container
rethinkdb:stop <app> Stop app's RethinkDB container
EOF
;;
esac