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

@ -53,7 +53,6 @@ zip_file(
"env/common/default/WEB-INF/logging.properties",
"env/common/default/WEB-INF/queue.xml",
"env/common/default/WEB-INF/web.xml",
"env/production/common/WEB-INF/nomulus-config.yaml",
"env/production/default/WEB-INF/appengine-web.xml",
"env/production/default/WEB-INF/cron.xml",
"//java/google/registry/module/frontend:frontend_jar_deploy.jar",
@ -61,7 +60,6 @@ zip_file(
out = "registry_default.war",
mappings = {
"domain_registry/java/google/registry/env/common/default": "",
"domain_registry/java/google/registry/env/production/common": "",
"domain_registry/java/google/registry/env/production/default": "",
"domain_registry/java/google/registry/module/frontend": "WEB-INF/lib",
},
@ -77,14 +75,12 @@ zip_file(
"env/common/backend/WEB-INF/logging.properties",
"env/common/backend/WEB-INF/web.xml",
"env/production/backend/WEB-INF/appengine-web.xml",
"env/production/common/WEB-INF/nomulus-config.yaml",
"//java/google/registry/module/backend:backend_jar_deploy.jar",
],
out = "registry_backend.war",
mappings = {
"domain_registry/java/google/registry/env/common/backend": "",
"domain_registry/java/google/registry/env/production/backend": "",
"domain_registry/java/google/registry/env/production/common": "",
"domain_registry/java/google/registry/module/backend": "WEB-INF/lib",
},
deps = [
@ -98,14 +94,12 @@ zip_file(
srcs = [
"env/common/tools/WEB-INF/logging.properties",
"env/common/tools/WEB-INF/web.xml",
"env/production/common/WEB-INF/nomulus-config.yaml",
"env/production/tools/WEB-INF/appengine-web.xml",
"//java/google/registry/module/tools:tools_jar_deploy.jar",
],
out = "registry_tools.war",
mappings = {
"domain_registry/java/google/registry/env/common/tools": "",
"domain_registry/java/google/registry/env/production/common": "",
"domain_registry/java/google/registry/env/production/tools": "",
"domain_registry/java/google/registry/module/tools": "WEB-INF/lib",
},
@ -138,13 +132,11 @@ registry_ear_file(
zip_file(
name = "registry_default_sandbox_war",
srcs = [
"env/sandbox/common/WEB-INF/nomulus-config.yaml",
"env/sandbox/default/WEB-INF/appengine-web.xml",
"env/sandbox/default/WEB-INF/cron.xml",
],
out = "registry_default_sandbox.war",
mappings = {
"domain_registry/java/google/registry/env/sandbox/common": "",
"domain_registry/java/google/registry/env/sandbox/default": "",
},
deps = [":registry_default_war"],
@ -154,12 +146,10 @@ zip_file(
name = "registry_backend_sandbox_war",
srcs = [
"env/sandbox/backend/WEB-INF/appengine-web.xml",
"env/sandbox/common/WEB-INF/nomulus-config.yaml",
],
out = "registry_backend_sandbox.war",
mappings = {
"domain_registry/java/google/registry/env/sandbox/backend": "",
"domain_registry/java/google/registry/env/sandbox/common": "",
},
deps = [":registry_backend_war"],
)
@ -167,12 +157,10 @@ zip_file(
zip_file(
name = "registry_tools_sandbox_war",
srcs = [
"env/sandbox/common/WEB-INF/nomulus-config.yaml",
"env/sandbox/tools/WEB-INF/appengine-web.xml",
],
out = "registry_tools_sandbox.war",
mappings = {
"domain_registry/java/google/registry/env/sandbox/common": "",
"domain_registry/java/google/registry/env/sandbox/tools": "",
},
deps = [":registry_tools_war"],
@ -200,13 +188,11 @@ registry_ear_file(
zip_file(
name = "registry_default_alpha_war",
srcs = [
"env/alpha/common/WEB-INF/nomulus-config.yaml",
"env/alpha/default/WEB-INF/appengine-web.xml",
"env/alpha/default/WEB-INF/cron.xml",
],
out = "registry_default_alpha.war",
mappings = {
"domain_registry/java/google/registry/env/alpha/common": "",
"domain_registry/java/google/registry/env/alpha/default": "",
},
deps = [":registry_default_war"],
@ -216,12 +202,10 @@ zip_file(
name = "registry_backend_alpha_war",
srcs = [
"env/alpha/backend/WEB-INF/appengine-web.xml",
"env/alpha/common/WEB-INF/nomulus-config.yaml",
],
out = "registry_backend_alpha.war",
mappings = {
"domain_registry/java/google/registry/env/alpha/backend": "",
"domain_registry/java/google/registry/env/alpha/common": "",
},
deps = [":registry_backend_war"],
)
@ -229,12 +213,10 @@ zip_file(
zip_file(
name = "registry_tools_alpha_war",
srcs = [
"env/alpha/common/WEB-INF/nomulus-config.yaml",
"env/alpha/tools/WEB-INF/appengine-web.xml",
],
out = "registry_tools_alpha.war",
mappings = {
"domain_registry/java/google/registry/env/alpha/common": "",
"domain_registry/java/google/registry/env/alpha/tools": "",
},
deps = [":registry_tools_war"],
@ -262,13 +244,11 @@ registry_ear_file(
zip_file(
name = "registry_default_crash_war",
srcs = [
"env/crash/common/WEB-INF/nomulus-config.yaml",
"env/crash/default/WEB-INF/appengine-web.xml",
"env/crash/default/WEB-INF/cron.xml",
],
out = "registry_default_crash.war",
mappings = {
"domain_registry/java/google/registry/env/crash/common": "",
"domain_registry/java/google/registry/env/crash/default": "",
},
deps = [":registry_default_war"],
@ -278,12 +258,10 @@ zip_file(
name = "registry_backend_crash_war",
srcs = [
"env/crash/backend/WEB-INF/appengine-web.xml",
"env/crash/common/WEB-INF/nomulus-config.yaml",
],
out = "registry_backend_crash.war",
mappings = {
"domain_registry/java/google/registry/env/crash/backend": "",
"domain_registry/java/google/registry/env/crash/common": "",
},
deps = [":registry_backend_war"],
)
@ -291,12 +269,10 @@ zip_file(
zip_file(
name = "registry_tools_crash_war",
srcs = [
"env/crash/common/WEB-INF/nomulus-config.yaml",
"env/crash/tools/WEB-INF/appengine-web.xml",
],
out = "registry_tools_crash.war",
mappings = {
"domain_registry/java/google/registry/env/crash/common": "",
"domain_registry/java/google/registry/env/crash/tools": "",
},
deps = [":registry_tools_war"],
@ -324,12 +300,10 @@ registry_ear_file(
zip_file(
name = "registry_default_local_war",
srcs = [
"env/local/common/WEB-INF/nomulus-config.yaml",
"env/local/default/WEB-INF/appengine-web.xml",
],
out = "registry_default_local.war",
mappings = {
"domain_registry/java/google/registry/env/local/common": "",
"domain_registry/java/google/registry/env/local/default": "",
},
deps = [":registry_default_war"],
@ -339,12 +313,10 @@ zip_file(
name = "registry_backend_local_war",
srcs = [
"env/local/backend/WEB-INF/appengine-web.xml",
"env/local/common/WEB-INF/nomulus-config.yaml",
],
out = "registry_backend_local.war",
mappings = {
"domain_registry/java/google/registry/env/local/backend": "",
"domain_registry/java/google/registry/env/local/common": "",
},
deps = [":registry_backend_war"],
)
@ -352,12 +324,10 @@ zip_file(
zip_file(
name = "registry_tools_local_war",
srcs = [
"env/local/common/WEB-INF/nomulus-config.yaml",
"env/local/tools/WEB-INF/appengine-web.xml",
],
out = "registry_tools_local.war",
mappings = {
"domain_registry/java/google/registry/env/local/common": "",
"domain_registry/java/google/registry/env/local/tools": "",
},
deps = [":registry_tools_war"],

View file

@ -7,7 +7,7 @@ licenses(["notice"]) # Apache 2.0
java_library(
name = "config",
srcs = glob(["*.java"]),
resources = glob(["*.yaml"]),
resources = glob(["files/*.yaml"]),
deps = [
"//java/google/registry/util",
"@com_google_appengine_api_1_0_sdk",

View file

@ -19,7 +19,6 @@ import static google.registry.config.ConfigUtils.makeUrl;
import static google.registry.config.YamlUtils.getConfigSettings;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
@ -27,6 +26,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
import dagger.Module;
import dagger.Provides;
import google.registry.config.RegistryConfigSettings.AppEngine.ToolsServiceUrl;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.net.URI;
@ -43,6 +43,11 @@ import org.joda.time.Duration;
/**
* Central clearing-house for all configuration.
*
* <p>This class does not represent the total configuration of the Nomulus service. It's <b>only
* meant for settings that need to be configured <i>once</i></b>. Settings which may be subject to
* change in the future, should instead be retrieved from Datastore. The {@link
* google.registry.model.registry.Registry Registry} class is one such example of this.
*/
public final class RegistryConfig {
@ -54,26 +59,7 @@ public final class RegistryConfig {
String value() default "";
}
/**
* Configuration example for the Nomulus codebase.
*
* <p>The Nomulus codebase contains many classes that inject configurable settings. This is
* the centralized class that is used by default to configure them all, in hard-coded type-safe
* Java code.
*
* <p>This class does not represent the total configuration of the Nomulus service. It's
* <b>only meant for settings that need to be configured <i>once</i></b>. Settings which may
* be subject to change in the future, should instead be retrieved from Datastore. The
* {@link google.registry.model.registry.Registry Registry} class is one such example of this.
*
* <h3>Customization</h3>
*
* <p>It is recommended that users do not modify this file within a forked repository. It is
* preferable to modify these settings by swapping out this module with a separate copied version
* in the user's repository. For this to work, other files need to be copied too, such as the
* {@code @Component} instances under {@code google.registry.module}. This allows modules to be
* substituted at the {@code @Component} level.
*/
/** Dagger module for providing configuration settings. */
@Module
public static final class ConfigModule {
@ -1056,22 +1042,8 @@ public final class RegistryConfig {
* <p>This is used by the {@code nomulus} tool to connect to the App Engine remote API.
*/
public static HostAndPort getServer() {
// TODO(b/33386530): Make this configurable in a way that is accessible from the nomulus
// command-line tool.
switch (RegistryEnvironment.get()) {
case PRODUCTION:
return HostAndPort.fromParts("tools-dot-domain-registry.appspot.com", 443);
case LOCAL:
return HostAndPort.fromParts("localhost", 8080);
case UNITTEST:
throw new UnsupportedOperationException("Unit tests can't spin up a full server");
default:
return HostAndPort.fromParts(
String.format(
"tools-dot-domain-registry-%s.appspot.com",
Ascii.toLowerCase(RegistryEnvironment.get().name())),
443);
}
ToolsServiceUrl url = CONFIG_SETTINGS.get().appEngine.toolsServiceUrl;
return HostAndPort.fromParts(url.hostName, url.port);
}
/** Returns the amount of time a singleton should be cached, before expiring. */

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);
}
/**

View file

@ -0,0 +1 @@
# Add environment-specific configuration here.