use of com.walmartlabs.concord.plugins.docker.DockerConstants.SUCCESS_EXIT_CODE in project concord by walmartlabs.
the class DockerTaskV2 method execute.
@Override
public TaskResult execute(Variables input) throws Exception {
Path workDir = this.workDir.getValue();
TaskParams params = new TaskParams(input);
boolean logStdOut = input.getBoolean(LOG_STD_OUT_KEY, true);
boolean logStdError = input.getBoolean(LOG_STD_ERR_KEY, true);
boolean saveStdOut = input.getBoolean(SAVE_STD_OUT_KEY, false);
boolean saveStdError = input.getBoolean(SAVE_STD_ERR_KEY, false);
boolean redirectErrorStream = input.getBoolean(REDIRECT_ERROR_STREAM_KEY, false);
String stdOutFilePath = null;
if (saveStdOut) {
Path logFile = DockerTaskCommon.createTmpFile(workDir, "stdout", ".log");
stdOutFilePath = workDir.relativize(logFile).toString();
}
DockerContainerSpec spec = DockerContainerSpec.builder().image(params.image()).env(DockerTaskCommon.stringify(params.env())).envFile(DockerTaskCommon.getEnvFile(workDir, params)).entryPoint(DockerTaskCommon.prepareEntryPoint(workDir, params)).forcePull(params.forcePull()).options(DockerContainerSpec.Options.builder().hosts(params.hosts()).build()).debug(params.debug()).redirectErrorStream(redirectErrorStream).stdOutFilePath(stdOutFilePath).pullRetryCount(params.pullRetryCount()).pullRetryInterval(params.pullRetryInterval()).build();
StringBuilder stdErr = new StringBuilder();
int code = dockerService.start(spec, logStdOut ? line -> processLog.info("DOCKER: {}", line) : null, logStdError || saveStdError ? line -> {
if (logStdError) {
processLog.info("DOCKER: {}", line);
}
if (saveStdError) {
stdErr.append(line).append("\n");
}
} : null);
String stdOut = null;
if (stdOutFilePath != null) {
InputStream inputStream = Files.newInputStream(Paths.get(stdOutFilePath));
stdOut = DockerTaskCommon.toString(inputStream);
}
if (code != SUCCESS_EXIT_CODE) {
log.warn("call ['{}', '{}', '{}'] -> finished with code {}", params.image(), params.cmd(), workDir, code);
return TaskResult.fail("Docker process finished with exit code " + code).value("stdout", stdOut).value("stderr", stdErr.toString());
}
log.info("call ['{}', '{}', '{}', '{}'] -> done", params.image(), params.cmd(), workDir, params.hosts());
return TaskResult.success().value("stdout", stdOut).value("stderr", stdErr.toString());
}
Aggregations