shell脚本:脱敏的脚本,对镜像服务进行取包发增量

shell脚本内容为,其中的python脚本在python合集内可以找到相关内容:

点击查看代码
#!/bin/sh

APPS_HOME=/aa/bb/cc/dd/ee/apps
RELEASE_HOME=/aa/bb/cc/dd/ee/weiba-release
if [ $(ls ./ | grep -i xlsx | grep -i '小尾巴想看雪' | wc -l) -eq 1 ]; then
  # 参数从清单获取,拿到清单后清单文件名进行处理
  RELEASE_NAME=$(ls ./ | grep -i xlsx | sed 's#小尾巴想看雪发布清单-##g' | sed 's#.xlsx##g')
  RELEASE_NUM=$(ls ./ | grep -i xlsx | sed 's#小尾巴想看雪发布清单-##g' | sed 's#例行_##g' | sed 's#紧急_##g' | sed 's#.xlsx##g')
  # 这个值给镜像版本使用
  if [ -n "$RELEASE_NUM" ]; then
    echo "==本次增量版本号为${RELEASE_NUM}================================"
    export RELEASE_NUM
  else
    echo "没有获取到想要的版本号,程序退出"
    exit
  fi
else
  echo "==${RELEASE_HOME}目录下清单文件不对,请检查================================"
  exit 111
fi
# curDay=$(date +%Y%m%d)
# curHour=$(date +%Y%m%d%H)
curTime=$(date +%Y%m%d%H%M)
IMAGES_TARNAME=weiba-images-${RELEASE_NAME}.tar.gz
SQL_ZIPNAME=weiba-sql-${RELEASE_NAME}.zip
# MOBILE_TARNAME=weiba-mobile-${RELEASE_NAME}.tar.gz
RELEASE_BAK=${RELEASE_HOME}/release_bak                          # 取包镜像备份目录
PACKAGE_BASE_BAK=${RELEASE_HOME}/package_base_bak/${RELEASE_NAME} # 基准包备份目录(初始备份+每次增量完后备份)
# checkSql=${RELEASE_HOME}/checkUpDataSql.sh
#FETCH_BUSS_LOGFILE=${RELEASE_HOME}/fetchLog.txt  #抽取日志
PACKAGE_HOME=${RELEASE_HOME}/${RELEASE_NAME}                                      # 取包存放目录
BACKUP_IMAGES="true"                                                             # 是否打包镜像
IMAGE_NAMES=""                                                                   # 定义镜像名,在使用时需要去重
SERVICE_NAMES=""                                                                 # 自定义增量后端服务变量名
STATIC_NAMES=                                                                    # 自定义增量前端服务变量名
DOC_HOME=${RELEASE_HOME}/releasedoc                                              # 脚本SVN库本地关联目录
DOC_SVN_PATH=http://192.168.xx.xx/bushiahoren/bushiahoren/haoren/doc # 脚本SVN库地址

function autoRelease() {
  # 自动处理填充release_*.txt文件,前提条件为将.xlsx文件上传到脚本目录
  # 本部分报错时:1)在main函数中注释本函数得引用,手动复制清单文件内容到release_*.txt文件 2)知会username1进行修正
  if [ $BACKUP_IMAGES == "true" ]; then
    echo "==使用autoRelease.py脚本根据清单文件自动填充txt文件================================"
    /usr/local/python3.10.9/bin/python3.10 autoRelease.py $RELEASE_HOME $BACKUP_IMAGES
  fi
}

function beginGetPack() {
  # 初始化本次发布包存放目录
  [ -d "${PACKAGE_HOME}" ] || mkdir -p ${PACKAGE_HOME}

  # 后端服务通过release_service处理获取
  SERVICE_NAMES+=$(cat ${RELEASE_HOME}/temp_release_service.txt | awk -F '/' '{print $2}' | sort | uniq | xargs echo -n)
  # 前端服务通过release_static处理获取
  STATIC_NAMES+=$(cat ${RELEASE_HOME}/temp_release_static.txt | awk -F '/' '{print $2}' | grep -iE 'weiba-huairen-static|weiba-haoren-static' | sort | uniq | xargs echo -n)
}

