/** * Get alias name with the generator name as default * @return an alias which is never null; */ protected String getAlias() { return getConfigWithSystemFallbackAndDefault(Config.alias, "fabric8.generator.alias", getName()); }
protected String getFromAsConfigured() { return getConfigWithSystemFallbackAndDefault(Config.from, "fabric8.generator.from", null); }
protected boolean shouldAddImageConfiguration(List<ImageConfiguration> configs) { return !containsBuildConfiguration(configs) || Configs.asBoolean(getConfig(Config.add)); }
@Override public Map<String, Integer> extract(MavenProject project) { Map<String, Integer> answer = new HashMap<>(); File configFile = getConfigLocation(project); if (configFile == null) { // No config file configured return answer; } if (!configFile.exists()) { log.warn("Could not find config: %s. Ignoring.", configFile.getAbsolutePath()); return answer; } try { Map<String, String> configMap = readConfig(configFile); for (Map.Entry<String, String> entry : configMap.entrySet()) { String key = entry.getKey(); if (isValidPortPropertyKey(key)) { addPortIfValid(answer, key, entry.getValue()); } } return answer; } catch (IOException e) { log.warn("Error reading config: [%s], due to: [%s]. Ignoring.", configFile.getAbsolutePath(), e.getMessage()); return answer; } }
String fromMode = getConfigWithSystemFallbackAndDefault(Config.fromMode, "fabric8.generator.fromMode", getFromModeDefault(context.getPlatformMode())); String from = getConfigWithSystemFallbackAndDefault(Config.from, "fabric8.generator.from", null); if ("docker".equalsIgnoreCase(fromMode)) { String fromImage = from; throw new IllegalArgumentException(String.format("Invalid 'fromMode' in generator configuration for '%s'", getName()));
/** * Get Image name with a standard default * * @return Docker image name which is never null */ protected String getImageName() { if (PlatformMode.isOpenShiftMode(getProject().getProperties())) { return getConfigWithSystemFallbackAndDefault(Config.name, "fabric8.generator.name", "%a:%l"); } else { return getConfigWithSystemFallbackAndDefault(Config.name, "fabric8.generator.name", "%g/%a:%l"); } }
protected String getConfigWithSystemFallbackAndDefault(Config name, String key, String defaultVal) { String value = getConfig(name); if (value == null) { value = Configs.getPropertyWithSystemAsFallback(getProject().getProperties(), key); } return value != null ? value : defaultVal; }
public File getConfigLocation(MavenProject project) { String propertyName = getConfigPathPropertyName(); if (StringUtils.isBlank(propertyName)) { return null; } // The system property / Maven property has priority over what is specified in the pom. String configPath = Configs.getPropertyWithSystemAsFallback(project.getProperties(),getConfigPathPropertyName()); if (configPath == null) { configPath = getConfigPathFromProject(project); } if (StringUtils.isBlank(configPath)) { return null; } return Paths.get(configPath).toFile(); }
/** * Reads the configuration from the file. * @param f * @return * @throws IOException */ private Map<String, String> readConfig(File f) throws IOException { Map<String, String> map; if (f.getName().endsWith(JSON_EXTENSION)) { map = flatten(JSON_MAPPER.readValue(f, Map.class)); } else if (f.getName().endsWith(YAML_EXTENSION) || f.getName().endsWith(YML_EXTENSION)) { map = flatten(YAML_MAPPER.readValue(f, Map.class)); } else if (f.getName().endsWith(PROPERTIES_EXTENSION)) { Properties properties = new Properties(); properties.load(new FileInputStream(f)); map = propertiesToMap(properties); } else { throw new IllegalArgumentException("Can't read configuration from: [" + f.getName() + "]. Unknown file extension."); } return map; }
protected void addLatestTagIfSnapshot(BuildImageConfiguration.Builder buildBuilder) { MavenProject project = getProject(); if (project.getVersion().endsWith("-SNAPSHOT")) { buildBuilder.tags(Collections.singletonList("latest")); } }
/** * Flattens a nested map into a Map<String, String>. * @param map The target map. * @return The flattened map. */ private Map<String, String> flatten(Map map) { Map<String, String> flat = new HashMap<>(); for (Object key : map.keySet()) { String stringKey = String.valueOf(key); Object value = map.get(key); if (value instanceof String) { flat.put(stringKey, (String) value); } else if (value instanceof Map) { for (Map.Entry<String, String> entry : flatten((Map) value).entrySet()) { flat.put( new StringBuilder(stringKey).append(DOT).append(entry.getKey()).toString(), entry.getValue()); } } else { flat.put(stringKey, String.valueOf(value)); } } return flat; }
public BaseGenerator(GeneratorContext context, String name, FromSelector fromSelector) { this.context = context; this.name = name; this.fromSelector = fromSelector; this.config = new GeneratorConfig(context.getProject().getProperties(), getName(), context.getConfig()); this.log = new PrefixedLogger(name, context.getLogger()); }
private AppServerHandler getAppServerHandler(GeneratorContext context) { String from = super.getFromAsConfigured(); if (from != null) { // If a base image is provided use this exclusively and dont do a custom lookup return createCustomAppServerHandler(from); } else { return new AppServerDetector(context.getProject()).detect(getConfig(Config.server)); } }
/** * Get the docker registry where the image should be located. * It returns null in Openshift mode. * * @return The docker registry if configured */ protected String getRegistry() { if (!PlatformMode.isOpenShiftMode(getProject().getProperties())) { return getConfigWithSystemFallbackAndDefault(Config.registry, "fabric8.generator.registry", null); } return null; }