|
| 1 | +#!/usr/bin/env groovy |
| 2 | + |
| 3 | +int call(ctx, oneStep, config) { |
| 4 | + def args = oneStep.args ?: [:] |
| 5 | + |
| 6 | + // Load the shared Slurm helper library from the pipeline shared library. |
| 7 | + // refer to this doc for more details: https://www.jenkins.io/doc/book/pipeline/shared-libraries/#using-libraries |
| 8 | + // Default is 'swx-jenkins-lib' (from Mellanox-lab org). Optional step arg 'ref' can override the git ref. |
| 9 | + // e.g., args: [ref: 'my-branch'] => library(identifier: 'swx-jenkins-lib@my-branch') |
| 10 | + def libRef = args.remove('ref') |
| 11 | + |
| 12 | + if (libRef) { |
| 13 | + library(identifier: "swx-jenkins-lib@${libRef}") |
| 14 | + } else { |
| 15 | + library('swx-jenkins-lib') |
| 16 | + } |
| 17 | + |
| 18 | + if (!args || args.size() < 1) { |
| 19 | + ctx.reportFail(oneStep.name, 'fatal: slurm module expects at least 1 arg') |
| 20 | + return 1 |
| 21 | + } |
| 22 | + |
| 23 | + for (def entry in ctx.entrySet(args)) { |
| 24 | + args[entry.key] = ctx.resolveTemplate(['env': env], entry.value.toString(), config) |
| 25 | + } |
| 26 | + def stepRun = oneStep.run |
| 27 | + def allowedOps = ['allocation', 'run', 'stop', 'stopAllForBuild'] as Set |
| 28 | + if (!allowedOps.contains(stepRun)) { |
| 29 | + ctx.reportFail(oneStep.name, "fatal: unsupported slurm operation '${stepRun}'; allowed: ${allowedOps.sort().join(', ')}") |
| 30 | + return 1 |
| 31 | + } |
| 32 | + println("Calling slurm.${stepRun} with args=" + args) |
| 33 | + |
| 34 | + def vars = [] |
| 35 | + vars += ctx.toEnvVars(config, config.env) |
| 36 | + vars += ctx.toEnvVars(config, oneStep.env) |
| 37 | + |
| 38 | + def rawResult = null |
| 39 | + withEnv(vars) { |
| 40 | + rawResult = slurm."${stepRun}"(args) |
| 41 | + } |
| 42 | + if (rawResult == false) { |
| 43 | + return 1 |
| 44 | + } |
| 45 | + if (rawResult instanceof Number) { |
| 46 | + return (rawResult as int) |
| 47 | + } |
| 48 | + return 0 |
| 49 | +} |
0 commit comments