Fix bug around spawning worker processes.
We were expecting to find the path to the `strawboss` binary implicitly from the environment, which meant that configuration was also implicit, and required more setup. Now the path to the binary is explicit in the StrawBoss runtime configuration, and the path to the configuration file can also be explicitly given.
This commit is contained in:
parent
42f37a21e6
commit
a1100f17d8
@ -41,6 +41,7 @@ Options
|
||||
else: "strawboss.config.json"
|
||||
|
||||
var cfg = loadStrawBossConfig(cfgFile)
|
||||
cfg.pathToExe = paramStr(0)
|
||||
if not existsDir(cfg.artifactsRepo):
|
||||
echo "Artifacts repo (" & cfg.artifactsRepo & ") does not exist. Creating..."
|
||||
createDir(cfg.artifactsRepo)
|
||||
|
@ -37,7 +37,9 @@ type
|
||||
StrawBossConfig* = object
|
||||
artifactsRepo*: string
|
||||
authSecret*: string
|
||||
filePath*: string
|
||||
debug*: bool
|
||||
pathToExe*: string
|
||||
projects*: seq[ProjectDef]
|
||||
pwdCost*: int8
|
||||
users*: seq[UserRef]
|
||||
@ -131,6 +133,7 @@ proc loadStrawBossConfig*(cfgFile: string): StrawBossConfig =
|
||||
raiseEx "strawboss config file not found: " & cfgFile
|
||||
|
||||
result = parseStrawBossConfig(parseFile(cfgFile))
|
||||
result.filePath = cfgFile
|
||||
|
||||
proc loadProjectConfig*(cfgFile: string): ProjectConfig =
|
||||
if not existsFile(cfgFile):
|
||||
|
@ -77,12 +77,16 @@ proc extractSession(cfg: StrawBossConfig, request: Request): Session =
|
||||
|
||||
result = fromJWT(cfg, headerVal[7..^1])
|
||||
|
||||
proc spawnWorker(req: RunRequest): Worker =
|
||||
proc spawnWorker(cfg: StrawBossConfig, req: RunRequest): Worker =
|
||||
## Kick off a new worker process with the given run information
|
||||
|
||||
let dir = mkdtemp()
|
||||
var args = @["run", req.projectName, req.stepName, "-r", req.buildRef, "-w", dir]
|
||||
var args = @["run", req.projectName, req.stepName, "-r", req.buildRef,
|
||||
"-w", dir, "-c", cfg.filePath]
|
||||
if req.forceRebuild: args.add("-f")
|
||||
debug "Launching worker: " & cfg.pathToExe & " " & args.join(" ")
|
||||
result = Worker(
|
||||
process: startProcess("strawboss", ".", args, loadEnv(), {poUsePath}),
|
||||
process: startProcess(cfg.pathToExe, ".", args, loadEnv(), {poUsePath}),
|
||||
workingDir: dir)
|
||||
|
||||
proc hashPwd*(pwd: string, cost: int8): string =
|
||||
@ -248,7 +252,18 @@ proc start*(cfg: StrawBossConfig): void =
|
||||
projectName: @"projectName",
|
||||
stepName: @"stepName",
|
||||
buildRef: if @"buildRef" != "": @"buildRef" else: nil,
|
||||
forceRebuild: false))) # TODO support this with optional query params
|
||||
forceRebuild: false) # TODO support this with optional query params
|
||||
|
||||
# TODO: instead of immediately spawning a worker, add the request to a
|
||||
# queue to be picked up by a worker. Allows capping the number of worker
|
||||
# prcesses, distributing, etc.
|
||||
let worker = spawnWorker(cfg, runRequest)
|
||||
workers.add(worker)
|
||||
|
||||
resp($(%*{
|
||||
"runRequest": runRequest,
|
||||
"status": { "state": "accepted", "details": "Run request has been queued." }
|
||||
}))
|
||||
|
||||
post "/service/debug/stop":
|
||||
if not cfg.debug: resp(Http404, makeJsonResp(Http404), JSON)
|
||||
|
@ -87,9 +87,22 @@ suite "strawboss server":
|
||||
let returnedVersions = parseJson(resp.body).getElems.mapIt(it.getStr)
|
||||
check sameContents(expectedVersions, returnedVersions)
|
||||
|
||||
#test "enqueue a build":
|
||||
# let http = newAuthenticatedHttpClient(apibase, "bob@builder.com", "password")
|
||||
# let resp = http.get(apiBase & "/project/" & testProjName & "
|
||||
test "run a successful build with artifacts":
|
||||
let http = newAuthenticatedHttpClient(apibase, "bob@builder.com", "password")
|
||||
let resp = http.get(apiBase & "/project/" & testProjName & "/step/build/run/0.1.0"
|
||||
check resp.status.startsWith("200")
|
||||
|
||||
# TODO
|
||||
# check that the project directory has been created in the artifacts repo
|
||||
# check that the run status file has been created in the artifacts repo
|
||||
# check that the run status is not failed
|
||||
# wait for the build to complete
|
||||
# check that the status is "complete"
|
||||
# check that the artifacts we expect are present
|
||||
check false
|
||||
|
||||
test "run a time-consuming build and check the status via the API"
|
||||
check false
|
||||
|
||||
# Last-chance catch to kill the server in case some test err'ed and didn't
|
||||
# reach it's teardown handler
|
||||
|
@ -5,14 +5,14 @@ import ../../../main/nim/strawbosspkg/configuration
|
||||
suite "load and save configuration objects":
|
||||
|
||||
# suite setup & common data
|
||||
let testProjDefStr = """{ "name": "test-project-1", "repo":
|
||||
let testProjDefStr = """{ "name": "dummy-project", "repo":
|
||||
"/non-existent/dir",
|
||||
"cfgFilePath": "strawhat.json",
|
||||
"defaultBranch": "deploy",
|
||||
"envVars": { "VAR1": "value" } }"""
|
||||
|
||||
let testProjDef = ProjectDef(
|
||||
name: "test-project-1",
|
||||
name: "dummy-project",
|
||||
repo: "/non-existent/dir",
|
||||
cfgFilePath: "strawhat.json",
|
||||
defaultBranch: "deploy",
|
||||
@ -23,7 +23,7 @@ suite "load and save configuration objects":
|
||||
let pd = parseProjectDef(parseJson(testProjDefStr))
|
||||
|
||||
check:
|
||||
pd.name == "test-project-1"
|
||||
pd.name == "dummy-project"
|
||||
pd.repo == "/non-existent/dir"
|
||||
pd.cfgFilePath == "strawhat.json"
|
||||
pd.defaultBranch == "deploy"
|
||||
@ -61,13 +61,13 @@ suite "load and save configuration objects":
|
||||
let expectedUsers = @[UserRef(name: "bob@builder.com", hashedPwd: "testvalue"),
|
||||
UserRef(name: "sam@sousa.com", hashedPwd: "testvalue")]
|
||||
let expectedProjects = @[
|
||||
ProjectDef(name: "test-project-1",
|
||||
ProjectDef(name: "dummy-project",
|
||||
repo: "/non-existent/dir",
|
||||
defaultBranch: "deploy",
|
||||
cfgFilePath: "strawhat.json",
|
||||
envVars: newStringTable("VAR1", "value", modeCaseSensitive)),
|
||||
ProjectDef(name: "test-strawboss",
|
||||
repo: "https://git.jdb-labs.com:jdb/test-strawboss.git",
|
||||
ProjectDef(name: "test-project",
|
||||
repo: "",
|
||||
defaultBranch: "master",
|
||||
cfgFilePath: "strawboss.json",
|
||||
envVars: newStringTable(modeCaseSensitive))]
|
||||
@ -80,10 +80,10 @@ suite "load and save configuration objects":
|
||||
sameContents(expectedProjects, cfg.projects)
|
||||
|
||||
test "loadProjectConfig":
|
||||
let pc = loadProjectConfig("src/test/json/test-project-1.config.json")
|
||||
let pc = loadProjectConfig("src/test/json/dummy-project.config.json")
|
||||
|
||||
check:
|
||||
pc.name == "test-project-1"
|
||||
pc.name == "dummy-project"
|
||||
pc.versionCmd == "git describe --all --always"
|
||||
pc.steps.len == 2
|
||||
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user