groovy 脚本实例 创建环境

groovy 脚本实例 创建环境

创建环境

package platform.branch_feature.mysql_docker_db

node('ecs_wuhan_docker') {
    println "${BUILD_URL}console"
    def xxlJob = ENVIRONMENT_NAME
    def xxlJobIp = "192.168.12.187:9094"

    //ecs2.0-jenkins通知群
    def wxKey = '79bc4571-2835-4458-9341-043ab3769436'
    //失败信息通知运维处理人,李文超
    def noticeTelNumber = '18201292571'

    def jfrogBaseAddress = 'http://ecs_read:ecs_read@192.168.48.96:8081/artifactory'

    def harborProject = 'ecs2-dev'
    def nginxImage = "${HARBOR_ADDRESS}/${harborProject}/${ENVIRONMENT_NAME}-web:latest"
    def javaImageConsole = "${HARBOR_ADDRESS}/${harborProject}/${ENVIRONMENT_NAME}-console-jar:latest"
    def javaImageEcs = "${HARBOR_ADDRESS}/${harborProject}/${ENVIRONMENT_NAME}-ecs-jar:latest"

    def nodeSelector = 'ecs2'

    // 定义jdbc相关信息
    def dbDriver = 'com.mysql.jdbc.Driver'
    def jdbcParam = "useUnicode=true\\&characterEncoding=UTF-8\\&useSSL=false\\&allowMultiQueries=true\\&rewriteBatchStatements=true\\&serverTimezone=GMT\\%2B8"
    def consoleDbUrl = "jdbc:mysql:\\/\\/mysql:3306\\/yn_console?${jdbcParam}"
    def ecsDbUrl = "jdbc:mysql:\\/\\/mysql:3306\\/yn_ecs?${jdbcParam}"
    def helperDialect = "mysql"
    def mysqlAdminUser = "ecs_dev"
    def mysqlAdminPassword = "ecs_dev"

    def branch = ENVIRONMENT_BRANCH

    try {
        stage('check') {
            deleteDir()
            if(branch == 'dev'){
                wxNotice("此流水线不允许申请dev分支", wxKey, PROPOSER_MOBILE)
                throw new Exception('此流水线不允许申请dev分支')
            }
        }
        
        stage('wx notice start') {
            wxNotice("'${PROPOSER_NAME}'申请feature环境:  '${ENVIRONMENT_NAME}'  基于'${ENVIRONMENT_BRANCH}'分支  开始创建!", wxKey, PROPOSER_MOBILE)
        }
        parallel(
                'db': {
                    stage('create schema on mysql') {
                        retry(5){
                            checkNamespace()
                        }
                        createMysqlK8s(nodeSelector)
                        rancherDeployMysql()
                        updateSystemInfo(branch)
                    }
                },
                'java': {
                    stage('package java') {
                        cloneProjectBranch(['ynbase', 'inf', 'ecs2'], 'ecs-platform', branch, 'jenkins')
                        javaBuild()
                    }
                    parallel(
                            'java-console': {
                                stage('make console image') {
                                    print("compile_java_upgrade console")
                                    dir("image_console_dir") {
                                        sh 'rm -rf console.jar application-dev.yml'
                                        sh 'cp ../ecs2/custom/console-custom/console-custom-web/target/console.jar ./'
                                        sh 'cp ../ecs2/custom/console-custom/console-custom-web/src/main/resources/application-dev.yml ./'
                                        def configFile = "application-dev.yml"
                                        fixJavaConfigDataSource(configFile, dbDriver, consoleDbUrl, mysqlAdminUser, mysqlAdminPassword, helperDialect)
                                        fixJavaConfigRedis(configFile)
                                        fixJavaConfigYn(configFile, xxlJobIp, xxlJob + '-console')
                                        fixEcsUrl(configFile)
                                        makeJavaImageDockerfile('console.jar', '3g', '1g', '2g', '1g')
                                        dockerPushImage(javaImageConsole)
                                    }
                                }
                            },
                            'java-ecs': {
                                stage('make ecs image') {
                                    dir("image_ecs_dir") {
                                        sh 'rm -rf ecs.jar application-dev.yml'
                                        sh 'cp ../ecs2/custom/ecs-custom/ecs-custom-web/target/ecs.jar ./'
                                        sh 'cp ../ecs2/custom/ecs-custom/ecs-custom-web/src/main/resources/application-dev.yml ./'
                                        def configFile = "application-dev.yml"
                                        fixJavaConfigDataSource(configFile, dbDriver, ecsDbUrl, mysqlAdminUser, mysqlAdminPassword, helperDialect)
                                        fixJavaConfigRedis(configFile)
                                        fixJavaConfigYn(configFile, xxlJobIp, xxlJob + '-ecs')
                                        fixConsoleUrl(configFile)
                                        makeJavaImageDockerfile('ecs.jar', '4g', '1g', '2g', '1g')
                                        dockerPushImage(javaImageEcs)
                                    }
                                }
                            },
                    )
                },
                'web': {
                    stage('make web image') {
                        dir("image_web_dir") {
                            for (item in ['designer', 'ecs-console', 'ecs_console', 'fssc-web', 'ecs-web', 'ecs-console-mobile', 'ecs-console-mobile-vue', 'ecs-mobile']) {
                                print("download web html ${item}")
                                sh "curl -O ${jfrogBaseAddress}/ecs2_java_dev/latest/feature-zip/${branch}/${item}.zip"
                                sh "rm -rf ${item};unzip ${item}.zip || echo 'skip step'"
                                sh "tar rvf ecs.tar.gz  ${item} || echo 'skip step'"
                            }
                            makeNginxImageDockerFile('ecs.tar.gz')
                            dockerPushImage(nginxImage)
                        }
                    }
                })
        stage('create kubernetes.yml') {
            createKubernetesDeployment(nodeSelector, nginxImage, javaImageConsole, javaImageEcs)
            rancherDeploy()
        }

        stage('wx notice start') {
            wxNotice("'${PROPOSER_NAME}'申请环境:  '${ENVIRONMENT_NAME}'  基于'${ENVIRONMENT_BRANCH}'分支  创建成功!", wxKey, PROPOSER_MOBILE)
        }
    } catch (e) {
        wxFailNotice(wxKey, noticeTelNumber)
        throw e
    } finally {
        deleteDir()
    }
}

def wxFailNotice(wxKey, noticeTelNumber) {
    def proposerName = PROPOSER_NAME
    def jobName = JOB_NAME
    def text = "<font color=info>【${proposerName}的${jobName}】</font>任务执行<font color=warning>失败!!!</font>\\n >[查看控制台](${BUILD_URL}console)"
    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":["${noticeTelNumber}"] } }'"""
}

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 javaBuild() {
    stage('build java') {
        if (isDevPomVersion()) {
            throw new Exception("feature分支不允许编译dev版本号代码")
        }
        def deployBuild = ['ynbase/mbase', 'ynbase/mbase-micro', 'inf/com.yuanian.infrastructure']
        mavenBuildProject(deployBuild, 'package install deploy')
        //取消mvn快照拉取策略
        dir('ecs2/ecs') {
            sh "find ./ -maxdepth 1 -name pom.xml -exec sed -i 's/enabled>true</enabled>false</g' {} \\;"
        }
        def installBuild = ['ecs2/ecs']
        mavenBuildProject(installBuild, 'package install')
        dir('ecs2/custom') {
            sh "find ./ -maxdepth 2 -name pom.xml -exec sed -i 's/enabled>true</enabled>false</g' {} \\;"
        }
        def packageBuild = ['ecs2/custom/console-custom', 'ecs2/custom/ecs-custom']
        mavenBuildProject(packageBuild, 'package')
    }
}

