Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # RDM Instance Upgrade Utility
- # MAKE SURE dialog IS INSTALLED!!!
- which dialog > /dev/null
- if [ $? != 0 ]; then
- echo "ERROR: dialog 0.4 or greater must be installed!"
- echo "This can be installed on Debian/Ubuntu by running: sudo apt-get install dialog"
- exit 2
- fi
- # Make sure we are root
- if [ "$(whoami)" != "root" ]; then
- echo "ERROR: This application must be ran as root!"
- echo "Please call sudo $0"
- exit 3
- fi
- # Debug settings; DO NOT CHANGE!
- __debug=${__debug:-1}
- __debuglog=${__debuglog:-debug.log}
- # End debug settings
- # Application settings
- __appname="RDM Upgrade Utility"
- __quietmode=${__quietmode:-0}
- __dialog=dialog
- __boxwidth=40
- __boxheight=15
- # End application settings
- # Database settings
- __dbuser=${__dbuser:-jboss}
- __dbpass=${__dbpass:-v3ecdbpw}
- # End database settings
- # Host containing update files
- __updatehost=${__updatehost:-rdm3.equipmentmonitors.net}
- __updateuser=${__updateuser:-tsc}
- # Path to JBoss
- __jbosspath=${__jbosspath:-/opt/jboss}
- # Path to init scripts
- __initdir=${__initdir:-/etc/init.d}
- # Path to upgrade packages
- __pkgpath=${__pkgpath:-$HOME/xfer}
- __backupdir=${__backupdir:-$HOME/backups}
- # Customer name
- # - if no customer is specified on cmdline, assume guided mode
- # Database name (usually customer name)
- # - if no database is specified on cmdline, assume customer name
- # Version number to upgrade to
- # - if no version is specified on cmdline then check pkgpath for latest
- # version and assume it.
- # Verify customer
- # Verify database
- # Verify upgrade pkg
- # Stop instance
- # Unpack ear
- # Unpack jasper
- # Create backup
- # Dump database
- # Copy ear
- # Copy jasper
- # Replace application root
- # Run dbAlter
- # Start instance
- # Remember for transfers to work:
- # sudo ssh-keygen -t dsa (NO PASSPHRASE!!!)
- # sudo ssh-copy-id -i /root/.ssh/id_dsa.pub $__updateuser@$__updatehost
- checkLatest() {
- scp $__updateuser@$__updatehost:~/xfer/latest . > /dev/null 2>scp.error
- if [ $? == 0 ]; then
- read latest < latest
- return 0
- fi
- return $?
- }
- backupDirs() {
- if [ $# != 1 ]; then
- return 1
- fi
- if [ ! -d $__backupdir ]; then
- mkdir -p $__backupdir
- if [ $? != 0 ]; then
- if [ $__debug != 0 ]; then
- echo "backupDirs(): mkdir -p $__backupdir: Failed!" >> $__debuglog
- fi
- fi
- fi
- tar cjf $__backupdir/$1-`date '+%Y-%m-%d'`.tar.bz2 $__jbosspath/server/$1/deploy/shockwatch.ear $__jbosspath/server/$1/deploy/jasperfiles.war
- return $?
- }
- backupDatabase() {
- if [ $# != 1 ]; then
- return 1
- fi
- mysqldump -u$__dbuser -p$__dbpass --single-transaction $1 | bzip2 > $__backupdir/$1-`date '+%Y-%m-%d'`.sql.bz2
- return $?
- }
- cleanup() {
- if [ $__quietmode == 0 ]; then
- $__dialog --backtitle "$__appname" --title "Exiting..." --msgbox "$__appname will now exit." $__boxheight $__boxwidth
- fi
- clear
- if [ $# != 0 ]; then
- if [ $1 == 0 ]; then
- echo "$__appname completed successfully."
- elif [ $1 == 20 ]; then
- echo "Usage: `basename $0`"
- echo " [customer]"
- echo " [customer] [version]"
- echo " [customer] [database] [version]"
- exit 0
- else
- echo "$__appname completed with errors."
- fi
- exit $1
- else
- echo "$__appname complete with unknown status."
- fi
- exit 2
- }
- checkInstanceState() {
- }
- stopInstance() {
- if [ $# != 1 ]; then
- if [ $__debug != 0 ]; then
- echo "stopInstance(): Invalid number of arguments!" >> $__debuglog
- fi
- return 1
- fi
- $__initdir/jboss_$1 stop
- sleep 10
- }
- startInstance() {
- if [ $# != 1 ]; then
- if [ $__debug != 0 ]; then
- echo "startInstance(): Invalid number of arguments!" >> $__debuglog
- fi
- return 1
- fi
- $__initdir/jboss_$1 start
- sleep 10
- }
- # Verify that the customer installation exists
- # Checks for both the customer directory and startup script
- # Returns 0 on success; Non-zero on failure
- # Error codes:
- # 1 - Customer directory doesn't exist
- # 2 - Customer ear doesn't exist
- # 3 - Customer jasperfiles doesn't exist
- # 4 - Customer application.xml doesn't exist
- # 5 - Customer startup script doesn't exist
- verifyCustomer() {
- if [ $__debug != 0 ]; then
- echo "verifyCustomer" > $__debuglog
- fi
- # I know that this is ugly and we could use && but I wanted specific errors
- if [ -d $__jbosspath/server/$1 ]; then
- if [ -d $__jbosspath/server/$1/deploy/shockwatch.ear ]; then
- if [ -d $__jbosspath/server/$1/deploy/jasperfiles.war ]; then
- if [ $__debug != 0 ]; then
- echo "Checking if $__jbosspath/server/$1/deploy/shockwatch.ear/META-INF/application.xml exists" >> $__debuglog
- fi
- if [ -f $__jbosspath/server/$1/deploy/shockwatch.ear/META-INF/application.xml ]; then
- if [ -f $__initdir/jboss_$1 ]; then
- return 0
- else
- err=5
- fi
- else
- err=4
- fi
- else
- err=3
- fi
- else
- err=2
- fi
- else
- err=1
- fi
- createCustomerErrorMsg $err
- if [ $? != 0 ]; then
- $__dialog --backtitle "$__appname" --title "Error" --yesno "The specified customer '$customer' does not exist or is corrupt. Would you like to try a different customer?\n\nError details:\n$errormsg" $__boxheight $__boxwidth
- if [ $? == 0 ]; then
- createCustomerInputMenu
- else
- # cleanup
- cleanup 2
- fi
- fi
- }
- createCustomerErrorMsg() {
- case "$1" in
- 0) return 0
- ;;
- 1) errormsg="Customer directory doesn't exist."
- ;;
- 2) errormsg="Customer shockwatch.ear directory doesn't exist."
- ;;
- 3) errormsg="Customer jasperfiles.war directory doesn't exist."
- ;;
- 4) errormsg="Customer application.xml doesn't exist."
- ;;
- 5) errormsg="Customer startup script doesn't exist."
- ;;
- *) errormsg="Unknown error!"
- ;;
- esac
- return $1
- }
- # Verifies that the customer database exists
- # Returns 0 on success; Non-zero on failure
- verifyDatabase() {
- mysql -u$__dbuser -p$__dbpass $1 -e 'quit' 2> mysql.error
- if [ $? != 0 ]; then
- $__dialog --backtitle "$__appname" --title "Error" --yesno "MySQL returned an error while checking database '$1'. Would you like to try a different database?\n\nError details:\n`cat mysql.error`" $__boxheight $__boxwidth
- if [ $? == 0 ]; then
- createDatabaseInputMenu
- else
- # cleanup
- cleanup 2
- fi
- fi
- return $?
- }
- createDatabaseInputMenu() {
- ret=$($__dialog --stdout --backtitle "$__appname" --title "Select One" --menu "How do you wish to select a database?: " $__boxheight $__boxwidth 2 1 "Manually input database name" 2 "Select from a list (unstable)")
- if [ $ret == "1" ]; then
- createDatabaseInputBox
- return $?
- elif [ $ret == "2" ]; then
- createDatabaseSelectList
- return $?
- else
- cleanup 0
- fi
- }
- getDatabasesAsArray() {
- count=0
- for i in `mysql -u$__dbuser -p$__dbpass -e 'show schemas;' 2>/dev/null | awk '{print $c}' c=${1:-1} | egrep -vw 'Database|information_schema'`; do
- databases[$count]=$i
- count=$(($count + 1))
- done
- # Verify that the databases array isn't empty.
- if [ $count == 0 ]; then
- return 1
- else
- return 0
- fi
- }
- createDatabaseSelectList() {
- getDatabasesAsArray
- if [ $? != 0 ]; then
- if [ $__debug != 0 ]; then
- echo "No databases..." >> $__debuglog
- fi
- return 1;
- fi
- ret=$($__dialog --stdout --backtitle "$__appname" --radiolist "Select database:" $__boxheight $__boxwidth 6 `(
- count=1
- for i in "${databases[@]}"
- do
- echo -ne "$count $i "
- if [ $count == 1 ]; then
- echo "on"
- else
- echo "off"
- fi
- count=$(($count + 1))
- done
- )`)
- if [ $ret != 0 ]; then
- database=${databases[$(($ret - 1))]}
- verifyDatabase $database
- return $?
- else
- # Think about how I want to handle this
- cleanup 0
- fi
- }
- # Loads all directory names in $__jbosspath/server into an array
- # template directories are ignored
- # Return 0 on sucess; Non-zero on failure
- getCustomersAsArray() {
- count=0
- for i in `ls $__jbosspath/server/ | grep -v 'template'`
- do
- customers[$count]=$i
- count=$(($count + 1))
- done
- if [ $count != 0 ]; then
- return 0
- else
- return 1
- fi
- }
- # Creates a radio list from the array of customers
- createCustomerSelectList() {
- getCustomersAsArray
- if [ $? != 0 ]; then
- echo "No customers..."
- return 1
- fi
- ret=$($__dialog --stdout --backtitle "$__appname" --radiolist "Select customer:" $__boxheight $__boxwidth 6 `(
- count=1
- for i in "${customers[@]}"
- do
- echo -ne "$count $i "
- if [ $count == 1 ]; then
- echo "on"
- else
- echo "off"
- fi
- count=$(($count + 1))
- done
- )`)
- if [ $ret != 0 ]; then
- customer=${customers[$(($ret - 1))]}
- verifyCustomer $customer
- return $?
- else
- # TODO maybe prompt a menu?
- cleanup 0
- fi
- }
- createCustomerInputMenu() {
- ret=$($__dialog --stdout --backtitle "$__appname" --title "Select One" --menu "How do you wish to select a customer?: " $__boxheight $__boxwidth 2 1 "Manually input customer name" 2 "Select from a list")
- if [ $ret == 1 ]; then
- createCustomerInputBox
- return $?
- elif [ $ret == 2 ]; then
- createCustomerSelectList
- return $?
- else
- cleanup 0
- fi
- }
- # Creates an input box for manually entering a database
- # The database is verified upon entering. If the database doesn't exist
- # then the user is given the option of trying again or exiting the app.
- # Return 0 on success. The app exits or returns to the menu on failure.
- createDatabaseInputBox() {
- database=$($__dialog --stdout --backtitle "$__appname" --title "Input" --inputbox "Enter database name." $__boxheight $__boxwidth)
- verifyDatabase $database
- return $?
- }
- createCustomerInputBox() {
- customer=$($__dialog --stdout --backtitle "$__appname" --title "Input" --inputbox "Enter customer name." $__boxheight $__boxwidth)
- verifyCustomer $customer
- return $?
- }
- main() {
- if [ $# == 1 ]; then
- # Assume $1 to be customer name, verify it exists and prompt version
- # Test customer name as db, if fail, prompt
- echo $1
- elif [ $# == 2 ]; then
- # Assume $1 to be customer and $2 to be version, verify both
- # Test customer name as db, if fail, prompt
- echo $1 $2
- elif [ $# == 3 ]; then
- # Assume $1 to be customer, $2 to be database, $3 to be version
- # Test customer name as db, if fail, prompt
- echo $1 $2 $3
- else
- # If 0 or > 3 parameters are specified then prompt for guided mode
- $__dialog --backtitle "$__appname" --title "Confirmation" --yesno "No customer was specified. Do you wish to enter guided mode?" $__boxheight $__boxwidth
- if [ $? == 0 ]; then
- # Enter guided mode
- createCustomerInputMenu
- if [ $? == 0 ]; then
- # yesno for customer as database here
- $__dialog --backtitle "$__appname" --title "Confirmation" --yesno "Do you wish to use '$customer' as the database?" $__boxheight $__boxwidth
- if [ $? == 0 ]; then
- verifyDatabase $customer
- if [ $? != 0 ]; then
- $__dialog --backtitle "$__appname" --title "Error" --yesno "The database, '$customer', doesn't exist. Do you want to try a different database?" $__boxheight $__boxwidth
- if [ $? == 0 ]; then
- createDatabaseInputMenu
- else
- cleanup 1
- fi
- else
- database=$customer
- fi
- else
- createDatabaseInputMenu
- fi
- # By the time we are here, database should be set
- # Prompt for version number to upgrade to.
- fi
- else
- # Exit
- cleanup 20
- fi
- fi
- #createDatabaseInputMenu
- }
- main $*
Add Comment
Please, Sign In to add comment