93 lines
2.7 KiB
Bash
Executable File
93 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
# Copyright 2015 Mellanox Technologies, Ltd
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# 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.
|
|
readonly LOG_FILE=/var/log/reboot_bootstrap_nodes.log
|
|
|
|
usage() {
|
|
echo "Usage: `basename $0` [-e environment_id] [-h] [-a]"
|
|
echo " This script is used to trigger reboot for nodes in 'discover' status,"
|
|
echo " of a given environment (if given) or of all environments. "
|
|
echo " Please wait for nodes to boot again after triggering this script."
|
|
|
|
echo "
|
|
Options:
|
|
|
|
-h Display the help message.
|
|
-e <env> Reboot all nodes in state 'discover' of the given environment.
|
|
-a Reboot all nodes in state 'discover' of all environments."
|
|
}
|
|
|
|
log() {
|
|
echo $1 | tee -a $LOG_FILE
|
|
}
|
|
|
|
while getopts "e:ha" opt; do
|
|
case $opt in
|
|
a)
|
|
env=''
|
|
log "Starting reboot cycle on env ${OPTARG} at `date`"
|
|
;;
|
|
h)
|
|
usage
|
|
exit 0
|
|
;;
|
|
e)
|
|
env="--env ${OPTARG}"
|
|
log "Starting reboot cycle on env ${OPTARG} at `date`"
|
|
;;
|
|
\?)
|
|
usage
|
|
exit 1
|
|
esac
|
|
done
|
|
if [ $OPTIND -eq 1 ]; then usage && exit 1; fi
|
|
shift $((OPTIND-1))
|
|
|
|
tmpjson=`mktemp`
|
|
trap "\rm -f $tmpjson; exit 1" SIGINT SIGTERM
|
|
|
|
fuel node $env --json > $tmpjson 2>/dev/null
|
|
ips=`python -c "import json; print '\n'.join([str(node['ip']) for node in json.load(open('$tmpjson')) if node['status'] == 'discover'])"`
|
|
|
|
if [ ${#ips} -eq 0 ];then
|
|
log "No nodes with state discover found for the given environments."
|
|
exit 0
|
|
fi
|
|
|
|
log "Rebooting nodes with IPs $ips"
|
|
for ip in $ips;
|
|
do
|
|
id=`python -c "import json; print '\n'.join([str(node['id']) for node in json.load(open('$tmpjson')) if node['ip'] == '$ip'])"`
|
|
ping -c 2 $ip &> /dev/null
|
|
if [ $? -eq 0 ];then
|
|
ssh $ip -o BatchMode=yes -o StrictHostKeyChecking=no reboot &> /dev/null
|
|
trials=20
|
|
log "waiting for node-$id to go offline."
|
|
while ping -c 2 $ip &> /dev/null && [ $trials -ne 0 ];do
|
|
((trials=$trials-1))
|
|
sleep 5;
|
|
done
|
|
if [ $trials -eq 0 ];then
|
|
log "node-$id reboot failed."
|
|
else
|
|
log "node-$id reboot succeeded. please wait for it to boot again."
|
|
fi
|
|
else
|
|
log "node-$id is already offline. please wait a few minutes or verify it is powered on."
|
|
fi
|
|
done
|
|
|
|
\rm -f $tmpjson
|