async def aquarium_startup(_: FastAPI, aquarium_api: FastAPI): lvl = "INFO" if not os.getenv("AQUARIUM_DEBUG") else "DEBUG" setup_logging(lvl) logger.info("Aquarium startup!") deployment = DeploymentMgr() try: await deployment.preinit() except DeploymentError as e: logger.error(f"Unable to pre-init the node: {e.message}") sys.exit(1) config: Config = Config() kvstore: KV = KV() gstate: GlobalState = GlobalState(config, kvstore) nodemgr: NodeMgr = NodeMgr(gstate) gstate_preinit(gstate) global _main_task _main_task = asyncio.create_task( aquarium_main_task( aquarium_api, config, kvstore, gstate, nodemgr, deployment ) )
async def test_preinit(mocker: MockerFixture) -> None: from gravel.controllers.deployment.mgr import ( DeploymentError, DeploymentMgr, InitStateEnum, ) from gravel.controllers.nodes.systemdisk import OverlayError mgr: DeploymentMgr = DeploymentMgr() mocker.patch( "gravel.controllers.nodes.systemdisk.SystemDisk.exists", new=mocker.AsyncMock(return_value=False), ) await mgr.preinit() assert mgr._preinited == True assert mgr._init_state == InitStateEnum.NONE mgr = DeploymentMgr() mocker.patch( "gravel.controllers.nodes.systemdisk.SystemDisk.exists", new=mocker.AsyncMock(return_value=True), ) mocker.patch( "gravel.controllers.nodes.systemdisk.SystemDisk.enable", new=mocker.AsyncMock(side_effect=OverlayError), ) raised = False try: await mgr.preinit() except DeploymentError: raised = True except Exception: assert False assert raised assert not mgr._preinited assert mgr._init_state == InitStateEnum.NONE mgr = DeploymentMgr() mocker.patch( "gravel.controllers.nodes.systemdisk.SystemDisk.enable", new=mocker.AsyncMock(), ) await mgr.preinit() assert mgr._preinited assert mgr._init_state == InitStateEnum.INSTALLED
async def aquarium_main_task( app: FastAPI, config: Config, kvstore: KV, gstate: GlobalState, nodemgr: NodeMgr, deployment: DeploymentMgr, ) -> None: logger.debug("Starting main Aquarium task.") app.state.deployment = deployment app.state.nodemgr = nodemgr while not _shutting_down and not deployment.installed: logger.debug("Waiting for node to be installed.") await asyncio.sleep(1.0) if _shutting_down: return assert deployment.installed try: await deployment.init() except InitError as e: logger.error(f"Unable to init node: {e.message}") sys.exit(1) logger.info("Init Node Manager.") config.init() kvstore.init() gstate_init(gstate, nodemgr) nodemgr.init() logger.info("Starting Node Manager.") await nodemgr.start() await gstate.start() logger.info("Post-Init Deployment.") deployment.postinit(gstate, nodemgr) app.state.gstate = gstate
def test_instance() -> None: from gravel.controllers.deployment.mgr import ( DeploymentMgr, DeploymentStateEnum, InitStateEnum, ) mgr = DeploymentMgr() assert mgr._init_state == InitStateEnum.NONE assert mgr._deployment_state == DeploymentStateEnum.NONE assert not mgr._preinited assert not mgr._inited
async def test_preinit_fails() -> None: from gravel.controllers.deployment.mgr import ( AlreadyInitedError, AlreadyPreInitedError, DeploymentMgr, ) raised: bool = False mgr = DeploymentMgr() mgr._preinited = True try: await mgr.preinit() except AlreadyPreInitedError: raised = True except Exception: assert False assert raised raised = False mgr = DeploymentMgr() mgr._inited = True try: await mgr.preinit() except AlreadyInitedError: raised = True except Exception: assert False assert raised
def _get_status(dep: DeploymentMgr) -> DeployStatusReplyModel: return DeployStatusReplyModel( installed=dep.installed, status=dep.get_status() )
async def test_init(fs: fake_filesystem.FakeFilesystem) -> None: from gravel.controllers.deployment.mgr import ( AlreadyInitedError, DeploymentMgr, InitError, InitStateEnum, NotPreInitedError, ) mgr = DeploymentMgr() raised: bool = False try: await mgr.init() except NotPreInitedError: raised = True except Exception: assert False assert raised mgr._preinited = True mgr._inited = True raised = False try: await mgr.init() except AlreadyInitedError: raised = True except Exception: assert False assert raised mgr._preinited = True mgr._inited = False await mgr.init() # succeeds because we're not installed yet. assert mgr._init_state < InitStateEnum.INSTALLED # test canonical case, we're installed but don't have a state yet. fs.reset() mgr = DeploymentMgr() mgr._preinited = True mgr._inited = False mgr._init_state = InitStateEnum.INSTALLED assert not fs.exists("/etc/aquarium") await mgr.init() assert mgr._inited assert fs.exists("/etc/aquarium/") assert fs.isdir("/etc/aquarium") assert fs.exists("/etc/aquarium/state.json") # ensure we have a malformed file in /etc/aquarium/state.json fs.reset() mgr = DeploymentMgr() mgr._preinited = True mgr._inited = False mgr._init_state = InitStateEnum.INSTALLED fs.create_dir("/etc/aquarium") with open("/etc/aquarium/state.json", "w") as f: f.write("foobarbaz") raised = False try: await mgr.init() except InitError: raised = True except Exception: assert False assert raised # now we have something in state.json that is not a file fs.reset() mgr = DeploymentMgr() mgr._preinited = True mgr._inited = False mgr._init_state = InitStateEnum.INSTALLED fs.create_dir("/etc/aquarium/state.json") raised = False try: await mgr.init() except InitError: raised = True except Exception: assert False assert raised