#!/bin/bash # The MIT License (MIT) # # Copyright (c) 2015 ISISLab, Università degli Studi di Salerno # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # FUNCTIONS DEFINITIONS # # host_exec_cmd # # Description: # Calls a command on a host, abstracting the locality of the host. # # Parameters: # $1 hostname where to perform the command # $2 the command to execute function host_exec_cmd { if [ "$MYHOST" == "$1" ]; then "$2" else ssh -t ${SSH_USER}${1}.routetopa.eu "$2" fi } # host_send_file # # Description: # Send a local file to remote host. If the copy is issued on the local host # itself, the command is ignored. # # Parameters: # $1 hostname to copy the file to # $2 local filename # $3 remote localhost function host_send_file { if [ "$MYHOST" == "$1" ]; then cp ${2} ${3} else scp ${2} ${SSH_USER}${1}.routetopa.eu:${3} fi } # host_recv_file # # Description: # Receive a remote file to localhost host. If the copy is issued on the local host # itself, the command is ignored. # # Parameters: # $1 hostname to copy the file from # $2 remote filename # $3 local filename function host_recv_file { if [ "$MYHOST" == "$1" ]; then cp ${2} ${3} else scp ${SSH_USER}${1}.routetopa.eu:${2} ./${3} fi } # # DEFAULT OPTIONS # SSH_USER= SSH_PASS= # MYSQL_HOST=localhost # IGNORED MYSQL_USER=root MYSQL_PASS=is15rdc MYSQL_DB=oxwall HOSTLIST=( 'spod' 'prato' 'dublin' 'issy' 'denhaag' 'groningen' ) # # OPTION PROCESSING # OPTIONS=$(getopt -o s: -- $@) eval set -- $OPTIONS while true; do case "$1" in -s) SSH_USER="${2}@" shift 2 ;; --) shift break ;; *) echo "Unknown option: $1" shift break ;; esac done # # VARIABLES # ALLOWEDHOST="spod.routetopa.eu" MYHOST=$(hostname) #if [ "$MYHOST" != "$ALLOWEDHOST" ]; then #echo "This script can be run only on ${ALLOWEDHOST}. It appears you are running it on ${MYHOST}" #echo "Exiting..." #exit -1; #fi # # PERFORM SELECTED ACTION # ACTION=$1 case "$ACTION" in cmd) shift ARG_COMMAND=$@ if [ -z "$ARG_COMMAND" ]; then echo "ERROR: Please specify a command: $0 $ACTION touch last_access" exit 1 fi for HOST in ${HOSTLIST[@]}; do host_exec_cmd ${HOST} "${ARG_COMMAND}" done ;; db-backup) ARG_BACKUP_NAME=$2 if [ -z $ARG_BACKUP_NAME ]; then ARG_BACKUP_NAME="${MYSQL_DB}-$(date +%Y%m%d-%H%M%S).sql" echo "No given backup filename. Using $ARG_BACKUP_NAME" fi for HOST in ${HOSTLIST[@]}; do host_exec_cmd ${HOST} "mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} > ${ARG_BACKUP_NAME}" host_recv_file ${HOST} ${ARG_BACKUP_NAME} ${HOST}-${ARG_BACKUP_NAME} done ;; db-restore) ARG_BACKUP_NAME=$2 if [ -z ${ARG_BACKUP_NAME} ]; then echo "ERROR: Please specify the name of the backup. e.g.: $0 $ACTION backup.sql" exit 1 fi for HOST in ${HOSTLIST[@]}; do if [ ! -f "${HOST}-${ARG_BACKUP_NAME}" ]; then echo "ERROR: File ${HOST}-${ARG_BACKUP_NAME} does not exists" exit 1 fi done for HOST in ${HOSTLIST[@]}; do host_send_file ${HOST} "${HOST}-${ARG_BACKUP_NAME}" "${ARG_BACKUP_NAME}" host_exec_cmd ${HOST} "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${ARG_BACKUP_NAME}" done ;; db-sql) ARG_SCRIPT_NAME=$2 if [ -z $ARG_SCRIPT_NAME ]; then echo "ERROR: Please specify the name of the sql. e.g.: $0 $ACTION do_something.sql" exit 1 fi if [ ! -f $ARG_SCRIPT_NAME ]; then echo "ERROR: File $ARG_SCRIPT_NAME does not exists" exit 1 fi for HOST in ${HOSTLIST[@]}; do host_send_file ${HOST} ${ARG_SCRIPT_NAME} ${HOST}-${ARG_SCRIPT_NAME} host_exec_cmd ${HOST} "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${HOST}-${ARG_SCRIPT_NAME}" #echo scp ${ARG_SCRIPT_NAME} ${SSH_USER}${HOST}.routetopa.eu:${ARG_SCRIPT_NAME} #echo ssh -t ${SSH_USER}${HOST}.routetopa.eu "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${ARG_SCRIPT_NAME}; rm ${ARG_SCRIPT_NAME}" done ;; script) ARG_SCRIPT_NAME=$2 if [ -z $ARG_SCRIPT_NAME ]; then echo "ERROR: Please specify the name of the script. e.g.: $0 $ACTION do_something.sh" exit 1 fi if [ ! -f $ARG_SCRIPT_NAME ]; then echo "ERROR: File $ARG_SCRIPT_NAME does not exists" exit 1 fi for HOST in ${HOSTLIST[@]}; do host_send_file ${HOST} ${ARG_SCRIPT_NAME} ${HOST}-${ARG_SCRIPT_NAME} host_exec_cmd ${HOST} "source ${HOST}-$ARG_SCRIPT_NAME; rm ${HOST}-$ARG_SCRIPT_NAME" #echo scp $ARG_SCRIPT_NAME ${SSH_USER}${HOST}.routetopa.eu:$ARG_SCRIPT_NAME #echo ssh -t ${SSH_USER}${HOST}.routetopa.eu "source $ARG_SCRIPT_NAME; rm $ARG_SCRIPT_NAME" done ;; update) echo "Updating production sites..." for HOST in ${HOSTLIST[@]}; do host_exec_cmd ${HOST} "sudo ./spod-manager.sh -a update -p isisadmin -q isislab.unisa@gmail.com -r is15rdc1 -w http://spod.routetopa.eu/" done ;; ""|help) echo "ROUTE-TO-PA PROJECT: CLUSTER-MANAGER.SH" echo " " echo "Usage: $0 [options]" echo " " echo "List of actions:" echo " help : Show this screen" echo " cmd : Execute on hosts" echo " db-backup [filename] : Perform a database backup" echo " db-restore [filename] : Restore a database backup" echo " db-sql : Copy to hosts and run with mysql client" echo " script : Copy to hosts and execute it" echo " update : Update code using git pull" echo " " echo "List of options:" echo " -s " ;; *) echo "Unknown action: \"$ACTION\". Try calling this script without arguments to get help." ;; esac