Esempio n. 1
0
def info_daemon(name: str, ):
    """
    Get information on a daemon.
    """
    if RUNTIME_CONFIG.exists():
        config = PyroLabConfiguration.from_file(RUNTIME_CONFIG)
    elif USER_CONFIG_FILE.exists():
        config = PyroLabConfiguration.from_file(USER_CONFIG_FILE)
    else:
        typer.secho("No configuration file found.", fg=typer.colors.RED)
        raise typer.Exit()

    if name in config.daemons:
        info = textwrap.indent(str(config.daemons[name].yaml()), '  ')
        typer.echo(f"{name}\n{info}")
    else:
        typer.secho("Daemon not found.", fg=typer.colors.RED)
        raise typer.Exit()
Esempio n. 2
0
def rename_service(
    old_name: str,
    new_name: str,
):
    """
    Rename a service.
    """
    if USER_CONFIG_FILE.exists():
        config = PyroLabConfiguration.from_file(USER_CONFIG_FILE)
        if old_name in config.services:
            config.services[new_name] = config.services.pop(old_name)
            export_config(config, USER_CONFIG_FILE)
        else:
            typer.secho("Nameserver not found.", fg=typer.colors.RED)
            raise typer.Exit()
    else:
        typer.secho("No user configuration file found.", fg=typer.colors.RED)
        raise typer.Exit()
Esempio n. 3
0
    def run(self) -> None:
        """
        Creates and runs the child process.

        When the kill signal is received, gracefully shuts down and removes
        its registration from the nameserver.
        """
        log.info(f"Starting")

        # Set Pyro5 settings for daemon
        log.info("got here")
        self.daemonconfig.update_pyro_config()
        log.info("also got here")
        daemon, uris = self.setup_daemon()
        log.info('are we out of the woods')

        GLOBAL_CONFIG = PyroLabConfiguration.from_file(RUNTIME_CONFIG)

        # Register all services with the nameserver
        log.debug("Registering services with nameserver")
        for sname, sinfo in self.serviceconfigs.items():
            for ns in sinfo.nameservers:
                nscfg = GLOBAL_CONFIG.nameservers[ns]
                try:
                    log.debug(
                        f"Attempting to register '{sname}' with nameserver '{ns}' at {nscfg.host}:{nscfg.ns_port}"
                    )
                    ns = locate_ns(nscfg.host, nscfg.ns_port)
                    ns.register(sname,
                                uris[sname],
                                metadata={sinfo.description})
                except Exception as e:
                    log.exception(e)
                    raise e
        log.debug("All registrations completed")

        for ns in self.daemonconfig.nameservers:
            nscfg = GLOBAL_CONFIG.nameservers[ns]
            ns = locate_ns(nscfg.host, nscfg.ns_port)
            description = f"Daemon for {', '.join([str(sname) for sname in self.serviceconfigs])}"
            ns.register(self.name, uris[self.name], metadata={description})

        # Start the request loop
        self.process_message_queue()
        log.debug(f"entering requestloop")
        daemon.requestLoop(loopCondition=self.stay_alive)
        log.debug(f"requestloop exited")

        # Cleanup
        log.info(f"Shutting down")
        self._timer.cancel()
        for sname, sinfo in self.serviceconfigs.items():
            for ns in sinfo.nameservers:
                nscfg = GLOBAL_CONFIG.nameservers[ns]
                try:
                    ns = locate_ns(nscfg.host, nscfg.ns_port)
                    ns.remove(sname)
                except Exception as e:
                    log.exception(e)
        for ns in self.daemonconfig.nameservers:
            nscfg = GLOBAL_CONFIG.nameservers[ns]
            try:
                ns = locate_ns(nscfg.host, nscfg.ns_port)
                ns.remove(self.name)
            except Exception as e:
                log.exception(e)
Esempio n. 4
0
from pyrolab.nameserver import start_ns, start_ns_loop
from pyrolab.service import Service


__all__ = [
    "locate_ns",
    "Proxy",
    "start_ns",
    "start_ns_loop",
    "Daemon",
    "LockableDaemon",
    "expose",
    "behavior",
    "oneway",
    "serve",
    "update_config",
    "reset_config",
    "Service",
    "PyroLabConfiguration",
    "NameServerConfiguration",
    "DaemonConfiguration",
    "ServiceConfiguration",
]

# If a user config file exists, load the first listed nameserver by default,
# so that locate_ns "just works." 
if USER_CONFIG_FILE.exists():
    cfg = PyroLabConfiguration.from_file(USER_CONFIG_FILE)
    nscfg = next(iter(cfg.nameservers.values()))
    nscfg.update_pyro_config()