通用技术 基于 Linux 的服务端环境自动部署

扫地僧 · 2016年03月29日 · 最后由 oly 回复于 2018年04月13日 · 2868 次阅读

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

需求背景

  • 自动识别当前系统,实行相应的环境变量配置方案
  • 支持 mysql(二进制包)、tomcat(二进制包)、jdk(rpm 包)等基础环境的全新安装、覆盖安装、卸载
  • tomcat 支持配置堆内存和永久保留区大小
#!/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

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

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 8 条回复 时间 点赞

赞赞赞!

轻量级的为啥不用 capistrino 或者 mina?
重量级的可以用 chef

脚本功底不错. 我们公司目前用的是 docker. 根据代码的 commit 直接部署环境. 测试团队没维护环境的负担了.

#2 楼 @lihuazhang 谢谢已收下!
13 年也查过有木有这方面的工具,可惜没查到你推荐的,而且需求也不单单是 web 应用部署

#3 楼 @seveniruby 每个公司情况不同吧,我现在的公司测试也不用维护了,docker 也是我学习的一个方向

#4 楼 @quqing 他们不止是 web 应用。 是属于运维工具。

开发是怎么把代码更新到镜像里面的?你们的实现方向是什么..麻烦给点思路..
网上看到实现的思路:开发添加 docker maven 插件,comnmit 推送到本地的 docker 仓库,然后测试或线上直接 pull 本地仓库镜像?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册