导语:目前交付流程,测试提供tgz的代码包,运维整合tgz,压缩成一个完整的tgz包。再选择一台验证服务器,通过交互输入yes,以及密钥,执行01环境准备脚本,02服务解压安装脚本。

打算后期对公司内部 cd部分进行优化,目前是一个交付包中有很多的tgz包需要更新,有多个jenkins任务,每次打包安装验证服务都要手动操作,总时长2-5小时,操作不多就是得一直去确认和交互一下。打算写成一个免交互的。大致流程 选择虚拟机,进行判断是否关机(关机代表没有人使用)并进行快照恢复。然后通过tmux执行一些后台命令,依次对交付包进行升级,执行一些命令。改好了测试了一下还未应用,做个笔记备份。

流水线如图

jenkins打包时死机 jenkins打包慢_vim

流水线脚本

有些判断不知道咋写就用了shell脚本去做。因为是sh不是bash。有些功能暂时注释了。

#!groovy

pipeline {
    agent any

    //定义构建参数
    parameters {
        string(name: '模块', defaultValue: 'aiserverconfig')
        string(name: '项目版本', defaultValue: 'CTA_220430_test',description: '请请输入930之后的项目版本,如DrWise_1130、CTA_0930')
        text(name: 'update_url', defaultValue: '192.168.10.120:/data1/PublishPackage/CTA/dwcta_CTA_V0430FIX1_220620.tgz\n192.168.10.120:/data1/PublishPackage/aiserver/aiserver_CTA_V0430FIX1_220620.tgz',description: '请输入测试提供的地址如:\n192.168.10.120:/data1/PublishPackage/aiserverconfig/aiserverconfig_1130_211203.tgz\n192.168.10.120:/data1/PublishPackage/aiserverconfig/aiserverconfig_1130_211203.tgz')
        choice(name: '项目', choices: ['CTA','DrWise','CTA_DrWise'], description: '请选择需要发布的环境,默认DrWise')
        choice(name: '验证环境', choices: ['1','192.168.10.81','192.168.10.82','192.168.10.83','192.168.10.84','192.168.10.85'], description: '请选择需要发布的地址')
    }

    //定义变量
    environment {
        // 项目名称
        PROJECT = "${params.项目}"
        // 项目版本
        VERSION = "${params.项目版本}"
        // 项目名称
        PROJECT_PATH = "${params.项目版本}"
        // 工程名称
        APP_NAME = "${params.模块}"
        // 发布地址
        DEPLOY_PATH = "${params.包地址}"
        // 模块路径
        APP_PATH = "/pkg/build/${PROJECT}/${PROJECT_PATH}/packages/lung"
        // 
        PACKAGE_PATH = "/pkg/build/${PROJECT}"
        //默认邮件接收人
        DEFAULT_EMAIL = ' '
        //负责邮件接收人
        OWNER_EMAIL = ' '
        //PL邮件接收人
        PL_EMAIL = ' '
        //待更新的项目
        UPDATE_PATHS = "${params.update_url}"
        SECRET="`</dev/urandom  tr -dc   'A-Za-z0-9*,-.?@^_~'  |  head -c 12`"
        SCRIPT_PATH="${PROJECT}/${VERSION}/${PROJECT_PATH}_`date '+%Y%m%d'`.tgz"
    

    }

    //来源全局设置配置的变量
//    tools {
//        jdk 'java-8'
//    }

    options {
        timestamps()
        //保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '10'))
    }

    //执行任务模块
    stages {
    //     stage('检查验证环境虚拟机是否关机') {
    //         agent {
    //             label "master"
    //         }
    //         steps {
    //             echo "---------------------------------------------------------------------------------"
    //             echo "Check Server ${params.验证环境}......"
    //             echo "---------------------------------------------------------------------------------"
    //             script {
    //                 sh """
				// 	if   ping -c 1 -w 1 ${params.验证环境} > /dev/null ; then  echo "ping success 虚拟机未关机" && echo "do shutdown 准备关机并确认 ";else   echo "虚拟机已经关机 准备进行快照恢复" ; fi
				// 	"""
				// 	}

    //             }
    //         }

        // stage('执行快照恢复') {
        //     agent {
        //         label "master"
        //     }
        //     steps {
        //         echo "---------------------------------------------------------------------------------"
        //         echo "操作 ${params.验证环境}恢复快照......"
        //         echo "---------------------------------------------------------------------------------"
        //         script {
        //             sh """
        //             echo 123
        //             bash -c "
        //             if [[ ${params.验证环境} =~ '192.168.10.84' ]]
        //             then
        //               echo 192.168.10.84
        //               #vim-cmd vmsvc/power.off  54
        //               ssh root@192.168.10.23  'vim-cmd vmsvc/power.off   54  '
        //               ssh root@192.168.10.23  'vim-cmd vmsvc/snapshot.revert   54  3 suppressPowerOn'
        //               ssh root@192.168.10.23  'vim-cmd vmsvc/power.on   54  '
        //             else
        //               echo not 192.168.10.84
        //             fi
        //             "
        //             """
        //             }

        //         }
        //     }

        //拉取安装包
    //     stage('Pack test') {
    //         agent {
    //             label "Release-server"
    //         }
    //         steps {
    //             echo "---------------------------------------------------------------------------------"
    //             echo "Starting Install Application......"
    //             echo "---------------------------------------------------------------------------------"
    //             script {
				// 	for (item in UPDATE_PATHS.tokenize('\n')){
					    
				// 		echo "item" + item +"Deploying to " 
				// 		//result="echo $item | grep -w 'aiserver' &>/dev/null"
				// 	    //echo "result:" + result

				// 		echo "cd /pkg/build/${params.项目}/${params.项目版本}/packages/lung" 
				// 	    sh  """
				// 	    echo APP_PATH: $APP_PATH
				// 	    echo item: ${item}
				// 	    #if [[ `echo ${item}  |grep '^[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}:/.*.tgz\$'` ]];then echo "url地址正常 "  ;else  echo "请检查输入的url地址是否有空格和中文符号";fi
				// 	    echo 0 
					    
    //                     echo 123
    //                     sudo bash /srv/judge.sh ${item} ${APP_PATH}
				// 	    echo 456

				// 	    """
	
				// 	    // echo "#${PROJECT}大包版本: ${APP_NAME}_`date '+%Y%m%d'`" >${VERSION}/version.md

				// 	}

    //             }
    //         }
    //     }
        // stage('Pack') {
        //     agent {
        //         label "Release-server"
        //     }
        //     steps {
        //         echo "---------------------------------------------------------------------------------"
        //         echo "Starting package Application......"
        //         echo "---------------------------------------------------------------------------------"
        //         script {
        //             sh """
        //                 cd $PACKAGE_PATH
        //                 echo pwd
        //                 pwd
        //                 whoami
        //                 echo "#${PROJECT}大包版本: ${PROJECT_PATH}_`date '+%Y%m%d'`" >${VERSION}/version.md
        //                 echo bash compress_new.sh ${PROJECT_PATH} ${VERSION}
        //                 #bash compress.sh ${PROJECT_PATH} ${VERSION}
        //                 echo mkdir -p /pkg/publish_pkg/${PROJECT}/${VERSION}
        //                 #mkdir -p /pkg/publish_pkg/${PROJECT}/${VERSION}
        //                 echo mv ${PROJECT_PATH}_`date '+%Y%m%d'`.tgz /pkg/publish_pkg/${PROJECT}/${VERSION}/
        //                 #mv ${PROJECT_PATH}_`date '+%Y%m%d'`.tgz /pkg/publish_pkg/${PROJECT}/${VERSION}/
        //                 echo mv ${PROJECT_PATH}_`date '+%Y%m%d'`.txt /pkg/publish_pkg/${PROJECT}/${VERSION}/
        //                 #mv ${PROJECT_PATH}_`date '+%Y%m%d'`.txt /pkg/publish_pkg/${PROJECT}/${VERSION}/
        //               """
        //         }
        //     }
        // }

        stage('Deploy') {
            agent {
                label "Release-server"
            }
            steps {
                echo "---------------------------------------------------------------------------------"
                echo "Starting package Application......"
                echo "---------------------------------------------------------------------------------"
                script {
                    
                    sh """
                        echo PROJECT ${PROJECT}
                        echo SCRIPT_PATH ${SCRIPT_PATH}
                        echo tgz ${PROJECT_PATH}_`date '+%Y%m%d'`.tgz
                        echo $SECRET
                        echo "do tmux"
                        #ssh root@${params.验证环境} "apt update;apt install pigz wget telnet net-tools tmux -y;cd /tmp ;wget -c  172.16.40.245:82/deploy.sh  ; bash /tmp/deploy.sh  ${SCRIPT_PATH} ${PROJECT} ${SECRET}"
                        
                      """
                }
            }
        }
        //拉取安装包
        // stage('Push OSS') {
        //     steps {
        //         echo "---------------------------------------------------------------------------------"
        //         echo "Starting push Application to OSS......"
        //         echo "---------------------------------------------------------------------------------"
        //         script {
        //             sh """
        //                 ossutil64 cp /pkg/publish_pkg/${PROJECT}/${VERSION}/${APP_NAME}_`date '+%Y%m%d'`.tgz oss://tech-deepwise/02-Dr.Wise_CTA/${VERSION}/
        //                 #ossutil64 cp /pkg/publish_pkg/${PROJECT}/${VERSION}/${APP_NAME}_`date '+%Y%m%d'`.txt oss://tech-deepwise/02-Dr.Wise_CTA/${VERSION}/
        //               """
        //         }
        //     }
        // }
    }
}

/srv/judge.sh

#!/bin/bash
# $1 是120上测试打出包的地址192.168.10.120:/data1/PublishPackage/aiserverconfig/aiserverconfig_1130_211203.tgz
# $2 是需要同步的目录名称  即包名称

# 加入判断传入参数是否为2个
# 判断url是否正确
#if [[ "$1" != "http?(s)://*.tgz" ]] ; then  echo "Invalid URL"  ;else  echo "待同步的url为: $1";fi
if [[ `echo $1  |grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:/.*.tgz$"` ]];then echo "url地址正常 即将同步$1 "  ;else  echo "URL异常!!!请检查输入的url地址是否有空格和中文符号" && exit 1;fi

cd $2
# 判断包的名称
if echo $1 | grep  'aiserver_'; then     echo "Package is aiserver: $1" &&   rm -r $2/aiserver_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'aiserverconfig'; then     echo "Package is aiserverconfig: $1" && rm -r aiserverconfig_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'pacsserver'; then     echo "Package is pacsserver: $1" && rm -r pacsserver_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'platform'; then     echo "Package is platform: $1" && rm -r platform_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'CTA_Client_Deploy'; then     echo "Package is CTA_Client_Deploy: $1" && rm -r CTA_Client_Deploy_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'lung'; then     echo "Package is lung: $1" && rm -r lung_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'aiviewer'; then     echo "Package is aiviewer: $1" && rm -r aiviewer_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'dwcta'; then     echo "Package is dwcta: $1" && rm -r dwcta_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'svrshell'; then     echo "Package is svrshell: $1" && rm -r svrshell_*  && rsync -avP $1  $2 ; fi
if echo $1 | grep  'aiclassifier'; then     echo "Package is aiclassifier: $1" && rm -r aiclassifier_*  && rsync -avP $1  $2 ; fi

