diff --git a/src/backend/core/build.gradle b/src/backend/core/build.gradle index a8253fd..2e90092 100644 --- a/src/backend/core/build.gradle +++ b/src/backend/core/build.gradle @@ -8,7 +8,7 @@ buildscript { dependencies { classpath "com.github.jengelman.gradle.plugins:shadow:5.1.0" - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30" classpath "org.jetbrains.kotlin:kotlin-allopen:1.3.21" } @@ -17,7 +17,10 @@ buildscript { svnkitVersion = "1.9.3" commonExecVersion = "1.3" jacksonVersion = "2.9.2" + jacksonDatabindVersion = "2.13.4.2" compressVersion = "1.15" + javaPluginSdkVersion = "1.1.5" + kotlinVersion = "1.5.30" } } @@ -80,22 +83,22 @@ allprojects { } dependencies { - compile "com.tencent.devops.ci-plugins:java-plugin-sdk:1.1.5" - compile files("lib/java-plugin-sdk-1.0.7.jar") - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + compile "com.tencent.devops.ci-plugins:java-plugin-sdk:${javaPluginSdkVersion}" + compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion" compile "org.bouncycastle:bcprov-jdk16:$bouncyCastleVersion" compile "org.apache.commons:commons-exec:$commonExecVersion" compile "com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion" compile "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$jacksonVersion" compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion" + compile "com.fasterxml.jackson.core:jackson-databind:$jacksonDatabindVersion" compile "org.apache.commons:commons-compress:$compressVersion" compile "org.hashids:hashids:1.0.3" compile group: 'commons-codec', name: 'commons-codec', version: '1.15' - compile("org.apache.commons:commons-compress:1.15") compile "org.tukaani:xz:1.2" testCompile group: 'junit', name: 'junit', version: '4.12' } + compileKotlin { kotlinOptions.jvmTarget = "1.8" } diff --git a/src/backend/core/src/main/kotlin/com/tencent/devops/docker/DockerRun.kt b/src/backend/core/src/main/kotlin/com/tencent/devops/docker/DockerRun.kt index d6e2cf2..c432f58 100644 --- a/src/backend/core/src/main/kotlin/com/tencent/devops/docker/DockerRun.kt +++ b/src/backend/core/src/main/kotlin/com/tencent/devops/docker/DockerRun.kt @@ -1,29 +1,25 @@ package com.tencent.devops.docker -import com.tencent.bk.devops.atom.pojo.Result -import com.tencent.bk.devops.plugin.api.impl.KubernetesBuildApi -import com.tencent.bk.devops.plugin.docker.CommonExecutor import com.tencent.bk.devops.plugin.docker.PcgDevCloudExecutor import com.tencent.bk.devops.plugin.docker.DockerApi -import com.tencent.bk.devops.plugin.docker.KubernetesExecutor -import com.tencent.bk.devops.plugin.docker.ThirdPartExecutor -import com.tencent.bk.devops.plugin.docker.exception.DockerRunLogException import com.tencent.bk.devops.plugin.docker.pojo.DockerRunLogRequest import com.tencent.bk.devops.plugin.docker.pojo.DockerRunLogResponse import com.tencent.bk.devops.plugin.docker.pojo.DockerRunRequest import com.tencent.bk.devops.plugin.docker.pojo.common.DockerStatus -import com.tencent.bk.devops.plugin.docker.pojo.common.KubernetesPodStatus -import com.tencent.bk.devops.plugin.docker.pojo.job.response.JobStatusResp import com.tencent.devops.docker.pojo.CommandParam import com.tencent.devops.docker.pojo.ImageParam import com.tencent.devops.docker.tools.LogUtils import com.tencent.devops.docker.utils.CodeccConfig +import com.tencent.devops.pojo.exception.CodeccDependentException import com.tencent.devops.pojo.exception.CodeccTaskExecException import java.io.File object DockerRun { val api = DockerApi() + // 64位containerId,预留docker后续升级拓展位 + private val containerIdRegex = Regex("^[0-9a-z]{64,}$") + fun runImage(imageParam: ImageParam, commandParam: CommandParam, toolName: String) { LogUtils.printLog("execute image params: $imageParam") @@ -41,10 +37,7 @@ object DockerRun { PcgDevCloudExecutor.PCG_TOKEN_SECRET_ID to (commandParam.extraPrams[PcgDevCloudExecutor.PCG_TOKEN_SECRET_ID] ?: ""), PcgDevCloudExecutor.PCG_TOKEN_SECRET_KEY to (commandParam.extraPrams[PcgDevCloudExecutor.PCG_TOKEN_SECRET_KEY] ?: ""), PcgDevCloudExecutor.PCG_REQUEST_HOST to (commandParam.extraPrams[PcgDevCloudExecutor.PCG_REQUEST_HOST] ?: "") - )), - // 不指定CPU - cpu = null, - memory = null + )) ) val dockerRunResponse = api.dockerRunCommand( projectId = commandParam.landunParam.devopsProjectId, @@ -96,8 +89,9 @@ object DockerRun { } private fun getRunLogResponse(api: DockerApi, commandParam: CommandParam, extraOptions: Map, timeGap: Long): DockerRunLogResponse { + var response: DockerRunLogResponse? = null try { - return dockerRunGetLog( + response = api.dockerRunGetLog( projectId = commandParam.landunParam.devopsProjectId, pipelineId = commandParam.landunParam.devopsPipelineId, buildId = commandParam.landunParam.buildId, @@ -108,132 +102,27 @@ object DockerRun { extraOptions = extraOptions ) ).data!! + return response } catch (e: Exception) { + LogUtils.printErrorLog("get docker run log response: $response") LogUtils.printErrorLog("fail to get docker run log: ${commandParam.landunParam.buildId}, " + - "${commandParam.landunParam.devopsVmSeqId}, " + - extraOptions.filter { !it.key.contains("token", ignoreCase = true) } + "${commandParam.landunParam.devopsVmSeqId}, " + + extraOptions.filter { !it.key.contains("token", ignoreCase = true) } ) - e.printStackTrace() - } - return DockerRunLogResponse( - log = listOf(), - status = DockerStatus.running, - message = "", - extraOptions = extraOptions - ) - } - - private fun dockerRunGetLog( - projectId: String, - pipelineId: String, - buildId: String, - param: DockerRunLogRequest - ): Result { - try { - val property = System.getenv("devops_slave_model") - val kubernetesEnv = System.getenv("devops.build.node.environment") - - val response = when { - "docker" == property -> CommonExecutor.getLogs(projectId, pipelineId, buildId, param) - "Kubernetes" == kubernetesEnv -> getLogs(param) - else -> ThirdPartExecutor.getLogs(param) - } - - - return com.tencent.bk.devops.atom.pojo.Result(response) - } catch (e: Exception) { - throw DockerRunLogException(e.message ?: "") - } - } - - fun getLogs(param: DockerRunLogRequest): DockerRunLogResponse { - val extraOptions = param.extraOptions.toMutableMap() - - val api = KubernetesBuildApi() - - LogUtils.printLog("[kubernetes]:getLog|param|$param") - - // get job status - val jobStatusFlag = param.extraOptions["jobStatusFlag"] - val jobName = param.extraOptions["kubernetesJobName"] ?: throw RuntimeException("kubernetesJobName is null") - var jobStatusResp: JobStatusResp? = null - var jobIp = "" - if (jobStatusFlag.isNullOrBlank() || jobStatusFlag == DockerStatus.running) { - jobStatusResp = api.getJobStatus(jobName).data - LogUtils.printLog("[kubernetes]:getLog|jobStatusFlag|resp|$jobStatusResp") - - jobIp = jobStatusResp?.pod_result!![0].ip ?: "" - val jobStatus = jobStatusResp.status - if (KubernetesPodStatus.failed != jobStatus && - "succeeded" != jobStatus && - KubernetesPodStatus.running != jobStatus - ) { - return DockerRunLogResponse( - status = DockerStatus.running, - message = "get job status...", - extraOptions = extraOptions - ) - } - } - extraOptions["jobIp"] = jobIp - extraOptions["jobStatusFlag"] = DockerStatus.success - - // actual get log logic - val startTimeStamp = extraOptions["startTimeStamp"]?.apply { - if (trim().length == 13) { - toLong() / 1000 + // container id不存在则认为失败 + val containerId = extraOptions["dockerContainerId"] + if (containerId.isNullOrBlank() || !containerId.matches(containerIdRegex)) { + LogUtils.printErrorLog("get docker containerId fail : $containerId") + throw CodeccDependentException(e.message ?: "") } else { - toLong() + e.printStackTrace() } - }?.toLong() ?: (System.currentTimeMillis() / 1000) - val logs = mutableListOf() - - val logResult = api.getLog(jobName, startTimeStamp) - - // only if not blank then add start time - val isNotBlank = logResult.isNullOrBlank() - if (!isNotBlank) extraOptions["startTimeStamp"] = (startTimeStamp + param.timeGap).toString() - - // add logs - if (!isNotBlank) logs.add(logResult!!) - - if (jobStatusResp == null) { - jobStatusResp = api.getJobStatus(jobName).data } - val finalStatus = jobStatusResp - val podResults = finalStatus?.pod_result - podResults?.forEach { ps -> - ps.events?.forEach { event -> - // add logs - logs.add(event.message) - } - } - LogUtils.printLog("[kubernetes]:getLog|finalStatus|resp|$finalStatus") - - if (finalStatus?.status in listOf(KubernetesPodStatus.failed, "succeeded")) { - Thread.sleep(6000) - val finalLogs = api.getLog(jobName, startTimeStamp + 6000) - if (finalStatus?.status == KubernetesPodStatus.failed) { - return DockerRunLogResponse( - log = logs.plus(finalLogs ?: ""), - status = DockerStatus.failure, - message = "docker run fail...", - extraOptions = extraOptions - ) - } - return DockerRunLogResponse( - log = logs.plus(finalLogs ?: ""), - status = DockerStatus.success, - message = "docker run success...", - extraOptions = extraOptions - ) - } - return DockerRunLogResponse( - log = logs, + log = listOf(), status = DockerStatus.running, - message = "get log...", + message = "", extraOptions = extraOptions ) }