-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.sh
executable file
·228 lines (195 loc) · 5.48 KB
/
poll.sh
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
#!/bin/bash
# ensure running bash
if ! [ -n "$BASH_VERSION" ];then
echo "this is not bash, calling self with bash....";
SCRIPT=$(readlink -f "$0")
/bin/bash $SCRIPT
exit;
fi
# cd to script dir to have correct pwd set
cd "$(dirname "$0")";
# color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# uncomment to debug
# set -x
# restarts containers
rebuild() {
repoType=$1
stopdock $repoType
rundock $repoType
}
# checks if container is running, returns true if not
is_down() {
container_name="${REPOS[$1]}"
if [ "$(docker ps -q -f name=$container_name)" ]; then
return 1
else
return 0
fi
}
# here docker runs for all containers are kept
rundock() {
NAME="${REPOS[$1]}"
printf "Starting $1 container...\n"
if [ $1 = 'front' ]; then
notify 'Starting FRONT container'
export $(cat $(pwd)/env_front | xargs)
cmd="docker run --rm -d \
--env-file $(pwd)/env_front \
--mount type=bind,source=/var/log/fpm-error.log,target=/var/log/error.log \
--mount type=bind,source=/var/log/fpm-access.log,target=/var/log/access.log \
--mount type=bind,source="$(pwd)"/www.conf,target=/usr/local/etc/php-fpm.d/www.conf \
-p 9000:9000 \
--name $NAME $REGISTRY/$NAME"
postCmd="docker cp $NAME:/var/www /var/www/front"
cacheClear="docker exec $NAME php artisan optimize"
runcmd "$cmd"
runcmd "$postCmd"
sleep 5
runcmd "$cacheClear"
elif [ $1 = 'back' ]; then
notify 'Starting BACK container'
export $(cat $(pwd)/env_back | xargs)
cmd="docker run --rm -d \
--env-file $(pwd)/env_back \
--mount type=bind,source=/var/log/backend-app.log,target=/var/log/backend-app.log \
--add-host=postgres:172.17.0.1 \
-p 8080:8080 \
-p 8443:8443 \
--name $NAME $REGISTRY/$NAME"
runcmd "$cmd"
elif [ $1 = 'admin' ]; then
notify 'Starting ADMIN container'
export $(cat $(pwd)/env_front | xargs)
cmd="docker run --rm -d \
--env-file $(pwd)/env_front \
--mount type=bind,source=/var/log/fpm-admin-error.log,target=/var/log/error.log \
--mount type=bind,source=/var/log/fpm-admin-access.log,target=/var/log/access.log \
--mount type=bind,source="$(pwd)"/www.conf,target=/usr/local/etc/php-fpm.d/www.conf \
--mount type=bind,source="$(pwd)"/php.ini,target=/usr/local/etc/php/php.ini \
-p 9900:9000 \
--name $NAME $REGISTRY/$NAME"
postCmd="docker cp $NAME:/var/www /var/www/admin"
cacheClear="docker exec $NAME php artisan optimize"
runcmd "$cmd"
runcmd "$postCmd"
sleep 5
runcmd "$cacheClear"
else
err 'Unexpected type'
fi
}
runcmd() {
cmd=$1
printf "RUN: $GREEN $cmd $NC\n"
${cmd}
printf "\n"
}
stopdock() {
NAME="${REPOS[$1]}"
printf "Stopping old containers...\n"
docker stop $NAME
docker rm $NAME
}
notify() {
/root/telegram.sh/telegram "$1" 2> /dev/null
}
# compares local and registry images
update_exists() {
REPO="${REPOS[$1]}"
# LATEST="`wget -qO- http://$REGISTRY/v2/$REPO/tags/list`"
# LATEST=`echo $LATEST | sed "s/{//g" | sed "s/}//g" | sed 's/.*tags"://' | sed "s/\"//g" | cut -d ' ' -f2`
#
# RUNNING=`docker inspect "$REGISTRY/$REPO" | grep Id | sed "s/\"//g" | sed "s/,//g" | tr -s ' ' | cut -d ' ' -f3`
LATEST=$(curl --silent -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"http://$REGISTRY/v2/$REPO/manifests/latest" | jq -r '.config.digest')
RUNNING=$(docker images -q --no-trunc $REGISTRY/$REPO:latest)
if [ -z "$LATEST" ]; then
return 1
fi
if [ "$RUNNING" == "$LATEST" ];then
return 1
else
return 0
fi
}
title() {
sleep 0.5
printf "\n$GREEN ----------------------------------------------------------\n| \
$1...\n ----------------------------------------------------------$NC\n"
}
err() {
printf "$RED\nERROR: $1 $NC\n"
}
######
# Main execution starts from here
######
if [ -z "$REGISTRY" ]; then
err "You need to set REGISTRY env var to point to docker hub"
echo "Execution stopped!"
exit
fi
declare -A REPOS
REPOS['front']='front-suroo-kg'
REPOS['back']='back-suroo-kg'
REPOS['admin']='admin-suroo-kg'
dt=$(date '+%d/%m/%Y %H:%M:%S');
printf "\n===== $dt ====="
title 'Checking if containers are up and starting if not'
if is_down 'front'; then
rundock 'front'
fi
if is_down 'back'; then
rundock 'back'
fi
if is_down 'admin'; then
rundock 'admin'
fi
title 'Checking container updates'
if update_exists 'front' ; then
notify 'FRONT update in progress...'
docker pull $REGISTRY/${REPOS['front']}
title 'Rebuilding front'
rebuild 'front'
else
echo 'Front is latest version'
fi
if update_exists "back" ; then
notify 'BACK update in progress...'
docker pull $REGISTRY/${REPOS['back']}
title 'Rebuilding back'
rebuild 'back'
else
echo 'Back is latest version'
fi
if update_exists "admin" ; then
notify 'ADMIN update in progress...'
docker pull $REGISTRY/${REPOS['admin']}
title 'Rebuilding admin'
rebuild 'admin'
else
echo 'Admin is latest version'
fi
title 'Checking if containers are up again'
if is_down 'front'; then
err 'Front container is down'
notify 'ACHTUNG! FRONT container is down'
else
printf "Front:$GREEN ✓ UP $NC\n";
fi
if is_down 'back'; then
err 'Back container is down'
notify 'ACHTUNG! BACK container is down'
else
printf "Back: $GREEN ✓ UP $NC\n";
fi
if is_down 'admin'; then
err 'Admin container is down'
notify 'ACHTUNG! ADMIN container is down'
else
printf "Admin: $GREEN ✓ UP $NC\n";
fi
title 'Cleanup: Purging old images'
docker image prune -a -f