move structure, workspace alias

master
Josha von Gizycki 3 weeks ago
parent 99fda410ba
commit 94c177234e

@ -17,4 +17,10 @@ class Processes {
environment()["ALFRED_REV"] = rev environment()["ALFRED_REV"] = rev
} }
fun startThread(block: () -> Unit) {
Thread {
block()
}.start()
}
} }

@ -10,7 +10,7 @@ import kotlin.io.deleteRecursively
@Service @Service
class Workspaces { class Workspaces {
fun withWorkspace(ctx: BuildContext, block: (Path) -> Unit) { fun withWorkspace(ctx: BuildContext, block: (Workspace) -> Unit) {
val wsDir = prepare(ctx) val wsDir = prepare(ctx)
try { try {
@ -20,7 +20,7 @@ class Workspaces {
} }
} }
fun prepare(ctx: BuildContext): Path { fun prepare(ctx: BuildContext): Workspace {
val workspacePath = Paths.get( val workspacePath = Paths.get(
ctx.config.workspace, UUID.randomUUID().toString() ctx.config.workspace, UUID.randomUUID().toString()
) )
@ -40,15 +40,14 @@ class Workspaces {
return workspacePath return workspacePath
} }
fun cleanUp(wsDir: Path) { fun cleanUp(wsDir: Workspace) {
wsDir.toFile().deleteRecursively() wsDir.toFile().deleteRecursively()
} }
} }
typealias Workspace = Path
class WorkspaceIsNotEmpty( class WorkspaceIsNotEmpty(
private val ws: String private val ws: String
) : RuntimeException() { ) : RuntimeException("${WorkspaceIsNotEmpty::class}: workspace $ws is not empty")
override fun toString() =
"${this::class}: workspace $ws is not empty"
}

@ -7,11 +7,11 @@ import alfred.web.core.build.BuildContext
import alfred.web.core.build.BuildId import alfred.web.core.build.BuildId
import alfred.web.core.build.Builds import alfred.web.core.build.Builds
import alfred.web.core.build.ProcessInfo import alfred.web.core.build.ProcessInfo
import alfred.web.core.build.Workspace
import alfred.web.core.build.Workspaces import alfred.web.core.build.Workspaces
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.nio.file.Path
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.collections.forEach import kotlin.collections.forEach
import kotlin.jvm.java import kotlin.jvm.java
@ -26,6 +26,8 @@ class GitRunner(
val scriptsDir = ".alfred" val scriptsDir = ".alfred"
val scriptFiles = listOf("pre.sh", "job.sh", "post.sh")
fun run(buildId: BuildId, rev: String): ProcessInfo { fun run(buildId: BuildId, rev: String): ProcessInfo {
val config = builds.buildConfig(buildId) val config = builds.buildConfig(buildId)
val logFile = builds.createLogFile(buildId) val logFile = builds.createLogFile(buildId)
@ -40,54 +42,56 @@ class GitRunner(
rev = rev rev = rev
) )
Thread { processes.startThread {
workspaces.withWorkspace(ctx) { wsPath -> workspaces.withWorkspace(ctx) { wsPath ->
logFile.header(buildId, rev, wsPath) logFile.header(buildId, rev, wsPath)
clone(ctx, wsPath) clone(ctx, wsPath)
checkout(ctx, wsPath)
execScripts(ctx, wsPath) execScripts(ctx, wsPath)
logFile.footer() logFile.footer()
} }
}.start() }
return ProcessInfo(-1, logFile) return ProcessInfo(-1, logFile)
} }
private fun clone(ctx: BuildContext, wsPath: Path) { private fun clone(ctx: BuildContext, ws: Workspace) {
ctx.log("build ${ctx.buildId}: cloning ${ctx.config.gitRepo} into $wsPath") ctx.log("build ${ctx.buildId}: cloning ${ctx.config.gitRepo} into $ws")
val cloneProc = processes.builder(ctx.config, ctx.logFile, "") val proc = processes.builder(ctx.config, ctx.logFile, "")
.command("git", "clone", ctx.config.gitRepo, ".") .command("git", "clone", ctx.config.gitRepo, ".")
.directory(wsPath.toFile()) .directory(ws.toFile())
.start() .start()
handles.add(Handle(cloneProc.toHandle(), ctx.buildId)) handles.add(Handle(proc.toHandle(), ctx.buildId))
val cloneSuccess = cloneProc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS) val cloneSuccess = proc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS)
if (!cloneSuccess) { if (!cloneSuccess) {
throw FailedToClone(ctx.buildId, ctx.config.gitRepo ?: "[no repo configured]") throw FailedToClone(ctx.buildId, ctx.config.gitRepo ?: "[no repo configured]")
} }
}
private fun checkout(ctx: BuildContext, ws: Workspace) {
ctx.log("build ${ctx.buildId}: checkout rev ${ctx.rev}") ctx.log("build ${ctx.buildId}: checkout rev ${ctx.rev}")
val checkoutProc = processes.builder(ctx.config, ctx.logFile, "")
val proc = processes.builder(ctx.config, ctx.logFile, "")
.command("git", "checkout", ctx.rev) .command("git", "checkout", ctx.rev)
.directory(wsPath.toFile()) .directory(ws.toFile())
.start() .start()
handles.add(Handle(checkoutProc.toHandle(), ctx.buildId)) handles.add(Handle(proc.toHandle(), ctx.buildId))
val checkoutSuccess = checkoutProc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS) val checkoutSuccess = proc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS)
if (!checkoutSuccess) { if (!checkoutSuccess) {
throw FailedToCheckout(ctx.buildId, ctx.config.gitRepo!!, ctx.rev) throw FailedToCheckout(ctx.buildId, ctx.config.gitRepo!!, ctx.rev)
} }
} }
private fun execScripts(ctx: BuildContext, wsPath: Path) { private fun execScripts(ctx: BuildContext, ws: Workspace) {
val scriptFiles = listOf("pre.sh", "job.sh", "post.sh")
ctx.log("build ${ctx.buildId}: looking for scripts $scriptFiles in $scriptsDir/") ctx.log("build ${ctx.buildId}: looking for scripts $scriptFiles in $scriptsDir/")
scriptFiles.forEach { script -> scriptFiles.forEach { script ->
val scriptFile = shFile(wsPath, script) val scriptFile = shFile(ws, script)
if (scriptFile.exists()) { if (scriptFile.exists()) {
ctx.log("build ${ctx.buildId}: found script $script, running it") ctx.log("build ${ctx.buildId}: found script $script, running it")
@ -95,7 +99,7 @@ class GitRunner(
val scriptProcess = processes.builder(ctx.config, ctx.logFile, ctx.rev) val scriptProcess = processes.builder(ctx.config, ctx.logFile, ctx.rev)
.command(scriptFile.absolutePath) .command(scriptFile.absolutePath)
.directory(wsPath.toFile()) .directory(ws.toFile())
.start() .start()
handles.add(Handle(scriptProcess.toHandle(), ctx.buildId)) handles.add(Handle(scriptProcess.toHandle(), ctx.buildId))
@ -109,8 +113,8 @@ class GitRunner(
} }
} }
private fun shFile(wsPath: Path, name: String) = private fun shFile(ws: Workspace, name: String) =
wsPath.resolve(scriptsDir).resolve(name).toFile() ws.resolve(scriptsDir).resolve(name).toFile()
val logger: Logger = LoggerFactory.getLogger(this::class.java) val logger: Logger = LoggerFactory.getLogger(this::class.java)

Loading…
Cancel
Save