Guest User

Untitled

a guest
Apr 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.25 KB | None | 0 0
  1. #!/bin/bash
  2. # RDM Instance Upgrade Utility
  3.  
  4. # MAKE SURE dialog IS INSTALLED!!!
  5. which dialog > /dev/null
  6. if [ $? != 0 ]; then
  7.     echo "ERROR: dialog 0.4 or greater must be installed!"
  8.     echo "This can be installed on Debian/Ubuntu by running: sudo apt-get install dialog"
  9.     exit 2
  10. fi
  11.  
  12. # Make sure we are root
  13. if [ "$(whoami)" != "root" ]; then
  14.     echo "ERROR: This application must be ran as root!"
  15.     echo "Please call sudo $0"
  16.     exit 3
  17. fi
  18.  
  19. # Debug settings; DO NOT CHANGE!
  20. __debug=${__debug:-1}
  21. __debuglog=${__debuglog:-debug.log}
  22. # End debug settings
  23.  
  24. # Application settings
  25. __appname="RDM Upgrade Utility"
  26. __quietmode=${__quietmode:-0}
  27. __dialog=dialog
  28. __boxwidth=40
  29. __boxheight=15
  30. # End application settings
  31.  
  32. # Database settings
  33. __dbuser=${__dbuser:-jboss}
  34. __dbpass=${__dbpass:-v3ecdbpw}
  35. # End database settings
  36.  
  37. # Host containing update files
  38. __updatehost=${__updatehost:-rdm3.equipmentmonitors.net}
  39. __updateuser=${__updateuser:-tsc}
  40.  
  41. # Path to JBoss
  42. __jbosspath=${__jbosspath:-/opt/jboss}
  43.  
  44. # Path to init scripts
  45. __initdir=${__initdir:-/etc/init.d}
  46.  
  47. # Path to upgrade packages
  48. __pkgpath=${__pkgpath:-$HOME/xfer}
  49. __backupdir=${__backupdir:-$HOME/backups}
  50.  
  51. # Customer name
  52. # - if no customer is specified on cmdline, assume guided mode
  53. # Database name (usually customer name)
  54. # - if no database is specified on cmdline, assume customer name
  55. # Version number to upgrade to
  56. # - if no version is specified on cmdline then check pkgpath for latest
  57. #   version and assume it.
  58. # Verify customer
  59. # Verify database
  60. # Verify upgrade pkg
  61. # Stop instance
  62. # Unpack ear
  63. # Unpack jasper
  64. # Create backup
  65. # Dump database
  66. # Copy ear
  67. # Copy jasper
  68. # Replace application root
  69. # Run dbAlter
  70. # Start instance
  71.  
  72.  
  73. # Remember for transfers to work:
  74. # sudo ssh-keygen -t dsa (NO PASSPHRASE!!!)
  75. # sudo ssh-copy-id -i /root/.ssh/id_dsa.pub $__updateuser@$__updatehost
  76. checkLatest() {
  77.     scp $__updateuser@$__updatehost:~/xfer/latest . > /dev/null 2>scp.error
  78.     if [ $? == 0 ]; then
  79.         read latest < latest
  80.         return 0
  81.     fi
  82.  
  83.     return $?
  84. }
  85.        
  86.    
  87.    
  88.  
  89. backupDirs() {
  90.     if [ $# != 1 ]; then
  91.         return 1
  92.     fi
  93.  
  94.     if [ ! -d $__backupdir ]; then
  95.         mkdir -p $__backupdir
  96.         if [ $? != 0 ]; then
  97.             if [ $__debug != 0 ]; then
  98.                 echo "backupDirs(): mkdir -p $__backupdir: Failed!" >> $__debuglog
  99.             fi
  100.         fi
  101.     fi
  102.    
  103.     tar cjf $__backupdir/$1-`date '+%Y-%m-%d'`.tar.bz2 $__jbosspath/server/$1/deploy/shockwatch.ear $__jbosspath/server/$1/deploy/jasperfiles.war
  104.    
  105.     return $?
  106. }
  107.  
  108. backupDatabase() {
  109.     if [ $# != 1 ]; then
  110.         return 1
  111.     fi
  112.    
  113.     mysqldump -u$__dbuser -p$__dbpass --single-transaction $1 | bzip2 > $__backupdir/$1-`date '+%Y-%m-%d'`.sql.bz2
  114.    
  115.     return $?
  116. }
  117.  
  118. cleanup() {
  119.     if [ $__quietmode == 0 ]; then
  120.         $__dialog --backtitle "$__appname" --title "Exiting..." --msgbox "$__appname will now exit." $__boxheight $__boxwidth
  121.     fi
  122.     clear
  123.    
  124.     if [ $# != 0 ]; then
  125.         if [ $1 == 0 ]; then
  126.             echo "$__appname completed successfully."
  127.         elif [ $1 == 20 ]; then
  128.             echo "Usage: `basename $0`"
  129.             echo "    [customer]"
  130.             echo "    [customer] [version]"
  131.             echo "    [customer] [database] [version]"
  132.             exit 0
  133.         else
  134.             echo "$__appname completed with errors."
  135.         fi
  136.         exit $1
  137.     else
  138.         echo "$__appname complete with unknown status."
  139.     fi
  140.    
  141.     exit 2
  142. }
  143.  
  144. checkInstanceState() {
  145.    
  146. }
  147.  
  148. stopInstance() {
  149.     if [ $# != 1 ]; then
  150.         if [ $__debug != 0 ]; then
  151.             echo "stopInstance(): Invalid number of arguments!" >> $__debuglog
  152.         fi
  153.        
  154.         return 1
  155.     fi
  156.  
  157.     $__initdir/jboss_$1 stop
  158.     sleep 10
  159. }
  160.  
  161. startInstance() {
  162.     if [ $# != 1 ]; then
  163.         if [ $__debug != 0 ]; then
  164.             echo "startInstance(): Invalid number of arguments!" >> $__debuglog
  165.         fi
  166.        
  167.         return 1
  168.     fi
  169.  
  170.     $__initdir/jboss_$1 start
  171.     sleep 10
  172. }
  173.  
  174. # Verify that the customer installation exists
  175. # Checks for both the customer directory and startup script
  176. # Returns 0 on success; Non-zero on failure
  177. # Error codes:
  178. #   1 - Customer directory doesn't exist
  179. #   2 - Customer ear doesn't exist
  180. #   3 - Customer jasperfiles doesn't exist
  181. #   4 - Customer application.xml doesn't exist
  182. #   5 - Customer startup script doesn't exist
  183. verifyCustomer() {
  184.     if [ $__debug != 0 ]; then
  185.         echo "verifyCustomer" > $__debuglog
  186.     fi
  187.     # I know that this is ugly and we could use && but I wanted specific errors
  188.     if [ -d $__jbosspath/server/$1 ]; then
  189.         if [ -d $__jbosspath/server/$1/deploy/shockwatch.ear ]; then
  190.             if [ -d $__jbosspath/server/$1/deploy/jasperfiles.war ]; then
  191.                 if [ $__debug != 0 ]; then
  192.                     echo "Checking if $__jbosspath/server/$1/deploy/shockwatch.ear/META-INF/application.xml exists" >> $__debuglog
  193.                 fi
  194.                 if [ -f $__jbosspath/server/$1/deploy/shockwatch.ear/META-INF/application.xml ]; then
  195.                     if [ -f $__initdir/jboss_$1 ]; then
  196.                         return 0
  197.                     else
  198.                         err=5
  199.                     fi
  200.                 else
  201.                     err=4
  202.                 fi
  203.             else
  204.                 err=3
  205.             fi
  206.         else
  207.             err=2
  208.         fi
  209.     else
  210.         err=1
  211.     fi
  212.    
  213.     createCustomerErrorMsg $err
  214.     if [ $? != 0 ]; then
  215.         $__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
  216.         if [ $? == 0 ]; then
  217.             createCustomerInputMenu
  218.         else
  219.             # cleanup
  220.             cleanup 2
  221.         fi
  222.     fi
  223. }
  224.  
  225. createCustomerErrorMsg() {
  226.     case "$1" in
  227.         0) return 0
  228.              ;;
  229.         1) errormsg="Customer directory doesn't exist."
  230.              ;;
  231.         2) errormsg="Customer shockwatch.ear directory doesn't exist."
  232.              ;;
  233.         3) errormsg="Customer jasperfiles.war directory doesn't exist."
  234.              ;;
  235.         4) errormsg="Customer application.xml doesn't exist."
  236.              ;;
  237.         5) errormsg="Customer startup script doesn't exist."
  238.              ;;
  239.         *) errormsg="Unknown error!"
  240.              ;;
  241.     esac
  242.    
  243.     return $1
  244.  
  245. }
  246.  
  247. # Verifies that the customer database exists
  248. # Returns 0 on success; Non-zero on failure
  249. verifyDatabase() {
  250.     mysql -u$__dbuser -p$__dbpass $1 -e 'quit' 2> mysql.error
  251.     if [ $? != 0 ]; then
  252.         $__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
  253.         if [ $? == 0 ]; then
  254.             createDatabaseInputMenu
  255.         else
  256.             # cleanup
  257.             cleanup 2
  258.         fi
  259.     fi
  260.     return $?
  261. }
  262.  
  263. createDatabaseInputMenu() {
  264.     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)")
  265.     if [ $ret == "1" ]; then
  266.         createDatabaseInputBox
  267.         return $?
  268.     elif [ $ret == "2" ]; then
  269.         createDatabaseSelectList
  270.         return $?
  271.     else
  272.         cleanup 0
  273.     fi
  274. }
  275.  
  276. getDatabasesAsArray() {
  277.     count=0
  278.     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
  279.         databases[$count]=$i
  280.         count=$(($count + 1))
  281.     done
  282.  
  283.     # Verify that the databases array isn't empty.
  284.     if [ $count == 0 ]; then
  285.         return 1
  286.     else
  287.         return 0
  288.     fi
  289. }
  290.  
  291. createDatabaseSelectList() {
  292.     getDatabasesAsArray
  293.     if [ $? != 0 ]; then
  294.         if [ $__debug != 0 ]; then
  295.             echo "No databases..." >> $__debuglog
  296.         fi
  297.         return 1;
  298.     fi
  299.    
  300.     ret=$($__dialog --stdout --backtitle "$__appname" --radiolist "Select database:" $__boxheight $__boxwidth 6 `(
  301.     count=1
  302.     for i in "${databases[@]}"
  303.     do
  304.         echo -ne "$count $i "
  305.         if [ $count == 1 ]; then
  306.             echo "on"
  307.         else
  308.             echo "off"
  309.         fi
  310.         count=$(($count + 1))
  311.     done
  312.     )`)
  313.    
  314.     if [ $ret != 0 ]; then
  315.         database=${databases[$(($ret - 1))]}
  316.         verifyDatabase $database
  317.         return $?
  318.     else
  319.         # Think about how I want to handle this
  320.         cleanup 0
  321.     fi
  322. }
  323.  
  324. # Loads all directory names in $__jbosspath/server into an array
  325. # template directories are ignored
  326. # Return 0 on sucess; Non-zero on failure
  327. getCustomersAsArray() {
  328.     count=0
  329.     for i in `ls $__jbosspath/server/ | grep -v 'template'`
  330.     do
  331.         customers[$count]=$i
  332.         count=$(($count + 1))
  333.     done
  334.    
  335.     if [ $count != 0 ]; then
  336.         return 0
  337.     else
  338.         return 1
  339.     fi     
  340. }
  341.  
  342. # Creates a radio list from the array of customers
  343. createCustomerSelectList() {
  344.     getCustomersAsArray
  345.     if [ $? != 0 ]; then
  346.         echo "No customers..."
  347.         return 1
  348.     fi
  349.    
  350.     ret=$($__dialog --stdout --backtitle "$__appname" --radiolist "Select customer:" $__boxheight $__boxwidth 6 `(
  351.     count=1
  352.     for i in "${customers[@]}"
  353.     do
  354.         echo -ne "$count $i "
  355.         if [ $count == 1 ]; then
  356.             echo "on"
  357.         else
  358.             echo "off"
  359.         fi
  360.         count=$(($count + 1))
  361.     done
  362.     )`)
  363.    
  364.    
  365.     if [ $ret != 0 ]; then
  366.         customer=${customers[$(($ret - 1))]}
  367.         verifyCustomer $customer
  368.         return $?
  369.     else
  370.         # TODO maybe prompt a menu?
  371.         cleanup 0
  372.     fi
  373. }
  374.  
  375. createCustomerInputMenu() {
  376.     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")
  377.     if [ $ret == 1 ]; then
  378.         createCustomerInputBox
  379.         return $?
  380.     elif [ $ret == 2 ]; then
  381.         createCustomerSelectList
  382.         return $?
  383.     else
  384.         cleanup 0
  385.     fi
  386. }
  387.  
  388.  
  389. # Creates an input box for manually entering a database
  390. # The database is verified upon entering. If the database doesn't exist
  391. # then the user is given the option of trying again or exiting the app.
  392. # Return 0 on success. The app exits or returns to the menu on failure.
  393. createDatabaseInputBox() {
  394.     database=$($__dialog --stdout --backtitle "$__appname" --title "Input" --inputbox "Enter database name." $__boxheight $__boxwidth)
  395.     verifyDatabase $database
  396.     return $?
  397. }
  398.  
  399. createCustomerInputBox() {
  400.     customer=$($__dialog --stdout --backtitle "$__appname" --title "Input" --inputbox "Enter customer name." $__boxheight $__boxwidth)
  401.     verifyCustomer $customer
  402.     return $?
  403. }
  404.  
  405. main() {
  406.     if [ $# == 1 ]; then
  407.         # Assume $1 to be customer name, verify it exists and prompt version
  408.         # Test customer name as db, if fail, prompt
  409.         echo $1
  410.     elif [ $# == 2 ]; then
  411.         # Assume $1 to be customer and $2 to be version, verify both
  412.         # Test customer name as db, if fail, prompt
  413.         echo $1 $2
  414.     elif [ $# == 3 ]; then
  415.         # Assume $1 to be customer, $2 to be database, $3 to be version
  416.         # Test customer name as db, if fail, prompt
  417.         echo $1 $2 $3
  418.     else
  419.         # If 0 or > 3 parameters are specified then prompt for guided mode
  420.         $__dialog --backtitle "$__appname" --title "Confirmation" --yesno "No customer was specified. Do you wish to enter guided mode?" $__boxheight $__boxwidth
  421.         if [ $? == 0 ]; then
  422.             # Enter guided mode
  423.             createCustomerInputMenu
  424.             if [ $? == 0 ]; then
  425.                 # yesno for customer as database here
  426.                 $__dialog --backtitle "$__appname" --title "Confirmation" --yesno "Do you wish to use '$customer' as the database?" $__boxheight $__boxwidth
  427.                 if [ $? == 0 ]; then
  428.                     verifyDatabase $customer
  429.                     if [ $? != 0 ]; then
  430.                         $__dialog --backtitle "$__appname" --title "Error" --yesno "The database, '$customer', doesn't exist. Do you want to try a different database?" $__boxheight $__boxwidth
  431.                         if [ $? == 0 ]; then
  432.                             createDatabaseInputMenu
  433.                         else
  434.                             cleanup 1
  435.                         fi
  436.                     else
  437.                         database=$customer
  438.                     fi
  439.                 else
  440.                     createDatabaseInputMenu
  441.                 fi
  442.  
  443.                 # By the time we are here, database should be set
  444.                 # Prompt for version number to upgrade to.
  445.             fi
  446.                
  447.                
  448.         else
  449.             # Exit
  450.             cleanup 20
  451.         fi
  452.     fi
  453.  
  454.     #createDatabaseInputMenu
  455. }
  456.  
  457. main $*
Add Comment
Please, Sign In to add comment