def mavenBuildProject(paths = [], mavenCmd) {
    if (paths == null || paths.size() == 0) {
        return
    }
    sh 'mkdir -p /tmp/.m2/repository'
    withDockerContainer(args: "-v /tmp/.m2/repository:/tmp/.m2/repository:rw,z", image: 'maven:3.6.3-openjdk-8') {
        configFileProvider([configFile(fileId: 'maven-setting2', variable: 'MAVEN_SETTINGS')]) {
            for (path in paths) {
                dir(path) {
                    println "def mavenBuildProject path: ${path} mavenCmd:${mavenCmd}"
                    sh "mvn -q -s $MAVEN_SETTINGS ${mavenCmd} -Dmaven.test.skip=true"
                }
            }
        }
    }
}

def fixJavaConfigDataSource(configFile, dbDriver, dbUrl, dbName, dbPassword, helperDialect) {
    sh "sed -i 's/helperDialect: .*/helperDialect: ${helperDialect}/g' ${configFile}"
    //不增加jdbc 可能误修改console-service-url
    sh "sed -i 's/url: jdbc.*/url: ${dbUrl}/g' ${configFile}"
    sh "sed -i 's/driver-class-name: .*/driver-class-name: ${dbDriver}/g' ${configFile}"
    sh "sed -i 's/username: .*/username: ${dbName}/g' ${configFile}"
    sh "sed -i 's/password: .*/password: ${dbPassword}/g' ${configFile}"
}

def fixJavaConfigYn(configFile, xxlJobIp, xxlJob) {
    sh "sed -i 's/nameServers: .*/nameServers: 192.168.61.54:9876/g' ${configFile}"
    sh "sed -i 's/addresses.*/addresses: http:\\/\\/'${xxlJobIp}'\\/xxl-job-admin/g' ${configFile}"
    sh "sed -i 's/prefix: .*/prefix: ${ENVIRONMENT_NAME}/g' ${configFile}"
    sh "sed -i 's/appname:.*/appname: ${xxlJob}/g' ${configFile}"
    sh "sed -i 's/businessid: .*/businessid: ${xxlJob}/g' ${configFile}"
    sh "sed -i 's/title:.*/title: ${xxlJob}/g' ${configFile}"
    sh "sed -i 's/fastjsonType:.*/fastjsonType: fastjson2/g' ${configFile}"

}

def fixConsoleUrl(configFile) {
    sh "sed -i 's/console:8095/java-console:8095/g' ${configFile}"
}

def fixEcsUrl(configFile) {
    sh "sed -i 's/masterdata-service-url:.*/masterdata-service-url: http:\\/\\/java-ecs:8096\\//g' ${configFile}"
}

def fixJavaConfigRedis(configFile) {
    sh "sed -i 's/host: .*/host: redis/g' ${configFile}"
    sh "sed -i 's/database: .*/database: 0/g' ${configFile}"
    sh "sed -i 's/port: \${redis_port:.*/port: 6379/g' ${configFile}"
    sh "sed -i '0,/password:.*/s/password:.*/password: /' ${configFile}"
}

def makeJavaImageDockerfile(jarName, xmx, xms, maxMetaspaceSize, metaspaceSize) {
    String text = """FROM 192.168.48.90:80/ecs-component/ecs-openjre:8
MAINTAINER ecs@yuanian.com
LABEL description="ecs-micro"
COPY ${jarName} /app.jar
COPY application-dev.yml /application-dev.yml
ENV JAVA_OPT="-Xmx${xmx} -Xms${xms} -XX:MaxMetaspaceSize=${maxMetaspaceSize} -XX:MetaspaceSize=${metaspaceSize} -Dfile.encoding=utf-8"
CMD java  -Dapp.log.projectName=ledger -Dapp.log.serviceName=${ENVIRONMENT_NAME} \${JAVA_OPT} -jar app.jar
"""
    println text
    writeFile file: 'Dockerfile', text: text, encoding: "UTF-8"
}