deploy.sh

#!/bin/bash
mkdir /data1
apt update;apt install pigz wget telnet net-tools tmux -y
#tmux new-session -d -s htop-session "wget -c  ; openssl des3 -d -k $2 -salt -in CTA_0930fix_20211202.tgz | tar --use-compress-program=pigz -xvpf -"
tmux new-session -d -s "install_session"
tmux send-keys -t "install_session:0" "cd /data1 ;wget -c http://192.168.10.121:20150/publish_pkg/$1 ;openssl des3 -d -k \"$3\" -salt -in $4 | tar --use-compress-program=pigz -xvpf - ;cd $2  ;yes  yes | bash 01-CTA-env.sh ;echo -n `dmidecode -t 1|grep 'UUID'|awk '{print \"secret\"\$2\"secret\"}'` |md5sum |awk '{print \$1}' > /etc/sign.conf ;bash 02-CTA-lung-install.sh" Enter
#tmux new-session -d -s htop-session "cd /data1 ;wget -c http://192.168.10.121:20150/publish_pkg/$1 ;openssl des3 -d -k '\$3' -salt -in CTA_0930fix_20211202.tgz | tar --use-compress-program=pigz -xvpf - ;cd $2  ;yes  yes | bash 01-CTA-env.sh ;echo -n `dmidecode -t 1|grep 'UUID'|awk '{print \"secret\"\$2\"secret\"}'` |md5sum |awk '{print \$1}' > /etc/sign.conf ;bash 02-CTA-lung-install.sh"
# tmux new-session -d -s htop-session "cd /data1 ;cd CTA_0930  ;yes  yes | bash 01-CTA-env.sh ;echo -n `dmidecode -t 1|grep 'UUID'|awk '{print \"secret\"\$2\"secret\"}'` |md5sum |awk '{print \$1}' > /etc/sign.conf ;bash 02-CTA-lung-install.sh"
#tmux new-session -d -s htop-session "cd /data1 ;wget -c http://192.168.10.121:20150/publish_pkg/${PROJECT}/${VERSION}/${PROJECT_PATH}_`date '+%Y%m%d'`.tgz  ;openssl des3 -d -k 'G^M136m#' -salt -in CTA_0930fix_20211202.tgz | tar --use-compress-program=pigz -xvpf - ;cd CTA_0930fix  ;yes  yes | bash 01-CTA-env.sh ;echo -n `dmidecode -t 1|grep 'UUID'|awk '{print \"secret\"\$2\"secret\"}'` |md5sum |awk '{print \$1}' > /etc/sign.conf ;bash 02-CTA-lung-install.sh"



