Search in sources :

Example 1 with Console

use of com.oracle.bedrock.runtime.options.Console in project oracle-bedrock by coherence-community.

the class DockerMachine method environmentFor.

/**
 * Obtain the {@link EnvironmentVariable}s that can be applied to a Docker command
 * to make it execute against the specified Docker Machine.
 *
 * @param machineName  the name of the Docker Machine
 *
 * @return  the {@link EnvironmentVariable}s required to use the Docker Machine
 */
public List<EnvironmentVariable> environmentFor(String machineName) {
    CapturingApplicationConsole console = new CapturingApplicationConsole();
    try (Application application = launch("env", Argument.of(machineName), Console.of(console))) {
        if (application.waitFor() == 0) {
            return console.getCapturedOutputLines().stream().filter((line) -> line.startsWith("export")).map((line) -> line.substring(7)).map((line -> {
                int index = line.indexOf('=');
                if (index >= 0) {
                    String name = line.substring(0, index);
                    String value = StringHelper.unquote(line.substring(index + 1));
                    return EnvironmentVariable.of(name, value);
                }
                return EnvironmentVariable.of(line);
            })).collect(Collectors.toList());
        }
        String msg = "Error obtaining environment for docker-machine " + machineName;
        logError(msg, console);
        throw new RuntimeException(msg);
    }
}
Also used : Arrays(java.util.Arrays) CapturingApplicationConsole(com.oracle.bedrock.runtime.console.CapturingApplicationConsole) Timeout(com.oracle.bedrock.options.Timeout) DockerPlatform(com.oracle.bedrock.runtime.docker.DockerPlatform) Level(java.util.logging.Level) InetAddress(java.net.InetAddress) Console(com.oracle.bedrock.runtime.options.Console) EnvironmentVariable(com.oracle.bedrock.runtime.options.EnvironmentVariable) Json(javax.json.Json) OptionsByType(com.oracle.bedrock.OptionsByType) Application(com.oracle.bedrock.runtime.Application) StringHelper(com.oracle.bedrock.lang.StringHelper) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) LocalPlatform(com.oracle.bedrock.runtime.LocalPlatform) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Arguments(com.oracle.bedrock.runtime.options.Arguments) File(java.io.File) Argument(com.oracle.bedrock.runtime.options.Argument) Option(com.oracle.bedrock.Option) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) StringReader(java.io.StringReader) Platform(com.oracle.bedrock.runtime.Platform) CapturingApplicationConsole(com.oracle.bedrock.runtime.console.CapturingApplicationConsole) Application(com.oracle.bedrock.runtime.Application)

Example 2 with Console

use of com.oracle.bedrock.runtime.options.Console in project oracle-bedrock by coherence-community.

the class Pod method getContainers.

/**
 * Obtain the containers for this Pod.
 *
 * @return  a {@link Map} of containers for this Pod keyed by container name
 */
public Map<String, PodContainer> getContainers() {
    if (containers == null) {
        synchronized (this) {
            if (containers == null) {
                CapturingApplicationConsole console = new CapturingApplicationConsole();
                List<String> args = getArgs("get");
                args.add("pod");
                args.add(podName);
                args.add("-o");
                args.add("jsonpath={range .spec.containers[*]}{.name}{\"\\n\"}{end}");
                int exitCode = k8s.kubectlAndWait(Arguments.of(args), Console.of(console), LaunchLogging.disabled());
                if (exitCode == 0) {
                    containers = console.getCapturedOutputLines().stream().filter(s -> !"(terminated)".equals(s)).filter(s -> !s.trim().isEmpty()).map(name -> new PodContainer(k8s, podName, name, namespace)).collect(Collectors.toMap(PodContainer::getContainerName, container -> container));
                } else {
                    String msg = String.join("\n", console.getCapturedErrorLines());
                    throw new RuntimeException("Error obtaining Pod containers\n" + msg);
                }
            }
        }
    }
    return Collections.unmodifiableMap(containers);
}
Also used : Arrays(java.util.Arrays) CapturingApplicationConsole(com.oracle.bedrock.runtime.console.CapturingApplicationConsole) ArrayList(java.util.ArrayList) ApplicationConsole(com.oracle.bedrock.runtime.ApplicationConsole) Console(com.oracle.bedrock.runtime.options.Console) LaunchLogging(com.oracle.bedrock.options.LaunchLogging) OutputToOutputStreamRedirector(com.oracle.bedrock.runtime.console.OutputToOutputStreamRedirector) FileWriterApplicationConsole(com.oracle.bedrock.runtime.console.FileWriterApplicationConsole) Map(java.util.Map) OptionsByType(com.oracle.bedrock.OptionsByType) Application(com.oracle.bedrock.runtime.Application) Path(java.nio.file.Path) ConsoleOutputRedirector(com.oracle.bedrock.runtime.options.ConsoleOutputRedirector) OutputStream(java.io.OutputStream) Files(java.nio.file.Files) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) Arguments(com.oracle.bedrock.runtime.options.Arguments) File(java.io.File) Option(com.oracle.bedrock.Option) Objects(java.util.Objects) ConsoleInputRedirector(com.oracle.bedrock.runtime.options.ConsoleInputRedirector) List(java.util.List) InputFromIntputStreamRedirector(com.oracle.bedrock.runtime.console.InputFromIntputStreamRedirector) Collections(java.util.Collections) CapturingApplicationConsole(com.oracle.bedrock.runtime.console.CapturingApplicationConsole)

Aggregations

Option (com.oracle.bedrock.Option)2 OptionsByType (com.oracle.bedrock.OptionsByType)2 Application (com.oracle.bedrock.runtime.Application)2 CapturingApplicationConsole (com.oracle.bedrock.runtime.console.CapturingApplicationConsole)2 Arguments (com.oracle.bedrock.runtime.options.Arguments)2 Console (com.oracle.bedrock.runtime.options.Console)2 File (java.io.File)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 StringHelper (com.oracle.bedrock.lang.StringHelper)1 LaunchLogging (com.oracle.bedrock.options.LaunchLogging)1 Timeout (com.oracle.bedrock.options.Timeout)1 ApplicationConsole (com.oracle.bedrock.runtime.ApplicationConsole)1 LocalPlatform (com.oracle.bedrock.runtime.LocalPlatform)1 Platform (com.oracle.bedrock.runtime.Platform)1 FileWriterApplicationConsole (com.oracle.bedrock.runtime.console.FileWriterApplicationConsole)1 InputFromIntputStreamRedirector (com.oracle.bedrock.runtime.console.InputFromIntputStreamRedirector)1 OutputToOutputStreamRedirector (com.oracle.bedrock.runtime.console.OutputToOutputStreamRedirector)1 DockerPlatform (com.oracle.bedrock.runtime.docker.DockerPlatform)1