基于 Linux 的服务端环境自动部署_测男的博客-程序员宝宝_服务端 自动部署环境

技术标签: Linux  shell  测试  linux shell 运维 测试 部署  

无意中翻出了几年前写的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 [email protected]'%';
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

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/testman930/article/details/51279329

智能推荐

Java 的8种基本类型和3种引用类型_intelrain的博客-程序员宝宝_arraylist是引用类型吗?

1、计算机的内存单位 计算机中的数据都是以 0 和 1 来表示的,其中一个 0 或者一个 1 称之为一位 (bit)。 8位称为一个字节 (Byte),两个字节称为一个字 (Word) ,四个字节称为双字 (Dword)。 1Byte=8bit , 1KB= 1024 Byte, 1M=1024 KB。2、Java 有四类8种基本类型 a、4...

大数据开发的四个维度_IT时代周刊的博客-程序员宝宝_大数据的维度

大数据词已经无处不在,然而,其概念仍然存在混淆。大数据已被用于承载所有类型的概念,包括:巨量的数据、社交媒体分析、下一代数据管理能力、实时数据等。无论是任何种类,企业都已经开始理解并且探索如何以新的方式处理并分析大量的信息。这样,数量较少但不断增加的先驱者实现了突破性的业务成果。在对大数据的混淆中,很大一部分从大数据的定义开始。为了了解我们的调研受访者对该术语的定义,我们让每个受访者选出大数据的两...

读写文件并填写行号_读写文本文件并添加行号_weixin_45881103的博客-程序员宝宝

适 用 专 业适用于所有专业。实 验 目 的(1)熟练掌握内置函数open()的用法。(2)熟练运用内置函数len()、max()、enumerate()。(3)熟练运用字符串的strip()、just()和其他方法。(4)熟练运用列表推导式。实 验 内 容编写一个程序demo.py,要求运行该程序后,生成demo_new.py文件,其中内容与demo.py一样,只是在每一行的后面加上行号。要求行号以#开始,并且所有行的#垂直对齐。代码filename = 'demo.py'with

HTTP状态500 - java.lang.ClassNotFoundException:org.apache.jsp.index_jsp_追0逐的博客-程序员宝宝

HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.index_jsptypeException reportmessagejava.lang.ClassNotFoundException: org.apache.jsp.index_jspdescriptionThe server encounte...

router vue 动态改变url_vue: router-link 动态路由_weixin_39713317的博客-程序员宝宝

动态路由:根据路由的不同请求不同的数据。$router 获取vue-router实例$route 获取url的详细信息:id表示在user后任意参数都可以访问到对应组件,但是必须有" / "。如: localhost:8080/user/1// router.js文件import Vue from 'vue'import Router from 'vue-router'import Home fr...

Android -- GreenDao3.2的简单使用_weixin_34248487的博客-程序员宝宝

1,最近看了一篇文章关于GreenDao的文章 ,感觉使用这个操作数据库还是很好用的,帮我们省了不少的查询代码,今天就和大家一起来简单的使用一下吧。首先这是官网地址:https://github.com/greenrobot/greendao,我们来按照文档一点点的来写一下2,首先要认识一下GreenDao是使用ORM(Object RelationShop Mapping)对象关系映射,就是...

随便推点

ROS Unable to contact my own server at [http://eric-ms-7a54:42579/]_农民小飞侠的博客-程序员宝宝_42579、at

今天在使用ros环境的时候出现了下面的问题:➜ ~ roscore ... logging to /home/eric/.ros/log/4f4e2e38-d093-11eb-85c4-0242323584ca/roslaunch-eric-ms-7a54-2130.logChecking log directory for disk usage. This may take awhile.Press Ctrl-C to interruptDone checking log file

Oracle Scheduler中的repeat_interval_Demonson的博客-程序员宝宝_oracle scheduler repeat_interval

Oracle 11g版本中引入了Scheduler(调度)来取代之前版本的JOB(任务)。这里简单介绍一下Scheduler中repeat_interval参数的含义和使用方法。repeat_interval从字面意思来说就是重复间隔。是指用户定义间隔多长时间执行指定的任务。如果不指定该参数,则任务只执行一次。repeat_interval语法如下:repeat_interval =..._1671465600

Android 将App的内容延伸到状态栏/导航栏_Amazing_Alex的博客-程序员宝宝_android把内容放到状态栏

来自我的程序员宝宝:http://blog.csdn.net/dahaohan/article/details/52175190看过Android的桌面应用都是介样的:如何让自己的应用也达到这般效果呢?这里就介绍几种常用的方法以及它们之间的区别。首先展示下此次demo的布局和初始状态:xml version="1.0" encoding="utf-8"?>Rel

PTA_Python_第6章-8 *输出全排列(选作) (20分)_dgyzmyx_的博客-程序员宝宝

输入整数n(3&lt;=n&lt;=7),编写程序输出1,2,...,n整数的全排列,按字典序输出。输入格式:一行输入正整数n。输出格式:按字典序输出1到n的全排列。每种排列占一行,数字间无空格。输入样例:在这里给出一组输入。例如:3输出样例:在这里给出相应的输出。例如:123132213231312321代码:def f(num):#计算num之前所有数的阶...

ssh的密码登录和公钥登录_程序员八一的博客-程序员宝宝_ssh 密码

一.简述sshssh是加密的远程登录和远程执行命令的工具,它不仅在密码进行加密,对登录后执行的命令也进行了加密,所以比较安全,在这之前使用的是telent和R系列的命令,这些是明文传输,很不安全.一般的linux发行版本都带了ssh,不要手动安装.二.密码登录1.工作原理1.客户端向服务端发起登录请求,服务端返回公钥2.客户端用公钥加密自己的认证信息,发送到服务端3.服务端用私钥解密客户端发来的认证信息,匹配则登录成功,否则失败2.登录命令ssh 用户名@主机ip或者hostname

c++(基础)————模板(函数模板与类模板)_djh971102的博客-程序员宝宝

一、模板是干什么的?比如实现一个简单的比较大小的函数,如果要比较int、double、char等多种类型的数据,那么有多少中数据类型,我们就得实现多少个比较函数,然而这些函数除了类型不同之外,其他代码都是一样的(也就是代码实现的功能都相同,只是类型不同)。所以为了解决这种问题,我们就引入了模板,模板就是为了代码重用而生的二、函数模板:函数模板提供了一种函数行为,该函数行为可以用多种不同的类型...

推荐文章

热门文章

相关标签