-
Notifications
You must be signed in to change notification settings - Fork 2
/
swarmt.sh
executable file
·263 lines (236 loc) · 6.9 KB
/
swarmt.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
#!/bin/bash
# shellcheck disable=SC1090
# set -x
# ------------------------ Introduction --------------------------------- #
#
# Name : swarmt.sh
#
# Description : Deploy and manage your swarm clusters using docker-machine
#
# Syntaxe : swarmt.sh
#
# Arguments:
# init : create and initialize swarm cluster
# start : start an existing swarm cluster
# list : list existing nodes based of config files
# stop : Halt every swarm nodes
# rm : delete the swarm cluster
# Options:
# -c [configuration file]: optional configuration file
# --------------------------------------------------------------------------- #
#Initialize variables
cfg_file="$(echo ${0##*/} | sed 's/.sh/.conf/g')"
# Interactive documentation
usage() {
echo " "
echo "================================================="
echo " $(basename ${0}) takes arguments described below: "
echo " -h : show this help box "
echo " init : create and initialize swarm cluster "
echo " start : start an existing swarm cluster "
echo " list : list all existing nodes "
echo " stop : Halt every swarm nodes "
echo " rm : delete the swarm cluster "
echo " "
echo " You can pass a specific configuration file:"
echo " ./swarmt.sh -c swarm.conf init"
echo " "
echo " By default the script will be looking "
echo " for a config file named: swarmt.conf "
echo "================================================="
exit 0
}
# Create managers and worker nodes
create_machines(){
for (( nm=1; nm<="${smanager}"; nm++ ))
do
docker-machine create -d "${mdriver}" "${moption}" "${project}m${nm}";
done
for (( nw=1; nw<="${sworker}"; nw++ ))
do
docker-machine create -d "${mdriver}" "${moption}" "${project}w${nw}";
done
} 2> /dev/null
# initialize and bootstrap swarm cluster
swarm_init(){
# initialize Swarm Manager and tokens
docker-machine ssh "${project}m1" "docker swarm init \
--listen-addr $(docker-machine ip ${project}m1) \
--advertise-addr $(docker-machine ip ${project}m1)"
worker_token="$(docker-machine ssh ${project}m1 docker swarm \
join-token worker -q)"
manager_token="$(docker-machine ssh ${project}m1 docker swarm \
join-token manager -q)"
export worker_token manager_token
#make other managers join the cluster
for (( nm=2; nm<=${smanager}; nm++ ))
do
docker-machine ssh ${project}m$nm "docker swarm join \
--token=${manager_token} \
--listen-addr $(docker-machine ip ${project}m$nm) \
--advertise-addr $(docker-machine ip ${project}m$nm) \
$(docker-machine ip ${project}m1)"
done
#make workers join the cluster
for (( nw=1; nw<=${sworker}; nw++ ))
do
docker-machine ssh ${project}w$nw "docker swarm join \
--token=${worker_token} \
--listen-addr $(docker-machine ip ${project}w$nw) \
--advertise-addr $(docker-machine ip ${project}w$nw) \
$(docker-machine ip ${project}m1)"
done
if [ $? -eq 0 ]
then
echo " "
echo "==========================================="
echo " ${project} swarm cluster is up and running "
echo "==========================================="
else
echo " "
echo "something wen't wrong!"
exit 1
fi
} 2> /dev/null
# start an existing swarm cluster
swarm_start(){
swarm_nodes=$(docker-machine ls | grep ${project} | awk '{print$1}')
for i in $swarm_nodes
do echo "${i} swarm node is starting :"; docker-machine start $i;echo " "
done
if [ "${?}" -eq 0 ]
then
echo " "
echo "==================================="
echo " ${project} swarm cluster is ready "
echo "==================================="
fi
} 2> /dev/null
# start a docker swarm stack if exists
start_stack(){
if [ -f "${stackfile}" ]
then
eval "$(docker-machine env ${project}m1)"
docker stack deploy -c "${stackfile}" "${project}"
if [ $? -eq 0 ]
then
echo " "
echo "==========================="
echo " ${project} stack is ready "
echo "==========================="
fi
fi
} 2> /dev/null
# stop all swarm nodes nodes
swarm_halt(){
for (( nm=1; nm<="${smanager}"; nm++ ))
do
docker-machine stop "${project}m${nm}";
done
for (( nw=1; nw<="${sworker}"; nw++ ))
do
docker-machine stop "${project}w${nw}";
done
if [ "${?}" -eq 0 ]
then
echo " "
echo "==================================="
echo " ${project} swarm cluster is halted "
echo "==================================="
fi
} 2> /dev/null
# stop and delete all swarm nodes
swarm_delete(){
swarm_halt
for (( nm=1; nm<="${smanager}"; nm++ ))
do
docker-machine rm "${project}m${nm}" -f;
done
for (( nw=1; nw<="${sworker}"; nw++ ))
do
docker-machine rm "${project}w${nw}" -f;
done
if [ "${?}" -eq 0 ]
then
echo " "
echo "==========================================="
echo " ${project} swarm cluster has been deleted "
echo "==========================================="
fi
} 2> /dev/null
# list all existing swarm nodes in all configuration files
swarm_list(){
project_list=$(grep -h "project" *.conf | awk -F'=' '{print$2}')
for i in ${project_list}
do echo "${i} swarm nodes:"; docker-machine ls | grep "${i}";echo " "
done
} 2> /dev/null
# Load and check configuration settings
main() {
if [ $# -eq 1 ] && [ "${1}" = "-h" ]
then usage;
fi
if [ "${1}" == "-c" ]
then
if [ -s "${2}" ] && [ -r "${2}" ]
then
cfg_file="${2}"
source "${cfg_file}"
else
echo "your configuration file is missing or is empty"
exit 1
fi
if [ "${1}" != "-c" ] && [ -s ${cfg_file} ] && [ -r ${cfg_file} ]
then
echo "${1}"
source "${cfg_file}"
fi
else
source "${cfg_file}"
fi
if [[ "${mdriver}" != "digitalocean" ]]
then
if [ -n "${mimage}" ]
then
moption="--virtualbox-boot2docker-url=${mimage}"
else
moption=''
fi
else
mdriver="digitalocean"
if [ -z "${dotoken}" ]
then
echo "you must specify a Digital Ocean token in you ${cfg_file} file"
exit 1
else
moption="--digitalocean-access-token=${dotoken}"
fi
fi
param=${!#}
case "${param}" in
init)
create_machines
swarm_init
start_stack
;;
start)
swarm_start
;;
stop)
swarm_halt
;;
rm)
swarm_halt
swarm_delete
;;
list)
swarm_list
;;
*)
usage
;;
esac
} 2> /dev/null
# ---- MAIN --------------------------------------------------------------- #
main "$@"
# --------------------------------------------------------------------------- #