shell实现的自动安装OnPremise脚本

Zss 发表于:

每次工作中新的版本发布时需要手动来安装部署server,所以使用shell来实现一个自动下载安装的脚本

1.需要从另外的服务器上面下载最新的版本,这中间就涉及到了登陆远程服务器的问题了,使用ssh来远程登陆,但是不能自动输入账号密码,说以使用sshpass依赖包来实现

stty erase ^H
    sshpass -p ${server_password} ssh ${server_name}@${server_ip} -o StrictHostKeyChecking=no <<DOC
    if [ -f ${server_path}/file_list.txt ];then
       rm -f ${server_path}/file_list.txt
    fi    
    ls ${server_path}/ | grep altaicare > ${server_path}/file_list.txt 2>&1  
    `exit`
DOC

2.首先获取到远程服务器上目录下的各个版本,然后生成相应的选项列表供用户选择,选择后进行判断若本地存在则不下载,不存在则使用sshpas+scp来自动下载所选择的版本

##############  用户输入选择来判断下载的版本###################

    if [ ! -f ${host_path}/${file_list[${user_choice}]} ];then    
        sshpass -p ${server_password} scp ${server_name}@${server_ip}:${host_path}/${file_list[${user_choice}]} ${host_path} &
        echo -e "Downloading ${file_list[${user_choice}]} form ${server_ip} server, Please wait..."
       echo -e "---------------------------------------------"
       file_path=${file_list[${user_choice}]}
        while :;do
            sleep 3
            i=`ls -l ${host_path}/${file_path} | awk '{print $5}'`
            sleep 2
            a=`ls -l ${host_path}/${file_path} | awk '{print $5}'`
            d=`ls -hl ${host_path}/${file_path} | awk '{print $5}'`
            
            if [ $i = $a ];then
                echo -e "---------------------------------------------"
                echo "----->   File download complete  ^ __ ^ <-----"
                echo -e "---------------------------------------------"
                break
            else 
                echo "----->  Down_size is ${d}, Please wait...  <-----"
            fi
        done
    else
        echo -e "${file_list[${user_choice}]} File already exists!!! "  
       file_path=${file_list[${user_choice}]}
        if [ -f ${host_path}/${file_path%.tar.gz*} ];then
            rm -fr ${host_path}/${file_path%.tar.gz*}
            sleep 1
        fi
    fi

3.当固件下载到本地后,判断是否存在相同的解压包,若存在则删除再次解压此安装包,为的是防止之前的解压出现了问题,索性解压时间不长所以,不过存不存在再次解压覆盖

echo -e "${file_list[${user_choice}]} File already exists!!! "  
       file_path=${file_list[${user_choice}]}
        if [ -f ${host_path}/${file_path%.tar.gz*} ];then
            rm -fr ${host_path}/${file_path%.tar.gz*}
            sleep 1
        fi

4.当解压后,我需要进行安装的操作,这个过程涉及到两个问题一个问题是如何自动的输入用户选择,因为安装包是交互式的,所以需要自动的输入选项来进行下一步,另外一个问题是

交互时中间网卡的选择,首先我在本地做判断需要使用哪一张网卡来选择,网卡的判断的思路是:/var/class/net 下的所有目录名代表电脑中各个网络接口,前几个一般是物理接口,所以我是使用的是

第一个网卡的第一个字母来进行比较每个接口的首字母是否一致,不一致则为虚拟接口,相同则为物理接口,依次加入到数组中,这时我就得到了所有的物理网卡

如何判断网卡是否活跃? 依次将每个网卡名字所对应的statistics文件夹下的tx 和rx 进行判断是否为0,如果没有数据则为0也就是不是活跃状态,不为0也就是我所需要安装的这张网卡

