-
Notifications
You must be signed in to change notification settings - Fork 17
/
upgrade
executable file
·264 lines (236 loc) · 6.67 KB
/
upgrade
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
#!/bin/bash
if [ -z "$COOG_CODE_DIR" ] || [ ! -d "$COOG_CODE_DIR" ] || [ -z "$COOG_DATA_DIR" ]
then
{
echo "Please make sure that these two env vars are set:"
echo " COOG_CODE_DIR: your coog-admin install folder"
echo " COOG_DATA_DIR: the folder where to keep your custom config"
} >&2 && exit 1
fi
OPTIND=1
COOG_IMAGE_TAG="-"
COOG_IMAGE_ARCHIVE="-"
COOG_IMAGE_REPO="-"
COOG_DB_BACKUP=n
COOG_DB_UPDATE=n
docker_image() {
[ -z "$1" ] && return 1
docker images --format '{{.Repository}}:{{.Tag}}' --filter "reference=$1"
}
docker_last_image() {
docker images --format '{{.Repository}}:{{.Tag}}' | head -1
}
docker_container() {
[ -z "$1" ] && return 1
docker ps --format '{{.Names}}' --filter "name=$NETWORK_NAME-$1"
}
cache_clear() {
[ ! -z "$COOG_CACHE_DB" ] && "$COOG_CODE_DIR/redis" client -n "$COOG_CACHE_DB" FLUSHDB
}
database_backup() {
local file_name
file_name="coog-$USER-$COOG_DB_NAME-$(date +'%y%m%d-%H%M%S').dmp"
"$COOG_CODE_DIR/postgres" dump -F c > "/tmp/$file_name"
}
database_update() {
"$COOG_CODE_DIR/coog" admin -u ir "$@"
}
print_help() {
echo "This command upgrades Coog installation"
echo
echo "Command options:"
echo " -t <image-tag>"
echo " -a <image-archive>"
echo " -p <image-repository>"
echo " -s <server-workers-number>"
echo " -c <celery-workers-number>"
echo " -b : backup database"
echo " -u : update database"
echo " -h : print this help"
echo
echo "Extra parameters will be forwarded to the database update command"
}
while getopts "t:a:p:s:c:buh" opt
do
case "$opt" in
t) COOG_IMAGE_TAG="$OPTARG"
;;
p) COOG_IMAGE_REPO="$OPTARG"
;;
a) COOG_IMAGE_ARCHIVE="$OPTARG"
;;
b) COOG_DB_BACKUP=y
;;
u) COOG_DB_UPDATE=y
;;
h) print_help && exit 0
;;
esac
done
shift $((OPTIND-1))
[ "$COOG_IMAGE_ARCHIVE" != "-" ] && [ "$COOG_IMAGE_REPO" != "-" ] && \
echo "Archive (-a) and repository (-p) parameters are mutually exclusive" && exit 1
main() {
source "$COOG_CODE_DIR/config"
[ ! -z "$DB_NAME" ] && COOG_DB_NAME="$DB_NAME"
if [ "$COOG_IMAGE_ARCHIVE" != "-" ]
then
echo
echo "loading image from $COOG_IMAGE_ARCHIVE"
docker load -i "$COOG_IMAGE_ARCHIVE" || return $?
COOG_IMAGE_TAG="$(docker_last_image)"
fi
if [ "$COOG_IMAGE_REPO" != "-" ]
then
echo
echo "pulling image from $COOG_IMAGE_REPO"
docker pull "$COOG_IMAGE_REPO" || return $?
COOG_IMAGE_TAG="$COOG_IMAGE_REPO"
fi
if [ "$COOG_IMAGE_TAG" != "-" ]
then
local img
img=$(docker_image "$COOG_IMAGE_TAG")
echo
if [ ! -z "$img" ]
then
echo "adding $COOG_IMAGE_TAG image to config"
( cd "$COOG_DATA_DIR" \
&& echo "COOG_IMAGE=$COOG_IMAGE_TAG" >> config \
&& git add config \
&& config_data_commit -m "update config (upgrade)" )
[ $? -ne 0 ] \
&& echo "can not set new image tag" >&2 \
&& return 1
else
echo "$COOG_IMAGE_TAG image not found"
return 1
fi
fi
local paybox_container
paybox_container=$(docker_container paybox)
echo
if [ ! -z "$paybox_container" ]
then
echo "removing paybox container"
"$COOG_CODE_DIR/paybox" rm -v -f
else
echo "no paybox container running"
fi
local nginx_container
nginx_container=$(docker_container nginx)
echo
if [ ! -z "$nginx_container" ]
then
echo "removing nginx container"
"$COOG_CODE_DIR/nginx" rm -v -f
else
echo "no nginx container running"
fi
local web_container
web_container=$(docker_container web)
echo
if [ ! -z "$web_container" ]
then
echo "removing web container"
"$COOG_CODE_DIR/web" rm -v -f
else
echo "no web container running"
fi
local unoconv_container
unoconv_container=$(docker_container unoconv)
echo
if [ ! -z "$unoconv_container" ]
then
echo "removing unoconv container"
"$COOG_CODE_DIR/unoconv" rm -v -f
else
echo "no unoconv container running"
fi
local server_container
server_container=$(docker_container coog-server)
echo
if [ ! -z "$server_container" ]
then
echo "removing server container"
"$COOG_CODE_DIR/coog" -- server rm -v -f
else
echo "no server container running"
fi
local celery_container
celery_container=$(docker_container coog-celery)
echo
if [ ! -z "$celery_container" ]
then
echo "removing celery container"
"$COOG_CODE_DIR/coog" -- celery rm -v -f
else
echo "no celery container running"
fi
echo
echo "clearing cache" && { cache_clear || return $?; }
if [ "$COOG_DB_BACKUP" = y ]
then
echo "backup database $COOG_DB_NAME" && { database_backup || return $?; }
fi
if [ "$COOG_DB_UPDATE" = y ]
then
echo "updating database $COOG_DB_NAME" && { database_update "$@" || return $?; }
echo "clearing cache" && { cache_clear || return $?; }
fi
if [ ! -z "$celery_container" ]
then
echo
echo "starting celery container"
"$COOG_CODE_DIR/coog" celery
fi
if [ ! -z "$unoconv_container" ]
then
echo
echo "starting unoconv container"
"$COOG_CODE_DIR/unoconv" run
fi
if [ ! -z "$server_container" ]
then
echo
echo "starting server container"
"$COOG_CODE_DIR/coog" server
fi
if [ ! -z "$web_container" ]
then
echo
echo "starting web container"
"$COOG_CODE_DIR/web" server
fi
"$COOG_CODE_DIR/portal" restart
if [ ! -z "$nginx_container" ]
then
echo
echo "starting nginx container"
"$COOG_CODE_DIR/nginx" run
fi
if [ ! -z "$paybox_container" ]
then
echo
echo "starting paybox container"
"$COOG_CODE_DIR/paybox" run
fi
}
echo "This command is about to **upgrade** your Coog installation!"
echo "Please make sure this is what you want to do and check parameters below before confirming"
echo
echo "- Image tag: $COOG_IMAGE_TAG (-t)"
echo "- Image archive: $COOG_IMAGE_ARCHIVE (-a)"
echo "- Image repository: $COOG_IMAGE_REPO (-p)"
echo "- Backup database: $COOG_DB_BACKUP (-b)"
echo "- Update database: $COOG_DB_UPDATE (-u)"
echo
read -p "Confirm upgrading? [y/N] " choice
case "$choice" in
y|Y) main "$@"
;;
n|N) echo "leaving" && exit 1
;;
*) echo "leaving" && exit 1
;;
esac