Browse Source

Ensure that first time run changes network state

pull/20/head
Kristijan Mitrovic 4 years ago
parent
commit
a472d171ca
1 changed files with 28 additions and 15 deletions
  1. +28
    -15
      bubble-server/src/main/java/bubble/server/listener/BubbleFirstTimeListener.java

+ 28
- 15
bubble-server/src/main/java/bubble/server/listener/BubbleFirstTimeListener.java View File

@@ -18,6 +18,7 @@ import bubble.model.cloud.BubbleNetwork;
import bubble.model.cloud.BubbleNetworkState; import bubble.model.cloud.BubbleNetworkState;
import bubble.server.BubbleConfiguration; import bubble.server.BubbleConfiguration;
import bubble.service.boot.SageHelloService; import bubble.service.boot.SageHelloService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.cobbzilla.wizard.cache.redis.RedisService; import org.cobbzilla.wizard.cache.redis.RedisService;
import org.cobbzilla.wizard.server.RestServer; import org.cobbzilla.wizard.server.RestServer;
@@ -40,6 +41,8 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
public static final long UNLOCK_EXPIRATION = HOURS.toSeconds(48); public static final long UNLOCK_EXPIRATION = HOURS.toSeconds(48);
public static final int UNLOCK_KEY_LEN = 6; public static final int UNLOCK_KEY_LEN = 6;


private static final FirstTimeType FIRST_TIME_TYPE_DEFAULT = FirstTimeType.install;

private static AtomicReference<RedisService> redis = new AtomicReference<>(); private static AtomicReference<RedisService> redis = new AtomicReference<>();
public static String getUnlockKey () { public static String getUnlockKey () {
final RedisService r = redis.get(); final RedisService r = redis.get();
@@ -52,9 +55,16 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
redis.set(configuration.getBean(RedisService.class)); redis.set(configuration.getBean(RedisService.class));


final AccountDAO accountDAO = configuration.getBean(AccountDAO.class); final AccountDAO accountDAO = configuration.getBean(AccountDAO.class);
final var network = configuration.getThisNetwork();
if (FIRST_TIME_FILE.exists()) { if (FIRST_TIME_FILE.exists()) {
try { try {
final FirstTimeType firstTimeType = FirstTimeType.fromString(toStringOrDie(FIRST_TIME_FILE));
try {
final var firstTimeType = FirstTimeType.fromString(toStringOrDie(FIRST_TIME_FILE));
updateNetworkState(configuration, network, firstTimeType);
} catch (Exception e) {
log.warn("Cannot open and/or read/parse first time file " + FIRST_TIME_FILE.getAbsolutePath());
updateNetworkState(configuration, network, FIRST_TIME_TYPE_DEFAULT);
}


final Account adminAccount = accountDAO.getFirstAdmin(); final Account adminAccount = accountDAO.getFirstAdmin();
if (adminAccount == null) { if (adminAccount == null) {
@@ -62,6 +72,7 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
accountDAO.unlock(); accountDAO.unlock();
return; return;
} }

final AccountPolicy adminPolicy = configuration.getBean(AccountPolicyDAO.class).findSingleByAccount(adminAccount.getUuid()); final AccountPolicy adminPolicy = configuration.getBean(AccountPolicyDAO.class).findSingleByAccount(adminAccount.getUuid());
if (adminPolicy == null || !adminPolicy.hasVerifiedNonAuthenticatorAccountContacts()) { if (adminPolicy == null || !adminPolicy.hasVerifiedNonAuthenticatorAccountContacts()) {
log.error("onStart: no AccountPolicy found (or no verified non-authenticator contacts) for admin account (" + adminAccount.getEmail() + "), cannot send first time install message, unlocking now"); log.error("onStart: no AccountPolicy found (or no verified non-authenticator contacts) for admin account (" + adminAccount.getEmail() + "), cannot send first time install message, unlocking now");
@@ -69,9 +80,6 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
return; return;
} }


final BubbleNetwork network = configuration.getThisNetwork();
final SageHelloService helloService = configuration.getBean(SageHelloService.class);

final AccountMessage readyMessage = new AccountMessage() final AccountMessage readyMessage = new AccountMessage()
.setAccount(adminAccount.getUuid()) .setAccount(adminAccount.getUuid())
.setNetwork(network.getUuid()) .setNetwork(network.getUuid())
@@ -82,19 +90,14 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
if (!network.launchLock()) { if (!network.launchLock()) {
log.info("onStart: thisNetwork.launchLock was false, unlocking now"); log.info("onStart: thisNetwork.launchLock was false, unlocking now");
accountDAO.unlock(); accountDAO.unlock();
helloService.setUnlockMessage(readyMessage.setData(null));
return;
readyMessage.setData(null);
} else {
final String unlockKey = randomAlphabetic(UNLOCK_KEY_LEN).toUpperCase();
redis.get().set(UNLOCK_KEY, unlockKey, EX, UNLOCK_EXPIRATION);
readyMessage.setData(unlockKey);
} }
configuration.getBean(SageHelloService.class).setUnlockMessage(readyMessage);


final String unlockKey = randomAlphabetic(UNLOCK_KEY_LEN).toUpperCase();
redis.get().set(UNLOCK_KEY, unlockKey, EX, UNLOCK_EXPIRATION);
helloService.setUnlockMessage(readyMessage.setData(unlockKey));

if (network.getState().equals(BubbleNetworkState.starting)) {
network.setState(firstTimeType.equals(FirstTimeType.install) ? BubbleNetworkState.running
: BubbleNetworkState.restoring);
configuration.getBean(BubbleNetworkDAO.class).update(network);
}
} finally { } finally {
if (!FIRST_TIME_FILE.delete()) { if (!FIRST_TIME_FILE.delete()) {
log.error("onStart: error deleting: "+abs(FIRST_TIME_FILE)); log.error("onStart: error deleting: "+abs(FIRST_TIME_FILE));
@@ -104,8 +107,18 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
if (!accountDAO.locked()) { if (!accountDAO.locked()) {
log.info("onStart: system is not locked, ensuring all accounts are unlocked"); log.info("onStart: system is not locked, ensuring all accounts are unlocked");
accountDAO.unlock(); accountDAO.unlock();
updateNetworkState(configuration, network, FIRST_TIME_TYPE_DEFAULT);
} }
} }
} }


private void updateNetworkState(@NonNull final BubbleConfiguration config, @NonNull final BubbleNetwork network,
@NonNull final FirstTimeType firstTimeType) {
if (network.getState().equals(BubbleNetworkState.starting)) {
network.setState(firstTimeType.equals(FirstTimeType.restore) ? BubbleNetworkState.restoring
: BubbleNetworkState.running);
config.getBean(BubbleNetworkDAO.class).update(network);
}
}

} }

Loading…
Cancel
Save