shell脚本一键部署jdk,python,mysql,postgresql,es等组件

1、根据需求,快速部署一些常用的组件。

  1. 安装jdk,版本:1.8.0_131

  2. 安装python,版本:2.7.8

  3. 安装mysql,版本:5.6.39

  4. 安装neo4j,版本:3.1.4

  5. 安装postgresql,版本:9.6.3

  6. 安装node.js,版本:8.9.0

  7. 安装nginx,版本:1.14.0

  8. 安装elasticsearch,版本:5.2.2

  9. 安装es-head

#!/bin/sh
#存放二进制包的路径
SOFTWARE_PATH='/opt/software'
 
#java部分信息
JDK_VERSION='jdk1.8.0_131'                                #jdk的版本

#python部分信息
PYTHON_VERSION='python-2.7.8'                             #python的版本

#mysql安装包名
MYSQL_VERSION='mysql-5.6.39-linux-glibc2.12-x86_64'       #mysql的版本


#neo4j部分信息											  #neo4j的版本
NEO4J_VERSION='neo4j-community-3.1.4' 

#postgresql部分信息
POSTGRESQL_VERSION='postgresql-9.6.3'                     #postgresql的版本


#node.js部分信息,jdk要求1.8以上						  #node.js版本
NODE_VERSION='node-v4.8.4' 


#nginx部分信息
NGINX_VERSION='nginx-1.14.0'                              #nginx的版本


#ES部分信息
ES_VERSION='elasticsearch-5.2.2'						  #ES的版本

#es-head部分信息
ES_HEAD_VERSION='elasticsearch-head'

#获取当前脚本所在的ip
local_ip=`ifconfig -a |head -n 2|grep inet|awk '{print $2}'`

 
install_list=(
 1.安装jdk,版本:1.8.0_131
 2.安装python,版本:2.7.8
 3.安装mysql,版本:5.6.39
 4.安装neo4j,版本:3.1.4
 5.安装postgresql,版本:9.6.3
 6.安装node.js,版本:8.9.0
 7.安装nginx,版本:1.14.0
 8.安装elasticsearch,版本:5.2.2
 9.安装es-head
)



#1 步骤输出 紫色
#2 正确输出 绿色
#3 错误输出 红色
#4 提示输出 蓝色
#5 警告输出 黄色
#根据不同的颜色打印出提示信息
function echo_fun(){
  if [ $# -ge 2 ];then
      params_num=$1
      shift 1
      params_mes=$@
  else
      echo_fun 3 请至少输入两个参数 echo_fun ..
      exit
  fi
  case $params_num in
        1)
        echo -e "\n\033[35;40;1m ****************************** ${params_mes} ******************************\033[0m\r\n"
        ;;
        2)
        echo -e "\033[32;40;1m ${params_mes}\033[0m\r\n"
        ;;
        3)
        echo -e "\n\033[31;40;1m ${params_mes}\033[0m\r\n"
        ;;
        4)
        echo -e "\033[36;40;1m ${params_mes}\033[0m\r\n"
        ;;
        5)
        echo -e "\033[33;40;1m ${params_mes} \033[0m\r\n"
        ;;
        *)
        echo_fun 3 参数异常第一个参数应为1,2,3,4,5
        ;;
   esac
}


function echo_list(){
    echo_fun 5 安装目录如下,请选择安装步骤:
    for (( i=0;i<${#install_list[@]};i++ ))
    do
      echo_fun 4 ${install_list[i]}
    done
}

 
function yn_install(){
   read -p "选择安装项[1-9]: " num
   case $num in
        1)
        echo_fun 1  ${install_list[0]}
        step_fun_1
        ;;
        2)
        echo_fun 1  ${install_list[1]}
        step_fun_2
        ;;
        3)
        echo_fun 1  ${install_list[2]}
        step_fun_3
        ;;
        4)
        echo_fun 1  ${install_list[3]}
        step_fun_4
        ;;
        5)
        echo_fun 1  ${install_list[4]}
        step_fun_5
        ;;
        6)
        echo_fun 1  ${install_list[5]}
        step_fun_6
        ;;
        7)
        echo_fun 1  ${install_list[6]}
        step_fun_7
        ;;
        8)
        echo_fun 1  ${install_list[7]}
        step_fun_8
        ;;
        9)
        echo_fun 1  ${install_list[8]}
        step_fun_9
        ;;
        *)
        echo_fun 3 数字应该在1-9之间
        exit
        ;;
   esac
}

