use of com.spotify.docker.client.messages.Container in project helios by spotify.
the class ZooKeeperClusterIdTest method testAgent.
@Test
public void testAgent() throws Exception {
startDefaultMaster("--zk-cluster-id=" + zkClusterId);
startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Create job and deploy it
final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
deployJob(jobId, testHost());
final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING);
final String containerId = runningStatus.getContainerId();
// Delete the config node which contains the cluster ID and all the job definitions
zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config");
// Sleep for a second so agent has a chance to react to deletion
Thread.sleep(1000);
// Make sure the agent didn't stop the job
try (final DockerClient docker = getNewDockerClient()) {
final List<Container> containers = docker.listContainers();
final CustomTypeSafeMatcher<Container> containerIdMatcher = new CustomTypeSafeMatcher<Container>("Container with id " + containerId) {
@Override
protected boolean matchesSafely(Container container) {
return container.id().equals(containerId);
}
};
assertContainersMatch(containers, containerIdMatcher);
}
}
use of com.spotify.docker.client.messages.Container in project helios by spotify.
the class DeploymentTest method testLotsOfConcurrentJobs.
@Test
public void testLotsOfConcurrentJobs() throws Exception {
startDefaultMaster();
final HeliosClient client = defaultClient();
startDefaultAgent(testHost());
awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final int numberOfJobs = 40;
final List<JobId> jobIds = Lists.newArrayListWithCapacity(numberOfJobs);
final String jobName = testJobName + "_" + toHexString(ThreadLocalRandom.current().nextInt());
// create and deploy a bunch of jobs
for (Integer i = 0; i < numberOfJobs; i++) {
final Job job = Job.newBuilder().setName(jobName).setVersion(i.toString()).setImage(BUSYBOX).setCommand(IDLE_COMMAND).setCreatingUser(TEST_USER).build();
final JobId jobId = job.getId();
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final Deployment deployment = Deployment.of(jobId, START, TEST_USER);
final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
jobIds.add(jobId);
}
// get the container ID's for the jobs
final Set<String> containerIds = Sets.newHashSetWithExpectedSize(numberOfJobs);
for (final JobId jobId : jobIds) {
final TaskStatus taskStatus = awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
containerIds.add(taskStatus.getContainerId());
}
try (final DockerClient dockerClient = getNewDockerClient()) {
// kill all the containers for the jobs
for (final String containerId : containerIds) {
dockerClient.killContainer(containerId);
}
// make sure all the containers come back up
final int restartedContainers = Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int matchingContainerCount = 0;
for (final Container c : dockerClient.listContainers()) {
for (final String name : c.names()) {
if (name.contains(jobName)) {
matchingContainerCount++;
}
}
}
if (matchingContainerCount < containerIds.size()) {
return null;
} else {
return matchingContainerCount;
}
}
});
assertEquals(numberOfJobs, restartedContainers);
}
}
use of com.spotify.docker.client.messages.Container in project azure-tools-for-java by Microsoft.
the class DockerHostRunState method executeSteps.
@Override
public String executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
final String[] runningContainerId = { null };
processHandler.addProcessListener(new ProcessListener() {
@Override
public void startNotified(ProcessEvent processEvent) {
}
@Override
public void processTerminated(ProcessEvent processEvent) {
}
@Override
public void processWillTerminate(ProcessEvent processEvent, boolean b) {
try {
DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
DockerUtil.stopContainer(docker, runningContainerId[0]);
} catch (Exception e) {
// ignore
}
}
@Override
public void onTextAvailable(ProcessEvent processEvent, Key key) {
}
});
processHandler.setText("Starting job ... ");
String basePath = project.getBasePath();
if (basePath == null) {
processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
throw new FileNotFoundException("Project base path is null.");
}
// locate artifact to specified location
String targetFilePath = dataModel.getTargetPath();
processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
// validate dockerfile
Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
if (!targetDockerfile.toFile().exists()) {
throw new FileNotFoundException("Dockerfile not found.");
}
// replace placeholder if exists
String content = new String(Files.readAllBytes(targetDockerfile));
content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
Files.write(targetDockerfile, content.getBytes());
// build image
String imageNameWithTag = String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName());
processHandler.setText(String.format("Building image ... [%s]", imageNameWithTag));
DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
DockerUtil.ping(docker);
DockerUtil.buildImage(docker, imageNameWithTag, targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
// docker run
String containerServerPort = getPortFromDockerfile(content);
if (StringUtils.isBlank(containerServerPort)) {
if (StringUtils.endsWith(targetFilePath, MavenConstants.TYPE_WAR)) {
containerServerPort = "80";
} else {
containerServerPort = "8080";
}
}
String containerId = DockerUtil.createContainer(docker, String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName()), containerServerPort);
runningContainerId[0] = containerId;
Container container = DockerUtil.runContainer(docker, containerId);
// props
String hostname = new URI(dataModel.getDockerHost()).getHost();
String publicPort = null;
ImmutableList<Container.PortMapping> ports = container.ports();
if (ports != null) {
for (Container.PortMapping portMapping : ports) {
if (StringUtils.equals(containerServerPort, String.valueOf(portMapping.privatePort()))) {
publicPort = String.valueOf(portMapping.publicPort());
}
}
}
processHandler.setText(String.format(Constant.MESSAGE_CONTAINER_STARTED, (hostname != null ? hostname : "localhost") + (publicPort != null ? ":" + publicPort : "")));
return null;
}
use of com.spotify.docker.client.messages.Container in project zalenium by zalando.
the class DockerContainerMock method getMockedDockerContainerClient.
@SuppressWarnings("ConstantConditions")
public static DockerContainerClient getMockedDockerContainerClient(String networkName) {
DockerClient dockerClient = mock(DockerClient.class);
ExecCreation execCreation = mock(ExecCreation.class);
LogStream logStream = mock(LogStream.class);
when(logStream.readFully()).thenReturn("ANY_STRING");
when(execCreation.id()).thenReturn("ANY_ID");
ContainerCreation containerCreation = mock(ContainerCreation.class);
when(containerCreation.id()).thenReturn("ANY_CONTAINER_ID");
AttachedNetwork attachedNetwork = mock(AttachedNetwork.class);
NetworkSettings networkSettings = mock(NetworkSettings.class);
HostConfig hostConfig = mock(HostConfig.class);
ImageInfo imageInfo = mock(ImageInfo.class);
ContainerConfig containerConfig = mock(ContainerConfig.class);
ContainerInfo containerInfo = mock(ContainerInfo.class);
ContainerMount tmpMountedMount = mock(ContainerMount.class);
when(tmpMountedMount.destination()).thenReturn("/tmp/node/tmp/mounted");
when(tmpMountedMount.source()).thenReturn("/tmp/mounted");
ContainerMount homeFolderMount = mock(ContainerMount.class);
when(homeFolderMount.destination()).thenReturn("/tmp/node/home/seluser/folder");
when(homeFolderMount.source()).thenReturn("/tmp/folder");
when(containerInfo.mounts()).thenReturn(ImmutableList.of(tmpMountedMount, homeFolderMount));
when(attachedNetwork.ipAddress()).thenReturn("127.0.0.1");
when(networkSettings.networks()).thenReturn(ImmutableMap.of(networkName, attachedNetwork));
when(networkSettings.ipAddress()).thenReturn("");
when(containerInfo.networkSettings()).thenReturn(networkSettings);
when(hostConfig.extraHosts()).thenReturn(null);
when(containerInfo.hostConfig()).thenReturn(hostConfig);
String[] httpEnvVars = { "zalenium_http_proxy=http://34.211.100.239:8080", "zalenium_https_proxy=http://34.211.100.239:8080" };
when(containerConfig.env()).thenReturn(ImmutableList.copyOf(Arrays.asList(httpEnvVars)));
when(containerInfo.config()).thenReturn(containerConfig);
String containerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
Container container_40000 = mock(Container.class);
when(container_40000.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40000")));
when(container_40000.id()).thenReturn(containerId);
when(container_40000.status()).thenReturn("running");
when(container_40000.image()).thenReturn("elgalu/selenium");
Container container_40001 = mock(Container.class);
when(container_40001.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40001")));
when(container_40001.id()).thenReturn(containerId);
when(container_40001.status()).thenReturn("running");
when(container_40001.image()).thenReturn("elgalu/selenium");
String zaleniumContainerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
Container zalenium = mock(Container.class);
when(zalenium.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium")));
when(zalenium.id()).thenReturn(zaleniumContainerId);
when(zalenium.status()).thenReturn("running");
when(zalenium.image()).thenReturn("dosel/zalenium");
Info dockerInfo = mock(Info.class);
when(dockerInfo.name()).thenReturn("ubuntu_vm");
try {
URL logsLocation = TestUtils.class.getClassLoader().getResource("logs.tar");
URL videosLocation = TestUtils.class.getClassLoader().getResource("videos.tar");
File logsFile = new File(logsLocation.getPath());
File videosFile = new File(videosLocation.getPath());
when(dockerClient.archiveContainer(containerId, "/var/log/cont/")).thenReturn(new FileInputStream(logsFile));
when(dockerClient.archiveContainer(containerId, "/videos/")).thenReturn(new FileInputStream(videosFile));
String[] startVideo = { "bash", "-c", START_RECORDING.getContainerAction() };
String[] stopVideo = { "bash", "-c", STOP_RECORDING.getContainerAction() };
String[] transferLogs = { "bash", "-c", TRANSFER_LOGS.getContainerAction() };
String[] cleanupContainer = { "bash", "-c", CLEANUP_CONTAINER.getContainerAction() };
String[] sendNotificationCompleted = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(COMPLETED.getTestNotificationMessage()) };
String[] sendNotificationTimeout = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(TIMEOUT.getTestNotificationMessage()) };
when(dockerClient.execCreate(containerId, startVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
when(dockerClient.execCreate(containerId, stopVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
when(dockerClient.execCreate(containerId, transferLogs, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
when(dockerClient.execCreate(containerId, cleanupContainer, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
when(dockerClient.execCreate(containerId, sendNotificationCompleted, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
when(dockerClient.execCreate(containerId, sendNotificationTimeout, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
when(dockerClient.execStart(anyString())).thenReturn(logStream);
doNothing().when(dockerClient).stopContainer(anyString(), anyInt());
when(dockerClient.info()).thenReturn(dockerInfo);
when(dockerClient.createContainer(any(ContainerConfig.class), anyString())).thenReturn(containerCreation);
when(dockerClient.listContainers(DockerClient.ListContainersParam.allContainers())).thenReturn(Arrays.asList(container_40000, container_40001, zalenium));
when(containerConfig.labels()).thenReturn(ImmutableMap.of("selenium_firefox_version", "52", "selenium_chrome_version", "58"));
when(imageInfo.config()).thenReturn(containerConfig);
when(dockerClient.inspectContainer(null)).thenReturn(containerInfo);
when(dockerClient.inspectContainer(zaleniumContainerId)).thenReturn(containerInfo);
when(dockerClient.inspectContainer(containerId)).thenReturn(containerInfo);
when(dockerClient.inspectImage(anyString())).thenReturn(imageInfo);
when(dockerClient.listImages(DockerClient.ListImagesParam.byName("elgalu/selenium"))).thenReturn(Collections.emptyList());
} catch (DockerException | InterruptedException | IOException e) {
e.printStackTrace();
}
DockerContainerClient dockerContainerClient = new DockerContainerClient();
dockerContainerClient.setContainerClient(dockerClient);
return dockerContainerClient;
}
use of com.spotify.docker.client.messages.Container in project zalenium by zalando.
the class DockerContainerClient method createContainer.
public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort) {
String containerName = generateContainerName(zaleniumContainerName, nodePort);
loadMountedFolders(zaleniumContainerName);
// In some environments the created containers need to be labeled so the platform can handle them. E.g. Rancher.
loadSeleniumContainerLabels();
loadPullSeleniumImageFlag();
loadIsZaleniumPrivileged(zaleniumContainerName);
loadStorageOpts(zaleniumContainerName);
List<String> binds = generateMountedFolderBinds();
binds.add("/dev/shm:/dev/shm");
String noVncPort = envVars.get("NOVNC_PORT");
String networkMode = getZaleniumNetwork(zaleniumContainerName);
List<String> extraHosts = new ArrayList<>();
extraHosts.add(String.format("%s:%s", DOCKER_FOR_MAC_LOCALHOST_NAME, DOCKER_FOR_MAC_LOCALHOST_IP));
// Allows "--net=host" work. Only supported for Linux.
if (DOCKER_NETWORK_HOST_MODE_NAME.equalsIgnoreCase(networkMode)) {
envVars.put("SELENIUM_HUB_HOST", "127.0.0.1");
envVars.put("SELENIUM_NODE_HOST", "127.0.0.1");
envVars.put("PICK_ALL_RANDOM_PORTS", "true");
try {
String hostName = dockerClient.info().name();
extraHosts.add(String.format("%s:%s", hostName, "127.0.1.0"));
} catch (DockerException | InterruptedException e) {
logger.debug(nodeId + " Error while getting host name", e);
}
}
// Reflect extra hosts of the hub container
final List<String> hubExtraHosts = getContainerExtraHosts(zaleniumContainerName);
extraHosts.addAll(hubExtraHosts);
HostConfig hostConfig = HostConfig.builder().appendBinds(binds).networkMode(networkMode).extraHosts(extraHosts).autoRemove(true).storageOpt(storageOpt).privileged(isZaleniumPrivileged).build();
List<String> flattenedEnvVars = envVars.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList());
flattenedEnvVars.addAll(zaleniumHttpEnvVars);
final String[] exposedPorts = { nodePort, noVncPort };
ContainerConfig.Builder builder = ContainerConfig.builder().image(image).env(flattenedEnvVars).exposedPorts(exposedPorts).hostConfig(hostConfig);
if (seleniumContainerLabels.size() > 0) {
builder.labels(seleniumContainerLabels);
}
final ContainerConfig containerConfig = builder.build();
try {
if (pullSeleniumImage) {
List<Image> images = dockerClient.listImages(DockerClient.ListImagesParam.byName(image));
if (images.size() == 0) {
// If the image has no tag, we add latest, otherwise we end up pulling all the images with that name.
String imageToPull = image.lastIndexOf(':') > 0 ? image : image.concat(":latest");
dockerClient.pull(imageToPull, new AnsiProgressHandler());
}
}
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while checking (and pulling) if the image is present", e);
ga.trackException(e);
}
try {
final ContainerCreation container = dockerClient.createContainer(containerConfig, containerName);
dockerClient.startContainer(container.id());
return new ContainerCreationStatus(true, containerName, nodePort);
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while starting a new container", e);
ga.trackException(e);
return new ContainerCreationStatus(false);
}
}
Aggregations