Move the environment configuration YAML files into the main JAR

This allows configuration to work properly from the nomulus tool.

TESTED=I built and ran it against several environments, and all worked
properly.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146697124
This commit is contained in:
mcilwain 2017-02-06 12:36:27 -08:00 committed by Ben McIlwain
parent a904f2c6ee
commit bf068e61d9
14 changed files with 51 additions and 128 deletions

View file

@ -14,62 +14,52 @@
package google.registry.config;
import static google.registry.config.RegistryEnvironment.UNITTEST;
import static com.google.common.base.Ascii.toLowerCase;
import static google.registry.util.FormattingLogger.getLoggerForCallerClass;
import static google.registry.util.ResourceUtils.readResourceUtf8;
import com.google.common.base.Optional;
import com.google.common.io.CharStreams;
import google.registry.util.FormattingLogger;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
/** Utility methods for dealing with YAML. */
/**
* Utility methods for dealing with YAML.
*
* <p>There are always two YAML configuration files that are used: the {@code default-config.yaml}
* file, which contains default configuration for all environments, and the environment-specific
* {@code nomulus-config-ENVIRONMENT.yaml} file, which contains overrides for the default values for
* environment-specific settings such as the App Engine project ID. The environment-specific
* configuration can be blank, but it must exist.
*/
public final class YamlUtils {
private static final FormattingLogger logger = getLoggerForCallerClass();
private static final String CUSTOM_CONFIG_PATH = "WEB-INF/nomulus-config.yaml";
private static final String ENVIRONMENT_CONFIG_FORMAT = "files/nomulus-config-%s.yaml";
private static final String YAML_CONFIG_PROD =
readResourceUtf8(RegistryConfig.class, "default-config.yaml");
private static final String YAML_CONFIG_UNITTEST =
readResourceUtf8(RegistryConfig.class, "unittest-config.yaml");
readResourceUtf8(RegistryConfig.class, "files/default-config.yaml");
/**
* Loads the {@link RegistryConfigSettings} POJO from the YAML configuration file(s).
* Loads the {@link RegistryConfigSettings} POJO from the YAML configuration files.
*
* <p>The {@code default-config.yaml} file in this directory is loaded first, and a fatal error is
* thrown if it cannot be found or if there is an error parsing it. Separately, the custom config
* file located in {@code WEB-INF/nomulus-config.yaml} is also loaded and those values merged into
* the POJO. If the custom config file does not exist then an info notice is logged, but if it
* does exist and is invalid then a fatal error is thrown.
* thrown if it cannot be found or if there is an error parsing it. Separately, the
* environment-specific config file named {@code nomulus-config-ENVIRONMENT.yaml} is also loaded
* and those values merged into the POJO.
*
* <p>Unit tests load the {@code unittest-config.yaml} file for custom config.
* @throws IllegalStateException if the configuration files don't exist or are invalid
*/
static RegistryConfigSettings getConfigSettings() {
String yaml = YAML_CONFIG_PROD;
if (RegistryEnvironment.get() == UNITTEST) {
yaml = mergeYaml(yaml, YAML_CONFIG_UNITTEST);
} else {
try {
// We have to load the file this way because App Engine does not allow loading files in the
// WEB-INF directory using a class loader.
FileInputStream fin = new FileInputStream(new File(CUSTOM_CONFIG_PATH));
String customYaml = CharStreams.toString(new InputStreamReader(fin, "UTF-8"));
yaml = mergeYaml(yaml, customYaml);
} catch (IOException e) {
logger.warningfmt(
"There was no custom configuration file to load at %s", CUSTOM_CONFIG_PATH);
}
}
String configFilePath =
String.format(ENVIRONMENT_CONFIG_FORMAT, toLowerCase(RegistryEnvironment.get().name()));
String customYaml = readResourceUtf8(RegistryConfig.class, configFilePath);
try {
return new Yaml().loadAs(yaml, RegistryConfigSettings.class);
String mergedYaml = mergeYaml(YAML_CONFIG_PROD, customYaml);
return new Yaml().loadAs(mergedYaml, RegistryConfigSettings.class);
} catch (Exception e) {
throw new IllegalStateException("Fatal error: Custom YAML configuration file is invalid", e);
throw new IllegalStateException(
"Fatal error: Environment configuration YAML file is invalid", e);
}
}
@ -89,18 +79,14 @@ public final class YamlUtils {
static String mergeYaml(String defaultYaml, String customYaml) {
Yaml yaml = new Yaml();
Map<String, Object> yamlMap = loadAsMap(yaml, defaultYaml).get();
try {
Optional<Map<String, Object>> customMap = loadAsMap(yaml, customYaml);
if (customMap.isPresent()) {
yamlMap = mergeMaps(yamlMap, customMap.get());
logger.infofmt("Successfully loaded custom YAML configuration file.");
} else {
logger.infofmt("Ignoring empty custom YAML configuration file.");
}
return yaml.dump(yamlMap);
} catch (Exception e) {
throw new IllegalStateException("Fatal error: Custom YAML configuration file is invalid", e);
Optional<Map<String, Object>> customMap = loadAsMap(yaml, customYaml);
if (customMap.isPresent()) {
yamlMap = mergeMaps(yamlMap, customMap.get());
logger.infofmt("Successfully loaded environment configuration YAML file.");
} else {
logger.infofmt("Ignoring empty environment configuration YAML file.");
}
return yaml.dump(yamlMap);
}
/**