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

io.fabric8.maven.docker.service.helper.BuildArgResolver Maven / Gradle / Ivy

There is a newer version: 0.45.0
Show newest version
package io.fabric8.maven.docker.service.helper;

import com.google.gson.JsonObject;
import io.fabric8.maven.docker.service.BuildService;
import io.fabric8.maven.docker.util.DockerFileUtil;
import io.fabric8.maven.docker.util.Logger;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Stream;

public class BuildArgResolver {
  private static final String ARG_PREFIX = "docker.buildArg.";
  private final Logger log;

  public BuildArgResolver(Logger log) {
    this.log = log;
  }

  public Map resolveBuildArgs(BuildService.BuildContext buildContext) {
    Map buildArgsFromProject = addBuildArgsFromProperties(buildContext.getMojoParameters().getProject().getProperties());
    Map buildArgsFromSystem = addBuildArgsFromProperties(System.getProperties());
    Map buildArgsFromDockerConfig = addBuildArgsFromDockerConfig();

    //merge build args from all the sources into one map. Different sources maps are allowed to contain duplicate keys between them
    return mergeBuildArgsFrom(buildArgsFromDockerConfig,
      buildContext.getBuildArgs() != null ? buildContext.getBuildArgs() : Collections.emptyMap(),
      buildArgsFromProject,
      buildArgsFromSystem);
  }

  private Map addBuildArgsFromProperties(Properties properties) {
    Map buildArgs = new HashMap<>();
    for (Object keyObj : properties.keySet()) {
      String key = (String) keyObj;
      if (key.startsWith(ARG_PREFIX)) {
        String argKey = key.replaceFirst(ARG_PREFIX, "");
        String value = properties.getProperty(key);

        if (StringUtils.isNotEmpty(value)) {
          buildArgs.put(argKey, value);
        }
      }
    }
    log.debug("Build args set %s", buildArgs);
    return buildArgs;
  }

  private Map addBuildArgsFromDockerConfig() {
    JsonObject dockerConfig = DockerFileUtil.readDockerConfig();
    if (dockerConfig == null) {
      return Collections.emptyMap();
    }

    // add proxies
    Map buildArgs = new HashMap<>();
    if (dockerConfig.has("proxies")) {
      JsonObject proxies = dockerConfig.getAsJsonObject("proxies");
      if (proxies.has("default")) {
        JsonObject defaultProxyObj = proxies.getAsJsonObject("default");
        String[] proxyMapping = new String[]{
          "httpProxy", "http_proxy",
          "httpsProxy", "https_proxy",
          "noProxy", "no_proxy",
          "ftpProxy", "ftp_proxy"
        };

        for (int index = 0; index < proxyMapping.length; index += 2) {
          if (defaultProxyObj.has(proxyMapping[index])) {
            buildArgs.put(proxyMapping[index + 1], defaultProxyObj.get(proxyMapping[index]).getAsString());
          }
        }
      }
    }
    log.debug("Build args set %s", buildArgs);
    return buildArgs;
  }

  @SafeVarargs
  private static Map mergeBuildArgsFrom(Map... buildArgSources) {
    final Map buildArgs = new HashMap<>();
    Stream.of(buildArgSources)
      .filter(Objects::nonNull)
      .flatMap(map -> map.entrySet().stream())
      .forEach(entry -> buildArgs.put(entry.getKey(), entry.getValue()));
    return buildArgs;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy