50010623 发表于 2018-8-21 07:13:03

使用shell实现代码自动发布

#!/bin/bash  
#Usage: watch_file absolute_dirname
  
#Author: zhangchunyang Date: 2017/01/05 17:00
  
#Auto online
  

  
path="$1"
  
script_dir="/home/zhangchunyang/ansible_stage/roles/online/tasks"
  
var_dir="/home/online_2"
  
tmpfile="/tmp/contrast"
  

  
#control platform
  
declare -a platform=("xxx1" "xxx2" "xxx3" "xxx4" "xxx5" "xxx6" "xxx7" "xxx8" "xxx9" "xxx10.11000" "xxx11.11100")
  

  

  

  
execute (){
  sleep 15
  cd $1
  ansible-playbook $2.yml -e "hosts=$3_2" -f 6 &>> /tmp/online.log_$(date +%Y%m%d)
  #echo "ansible-playbook $2yml -e "hosts=$3_2""
  
}
  

  
goods_center_restart() {
  sleep 15;
  cd $1
  ansible-playbook $2.yml -e "hosts=$3_2" -t stop -f 6 &>> /tmp/online.log_$(date +%Y%m%d)
  ansible-playbook $2.yml -e "hosts=$3_2" -t start -f 6 &>> /tmp/online.log_$(date +%Y%m%d)
  #echo "ansible-playbook $2.yml -e "hosts=$3_2 -t stop""
  
}
  

  
mail_x() {
  echo -e "$1 online is success! \n \n $(tail -1 /tmp/online.log_$(date +%Y%m%d))" | mail -s 'Gray online status' 1064187464@qq.com
  
}
  

  

  
inotifywait -mq --timefmt '%d/%m/%y/%H:%M' --format '%T %w %e %f' -e ATTRIB $path | \
  
while read date watchdir event watchfile
  
do
  
      logfile="/tmp/online.log_$(date +%Y%m%d)"
  #judge file is not hidden file or buffer file
  [[ $watchfile =~ ^\. ]] && continue || echo "$watchfile is not buffer file or hidden file. next choice:''" &>> $logfile
  #echo $date $event$watchdir $watchfile
  

  #judge file endswith zip or war
  endswith=$(echo $watchfile | awk -F"." '{printf ".%s\n",$NF}')
  if [ "$endswith" != ".zip" -a "$endswith" != ".war" ];then
  echo "$watchfile endswith isn't war or zip! " &>> $logfile
  continue
  else
  echo "$watchfile endswith is zip or war. " &>> $logfile
  fi
  #[[ $watchfile =~ \.$ ]] || echo "$watchfile endswith isn't war or zip! " &>> $logfile
  #[[ $watchfile =~ \.$ ]] && echo "$watchfile endswith is zip or war. " &>> $logfile || continue
  

  #next
  [ -e $tmpfile ] || touch $tmpfile
  olditem=$(grep $watchfile $tmpfile | awk -F":" '$1~/^'"$watchfile"'$/{printf "%s:%s\n",$1,$2}')
  md5=$(md5sum $var_dir/$watchfile | cut -d" " -f1)
  newitem="${watchfile}:$md5"
  for item in $(cat $tmpfile)
  do
  if [ -z $(echo $item | cut -d":" -f2) ];then
  sed -i "/$item/d" $tmpfile
  fi
  done
  if [ -z $olditem ];then
  echo "${watchfile}:$RANDOM" >> $tmpfile
  touch $var_dir/$watchfile
  elif [ $(echo $olditem | cut -d":" -f2) == "$md5" ];then
  echo "$watchfile is already online. Don't need online any more!" &>> /tmp/online.log_$(date +%Y%m%d)
  else
  sed -i "s/$olditem/$newitem/g" $tmpfile
  echo "Start: $date-->$watchdir $event $watchfile" &>> /tmp/online.log_$(date +%Y%m%d)
  plat_name="$(echo $watchfile | cut -d"." -f 1)"
  port="$(echo $watchfile | cut -d"." -f 2)"
  if [ -n "$port" -a "$port" != "zip" -a "$port" != "war" ];then
  yml_name=${plat_name}.${port}
  elif [ "$plat_name" == "xxx3" -a "$port" == "zip" ];then
  yml_name=${plat_name}.${port}
  else
  yml_name=${plat_name}
  fi
  

  #echo "$yml_name $plat_name"
  

  if echo ${platform[@]} | grep -w "$plat_name" &> /dev/null;then
  if [ "$plat_name" == "${platform}" ];then
  execute $script_dir $yml_name $plat_name
  goods_center_restart $script_dir $yml_name $plat_name
  mail_x $plat_name
  echo "End: $(date +%Y%m%d) --> $date $watchfile" &>> $logfile
  else
  execute $script_dir $yml_name $plat_name
  mail_x $plat_name
  echo "End: $date-->$watchfile" &>> $logfile
  fi
  else
  echo "No $plat_name.yml!"&>> $logfile
  fi
  fi
  
done


页: [1]
查看完整版本: 使用shell实现代码自动发布