Пример #1
0
  /**
   * Predict the gateway IP address for this HeliosSoloDeployment by running and inspecting a
   * container.
   *
   * @return The gateway IP address of the gateway probe container.
   * @throws HeliosDeploymentException if the gateway probe container could not be deployed.
   */
  private String containerGateway() throws HeliosDeploymentException {
    // TODO(negz): Merge with dockerReachableFromContainer() ?
    final ContainerConfig containerConfig =
        ContainerConfig.builder()
            .env(env)
            .hostConfig(HostConfig.builder().build())
            .image(PROBE_IMAGE)
            .cmd(ImmutableList.of("sh", "-c", "while true;do sleep 10;done"))
            .build();

    final ContainerCreation creation;
    try {
      dockerClient.pull(PROBE_IMAGE);
      creation = dockerClient.createContainer(containerConfig);
    } catch (DockerException | InterruptedException e) {
      throw new HeliosDeploymentException("helios-solo gateway probe container creation failed", e);
    }

    try {
      dockerClient.startContainer(creation.id());
      final String gateway =
          dockerClient.inspectContainer(creation.id()).networkSettings().gateway();
      killContainer(creation.id());
      removeContainer(creation.id());
      return gateway;
    } catch (DockerException | InterruptedException e) {
      killContainer(creation.id());
      removeContainer(creation.id());
      throw new HeliosDeploymentException("helios-solo gateway probe failed", e);
    }
  }
Пример #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();
  }
  public static DockerContainer create(
      CreateAgentRequest request, PluginSettings settings, DockerClient docker)
      throws InterruptedException, DockerException, IOException {
    String containerName = UUID.randomUUID().toString();

    HashMap<String, String> labels = labelsFrom(request);
    String imageName = image(request.properties());
    List<String> env = environmentFrom(request, settings, containerName);

    try {
      docker.inspectImage(imageName);
    } catch (ImageNotFoundException ex) {
      LOG.info("Image " + imageName + " not found, attempting to download.");
      docker.pull(imageName);
    }

    ContainerConfig.Builder containerConfigBuilder = ContainerConfig.builder();
    if (request.properties().containsKey("Command")) {
      containerConfigBuilder.cmd(
          splitIntoLinesAndTrimSpaces(request.properties().get("Command"))
              .toArray(new String[] {}));
    }

    ContainerConfig containerConfig =
        containerConfigBuilder.image(imageName).labels(labels).env(env).build();
    ContainerCreation container = docker.createContainer(containerConfig, containerName);
    String id = container.id();

    ContainerInfo containerInfo = docker.inspectContainer(id);

    LOG.debug("Created container " + containerName);
    docker.startContainer(containerName);

    return new DockerContainer(
        containerName, containerInfo.created(), request.properties(), request.environment());
  }
Пример #4
0
  /**
   * @throws HeliosDeploymentException if we cannot reach the Docker daemon's API from inside a
   *     probe container.
   */
  private void assertDockerReachableFromContainer() 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 {
      dockerClient.pull(PROBE_IMAGE);
      creation = dockerClient.createContainer(containerConfig, probeName);
    } catch (DockerException | InterruptedException e) {
      throw new HeliosDeploymentException("helios-solo probe container creation failed", e);
    }

    final ContainerExit exit;
    try {
      dockerClient.startContainer(creation.id());
      exit = dockerClient.waitContainer(creation.id());
    } catch (DockerException | InterruptedException e) {
      killContainer(creation.id());
      removeContainer(creation.id());
      throw new HeliosDeploymentException("helios-solo probe container failed", e);
    }

    if (exit.statusCode() != 0) {
      removeContainer(creation.id());
      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()));
    }

    removeContainer(creation.id());
  }
  @Override
  public int execute(
      final List<String> commandLine,
      final File executionDirectory,
      final Map<String, String> environmentVariables,
      final File temporaryDirectory,
      final File stdoutFile,
      final File stderrFile,
      final boolean redirectErrorStream)
      throws EoulsanException {

    checkNotNull(commandLine, "commandLine argument cannot be null");
    checkNotNull(executionDirectory, "executionDirectory argument cannot be null");
    checkNotNull(stdoutFile, "stdoutFile argument cannot be null");
    checkNotNull(stderrFile, "stderrFile argument cannot be null");

    checkArgument(
        executionDirectory.isDirectory(),
        "execution directory does not exists or is not a directory: "
            + executionDirectory.getAbsolutePath());

    try {

      final List<String> env = new ArrayList<>();

      if (environmentVariables != null) {
        for (Map.Entry<String, String> e : environmentVariables.entrySet()) {
          env.add(e.getKey() + '=' + e.getValue());
        }
      }

      // Pull image if needed
      pullImageIfNotExists(this.dockerClient, this.dockerImage);

      // Create container configuration
      getLogger().fine("Configure container, command to execute: " + commandLine);

      final ContainerConfig.Builder builder =
          ContainerConfig.builder().image(dockerImage).cmd(commandLine);

      // Set the working directory
      builder.workingDir(executionDirectory.getAbsolutePath());

      // Set the UID and GID of the docker process
      if (this.userUid >= 0 && this.userGid >= 0) {
        builder.user(this.userUid + ":" + this.userGid);
      }

      // Define temporary directory
      final List<File> toBind;
      if (temporaryDirectory.isDirectory()) {
        toBind = singletonList(temporaryDirectory);
        env.add(TMP_DIR_ENV_VARIABLE + "=" + temporaryDirectory.getAbsolutePath());
      } else {
        toBind = Collections.emptyList();
      }

      builder.hostConfig(createBinds(executionDirectory, toBind));

      // Set environment variables
      builder.env(env);

      // Create container
      final ContainerCreation creation = this.dockerClient.createContainer(builder.build());

      // Get container id
      final String containerId = creation.id();

      // Start container
      getLogger().fine("Start of the Docker container: " + containerId);
      this.dockerClient.startContainer(containerId);

      // Redirect stdout and stderr
      final LogStream logStream =
          this.dockerClient.logs(
              containerId, LogsParameter.FOLLOW, LogsParameter.STDERR, LogsParameter.STDOUT);
      redirect(logStream, stdoutFile, stderrFile, redirectErrorStream);

      // Wait the end of the container
      getLogger().fine("Wait the end of the Docker container: " + containerId);
      this.dockerClient.waitContainer(containerId);

      // Get process exit code
      final ContainerInfo info = this.dockerClient.inspectContainer(containerId);
      final int exitValue = info.state().exitCode();
      getLogger().fine("Exit value: " + exitValue);

      // Stop container before removing it
      this.dockerClient.stopContainer(containerId, SECOND_TO_WAIT_BEFORE_KILLING_CONTAINER);

      // Remove container
      getLogger().fine("Remove Docker container: " + containerId);
      try {
        this.dockerClient.removeContainer(containerId);
      } catch (DockerException | InterruptedException e) {
        EoulsanLogger.getLogger().severe("Unable to remove Docker container: " + containerId);
      }

      return exitValue;
    } catch (DockerException | InterruptedException e) {
      throw new EoulsanException(e);
    }
  }
Пример #6
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);
    }
  }