Search in sources :

Example 1 with SUCCESS_EXIT_CODE

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());
}
Also used : Path(java.nio.file.Path) com.walmartlabs.concord.runtime.v2.sdk(com.walmartlabs.concord.runtime.v2.sdk) Inject(javax.inject.Inject) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Paths(java.nio.file.Paths) SUCCESS_EXIT_CODE(com.walmartlabs.concord.plugins.docker.DockerConstants.SUCCESS_EXIT_CODE) LoggerFactory(org.slf4j.LoggerFactory) Named(javax.inject.Named) Path(java.nio.file.Path) InputStream(java.io.InputStream) InputStream(java.io.InputStream)

Aggregations

SUCCESS_EXIT_CODE (com.walmartlabs.concord.plugins.docker.DockerConstants.SUCCESS_EXIT_CODE)1 com.walmartlabs.concord.runtime.v2.sdk (com.walmartlabs.concord.runtime.v2.sdk)1 InputStream (java.io.InputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Inject (javax.inject.Inject)1 Named (javax.inject.Named)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1