コード例 #1
0
 /**
  * Return the first host port bound to the requested container port.
  *
  * @param containerId The container in which to find the requested port.
  * @param containerPort The container port to resolve to a host port.
  * @return The first host port bound to the requested container port.
  * @throws HeliosDeploymentException when no host port is found.
  */
 private String getHostPort(final String containerId, final int containerPort)
     throws HeliosDeploymentException {
   final String heliosPort = String.format("%d/tcp", containerPort);
   try {
     final NetworkSettings settings = dockerClient.inspectContainer(containerId).networkSettings();
     for (Map.Entry<String, List<PortBinding>> entry : settings.ports().entrySet()) {
       if (entry.getKey().equals(heliosPort)) {
         for (PortBinding portBinding : entry.getValue()) {
           return portBinding.hostPort();
         }
       }
     }
   } catch (DockerException | InterruptedException e) {
     throw new HeliosDeploymentException(
         String.format(
             "unable to find port binding for %s in container %s.", heliosPort, containerId),
         e);
   }
   throw new HeliosDeploymentException(
       String.format(
           "unable to find port binding for %s in container %s.", heliosPort, containerId));
 }
コード例 #2
0
  /**
   * @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<String>();
    env.addAll(this.env);
    env.add("HELIOS_NAME=" + HELIOS_NAME_PREFIX + this.namespace);
    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(HELIOS_IMAGE)
            .build();

    final ContainerCreation creation;
    try {
      dockerClient.pull(HELIOS_IMAGE);
      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);
    }

    return creation.id();
  }
コード例 #3
0
  private void assertDockerReachable(final int probePort) throws Exception {
    try (final DockerClient docker = getNewDockerClient()) {
      // Pull our base images
      try {
        docker.inspectImage(BUSYBOX);
      } catch (ImageNotFoundException e) {
        docker.pull(BUSYBOX);
      }

      try {
        docker.inspectImage(ALPINE);
      } catch (ImageNotFoundException e) {
        docker.pull(ALPINE);
      }

      // Start a container with an exposed port
      final HostConfig hostConfig =
          HostConfig.builder()
              .portBindings(
                  ImmutableMap.of("4711/tcp", singletonList(PortBinding.of("0.0.0.0", probePort))))
              .build();
      final ContainerConfig config =
          ContainerConfig.builder()
              .image(BUSYBOX)
              .cmd("nc", "-p", "4711", "-lle", "cat")
              .exposedPorts(ImmutableSet.of("4711/tcp"))
              .hostConfig(hostConfig)
              .build();
      final ContainerCreation creation = docker.createContainer(config, testTag + "-probe");
      final String containerId = creation.id();
      docker.startContainer(containerId);

      // Wait for container to come up
      Polling.await(
          5,
          SECONDS,
          new Callable<Object>() {
            @Override
            public Object call() throws Exception {
              final ContainerInfo info = docker.inspectContainer(containerId);
              return info.state().running() ? true : null;
            }
          });

      log.info("Verifying that docker containers are reachable");
      try {
        Polling.awaitUnchecked(
            5,
            SECONDS,
            new Callable<Object>() {
              @Override
              public Object call() throws Exception {
                log.info("Probing: {}:{}", DOCKER_HOST.address(), probePort);
                try (final Socket ignored = new Socket(DOCKER_HOST.address(), probePort)) {
                  return true;
                } catch (IOException e) {
                  return false;
                }
              }
            });
      } catch (TimeoutException e) {
        fail(
            "Please ensure that DOCKER_HOST is set to an address that where containers can "
                + "be reached. If docker is running in a local VM, DOCKER_HOST must be set to the "
                + "address of that VM. If docker can only be reached on a limited port range, "
                + "set the environment variable DOCKER_PORT_RANGE=start:end");
      }

      docker.killContainer(containerId);
    }
  }