Skip to content

Commit

Permalink
Merge pull request #6444 from sawyersong2/issue6439_github_branch
Browse files Browse the repository at this point in the history
feat:新无编译环境方案挂载临时工作空间 #6439
  • Loading branch information
irwinsun authored Apr 6, 2022
2 parents 81d5179 + cee3ee3 commit 11489e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class BuildLessConfig {
@Value("\${dockerCli.hostPathWorkspace:#{null}}")
var hostPathWorkspace: String? = null

@Value("\${dockerCli.hostPathLinkDir}")
var hostPathLinkDir: String = "/tmp/bkci"

@Value("\${dockerCli.memoryLimitBytes:34359738368}")
var memory: Long = 34359738368L // 1024 * 1024 * 1024 * 32 Memory limit in bytes. 32G

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ import com.tencent.devops.buildless.utils.ENV_JOB_BUILD_TYPE
import com.tencent.devops.buildless.utils.ENV_KEY_GATEWAY
import com.tencent.devops.buildless.utils.RandomUtil
import com.tencent.devops.buildless.utils.RedisUtils
import com.tencent.devops.common.api.util.ShaUtils
import com.tencent.devops.common.service.config.CommonConfig
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.io.File
import java.nio.file.FileSystems
import java.nio.file.Files
import java.text.SimpleDateFormat
import java.util.TimeZone
import kotlin.streams.toList
Expand Down Expand Up @@ -100,12 +104,16 @@ class BuildLessContainerService(
val containerName = "$BUILDLESS_POOL_PREFIX-${RandomUtil.randomString()}"

val hostWorkspace = buildLessConfig.hostPathWorkspace + "/$containerName"

val linkPath = createSymbolicLink(hostWorkspace)

val binds = Binds(
Bind(buildLessConfig.hostPathApps, volumeApps, AccessMode.ro),
Bind(buildLessConfig.hostPathInit, volumeInit, AccessMode.ro),
Bind(buildLessConfig.hostPathSleep, volumeSleep, AccessMode.ro),
Bind(buildLessConfig.hostPathLogs + "/$containerName", volumeLogs),
Bind(hostWorkspace, volumeWs)
Bind(hostWorkspace, volumeWs),
Bind(linkPath, Volume(linkPath))
)

try {
Expand All @@ -123,7 +131,7 @@ class BuildLessContainerService(
"$ENV_BK_CI_DOCKER_HOST_IP=${CommonUtils.getInnerIP()}",
"$ENV_JOB_BUILD_TYPE=BUILD_LESS",
"$ENV_CONTAINER_NAME=$containerName",
"$ENV_BK_CI_DOCKER_HOST_WORKSPACE=$hostWorkspace"
"$ENV_BK_CI_DOCKER_HOST_WORKSPACE=$linkPath"
)
)
.withHostConfig(
Expand Down Expand Up @@ -242,6 +250,27 @@ class BuildLessContainerService(
return timeoutContainerList
}

fun createSymbolicLink(hostWorkspace: String): String {
val hostWorkspaceFile = File(hostWorkspace)
if (!hostWorkspaceFile.exists()) {
hostWorkspaceFile.mkdirs() // 新建的流水线的工作空间路径为空则新建目录
}
val shaContent = ShaUtils.sha1(hostWorkspace.toByteArray())
val linkFilePathDir = buildLessConfig.hostPathLinkDir
val linkFileDir = File(linkFilePathDir)
if (!linkFileDir.exists()) {
linkFileDir.mkdirs()
}
val linkPath = "$linkFilePathDir/$shaContent"
logger.info("hostWorkspace:$hostWorkspace linkPath is: $linkPath")
val link = FileSystems.getDefault().getPath(linkPath)
if (!link.toFile().exists()) {
val target = FileSystems.getDefault().getPath(hostWorkspace)
Files.createSymbolicLink(link, target) // 为真实工作空间地址创建软链
}
return linkPath
}

private fun checkStartTime(utcTime: String?): Boolean {
if (utcTime != null && utcTime.isNotEmpty()) {
val array = utcTime.split(".")
Expand Down

0 comments on commit 11489e3

Please sign in to comment.