#获取安装机器ip
function get_host_list(){
	echo_fun 4 请输入需要安装的机器,多个ip之间用逗号隔开
	read -p "host_list=" host_list
	echo ''
	str=${host_list//,/ }
	host_ip=($str)
}


#检查安装目录是否存在,存在则退出程序,以防误安装
function check_cluster_catalog_exist(){
   num=0
   for ip in ${host_ip[*]}
   do
      num=$(ssh $ip test -d $1 &&  echo 1 || echo 0 )
      if [ $num -eq 1 ];then
        echo_fun 3 "$ip机器上的$2目录存在,请注意检查" 
        exit
      fi
done
}


#检查用户是否存在,不在就开始创建
function check_cluster_etc_profile(){

   cluster_path_num=0
   for ip in ${host_ip[*]}
   do
     cluster_path_num=$(ssh $ip cat /etc/profile |grep -w $1 |wc -l )
     if [ ${cluster_path_num} -ge 1 ];then
        echo_fun 5 $ip机器上$1环境变量已经配置,请检查准确性
        read -p "正确与否[y/n]:" answer		
	    echo ''
	    if [ $answer == 'y' ];then
          echo_fun 4 $ip机器上环境变量已经存在,且配置正确
        fi
	
        if [ $answer == 'n' ];then
          exit
        fi
  
     else
        $2 ${ip}    #创建用户其中$2是函数
     fi
done
}


#解压二进制包,并设置软连接
function extract_pkg(){
    cd $1  
    tar -xf $2.tar.gz  -C  $3 
    ln -s $3/$2   $3/$4
}
 
#同步安装包和软连接到其他的集群机器上
function rsync_pkg(){
    sleep 1s
    cd $1 
    for ((i=0;i<${#host_ip[*]};i++))
    do
       rsync -av $2  $3  ${host_ip[i]}:$1  >/dev/null 2>&1
       echo_fun 2 ${host_ip[i]}机器同步成功
    done 
}
 
#如果本机上不需要安装,则删除解压后的包 
function remove_pkg(){

for ((i=0;i<${#host_ip[*]};i++))
   do
      if [ ${local_ip} == ${host_ip[i]} ];then
         remove_num=1
      fi
   done
}


#授权
function chown_user(){
   for ip in ${host_ip[*]}
   do
      ssh $ip "cd $1;chown -R $2:$3 $4"
   done
}

#检查用户是否存在,不在就开始创建
function check_cluster_exist_user(){
   cluster_num_use=0
   for ip in ${host_ip[*]}
   do
      echo_fun 4 检查$ip机器上的$1用户是否存在
      cluster_num_user=$(ssh $ip cat /etc/passwd|grep -w $1|wc -l )
      if [ ${cluster_num_user} -ge 1 ];then
          echo_fun 5 $ip机器已经$1用户已经存在,请检查
          read -p "是否继续下一步[y/n]:" answer		
          if [ $answer == 'n' ];then
            exit
          fi	
     else
          echo_fun 4  ${ip}机器添加用户,并设置密码
          ssh -Tq ${ip} </dev/null 2>&1"
       fi
    done
}


#jdk环境变量
function etc_jdk_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile
      source /etc/profile
      exit
remotessh
}

#python的环境变量
function etc_python_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile
      source /etc/profile
      exit
remotessh
}

#neo4j的环境变量
function etc_neo4j_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile
      source /etc/profile
      exit
remotessh
}


#psql环境变量
function etc_psql_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile
      source /etc/profile
      exit
remotessh
}

#node.js环境变量
function etc_node_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile
      source /etc/profile
      exit
remotessh
}


#nginx环境变量
function etc_nginx_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile
      source /etc/profile
      exit
remotessh
}

 
#es环境变量
function etc_es_profile(){
   echo_fun 4 配置$ip机器上的环境变量
   ssh -Tq root@$1 <> /etc/profile 
      source /etc/profile
      exit
remotessh
}
 
# ------------------------------------------------第一步jdk----------------------------------------------------
#安装jdk
function step_fun_1(){
   #获取安装机器ip
   get_host_list

   echo_fun 4 请输入java安装位置如[/opt]
   read -p "jdk_install_path=" JDK_INSTALL_PATH
   echo ''

   echo_fun 5  检查java安装目录是否存在

   #循环检查/opt/java目录是否存在
   check_cluster_catalog_exist ${JDK_INSTALL_PATH}/java java
 

   if [ $num -eq 0 ];then
     #解压jdk包
     echo_fun 4 解压jdk二进制包,并创建软连接
     extract_pkg ${SOFTWARE_PATH}  ${JDK_VERSION}  ${JDK_INSTALL_PATH} java

     #同步
     echo_fun 4 同步jdk目录和软连接到其他的集群机器上
     rsync_pkg ${JDK_INSTALL_PATH}  java  ${JDK_VERSION} 

     #删除
     remove_num=0
     remove_pkg  ${JDK_INSTALL_PATH} ${JDK_VERSION}
     if [ ${remove_num} -ne 1 ];then
   	   cd ${JDK_INSTALL_PATH}
   	   rm -rf ${JDK_INSTALL_PATH}/java  ${JDK_INSTALL_PATH}/${JDK_VERSION}
     fi
   fi 


    echo_fun 4 检查环境变量
    check_cluster_etc_profile ${JDK_INSTALL_PATH}/java  etc_jdk_profile
}
 

# ------------------------------------------------第二步python---------------------------------------------------- 

#安装python
function step_fun_2(){

   #获取安装机器ip
   get_host_list

   echo_fun 4 请输入python安装位置如[/opt]
   read -p "python_install_path="  PYTHON_INSTALL_PATH
   echo ''
   echo_fun 4  检查python安装目录是否存在
   #循环检查python目录是否存在
   check_cluster_catalog_exist ${PYTHON_INSTALL_PATH}/python  python
 
 
   if [ $num -eq 0 ];then
     #解压python包
     echo_fun 4 解压python二进制包,并创建软连接
     extract_pkg ${SOFTWARE_PATH}  ${PYTHON_VERSION}  ${PYTHON_INSTALL_PATH} python

     #同步
     echo_fun 4 同步python目录和软连接到其他的集群机器上
     rsync_pkg ${PYTHON_INSTALL_PATH}  python  ${PYTHON_VERSION} 

     #删除
     remove_num=0
     remove_pkg  ${PYTHON_INSTALL_PATH} ${PYTHON_VERSION}
     if [ ${remove_num} -ne 1 ];then
   	    cd ${PYTHON_INSTALL_PATH}
   	    rm -rf ${PYTHON_INSTALL_PATH}/python  ${PYTHON_INSTALL_PATH}/${PYTHON_VERSION}
     fi
   fi 
 
   echo_fun 4 检查环境变量
   check_cluster_etc_profile ${PYTHON_INSTALL_PATH}/python   etc_python_profile 
}



#---------------------------------------------------------第三步---------------------------------------------------- 
#本地解压,发送到主从机器
function tar_mysql(){
	
   cd ${SOFTWARE_PATH}
   tar -xf ${MYSQL_VERSION}.tar.gz  -C  ${MYSQL_INSTALL_PATH}
   ln -s ${MYSQL_INSTALL_PATH}/${MYSQL_VERSION}   ${MYSQL_INSTALL_PATH}/mysql
   cd ${MYSQL_INSTALL_PATH}
   for ((i=0;i<${#host_ip[*]};i++))
   do
      rsync -av mysql  ${MYSQL_VERSION}  ${host_ip[i]}:${MYSQL_INSTALL_PATH} >/dev/null 2>&1
      echo_fun 2 ${host_ip[i]}机器mysql安装目录已经同步完毕
   done

   #删除
   remove_num=0
   remove_pkg  ${MYSQL_INSTALL_PATH} ${MYSQL_VERSION}
   if [ ${remove_num} -ne 1 ];then
      cd ${MYSQL_INSTALL_PATH}
      rm -rf ${MYSQL_INSTALL_PATH}/mysql  ${MYSQL_INSTALL_PATH}/${MYSQL_VERSION}
   fi
}

#修改单机mysql上/etc/my.cnf配置文件
function  alter_mysql_file(){
   ssh -Tq $1 </dev/null 2>&1
	 /etc/init.d/mysql.server start
	 exit
remotessh
}



#mysql主从复制
function mysql_ha(){

   echo_fun 5 mysql的主从安装开始

   #获取安装机器ip
   get_host_list
   echo ''
   echo_fun 4 请输入mysql安装位置如[/usr/local]
   read -p "mysql_install_path=" MYSQL_INSTALL_PATH
   echo ''

   #检查主机器mysql安装目录是否存在
   check_cluster_catalog_exist ${MYSQL_INSTALL_PATH}/mysql mysql

   #安装依赖
   check_cluster_package autoconf
   check_cluster_package libaio-devel
   


   echo_fun 5 请输入mysql的属组用户
   read -p "mysql_user=" MYSQL_USER
   echo ''
   check_cluster_exist_user ${MYSQL_USER}
 
   if [ ${cluster_num_user} -eq 0 ]; then
     echo_fun  4 给mysql用户输入密码
     read -p "mysql_password=" MYSQL_USER_PASSWD
     echo ''
     add_cluster_user ${MYSQL_USER} ${MYSQL_USER_PASSWD}
   fi
 

   #开始安装
   echo_fun 4 本机解压,然后发送到主从机器上,时间较长请耐心等待
   tar_mysql 

   #授权
   chown_user ${MYSQL_INSTALL_PATH} ${MYSQL_USER} ${MYSQL_USER} ${MYSQL_INSTALL_PATH}/mysql*
 

   echo_fun 4 修改主mysql的配置文件
   alter_master_file ${host_ip[0]}

   echo_fun 2 主机器配置文件修改完毕

   echo_fun 4 修改从mysql的配置文件
   alter_slave_file ${host_ip[1]}
   echo_fun 2 从机器配置文件修改完毕

 
   echo_fun 4 检查环境变量
   check_cluster_etc_profile ${MYSQL_INSTALL_PATH}/mysql

   if [ ${cluster_path_num} -lt 1 ];then
      echo_fun 4 配置环境变量
      for ((i=0;i<${#host_ip[*]};i++))
      do
         echo_fun 4 配置${host_ip[i]}机器上的环境变量
         ssh -Tq root@${host_ip[i]} <> /etc/profile 
         source /etc/profile
         exit
remotessh
      done
   fi 

   #初始化mysql,并启动
   echo_fun 4 初始化主mysql,并启动主mysql
   init_mysql ${host_ip[0]}
   echo ''

   echo_fun 4 初始化从mysql,并启动从mysql
   init_mysql ${host_ip[1]}
   echo ''


   #主mysql设置密码和配置
   echo_fun 4  初始化主mysql密码,创建同步master端数据库的用户

   echo_fun 5 请输入root用户密码
   read -p "root_passwd=" root_passwd
   echo ''

   echo_fun 5 输入同步master数据库到slave端用户
   read -p "replication_user=" r_user
   echo ''

   echo_fun 5 输入用户密码
   read -p "replication_passwd=" r_passwd

   ssh -Tq ${host_ip[0]}</dev/null|grep Position |cut -f2 -d :
EOF
)
   echo_fun 5 pos地址:${postion_num}

   #bin-log文件名 
   log_name=$(ssh -Tq ${host_ip[0]} </dev/null|grep File |cut -f2 -d :
EOF
)
   echo_fun 5 bin-log文件名:${log_name}

   #从mysql设置密码和配置
   ssh -Tq ${host_ip[1]} </dev/null |grep "Running: Yes"|wc -l
eof
)

   if [ $running_num -eq 2 ];then
      echo_fun 2 "mysql主从复制搭建成功"     
   else
      echo_fun 3  "mysql主从搭建失败"
   fi

}

   #mysql单机部署
   function mysql_single(){

   echo_fun 5 mysql的单机安装开始

   #获取安装机器ip
   get_host_list
   echo ''
   echo_fun 4 请输入mysql安装位置如[/usr/local]
   read -p "mysql_install_path=" MYSQL_INSTALL_PATH
   echo ''

   #检查主机器mysql安装目录是否存在
   check_cluster_catalog_exist ${MYSQL_INSTALL_PATH}/mysql mysql
   #安装依赖
   check_cluster_package autoconf
   check_cluster_package libaio-devel

   echo_fun 5 请输入mysql的属组用户
   read -p "mysql_user=" MYSQL_USER
   echo ''
   check_cluster_exist_user ${MYSQL_USER}
 
   if [ ${cluster_num_user} -eq 0 ]; then
     echo_fun  4 给mysql用户输入密码
     read -p "mysql_password=" MYSQL_USER_PASSWD
     echo ''
     add_cluster_user ${MYSQL_USER} ${MYSQL_USER_PASSWD}
   fi
 

   #开始安装
    echo_fun 4 本机解压,然后发送到被安装机器上
    tar_mysql 

   #授权
    chown_user ${MYSQL_INSTALL_PATH} ${MYSQL_USER} ${MYSQL_USER} ${MYSQL_INSTALL_PATH}/mysql*
 
    echo_fun 4 修改mysql的配置文件
    alter_mysql_file ${host_ip[0]}
    echo_fun 2 mysql配置文件修改完毕


    echo_fun 4 检查环境变量
    check_cluster_etc_profile ${MYSQL_INSTALL_PATH}/mysql

    if [ ${cluster_path_num} -lt 1 ];then
      echo_fun 4 配置环境变量
      for ((i=0;i<${#host_ip[*]};i++))
      do
        echo_fun 4 配置${host_ip[i]}机器上的环境变量
        ssh -Tq root@${host_ip[i]} <> /etc/profile 
        source /etc/profile
        exit
remotessh
      done
    fi 

   #初始化mysql,并启动
   echo_fun 4 初始化mysql,并启动
   init_mysql ${host_ip[0]}
   echo ''


   #mysql设置密码和配置 
   echo_fun 4  初始化主mysql密码

   echo_fun 5 请输入root用户密码 
   read -p "root_passwd=" root_passwd
   echo ''

   ssh -Tq ${host_ip[0]}</home/${NEO4J_USER}/neo4j.log 2>&1
EOF
    exit
remotessh

   #判断进程是否存在
   neo4j_pid_num=$(ssh ${ip} "ps -ef |grep neo4j|grep -v grep|wc -l")
   if [ ${neo4j_pid_num} -ge 1 ];then
      echo_fun 2  ${ip}机器上的neo4j has started.... 
   fi
   done
}
 

function step_fun_4(){
 
   #获取安装机器ip
   get_host_list

   echo_fun 4 请输入neo4j安装位置如[/opt]
   read -p "neo4j_install_path=" NEO4J_INSTALL_PATH
   echo ''

   echo_fun 4 检查neo4j安装目录是否存在

   #循环检查neo4j目录是否存在
   check_cluster_catalog_exist ${NEO4J_INSTALL_PATH}/${NEO4J_VERSION} neo4j

 
   echo_fun 5 请输入neo4j的属组用户
   read -p "neo4j_user=" NEO4J_USER
   echo ''
   
   echo_fun  4 给neo4j用户输入密码
   read -p "neo4j_password=" NEO4J_PASSWD
   echo ''
  
   check_cluster_exist_user  ${NEO4J_USER} ${NEO4J_PASSWD}

   if [ $num -eq 0 ];then
     #解压neo4j包
     echo_fun 4 解压neo4j二进制包,并创建软连接
     extract_pkg ${SOFTWARE_PATH}  ${NEO4J_VERSION}  ${NEO4J_INSTALL_PATH} neo4j

     #同步
     echo_fun 4 同步neo4j目录和软连接到安装机器上
     rsync_pkg ${NEO4J_INSTALL_PATH}  neo4j  ${NEO4J_VERSION}
   
     #修改配置文件
     echo_fun 4 修改neo4j配置文件内容
     alter_neo4j_config
   
     #删除
     remove_num=0
     remove_pkg  ${NEO4J_INSTALL_PATH} ${NEO4J_INSTALL_VERSION}
     if [ ${remove_num} -ne 1 ];then
       cd ${NEO4J_INSTALL_PATH}
       rm -rf ${NEO4J_INSTALL_PATH}/neo4j  ${NEO4J_INSTALL_PATH}/${NEO4J_VERSION}
     fi
   fi 
     #授权
     chown_user ${NEO4J_INSTALL_PATH} ${NEO4J_USER} ${NEO4J_USER} ${NEO4J_INSTALL_PATH}/neo4j*

     echo_fun 4 检查环境变量
     check_cluster_etc_profile  ${NEO4J_INSTALL_PATH}/neo4j  etc_neo4j_profile
 
     #启动
     start_neo4j
}

#-------------------------------------------------------第五步--------------------------------------------------
#编译,安装
function configure_psql(){
   for ip in ${host_ip[*]}
   do
     echo_fun 4  ${ip}机器上的psql编译安装,时间较长,耐心等待....
     ssh -Tq ${ip}</dev/null 2>&1
     sleep 2s
     make  >/dev/null 2>&1 && make install >/dev/null 2>&1
remotessh
   done
}


#初始化psql
function init_psql(){
   for ip in ${host_ip[*]}
   do
   ssh -Tq ${ip} </dev/null 2>&1
    sleep 5s
EOF
remotessh
done
}

function alter_sqpl_config(){
   for ip in ${host_ip[*]}
   do
   ssh -Tq ${ip} </dev/null 2>&1
    make >/dev/null 2>&1 && make install >/dev/null 2>&1
remotessh
  done
}
 
 
function start_nginx(){
   echo_fun 4 启动nginx
   for ip in ${host_ip[*]}
   do
    ssh -Tq ${ip} </home/${NGINX_USER}/nginx.log 2>&1
    exit
remotessh

   #判断进程是否存在
   nginx_pid_num=$(ssh ${ip} "ps -ef |grep nginx|grep -v grep|wc -l")
   if [ ${nginx_pid_num} -ge 1 ];then
     echo_fun 2 ${ip}机器上的nginx has started.... 
   fi
   done
}
 
 
#nginx的部署
function step_fun_7(){
 
  #获取安装机器ip
  get_host_list

  echo_fun 4 请输入nginx安装位置如[/usr/local]
  read -p "nginx_install_path=" NGINX_INSTALL_PATH
  echo ''

  echo_fun 4 检查nginx安装目录是否存在

  #循环检查nginx目录是否存在
  check_cluster_catalog_exist ${NGINX_INSTALL_PATH}/${NGINX_VERSION} nginx


  echo_fun 5 请输入nginx的work进程用户
  read -p "nginx_user=" NGINX_USER
  echo ''

  echo_fun  4 给nginx用户输入密码
  read -p "nginx_password=" NGINX_PASSWD
  echo ''
    
  check_cluster_exist_user ${NGINX_USER}  ${NGINX_PASSWD}


  if [ $num -eq 0 ];then
    #解压nginx包
    echo_fun 4 解压nginx二进制包,并同步到其安装机器
    cd ${SOFTWARE_PATH}
    tar -xf  ${NGINX_VERSION}.tar.gz 
    for ip in ${host_ip[*]}
    do
       scp -r ${NGINX_VERSION} root@${ip}:${NGINX_INSTALL_PATH}  >/dev/null 2>&1
    done
    #删除解压的包
    cd ${SOFTWARE_PATH}
    rm -rf ${NGINX_VERSION}
  fi 


   #安装依赖
   check_cluster_package openssl-devel
   check_cluster_package pcre-devel
   check_cluster_package zlib-devel
   check_cluster_package gcc

   #初始化nginx
   init_nginx ${NGINX_USER} ${NGINX_USER}

   echo_fun 4 检查环境变量
   check_cluster_etc_profile  ${NGINX_INSTALL_PATH}/nginx   etc_nginx_profile
  

   #启动
   start_nginx
}



# ------------------------------------------------第八步----------------------------------------------------
 #定义修改es配置文件 
 function alter_es_config(){ 
   echo_fun 4 修改es配置文件内容
   sleep 1s
   cd ${ES_INSTALL_PATH}/elasticsearch/config 

   sed -i 's/#cluster.name: my-application/cluster.name: '"${cluster_name}"'/g' elasticsearch.yml
   sed -i 's/#node.name: node-1/node.name: '"${host_name[0]}"'/g' elasticsearch.yml
   sed -i 's/#path.data: \/path\/to\/data/path.data: '"${data_path}\/data"'/g' elasticsearch.yml
   sed -i 's/#path.logs: \/path\/to\/logs/path.logs: '"${logs_path}\/log"'/g' elasticsearch.yml
   sed -i 's/#network.host: 192.168.0.1/network.host: '"${host_ip[0]}"'/g' elasticsearch.yml
   sed -i '/#http.port: 9200/a\transport.tcp.port: 9300' elasticsearch.yml
   sed -i 's/#http.port: 9200/http.port: 9200/g' elasticsearch.yml
   sed -i 's/\["host1", "host2"\]//' elasticsearch.yml
   sed -i 's/#discovery.zen.ping.unicast.hosts:/discovery.zen.ping.unicast.hosts:/g' elasticsearch.yml

   for ((i=${#host_name[*]};i>0;i--))
   do
      sed -i '/discovery.zen.ping.unicast.hosts/a\               - '"${host_name[i-1]}"':9300' elasticsearch.yml
   done
   sed -i 's/#discovery.zen.minimum_master_nodes: 3/discovery.zen.minimum_master_nodes: 1/g' elasticsearch.yml
   sed -i '/#action.destructive_requires_name/a\bootstrap.system_call_filter: false' elasticsearch.yml
   sed -i '/#action.destructive_requires_name/a\http.cors.allow-origin: "*"' elasticsearch.yml
   sed -i '/#action.destructive_requires_name/a\http.cors.enabled: true' elasticsearch.yml
   sed -i '/#action.destructive_requires_name/a\node.data: true' elasticsearch.yml
   sed -i '/#action.destructive_requires_name/a\node.master: false' elasticsearch.yml

   } 

#同步es安装包和软连接到其他的集群机器上并修改配置内容
function rsync_tar(){
   echo_fun 4 同步es安装包和软连接到其他的集群机器上并修改配置内容
   sleep 1s
   cd ${ES_INSTALL_PATH}
	 
   for ((i=1;i<${#host_name[*]};i++))
   do
   rsync -av elasticsearch  ${ES_VERSION} ${host_name[i]}:/opt  >/dev/null 2>&1
   ssh -Tq ${host_name[i]} <> /etc/sysctl.conf"
        echo_fun 2 添加成功
        sysctl -p
     fi
   done
}

#所以直接判断有没有这个用户的记录,如果没有就添加,有就手动去检查
function limits_config(){
   for ip in ${host_ip[*]}
   do
     #根据用户来获取,一般是4行,如下
     # hadoop        hard  nproc    102400
     # hadoop        soft  nproc    102400
     # hadoop        hard  nofile   102400
     # hadoop        soft  nofile   102400
     nofile_nproc_num=$(ssh $ip cat /etc/security/limits.conf|grep -w $1 |wc -l)
     if [ ${nofile_nproc_num} -ge 4 ];then
       echo_fun 2 $ip机器已经设置了打开$1用户的最大进程数和文件数
     else
       echo_fun 3 $ip机器未设置打开$1用户最大进程数和文件数,进行设置
       ssh $ip "echo '$1        hard  nproc    102400' >>/etc/security/limits.conf;echo '$1        soft  nproc    102400' >>/etc/security/limits.conf;echo '$1        hard  nofile   102400' >>/etc/security/limits.conf;echo '$1        soft  nofile   102400' >>/etc/security/limits.conf"
       if [ $? -eq 0 ];then
         echo_fun 2 设置成功
       fi
     fi
   done
}




function start_es(){
  for ip in ${#host_ip[*]}
  do
    ssh -Tq root@${ip} </dev/null 2>&1 &
  exit
remotessh

   #判断进程是否存在
  es_head_pid_num=$(ssh ${es_head_ip} "ps -ef |grep -w grunt |grep -v  grep|wc -l")
  if [ ${es_head_pid_num} -ge 1 ];then
    echo_fun 2 es_head has started.... 
  else
    echo_fun 5 "es_head starting fail...."
  fi
}

 

function step_fun_9(){

   #获取安装机器ip
   echo_fun 4 请输入需要安装的机器ip
   read -p "es_head_ip=" es_head_ip

   echo_fun 4 请输入es-head安装位置如[/opt]
   read -p "es_head_install_path=" ES_HEAD_INSTALL_PATH
   echo ''

   #检查目录是否存在
   echo_fun 4 检查es-head安装目录是否存在 

   check_cluster_catalog_exist ${ES_HEAD_INSTALL_PATH}/${ES_HEAD_VERSION}  ${ES_HEAD_VERSION}

   if [ $num -eq 0 ];then
     #解压jdk包
     echo_fun 4 解压es-head二进制包,并修改配置文件
   
     extract_es_head ${SOFTWARE_PATH}

     #同步
     echo_fun 4 同步es-head目录到其安装机器上
  
     cd ${ES_HEAD_INSTALL_PATH}
     scp -r  ${ES_HEAD_VERSION}   ${es_head_ip}:${ES_HEAD_INSTALL_PATH}  >/dev/null 2>&1 

     #删除
     if [ ${local_ip} != ${es_head_ip} ];then
   	    cd ${ES_INSTALL_PATH}
   	    rm -rf  ${ES_HEAD_INSTALL_PATH}/${ES_HEAD_VERSION}
     fi
   fi 

    #启动
    start_es_head
}


# ------------------------------------------------程序开始----------------------------------------------------
yn='y'
echo_fun 1 部署脚本正在执行
while [ "$yn" == "y" ];do 
  echo_list
  yn_install
  read -p '是否继续安装其他组件[y/n]:' yn
  echo ''
done





执行的结果如下:

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第1张图片

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第2张图片

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第3张图片

安装之前会检查是否之前有过安装,和配置过环境变量。如果出现上述2种情况,则程序终止,需要手动检查。

正确安装结果如下:

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第4张图片

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第5张图片

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第6张图片

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第7张图片

shell脚本一键部署jdk,python,mysql,postgresql,es等组件_第8张图片

你可能感兴趣的:(自动化部署脚本)