自动部署Vm-care服务脚本

Zss 发表于:

结合到之前写的自动安装vmcare的脚本想到写一个定时检测的脚本,因为测试的版本是不定时的更新的,每次需要通知到我们

而镜像文件实际上开发每次传输到我们的服务器上面的,所以写一个定时循环监测的脚本,当发现新的镜像时,自动来下载,自动安装部署

1.使用到sshpass时,为什么在远程服务器上获取到的信息不能够来赋值给shell中的变量呢? 所以这个过程我使用了傻瓜的方式来检测

将已存在的镜像版本文件重定向到一个文件,一段时间再次重定向到另外一个文件,再将两个文件拉去到本地来进行变量的赋值与对比,来判断是否存在新的版本镜像

 

2.还有问题是镜像比较大,当镜像在传输时但是没有传输完,所以我需要来判断这个镜像是不是已经传输完后了,所以我使用的方式是第一次获取它的文件大小

sleep一点时间,再次来获取它的文件大小,文件大小是最小单位B,这样对比两次的文件大小就可以判断是否传输完成了,当文件大小不一致时,说明还在传输,一致后,说明文件传输完毕了

 

3.将判断正确的镜像名来赋值给之前写好的自动安装的脚本,这样子就实现了新版本发布后,脚本会自动来检测新版本的安装和部署

#!/bin/sh
function make()
{
    sshpass -p ${server_password} ssh ${server_name}@${server_ip} -o StrictHostKeyChecking=no <<DOC
    if [ -f ${server_path}/result1.log ];then
        rm -f ${server_path}/result1.log
    fi    
    ls -lt ${server_path}|grep altaicare >>${server_path}/result1.log
 
    sleep 10

    if [ -f ${server_path}/result2.log ];then
        rm -f ${server_path}/result2.log
    fi    
    ls -lt ${server_path}|grep altaicare >>${server_path}/result2.log 
    `exit`
DOC

}

function main()
{
    echo "---------------------------------------"
while :;do
    make
    sshpass -p ${server_password} scp ${server_name}@${server_ip}:${server_path}/result1.log ${host_path}
    sshpass -p ${server_password} scp ${server_name}@${server_ip}:${server_path}/result2.log ${host_path}
    r1=`cat ${host_path}/result1.log|wc -l`
    r2=`cat ${host_path}/result2.log|wc -l`
    date
    echo "正在检测Server是否存在新的镜像文件。。。"
    if [ ${r1} != ${r2} ];then
        echo "发现远程服务器新镜像!!!等待下载。。。"
        echo "---------------------------------------"
        break
    else       
        echo "暂未发现新镜像!!!等待下一次检测。。。"
        echo "---------------------------------------"
    fi
done 
    echo "---------------------------------------"
#### 检测iso文件是否完整
    new_iso=`diff ${host_path}/result1.log ${host_path}/result2.log|awk '{print $10}'|awk 'NR==2{print}'`
while :;do    
    sshpass -p ${server_password} ssh ${server_name}@${server_ip} -o StrictHostKeyChecking=no <<DOC
    if [ -f ${host_path}/result3.log ];then
        rm -f ${host_path}/result3.log
    fi
    ls -l ${server_path}/${new_iso} |awk '{print $5}'>> ${server_path}/result3.log
    `exit`
DOC
    if [ -f ${host_path}/result3.log ];then
        rm -f ${host_path}/result3.log
    fi
    sshpass -p ${server_password} scp ${server_name}@${server_ip}:${server_path}/result3.log ${host_path}
    file_size1=`cat ${server_path}/result3.log | awk '{print $5}'`
    
    sleep 10

    sshpass -p ${server_password} ssh ${server_name}@${server_ip} -o StrictHostKeyChecking=no <<DOC
    if [ -f ${host_path}/result4.log ];then
        rm -f ${host_path}/result4.log
    fi
    ls -l ${server_path}/${new_iso} |awk '{print $5}'>> ${server_path}/result4.log
    `exit`
DOC
    if [ -f ${host_path}/result4.log ];then
        rm -f ${host_path}/result4.log
    fi
    sshpass -p ${server_password} scp ${server_name}@${server_ip}:${server_path}/result4.log ${host_path}
    file_size2=`cat ${server_path}/result4.log | awk '{print $5}'`
####
    if [ ${file_size1} = ${file_size2} ];then
        echo "文件完成"
        sshpass -p ${server_password} ssh ${server_name}@${server_ip} -o StrictHostKeyChecking=no <<DOC
        rm -f ${server_path}/result*.log
        `exit`
DOC
        rm -f ${host_path}/result*.log
        break              
    fi
done
#### 这里传入需要下载的镜像名

    if [ ! -f ${host_path}/${new_iso} ];then    
        sshpass -p ${server_password} scp ${server_name}@${server_ip}:${host_path}/${new_iso} ${host_path} &
        echo -e "Downloading ${new_iso} form ${server_ip} server, Please wait..."
        echo -e "---------------------------------------------"
        file_path=${new_iso}
        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 "${new_iso} File already exists!!! "  
        file_path=${new_iso}
        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
}
#### Server config
    server_path="/home/OnPremiseBuild"
    server_ip="10.6.161.252"
    server_name="root"
    server_password="admin!234"
    host_path="/home/OnPremiseBuild"
    main