#tmux new-session -d -s "install_session"
#tmux send-keys -t "new-session:0" "xxxxx" Enter

补充

tmux new-session -d -s htop-session 'top';  # start new detached tmux session, run htop
tmux ls 
tmus a -t htop-session
#tmux split-window;                             # split the detached tmux session
#tmux send 'top' ENTER;                     # send 2nd command 'htop -t' to 2nd pane. I believe there's a `--#target` option to target specific pane.
#tmux a;                                        # open (attach) tmux session.

执行01的时候输入yes 密钥也会变成yes

# 这里需要判断cta还是drwise么
yes  yes | ./01-CTA-env.sh

输入密钥

echo -n `dmidecode -t 1|grep 'UUID'|awk '{print "secret"$2"secret"}'` |md5sum |awk '{print $1}' > /etc/sign.conf

手动恢复快照 这个需要通过命令去获取虚拟机的id和快照id

./t.1.sh  验证环境-192.168.10.84 虚拟机快照2022-01-12
 
 # 84
 vim-cmd vmsvc/snapshot.revert   54  3 suppressPowerOn
 # 81   
 51 7
 # 82
 49  5
 # 83
 52 3
 # 85
 55 3

虚拟机快照恢复

判断是否能ping通 可以加入关机等操作

if   ping -c 1 -w 1 192.168.10.82 > /dev/null ; then  echo "ping success";else   echo "ping failed" &&  echo exit ; fi

在exsi上关闭虚拟机

vim-cmd vmsvc/power.off  54

判断url是否为tgz的url

if [[ $1 != http?(s)://*.tgz ]]; then  echo "Invalid URL"  ;else  echo "待同步的url为:$1";fi

防止tmux 执行完成之后直接退出了 在tmux中无法找到

tmux new-session -d -s "new_name"
tmux send-keys -t "new_name:0" "python my file.py" Enter
# https://www.thinbug.com/q/50988416