Make nomulus work locally (#2349)

Chose the default transaction manager based on RegistryEnvironment. This
makes it possible to run nomulus on Jetty locally. Tested with the
following:

```bash
./gradle :jetty:run -Penvironment=alpha
curl http://localhost:8080/beta.app
```

The docker image is also updated to take an argument that specifies the
environment. It runs locally as well but the container doesn't get
access to locally stored credentials, so it fails to initialize the
transaction manager.
This commit is contained in:
Lai Jiang
2024-03-11 16:05:44 +00:00
committed by GitHub
parent 34a8a94083
commit 1fb27fcf8e
7 changed files with 55 additions and 30 deletions
@@ -18,7 +18,7 @@ import dagger.Component;
import google.registry.config.CredentialModule;
import google.registry.config.RegistryConfig.ConfigModule;
import google.registry.keyring.secretmanager.SecretManagerKeyringModule;
import google.registry.persistence.PersistenceModule.AppEngineJpaTm;
import google.registry.persistence.PersistenceModule.DefaultJpaTm;
import google.registry.persistence.PersistenceModule.ReadOnlyReplicaJpaTm;
import google.registry.persistence.transaction.JpaTransactionManager;
import google.registry.privileges.secretmanager.SecretManagerModule;
@@ -39,8 +39,8 @@ import javax.persistence.EntityManagerFactory;
})
public interface PersistenceComponent {
@AppEngineJpaTm
JpaTransactionManager appEngineJpaTransactionManager();
@DefaultJpaTm
JpaTransactionManager jpaTransactionManager();
@ReadOnlyReplicaJpaTm
JpaTransactionManager readOnlyReplicaJpaTransactionManager();
@@ -220,8 +220,8 @@ public abstract class PersistenceModule {
@Provides
@Singleton
@AppEngineJpaTm
static JpaTransactionManager provideAppEngineJpaTm(
@DefaultJpaTm
static JpaTransactionManager provideDefaultJpaTm(
SqlCredentialStore credentialStore,
@PartialCloudSqlConfigs ImmutableMap<String, String> cloudSqlConfigs,
Clock clock) {
@@ -380,10 +380,10 @@ public abstract class PersistenceModule {
@Documented
public @interface SchemaManagerConnection {}
/** Dagger qualifier for {@link JpaTransactionManager} used for App Engine application. */
/** Dagger qualifier for {@link JpaTransactionManager} used by default. */
@Qualifier
@Documented
@interface AppEngineJpaTm {}
@interface DefaultJpaTm {}
/** Dagger qualifier for {@link JpaTransactionManager} used inside BEAM pipelines. */
@Qualifier
@@ -17,8 +17,6 @@ package google.registry.persistence.transaction;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.appengine.api.utils.SystemProperty;
import com.google.appengine.api.utils.SystemProperty.Environment.Value;
import com.google.common.base.Suppliers;
import google.registry.persistence.DaggerPersistenceComponent;
import google.registry.tools.RegistryToolEnvironment;
@@ -43,34 +41,21 @@ public final class TransactionManagerFactory {
private static JpaTransactionManager createJpaTransactionManager() {
// If we are running a nomulus command, jpaTm will be injected in RegistryCli.java
// by calling setJpaTm().
if (isInAppEngine()) {
return DaggerPersistenceComponent.create().appEngineJpaTransactionManager();
if (RegistryEnvironment.get() != RegistryEnvironment.UNITTEST) {
return DaggerPersistenceComponent.create().jpaTransactionManager();
} else {
return DummyJpaTransactionManager.create();
}
}
private static JpaTransactionManager createReplicaJpaTransactionManager() {
if (isInAppEngine()) {
if (RegistryEnvironment.get() != RegistryEnvironment.UNITTEST) {
return DaggerPersistenceComponent.create().readOnlyReplicaJpaTransactionManager();
} else {
return DummyJpaTransactionManager.create();
}
}
/**
* This function uses App Engine API to determine if the current runtime environment is App
* Engine.
*
* @see <a
* href="https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/utils/SystemProperty">App
* Engine API public doc</a>
*/
private static boolean isInAppEngine() {
// SystemProperty.environment.value() returns null if the current runtime is local JVM
return SystemProperty.environment.value() == Value.Production;
}
/**
* Returns {@link JpaTransactionManager} instance.
*