无意中翻出了几年前写的 shell 脚本。当时,测试环境有好几套,而且每套环境又是分布式 + 负载均衡 + 热备,全部由测试部维护和管理的。
当时,测试部就几个人,手动部署耗费人力不说,且经常出错。作为部门老大,亲力亲为的实现了测试环境自动化部署解决方案,这些脚本不但让测试部受益,让奋斗在前线的技术支持也成为受益者。
由于时间久远,开发和调试都是基于当时的系统版本完成的。
考虑到公司隐私,本文只开放部分基础环境的自动安装。

需求背景

#!/bin/sh
#create by quqing 2013.3.12

jdk_path=/usr/local/jdk
tomcat_path=/usr/local/tomcat
weblogic_path=/usr/local/weblogic
mysql_path=/usr/local/mysql
bakup_path=/usr/local/bakup

function generateEnvRedHat()
{
 if [[ -f $bakup_path/.bash_profile || -f  ~/.bash_profile.bak ]]; then
     echo -e "\033[1;32mEnvironment Variable has already been generated,Please check $bakup_path/.bash_profile or ~/.bash_profile.bak,maybe has exist!"
     sleep 1
 else
     cp ~/.bash_profile $bakup_path/.bash_profile >/dev/null 2>&1
     mv ~/.bash_profile ~/.bash_profile.bak >/dev/null 2>&1

     sed -i '/# User specific environment and startup programs/a export PATH=$JAVA_HOME/bin:$PATH:$MW_HOME:$CATALINA_HOME/bin:$MYSQL_HOME/bin' $bakup_path/.bash_profile
     sed -i '/# User specific environment and startup programs/a export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/jre/lib/rt.jar' $bakup_path/.bash_profile
     sed -i "/# User specific environment and startup programs/a export CATALINA_HOME=$tomcat_path" $bakup_path/.bash_profile
     sed -i "/# User specific environment and startup programs/a export MYSQL_HOME=$mysql_path" $bakup_path/.bash_profile
     sed -i "/# User specific environment and startup programs/a export MW_HOME=$weblogic_path" $bakup_path/.bash_profile
     sed -i "/# User specific environment and startup programs/a export JAVA_HOME=$jdk_path" $bakup_path/.bash_profile
     sed -i '/# User specific environment and startup programs/a \\t' $bakup_path/.bash_profile

     cp $bakup_path/.bash_profile  ~
     source ~/.bash_profile
 fi
}

function generateEnvSUSE()
{
 if [[ -f $bakup_path/profile || -f  /etc/profile.bak ]]; then
     echo -e "\033[1;32mEnvironment Variable has already been generated,Please check $bakup_path/profile or /etc/profile.bak,maybe has exist!"
     sleep 1
 else
     cp /etc/profile $bakup_path/profile >/dev/null 2>&1
     mv /etc/profile /etc/profile.bak >/dev/null 2>&1

     sed -i '/# End of \/etc\/profile/i \\t' $bakup_path/profile
     sed -i "/# End of \/etc\/profile/i export JAVA_HOME=$jdk_path" $bakup_path/profile
     sed -i "/# End of \/etc\/profile/i export MW_HOME=$weblogic_path" $bakup_path/profile
     sed -i "/# End of \/etc\/profile/i export MYSQL_HOME=$mysql_path" $bakup_path/profile
     sed -i "/# End of \/etc\/profile/i export CATALINA_HOME=$tomcat_path" $bakup_path/profile
     sed -i '/# End of \/etc\/profile/i export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/jre/lib/rt.jar' $bakup_path/profile
     sed -i '/# End of \/etc\/profile/i export PATH=$JAVA_HOME/bin:$PATH:$MW_HOME:$CATALINA_HOME/bin:$MYSQL_HOME/bin' $bakup_path/profile
     sed -i '/# End of \/etc\/profile/i \\t' $bakup_path/profile

     cp $bakup_path/profile /etc/profile
     source /etc/profile
 fi
}

function Init()
{
 if [ -d $bakup_path ]; then
    sleep 0.01
 else
    mkdir $bakup_path >/dev/null 2>&1
 fi

 OS=`lsb_release -a | grep Distributor | cut -d ":" -f2`
 case $OS in
     *RedHatEnterprise*|*CentOS*)
            generateEnvRedHat
            ;;
     *SUSE*)
            generateEnvSUSE
            ;;
     *)
            echo -e "\033[1;31mUnknown OS!\033[0m "
            exit
            ;;
 esac
}

function Uninstall()
{
 echo -ne "\033[1;31mUninstall All services(y or n):"
 read YES 
 if [ "$YES" = "y" ]; then
     UninstallMYSQL
     UninstallWEBLOGIC
     UninstallTOMCAT
     UninstallJDK
     echo ""
     echo "Uninstall success!"
 else
     echo -e "\033[1;32mInstall exit \\033[0m"
     sleep 1
     exit
 fi
}

function UninstallJDK()
{
 if [ -d $jdk_path ]; then
    rm -rf $bakup_path/jdk.`date +%F` >/dev/null 2>&1
    mv $jdk_path $bakup_path/jdk.`date +%F` >/dev/null 2>&1
 fi
}

