forked from coopengo/coog-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade
executable file
·256 lines (229 loc) · 6.07 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
#!/bin/bash
# vim: set ft=sh:
OPTIND=1
COOG_IMAGE_TAG="-"
COOG_IMAGE_ARCHIVE="-"
COOG_IMAGE_REPO="-"
COOG_DB_BACKUP=n
COOG_DB_UPDATE=n
get_dir() {
local script_path; script_path=$(readlink -f "$0")
local script_dir; script_dir=$(dirname "$script_path")
echo "$script_dir"
}
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" ] && "$(get_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"
"$(get_dir)/postgres" dump -F c > "/tmp/$file_name"
}
database_update() {
"$(get_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() {
local dir
dir=$(get_dir)
source "$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"
echo "COOG_IMAGE=$COOG_IMAGE_TAG" >> "$PREFIX/config"
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"
"$dir/paybox" rm -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"
"$dir/nginx" rm -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"
"$dir/web" rm -f
else
echo "no web container running"
fi
local server_container
server_container=$(docker_container coog-server)
echo
if [ ! -z "$server_container" ]
then
echo "removing server container"
"$dir/coog" -- server rm -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"
"$dir/coog" -- celery rm -f
else
echo "no celery container running"
fi
local cron_container
cron_container=$(docker_container coog-cron)
echo
if [ ! -z "$cron_container" ]
then
echo "removing cron container"
"$dir/coog" -- cron rm -f
else
echo "no cron container running"
fi
echo
echo "clearing cache" && { cache_clear || return $?; }
if [ "$COOG_DB_BACKUP" = y ]
then
echo "backup database" && { database_backup || return $?; }
fi
if [ "$COOG_DB_UPDATE" = y ]
then
echo "updating database" && { database_update "$@" || return $?; }
echo "clearing cache" && { cache_clear || return $?; }
fi
if [ ! -z "$celery_container" ]
then
echo
echo "starting celery container"
"$dir/coog" celery
fi
if [ ! -z "$cron_container" ]
then
echo
echo "starting cron container"
"$dir/coog" cron
fi
if [ ! -z "$server_container" ]
then
echo
echo "starting server container"
"$dir/coog" server
fi
if [ ! -z "$web_container" ]
then
echo
echo "starting web container"
"$dir/web" run
fi
if [ ! -z "$nginx_container" ]
then
echo
echo "starting nginx container"
"$dir/nginx" run
fi
if [ ! -z "$paybox_container" ]
then
echo
echo "starting paybox container"
"$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