Skip to content

Commit

Permalink
feat:java插件支持在指定的java环境下运行 TencentBlueKing#10978
Browse files Browse the repository at this point in the history
  • Loading branch information
carlyin0801 committed Sep 20, 2024
1 parent 78d7bb6 commit 2fe5a00
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ interface AtomBusHandleService {
* 处理系统预置指令及用户(task.json.target)设置指令逻辑
*/
fun handleTarget(reqTarget: String?, target: String): String

/**
* 获取插件默认运行环境版本
*/
fun getDefaultRuntimeVersion(): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ class CommonAtomBusHandleHandleServiceImpl : AtomBusHandleService {
override fun handleTarget(reqTarget: String?, target: String): String {
return if (reqTarget.isNullOrBlank()) target else reqTarget
}

override fun getDefaultRuntimeVersion(): String? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ class GolangAtomBusHandleHandleServiceImpl : AtomBusHandleService {
override fun handleTarget(reqTarget: String?, target: String): String {
return if (reqTarget.isNullOrBlank()) target else reqTarget
}

override fun getDefaultRuntimeVersion(): String? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ class JavaAtomBusHandleHandleServiceImpl : AtomBusHandleService {
val jarName = matchResult?.value ?: ""
return jarName
}

override fun getDefaultRuntimeVersion(): String {
// java插件的runtimeVersion默认值为8
return "8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,12 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
val osList = executionInfoMap[KEY_OS] as? List<Map<String, Any>>
val finishKillFlag = executionInfoMap[KEY_FINISH_KILL_FLAG] as? Boolean
val runtimeVersion = executionInfoMap[KEY_RUNTIME_VERSION] as? String
val finalRuntimeVersion = if (runtimeVersion.isNullOrBlank() && language == NODEJS) {
// 如果nodejs插件未配置runtimeVersion,则给runtimeVersion赋默认值10.*
"10.*"
val atomBusHandleService = AtomBusHandleFactory.createAtomBusHandleService(language)
val finalRuntimeVersion = if (runtimeVersion.isNullOrBlank()) {
atomBusHandleService.getDefaultRuntimeVersion()
} else {
runtimeVersion
}
val atomBusHandleService = AtomBusHandleFactory.createAtomBusHandleService(language)
if (null != osList) {
val osDefaultEnvNumMap = mutableMapOf<String, Int>()
osList.forEach { osExecutionInfoMap ->
Expand All @@ -446,14 +445,10 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
params = arrayOf(KEY_OS_NAME)
)
}
val target = osExecutionInfoMap[KEY_TARGET] as? String
if (target == null) {
// 执行入口为空则校验失败
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NULL,
params = arrayOf(KEY_TARGET)
)
}
val target = osExecutionInfoMap[KEY_TARGET] as? String ?: // 执行入口为空则校验失败
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NULL, params = arrayOf(KEY_TARGET)
)
val osArch = osExecutionInfoMap[KEY_OS_ARCH] as? String
val defaultFlag = osExecutionInfoMap[KEY_DEFAULT_FLAG] as? Boolean ?: false
// 统计每种操作系统默认环境配置数量
Expand Down Expand Up @@ -492,14 +487,10 @@ class MarketAtomCommonServiceImpl : MarketAtomCommonService {
}
}
} else {
val target = executionInfoMap[KEY_TARGET] as? String
if (target == null) {
// 执行入口为空则校验失败
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NULL,
params = arrayOf(KEY_TARGET)
)
}
val target = executionInfoMap[KEY_TARGET] as? String ?: // 执行入口为空则校验失败
throw ErrorCodeException(
errorCode = StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_NULL, params = arrayOf(KEY_TARGET)
)
val pkgLocalPath = executionInfoMap[KEY_PACKAGE_PATH] as? String ?: ""
val atomEnvRequest = AtomEnvRequest(
userId = userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ class NodeJsAtomBusHandleHandleServiceImpl : AtomBusHandleService {
}

override fun handleTarget(reqTarget: String?, target: String): String = target

override fun getDefaultRuntimeVersion(): String {
// nodejs插件的runtimeVersion默认值为10.*
return "10.*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ class PythonAtomBusHandleHandleServiceImpl : AtomBusHandleService {
// python插件目前没有用到osArch,无需转换
return osArch
}

override fun getDefaultRuntimeVersion(): String? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ object AgentEnv {
const val BK_TAG = "devops_bk_tag"
const val PUBLIC_HOST_MAX_FILE_CACHE_SIZE = "devops.public.host.maxFileCacheSize"
const val THIRD_HOST_MAX_FILE_CACHE_SIZE = "devops.public.third.maxFileCacheSize"
const val AGENT_JDK_PATH = "DEVOPS_AGENT_JDK_%s_PATH"

private var projectId: String? = null
private var agentId: String? = null
Expand Down Expand Up @@ -288,4 +289,16 @@ object AgentEnv {
fun getLocaleLanguage(): String {
return System.getProperty(LOCALE_LANGUAGE) ?: System.getenv(LOCALE_LANGUAGE) ?: DEFAULT_LOCALE_LANGUAGE
}

fun getRuntimeJdkVersion(): String {
val javaVersion = System.getProperty("java.version").substringBefore(".")
// 在 Java 8 及之前的版本中的版本格式都是1.xxx.xxx,蓝盾只会使用java8及其以后的版本,故只需对java8环境做兼容处理
return if (javaVersion.toInt() > 1) {
javaVersion
} else {
"8"
}
}

fun getRuntimeJdkPath(): String = File(System.getProperty("java.home"), "/bin/java").absolutePath
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ package com.tencent.devops.worker.common.service.impl

import com.tencent.devops.common.api.enums.OSType
import com.tencent.devops.store.pojo.common.ATOM_POST_ENTRY_PARAM
import com.tencent.devops.worker.common.BK_CI_ATOM_EXECUTE_ENV_PATH
import com.tencent.devops.worker.common.JAVA_PATH_ENV
import com.tencent.devops.worker.common.env.AgentEnv
import com.tencent.devops.worker.common.logger.LoggerService
import com.tencent.devops.worker.common.service.AtomRunConditionHandleService
import org.slf4j.LoggerFactory
import java.io.File
Expand All @@ -44,6 +47,23 @@ class JavaAtomRunConditionHandleServiceImpl : AtomRunConditionHandleService {
runtimeVersion: String,
workspace: File
): Boolean {
val runtimeJdkVersion = AgentEnv.getRuntimeJdkVersion()
val runtimeJdkPath = AgentEnv.getRuntimeJdkPath()
// 这里首先使用的是插件配置的jdk版本,如果构建机没有该jdk环境,那么打印日志提示并使用worker的jdk版本执行
val atomJdkVersion = runtimeVersion
var javaPath = runtimeJdkPath
if (atomJdkVersion != runtimeJdkVersion) {
val atomJdkPath = System.getenv(String.format(AgentEnv.AGENT_JDK_PATH, atomJdkVersion))
if (atomJdkPath.isNullOrBlank()) {
LoggerService.addWarnLine(
"the java$atomJdkVersion environment required by the plugin is not available, " +
"so it is run using the agent's java$runtimeJdkVersion environment."
)
} else {
javaPath = atomJdkPath
}
}
System.setProperty(BK_CI_ATOM_EXECUTE_ENV_PATH, javaPath)
return true
}

Expand All @@ -53,11 +73,9 @@ class JavaAtomRunConditionHandleServiceImpl : AtomRunConditionHandleService {
postEntryParam: String?
): String {
logger.info("handleAtomTarget|target:$target,osType:$osType,postEntryParam:$postEntryParam")
var convertTarget = target
// java插件先统一采用agent带的jre执行,如果是windows构建机需把target的启动命令替换下
if (osType == OSType.WINDOWS) {
convertTarget = target.replace("\$" + JAVA_PATH_ENV, "%$JAVA_PATH_ENV%")
}
val executePath = System.getProperty(BK_CI_ATOM_EXECUTE_ENV_PATH)
// java插件先统一采用agent带的jre执行
var convertTarget = target.replace("\$" + JAVA_PATH_ENV, executePath)
if (postEntryParam != null) {
convertTarget = convertTarget.replace(oldValue = " -jar ",
newValue = " -D$ATOM_POST_ENTRY_PARAM=$postEntryParam -jar ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ open class MarketAtomTask : ITask() {
DIR_ENV to atomTmpSpace.absolutePath,
INPUT_ENV to inputFile,
OUTPUT_ENV to outputFile,
JAVA_PATH_ENV to getJavaFile().absolutePath
JAVA_PATH_ENV to AgentEnv.getRuntimeJdkPath()
)
).toMutableMap()

Expand Down Expand Up @@ -1069,8 +1069,6 @@ open class MarketAtomTask : ITask() {
}
}

private fun getJavaFile() = File(System.getProperty("java.home"), "/bin/java")

private fun getContainerVariables(
buildTask: BuildTask,
buildVariables: BuildVariables,
Expand Down

0 comments on commit 2fe5a00

Please sign in to comment.