Friday, April 27, 2012

A New Home

The Argo Database web application is getting a new home at argoweb.whoi.edu! This post documents the changes made to Argoweb.

Changes to Argoweb

Installation of MySQL Database and Apache Tomcat was accomplished using a custom bash shell script to download, unzip, compile the applications from source. The following changes were made to argoweb:
  • added groups 'argoweb', 'mysql', and 'tomcat'
  • added users 'argoweb', 'mysql', and 'tomcat'
  • removed:
    • apache2 (replaced by Tomcat)
  • installed:
    • vim 
    • openjdk 6
    • g++ 4.4
    • MySQL 5.1
    • Apache Tomcat 6 Apache Tomcat 7 
      • Note: Tomcat 6 does not support the latest version of Java. Tomcat 7, however, is not natively supported by Debian so it must be installed from source.
  • used iptables to route incoming traffic on port 80 to the Tomcat server:

root@argoweb:/etc/tomcat6# iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
root@argoweb:/etc/tomcat6# iptables-save
# Generated by iptables-save v1.4.8 on Thu May 3 05:31:30 2012
*nat
:PREROUTING ACCEPT [53:7910]
:POSTROUTING ACCEPT [2:191]
:OUTPUT ACCEPT [2:191]
-A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
COMMIT
# Completed on Thu May 3 05:31:30 2012

  • performed dump of previous database :
mysqldump -u root -p --databases floats > argoweb_dump.sql
  • used sql file to populate Argoweb database
  • deployed the Argo Database application on the Tomcat server
  • created a script to mirror some directories from Ursa to ArgoWeb (/usr/local/bin/ursa-volume-sync):
    • ursa:/Volumes/U1/argo -> argoweb:/data/Volumes/U1/argo
    • ursa:/Volumes/U2 -> argoweb:/data/Volumes/U2
    • cron'd to run twice daily. After the initial transfer, this script has completed in under 10 minutes, without any major changes to files on Ursa. After an update to the GDAC, it may take significantly longer.
