All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.gesellix.gradle.docker.tasks.GenericDockerTask Maven / Gradle / Ivy

package de.gesellix.gradle.docker.tasks;

import de.gesellix.docker.client.DockerClient;
import de.gesellix.docker.client.DockerClientImpl;
import de.gesellix.docker.client.authentication.AuthConfig;
import de.gesellix.docker.engine.DockerEnv;
import org.gradle.api.DefaultTask;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;

import javax.inject.Inject;
import java.net.Proxy;

import static java.net.Proxy.NO_PROXY;

public class GenericDockerTask extends DefaultTask {

  Property dockerHost;

  @Input
  @Optional
  public Property getDockerHost() {
    return dockerHost;
  }

  /**
   * @deprecated This setter will be removed, please use the Property instead.
   * @see #getDockerHost()
   */
  @Deprecated
  public void setDockerHost(String dockerHost) {
    this.dockerHost.set(dockerHost);
  }

  Property certPath;

  @Input
  @Optional
  public Property getCertPath() {
    return certPath;
  }

  Property proxy;

  @Input
  @Optional
  public Property getProxy() {
    return proxy;
  }

  Property authConfig;

  @Input
  @Optional
  public Property getAuthConfig() {
    return authConfig;
  }

  @Internal
  public String getEncodedAuthConfig() {
    return authConfig.map((AuthConfig a) -> getDockerClient().encodeAuthConfig(a)).getOrElse("");
  }

  /**
   * @deprecated This setter will be removed, please use the Property instead.
   * @see #authConfig
   */
  @Deprecated
  public void setAuthConfigPlain(AuthConfig authConfig) {
    this.authConfig.set(authConfig);
  }

  /**
   * @deprecated This setter will be removed, please use the Property instead.
   * @see #authConfig
   */
  @Deprecated
  public void setAuthConfigEncoded(String ignored) {
    throw new UnsupportedOperationException("please use the authConfig method instead");
  }

  private DockerClient dockerClient;

  void setDockerClient(DockerClient dockerClient) {
    this.dockerClient = dockerClient;
  }

  @Inject
  public GenericDockerTask(ObjectFactory objectFactory) {
    dockerHost = objectFactory.property(String.class);
    certPath = objectFactory.property(String.class);
    proxy = objectFactory.property(Proxy.class);
    authConfig = objectFactory.property(AuthConfig.class);
    setGroup("Docker");
  }

  @Internal
  public DockerClient getDockerClient() {
    if (dockerClient == null) {
      if (dockerHost.isPresent() || certPath.isPresent()) {
        DockerEnv dockerEnv = new DockerEnv();
        if (dockerHost.isPresent()) {
          dockerEnv.setDockerHost(dockerHost.get());
        }

        if (certPath.isPresent()) {
          dockerEnv.setCertPath(getProject().file(certPath.get()).getAbsolutePath());
        }

        dockerClient = new DockerClientImpl(dockerEnv, proxy.getOrElse(NO_PROXY));
      }
      else {
        dockerClient = new DockerClientImpl();
      }
    }

    return dockerClient;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy