-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.sh
executable file
·215 lines (181 loc) · 5.98 KB
/
server.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
#!/bin/bash
function usage() {
echo "Usage: $0 {init|join|network|start|scale|stop|clean|stats|services|publish|benchmark}"
echo " init: initialize the swarm cluster"
echo " join TOKEN IP:PORT: join the swarm cluster"
echo " network (REQUIRES SWARM CLUSTER): create shared networks for the swarm cluster"
echo " start [-n <N=1>: N xmpp servers] [-d: dev mode [-p: publish]] (REQUIRES SWARM CLUSTER): start the server"
echo " scale N: scale the server to N instances of xmpp servers"
echo " stop: stop the server"
echo " clean: stop the server and remove all docker data"
echo " stats: print stats from all services"
echo " services: print all services"
echo " publish [-d: dev mode (REQUIRES SWARM CLUSTER)]: publish the images to a registry"
echo " benchmark {start|stop|publish}: start/stop/publish benchmark"
exit 1
}
function init() {
if docker swarm init; then
echo "swarm cluster initialized"
else
echo "failed to initialize swarm cluster"
fi
}
function join() {
if [ -z "$1" ]; then
echo "missing token"
usage
fi
if [ -z "$2" ]; then
echo "missing address"
usage
fi
if docker swarm join --token "$1" "$2"; then
echo "swarm cluster joined"
else
echo "failed to join swarm cluster"
fi
}
function network() {
if docker network create --driver overlay --attachable sre-cs; then
echo "[simulation run environment <-> communication server] created"
else
echo "[simulation run environment <-> communication server] failed to create"
fi
if docker network create --driver overlay --attachable cs-benchmark; then
echo "[communication server <-> benchmark] created"
else
echo "[communication server <-> benchmark] failed to create"
fi
}
function start() {
DEV=0
N=1
PUBLISH=0
while getopts dn:p opt; do
case $opt in
d) DEV=1 ;;
n) N=$OPTARG ;;
p) PUBLISH=1 ;;
*) usage ;;
esac
done
if [ "$DEV" -eq "1" ]; then
COMPOSE_FILE=docker-compose.dev.swarm.yml
if [ "$PUBLISH" -eq "1" ]; then publish -d; fi
else
COMPOSE_FILE=docker-compose.swarm.yml
fi
source .version
if env VERSION="${VERSION}" docker stack deploy -c ./"$COMPOSE_FILE" cs; then
if [ "$N" -gt "1" ]; then scale "$N"; fi
echo "Version: ${VERSION}"
echo "OK"
else
echo ""
echo "failed to start the server"
echo ""
echo "if you see the following error:"
echo "failed to create service X: Error response from daemon: network Y not found"
echo "then restart docker daemon (i.e. sudo systemctl restart docker) and run ./server.sh clean"
echo ""
echo "if you see the following error:"
echo "network X is declared as external, but could not be found"
echo "run the script with the network option to create the required networks"
fi
}
function scale() {
if [ -z "${1}" ]; then
echo "missing number of instances"
usage
fi
docker service scale cs_xmpp-server="${1}"
}
function stop() {
docker stack rm cs
}
function clean() {
stop
docker swarm leave --force
docker system prune --all --volumes
}
function stats() {
docker stats
}
function services() {
docker service ls
echo ""
echo "if you notice that some of the services are not up"
echo "then stop the server, publish the images, create the shared networks, and start the server again"
}
function publish() {
local OPTIND
DEV=0
while getopts d opt; do
case $opt in
d) DEV=1 ;;
*) usage ;;
esac
done
if [ "$DEV" -eq "1" ]; then
echo "creating local registry"
if ! docker service ps -q registry > /dev/null 2>&1; then
if ! docker service create --name registry --publish published=5000,target=5000 registry:2; then
echo "failed to create registry"
usage
fi
else
echo "registry already exists"
fi
docker-compose -f docker-compose.dev.swarm.yml build --parallel && \
docker-compose -f docker-compose.dev.swarm.yml push
else
source .version
read -p "Publish version ${VERSION} to registry? [y/n] " -r VERSION_ANSWER
read -p "Publish version latest to registry? [y/n] " -r LATEST_ANSWER
if [ "$VERSION_ANSWER" != "y" ] && [ "$LATEST_ANSWER" != "y" ]; then
echo "aborting"
exit 1
fi
if [ "$VERSION_ANSWER" == "y" ]; then
env VERSION="${VERSION}" docker-compose -f docker-compose.swarm.yml build --parallel && \
env VERSION="${VERSION}" docker-compose -f docker-compose.swarm.yml push && \
echo "published version ${VERSION}"
fi
if [ "$LATEST_ANSWER" == "y" ]; then
env VERSION=latest docker-compose -f docker-compose.swarm.yml build --parallel && \
env VERSION=latest docker-compose -f docker-compose.swarm.yml push && \
echo "published version latest"
fi
fi
}
function benchmark() {
case "${1}" in
start) docker run \
-d \
-p 8091:8091 \
--rm \
--network cs-benchmark \
--name tsung-benchmark \
aasm/cs-tsung-benchmark && \
echo "benchmark is running on http://localhost:8091"
;;
stop) docker stop tsung-benchmark ;;
publish) ./tsung-benchmark/publish.sh ;;
*) usage ;;
esac
}
case "${1}" in
init) init ;;
join) join "${2}" "${3}" ;;
network) network ;;
start) start "${@:2}" ;;
scale) scale "${2}" ;;
stop) stop ;;
clean) clean ;;
stats) stats ;;
services) services ;;
publish) publish "${@:2}" ;;
benchmark) benchmark "${2}" ;;
*) usage ;;
esac