###############  获取PC中网卡列表 ############
   echo -e "---------------------------------------------"
   echo -e "-->  Judging the appropriate network card <--"
   sleep 1
   eth=`ls /sys/class/net`
   eth_list=()
   all_eth=()
   panduan_eth=()
   index=0
   for i in ${eth};do
       eth_list[${index}]=${i}
       index=$((index+1))
   done
   index2=1
   for s in ${eth_list[*]};do
       t=${s:0:1}
       if [ ${t} = ${eth_list[0]:0:1} ];then               
           all_eth[${index2}]=${s}
           panduan_eth[${index2}]=${s}
           index2=$((index2+1))    
       fi
   done

#############   判断这些网卡是否活跃并给出选择和网卡mac  ###########
#############   ${eth_choice}:选择 ${eth_mac}:网卡mac  ###########
############    计算此mac地址所对应得ip地址   ##########
   for e in ${all_eth[*]};do
       tx=`cat /sys/class/net/${e}/statistics/tx_bytes` 
       rx=`cat /sys/class/net/${e}/statistics/rx_bytes`
       if [ ${tx} != '0' -a ${rx} != '0' ];then
            number=${#panduan_eth[@]}
            for eth_index in `seq 1 ${number}`;do
                if [ ${e} = ${panduan_eth[${eth_index}]} ];then
                    eth_choice=${eth_index}  
                fi
            done
           #  echo ${e}
       fi      
   done

总结:

1.一个数列的构造:`seq 1 ${number}`

2.判断时多个判断  且 操作为:[ ${tx} != ‘0’ -a ${rx} != ‘0’ ]

3.文件读取的循环赋值操作:

while read message
do
file_list[${index}]=${message}
echo -e “(${index}). ${message}”
index=$((index+1))
done < /${host_path}/file_list.txt

4.判断文件是否存在 -f  判断

5.字符串截断  ${file_path%.tar.gz*}  从.tar.gz 截断取左边的字符

6.数组的长度:number=${#panduan_eth[@]}

7.显示数组中所有元素:${eth_list[*]}

8.显示第五行信息: i=`ls -l ${host_path}/${file_path} | awk ‘{print $5}’`

9.字符串从右开始两个字符截断:end_mac=${eth_mac:0-2}

10.结果不为空判断:[ -n “${img_list}” ]     为空: [  ! -n “${img_list}” ]

#/bin/sh

#######   versions   ######
#1.0---   基本功能
#1.1---   添加文件存在时删除旧文件来进行新的解压
#1.2---   添加旧文件的停止与卸载操作
#1.3---   添加自动选择合适网卡
#1.4---   添加新系统启动判断及旧系统的卸载情况判断
#1.5---   移动选择到最顶端使输入一次完成
###########################

function main()
{ 

###########          Server配置       ###########
  
    server_path="/home/OnPremiseBuild"
    server_ip="10.6.161.252"
    server_name="root"
    server_password="admin!234"

###########           Host配置         ##########
  
    host_path="/home/OnPremiseBuild"

########### 远程拉取文件目录下的镜像名  ###########
       
    stty erase ^H
    sshpass -p ${server_password} ssh ${server_name}@${server_ip} -o StrictHostKeyChecking=no <<DOC
    if [ -f ${server_path}/file_list.txt ];then
       rm -f ${server_path}/file_list.txt
    fi    
    ls ${server_path}/ | grep altaicare > ${server_path}/file_list.txt 2>&1  
    `exit`
DOC

############# 通过拉取的文件生成可以安装的版本  ############
   
    if [ ! -d ${host_path} ];then    
         mkdir ${host_path}
    fi
    sshpass -p ${server_password} scp ${server_name}@${server_ip}:${host_path}/file_list.txt ${host_path} 
    echo -e "\n\n------->>>  Installer OnPremise   <<<-------"
    index=1
    file_list=()
    echo -e "Option list:"
    while read message
    do
        file_list[${index}]=${message}
        echo -e "(${index}). ${message}"
        index=$((index+1))
    done < /${host_path}/file_list.txt
    echo -e "---------------------------------------------"
    echo -e "Input your choice:"
    read user_choice
    echo -e "Your choice is No.${user_choice}---${file_list[${user_choice}]}"
    echo -e "\n"
    echo -e "  | |    | |   | |    | |   | |   | |   | | "
    echo -e "  | |    | |   | |    | |   | |   | |   | | "
    echo -e "  V V    V V   V V    V V   V V   V V   V V"
    echo -e "\n"
    sleep 1
###########   获取当前系统已安装的版本 #########

   echo -e "---------------------------------------------"
   echo -e "            System Hard disk case "
   echo -e "---------------------------------------------"
   df -h
   echo -e "---------------------------------------------"
   echo -e "  Check the installed version of the system  "
   echo -e "---------------------------------------------"

   img_list=`ls /var/lib/libvirt/images`
   img_index=1
   img=()
   if [ -n "${img_list}" ];then
       echo " (0).Skip -- don't delete" 
       for iso in ${img_list};do
           echo " (${img_index}).${iso%.img*}"
           img[${img_index}]=${iso%.img*}
           img_index=$((img_index+1))
       done
       echo -e "---------------------------------------------"
       echo -e "    If you want to delete it version !!!"
       echo -e "     --->     Input your choice:" 
       echo -e " If you want to skip and install the new system "
       echo -e "     --->          Input 0           " 
       echo -e "---------------------------------------------"
       stty erase ^H
       read del_choice
       if [ ! ${del_choice} -eq "0" ];then
            del_img=${img[${del_choice}]}".img"
            echo "        Are you sure??? Input(yes/no)"
            read sure
            if [ ${sure} = "yes" ];then
                 echo -e "---------------------------------------------"
                 echo "--->  Stoping ${img[${del_choice}]%.tar.gz*}, Please wait...  <---"
                 sh ${host_path}/${img[${del_choice}]%.tar.gz*}/care_service.sh stop
                 echo -e "---------------------------------------------"
                 echo "--->  Uninstll ${img[${del_choice}]%.tar.gz*}, Please wait...  <---"
                 sh ${host_path}/${img[${del_choice}]%.tar.gz*}/care_service.sh uninstall <<DOC
yes
DOC
                 old_server_status=`ps -aux|grep ${img[${del_choice}]%.tar.gz*}|grep qemu|awk '{print $2}'`
                 if [ -n "${old_server_status}"];then
                     echo -e "----->         Uninstll is ok          <-----"
                     echo -e "---------------------------------------------"
                     sleep 1
                rm -f /var/lib/libvirt/images/${del_img}
                     echo "          Del ${img[${del_choice}]%.tar.gz*}"
                echo -e "---------------  Move is OK  ----------------"
                     echo -e "---------------------------------------------"
            else
                     echo -e "----->        Uninstll is fail         <-----"
                     echo -e "---------------------------------------------" 
                 fi
            else
                echo "-------------   Cancel delete  --------------"
            fi
       fi
   else
       echo -e " No version of the system has been installed "
       echo -e "---------------------------------------------"
   fi
   

##############  用户输入选择来判断下载的版本###################

    if [ ! -f ${host_path}/${file_list[${user_choice}]} ];then    
        sshpass -p ${server_password} scp ${server_name}@${server_ip}:${host_path}/${file_list[${user_choice}]} ${host_path} &
        echo -e "Downloading ${file_list[${user_choice}]} form ${server_ip} server, Please wait..."
       echo -e "---------------------------------------------"
       file_path=${file_list[${user_choice}]}
        while :;do
            sleep 3
            i=`ls -l ${host_path}/${file_path} | awk '{print $5}'`
            sleep 2
            a=`ls -l ${host_path}/${file_path} | awk '{print $5}'`
            d=`ls -hl ${host_path}/${file_path} | awk '{print $5}'`
            
            if [ $i = $a ];then
                echo -e "---------------------------------------------"
                echo "----->   File download complete  ^ __ ^ <-----"
                echo -e "---------------------------------------------"
                break
            else 
                echo "----->  Down_size is ${d}, Please wait...  <-----"
            fi
        done
    else
        echo -e "${file_list[${user_choice}]} File already exists!!! "  
       file_path=${file_list[${user_choice}]}
        if [ -f ${host_path}/${file_path%.tar.gz*} ];then
            rm -fr ${host_path}/${file_path%.tar.gz*}
            sleep 1
        fi
    fi

###############  解压文件  ####################
  
    echo -e "In the decompression, Please wait..."
    sleep 1
    echo -e "---------------------------------------------"
    tar -zvxf ${host_path}/${file_path} -C ${host_path}
    echo -e "---------------------------------------------"
    echo -e "----->      File zip is ok  ^ __ ^     <-----"

###############  获取PC中网卡列表 ############
   echo -e "---------------------------------------------"
   echo -e "-->  Judging the appropriate network card <--"
   sleep 1
   eth=`ls /sys/class/net`
   eth_list=()
   all_eth=()
   panduan_eth=()
   index=0
   for i in ${eth};do
       eth_list[${index}]=${i}
       index=$((index+1))
   done
   index2=1
   for s in ${eth_list[*]};do
       t=${s:0:1}
       if [ ${t} = ${eth_list[0]:0:1} ];then               
           all_eth[${index2}]=${s}
           panduan_eth[${index2}]=${s}
           index2=$((index2+1))    
       fi
   done

#############   判断这些网卡是否活跃并给出选择和网卡mac  ###########
#############   ${eth_choice}:选择 ${eth_mac}:网卡mac  ###########
############    计算此mac地址所对应得ip地址   ##########
   for e in ${all_eth[*]};do
       tx=`cat /sys/class/net/${e}/statistics/tx_bytes` 
       rx=`cat /sys/class/net/${e}/statistics/rx_bytes`
       if [ ${tx} != '0' -a ${rx} != '0' ];then
            number=${#panduan_eth[@]}
            for eth_index in `seq 1 ${number}`;do
                if [ ${e} = ${panduan_eth[${eth_index}]} ];then
                    eth_choice=${eth_index}  
                fi
            done
           #  echo ${e}
       fi      
   done
   eth_mac=`cat /sys/class/net/${panduan_eth[${eth_choice}]}/address`
   end_mac=${eth_mac:0-2}
   ip="192.168.199."`echo $((0x${end_mac}))`
   #echo ${ip}
   echo -e "---------------------------------------------"
   echo -e "     Network card name is :${all_eth[${eth_choice}]}"
   echo -e "     Network card mac is :${eth_mac}"
   echo -e "     OnPremise Server ip is :${ip}"
        echo -e "---------------------------------------------"

################  安装文件  ####################    
    
    echo -e "---------------------------------------------"
    echo -e "         Installing... Please wait..."
    echo -e "---------------------------------------------"
    sleep 1
    ./auto_choice.sh home OnPremiseBuild ${file_path%.tar.gz*} ${eth_choice}
    echo -e "---------------------------------------------"
    echo -e "----->      Install is ok  ^ __ ^     <-----"



################   启动系统  ###################

    echo -e "---------------------------------------------"
    echo -e "          Starting... Please wait..."
    echo -e "---------------------------------------------"
    sleep 1
    sh ${host_path}/${file_path%.tar.gz*}/care_service.sh start
    echo -e "---------------------------------------------"
    sleep 1
    server_status=`ps -aux|grep ${file_path%.tar.gz*}|grep qemu|awk '{print $2}'`
    if [ -n "${server_status}" ];then
        echo -e "----->    Server start is ok  ^ __ ^   <-----"
        echo -e "    OnPremise Server ip is :${ip}"
        echo -e "---------------------------------------------"
    else
        echo -e "----->  Server start is fail‘(*>﹏<*)  <-----"
        echo -e "---------------------------------------------"
    fi
}
main