The following script was used to install MySQL database and Apache Tomcat from source:



  1. #!/bin/bash
  2. ##########################################
  3. # MYSQL AND APACHE TOMCAT INSTALL SCRIPT #
  4. ##########################################
  5. #
  6. # AUTHOR
  7. #   Alex Ekholm, aekholm@whoi.edu, Woods Hole Oceanographic Institution
  8. #
  9. # CREATED
  10. #   Fri Apr 27, 2012
  11. #
  12. # NOTES
  13. #   - This script can be used to automate installation of the following applications from source:
  14. #       1) MySQL 5.1.62
  15. #       2) Apache Tomcat 7.0.27
  16. #   - Will attempt to download source code automatically if not present.
  17. #
  18. # REQUIREMENTS
  19. #   - Must be run as root user
  20. #   - wget, getopts, gcc, g++, make, ant
  21. #
  22. #
  23. ##########################################
  24. # CONFIGURATION              #
  25. ##########################################
  26. #
  27. # Relevent files, directories, and other misc. install options are specified here.
  28. #
  29. # MySQL configuration
  30. mysql_home="/home/mysql"
  31. mysql_prefix="/usr/share/mysql"
  32. mysql_dir="$mysql_prefix/mysql-5.1.62"
  33. mysql_tar="$mysql_dir.tar.gz"
  34. mysql_config_opts="--prefix=$mysql_prefix"
  35. # Apache Tomcat configuration
  36. tomcat_home="/home/tomcat"
  37. tomcat_dir="./tomcat/apache-tomcat-7.0.27-src"
  38. tomcat_tar="$tomcat_dir.tar.gz"
  39. tomcat_prefix="/usr/share/tomcat7"
  40. tomcat_config_opts="base.path=$tomcat_prefix"
  41. # tar options
  42. TAROPTS="-xzf"
  43. # debug - set DEBUG=1 to display debug info
  44. _DEBUG=0
  45. # verbose operation
  46. VERBOSE=$_DEBUG
  47. # working dir - default is current dir
  48. src_dir="/usr/local/src"
  49. # mirrors
  50. mysql_mirrors=(
  51.     "http://mysql.he.net/Downloads/MySQL-5.1/mysql-5.1.62.tar.gz"
  52.     "http://mysql.mirrors.hoobly.com/Downloads/MySQL-5.1/mysql-5.1.62.tar.gz"
  53.     "http://mirror.services.wisc.edu/mysql/Downloads/MySQL-5.1/mysql-5.1.62.tar.gz")
  54. tomcat_mirrors=(
  55.     "http://www.fightrice.com/mirrors/apache/tomcat/tomcat-7/v7.0.27/src/apache-tomcat-7.0.27-src.tar.gz"
  56.     "http://mirrors.ibiblio.org/apache/tomcat/tomcat-7/v7.0.27/src/apache-tomcat-7.0.27-src.tar.gz"
  57.     "http://mirror.candidhosting.com/pub/apache/tomcat/tomcat-7/v7.0.27/src/apache-tomcat-7.0.27-src.tar.gz")
  58. ##########################################
  59. # INSTALL MYSQL DATABASE         #
  60. ##########################################
  61. function install_mysql {
  62.     # check directory
  63.     if [ ! -d "mysql" ]; then
  64.         mkdir mysql
  65.     fi
  66.    
  67.     # download source if necessary
  68.     ask "Download MySQL source?" Y && if [ ! -f "$mysql_tar" ]; then
  69.         # try each mirror until success
  70.         for mirror in "${mysql_mirrors[@]}"; do
  71.             # download source
  72.             WGETOPTS="-P mysql $mirror"
  73.             dbg "executing wget: 'wget $WGETOPTS'"
  74.             wget $VOPT $WGETOPTS
  75.            
  76.             if [ -f "$mysql_tar" ]; then break; fi
  77.         done
  78.        
  79.         # check
  80.         if [ ! -f "$mysql_tar" ]; then
  81.             echo "Failed to download MySQL source: $mysql_tar"
  82.             exit 1
  83.         fi
  84.     fi
  85.     # create group
  86.     ask "Create group 'mysql'?" Y && groupadd mysql
  87.     # create user
  88.     ask "Create user 'mysql'?" Y && useradd -b $mysql_home \
  89.         -d $mysql_home \
  90.         -g mysql \
  91.         mysql
  92.     # unzip
  93.     ask "Unzip MySQL database?" Y && tar $TAROPTS $mysql_tar -C mysql
  94.    
  95.     # change to mysql dir
  96.     chdir $mysql_dir
  97.     # configure
  98.     verbose "MySQL Configuration options: '$mysql_config_opts'"
  99.     ask "Configure MySQL?" Y && ./configure $mysql_config_opts
  100.        
  101.     # compile
  102.     ask "Make clean?" Y && make clean
  103.     ask "Compile MySQL?" Y && make
  104.     ask "Compile MySQL installation modules?" Y && make install
  105.     # postinstallation setup
  106.     chdir $mysql_prefix
  107.     chown -R mysql .
  108.     chgrp -R mysql .
  109.     ask "Install MySQL?" Y && ./bin/mysql_install_db --user=mysql --basedir=$mysql_prefix
  110.     chown -R root .
  111.     chown -R mysql var
  112.     # optional 
  113.     ask "Copy support files?" N && cp support-files/my-medium.cnf /etc/my.cnf
  114.     # start mysql daemon
  115.     if ask "Start MySQL daemon?" Y ; then
  116.         ./bin/mysqld_safe --user=mysql --basedir=$mysql_prefix &
  117.     fi
  118.     # return to root dir
  119.     chdir $src_dir
  120. } # install_mysql
  121. ##########################################
  122. # INSTALL APACHE TOMCAT          #
  123. ##########################################
  124. function install_tomcat {
  125.     # check directory
  126.     if [ ! -d "tomcat" ]; then
  127.         mkdir tomcat
  128.     fi
  129.    
  130.     # download source if necessary
  131.     ask "Download Apache Tomcat source?" Y && if [ ! -f "$tomcat_tar" ]; then
  132.         # try each mirror until success
  133.         for mirror in "${tomcat_mirrors[@]}"; do
  134.             # download source
  135.             WGETOPTS="$VOPT -P tomcat $mirror"
  136.             dbg "wget $WGETOPTS"
  137.             wget $WGETOPTS
  138.            
  139.             if [ -f "$tomcat_tar" ]; then break; fi
  140.         done
  141.        
  142.         # check
  143.         if [ ! -f "$tomcat_tar" ]; then
  144.             echo "Failed to download Apache Tomcat source: $tomcat_tar"
  145.             exit 1
  146.         fi
  147.     fi
  148.     # create group
  149.     ask "Create group 'tomcat'?" Y && groupadd tomcat
  150.     # create user
  151.     ask "Create user 'tomcat'?" Y &&  useradd -b $tomcat_home \
  152.         -d $tomcat_home \
  153.         -g tomcat \
  154.         tomcat
  155.     # unzip
  156.     ask "Unzip Apache Tomcat Server?" Y && tar $TAROPTS $tomcat_tar -C tomcat
  157.     # change to tomcat dir
  158.     chdir $tomcat_dir
  159.     # configure tomcat
  160.     # cd echo "$tomcat_config_opts" > build.properties
  161.    
  162.     # compile tomcat
  163.     ask "Compile Apache Tomcat Server from source?" Y && ant
  164.     # create directories
  165.     if ask "Create directory structure?" Y ; then
  166.         # /var/lib
  167.         if [ ! -d /var/lib/tomcat7 ] ; then
  168.             mkdir -p /var/lib/tomcat7/common/classes \
  169.                  /var/lib/tomcat7/server/classes \
  170.                  /var/lib/tomcat7/shared/classes
  171.         fi
  172.        
  173.         # /var/log
  174.         if [ ! -d /var/log/tomcat7 ] ; then
  175.             mkdir /var/log/tomcat7
  176.         fi
  177.        
  178.         # /var/cache
  179.         if [ ! -d /var/cache/tomcat7 ] ; then
  180.             mkdir /var/cache/tomcat7
  181.         fi
  182.         # /etc - conf
  183.         if [ ! -d /etc/tomcat7 ] ; then
  184.             mkdir /etc/tomcat7
  185.         fi
  186.     fi
  187.    
  188.     # copy compiled files
  189.     if ask "Copy compiled files to install directory?" Y ; then
  190.         # /usr/share/
  191.         cp -r output/build/bin $tomcat_prefix/
  192.         cp -r output/build/lib $tomcat_prefix/
  193.        
  194.         # /etc
  195.         cp -r output/build/conf/* /etc/tomcat7
  196.        
  197.         # /var/lib
  198.         cp -r output/build/webapps /var/lib/tomcat7/webapps
  199.         ln -s /etc/tomcat7 /var/lib/tomcat7/conf
  200.         ln -s /var/log/tomcat7 /var/lib/tomcat7/logs
  201.         ln -s /var/cache/tomcat7 /var/lib/tomcat7/work
  202.        
  203.         # permissions
  204.         chown tomcat /var/log/tomcat7 -R
  205.         chgrp tomcat /var/log/tomcat7 -R
  206.         chown tomcat /var/lib/tomcat7 -R
  207.         chgrp tomcat /var/lib/tomcat7 -R
  208.         chown tomcat /var/cache/tomcat7 -R
  209.         chgrp tomcat /var/cache/tomcat7 -R
  210.         chown tomcat /etc/tomcat7 -R
  211.         chgrp tomcat /etc/tomcat7 -R
  212.         chown tomcat /usr/share/tomcat7 -R
  213.         chgrp tomcat /usr/share/tomcat7 -R
  214.     fi
  215.     # return
  216.     chdir $src_dir
  217. } # install_tomcat
  218. ##########################################
  219. # PROMPT FUNCTION            #
  220. ##########################################
  221. #
  222. # USAGE
  223. #   (long version)
  224. #       if [ ask "<prompt>" <optional-default> ]; then
  225. #           # Control will enter here if yes
  226. #       fi
  227. #
  228. #   (short version)
  229. #       ask "<prompt>" && <command-to-exec-if-yes>
  230. #       ask "<prompt>" <default> && <command-to-exec-if-yes>
  231. #       ask "<prompt>" <default> || <command-to-exec-if-no>
  232. #    
  233. # EXAMPLE
  234. #   ask "Install MySQL database?" Y && install_mysql
  235. #
  236. # RETURN VALUE
  237. #   0 if user responds YES
  238. #   1 if user responds NO
  239. #
  240. function ask {
  241.     while true; do
  242.     # Assign prompt and default
  243.         if [ "${2:-}" = "Y" ]; then
  244.             prompt="Y/n"
  245.             default=Y
  246.         elif [ "${2:-}" = "N" ]; then
  247.             prompt="y/N"
  248.             default=N
  249.         else
  250.             prompt="y/n"
  251.             default=
  252.         fi
  253.         # Ask the question
  254.         read -p "$1 [$prompt] " REPLY
  255.         # Default
  256.         if [ -z "$REPLY" ]; then
  257.             REPLY=$default
  258.         fi
  259.         # Return only if the reply is valid
  260.         case "$REPLY" in
  261.             Y*|y*) return 0 ;;
  262.             N*|n*) return 1 ;;
  263.         esac
  264.     done
  265. } # ask
  266. ##########################################
  267. # CHANGE DIRECTORY           #
  268. ##########################################
  269. function chdir {
  270.     verbose "Changing to directory: $@"
  271.     cd $@  
  272. } # err
  273. ##########################################
  274. # DEBUG FUNCTION             #
  275. ##########################################
  276. #
  277. # Like 'echo', but prints only if debug operation is specified.
  278. function dbg {
  279.     [ "$_DEBUG" == 1 ] && echo "[DEBUG] $@"
  280. } # dbg
  281. ##########################################
  282. # ERROR FUNCTION             #
  283. ##########################################
  284. #
  285. # Like 'echo', but prints to error stream
  286. function err {
  287.     echo "[ERROR] $@" 1>&2
  288. } # err
  289. ##########################################
  290. # VERBOSE FUNCTION           #
  291. ##########################################
  292. #
  293. # Like 'echo', but prints only if verbose operation is specified.
  294. function verbose {
  295.     [ "$VERBOSE" == 1 ] && echo "$@"
  296. } # verbose
  297. ##########################################
  298. # INSTALL ARGOWEB APPS           #
  299. ##########################################
  300. dbg "Running in debug mode..."
  301. # Make sure user is root
  302. if [ "$(id -u)" != "0" ]; then
  303.    err "This script must be run as root"
  304.    exit 1
  305. fi
  306. # Get command line arguments
  307. while getopts ":cd:v" opt; do
  308.     dbg "processing command-line argument: -$opt"
  309.     case $opt in
  310.         c)
  311.             # clean working directory.
  312.             rm -rf mysql tomcat
  313.                 ;;
  314.         d)
  315.             # Specify working directory. This is the directory to which all files will be downloaded and unzipped.         
  316.             src_dir=$OPTARG
  317.             chdir $src_dir
  318.                 ;;
  319.             v)
  320.             # Verbose operation
  321.                 VERBOSE=1
  322.             VOPT="-v"
  323.             TAROPTS="-xzvf"
  324.             verbose "Operating in verbose mode."
  325.                 ;;
  326.             \?)
  327.                 err "Invalid option: -$opt"
  328.             exit 1
  329.                 ;;
  330.         :)
  331.                 err "Option -$OPTARG requires an argument."
  332.                 exit 1
  333.                 ;;
  334.     esac
  335. done
  336. dbg "VERBOSE=$VERBOSE"
  337. dbg "src_dir=$src_dir"
  338. verbose "Working directory: $src_dir"
  339. chdir $src_dir
  340. # Prompt and install MySQL database
  341. ask "Install MySQL database?" Y && install_mysql
  342. # Prompt and install Apache Tomcat Server
  343. ask "Install Apache Tomcat Server?" Y && install_tomcat

No comments:

Post a Comment