function getServicePackage() {
  # 根据清单获取微服务后端增量清单
  # 移动清单到基准目录
  echo "==提取后端发布包================================"

  if [ -n "$SERVICE_NAMES" ]; then
    for SERVICE_NAME in ${SERVICE_NAMES}; do
      # 从清单文件中获取该服务对应的后端文件
      file_list=$(cat ${RELEASE_HOME}/temp_release_service.txt | grep ${SERVICE_NAME} | sed 's#/'"${SERVICE_NAME}"'/##g' | sort | uniq | xargs echo -n)

      # SERVICE_NAME来对SERVICE进行赋值,用于基准目录名
      if [ ${SERVICE_NAME} == "weiba-sproc" ]; then
        SERVICE=sproc4sx
      elif [ ${SERVICE_NAME} == "weiba--haha-huairen" -o ${SERVICE_NAME} = "weiba-haha-kao" -o ${SERVICE_NAME} = "weiba-jadp" ]; then
        SERVICE=kao
      elif [ ${SERVICE_NAME} == "weiba-atm" ]; then
        SERVICE=atm
      else
        echo "没有匹配到服务,程序退出"
        exit 111
      fi

      # 向变量添加参数,用于镜像打包
      if [ $SERVICE == "bushihaoren" ]; then
        IMAGE_NAMES+=" "haoren-service:${RELEASE_NUM}
      else
        IMAGE_NAMES+=" "${SERVICE}-service:${RELEASE_NUM}
      fi

      # 从测试环境构建目录构建目录将后端文件移动到到基准目录
      echo "移动${SERVICE_NAME}增量文件到基准目录"
      echo "cd ${APPS_HOME}/${SERVICE}"
      cd ${APPS_HOME}/${SERVICE} || exit 111
      echo "tar cfz ${SERVICE_NAME}.tar.gz $file_list"
      tar cfz ${SERVICE_NAME}.tar.gz $file_list || exit 111
      echo "tar xfz ${SERVICE_NAME}.tar.gz -C ${RELEASE_HOME}/${SERVICE}/"
      tar xfz ${SERVICE_NAME}.tar.gz -C ${RELEASE_HOME}/${SERVICE}/ || exit 111
      rm -f ${SERVICE_NAME}.tar.gz || exit 111
    done

  else
    echo "==没有匹配到后端增量==============================="
  fi
}

function getStaticPackage() {
  # 根据前端资源文件路径获取前增量发布包
  echo "==提取考试服务前端发布包=================================="

  # STATIC_NAMES存在则代表一定有前端
  if [ -n "${STATIC_NAMES}" ]; then
    for STATIC_NAME in $STATIC_NAMES; do
      # 从清单文件中获取服务对应的前端文件
      file_list=$(cat ${RELEASE_HOME}/temp_release_static.txt | grep -i ${STATIC_NAME} | sed 's#/'"${STATIC_NAME}"'/##g' | xargs echo -n)

      # 从测试环境构建目录移动前端增量文件到基准目录
      echo "移动${STATIC_NAME}增量文件到基准目录"
      echo "cd ${APPS_HOME}/nginx/"
      cd ${APPS_HOME}/nginx/ || exit 111
      echo "tar cfz kao-nginx.tar.gz $file_list"
      tar cfz kao-nginx.tar.gz $file_list || exit 111
      echo "tar xfz kao-nginx.tar.gz -C ${RELEASE_HOME}/nginx"
      tar xfz kao-nginx.tar.gz -C ${RELEASE_HOME}/nginx || exit 111
      rm -f kao-nginx.tar.gz || exit 111

    done

    # 打包镜像增加nginx服务
    IMAGE_NAMES+=" "nginx-service:${RELEASE_NUM}

  else
    echo "==没有匹配到前端增量================================"
  fi
}

function getMobilePackage() {
  #根据移动端资源文件路径获取前增量发布包
  echo "==提取移动端发布包======================================="
  mobile_x=$(cat ${RELEASE_HOME}/temp_release_mobile.txt | xargs echo -n)
  # 内容存在后补充
}

