Naoki Shiota | 9a1e6d1 | 2014-04-03 14:47:12 -0700 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | ### Env vars used by this script. (default value) ### |
| 6 | # $ONOS_HOME : path of root directory of ONOS repository (~/ONOS) |
| 7 | # $ONOS_CLUSTER_HOME : path of ONOS cluster tools directory (this script's dir) |
| 8 | # $REMOTE_ONOS_HOME : path of root directory of ONOS repository in remote hosts (ONOS) |
| 9 | # $SSH : command name to access host |
| 10 | # $PSSH : command name to access hosts in parallel |
| 11 | # $SCP : command name to copy config file to each host |
| 12 | ##################################################### |
| 13 | |
| 14 | |
| 15 | ### Variables read from ONOS config file ### |
| 16 | ONOS_HOME=${ONOS_HOME:-${HOME}/ONOS} |
| 17 | |
| 18 | source ${ONOS_HOME}/scripts/common/utils.sh |
| 19 | |
| 20 | CLUSTER_HOME=${ONOS_CLUSTER_HOME:-$(cd `dirname $0`; pwd)} |
| 21 | CLUSTER_CONF_DIR=${CLUSTER_HOME}/conf |
| 22 | CLUSTER_CONF=${ONOS_CLUSTER_CONF:-${CLUSTER_CONF_DIR}/onos-cluster.conf} |
| 23 | CLUSTER_TEMPLATE_DIR=${CLUSTER_CONF_DIR}/template |
| 24 | |
| 25 | REMOTE_ONOS_HOME=${REMOTE_ONOS_HOME:-ONOS} |
| 26 | REMOTE_ONOS_CONF_DIR=${REMOTE_ONOS_HOME}/conf |
| 27 | |
| 28 | if [ ! -f ${CLUSTER_CONF} ]; then |
| 29 | echo "${CLUSTER_CONF} not found." |
| 30 | exit 1 |
| 31 | fi |
| 32 | CLUSTER_HOSTS=$(read-conf ${CLUSTER_CONF} cluster.hosts.names `hostname` | tr ',' ' ') |
| 33 | CLUSTER_BACKEND=$(read-conf ${CLUSTER_CONF} cluster.hosts.backend) |
| 34 | CLUSTER_RC_PROTOCOL=$(read-conf ${CLUSTER_CONF} cluster.hosts.ramcloud.protocol "fast+udp") |
| 35 | CLUSTER_HC_NETWORK=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.network) |
| 36 | CLUSTER_HC_ADDR=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.multicast.address "224.2.2.3") |
| 37 | CLUSTER_HC_PORT=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.multicast.port "54327") |
| 38 | ############################################ |
| 39 | |
| 40 | |
| 41 | ONOS_CONF_TEMPLATE=${CLUSTER_TEMPLATE_DIR}/onos_node.conf.template |
| 42 | |
| 43 | |
| 44 | ### Parallel SSH settings ### |
| 45 | SSH=${SSH:-ssh} |
| 46 | PSSH=${PSSH:-parallel-ssh} |
| 47 | PSSH_CONF=${CLUSTER_CONF_DIR}/pssh.hosts |
| 48 | SCP=${SCP:-scp} |
| 49 | ############################# |
| 50 | |
| 51 | |
| 52 | ############# Common functions ############# |
| 53 | function print_usage { |
| 54 | local filename=`basename ${ONOS_CLUSTER_CONF}` |
| 55 | local usage="Usage: setup/deploy/start/stop/status ONOS cluster. |
| 56 | \$ $0 setup [-f] |
| 57 | Set up ONOS cluster using ${filename}. |
| 58 | If -f option is used, all existing files will be overwritten without confirmation. |
| 59 | \$ $0 deploy [-f] |
| 60 | Deliver node config files to cluster nodes. |
| 61 | If -f option is used, all existing files will be overwritten without confirmation. |
| 62 | \$ $0 start |
| 63 | Start ONOS cluster |
| 64 | \$ $0 stop |
| 65 | Stop ONOS cluster |
| 66 | \$ $0 status |
| 67 | Show status of ONOS-cluster" |
| 68 | |
| 69 | echo "${usage}" |
| 70 | } |
| 71 | |
| 72 | ############################################ |
| 73 | |
| 74 | |
| 75 | ############# Setup functions ############## |
| 76 | |
| 77 | function list-zk-hosts { |
| 78 | local list=() |
| 79 | for host in ${CLUSTER_HOSTS}; do |
| 80 | local zk_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.zk.host") |
| 81 | |
| 82 | if [ -z ${zk_host_string} ]; then |
| 83 | # falling back to ip |
| 84 | zk_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.ip") |
| 85 | fi |
| 86 | if [ -z ${zk_host_string} ]; then |
| 87 | # falling back to hostname |
| 88 | zk_host_string=${host} |
| 89 | fi |
| 90 | |
| 91 | list=("${list[@]}" ${zk_host_string}) |
| 92 | done |
| 93 | |
| 94 | # join with comma |
| 95 | local IFS=, |
| 96 | echo "${list[*]}" |
| 97 | } |
| 98 | |
| 99 | function list-hc-hosts { |
| 100 | local list=() |
| 101 | for host in ${CLUSTER_HOSTS}; do |
| 102 | local hc_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.hazelcast.ip") |
| 103 | |
| 104 | if [ -z ${hc_host_string} ]; then |
| 105 | # falling back to ip |
| 106 | hc_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.ip") |
| 107 | fi |
| 108 | |
| 109 | if [ -z ${hc_host_string} ]; then |
| 110 | # falling back to hostname |
| 111 | hc_host_string=${host} |
| 112 | fi |
| 113 | |
| 114 | list=("${list[@]}" ${hc_host_string}) |
| 115 | done |
| 116 | |
| 117 | local IFS=, |
| 118 | echo "${list[*]}" |
| 119 | } |
| 120 | |
| 121 | function create-pssh-conf { |
| 122 | local tempfile=`begin-conf-creation ${PSSH_CONF}` |
| 123 | |
| 124 | # creation of pssh config file |
| 125 | for host in ${CLUSTER_HOSTS}; do |
| 126 | local user=$(read-conf ${CLUSTER_CONF} remote.${host}.ssh.user) |
| 127 | if [ -z ${user} ]; then |
| 128 | # falling back to common setting |
| 129 | user=$(read-conf ${CLUSTER_CONF} remote.common.ssh.user) |
| 130 | fi |
| 131 | |
| 132 | if [ -z ${user} ]; then |
| 133 | echo ${host} >> ${tempfile} |
| 134 | else |
| 135 | echo ${user}@${host} >> ${tempfile} |
| 136 | fi |
| 137 | done |
| 138 | |
| 139 | end-conf-creation ${PSSH_CONF} |
| 140 | } |
| 141 | |
| 142 | # create-onos-conf {hostname} |
| 143 | function create-onos-conf { |
| 144 | local host_name=${1} |
| 145 | |
| 146 | if [ -z ${host_name} ]; then |
| 147 | echo "FAILED" |
| 148 | echo "[ERROR] invalid hostname ${host_name}" |
| 149 | exit 1 |
| 150 | fi |
| 151 | |
| 152 | local onos_conf="${CLUSTER_CONF_DIR}/onos_node.${host_name}.conf" |
| 153 | local tempfile=`begin-conf-creation ${onos_conf}` |
| 154 | |
| 155 | cp ${ONOS_CONF_TEMPLATE} ${tempfile} |
| 156 | |
| 157 | local prefix="cluster.${host}" |
| 158 | |
| 159 | local host_ip=$(read-conf ${CLUSTER_CONF} "${prefix}.ip") |
| 160 | local host_string=${host_ip} |
| 161 | if [ -z "${host_string}" ]; then |
| 162 | host_string=${host_name} |
| 163 | fi |
| 164 | local host_role=$(read-conf ${CLUSTER_CONF} "${prefix}.role") |
| 165 | local zk_hosts=`list-zk-hosts` |
| 166 | local rc_ip=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.ip" ${host_string}) |
| 167 | local rc_coord_port=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.coordinator.port" 12246) |
| 168 | local rc_server_port=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.server.port" 12242) |
| 169 | local hc_hosts=`list-hc-hosts` |
| 170 | |
| 171 | # creation of ONOS node config file |
| 172 | sed -i -e "s|__HOST_NAME__|${host_name}|" ${tempfile} |
| 173 | if [ -z "${host_ip}" ]; then |
| 174 | # comment out |
| 175 | sed -i -e "s|^\(.*__HOST_IP__.*\)$|#\1|" ${tempfile} |
| 176 | else |
| 177 | sed -i -e "s|__HOST_IP__|${host_ip}|" ${tempfile} |
| 178 | fi |
| 179 | sed -i -e "s|__ONOS_ROLE__|${host_role}|" ${tempfile} |
| 180 | sed -i -e "s|__BACKEND__|${CLUSTER_BACKEND}|" ${tempfile} |
| 181 | sed -i -e "s|__ZK_HOSTS__|${zk_hosts}|" ${tempfile} |
| 182 | sed -i -e "s|__RAMCLOUD_PROTOCOL__|${CLUSTER_RC_PROTOCOL}|" ${tempfile} |
| 183 | sed -i -e "s|__RAMCLOUD_IP__|${rc_ip}|" ${tempfile} |
| 184 | sed -i -e "s|__RAMCLOUD_COORD_PORT__|${rc_coord_port}|" ${tempfile} |
| 185 | sed -i -e "s|__RAMCLOUD_SERVER_PORT__|${rc_server_port}|" ${tempfile} |
| 186 | |
| 187 | if [ ${CLUSTER_HC_NETWORK} = "tcp-ip" ]; then |
| 188 | sed -i -e "s|__HAZELCAST_MEMBERS__|${hc_hosts}|" ${tempfile} |
| 189 | |
| 190 | # Comment out unused parameters |
| 191 | sed -i -e "s|^\(.*__HAZELCAST_MULTICAST_GROUP__.*\)$|#\1|" ${tempfile} |
| 192 | sed -i -e "s|^\(.*__HAZELCAST_MULTICAST_PORT__.*\)$|#\1|" ${tempfile} |
| 193 | elif [ ${CLUSTER_HC_NETWORK} = "multicast" ]; then |
| 194 | sed -i -e "s|__HAZELCAST_MULTICAST_GROUP__|${CLUSTER_HC_ADDR}|" ${tempfile} |
| 195 | sed -i -e "s|__HAZELCAST_MULTICAST_PORT__|${CLUSTER_HC_PORT}|" ${tempfile} |
| 196 | |
| 197 | sed -i -e "s|^\(.*__HAZELCAST_MEMBERS__.*\)$|#\1|" ${tempfile} |
| 198 | fi |
| 199 | |
| 200 | end-conf-creation ${onos_conf} |
| 201 | } |
| 202 | |
| 203 | # setup -f : force overwrite existing files |
| 204 | function setup { |
| 205 | if [ "${1}" = "-f" ]; then |
| 206 | create-pssh-conf |
| 207 | |
| 208 | for host in ${CLUSTER_HOSTS}; do |
| 209 | create-onos-conf ${host} |
| 210 | done |
| 211 | else |
| 212 | create-conf-interactive ${PSSH_CONF} create-pssh-conf |
| 213 | |
| 214 | for host in ${CLUSTER_HOSTS}; do |
| 215 | local filename="${CLUSTER_CONF_DIR}/onos_node.${host}.conf" |
| 216 | create-conf-interactive ${filename} create-onos-conf ${host} |
| 217 | done |
| 218 | fi |
| 219 | } |
| 220 | |
| 221 | ############################################ |
| 222 | |
| 223 | |
| 224 | ############ Deploy functions ############## |
| 225 | |
| 226 | function deploy { |
| 227 | if [ ! -f ${PSSH_CONF} ]; then |
| 228 | echo "[ERROR] ${PSSH_CONF} not found" |
| 229 | local command=`basename ${0}` |
| 230 | echo "[ERROR] Try \"${command} setup\" to create files." |
| 231 | exit 1 |
| 232 | fi |
| 233 | |
| 234 | for host in ${CLUSTER_HOSTS}; do |
| 235 | local conf=${CLUSTER_CONF_DIR}/onos_node.${host}.conf |
| 236 | if [ ! -f ${conf} ]; then |
| 237 | echo "[ERROR] ${conf} not found" |
| 238 | local command=`basename ${0}` |
| 239 | echo "[ERROR] Try \"${command} setup\" to create files." |
| 240 | exit 1 |
| 241 | fi |
| 242 | |
| 243 | local user=$(read-conf ${CLUSTER_CONF} "remote.${host}.ssh.user") |
| 244 | if [ -z ${user} ]; then |
| 245 | # falling back to common setting |
| 246 | user=$(read-conf ${CLUSTER_CONF} "remote.common.ssh.user") |
| 247 | fi |
| 248 | |
| 249 | ${SCP} ${conf} ${user}@${host}:${REMOTE_ONOS_CONF_DIR} |
| 250 | ${SSH} ${user}@${host} "cd ${REMOTE_ONOS_HOME}; ./onos.sh setup -f" |
| 251 | done |
| 252 | |
| 253 | # TODO: Replacing per-host ssh command with pssh command below. |
| 254 | # Need to solve concurrency problem when ONOS directory is shared among hosts. |
| 255 | # ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh setup -f" |
| 256 | } |
| 257 | ############################################ |
| 258 | |
| 259 | |
| 260 | ############# Start functions ############## |
| 261 | |
| 262 | function start { |
| 263 | if [ ! -f ${PSSH_CONF} ]; then |
| 264 | echo "[ERROR] ${PSSH_CONF} not found" |
| 265 | local command=`basename ${0}` |
| 266 | echo "[ERROR] Try \"${command} setup\" to create files." |
| 267 | exit 1 |
| 268 | fi |
| 269 | |
| 270 | ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh start" |
| 271 | } |
| 272 | |
| 273 | ############################################ |
| 274 | |
| 275 | |
| 276 | ############# Stop functions $############## |
| 277 | |
| 278 | function stop { |
| 279 | if [ ! -f ${PSSH_CONF} ]; then |
| 280 | echo "[ERROR] ${PSSH_CONF} not found" |
| 281 | local command=`basename ${0}` |
| 282 | echo "[ERROR] Try \"${command} setup\" to create files." |
| 283 | exit 1 |
| 284 | fi |
| 285 | |
| 286 | ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh stop" |
| 287 | } |
| 288 | |
| 289 | ############################################ |
| 290 | |
| 291 | |
| 292 | ############ Status functions ############## |
| 293 | |
| 294 | function status { |
| 295 | if [ ! -f ${PSSH_CONF} ]; then |
| 296 | echo "[ERROR] ${PSSH_CONF} not found" |
| 297 | local command=`basename ${0}` |
| 298 | echo "[ERROR] Try \"${command} setup\" to create files." |
| 299 | exit 1 |
| 300 | fi |
| 301 | |
| 302 | ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh status" |
| 303 | } |
| 304 | |
| 305 | ############################################ |
| 306 | |
| 307 | |
| 308 | ################## Main #################### |
| 309 | case "$1" in |
| 310 | setup) |
| 311 | setup $2 |
| 312 | ;; |
| 313 | deploy) |
| 314 | deploy |
| 315 | ;; |
| 316 | start) |
| 317 | start |
| 318 | ;; |
| 319 | stop) |
| 320 | stop |
| 321 | ;; |
| 322 | stat*) # <- status |
| 323 | status |
| 324 | ;; |
| 325 | *) |
| 326 | print_usage |
| 327 | exit 1 |
| 328 | esac |