def makeNginxImageDockerFile(tarName) {
    createNginxConf()
    String text = """FROM nginx:1.17.0
MAINTAINER ecs@yuanian.com
LABEL description="ecs-micro"
ADD ${tarName} /usr/share/nginx/html
COPY nginx.conf /etc/nginx
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/\$TZ  /etc/localtime && echo \$TZ > /etc/timezone
CMD ["nginx", "-g", "daemon off;"]
"""
    println text
    writeFile file: 'Dockerfile', text: text, encoding: "UTF-8"
}

def dockerPushImage(image_name) {
    sh """
    docker login -u ${HARBOR_USERNAME} -p ${HARBOR_PASSWORD} http://${HARBOR_ADDRESS}
    docker build -t ${image_name} .
    docker push ${image_name}
    docker rmi -f ${image_name}
    """
}

def updateSystemInfo(branch) {
    def sysName = branch.replace('feature','f')
    def mysqlPodName = sh(returnStdout: true, script: "rancher kubectl get pods -n ${ENVIRONMENT_NAME} | grep mysql | awk '{print \$1}'").trim()
    sh "echo 'ecs_dev'|rancher kubectl exec -it ${mysqlPodName} -c mysql -n ${ENVIRONMENT_NAME} -- " +
            "mysql -u ecs_dev -p -D yn_console " +
            "-e \"update TP_I18N_DYNAMIC set TEXT = '${sysName}' where I18N_DYNAMIC_ID = '11ecd7529b36e9e2a2f05f35645fa306'\""
}

def rancherDeployMysql() {
    try {
        sh """
        rancher kubectl apply -f mysql-k8s.yml
        rancher kubectl rollout status StatefulSet mysql -n ${ENVIRONMENT_NAME}
        """
    } catch (e) {
        throw e
    }
}

def rancherDeploy() {
    try {
        sh """
        rancher kubectl create -f k8s-Deployment.yml
        """
    } catch (e) {
        shNotThrowError("rancher kubectl delete --all deployment -n ${ENVIRONMENT_NAME}")
        throw e
    }
    createKubernetesSvc()
    try {
        sh "rancher kubectl create -f k8s-svc.yml"
    } catch (e) {
        shNotThrowError("rancher kubectl delete --all deployment -n ${ENVIRONMENT_NAME}")
        shNotThrowError("rancher kubectl delete --all svc -n ${ENVIRONMENT_NAME}")
        shNotThrowError("rancher kubectl delete --all ingress -n ${ENVIRONMENT_NAME}")
        throw e
    }
}

def shNotThrowError(shell) {
    try {
        sh shell
    } catch (ignored) {
        println 'not throw exception'
    }
}

def upgradeDb(dbDriver, dbUrl, dbName, dbPassword, helperDialect) {
    //server.port=8089
    //dbType=oracle
    //spring.datasource.url=jdbc:oracle:thin:@192.168.12.104:1521:yndev
    //spring.datasource.username=ecs_md
    //spring.datasource.password=ecs_md
    //spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
    def configFile = "application.properties"
    sh "sed -i 's/dbType=.*/dbType=${helperDialect}/g' ${configFile}"
    sh "sed -i 's/server.port=.*/server.port=8888/g' ${configFile}"
    sh "sed -i 's/spring.datasource.url=.*/spring.datasource.url=${dbUrl}/g' ${configFile}"
    sh "sed -i 's/spring.datasource.username=.*/spring.datasource.username=${dbName}/g' ${configFile}"
    sh "sed -i 's/spring.datasource.password=.*/spring.datasource.password=${dbPassword}/g' ${configFile}"
    sh "sed -i 's/spring.datasource.driverClassName=.*/spring.datasource.driverClassName=${dbDriver}/g' ${configFile}"
    withDockerContainer(image: '192.168.48.90:80/ecs-component/ecs-openjre:8') {
        sh "java -Xmx2g -Xms1g -Dfile.encoding=utf-8 -jar ecs-upgrade-app.jar"
    }
}