function getSqlPackage() {
  #根据问题单号获取SQL脚本发布包、说明文档发布包
  echo "==提取SQL脚本和说明文档发布包=============================="
  temp_x=$(cat ${RELEASE_HOME}/temp_release_number.txt | xargs echo -n)
  # 自定义SQL相对路径,使用其进行打包
  SQL_DIR=""

  if [ -n "${temp_x}" ]; then
    echo "同步业务脚本和操作票到本地目录"
    [ -d $DOC_HOME ] || mkdir -p $DOC_HOME
    svn_dir_true=$(svn info $DOC_HOME | grep -i "$DOC_SVN_PATH" | wc -l)
    [ $svn_dir_true == 0 ] && svn checkout $DOC_SVN_PATH $DOC_HOME --username=username1 --password=xxxxxxxxxxxxxxxxxx
    [ $svn_dir_true == 1 ] && svn update $DOC_HOME --username=username1 --password=xxxxxxxxxxxxxxxxxx

    echo "cd $DOC_HOME/脚本和操作票"
    cd $DOC_HOME/脚本和操作票 || exit 111

    # 对单号进行路径匹配,匹配结果追加到变量SQL_DIR
    echo "根据变更单号进行路径匹配"
    for single_num in $temp_x; do
      single_num_dir=$(find -type d -name ${single_num}* | grep -Eiv '已发正式|已发现场|测试环境已执行|正式环境已执行|业务脚本-历史归档' | xargs echo -n)
      if [ -n "$single_num_dir" ]; then
        SQL_DIR+=' '${single_num_dir}
      else
        echo "变更单号${single_num}未匹配到业务脚本或操作票"
      fi
    done
    echo "本次所有变更单号匹配的路径为:${SQL_DIR}"

    if [ -n "$SQL_DIR" ]; then
      echo "对SQL及操作票文档进行打包"
      # 增量包存储目录/SQL及文档压缩包名 SQL及文档相对路径(相对路径为$DOC_HOME/脚本和操作票)
      echo "zip ${PACKAGE_HOME}/${SQL_ZIPNAME} -r $SQL_DIR"
      zip ${PACKAGE_HOME}/${SQL_ZIPNAME} -r $SQL_DIR
      [ $? -eq 0 ] || exit 111

      # 移动:变更单号目录($single_dir) 测试环境已执行(${dir}/测试环境已执行)
      echo "移动已打包目录到测试环境已执行"
      for single_dir in $SQL_DIR; do
        dir=$(dirname $single_dir)
        if [ ! -d "${dir}/已发现场" ]; then
          mkdir -p ${dir}/已发现场
          svn add ${dir}/已发现场
        fi

        echo "svn move $single_dir $dir/测试环境已执行/"
        svn move $single_dir $dir/已发现场/
        [ $? -eq 0 ] || exit 111
      done

      # 提交SVN更新
      echo "提交SVN更新"
      svn update --username=username1 --password=xxxxxxxxxxxxxxxxxx
      svn commit -m "发正式移动目录${curTime}" --username=username1 --password=xxxxxxxxxxxxxxxxxx
      [ $? -eq 0 ] || exit 111
    else
      echo "==本次变更单号没有匹配到业务脚本或操作票=============================="
    fi

  else
    echo "==未填写变更单号===================================="
  fi
}