function UninstallTOMCAT()
{
 if [ ! -s `pidof -s java` ]; then
    $tomcat_path/bin/shutdown.sh
    killall java
    sleep 1
    kill -9 `ps -A | grep java | cut -d " " -f1` >/dev/null 2>&1
 fi

 chkconfig --del tomcat >/dev/null 2>&1
 rm -f /etc/init.d/tomcat >/dev/null 2>&1

 if [ -d $tomcat_path ]; then
    mv $tomcat_path/conf/server.xml $bakup_path/server.xml.bak >/dev/null 2>&1
    rm -rf $bakup_path/tomcat_`date +%F` >/dev/null 2>&1
    mv $tomcat_path $bakup_path/tomcat_`date +%F` >/dev/null 2>&1
 fi
}

function UninstallWEBLOGIC()
{
 if [ ! -s `pidof -s java` ]; then
    killall java
    kill -9 `ps -A | grep java | cut -d " " -f1` >/dev/null 2>&1
 fi

 if [ -d $weblogic_path ]; then
    rm -rf $bakup_path/weblogic_`date +%F` >/dev/null 2>&1
    mv $weblogic_path $bakup_path/weblogic_`date +%F` >/dev/null 2>&1
 fi
}

function UninstallMYSQL()
{
 if [ ! -s `pidof -s mysqld` ]; then
    service mysql stop
    kill -9 `ps -A | grep mysqld | cut -d " " -f1` >/dev/null 2>&1
 fi

 chkconfig --del mysql >/dev/null 2>&1
 rm -f /etc/init.d/*mysql* >/dev/null 2>&1
 mv -f /etc/my.cnf $bakup_path/my.cnf_`date +%F` >/dev/null 2>&1

 if [ -d $mysql_path ]; then
    rm -rf $bakup_path/data_`date +%F` >/dev/null 2>&1
    mv $mysql_path/data $bakup_path/data_`date +%F` >/dev/null 2>&1
    rm -rf $bakup_path/mysql_`date +%F` >/dev/null 2>&1
    mv $mysql_path $bakup_path/mysql_`date +%F` >/dev/null 2>&1
 fi
}

function progress()
{
 i=0
 while [ $i -lt 100000000 ]
 do
   for j in '-' '\\' '|' '/'
   do
     echo -ne "\033[1D$j"
     usleep 50000
   done
   ((i++))
   if [ -s `pidof -s tar` ]; then
      echo -e "\033[1DOK!\033[0m"
      break
   fi
 done
}

function jdk()
{
 echo ""
 echo -e "\033[1;34m         Installing JDK ......\\033[0m"

 Init

 if [ -d $jdk_path ]; then
     echo -ne "\033[1;32mJDK is already exist,do you want reinstall?(y or n):"
     read YES
     if [ "$YES" = "y" ]; then
         UninstallJDK
         echo ""
         echo -e "\033[1;32mUNZIP JDK files, please wait    "
         sh `ls package/jdk*`
         mv `ls -d jdk*_*` $jdk_path
     else
         echo -e "\033[1;32mExit install JDK \\033[0m"
         sleep 0.5
     fi
 else
     echo ""
     echo -e "\033[1;32mUNZIP JDK files, please wait    "
     sh `ls package/jdk*`
     mv `ls -d jdk*_*` $jdk_path
 fi
}

function mysql()
{
 echo ""
 echo -e "\033[1;34m         Installing Mysql Server ......\\033[0m"

 Init
 UninstallMYSQL

 echo ""
 echo -ne "\033[1;32mUNZIP MYSQL files, please wait    "
 tar xzf `ls package/mysql*.tar.gz`
 progress

 mv `ls -d mysql-*` $mysql_path
 groupadd mysql >/dev/null 2>&1
 useradd -g mysql mysql >/dev/null 2>&1
 chown -R mysql.mysql $mysql_path
 cd $mysql_path
 ./scripts/mysql_install_db --user=mysql
 cd -
 cp $mysql_path/support-files/mysql.server /etc/init.d/mysql
 cp $mysql_path/support-files/my-huge.cnf /etc/my.cnf
 sed -i '/^\[mysqld\]/a skip-name-resolve' /etc/my.cnf
 sed -i '/^\[mysqld\]/a character_set_server = utf8' /etc/my.cnf
 sed -i '/^\[mysqld\]/a default-storage-engine = INNODB' /etc/my.cnf
 sed -i '/^\[client\]/a default-character-set = utf8' /etc/my.cnf
 echo -ne "\033[1;32mIs case sensitive?(y or n):"
 read YES
 if [ "$YES" = "y" ]; then
     sed -i '/^\[mysqld\]/a lower_case_table_names = 0' /etc/my.cnf
 else
     sed -i '/^\[mysqld\]/a lower_case_table_names = 1' /etc/my.cnf
 fi
 chmod 755 /etc/init.d/mysql
 chkconfig --add mysql
 chkconfig --level 3 mysql on
 service mysql start

 while [ "$select" != "n" ]
    do
      echo -e "\033[1;32mPlease modify new password"
      $mysql_path/bin/mysqladmin -uroot password
      #mysqladmin -u root -p password
      echo -ne "\033[1;32mIf your operation failed,Please input 'y',If your operation successful,Please input 'n'(y/n):"
      read select
 done

 service mysql restart

 while [ "$input" != "n" ]
    do
      echo -e "\033[1;32mNow grant privileges,please input password"
$mysql_path/bin/mysql -uroot -p << EOF
UPDATE mysql.user SET Host='%' WHERE Host='localhost';
GRANT ALL PRIVILEGES ON *.* TO root@'%';
FLUSH PRIVILEGES;
EOF
      echo -ne "\033[1;32mIf your operation failed,Please input 'y',If your operation successful,Please input 'n'(y/n):"
      read input
 done

 service mysql restart

 echo -e "\033[1;32mInstall MySql Complete! \\033[0m"
}

function tomcat()
{
 echo ""
 echo -e "\033[1;34m         Installing Apache Tomcat Server ......\\033[0m"

 Init
 UninstallTOMCAT
 jdk

 cp `ls package/tomcat` /etc/init.d/
 chmod 755 /etc/init.d/tomcat

 echo ""
 echo -e "\033[1;32mUNZIP tomcat files, please wait     "
 tar xzf `ls package/apache-tomcat*.tar.gz`
 progress

 mv `ls -d apache-tomcat-*` $tomcat_path
 chkconfig --add tomcat
 chkconfig --level 3 tomcat on

 echo -e "\033[1;32mNow config the memory for JAVA Virtual Machine"
 size='m'
 while [ "$select" != "n" ]
    do
      echo -ne "\033[1;32mPlease set HeapSize(-Xms):"
      read xms
      echo -ne "\033[1;32mPlease set HeapSize(-Xmx):"
      read xmx
      echo -ne "\033[1;32mPlease set PermSize(-XX:PermSize):"
      read PermSize
      echo -ne "\033[1;32mPlease set MaxPermSize(-XX:MaxPermSize):"
      read MaxPermSize
      echo -ne "\033[1;32mIf you want reset,Please input 'y',If not please input 'n'(y/n):"
      read select
 done

 sed -i "/^#!\/bin\/sh/a export JAVA_OPTS=\"-Xms$xms$size -Xmx$xmx$size -XX:PermSize=$PermSize$size -XX:MaxPermSize=$MaxPermSize$size\"" $tomcat_path/bin/catalina.sh

 if [ -f $bakup_path/server.xml.bak ]; then
    echo -ne "\033[1;32mDo you want use the bakfile(server.xml.bak)?(y/n):"
    read ifuse
    if [ "$ifuse" = "y" ]; then
       mv -f $bakup_path/server.xml.bak $tomcat_path/conf/server.xml
    else
       sleep 0.01
    fi
 fi

 echo -ne "\033[1;31mDo you want to start tomcat service now?(y or n):"
 read ifstart
 if [ "$ifstart" = "y" ]; then
     $tomcat_path/bin/startup.sh
 else
     echo -e "\033[1;32mInstall tomcat complete! \\033[0m"
     sleep 1
 fi
}

function weblogic()
{
 echo ""
 echo -e "\033[1;34m         Installing WebLogic Server ......\\033[0m"

 Init
 UninstallWEBLOGIC
 jdk

 mkdir $weblogic_path
 cd $weblogic_path
 echo -ne "\033[1;32mUNZIP weblogic files, please wait            "
 unzip `cd -`/package/wls1034_dev.zip >/dev/null 2>&1
 cd -

 sh $weblogic_path/configure.sh
 sh $weblogic_path/wlserver/server/bin/setWLSEnv.sh
 echo -e "\033[1;32mCreate WebLogic Domain Now"
 sh $weblogic_path/wlserver/common/bin/config.sh
}

Flag=1
while [ "$Flag" -eq "1" ]
do
    clear
    echo ""
    echo ""
    echo -e "                           \033[1;34m Please Select Component Install \\033[0m"
    echo ""
    echo -e "                      \033[1;32m 1. Install Apache Tomcat on this server \\033[0m"
    echo ""
    echo -e "                      \033[1;32m 2. Install WEBLOGIC on this server \\033[0m"
    echo ""
    echo -e "                      \033[1;32m 3. Install MySql on this server \\033[0m"
    echo ""
    echo -e "                      \033[1;32m 4. Install JDK on this server \\033[0m"
    echo ""
        echo -e "                      \033[1;32m 5. Uninstall all services \\033[0m"
        echo ""
    echo -e "                      \033[1;32m 6. Exit \\033[0m"
    echo ""
    echo -ne "\033[1;32m Please input your choice:"
    read selection
    case $selection in
    "1")
        tomcat
        ;;
    "2")
        weblogic
        ;;
    "3")
        mysql
        ;;
    "4")
        jdk
        ;;
    "5")
        Uninstall
        ;;
    "6")
        exit
        ;;
    *)
        clear
        echo ""
        echo ""
        echo -e "\033[1;31m    Input Error!\033[0m "
            sleep 3 
        ;;
    esac
done

友情提示,本文系原创,转载请注明出处


↙↙↙阅读原文可查看相关链接,并与作者交流