private void checkDiskSpace(DockerClient dockerClient, String id) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { dockerClient .execStartCmd(dockerClient.execCreateCmd(id).withAttachStdout(true).withCmd("df", "-P").exec().getId()) .exec(new ExecStartResultCallback(outputStream, null)) .awaitCompletion(); } catch (Exception e) { log.debug("Can't exec disk checking command", e); } DiskSpaceUsage df = parseAvailableDiskSpace(outputStream.toString()); VisibleAssertions.assertTrue( "Docker environment should have more than 2GB free disk space", df.availableMB.map(it -> it >= 2048).orElse(true) ); }
private void exposePortThroughProxy(String networkAlias, int originalPort, int mappedPort) { ExecCreateCmdResponse createCmdResponse = dockerClient .execCreateCmd(proxy.getContainerId()) .withCmd("/usr/bin/socat", "TCP-LISTEN:" + originalPort + ",fork,reuseaddr", "TCP:" + networkAlias + ":" + mappedPort) .exec(); dockerClient.execStartCmd(createCmdResponse.getId()) .exec(new ExecStartResultCallback()); }
callback.addConsumer(OutputFrame.OutputType.STDERR, stderrConsumer); dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(callback).awaitCompletion();
public void execStartDetached(String containerId, String... commands) { this.readWriteLock.readLock().lock(); try { String id = execCreate(containerId, commands); this.dockerClient.execStartCmd(id).withDetach(true).exec(new ExecStartResultCallback()); } finally { this.readWriteLock.readLock().unlock(); } }
public void execStartDetached(String containerId, String... commands) { this.readWriteLock.readLock().lock(); try { String id = execCreate(containerId, commands); this.dockerClient.execStartCmd(id).withDetach(true).exec(new ExecStartResultCallback()); } finally { this.readWriteLock.readLock().unlock(); } }
private CubeOutput getOutputOfCommand(String containerId, String[] command) { String execResponseId = execCreate(containerId, command); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); OutputStream errorStream = new ByteArrayOutputStream(); try { this.client.execStartCmd(execResponseId).withDetach(false) .exec(new ExecStartResultCallback(outputStream, errorStream)).awaitCompletion(); } catch (InterruptedException e) { return new CubeOutput("", ""); } return new CubeOutput(outputStream.toString().trim(), errorStream.toString().trim()); }
private CubeOutput execStartOutput(String id) { OutputStream outputStream = new ByteArrayOutputStream(); OutputStream errorStream = new ByteArrayOutputStream(); try { dockerClient.execStartCmd(id).withDetach(false) .exec(new ExecStartResultCallback(outputStream, errorStream)).awaitCompletion(); } catch (InterruptedException e) { return new CubeOutput("", ""); } return new CubeOutput(outputStream.toString(), errorStream.toString()); }
private CubeOutput execStartOutput(String id) { OutputStream outputStream = new ByteArrayOutputStream(); OutputStream errorStream = new ByteArrayOutputStream(); try { dockerClient.execStartCmd(id).withDetach(false) .exec(new ExecStartResultCallback(outputStream, errorStream)).awaitCompletion(); } catch (InterruptedException e) { return new CubeOutput("", ""); } return new CubeOutput(outputStream.toString(), errorStream.toString()); }
public static ExecCmdResult execCmd(DockerClient dockerClient, String containerId, String[] command) { ExecCreateCmdResponse cmd = dockerClient.execCreateCmd(containerId) .withAttachStdout(true) .withAttachStderr(true) .withCmd(command) .exec(); String cmdStdout; String cmdStderr; try (ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); ExecStartResultCallback cmdCallback = new ExecStartResultCallback(stdout, stderr)) { dockerClient.execStartCmd(cmd.getId()).exec(cmdCallback).awaitCompletion(); cmdStdout = stdout.toString(StandardCharsets.UTF_8.name()); cmdStderr = stderr.toString(StandardCharsets.UTF_8.name()); } catch (Exception e) { String format = String.format("Exception was thrown when executing: %s, for container: %s ", Arrays.toString(command), containerId); throw new IllegalStateException(format, e); } int exitCode = dockerClient.inspectExecCmd(cmd.getId()).exec().getExitCode(); String output = cmdStdout.isEmpty() ? cmdStderr : cmdStdout; return new ExecCmdResult(exitCode, output); }
public Void call() throws Exception { final ConsoleLogger console = new ConsoleLogger(listener); DockerClient client = DockerCommand.getClient(descriptor, cfgData.dockerUrlRes, cfgData.dockerVersionRes, cfgData.dockerCertPathRes, null); ExecStartResultCallback callback = new ExecStartResultCallback() { @Override public void onNext(Frame item) { console.logInfo(item.toString()); super.onNext(item); } @Override public void onError(Throwable throwable) { console.logError("Failed to exec start:" + throwable.getMessage()); super.onError(throwable); } }; try { client.execStartCmd(cmdId).exec(callback).awaitCompletion(); } catch (InterruptedException e) { console.logError("Failed to exec start:" + e.getMessage()); } return null; }
@Override public ProcessResult executeInContainerAsUser(ContainerName containerName, String user, OptionalLong timeoutSeconds, String... command) { try { ExecCreateCmdResponse response = execCreateCmd(containerName, user, command); ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream errors = new ByteArrayOutputStream(); ExecStartResultCallback callback = dockerClient.execStartCmd(response.getId()) .exec(new ExecStartResultCallback(output, errors)); if (timeoutSeconds.isPresent()) { if (!callback.awaitCompletion(timeoutSeconds.getAsLong(), TimeUnit.SECONDS)) throw new DockerExecTimeoutException(String.format( "Command '%s' did not finish within %s seconds.", command[0], timeoutSeconds)); } else { // Wait for completion no timeout callback.awaitCompletion(); } InspectExecResponse state = dockerClient.inspectExecCmd(response.getId()).exec(); if (state.isRunning()) throw new DockerException("Command '%s' did not finish within %s seconds."); return new ProcessResult(state.getExitCode(), new String(output.toByteArray()), new String(errors.toByteArray())); } catch (RuntimeException | InterruptedException e) { numberOfDockerDaemonFails.add(); throw new DockerException("Container '" + containerName.asString() + "' failed to execute " + Arrays.toString(command), e); } }
public String execCommand(String containerId, boolean awaitCompletion, String... command) { ExecCreateCmdResponse exec = client.execCreateCmd(containerId).withCmd(command).withTty(false) .withAttachStdin(true).withAttachStdout(true).withAttachStderr(true).exec(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String output = null; try { ExecStartResultCallback resultCallback = client.execStartCmd(exec.getId()).withDetach(false) .withTty(true).exec(new ExecStartResultCallback(outputStream, System.err)); if (awaitCompletion) { resultCallback.awaitCompletion(); } output = new String(outputStream.toByteArray()); } catch (InterruptedException e) { log.warn("Exception executing command {} on container {}", Arrays.toString(command), containerId, e); } return output; }
public String execCommand(String containerId, boolean awaitCompletion, String... command) { ExecCreateCmdResponse exec = client.execCreateCmd(containerId).withCmd(command).withTty(false) .withAttachStdin(true).withAttachStdout(true).withAttachStderr(true).exec(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String output = null; try { ExecStartResultCallback resultCallback = client.execStartCmd(exec.getId()).withDetach(false) .withTty(true).exec(new ExecStartResultCallback(outputStream, System.err)); if (awaitCompletion) { resultCallback.awaitCompletion(); } output = new String(outputStream.toByteArray()); } catch (InterruptedException e) { log.warn("Exception executing command {} on container {}", Arrays.toString(command), containerId, e); } return output; }
.withExecId(execID) .withDetach(false) .exec(execStartResultCallback).awaitCompletion();
.withExecId(execID) .withDetach(false) .exec(execStartResultCallback).awaitCompletion();
@Test public void network_should_reach_pingpong2_by_alias_pong() throws UnsupportedEncodingException, InterruptedException { ExecCreateCmdResponse exec = dockerClient.execCreateCmd("pingpong").withCmd("curl", "http://pong:8080/").withAttachStdout(true).exec(); ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); dockerClient.execStartCmd(exec.getId()).exec(new ExecStartResultCallback(stdOut, null)).awaitCompletion(2, TimeUnit.SECONDS); assertThat(stdOut.toString("UTF-8")).isEqualToIgnoringWhitespace(EXPECTED_RESPONSE); }
@Test public void network_should_reach_pingpong_by_alias_foo() throws UnsupportedEncodingException, InterruptedException { ExecCreateCmdResponse exec = dockerClient.execCreateCmd("pingpong2").withCmd("curl", "http://foo:8080/").withAttachStdout(true).exec(); ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); dockerClient.execStartCmd(exec.getId()).exec(new ExecStartResultCallback(stdOut, null)).awaitCompletion(2, TimeUnit.SECONDS); assertThat(stdOut.toString("UTF-8")).isEqualToIgnoringWhitespace(EXPECTED_RESPONSE); } }
@Test public void network_should_reach_pingpong_by_alias_ping() throws UnsupportedEncodingException, InterruptedException { ExecCreateCmdResponse exec = dockerClient.execCreateCmd("pingpong2").withCmd("curl", "http://ping:8080/").withAttachStdout(true).exec(); ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); dockerClient.execStartCmd(exec.getId()).exec(new ExecStartResultCallback(stdOut, null)).awaitCompletion(2, TimeUnit.SECONDS); assertThat(stdOut.toString("UTF-8")).isEqualToIgnoringWhitespace(EXPECTED_RESPONSE); }
private String asyncRunCommand(String command, InputStream input, DockerStreamDecoder outputDecoder) { ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId) .withAttachStdin(true) .withAttachStdout(true) .withAttachStderr(true) .withCmd(command.split("\\s")) .exec(); ExecStartCmd execStartCmd = dockerClient.execStartCmd(containerId) .withTty(false) .withExecId(execCreateCmdResponse.getId()) .withDetach(false); if (input != null) { execStartCmd.withStdIn(input); } execStartCmd.exec(outputDecoder); return execCreateCmdResponse.getId(); }
@Test public void container_should_have_static_ip_for_app_net_network() throws InterruptedException, IOException { final InspectContainerResponse pingpong = dockerClient.inspectContainerCmd("pingpong").exec(); ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(pingpong.getId()) .withAttachStdout(true).withAttachStdin(true).withAttachStderr(true).withTty(false).withCmd("ifconfig") .exec(); try (OutputStream outputStream = new ByteArrayOutputStream(); OutputStream errorStream = new ByteArrayOutputStream()) { dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false) .exec(new ExecStartResultCallback(outputStream, errorStream)).awaitCompletion(); assertThat(outputStream.toString()).contains("inet addr:172.16.238.10", "inet6 addr: fe80::42:acff:fe10:ee0a/64"); } }