groovy 脚本实例 升级数据库脚本

groovy 脚本实例 升级数据库脚本

升级数据库脚本

经常需要根据数据库类型,数据库个数,种子文件来源等升级数据库,人工操作比较麻烦,升级数据库仅供参考

package feature
node('ecs_wuhan_docker') {

    /** 参数部分:
     * 这部分是自己定义的参数,根据生产线不通,需要根据情况进行修改
     */
    // 定义mysql的IP地址
    def mysqlSshIp=MYSQL_IP
    // 定义登陆mysql时的账号
    def mysqlAdminUser=MYSQL_USER
    // 定义登陆mysql时的密码
    def mysqlAdminPassword=MYSQL_PASS
    // 定义mysql的类型
    def helperDialect= "mysql"
    // 定义登陆jfrog拉取文件的账号密码
    def jfrogCredentials = 'ecs_read:ecs_read'
    // 定义tag的名称
    def tagVersion=TAG_VERSION
    // 定义数据库schemaName,ENVIRONMENT_NAME是cicd模板参数传入
    def schemaName = SCHEMA_NAME
    def schemaNameConsole=SCHEMA_NAME
    def schemaNameEcs=SCHEMA_NAME_ECS
    def upgradeConsole="http://192.168.48.96:8081/artifactory/ecs2_java_prod/tags/$tagVersion/release/console/backend/metadata.zip"
    def upgradeEcs="http://192.168.48.96:8081/artifactory/ecs2_java_prod/tags/$tagVersion/release/ecs/backend/metadata.zip"
    def upgradeFile="http://192.168.48.96:8081/artifactory/ecs2_java_prod/tags/$tagVersion/release/upgrade/upgrade.zip"
    def hotfixupgradeConsole="http://192.168.48.96:8081/artifactory/ecs2_java_dev/SNAPSHOT/$tagVersion/java-ecs2/metadata/console-metadata/metadata.zip"
    def hotfixupgradeEcs="http://192.168.48.96:8081/artifactory/ecs2_java_dev/SNAPSHOT/$tagVersion/java-ecs2/metadata/ecs-metadata/metadata.zip"
    def hotfixupgradeFile="http://192.168.48.96:8081/artifactory/ecs2_java_dev/SNAPSHOT/$tagVersion/java-ecs2/upgrade/upgrade.zip"
    // 定义配置文件名称
    def upDbConfigFile = 'application.properties'
    // 定义数据库driver
    def dbDriver = 'com.mysql.jdbc.Driver'
    // 定义初始化的jdbc,这里不用修改
    def dbUrl = "jdbc:mysql:\\/\\/127.0.0.1:3306\\/e7fssc?useUnicode=true\\&characterEncoding=UTF-8\\&useSSL=false\\&allowMultiQueries=true\\&serverTimezone=Asia\\/Shanghai"

    /** 参数部分:
     * 这部分是cicd平台参数数传入,一般不需要修改
     */
    // 定义buildNumber,BUILD_NUMBER是jenkins默认参数,cicd模板参数导入
    def buildNumber=BUILD_NUMBER
    // 定义微信群通知的keyWX_KEY是cicd模板参数导入
    def wxKey = WX_KEY
    // 定义数据库类型
    def updateType = "mysql"
    // 定义proposerName,PROPOSER_NAME是cicd模板参数导入
    def proposerName=CURRENT_OPERATOR_MOBILE
    // 定义,cicd的地址,cicd模板参数导入
    def cicdServerUrl=CICD_SERVER_URL
    // 定义proposerMobile,PROPOSER_MOBILE是手机号码,cicd模板参数导入
    def proposerMobile=CURRENT_OPERATOR_MOBILE
    // 定义Jobname,cicd模板参数导入
    def jobName=JOB_NAME
    def diffOrSame = DIFF_OR_SAME
    def updateFromType=UPDATE_FROM_TYPE

    try {
        stage('wx notice start') {
            deleteDir()
        }
        stage('build') {
            build(isWithMicro = false,helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaName,mysqlSshIp,jfrogCredentials,upgradeEcs,upgradeConsole,upgradeFile,updateType,diffOrSame,updateFromType,hotfixupgradeConsole,hotfixupgradeEcs,hotfixupgradeFile,schemaNameConsole,schemaNameEcs)
        }
        stage('wx notice start') {
            wxNotice("'${proposerName}'升级数据库:基于'${tagVersion}'分支  升级成功!",wxKey,proposerMobile)
        }
    } catch (e) {
        wxFailNotice(wxKey,proposerName,jobName,buildNumber,cicdServerUrl)
        throw e
    } finally {
    }
}

