目录
groovy 调整k8s的副本数定时任务
package platform
node('ecs_wuhan_docker') {
println "${BUILD_URL}console"
def wxKey = 'xxxxxx'
def proposerName = PROPOSER_NAME
def proposerMobile = PROPOSER_MOBILE
// pause,start
def type = OPT_TYPE
try {
stage('update pod') {
if (type == 'pause') {
changeWorkerNumbers(ENVIRONMENT_NAME, 0)
wxNotice("${proposerName}暂停:${ENVIRONMENT_NAME}", wxKey, proposerMobile)
executeSql("update T_ENVIRONMENT_MANAGER set ENVIRONMENT_STATUS = 'pause' where ENVIRONMENT_NAME = '${ENVIRONMENT_NAME}'")
} else {
changeWorkerNumbers(ENVIRONMENT_NAME, 1)
wxNotice("${proposerName}启动:${ENVIRONMENT_NAME}", wxKey, proposerMobile)
executeSql("update T_ENVIRONMENT_MANAGER set ENVIRONMENT_STATUS = 'running', UPDATE_DATE = NOW() where ENVIRONMENT_NAME = '${ENVIRONMENT_NAME}'")
}
}
} catch (e) {
wxFailNotice(wxKey, proposerName)
throw e
} finally {
println "${BUILD_URL}console"
deleteDir()
}
}
def executeSql(sql) {
def remote = [:]
remote.name = 'mysqlServer'
remote.host = "192.168.61.52"
remote.allowAnyHosts = true
remote.user = "root"
remote.password = "A09QXN2Ds1w5NDSN"
sshCommand remote: remote, failOnError: false, command: "mysql -uroot -pYuaNian@2999! -D ecs-cicd -e \"${sql}\""
}
def wxFailNotice(wxKey, proposerName) {
def text = "<font color=info>【${proposerName}的${JOB_NAME}】</font>修改副本数<font color=warning>失败!!!</font>\\n >[查看控制台](${CICD_SERVER_URL}'/pipeline/job/consolesHTML/'${jobName}'/'${BUILD_NUMBER})"
sh """curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='${wxKey}'' -H 'Content-Type: application/json' -d '{ "msgtype": "markdown", "markdown": { "content": "${text}", } }'"""
sh """curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='${wxKey}'' -H 'Content-Type: application/json' -d '{ "msgtype": "text", "text": { "mentioned_mobile_list":["18201292571"] } }'"""
}
def wxNotice(msg, wxKey, proposerMobile) {
sh """
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${wxKey}' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "${msg}",
"mentioned_mobile_list":["${proposerMobile}"]
}
}'
"""
}
def changeWorkerNumbers(environmentName, changeWorkNumber) {
sh "rancher kubectl scale --all StatefulSet -n ${environmentName} --replicas=${changeWorkNumber} || echo 'skip'"
sh "rancher kubectl scale --all deployment -n ${environmentName} --replicas=${changeWorkNumber}"
}
标签:groovy,副本,remote,NAME,text,wxKey,ENVIRONMENT,k8s,def
From: https://www.cnblogs.com/liwenchao1995/p/16732430.html