forked from rcbops/opencenter-install-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencenter-cluster.sh
executable file
·340 lines (297 loc) · 10.5 KB
/
opencenter-cluster.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env bash
# OpenCenter(TM) is Copyright 2013 by Rackspace US, Inc.
##############################################################################
#
# OpenCenter is licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. This
# version of OpenCenter includes Rackspace trademarks and logos, and in
# accordance with Section 6 of the License, the provision of commercial
# support services in conjunction with a version of OpenCenter which includes
# Rackspace trademarks and logos is prohibited. OpenCenter source code and
# details are available at: # https://github.com/rcbops/opencenter or upon
# written request.
#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 and a copy, including this
# notice, is available in the LICENSE file accompanying this software.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the # specific language governing permissions and limitations
# under the License.
#
##############################################################################
#
#
set -e
set -u
declare -A PIDS
#command to use for nova; read from environment or "nova" by default.
#This is so you can set NOVA="supernova env" before running the script.
OPENCENTER_PASSWORD=${OPENCENTER_PASSWORD:-"opencentre"}
NOVA=${NOVA:-nova}
RERUN=${RERUN:-false}
USE_PACKAGES=false
USE_NETWORK=false
PRIV_NETWORK="192.168.0.0/24"
CLUSTER_PREFIX="c1"
CLIENT_COUNT=2
if [ -L $0 ]; then
BASEDIR=$(dirname $(readlink $0))
else
BASEDIR=$(dirname $0)
fi
SSHOPTS="-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
DASHBOARD_PORT=3000
USAGE="Usage: opencenter-cluster.sh <Cluster-Prefix> <Number of Clients> [--packages] [--network(=<CIDR>)]"
IMAGE_TYPE=${IMAGE_TYPE:-"12.04 LTS"}
if [ "x$1" != "x" ]; then
CLUSTER_PREFIX=$1
fi
if [ $# -ge 2 ]; then
if [ $2 -eq $2 2>/dev/null ]; then
CLIENT_COUNT=$2
else
echo $USAGE
exit 1
fi
fi
if [ $# -ge 3 ]; then
flag=$(echo $3 | cut -d "=" -f1)
if [ "$flag" == "--packages" ]; then
USE_PACKAGES=true
DASHBOARD_PORT=80
if [ $# -ge 4 ]; then
flag=$(echo $4 | cut -d "=" -f1 )
if [ "$flag" == "--network" ]; then
net_range=$(echo $4 | cut -d "=" -f2)
echo $net_range
USE_NETWORK=true
if ( echo $net_range | egrep "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{2}$" > /dev/null 2>&1); then
PRIV_NETWORK=$net_range
elif [ "$net_range" != "--network" ]; then
echo $USAGE
exit 1
fi
echo "Using Private Network: $PRIV_NETWORK"
fi
fi
echo "Using Packages"
elif [ "$flag" == "--network" ]; then
net_range=$(echo $3 | cut -d "=" -f2 )
USE_NETWORK=true
if ( echo $net_range | egrep "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{2}$" > /dev/null 2>&1); then
PRIV_NETWORK=$net_range
elif [ "$net_range" != "--network" ]; then
echo $USAGE
exit 1
fi
if [ $# -ge 4 ] && [ "$4" == "--packages" ]; then
echo "Using Packages"
USE_PACKAGES=true
DASHBOARD_PORT=80
elif [ $# -ge 4 ] && [ "$4" != "--packages" ]; then
echo $USAGE
exit 1
fi
echo "Using Private Network: $PRIV_NETWORK"
else
echo $USAGE
exit 1
fi
fi
function mangle_name() {
server=${1:-}
if [[ ${server} == ${CLUSTER_PREFIX}* ]]; then
echo ${server}
else
echo ${CLUSTER_PREFIX}-${server}
fi
}
function ip_for() {
server=$(mangle_name $1)
ip=$($NOVA show ${server} | sed -En "/public network/ s/^.* ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*$/\1/p")
if [[ ${ip} =~ "." ]]; then
echo ${ip}
else
echo ""
fi
}
function wait_for_ip() {
server=$(mangle_name $1)
count=0
max_count=20
echo "Waiting for IPv4 on ${server}"
while ( ! $NOVA list | grep ${server} | grep -q "ERROR" ); do
ip=$(ip_for ${server});
if [ "${ip}" == "" ]; then
sleep 20
count=$(( count + 1 ))
if [ ${count} -gt ${max_count} ]; then
echo "Aborting... too slow"
exit 1
fi
else
echo "Got IPv4: ${ip} for server: ${server}"
break
fi
done
if ( $NOVA list | grep ${server} | grep -q "ERROR" ); then
echo "${server} in ERROR state, build failed"
exit 1
fi
}
function wait_for_ssh() {
server=$(mangle_name $1)
count=0
max_ping=60 # 10 frigging minutes.
max_count=18 # plus 3 min (*2) for ssh and getty
wait_for_ip ${server}
ip=$(ip_for ${server})
echo "Waiting for ping on ${ip}"
count=0
while ( ! ping -c1 ${ip} > /dev/null 2>&1 ); do
count=$(( count + 1 ))
if [ ${count} -gt ${max_ping} ]; then
echo "timeout waiting for ping"
exit 1
fi
sleep 10
done
echo "Waiting for ssh on ${ip}"
count=0
while ( ! nc -w 1 ${ip} 22 | grep -q "SSH" ); do
count=$(( count + 1 ))
if [ ${count} -gt ${max_count} ]; then
echo "timeout waiting for ssh"
exit 1
fi
sleep 10
done
echo "SSH ready - waiting for valid login"
count=0
while ( ! ssh ${SSHOPTS} root@${ip} id | grep -q "root" ); do
count=$(( count + 1 ))
if [ ${count} -gt ${max_count} ]; then
echo "timeout waiting for login"
exit 1
fi
sleep 10
done
echo "Login successful"
}
function setup_server_as() {
server=$(mangle_name $1)
as=$2
ip=$(ip_for "opencenter-server")
if [[ ! -f ${HOME}/.ssh/id_github ]]; then
echo "Please setup your github key in ${HOME}/.ssh/id_github"
exit 1
fi
scriptName="install-dev"
if ( $USE_PACKAGES ); then
scriptName="install"
fi
scp ${SSHOPTS} ${BASEDIR}/${scriptName}.sh root@$(ip_for ${server}):/tmp
if !( $USE_PACKAGES ); then
echo "Loading github key"
scp ${SSHOPTS} ${HOME}/.ssh/id_github root@$(ip_for ${server}):/root/.ssh/id_rsa
fi
# Upload screen.rc file if exists
if [[ -f ${HOME}/.screenrc ]]; then
echo "Setting up .screenrc file"
scp ${SSHOPTS} ${HOME}/.screenrc root@$(ip_for ${server}):/root/.screenrc
fi
ssh ${SSHOPTS} root@$(ip_for ${server}) "cat /tmp/${scriptName}.sh | /bin/bash -s - ${as} ${ip} ${OPENCENTER_PASSWORD}"
if !( $USE_PACKAGES ); then
echo "removing github key"
ssh ${SSHOPTS} root@$(ip_for ${server}) 'rm /root/.ssh/id_rsa'
fi
}
instance_exists(){
name=$(mangle_name $1)
$NOVA list |grep -q $name
}
#only need to source nova env if not using supernova
if [[ "$NOVA" == "nova" ]]
then
if [[ -f ${HOME}/csrc ]]; then
source ${HOME}/csrc
else
echo "Please setup your cloud credentials file in ${HOME}/csrc"
exit 1
fi
fi
network_string=""
if $USE_NETWORK
then
if ( $NOVA network-list | grep -q ${CLUSTER_PREFIX} ); then
echo "Network ${CLUSTER_PREFIX}-net already exists, delete and re-run or use different prefix"
exit 1
fi
if !( $NOVA network-create ${CLUSTER_PREFIX}-net ${PRIV_NETWORK} > /dev/null 2>&1 ); then
echo "Error creating Network - run $NOVA network-create ${CLUSTER_PREFIX}-net ${PRIV_NETWORK} to diagnose"
exit 1
fi
priv_network_id=$($NOVA network-list | grep ${CLUSTER_PREFIX}-net | awk '{print $2}')
network_string="--nic net-id=${priv_network_id}"
echo "Network ${priv_network_id} created"
fi
imagelist=$($NOVA image-list)
flavorlist=$($NOVA flavor-list)
image=$(echo "${imagelist}" | grep "${IMAGE_TYPE}" | head -n1 | awk '{ print $2 }')
flavor_2g=$(echo "${flavorlist}" | grep 2GB | head -n1 | awk '{ print $2 }')
flavor_4g=$(echo "${flavorlist}" | grep 4GB | head -n1 | awk '{ print $2 }')
if ! $RERUN
then
if ( $NOVA list | grep -q $(mangle_name) ); then
echo "$(mangle_name) prefix is already in use, select another, or delete existing servers"
exit 1
fi
fi
if [[ -f ${HOME}/.ssh/authorized_keys ]]; then
instance_exists opencenter-server || $NOVA boot --flavor=${flavor_4g} --image ${image} ${network_string} --file /root/.ssh/authorized_keys=${HOME}/.ssh/authorized_keys $(mangle_name opencenter-server) > /dev/null 2>&1
for client in $(seq 1 $CLIENT_COUNT); do
instance_exists opencenter-client${client} || $NOVA boot --flavor=${flavor_2g} --image ${image} ${network_string} --file /root/.ssh/authorized_keys=${HOME}/.ssh/authorized_keys $(mangle_name opencenter-client${client}) > /dev/null 2>&1
done
instance_exists opencenter-dashboard || $NOVA boot --flavor=${flavor_2g} --image ${image} ${network_string} --file /root/.ssh/authorized_keys=${HOME}/.ssh/authorized_keys $(mangle_name opencenter-dashboard) > /dev/null 2>&1
else
echo "Please setup your ${HOME}/.ssh/authorized_keys file for key injection to cloud servers "
exit 1
fi
nodes=("opencenter-server")
wait_for_ssh "opencenter-server"
for client in $(seq 1 $CLIENT_COUNT); do
wait_for_ssh "opencenter-client${client}"
nodes=(${nodes[@]} "opencenter-client${client}")
done
wait_for_ssh "opencenter-dashboard"
nodes=(${nodes[@]} "opencenter-dashboard")
for svr in ${nodes[@]}; do
what=agent
if [ "${svr}" == "opencenter-server" ]; then
what=server
fi
if [ "${svr}" == "opencenter-dashboard" ]; then
what=dashboard
fi
setup_server_as ${svr} ${what} > /tmp/$(mangle_name ${svr}).log 2>&1 &
echo "Setting up server $(mangle_name ${svr}) as ${what} - logging status to /tmp/$(mangle_name ${svr}).log"
PIDS["$!"]=${svr}
done
for pid in ${!PIDS[@]}; do
echo "Waiting on pid ${pid}: ${PIDS[${pid}]}"
if [ ${pid} -ne 0 ]; then
wait ${pid} > /dev/null 2>&1
echo "Reaped ${pid}"
if [ $? -ne 0 ]; then
echo "Error setting up ${PIDS[${pid}]}"
fi
fi
done
server_ip=$(ip_for opencenter-server)
echo -e "\n*** COMPLETE ***\n"
echo -e "Run \"export OPENCENTER_ENDPOINT=http://${server_ip}:8080\" to use the opencentercli"
dashboard_ip=$(ip_for opencenter-dashboard)
echo -e "Or connect to \"http://${dashboard_ip}:${DASHBOARD_PORT}\" to manage via the opencenter-dashboard interface\n"