forked from diadata-org/diadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.sh
executable file
·294 lines (251 loc) · 5.22 KB
/
push.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
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
286
287
288
289
290
291
292
293
294
#!/bin/bash
if [ "$DOCKER_HUB_LOGIN" == "" ]
then
echo missing env DOCKER_HUB_LOGIN variable
exit
fi
REMOTE="n"
NOCACHE="--no-cache"
LINENUMBER=""
DEPLOY="n"
SLAVES="n"
WATCH="n"
DIRECTORY=`pwd`
MACHINE=""
STACKNAME=""
FILETMP=/tmp/$0.$$.tmp
FILETMP2=/tmp/services.txt.$$
selectLocalHost() {
echo DOCKER_HOST:$DOCKER_HOST
unset DOCKER_HOST
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
echo switching to LocalHost
env | grep DOCKER
}
checkMachine() {
if [ ! -d ~/secrets/live/$1/ ]; then
echo "no configuration for machine $1 in your ~/secrets directory"
exit
fi
}
selectMachine() {
export DOCKER_HOST="tcp://$1:2376"
export DOCKER_ID_USER="reg"
export DOCKER_TLS_VERIFY="1"
export DOCKER_CERT_PATH=~/secrets/docker-certs/$1/
H=`echo $1 | cut -d"." -f2-3`
#export DOCKER_HUB_LOGIN=registry.$H
if [ -d "$DOCKER_CERT_PATH" ]; then
rm ~/secrets/live/$1/ca.pem
rm ~/secrets/live/$1/key.pem
ln -s ~/secrets/live/$1/fullchain.pem ~/secrets/live/$1/ca.pem 2>&1 > /dev/null
ln -s ~/secrets/live/$1/privkey.pem ~/secrets/live/$1/key.pem 2>&1 > /dev/null
else
echo cant find machine $1
exit
fi
}
selectMaster() {
if [ "$REMOTE" != "n" ]
then
selectMachine $MACHINE
echo switching to $DOCKER_HOST
fi
}
selectSlave() {
if [ "$REMOTE" != "n" ]
then
export DOCKER_HOST="tcp://TOFIX:2376"
export DOCKER_ID_USER="TOFIX"
export DOCKER_TLS_VERIFY="TOFIX"
export DOCKER_CERT_PATH="TOFIX"
echo switching to $DOCKER_HOST
fi
}
watch_service() {
docker service logs $1 -f
}
rebuild_service() {
echo building $1 NOCACHE: $NOCACHE
GITHUB_DIRECTORY=`pwd | sed 's/\.com.*/.com/'`
echo cd $GITHUB_DIRECTORY
cd $GITHUB_DIRECTORY
selectLocalHost
docker-compose -f $DIRECTORY/$COMPOSE_FILE build $NOCACHE $1
if [ $? -eq 0 ]
then
IMAGE=${DOCKER_HUB_LOGIN}/${STACKNAME}_$1
echo "$0 pushing $IMAGE"
docker push $IMAGE
if [ $? -ne 0 ]
then
echo error pushing image, press any key to continue
read
fi
if [ "$REMOTE" != "n" ]
then
if [ "$SLAVES" != "n" ]
then
selectSlave
echo $DOCKER_HOST
docker pull $IMAGE
fi
selectMaster
echo "$0 pulling image"
docker pull $IMAGE
if [ $? -ne 0 ]
then
echo error pulling image
exit 0
fi
fi
SERVICE=${STACKNAME}_$1
echo service update on $DOCKER_HOST for $SERVICE
RESTARTED=1
while [ $RESTARTED -ne 0 ]
do
docker service update --with-registry-auth --force ${SERVICE} --image ${IMAGE}
RESTARTED=$?
if [ $RESTARTED -ne 0 ]
then
echo "$0 failed on update service ${SERVICE} trying again in 10 secs"
echo "run this to get logs:"
echo "docker service logs -f ${SERVICE}"
sleep 10
fi
done
if [ "$WATCH" == "y" ]
then
watch_service $SERVICE
fi
else
exit
fi
cd -
}
clean() {
docker volume rm $(docker volume ls -qf dangling=true) 2> /dev/null
docker system prune -f
}
showHelp() {
echo "-s <stack>" >&2
ls deployments/docker-compose.*
echo "-c to use cache" >&2
echo "-d to deploy stack" >&2
echo "-r <machine> for remote" >&2
echo "-n <number> to build service <number> from stack" >&2
echo "-a to build all services" >&2
}
main() {
while getopts "n:hr:dcaws:" opt; do
case $opt in
s)
export STACKNAME=$OPTARG
export COMPOSE_FILE=`find . | grep docker-compose | grep "$STACKNAME\.yml"`
if [ ! -f "$COMPOSE_FILE" ]
then
echo cant find stack $STACKNAME
exit
fi
;;
w)
WATCH="y"
;;
c)
echo using cache
NOCACHE=""
;;
d)
DEPLOY="y"
;;
a)
LINENUMBER="a"
;;
n)
echo "-n was triggered, Parameter: $OPTARG" >&2
LINENUMBER=$OPTARG
;;
h)
showHelp
exit
;;
r)
REMOTE="y"
MACHINE=$OPTARG
if [ "$MACHINE" == "" ]
then
echo you must select a machine
exit
fi
checkMachine $MACHINE
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ "$STACKNAME" == "" ]
then
echo missing -s option to choose stack
showHelp
exit
fi
if [ "$DEPLOY" = "y" ]
then
selectMaster
if [ $STACKNAME == "dia" ]
then
docker stack deploy -c docker-compose.kafka.yml kafka
fi
echo deploying on stack $DIRECTORY/$COMPOSE_FILE $STACKNAME
docker stack deploy -c $DIRECTORY/$COMPOSE_FILE $STACKNAME
exit
fi
NUMBER=0
rm -f $FILETMP 2>/dev/null
docker-compose -f $DIRECTORY/$COMPOSE_FILE config --services | sort -u > $FILETMP2
if [ $? -ne 0 ]
then
echo error in docker compose file
exit
fi
cat $FILETMP2 | grep -v visualizer | while read line; do echo "$NUMBER-$line" >> $FILETMP;NUMBER=`expr $NUMBER + 1`; done
rm -f $FILETMP2
cat $FILETMP
echo "a-for all"
if [ "$LINENUMBER" == "" ]
then
read LINENUMBER
fi
if [ "$LINENUMBER" == "a" ]
then
NUMBER=`wc -l $FILETMP | cut -f1 -d\ `
for (( c=0; c<$NUMBER; c++ ))
do
RESULT=`cat $FILETMP | grep $c | cut -f2- -d\- `
rebuild_service $RESULT
done
else
RESULT=`cat $FILETMP | grep ${LINENUMBER}- | cut -f2- -d\- `
rebuild_service $RESULT
fi
clean
if [ "$SLAVES" != "n" ]
then
selectSlave
clean
fi
selectLocalHost
clean
rm -f $FILETMP $FILETMP2
echo "docker stack services $STACKNAME "
echo "run this to get logs:"
echo "docker service logs -f ${SERVICE}"
}
main $*
echo bye bye