#!/bin/bash # # Execute commands on multiple nodes. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public # License along with this program. If not, see # . # Issue commands with pssh function mass_pssh { hydra_install_package pssh parallel-ssh -P -p 4 -t 0 -h $TMPWORK sudo $COMMAND } # Issue commands with a for loop function mass_loop { for node in `cat $TMPWORK`; do echo "Connecting to $node..." $HYDRA_CONNECT $node sudo $COMMAND done } # Issue commands with dish function mass_dish { hydra_install_package dish dish -f -p /dev/null -e "sudo $COMMAND" -g $TMPWORK } # Issue commands with mussh function mass_mussh { hydra_install_package mussh mussh -H $TMPWORK -c "sudo $COMMAND" } # Set nodes function mass_set_nodes { hydra_set_tmpfile nodes hydra $HYDRA nodes $1 > $TMPWORK } # Execute command on localhost function mass_exec_localhost { if grep -q -e "^$LOCAL$" $TMPWORK; then # Remove localhost from node list grep -v -e "^$LOCAL$" $TMPWORK > $TMPWORK.tmp mv $TMPWORK.tmp $TMPWORK echo "Issuing command on localhost..." $SUDO $COMMAND fi } # Load source $APP_BASE/lib/hydra/functions || exit 1 hydra_config_load # Command line arguments BASENAME="`basename $0`" COMMAND="$*" # Initial node list BASEDIR="/tmp" LOCAL="`facter fqdn`" mass_set_nodes # Set sudo config if [ "$WHOAMI" != 'root' ]; then sudo="sudo" else echo "Sorry, cannot run as root" exit 1 fi # Validation if [ "$BASENAME" == "mass-upgrade" ]; then COMMAND="DEBIAN_FRONTEND=noninteractive hydractl upgrade clean" elif [ "$BASENAME" == "mass-web" ]; then mass_set_nodes web fi # Execute commands in hosts echo "Issuing $COMMAND on multiple nodes..." mass_exec_localhost mass_pssh hydra_unset_tmpfile $TMPWORK