function tarImages() {
  # 将已经更新好的镜像进行打包
  if [ ${BACKUP_IMAGES} == "true" ]; then
    # 移动清单文件到增量目录下
    echo "==移动清单文件到增量目录下===================================="
    if [ $(ls ${RELEASE_HOME} | grep -i "${RELEASE_NAME}" | grep -i xls | wc -l) -ge 1 ]; then
      mv ${RELEASE_HOME}/$(ls ${RELEASE_HOME} | grep -i "${RELEASE_NAME}" | grep -i xls) ${PACKAGE_HOME}
    fi
    if [ $(ls ${RELEASE_HOME} | grep -i "${RELEASE_NAME}" | grep -i doc | wc -l) -ge 1 ]; then
      mv ${RELEASE_HOME}/$(ls ${RELEASE_HOME} | grep -i "${RELEASE_NAME}" | grep -i doc) ${PACKAGE_HOME}
    fi

    # 打包镜像
    if [ -n "$IMAGE_NAMES" ]; then
      # 全部完成后进行增量镜像启动测试
      echo "==镜像启动容器测试==================================="
      cd ${RELEASE_HOME} || exit 111
      sh build_image.sh

      echo "==打包镜像===================================="
      # 对镜像名进行去重,在kao/basicommon/jadp时追加重名对象
      echo "docker save $(echo $IMAGE_NAMES | sed 's# #\n#g' | sort | uniq | xargs echo -n) -o ${PACKAGE_HOME}/$IMAGES_TARNAME"
      docker save $(echo $IMAGE_NAMES | sed 's# #\n#g' | sort | uniq | xargs echo -n) -o ${PACKAGE_HOME}/$IMAGES_TARNAME
      [ $? -eq 0 ] || exit 111

      # 备份基准包
      echo "==备份基准包目录==================================="
      echo "[ -d "$PACKAGE_BASE_BAK" ] || mkdir -p $PACKAGE_BASE_BAK"
      [ -d "$PACKAGE_BASE_BAK" ] || mkdir -p $PACKAGE_BASE_BAK
      # 除了初始备份外,基准包目录在每次镜像制作完成后进行备份,备份以版本号为准
      # $PACKAGE_BASE_BAK为基准包备份目录
      for service in $IMAGE_NAMES; do
        SERVICE=$(echo $service | awk -F '-' '{print $1}')
        # 特殊处理生产目录
        [ $SERVICE == "sproc" ] && SERVICE=sproc4sx
        echo "cp -a ${RELEASE_HOME}/${SERVICE} ${PACKAGE_BASE_BAK}/${SERVICE}-${RELEASE_NAME}"
        cp -a ${RELEASE_HOME}/${SERVICE} ${PACKAGE_BASE_BAK}/${SERVICE}-${RELEASE_NAME}
        [ $? -eq 0 ] || exit 111
      done

      # 需要额外添加停止服务的命令
      echo "==在10min后删除本次增量镜像启动的容器==================================="
      echo "sh ${RELEASE_HOME}/stop_container.sh"
      sh ${RELEASE_HOME}/stop_container.sh &

    else
      echo "==没有匹配的镜像名,不需打包镜像===================================="
    fi

    # 将增量清单写入文件
    echo "==将增量清单写入txt文件===================================="
    [ -f "${PACKAGE_HOME}/${RELEASE_NAME}_增量清单.txt" ] || touch ${PACKAGE_HOME}/${RELEASE_NAME}_增量清单.txt
    date >>${PACKAGE_HOME}/${RELEASE_NAME}_增量清单.txt
    ls -lrth ${PACKAGE_HOME} | awk '{print $9}' | grep -v ${RELEASE_NAME}_增量清单 >${PACKAGE_HOME}/${RELEASE_NAME}_增量清单.txt

    # 完整备份增量包,${RELEASE_HOME}/release_bak/${RELEASE_NAME}为备份目录
    echo "cp -a ${PACKAGE_HOME} ${RELEASE_HOME}/release_bak/"
    cp -a ${PACKAGE_HOME} ${RELEASE_HOME}/release_bak/
    [ $? -eq 0 ] || exit 111

  else
    echo "==BACKUP_IMAGES不为true,不需打包镜像===================================="
  fi
}

function detailedList() {
  cd $RELEASE_HOME || exit 111
  # 将temp_*.txt文件读取到日志文件中
  echo ""
  echo ""
  echo "===读取清单文件记录============================================================="
  # 读取的是处理后的文件
  for item in number service static mobile; do
    temp_file=temp_release_${item}.txt
    echo "==读取$temp_file文件内容===================================="
    cat $temp_file
    echo ""
  done
  echo ""
  echo ""
  echo ""

  echo "=============================增量取包清单============================="
  # 前端,只有考试和CAP公共存在前端
  echo "=============================前端文件清单============================="
  cat temp_release_static.txt | grep -iE 'weiba-huairen-static|weiba-haoren-static'
  echo ""

  # 后端更新文件清单
  echo "=============================后端文件清单清单========================="
  cat temp_release_service.txt
  echo ""

  # 镜像清单,存在kao出现追加多次的情况,输出时处理重复镜像名
  echo "=============================容器镜像清单============================="
  if [ -n "$IMAGE_NAMES" ]; then
    for item in $(echo $IMAGE_NAMES | sed 's# #\n#g' | sort | uniq | xargs echo -n); do
      echo $item
    done
  fi
  echo ""

  # 文档和SQL
  if [ -f ${PACKAGE_HOME}/${SQL_ZIPNAME} ]; then
    echo "=============================业务脚本清单============================="
    unzip -l ${PACKAGE_HOME}/${SQL_ZIPNAME} | awk '$1!=0 {print $4}' | grep -iE '业务脚本'
    echo ""

    echo "=============================操作票文档================================="
    unzip -l ${PACKAGE_HOME}/${SQL_ZIPNAME} | awk '$1!=0 {print $4}' | grep -iE '元数据文档|操作票|关注文档'
    echo ""
  else
    echo "=============================业务脚本清单============================="
    echo ""
    echo "=============================操作票文档================================="
    echo ""
  fi
}