def cloneProjectBranch(projectNames, group, branch, userName) {
    withCredentials([usernamePassword(credentialsId: 'ecs_git', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
        def encodedPass = URLEncoder.encode(PASS, "UTF-8")
        Map parallelMap = [:]
        for (item in projectNames) {
            def projectName = item
            parallelMap.put(projectName, {
                sh "git clone --depth=1 -b ${branch} https://${USER}:${encodedPass}@192.168.48.50/${group}/${projectName}.git ${projectName}"
            })
        }
        parallel(parallelMap)
    }
}

def isHasFeature(featureName) {
    String featureCount = sh(returnStdout: true, script: "git ls-remote|grep .*/${featureName}\$ | wc -l").trim()
    return Integer.parseInt(featureCount) > 0
}

def isDevPomVersion() {
    String count = sh(returnStdout: true, script: "find ./ -maxdepth 4 -name pom.xml | xargs grep 'ECS2-dev-SNAPSHOT' |wc -l").trim()
    return Integer.parseInt(count) > 0
}

def checkNamespace(){
    sleep time: 1, unit: 'MINUTES'
    String updateMsg = sh(returnStdout: true, script: "rancher kubectl get namespace|grep ${ENVIRONMENT_NAME}").trim()
    println updateMsg
    if (updateMsg.contains(ENVIRONMENT_NAME)) {
        println "${ENVIRONMENT_NAME}命名空间已经创建成功!"
    } else {
        throw new Exception(updateMsg)
    }
}

def createMysqlK8s(NODE_SELECTOR){
    String text = """
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: ${ENVIRONMENT_NAME}
data:
  my.cnf: |
    [client]
    default-character-set   = utf8mb4
    [mysql]
    default-character-set   = utf8mb4
    [mysqld]
    pid-file                = /var/run/mysqld/mysqld.pid
    socket                  = /var/run/mysqld/mysqld.sock
    datadir                 = /var/lib/mysql
    character-set-server    = utf8mb4
    collation-server        = utf8mb4_unicode_ci
    max_connections         = 5000
    default-time-zone       = '+8:00'
    sql_mode                = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES  
    transaction_isolation   = READ-COMMITTED
    max_allowed_packet      = 8g  
    innodb_log_buffer_size  = 167772160  
    innodb_log_file_size    = 1024M 
    innodb_strict_mode      = 0
    max_heap_table_size     = 512M
    wait_timeout            = 864000 
    interactive_timeout     = 864000
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: ${ENVIRONMENT_NAME}
spec:
  type: NodePort
  ports:
    - name: mysql-port
      protocol: TCP
      port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: ${ENVIRONMENT_NAME}
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: "mysql"
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      nodeSelector:
        productLine: ${NODE_SELECTOR}
      containers:
        - name: mysql
          image: 192.168.48.90:80/ecs2-dev/mysql-ecs-qa:latest
          imagePullPolicy: Always
          resources:
            requests:
              memory: 500Mi
            limits:
              memory: 2Gi
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: '123456'
          ports:
            - name: mysql-port
              containerPort: 3306
              protocol: TCP
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
            - name: config-volume
              mountPath: /etc/mysql/conf.d
          readinessProbe:
            tcpSocket:
              port: 3306
            initialDelaySeconds: 30
            periodSeconds: 30
            timeoutSeconds: 5
            failureThreshold: 40
      volumes:
        - name: mysql-persistent-storage
          hostPath:
            path: /data/mysql/${ENVIRONMENT_NAME}
            type: DirectoryOrCreate
        - name: config-volume
          configMap:
            name: mysql-config
"""
    println text
    writeFile file: 'mysql-k8s.yml', text: text, encoding: "UTF-8"
}

def createKubernetesDeployment(NODE_SELECTOR, NGINX_IMAGE, JAVA_IMAGE_CONSOLE, JAVA_IMAGE_ECS) {
    String text = """
apiVersion: v1
kind: PersistentVolume
metadata:
  name: ${ENVIRONMENT_NAME}-pv
  namespace: ${ENVIRONMENT_NAME}
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  nfs:
    path: /opt/fonts
    server: 192.168.48.33
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ${ENVIRONMENT_NAME}-pvc
  namespace: ${ENVIRONMENT_NAME}
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  storageClassName: nfs
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: ${ENVIRONMENT_NAME}
spec:
  selector:
    matchLabels:
      app: redis
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      nodeSelector:
        productLine: ${NODE_SELECTOR}
      containers:
        - name: redis
          image: 192.168.48.90:80/ecs-component/ecs-redis:6.0
          imagePullPolicy: Always
          ports:
            - name: redisport
              containerPort: 6379
          resources:
            requests:
              memory: 50Mi
            limits:
              memory: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: ${ENVIRONMENT_NAME}
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
        - name: logs
          emptyDir: {}
      nodeSelector:
        productLine: ${NODE_SELECTOR}
      containers:
        - name: nginx
          image: ${NGINX_IMAGE}
          imagePullPolicy: Always
          ports:
            - name: serviceport
              containerPort: 8080
          resources:
            requests:
              memory: 50Mi
            limits:
              memory: 150Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-console
  namespace: ${ENVIRONMENT_NAME}
spec:
  selector:
    matchLabels:
      app: java-console
  replicas: 1
  template:
    metadata:
      labels:
        app: java-console
    spec:
      volumes:
        - name: logs
          emptyDir: {}
        - name: myfonts
          persistentVolumeClaim:
            claimName: ${ENVIRONMENT_NAME}-pvc
      nodeSelector:
        productLine: ${NODE_SELECTOR}
      containers:
        - name: java-console
          image: ${JAVA_IMAGE_CONSOLE}
          imagePullPolicy: Always
          ports:
            - name: consoleport
              containerPort: 8095
          env:
            - name: JAVA_OPT
              value: -Xmx2g -Xms1g -XX:MaxMetaspaceSize=512m -XX:MetaspaceSize=127m
          resources:
            requests:
              memory: 1Gi
            limits:
              memory: 3Gi
          volumeMounts:
            - name: logs
              mountPath: /target/logs
            - name: myfonts
              mountPath: /usr/share/fonts/
          readinessProbe:
            tcpSocket:
              port: 8095
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 20
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-ecs
  namespace: ${ENVIRONMENT_NAME}
spec:
  selector:
    matchLabels:
      app: java-ecs
  replicas: 1
  template:
    metadata:
      labels:
        app: java-ecs
    spec:
      volumes:
        - name: logs
          emptyDir: {}
        - name: myfonts
          persistentVolumeClaim:
            claimName: ${ENVIRONMENT_NAME}-pvc
      nodeSelector:
        productLine: ${NODE_SELECTOR}
      containers:
        - name: java-ecs
          image: ${JAVA_IMAGE_ECS}
          imagePullPolicy: Always
          ports:
            - name: ecsport
              containerPort: 8096
          env:
            - name: JAVA_OPT
              value: -Xmx3g -Xms1g -XX:MaxMetaspaceSize=512m -XX:MetaspaceSize=127m
          resources:
            requests:
              memory: 1Gi
            limits:
              memory: 4Gi
          volumeMounts:
            - name: logs
              mountPath: /target/logs
            - name: myfonts
              mountPath: /usr/share/fonts/
          readinessProbe:
            tcpSocket:
              port: 8096
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 20
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cicd-callback
  namespace: ${ENVIRONMENT_NAME}
spec:
  selector:
    matchLabels:
      app: cicd-callback
  replicas: 1
  template:
    metadata:
      labels:
        app: cicd-callback
    spec:
      volumes:
        - name: logs
          emptyDir: {}
      nodeSelector:
        productLine: ${NODE_SELECTOR}
      containers:
        - name: cicd-callback
          image: 192.168.48.90:80/ecs-component/cicd-config-agent:1.1
          imagePullPolicy: Always
          env:
            - name: proxy_infos
              value: "[{host:'nginx',port:8080,contextPath:''}]"
          resources:
            requests:
              memory: 100Mi
            limits:
              memory: 256Mi
          ports:
            - name: configport
              containerPort: 8079
"""
    println text
    writeFile file: 'k8s-Deployment.yml', text: text, encoding: "UTF-8"

}

def createKubernetesSvc() {
    String text = """
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: ${ENVIRONMENT_NAME}
spec:
  type: NodePort
  ports:
    - name: serviceport
      port: 8080
  selector:
    app: nginx
---
apiVersion: v1
kind: Service
metadata:
  name: java-console
  namespace: ${ENVIRONMENT_NAME}
spec:
  type: NodePort
  ports:
    - name: consoleport
      port: 8095
  selector:
    app: java-console
---
apiVersion: v1
kind: Service
metadata:
  name: java-ecs
  namespace: ${ENVIRONMENT_NAME}
spec:
  type: NodePort
  ports:
    - name: ecsport
      port: 8096
  selector:
    app: java-ecs
---
apiVersion: v1
kind: Service
metadata:
  name: cicd-callback
  namespace: ${ENVIRONMENT_NAME}
spec:
  type: NodePort
  ports:
    - name: configport
      port: 8079
  selector:
    app: cicd-callback
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: ${ENVIRONMENT_NAME}
spec:
  type: NodePort
  ports:
    - name: redisport
      port: 6379
  selector:
    app: redis
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: "100M"
  name: ${ENVIRONMENT_NAME}-ingress
  namespace: ${ENVIRONMENT_NAME}
spec:
  rules:
    - host: ${ENVIRONMENT_NAME}.192.168.48.85.nip.io
      http:
        paths:
          - backend:
              serviceName: nginx
              servicePort: 8080
"""
    println text
    writeFile file: 'k8s-svc.yml', text: text, encoding: "UTF-8"
}

def createNginxConf() {
    String text = """
user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                  '\$status \$body_bytes_sent "\$http_referer" '
                  '"\$http_user_agent" "\$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    gzip on;
    gzip_types text/css
        application/javascript text/plain application/json application/x-javascript
        application/css application/xml application/xml+rss text/javascript
        application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;
    client_max_body_size 100M;

    upstream ecsSrv {
        server java-ecs:8096;
    }

    upstream ecs-console {
        server java-console:8095;
    }

    server {
        listen       8080;
        server_name  localhost;

        location = / {
            rewrite ^ \$scheme://\$http_host/ecs_console/index.html permanent;
        }
        location ~* \\.(css|js|html|map|gif|jpg|jpeg|png|ico|woff|woff2|svg|mp4|icon)\$ {
            root   /usr/share/nginx/html;
        }

        location ~ /console/ {
            rewrite ^/console/(.*)\$ /\$1 break;
            proxy_read_timeout 240s;
            proxy_pass http://ecs-console;
        }

        location ~ /(ecs|designer|businessobject|datadictionary|distribution|filemanagement|flow|masterdata|rule|treemanager)/ {
            rewrite ^/(ecs|designer|businessobject|datadictionary|distribution|filemanagement|flow|masterdata|rule|treemanager)?/(.*) /\$2 break;
            proxy_read_timeout 240s;
            proxy_pass http://ecsSrv;
        }

        location = /designer/ { index /designer/index.html; }
        location = /ecs-console/ { index /ecs-console/index.html; }
        location = /ecs_console/ { index /ecs_console/index.html; }
        location = /ecs-console-mobile/ { index /ecs-console-mobile/index.html; }
        location = /fssc-web/ { index /fssc-web/index.html; }
        location = /ecs-web/ { index /ecs-web/index.html; }
        location = /ecs-mobile/ { index /ecs-mobile/index.html; }
        location = /ecs-console-mobile-vue/ { index /ecs-console-mobile-vue/index.html; }

        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        real_ip_recursive on;
        keepalive_timeout  65;
    }
}
"""
    println text
    writeFile file: 'nginx.conf', text: text, encoding: "UTF-8"
}
posted @ 2022-09-26 20:54  liwenchao1995  阅读(116)  评论(0编辑  收藏  举报