Search in sources :

Example 1 with ProcessWrapper

use of com.thoughtworks.go.util.ProcessWrapper in project gocd by gocd.

the class CommandLine method run.

public int run(ProcessOutputStreamConsumer outputStreamConsumer, String processTag, String... input) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Running " + this);
    }
    addInput(input);
    SafeOutputStreamConsumer safeStreamConsumer = new SafeOutputStreamConsumer(outputStreamConsumer);
    safeStreamConsumer.addArguments(arguments);
    safeStreamConsumer.addSecrets(secrets);
    ProcessWrapper process = execute(safeStreamConsumer, new EnvironmentVariableContext(), processTag);
    return process.waitForExit();
}
Also used : ProcessWrapper(com.thoughtworks.go.util.ProcessWrapper)

Example 2 with ProcessWrapper

use of com.thoughtworks.go.util.ProcessWrapper in project gocd by gocd.

the class CommandLine method runOrBomb.

public ConsoleResult runOrBomb(boolean failOnNonZeroReturn, String processTag, String... input) {
    addInput(input);
    InMemoryStreamConsumer output = ProcessOutputStreamConsumer.inMemoryConsumer();
    ProcessWrapper process = execute(output, new EnvironmentVariableContext(), processTag);
    int returnValue = process.waitForExit();
    ConsoleResult result = new ConsoleResult(returnValue, output.getStdLines(), output.getErrLines(), arguments, secrets, failOnNonZeroReturn);
    if (result.failed()) {
        throw new CommandLineException(this, result);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Output: \n" + StringUtils.join(result.outputForDisplay(), "\n"));
    }
    return result;
}
Also used : ProcessWrapper(com.thoughtworks.go.util.ProcessWrapper)

Example 3 with ProcessWrapper

use of com.thoughtworks.go.util.ProcessWrapper in project gocd by gocd.

the class CommandLine method runScript.

public void runScript(Script script, StreamConsumer buildOutputConsumer, EnvironmentVariableContext environmentVariableContext, String processTag) throws CheckedCommandLineException {
    LOG.info("Running command: {}", toStringForDisplay());
    CompositeConsumer errorStreamConsumer = new CompositeConsumer(CompositeConsumer.ERR, StreamLogger.getWarnLogger(LOG), buildOutputConsumer);
    CompositeConsumer outputStreamConsumer = new CompositeConsumer(CompositeConsumer.OUT, StreamLogger.getInfoLogger(LOG), buildOutputConsumer);
    // TODO: The build output buffer doesn't take into account Cruise running in multi-threaded mode.
    ProcessWrapper process;
    int exitCode = -1;
    SafeOutputStreamConsumer streamConsumer = null;
    try {
        streamConsumer = new SafeOutputStreamConsumer(new ProcessOutputStreamConsumer(outputStreamConsumer, errorStreamConsumer));
        streamConsumer.addArguments(getArguments());
        for (EnvironmentVariableContext.EnvironmentVariable secureEnvironmentVariable : environmentVariableContext.getSecureEnvironmentVariables()) {
            streamConsumer.addSecret(new PasswordArgument(secureEnvironmentVariable.value()));
        }
        process = startProcess(environmentVariableContext, streamConsumer, processTag);
    } catch (CommandLineException e) {
        String message = String.format("Error happened while attempting to execute '%s'. \nPlease make sure [%s] can be executed on this agent.\n", toStringForDisplay(), getExecutable());
        String path = System.getenv("PATH");
        streamConsumer.errOutput(message);
        streamConsumer.errOutput(String.format("[Debug Information] Environment variable PATH: %s", path));
        LOG.error("[Command Line] {}. Path: {}", message, path);
        throw new CheckedCommandLineException(message, e);
    } catch (IOException e) {
        String msg = String.format("Encountered an IO exception while attempting to execute '%s'. Go cannot continue.\n", toStringForDisplay());
        streamConsumer.errOutput(msg);
        throw new CheckedCommandLineException(msg, e);
    }
    exitCode = process.waitForExit();
    script.setExitCode(exitCode);
}
Also used : ProcessWrapper(com.thoughtworks.go.util.ProcessWrapper) IOException(java.io.IOException)

Example 4 with ProcessWrapper

use of com.thoughtworks.go.util.ProcessWrapper in project gocd by gocd.

the class CommandLine method run.

public int run(ConsoleOutputStreamConsumer outputStreamConsumer, String processTag, String... input) {
    LOG.debug("Running {}", this);
    addInput(input);
    SafeOutputStreamConsumer safeStreamConsumer = new SafeOutputStreamConsumer(outputStreamConsumer);
    safeStreamConsumer.addArguments(arguments);
    safeStreamConsumer.addSecrets(secrets);
    ProcessWrapper process = execute(safeStreamConsumer, new EnvironmentVariableContext(), processTag);
    return process.waitForExit();
}
Also used : ProcessWrapper(com.thoughtworks.go.util.ProcessWrapper)

Example 5 with ProcessWrapper

use of com.thoughtworks.go.util.ProcessWrapper in project gocd by gocd.

the class CommandLine method runScript.

public void runScript(Script script, StreamConsumer buildOutputConsumer, EnvironmentVariableContext environmentVariableContext, ProcessTag processTag) throws CheckedCommandLineException {
    LOG.info("Running command: {}", toStringForDisplay());
    CompositeConsumer errorStreamConsumer = new CompositeConsumer(CompositeConsumer.ERR, StreamLogger.getWarnLogger(LOG), buildOutputConsumer);
    CompositeConsumer outputStreamConsumer = new CompositeConsumer(CompositeConsumer.OUT, StreamLogger.getInfoLogger(LOG), buildOutputConsumer);
    // TODO: The build output buffer doesn't take into account Cruise running in multi-threaded mode.
    ProcessWrapper process;
    int exitCode = -1;
    SafeOutputStreamConsumer streamConsumer = null;
    try {
        streamConsumer = new SafeOutputStreamConsumer(new ProcessOutputStreamConsumer(outputStreamConsumer, errorStreamConsumer));
        streamConsumer.addArguments(getArguments());
        streamConsumer.addSecrets(environmentVariableContext.secrets());
        process = startProcess(environmentVariableContext, streamConsumer, processTag);
    } catch (CommandLineException e) {
        String message = String.format("Error happened while attempting to execute '%s'. \nPlease make sure [%s] can be executed on this agent.\n", toStringForDisplay(), getExecutable());
        String path = System.getenv("PATH");
        streamConsumer.errOutput(message);
        streamConsumer.errOutput(String.format("[Debug Information] Environment variable PATH: %s", path));
        LOG.error("[Command Line] {}. Path: {}", message, path);
        throw new CheckedCommandLineException(message, e);
    } catch (IOException e) {
        String msg = String.format("Encountered an IO exception while attempting to execute '%s'. Go cannot continue.\n", toStringForDisplay());
        streamConsumer.errOutput(msg);
        throw new CheckedCommandLineException(msg, e);
    }
    exitCode = process.waitForExit();
    script.setExitCode(exitCode);
}
Also used : ProcessWrapper(com.thoughtworks.go.util.ProcessWrapper) IOException(java.io.IOException)

Aggregations

ProcessWrapper (com.thoughtworks.go.util.ProcessWrapper)18 Test (org.junit.jupiter.api.Test)6 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)3 KillAllChildProcessTask (com.thoughtworks.go.domain.KillAllChildProcessTask)2 Builder (com.thoughtworks.go.domain.builder.Builder)2 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)2 DefaultGoPublisher (com.thoughtworks.go.work.DefaultGoPublisher)2 IOException (java.io.IOException)2 TestOnly (org.jetbrains.annotations.TestOnly)1 Test (org.junit.Test)1 Timeout (org.junit.jupiter.api.Timeout)1 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)1