use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.
the class SyslogRedirectionTest method test.
@Test
public void test() throws Exception {
final String syslogOutput = "should-be-redirected";
try (final DockerClient docker = getNewDockerClient()) {
// Start a container that will be our "syslog" endpoint (just run netcat and print whatever
// we receive).
final String port = "4711";
final String expose = port + "/udp";
docker.pull(ALPINE);
final HostConfig hostConfig = HostConfig.builder().publishAllPorts(true).build();
final ContainerConfig config = ContainerConfig.builder().image(// includes spotify/busybox:latest with netcat with udp support
ALPINE).cmd(asList("nc", "-p", port, "-l", "-u")).exposedPorts(ImmutableSet.of(expose)).hostConfig(hostConfig).build();
final ContainerCreation creation = docker.createContainer(config, testTag + "_syslog");
final String syslogContainerId = creation.id();
docker.startContainer(syslogContainerId);
final ContainerInfo containerInfo = docker.inspectContainer(syslogContainerId);
assertThat(containerInfo.state().running(), equalTo(true));
final String syslogEndpoint = syslogHost + ":" + containerInfo.networkSettings().ports().get(expose).get(0).hostPort();
// Run a Helios job that logs to syslog.
startDefaultMaster();
startDefaultAgent(testHost(), "--syslog-redirect", syslogEndpoint);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final List<String> command = Lists.newArrayList();
final JobId jobId = createJob(testJobName, testJobVersion, testImage, command, ImmutableMap.of("SYSLOG_REDIRECTOR", "/syslog-redirector"));
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
{
// Verify the log for the task container
LogStream logs = null;
try {
logs = docker.logs(taskStatus.getContainerId(), stdout(), stderr());
final String log = logs.readFully();
// for old docker versions should be nothing in the docker output log, either error text
// or our message
assertEquals("", log);
} catch (DockerRequestException e) {
// for new docker versions, trying to read logs should throw an error but the syslog
// option should be set
final String logType = docker.inspectContainer(taskStatus.getContainerId()).hostConfig().logConfig().logType();
assertEquals("syslog", logType);
} finally {
if (logs != null) {
logs.close();
}
}
}
// Verify the log for the syslog container
{
final String log;
try (LogStream logs = docker.logs(syslogContainerId, stdout(), stderr())) {
log = logs.readFully();
}
// the output message from the command should appear in the syslog container
assertThat(log, containsString(syslogOutput));
}
}
}
use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.
the class SyslogRedirectionTest method setup.
@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 + "'");
}
}
}
use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.
the class HeliosSoloDeployment method deploySolo.
/**
* @param heliosHost The address at which the Helios agent should expect to find the Helios
* master.
* @return The container ID of the Helios Solo container.
* @throws HeliosDeploymentException if Helios Solo could not be deployed.
*/
private String deploySolo(final String heliosHost) throws HeliosDeploymentException {
//TODO(negz): Don't make this.env immutable so early?
final List<String> env = new ArrayList<>();
env.addAll(this.env);
env.add("HELIOS_NAME=" + agentName);
env.add("HELIOS_ID=" + this.namespace + HELIOS_ID_SUFFIX);
env.add("HOST_ADDRESS=" + heliosHost);
final String heliosPort = String.format("%d/tcp", HELIOS_MASTER_PORT);
final Map<String, List<PortBinding>> portBindings = ImmutableMap.of(heliosPort, singletonList(PortBinding.of("0.0.0.0", "")));
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).binds(binds).build();
final ContainerConfig containerConfig = ContainerConfig.builder().env(ImmutableList.copyOf(env)).hostConfig(hostConfig).image(heliosSoloImage).build();
log.info("starting container for helios-solo with image={}", heliosSoloImage);
final ContainerCreation creation;
try {
if (pullBeforeCreate) {
dockerClient.pull(heliosSoloImage);
}
final String containerName = HELIOS_CONTAINER_PREFIX + this.namespace;
creation = dockerClient.createContainer(containerConfig, containerName);
} catch (DockerException | InterruptedException e) {
throw new HeliosDeploymentException("helios-solo container creation failed", e);
}
try {
dockerClient.startContainer(creation.id());
} catch (DockerException | InterruptedException e) {
killContainer(creation.id());
removeContainer(creation.id());
throw new HeliosDeploymentException("helios-solo container start failed", e);
}
log.info("helios-solo container started, containerId={}", creation.id());
return creation.id();
}
use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.
the class HeliosSoloDeployment method checkDockerAndGetGateway.
/**
* Checks that the local Docker daemon is reachable from inside a container.
* This method also gets the gateway IP address for this HeliosSoloDeployment.
*
* @return The gateway IP address of the gateway probe container.
* @throws HeliosDeploymentException if we can't deploy the probe container or can't reach
* Docker daemon's API from inside the container.
*/
private String checkDockerAndGetGateway() throws HeliosDeploymentException {
final String probeName = randomString();
final HostConfig hostConfig = HostConfig.builder().binds(binds).build();
final ContainerConfig containerConfig = ContainerConfig.builder().env(env).hostConfig(hostConfig).image(PROBE_IMAGE).cmd(probeCommand(probeName)).build();
final ContainerCreation creation;
try {
pullIfAbsent(PROBE_IMAGE);
creation = dockerClient.createContainer(containerConfig, probeName);
} catch (DockerException | InterruptedException e) {
throw new HeliosDeploymentException("helios-solo probe container creation failed", e);
}
final ContainerExit exit;
final String gateway;
try {
dockerClient.startContainer(creation.id());
gateway = dockerClient.inspectContainer(creation.id()).networkSettings().gateway();
exit = dockerClient.waitContainer(creation.id());
} catch (DockerException | InterruptedException e) {
killContainer(creation.id());
throw new HeliosDeploymentException("helios-solo probe container failed", e);
} finally {
removeContainer(creation.id());
}
if (exit.statusCode() != 0) {
throw new HeliosDeploymentException(String.format("Docker was not reachable (curl exit status %d) using DOCKER_HOST=%s and " + "DOCKER_CERT_PATH=%s from within a container. Please ensure that " + "DOCKER_HOST contains a full hostname or IP address, not localhost, " + "127.0.0.1, etc.", exit.statusCode(), containerDockerHost.bindUri(), containerDockerHost.dockerCertPath()));
}
return gateway;
}
use of com.spotify.docker.client.messages.ContainerConfig in project helios by spotify.
the class HeliosSoloDeploymentTest method testDockerHostContainsLocalhost.
@Test
public void testDockerHostContainsLocalhost() throws Exception {
buildHeliosSoloDeployment();
boolean foundSolo = false;
for (final ContainerConfig cc : containerConfig.getAllValues()) {
if (cc.image().contains("helios-solo")) {
assertThat(cc.hostConfig().binds(), hasItem("/var/run/docker.sock:/var/run/docker.sock"));
foundSolo = true;
}
}
assertTrue("Could not find helios-solo container creation", foundSolo);
}
Aggregations