function autoVersionList() {
  echo ""
  echo ""
  echo "主动下发次增量版本"
  AGO_FIVE=`date -d  "30 day ago" +"%Y-%m-%d"`
  BEFORE_FIVE=`date -d  "-60 day ago" +"%Y-%m-%d"`
  sed -i 's#"startPlanReleaseTime".*#"startPlanReleaseTime": "'"${AGO_FIVE}"'",#g' autoVersionList.py
  sed -i 's#"endPlanReleaseTime".*#"endPlanReleaseTime": "'"${BEFORE_FIVE}"'",#g' autoVersionList.py

  /usr/local/python3.10.9/bin/python3.10 autoVersionList.py "$RELEASE_NAME"
  echo ""
}

function changeFileEncode() {
  #文件编码转换,防止编码错误
  dos2unix ${RELEASE_HOME}/release_service.txt
  dos2unix ${RELEASE_HOME}/release_static.txt
  dos2unix ${RELEASE_HOME}/release_mobile.txt
  dos2unix ${RELEASE_HOME}/release_number.txt
}

function removeDuplicate() {

  #文件去除重复内容,不规则字符去除
  sed -i 's#"##g' ${RELEASE_HOME}/release_service.txt
  sed -i 's#"##g' ${RELEASE_HOME}/release_static.txt
  #sed -i 's#"##g' ${RELEASE_HOME}/release_mobile.txt
  sed -i 's#"##g' ${RELEASE_HOME}/release_number.txt

  sed -i 's#代码路径##g' ${RELEASE_HOME}/release_service.txt
  sed -i 's#设计方案##g' ${RELEASE_HOME}/release_static.txt
  #sed -i 's#移动端前端代码路径##g' ${RELEASE_HOME}/release_mobile.txt
  sed -i 's#问题.*##g' ${RELEASE_HOME}/release_number.txt

  #删除汉子无
  sed -i 's#无##g' ${RELEASE_HOME}/release_service.txt
  sed -i 's#无##g' ${RELEASE_HOME}/release_static.txt
  sed -i 's#无##g' ${RELEASE_HOME}/release_number.txt

  #文件去pc前端和mobile端的存在的“\”
  sed -i 's/\\/\//g' ${RELEASE_HOME}/release_static.txt
  #sed -i 's/^\///g' ${RELEASE_HOME}/release_static.txt
  sed -i 's/\\/\//g' ${RELEASE_HOME}/release_mobile.txt
  #sed -i 's/^\///g' ${RELEASE_HOME}/release_mobile.txt

  #生成临时文件,用于提取发布包、核对发布包内容准确性
  cat ${RELEASE_HOME}/release_service.txt | grep ^[^#][^0] | sort | uniq >${RELEASE_HOME}/temp_release_service.txt
  cat ${RELEASE_HOME}/release_static.txt | grep ^[^#][^0] | sort | uniq >${RELEASE_HOME}/temp_release_static.txt
  #cat ${RELEASE_HOME}/release_mobile.txt | grep ^[^#][^0] | sort | uniq >${RELEASE_HOME}/temp_release_mobile.txt
  cat ${RELEASE_HOME}/release_number.txt | grep ^[^#][^0] | sort | uniq >${RELEASE_HOME}/temp_release_number.txt
}

function clearFile() {
  #清理工作文件内容,以免下次执行时取到错误内容
  cat /dev/null >${RELEASE_HOME}/release_service.txt
  cat /dev/null >${RELEASE_HOME}/release_static.txt
  #cat /dev/null >${RELEASE_HOME}/release_mobile.txt
  cat /dev/null >${RELEASE_HOME}/release_number.txt
}

function main() {
  echo ""
  echo "=============================${RELEASE_NAME}增量取包日志输出================================="
  echo ""
  # 自动填充txt文件
  autoRelease
  # 处理txt文档
  changeFileEncode
  removeDuplicate
  # 获取变更单号
  beginGetPack
  # 取包
  getServicePackage
  getStaticPackage
  #getMobilePackage
  getSqlPackage
  # 镜像打包、删除容器
  tarImages
  # 输出本次增量信息
  detailedList
  clearFile
  # 主动下发版本列表本次版本
  autoVersionList
  echo ""
}

main >> ${RELEASE_HOME}/recordLog.log 2>1&

tail -10f ${RELEASE_HOME}/recordLog.log

posted @ 2023-07-04 16:42  小尾巴想看雪  阅读(64)  评论(0编辑  收藏  举报