-
Notifications
You must be signed in to change notification settings - Fork 14
/
commands
executable file
·233 lines (182 loc) · 4.74 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
#!/bin/bash
_path=".o_volume"
version="1.0"
APP=$2
OLDHOME=$HOME
HOME="$DOKKU_ROOT/$_path"
uuid() {
cat /proc/sys/kernel/random/uuid
}
check_exists() {
if [[ ! -f "$HOME/volume_$APP" ]]; then
echo "No database configured with name : $APP"
exit 1
fi
}
check_app() {
if [[ -z "$APP" ]]; then
echo "You must specify an app name"
exit 1
fi
}
migrate() {
:
}
env_for() {
_env=""
if [[ -f "$HOME/volumes_$APP" ]]; then
IFS=$'\n'
file_content=$(cat "$HOME/volumes_$APP")
for LINE in $file_content; do
_env="$_env -v $LINE"
done
fi
echo "$_env"
}
path_for() {
_app=$1
_path_to_search=$2
_path=""
if [[ -f "$HOME/volumes_$APP" ]]; then
IFS=$'\n'
file_content=$(cat "$HOME/volumes_$APP")
for LINE in $file_content; do
real_path=$(echo "$LINE" | cut -d : -f 1)
virtual_path=$(echo "$LINE" | cut -d : -f 2)
if [[ "$_path_to_search" = "$virtual_path" ]]; then
_path=$real_path
break
fi
done
fi
if [[ -n "$_path" ]]; then
echo "$_path"
fi
}
restart_app() {
_app=$1
if [[ -n "$_app" ]]&&[[ -d "$DOKKU_ROOT/$_app" ]]; then
echo "-----> Restarting application : $_app"
dokku release $_app
dokku deploy $_app
fi
}
case "$1" in
volume:add)
check_app
_container_path=$3
_uuid=$(uuid)
mkdir -m 777 -p "$HOME/data/$_uuid"
echo $HOME/data/$_uuid:$_container_path >> "$HOME/volumes_$APP"
;;
volume:remove)
check_app
_container_path=$3
_real_path=$(path_for $APP $_container_path)
if [[ -f "$HOME/volumes_$APP" ]]; then
if [[ -f "$HOME/_volumes_$APP" ]]; then
rm -f "$HOME/_volumes_$APP"
fi
IFS=$'\n'
file_content=$(cat "$HOME/volumes_$APP")
for LINE in $file_content; do
real_path=$(echo "$LINE" | cut -d : -f 1)
virtual_path=$(echo "$LINE" | cut -d : -f 2)
if [[ "$_container_path" != "$virtual_path" ]]; then
echo "$LINE" >> "$HOME/_volumes_$APP"
fi
done
rm -f "$HOME/volumes_$APP"
if [[ -f "$HOME/_volumes_$APP" ]]; then
mv "$HOME/_volumes_$APP" "$HOME/volumes_$APP"
fi
rm -rf "$_real_path"
fi
;;
volume:list)
check_app
if [[ -f "$HOME/volumes_$APP" ]]; then
cat "$HOME/volumes_$APP"
fi
;;
volume:pre_build)
:
;;
volume:migrate)
migrate
;;
volume:env)
env_for $APP
;;
volume:path_for)
path_for $APP $3
;;
volume:dump)
check_app
_container_path=$3
_uuid=$(uuid)
_filename="$HOME/dump_$_uuid"
_real_path=$(path_for $APP $_container_path)
if [[ -d $_real_path ]]; then
cd $_real_path
tar -zcf $_filename . >/dev/null 2>&1
cat $_filename
fi
rm -f $_filename
;;
volume:restore)
check_app
_uuid=$(uuid)
_filename="$HOME/restore_$_uuid"
cat - > $_filename
_container_path=$3
_real_path=$(path_for $APP $_container_path)
_mimetype=$(file -b --mime-type $_filename)
_temp_folder=$(mktemp -d)
if [[ "$_mimetype" = "application/x-tar" ]]||[[ "$_mimetype" = "application/gzip" ]]; then
tar -tf $_filename >/dev/null
if [[ $? -eq 0 ]]; then
tar -xf $_filename --directory=$_temp_folder
else
tar -zxf $_filename --directory=$_temp_folder
fi
else
echo "Restore with file of type $_mimetype unsupported"
fi
if [[ -d $_real_path ]]&&[[ -d $_temp_folder ]]; then
rm -rf $_real_path
mv $_temp_folder $_real_path
else
rm -rf $_temp_folder
fi
chmod -R 777 $_real_path
;;
volume:install)
if [[ -d "$HOME" ]]; then
migrate
else
mkdir -p "$HOME/data"
echo "$version" > "$HOME/version"
chown -R dokku: "$HOME"
fi
;;
volume:update)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
dokku volume:migrate
;;
help)
cat && cat<<EOF
volume:env <app> Get generated environment variables for <app>
volume:add <app> <container_path> Mount a folder in <app> at <container_path>
volume:remove <app> <container_path> Remove folder in <app> at <container_path>
volume:dump <app> <container_path> > filename.tar.gz Dump volume data to filename.tar.gz
volume:restore <app> <container_path> < filename.tar.gz Restore volume data from filename.tar.gz
volume:list <app> Give list of volumes for <app>
volume:update Update this plugin
volume:migrate Migrate
EOF
;;
*)
exit $DOKKU_NOT_IMPLEMENTED_EXIT
;;
esac