#!/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:h: -- $@) eval set -- $OPTIONS while true; do case "$1" in -h) IFS=',' read -a HOSTLIST <<< "$2" shift 2 ;; -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}" done ;; db-transfer) ARG_TABLES=$2 ARG_HOST_FROM=$3 IFS=',' read -a ARG_HOST_TO <<< "$4" if [ -z ARG_TABLES ] || [ -z $ARG_HOST_FROM ]; then echo "ERROR: Please specify at least the table(s) and the source host." echo "Examples" echo " $0 $ACTION table sourcehost" echo " $0 $ACTION table1,table2 sourcehost destinationhost" echo " $0 $ACTION table sourcehost destinationhost1,destinationhost2,destinationhost3" exit 1 fi if [ -z "$ARG_HOST_TO" ]; then ARG_HOST_TO="${HOSTLIST[@]}" fi TABLES=$(echo "$ARG_TABLES" | tr , " ") ARG_BACKUP_NAME="TEMP-$(date +%Y%m%d-%H%M%S).sql" host_exec_cmd ${ARG_HOST_FROM} "mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} --add-drop-table ${MYSQL_DB} ${TABLES} > ${ARG_BACKUP_NAME}" host_recv_file ${ARG_HOST_FROM} ${ARG_BACKUP_NAME} LOCAL-${ARG_BACKUP_NAME} host_exec_cmd ${ARG_HOST_FROM} "rm ${ARG_BACKUP_NAME}" for HOST_TO in ${ARG_HOST_TO[@]}; do if [ "$ARG_HOST_FROM" == "$HOST_TO" ]; then continue fi host_send_file ${HOST_TO} LOCAL-${ARG_BACKUP_NAME} ${HOST_TO}-${ARG_BACKUP_NAME} host_exec_cmd ${HOST_TO} "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${HOST_TO}-${ARG_BACKUP_NAME}; rm ${HOST_TO}-${ARG_BACKUP_NAME}" done rm LOCAL-${ARG_BACKUP_NAME} ;; lang-add) ARG_PREFIX=$2 ARG_KEY=$3 if [ -z $ARG_KEY ]; then echo "ERROR: Please specify language prefix and key. e.g.: $0 $ACTION ode add_button" exit 1 fi SCRIPT_NAME="TEMP-$(date +%Y%m%d-%H%M%S).sql" echo "INSERT INTO ow_base_language_prefix(prefix, label) SELECT '$ARG_PREFIX', '$ARG_PREFIX' FROM DUAL WHERE NOT EXISTS (SELECT id FROM ow_base_language_prefix WHERE prefix LIKE '$ARG_PREFIX' LIMIT 1);" > $SCRIPT_NAME echo "SET @prefix_id = (SELECT id FROM ow_base_language_prefix WHERE prefix LIKE '$ARG_PREFIX' LIMIT 1);" >> $SCRIPT_NAME echo "INSERT INTO ow_base_language_key(prefixId, \`key\`) VALUES(@prefix_id, '$ARG_KEY');" >> $SCRIPT_NAME for HOST in ${HOSTLIST[@]}; do host_send_file ${HOST} ${SCRIPT_NAME} ${HOST}-${SCRIPT_NAME} host_exec_cmd ${HOST} "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${HOST}-${SCRIPT_NAME}; rm ${HOST}-${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" 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://${HOST}.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 : Restore a database backup" echo " db-transfer table1,table2 srchost dsthost1,dsthost2" echo " : Copy table(s) from srchost to desthost(s)" echo " db-sql : Copy to hosts and run with mysql client" echo " lang-add " echo " : Add a language string" echo " script : Copy to hosts and execute it" echo " update : Update code using git pull" echo " " echo "List of options:" echo " -s " echo " -h " ;; *) echo "Unknown action: \"$ACTION\". Try calling this script without arguments to get help." ;; esac