def wxFailNotice(wxKey,proposerName,jobName,buildNumber,cicdServerUrl) {
    def text = "<font color=info>【${proposerName}的${jobName}】</font>初始化数据库<font color=warning>失败</font>\\n >[查看控制台](${cicdServerUrl}'/pipeline/job/consolesHTML/'${jobName}'/'${buildNumber})"
    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 flushredisdb(productLine,environmentName,rancherAddress,rancherToken,rancherProjectId) {
    sh """
        rancher login ${rancherAddress} --token ${rancherToken} --skip-verify --context ${rancherProjectId}
        POD=\$(rancher kubectl get pods --namespace=${productLine}| grep ${productLine}-${environmentName}|awk '{print \$1}');
        rancher kubectl exec -it \$POD -c redis -n ${productLine} -- redis-cli -h 127.0.0.1 flushall
    """
}

def build(isWithMicro = false,helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaName,mysqlSshIp,jfrogCredentials,upgradeEcs,upgradeConsole,upgradeFile,updateType,diffOrSame,updateFromType,hotfixupgradeConsole,hotfixupgradeEcs,hotfixupgradeFile,schemaNameConsole,schemaNameEcs) {
    stage('build java') {
        if (updateType == "mysql" && diffOrSame== "same" && updateFromType == "release")  {
            mavenBuildProjectMysql(helperDialect, dbDriver, dbUrl, mysqlAdminUser, mysqlAdminPassword, upDbConfigFile, schemaName, mysqlSshIp, jfrogCredentials, upgradeEcs, upgradeConsole, upgradeFile)
        }
        if (updateType == "mysql" && diffOrSame== "same" && updateFromType == "hotfix")  {
            mavenBuildProjectMysql(helperDialect, dbDriver, dbUrl, mysqlAdminUser, mysqlAdminPassword, upDbConfigFile, schemaName, mysqlSshIp, jfrogCredentials, hotfixupgradeEcs, hotfixupgradeConsole, hotfixupgradeFile)
        }
        if (updateType == "mysql" && diffOrSame== "diff" && updateFromType == "release")  {
            print ("diff release")
            mavenBuildProjectMysqlConsole(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaNameConsole,mysqlSshIp,jfrogCredentials,upgradeConsole,upgradeFile)
            mavenBuildProjectMysqlEcs(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaNameEcs,mysqlSshIp,jfrogCredentials,upgradeEcs)
        }
        if (updateType == "mysql" && diffOrSame== "diff" && updateFromType == "hotfix")  {
            print ("diff hotfix")
            mavenBuildProjectMysqlConsole(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaNameConsole,mysqlSshIp,jfrogCredentials,hotfixupgradeConsole,hotfixupgradeFile)
            mavenBuildProjectMysqlEcs(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaNameEcs,mysqlSshIp,jfrogCredentials,hotfixupgradeEcs)
        }
    }
}

defmavenBuildProjectMysql(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaName,mysqlSshIp,jfrogCredentials,upgradeEcs,upgradeConsole,upgradeFile) {
    sh """
            curl -u ${jfrogCredentials} -O ${upgradeConsole}
            unzip metadata.zip -d console;rm -rf metadata.zip
            curl -u ${jfrogCredentials} -O ${upgradeEcs}
            unzip metadata.zip -d ecs;rm -rf metadata.zip
            cd ecs/;cp ../console/ecs/* ecs/
            pwd;ls -l;zip metadata.zip metadata.zip -r ecs/
            curl -u ${jfrogCredentials} -O ${upgradeFile};unzip upgrade.zip
            cd config; mv ../metadata.zip ./
            sed -i 's/url: jdbc.*/url: ${dbUrl}/g' ${upDbConfigFile}
            sed -i 's/dbType=.*/dbType='${helperDialect}'/g' ${upDbConfigFile}
            sed -i \"s/spring.datasource.url=.*/spring.datasource.url=jdbc:mysql:\\/\\/${mysqlSshIp}\\/${schemaName}?useUnicode=true\\&characterEncoding=UTF-8\\&useSSL=false\\&allowMultiQueries=true\\&serverTimezone=Asia\\/Shanghai/g\" ${upDbConfigFile}
            sed -i 's/spring.datasource.username=.*/spring.datasource.username='${mysqlAdminUser}'/g' ${upDbConfigFile}
            sed -i 's/spring.datasource.password=.*/spring.datasource.password='${mysqlAdminPassword}'/g' ${upDbConfigFile}
            sed -i 's/spring.datasource.driverClassName=.*/spring.datasource.driverClassName='${dbDriver}'/g' ${upDbConfigFile}
            sed -i 's/server.port=.*/server.port=8888/g' ${upDbConfigFile}
            java -Xmx1024m -Xms512m -Dfile.encoding=utf-8 -jar ecs-upgrade-app.jar || echo 'skip step1'
       """
}

defmavenBuildProjectMysqlConsole(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaNameConsole,mysqlSshIp,jfrogCredentials,upgradeConsole,upgradeFile) {
    sh """
            curl -u ${jfrogCredentials} -O ${upgradeConsole}
            curl -u ${jfrogCredentials} -O ${upgradeFile};unzip upgrade.zip
            cd config; mv ../metadata.zip ./
            sed -i 's/url: jdbc.*/url: ${dbUrl}/g' ${upDbConfigFile}
            sed -i 's/dbType=.*/dbType='${helperDialect}'/g' ${upDbConfigFile}
            sed -i \"s/spring.datasource.url=.*/spring.datasource.url=jdbc:mysql:\\/\\/${mysqlSshIp}\\/${schemaNameConsole}?useUnicode=true\\&characterEncoding=UTF-8\\&useSSL=false\\&allowMultiQueries=true\\&serverTimezone=Asia\\/Shanghai/g\" ${upDbConfigFile}
            sed -i 's/spring.datasource.username=.*/spring.datasource.username='${mysqlAdminUser}'/g' ${upDbConfigFile}
            sed -i 's/spring.datasource.password=.*/spring.datasource.password='${mysqlAdminPassword}'/g' ${upDbConfigFile}
            sed -i 's/spring.datasource.driverClassName=.*/spring.datasource.driverClassName='${dbDriver}'/g' ${upDbConfigFile}
            sed -i 's/server.port=.*/server.port=8888/g' ${upDbConfigFile}
            java -Xmx1024m -Xms512m -Dfile.encoding=utf-8 -jar ecs-upgrade-app.jar || echo 'skip step1'
       """
}
def mavenBuildProjectMysqlEcs(helperDialect,dbDriver,dbUrl,mysqlAdminUser,mysqlAdminPassword,upDbConfigFile,schemaNameEcs,mysqlSshIp,jfrogCredentials,upgradeEcs) {
    sh """
            ls 
            cd config; curl -u ${jfrogCredentials} -O ${upgradeEcs}
            sed -i 's/url: jdbc.*/url: ${dbUrl}/g' ${upDbConfigFile}
            sed -i 's/dbType=.*/dbType='${helperDialect}'/g' ${upDbConfigFile}
            sed -i \"s/spring.datasource.url=.*/spring.datasource.url=jdbc:mysql:\\/\\/${mysqlSshIp}\\/${schemaNameEcs}?useUnicode=true\\&characterEncoding=UTF-8\\&useSSL=false\\&allowMultiQueries=true\\&serverTimezone=Asia\\/Shanghai/g\" ${upDbConfigFile}
            sed -i 's/spring.datasource.username=.*/spring.datasource.username='${mysqlAdminUser}'/g' ${upDbConfigFile}
            sed -i 's/spring.datasource.password=.*/spring.datasource.password='${mysqlAdminPassword}'/g' ${upDbConfigFile}
            sed -i 's/spring.datasource.driverClassName=.*/spring.datasource.driverClassName='${dbDriver}'/g' ${upDbConfigFile}
            sed -i 's/server.port=.*/server.port=8888/g' ${upDbConfigFile}
            java -Xmx1024m -Xms512m -Dfile.encoding=utf-8 -jar ecs-upgrade-app.jar || echo 'skip step1'
       """
}
posted @ 2022-04-14 23:04  liwenchao1995  阅读(217)  评论(0编辑  收藏  举报