Ver a proveniência

Ensure that first time run changes network state

pull/20/head
Kristijan Mitrovic há 4 anos
ascendente
cometimento
a472d171ca
1 ficheiros alterados com 28 adições e 15 eliminações
  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 Ver ficheiro

@@ -18,6 +18,7 @@ import bubble.model.cloud.BubbleNetwork;
import bubble.model.cloud.BubbleNetworkState;
import bubble.server.BubbleConfiguration;
import bubble.service.boot.SageHelloService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.cobbzilla.wizard.cache.redis.RedisService;
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 int UNLOCK_KEY_LEN = 6;

private static final FirstTimeType FIRST_TIME_TYPE_DEFAULT = FirstTimeType.install;

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

final AccountDAO accountDAO = configuration.getBean(AccountDAO.class);
final var network = configuration.getThisNetwork();
if (FIRST_TIME_FILE.exists()) {
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();
if (adminAccount == null) {
@@ -62,6 +72,7 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
accountDAO.unlock();
return;
}

final AccountPolicy adminPolicy = configuration.getBean(AccountPolicyDAO.class).findSingleByAccount(adminAccount.getUuid());
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");
@@ -69,9 +80,6 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
return;
}

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

final AccountMessage readyMessage = new AccountMessage()
.setAccount(adminAccount.getUuid())
.setNetwork(network.getUuid())
@@ -82,19 +90,14 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
if (!network.launchLock()) {
log.info("onStart: thisNetwork.launchLock was false, unlocking now");
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 {
if (!FIRST_TIME_FILE.delete()) {
log.error("onStart: error deleting: "+abs(FIRST_TIME_FILE));
@@ -104,8 +107,18 @@ public class BubbleFirstTimeListener extends RestServerLifecycleListenerBase<Bub
if (!accountDAO.locked()) {
log.info("onStart: system is not locked, ensuring all accounts are unlocked");
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);
}
}

}

Carregando…
Cancelar
Guardar