Created
December 12, 2012 19:33
-
-
Save dentarg/4270830 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| DEBIAN_HOSTS="server1 server2 server3"; | |
| LOG_FILE="server-update-`date +%Y%m%d%H%M%S`.log" | |
| requires_reboot=""; | |
| function execute_command() | |
| { | |
| command="$1"; | |
| # $2 = variable to store output | |
| if [ $# = 2 ]; then | |
| tmp=$(ssh -o ConnectTimeout=5 -t -t -t ${server} "$command" | tee -a $LOG_FILE) | |
| eval $2=\$tmp | |
| else | |
| ssh -o ConnectTimeout=5 -t -t -t ${server} "$command" | tee -a $LOG_FILE | |
| fi | |
| } | |
| function execute_command_with_password() | |
| { | |
| command="$1"; | |
| pass="$2"; | |
| ssh -o ConnectTimeout=5 -t -t -t ${server} "sudo -S $command <<EOF | |
| $pass | |
| EOF | |
| " | tee -a $LOG_FILE | |
| } | |
| stty -echo | |
| read -p "* sudo password: " password; echo | |
| stty echo | |
| echo "NOTE: You might see a [sudo] prompt once in a while; ignore it!"; | |
| for server in $DEBIAN_HOSTS | |
| do | |
| echo "* Connecting to $server" | tee -a $LOG_FILE; | |
| # Connect to the next server, download updates and perform safe-upgrade | |
| echo "* Invalidating sudo session..."; | |
| execute_command "sudo -k" | |
| echo "* Updating packages..."; | |
| execute_command_with_password "aptitude update" $password | |
| echo "* Upgrading packages..."; | |
| execute_command "sudo -S aptitude safe-upgrade" | |
| if [ $? -ne 0 ]; then | |
| echo "Non-zero exit code - continue with next server (y/n)?"; | |
| read CONTINUE | |
| if [ "$CONTINUE" != "y" ]; then | |
| exit 1; | |
| fi | |
| fi | |
| echo "* Checking if reboot is needed..."; | |
| reboot_required=0; | |
| execute_command "[ -e /var/run/reboot-required ] && echo 1;" reboot_required | |
| if [ $reboot_required ]; then | |
| export requires_reboot="$server $requires_reboot" | |
| fi | |
| done | |
| # Print servers that requires rebooting | |
| if [ ! "$requires_reboot" = "" ]; then | |
| echo "---------------------------------------"; | |
| echo "THE FOLLOWING SERVERS MUST BE REBOOTED:"; | |
| for server in $requires_reboot | |
| do | |
| echo "$server"; | |
| done | |
| echo "---------------------------------------"; | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment