com.spotify.docker.client.LogStream Java Examples

The following examples show how to use com.spotify.docker.client.LogStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerService.java    From selenium-jupiter with Apache License 2.0 6 votes vote down vote up
public String execCommandInContainer(String containerId, String... command)
        throws DockerException, InterruptedException {
    String commandStr = Arrays.toString(command);
    log.trace("Running command {} in container {}", commandStr,
            containerId);
    String execId = dockerClient.execCreate(containerId, command,
            DockerClient.ExecCreateParam.attachStdout(),
            DockerClient.ExecCreateParam.attachStderr()).id();
    String output = null;
    try (LogStream stream = dockerClient.execStart(execId)) {
        if (stream.hasNext()) {
            output = UTF_8.decode(stream.next().content()).toString();
        }
    } catch (Exception e) {
        log.trace("Exception executing command in container", e);
    }
    log.trace("Result of command {} in container {}: {}", commandStr,
            containerId, output);
    return output;
}
 
Example #2
Source File: HeliosSoloDeploymentTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogService() throws Exception {
  final InMemoryLogStreamFollower logStreamProvider = InMemoryLogStreamFollower.create();
  final HeliosSoloLogService logService = new HeliosSoloLogService(heliosClient,
      dockerClient,
      logStreamProvider);

  final ListenableFuture<List<String>> hostsFuture = Futures.<List<String>>immediateFuture(
      ImmutableList.of(HOST1));
  when(heliosClient.listHosts()).thenReturn(hostsFuture);

  final ListenableFuture<HostStatus> statusFuture = Futures.immediateFuture(
      HostStatus.newBuilder()
          .setStatus(Status.UP)
          .setStatuses(ImmutableMap.of(JOB_ID1, TASK_STATUS1))
          .setJobs(ImmutableMap.of(JOB_ID1, Deployment.of(JOB_ID1, Goal.START)))
          .build());
  when(heliosClient.hostStatus(HOST1)).thenReturn(statusFuture);

  when(dockerClient.logs(anyString(), Matchers.<DockerClient.LogsParam>anyVararg()))
      .thenReturn(mock(LogStream.class));
  logService.runOneIteration();

  verify(dockerClient, timeout(5000)).logs(eq(CONTAINER_ID),
      Matchers.<DockerClient.LogsParam>anyVararg());
}
 
Example #3
Source File: HeliosSoloLogService.java    From helios with Apache License 2.0 6 votes vote down vote up
@Override
public Void call() throws IOException, DockerException {
  try (final LogStream logStream =
           dockerClient.logs(containerId, stdout(), stderr(), follow())) {
    log.info("attaching stdout/stderr for job={}, container={}", jobId, containerId);
    logStreamFollower.followLog(jobId, containerId, logStream);
  } catch (InterruptedException e) {
    // Ignore
  } catch (final Throwable t) {
    if (!(Throwables.getRootCause(t) instanceof ConnectionClosedException)) {
      log.warn("error streaming log for job={}, container={}", jobId, containerId, t);
    }
    throw t;
  }
  return null;
}
 
Example #4
Source File: DnsServerTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoDnsParam() throws Exception {
  startDefaultMaster();
  startDefaultAgent(testHost());
  awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

  final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX,
      asList("cat", "/etc/resolv.conf"));

  deployJob(jobId, testHost());

  final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
  try (final DockerClient dockerClient = getNewDockerClient()) {
    final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr());
    final String log = logs.readFully();

    // Verify that a nameserver is set even if we don't specify the --dns param
    assertThat(log, containsString("nameserver"));
  }
}
 
Example #5
Source File: DnsServerTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testDnsParam() throws Exception {
  final String server1 = "127.0.0.1";
  final String server2 = "127.0.0.2";
  startDefaultMaster();
  startDefaultAgent(testHost(), "--dns", server1, "--dns", server2);
  awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

  final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX,
      asList("cat", "/etc/resolv.conf"));

  deployJob(jobId, testHost());

  final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
  try (final DockerClient dockerClient = getNewDockerClient()) {
    final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr());
    final String log = logs.readFully();

    assertThat(log, containsString(server1));
    assertThat(log, containsString(server2));
  }
}
 
Example #6
Source File: AddExtraHostTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
  try (final DockerClient docker = getNewDockerClient()) {
    // Start Helios agent, configured to bind host /etc/hostname into container /mnt/hostname
    startDefaultMaster();
    startDefaultAgent(testHost(), "--add-host", "secrethost:169.254.169.254");
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

    // a job that cat's /etc/hosts
    final List<String> command = ImmutableList.of("cat", "/etc/hosts");
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command);
    deployJob(jobId, testHost());

    final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);

    final String log;
    try (LogStream logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr())) {
      log = logs.readFully();

      assertThat(log, containsString("169.254.169.254\tsecrethost"));
    }
  }
}
 
Example #7
Source File: MultiplePortJobTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testPortEnvVars() throws Exception {
  startDefaultMaster();
  startDefaultAgent(testHost());
  awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

  final Map<String, PortMapping> ports =
      ImmutableMap.of("bar", staticMapping1);

  try (final DockerClient dockerClient = getNewDockerClient()) {
    final JobId jobId = createJob(testJobName + 1, testJobVersion, BUSYBOX,
        asList("sh", "-c", "echo $HELIOS_PORT_bar"), EMPTY_ENV, ports);

    deployJob(jobId, testHost());

    final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);

    final String log;
    try (final LogStream logs = dockerClient.logs(taskStatus.getContainerId(),
        stdout(), stderr())) {
      log = logs.readFully();
    }
    assertEquals(testHost() + ":" + externalPort1, log.trim());
  }
}
 
Example #8
Source File: DockerInterpreterProcess.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private void execInContainer(String containerId, String execCommand, boolean logout)
    throws DockerException, InterruptedException {

  LOGGER.info("exec container commmand: " + execCommand);

  final String[] command = {"sh", "-c", execCommand};
  final ExecCreation execCreation = docker.execCreate(
      containerId, command, DockerClient.ExecCreateParam.attachStdout(),
      DockerClient.ExecCreateParam.attachStderr());

  LogStream logStream = docker.execStart(execCreation.id());
  while (logStream.hasNext() && logout) {
    final String log = UTF_8.decode(logStream.next().content()).toString();
    LOGGER.info(log);
  }
}
 
Example #9
Source File: RedisContainer.java    From pay-publicapi with MIT License 6 votes vote down vote up
void stop() {
    if (stopped) {
        return;
    }
    try {
        stopped = true;
        System.err.println("Killing redis container with ID: " + containerId);
        LogStream logs = docker.logs(containerId, DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr());
        System.err.println("Killed container logs:\n");
        logs.attach(System.err, System.err);
        docker.stopContainer(containerId, 5);
        docker.removeContainer(containerId);
    } catch (DockerException | InterruptedException | IOException e) {
        System.err.println("Could not shutdown " + containerId);
        e.printStackTrace();
    }
}
 
Example #10
Source File: TerminationTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testTermOnExit() throws Exception {
  startDefaultMaster();

  final String host = testHost();
  startDefaultAgent(host);

  final HeliosClient client = defaultClient();

  awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);

  // Note: signal 15 is SIGTERM
  final Job jobToInterrupt = Job.newBuilder()
      .setName(testJobName)
      .setVersion(testJobVersion)
      .setImage(BUSYBOX)
      .setCommand(asList("/bin/sh", "-c", "trap handle 15; handle() { echo term; exit 0; }; "
                                          + "while true; do sleep 1; done"))
      .build();

  final JobId jobId = createJob(jobToInterrupt);
  deployJob(jobId, host);
  awaitTaskState(jobId, host, RUNNING);

  client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER,
      Deployment.EMPTY_DEPLOYER_MASTER,
      Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);

  final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);

  final String log;
  try (final DockerClient dockerClient = getNewDockerClient();
       LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
    log = logs.readFully();
  }

  // Message expected, because the SIGTERM handler in the script should have run
  assertEquals("term\n", log);
}
 
Example #11
Source File: SyslogRedirectionTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  try (final DockerClient docker = getNewDockerClient()) {
    // Build an image with an ENTRYPOINT and CMD prespecified
    final String dockerDirectory = Resources.getResource("syslog-test-image").getPath();
    docker.build(Paths.get(dockerDirectory), testImage);

    // Figure out the host IP from the container's point of view (needed for syslog)
    final ContainerConfig config = ContainerConfig.builder()
        .image(BUSYBOX)
        .cmd(asList("ip", "route", "show"))
        .build();
    final ContainerCreation creation = docker.createContainer(config);
    final String containerId = creation.id();
    docker.startContainer(containerId);

    // Wait for the container to exit.
    // If we don't wait, docker.logs() might return an epmty string because the container
    // cmd hasn't run yet.
    docker.waitContainer(containerId);

    final String log;
    try (LogStream logs = docker.logs(containerId, stdout(), stderr())) {
      log = logs.readFully();
    }

    final Matcher m = DEFAULT_GATEWAY_PATTERN.matcher(log);
    if (m.find()) {
      syslogHost = m.group("gateway");
    } else {
      fail("couldn't determine the host address from '" + log + "'");
    }
  }
}
 
Example #12
Source File: BindVolumeTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  try (final DockerClient docker = getNewDockerClient()) {
    // Start Helios agent, configured to bind host /etc/hostname into container /mnt/hostname
    startDefaultMaster();
    startDefaultAgent(testHost(), "--bind", "/etc/hostname:/mnt/hostname:ro");
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

    // Figure out the host kernel version
    final String hostname = docker.info().name();

    // Run a job that cat's /mnt/hostname, which should be the host's name
    final List<String> command = ImmutableList.of("cat", "/mnt/hostname");
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, command);
    deployJob(jobId, testHost());

    final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);

    final String log;
    try (LogStream logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr())) {
      log = logs.readFully();
    }

    // the kernel version from the host should be in the log
    assertThat(log, containsString(hostname));
  }
}
 
Example #13
Source File: ContainerHostNameTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidHostname() throws Exception {
  startDefaultMaster();
  startDefaultAgent(testHost());
  awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);

  try (final DockerClient dockerClient = getNewDockerClient()) {

    final List<String> command = asList("hostname", "-f");

    // Create job
    final JobId jobId = createJob(Job.newBuilder()
        .setName(testJobName)
        .setVersion(testJobVersion)
        .setImage(BUSYBOX)
        .setHostname(testHost())
        .setCommand(command)
        .build());

    // deploy
    deployJob(jobId, testHost());

    final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);

    final String log;
    try (final LogStream logs = dockerClient.logs(
        taskStatus.getContainerId(), stdout(), stderr())) {
      log = logs.readFully();
    }

    assertThat(log, containsString(testHost()));
  }
}
 
Example #14
Source File: TerminationTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoIntOnExit() throws Exception {
  startDefaultMaster();

  final String host = testHost();
  startDefaultAgent(host);

  final HeliosClient client = defaultClient();

  awaitHostStatus(client, host, UP, LONG_WAIT_SECONDS, SECONDS);

  // Note: signal 2 is SIGINT
  final Job jobToInterrupt = Job.newBuilder()
      .setName(testJobName)
      .setVersion(testJobVersion)
      .setImage(BUSYBOX)
      .setCommand(asList("/bin/sh", "-c", "trap handle 2; handle() { echo int; exit 0; }; "
                                          + "while true; do sleep 1; done"))
      .build();

  final JobId jobId = createJob(jobToInterrupt);
  deployJob(jobId, host);
  awaitTaskState(jobId, host, RUNNING);

  client.setGoal(new Deployment(jobId, Goal.STOP, Deployment.EMTPY_DEPLOYER_USER,
      Deployment.EMPTY_DEPLOYER_MASTER,
      Deployment.EMPTY_DEPLOYMENT_GROUP_NAME), host);

  final TaskStatus taskStatus = awaitTaskState(jobId, host, STOPPED);

  final String log;
  try (final DockerClient dockerClient = getNewDockerClient();
       LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout())) {
    log = logs.readFully();
  }

  // No message expected, since SIGINT should not be sent
  assertEquals("", log);
}
 
Example #15
Source File: DockerContainerClient.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private boolean exec(final ContainerInfo container,
                     final String commands,
                     final OutputStream outputStream)
{
  String containerId = container.id();
  String shortId = left(containerId, SHORT_ID_LENGTH);

  log.info("Attempting to exec commands '{}' in container '{}' for image '{}'", commands, shortId, image);

  try {
    // attach stdin as well as stdout/stderr to workaround https://github.com/spotify/docker-client/issues/513
    final ExecCreation execCreation = dockerClient
        .execCreate(containerId, cmd(commands), attachStdin(), attachStdout(), attachStderr());

    try (final LogStream stream = dockerClient.execStart(execCreation.id())) {
      // pretend to be a command line, by printing command to run
      log.debug("$ " + commands);

      // Why read each, instead attaching to out and err stream? Mostly because
      // Logstream preserves order of written out and err if/when they get written.
      stream.forEachRemaining(logMessage -> write(outputStream, logMessage));
    }

    log.info("Successfully exec commands '{}' in container '{}' for image '{}'", commands, shortId, image);

    return true;
  }
  catch (DockerException | InterruptedException e) {
    log.error("Failed to exec commands '{}' in container '{}' for image '{}'", commands, shortId, image, e);
  }

  return false;
}
 
