Dec 15

Puppet Agent连接Master脚本 不指定

djangowang , 14:19 , 工作相关 » Puppet , 评论(0) , 引用(0) , 阅读(1183) , Via 本站原创 | |
通过Puppet管理大量机器的过程中会经常遇到连接超时的情况,所以通过Shell脚本包装Puppet Agent随机连接也是一种很好的方法,以下为连接过程脚本。



Puppet连接过程架构
点击在新窗口中浏览此图片



Agent连接Master脚本(注:部分内部为了安全被替换为中文注释)


#!/bin/bash
# author: wds
# time : 20151212

## init
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin/"
export PATH
export LC_ALL=POSIX
export LANG=en
_PWD="/etc/puppet"
_PUPPET="/usr/local/services/puppet/bin/puppet agent"
_HOST="puppet.example.com"
_LOG_TIME=`date +%Y%m%d%H%M`

[ ! -e ${_PWD}/lock ] && mkdir ${_PWD}/lock
[ ! -e ${_PWD}/log ] && mkdir ${_PWD}/log
[ -e /etc/puppet/log/ ] && find /etc/puppet/log/ -name "*.log" -mtime +3 -exec rm -rf {} \;
[ -e /etc/puppet/lock/ ] && find /etc/puppet/lock/ -name "*.lock" -mmin +17 -exec rm -rf {} \;

## function
getip()
{
    IPADDR=`ifconfig eth1 | awk '/inet addr/ {print $2}' | cut -f2 -d ":"`
    if [ "x${IPADDR}" == "x" ];then
        IPADDR=`ifconfig eth1 | awk '/inet addr/ {print $2}' | cut -f2 -d ":"`
    fi
    echo $IPADDR
}

netlog()
{
    #打网络日志
}

#content title
sendalarm()
{
    #发送告警
}

rand(){
    min=$1
    max=$(($2-$min+1))
    num=$(date +%s%N)
    echo $(($num%$max+$min))
}

clip_ini(){

if [ ! -e /usr/local/services/clip/clip ];then
    netlog "clip not exists fail"
    exit
fi
# clip 相关介绍http://blog.puppeter.com/read.php?7

if [ ! -e /etc/facter/facts.d/clip ];then
    ipaddress=`getip`

    clip=`/usr/local/services/clip/clip cstring -i $ipaddress`
    if [ ! -e /etc/facter/facts.d/ ];then
      mkdir -p /etc/facter/facts.d/
    fi

    echo -n "#!/bin/bash
    echo \"clip=$clip\"
    " > /etc/facter/facts.d/clip
    chmod -R 777 /etc/facter/facts.d/clip
fi

return 0
}

hostname=`getip | sed 's/\./_/g'`
if [ "x${hostname}" != "x" ];then
        hostname $hostname
fi

##############################################################################################
#/usr/local/services/puppet/bin/puppet agent --server puppet.example.com --test

## first  首次连接
if [ ! -e ${_PWD}/lock/first.lock ];then
    sed -i '/puppet/d' /etc/hosts
    echo "IP $_HOST # puppet" >> /etc/hosts
    res=`$_PUPPET --server $_HOST  --test | tee -a ${_PWD}/log/puppet.log | grep "Finished catalog run" | wc -l`
    if [ $res -eq 0 ];then
    netlog "puppet first fail"
    else
        touch ${_PWD}/lock/first.lock
    fi
fi

## second 二次连接

if [ ! -e ${_PWD}/lock/second.lock ];then
ipaddress=`getip`
clip=`/usr/local/services/clip/clip cstring -i $ipaddress`
if [ "x$clip" != "x" ];then
  touch ${_PWD}/lock/second.lock
fi

idc=`echo $clip | awk -F '-' '{print $1}'`
if [ "$idc" == "tj" ];then
    _host='IP'
elif [ "$idc" == "sz" ];then
    _host='IP'
elif [ "$idc" == "sh" ];then
    _host='IP'
else
    _host='IP'
fi

sed -i '/puppet/d' /etc/hosts
echo "$_host $_HOST # puppet" >> /etc/hosts
fi

## app
##########################################################################################
if [ -e ${_PWD}/lock/puppet.lock ];then
exit;
fi

clip_ini
wait
touch ${_PWD}/lock/puppet.lock
rnd=$(rand 1 5)
sleep $rnd
res=`$_PUPPET --server $_HOST  --test | tee -a ${_PWD}/log/puppet.log | grep "Finished catalog run" | wc -l`
if [ $res -eq 0 ];then
  rnd=$(rand 1 20)
  sleep $rnd
  res=`$_PUPPET --server $_HOST  --environment=${_ENVIROMENT} --test | grep "Finished catalog run" | wc -l`
  if [ $res -eq 0 ];then
    netlog "puppet cluster fail"
  fi
fi

netlog "puppet succ"
rm -rf ${_PWD}/lock/puppet.lock
exit 0;
Tags: ,
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]