Example #16
Source File: ExecHealthCheckerTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  final ExecHealthCheck healthCheck = ExecHealthCheck.of("exit 0");

  final Info info = mock(Info.class);
  when(info.executionDriver()).thenReturn("native-0.2");

  final Version version = mock(Version.class);
  when(version.apiVersion()).thenReturn("1.18");

  final ExecState execState = mock(ExecState.class);
  when(execState.exitCode()).thenReturn(0L);

  final LogStream log = mock(LogStream.class);
  when(log.readFully()).thenReturn("");

  docker = mock(DockerClient.class);
  when(docker.info()).thenReturn(info);
  when(docker.version()).thenReturn(version);
  when(docker.execCreate(eq(CONTAINER_ID), any(String[].class),
      (DockerClient.ExecCreateParam) anyVararg()))
      .thenReturn(ExecCreation.create(EXEC_ID, emptyList()));
  when(docker.execStart(eq(EXEC_ID), (ExecStartParameter) anyVararg())).thenReturn(log);
  when(docker.execInspect(EXEC_ID)).thenReturn(execState);

  checker = new ExecHealthChecker(healthCheck, docker);
}
 
Example #17
Source File: DockerServiceElasticAgent.java    From docker-swarm-elastic-agent-plugin with Apache License 2.0 5 votes vote down vote up
public static DockerServiceElasticAgent fromService(Service service, DockerClient client) throws DockerException, InterruptedException {
    DockerServiceElasticAgent agent = new DockerServiceElasticAgent();

    agent.id = service.id();
    agent.name = service.spec().name();
    agent.createdAt = service.createdAt();
    agent.jobIdentifier = JobIdentifier.fromJson(service.spec().labels().get(JOB_IDENTIFIER_LABEL_KEY));

    LogStream logStream = client.serviceLogs(service.id(), DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr());
    agent.logs = logStream.readFully();
    logStream.close();

    TaskSpec taskSpec = service.spec().taskTemplate();

    agent.image = taskSpec.containerSpec().image();
    agent.hostname = taskSpec.containerSpec().hostname();
    agent.limits = resourceToString(taskSpec.resources().limits());
    agent.reservations = resourceToString(taskSpec.resources().reservations());
    agent.command = listToString(taskSpec.containerSpec().command());
    agent.args = listToString(taskSpec.containerSpec().args());
    agent.placementConstraints = listToString(taskSpec.placement().constraints());
    agent.environments = toMap(taskSpec);
    agent.hosts = listToString(taskSpec.containerSpec().hosts());

    final List<Task> tasks = client.listTasks(Task.Criteria.builder().serviceName(service.id()).build());
    if (!tasks.isEmpty()) {
        for (Task task : tasks) {
            agent.tasksStatus.add(new TaskStatus(task));
        }
    }

    return agent;
}
 
Example #18
Source File: MongoTestRunner.java    From aion with MIT License 4 votes vote down vote up
/**
 * Helper method to run some initialization command on Mongo with some retry logic if the
 * command fails. Since it's not determinate how long starting the database will take, we need
 * this retry logic.
 *
 * @param initializationCommands The command to actually run
 * @param retriesRemaining How many more times to retry the command if it fails
 * @param pauseTimeMillis How long to pause between retries
 * @throws InterruptedException Thrown when the thread gets interrupted trying to sleep.
 */
private void tryInitializeDb(
        String[] initializationCommands, int retriesRemaining, long pauseTimeMillis)
        throws InterruptedException {

    Exception exception = null;
    String execOutput = "";
    try {
        final ExecCreation execCreation =
                dockerClient.execCreate(
                        this.runningDockerContainerId,
                        initializationCommands,
                        DockerClient.ExecCreateParam.attachStdout(),
                        DockerClient.ExecCreateParam.attachStderr(),
                        DockerClient.ExecCreateParam.detach(false));
        final LogStream output = dockerClient.execStart(execCreation.id());
        execOutput = output.readFully();
    } catch (Exception e) {
        exception = e;
    }

    // We can't get the exit code, but look for an expected message in the output to determine
    // success
    if (exception != null
            || !execOutput.contains("Using a default configuration for the set")) {
        // This is the case that the command didn't work
        if (retriesRemaining == 0) {
            // We're out of retries, we should fail
            if (exception != null) {
                exception.printStackTrace();
            }

            fail(
                    "Failed to initialize MongoDB, no retries remaining. Output was: "
                            + execOutput);
        } else {
            Thread.sleep(pauseTimeMillis);
            tryInitializeDb(initializationCommands, retriesRemaining - 1, pauseTimeMillis);
        }
    }
}