Example #1
0
def main(host: str,
         port: int,
         count: int,
         text: str,
         queue_size: int,
         msg_delay: float,
         end_delay: float):
    """Syslog test message generator"""
    aio.init_asyncio()

    logging.config.dictConfig({
        'version': 1,
        'formatters': {
            'default': {}},
        'handlers': {
            'syslog': {
                'class': 'hat.syslog.handler.SysLogHandler',
                'host': host,
                'port': port,
                'comm_type': 'TCP',
                'level': 'DEBUG',
                'formatter': 'default',
                'queue_size': queue_size}},
        'root': {
            'level': 'INFO',
            'handlers': ['syslog']},
        'disable_existing_loggers': False})

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(count, text, msg_delay, end_delay))
Example #2
0
def main(conf: typing.Optional[Path], ui_path: Path):
    """Orchestrator"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'orchestrator').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    json_schema_repo.validate('hat://orchestrator.yaml#', conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf, ui_path))
Example #3
0
def test_run_asyncio():
    ident = threading.get_ident()

    def thread():
        time.sleep(0.01)
        signal.pthread_kill(ident, signal.SIGINT)

    t = threading.Thread(target=thread)
    t.start()

    async def f():
        await asyncio.Future()

    with pytest.raises(asyncio.CancelledError):
        aio.run_asyncio(f())
    t.join()
Example #4
0
def test_run_async_example():

    async def run():
        await asyncio.sleep(0)
        return 123

    result = aio.run_asyncio(run())
    assert result == 123
Example #5
0
def main(conf: typing.Optional[Path], ui_path: Path):
    """Main entry point"""
    aio.init_asyncio()

    conf, conf_path = None, conf
    if not conf_path:
        for suffix in ('.yaml', '.yml', '.json'):
            conf_path = (user_conf_dir / 'manager').with_suffix(suffix)
            if conf_path.exists():
                break
    if conf_path.exists():
        conf = json.decode_file(conf_path)
    else:
        conf = common.default_conf
    common.json_schema_repo.validate('hat://manager/main.yaml#', conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf, conf_path, ui_path))
Example #6
0
def main(conf: typing.Optional[Path]):
    """Gateway"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'gateway').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    common.json_schema_repo.validate('hat://gateway/main.yaml#', conf)

    for device_conf in conf['devices']:
        module = importlib.import_module(device_conf['module'])
        if module.json_schema_repo and module.json_schema_id:
            module.json_schema_repo.validate(module.json_schema_id,
                                             device_conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf))
Example #7
0
def main(conf: typing.Optional[Path]):
    """Event Server"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'event').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    common.json_schema_repo.validate('hat://event/main.yaml#', conf)

    sub_confs = ([conf['backend_engine']['backend']] +
                 conf['module_engine']['modules'])
    for sub_conf in sub_confs:
        module = importlib.import_module(sub_conf['module'])
        if module.json_schema_repo and module.json_schema_id:
            module.json_schema_repo.validate(module.json_schema_id, sub_conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf))
Example #8
0
def main(conf: typing.Optional[Path],
         ui_path: Path):
    """GUI Server"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'gui').with_suffix(suffix)
            if conf.exists():
                break
    conf = json.decode_file(conf)
    hat.gui.common.json_schema_repo.validate('hat://gui/main.yaml#', conf)

    for adapter_conf in conf['adapters']:
        module = importlib.import_module(adapter_conf['module'])
        if module.json_schema_repo and module.json_schema_id:
            module.json_schema_repo.validate(module.json_schema_id,
                                             adapter_conf)

    logging.config.dictConfig(conf['log'])

    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main(conf, ui_path))
Example #9
0
def test_run_asyncio_with_multiple_signals():
    ident = threading.get_ident()

    def thread():
        time.sleep(0.01)
        for _ in range(5):
            signal.pthread_kill(ident, signal.SIGINT)
            time.sleep(0.001)

    t = threading.Thread(target=thread)
    t.start()

    async def f():
        await aio.uncancellable(asyncio.sleep(0.02), raise_cancel=False)
        return 1

    assert aio.run_asyncio(f()) == 1
    t.join()
Example #10
0
def main(conf: typing.Optional[Path], log: typing.Optional[str],
         syslog_addr: typing.Optional[str], syslog_pem: typing.Optional[Path],
         ui_addr: typing.Optional[str], ui_pem: typing.Optional[Path],
         ui_path: Path, db_path: typing.Optional[Path],
         db_low_size: typing.Optional[int], db_high_size: typing.Optional[int],
         db_enable_archive: bool, db_disable_journal: bool):
    """Syslog Server"""
    aio.init_asyncio()

    if not conf:
        for suffix in ('.yaml', '.yml', '.json'):
            conf = (user_conf_dir / 'syslog').with_suffix(suffix)
            if conf.exists():
                break
        else:
            conf = None
    if conf:
        conf = json.decode_file(conf)
        common.json_schema_repo.validate('hat://syslog.yaml#', conf)

    if log:
        log_conf = _get_console_log_conf(log)
    elif conf:
        log_conf = conf['log']
    else:
        log_conf = {'version': 1}

    if not syslog_addr:
        syslog_addr = conf['syslog_addr'] if conf else default_syslog_addr

    if not syslog_pem and conf and 'syslog_pem' in conf:
        syslog_pem = Path(conf['syslog_pem'])

    if not ui_addr:
        ui_addr = conf['ui_addr'] if conf else default_ui_addr

    if not ui_pem and conf and 'ui_pem' in conf:
        ui_pem = Path(conf['ui_pem'])

    if not db_path:
        db_path = Path(conf['db_path']) if conf else default_db_path

    if db_low_size is None:
        db_low_size = conf['db_low_size'] if conf else default_db_low_size

    if db_high_size is None:
        db_high_size = conf['db_high_size'] if conf else default_db_high_size

    if not db_enable_archive and conf:
        db_enable_archive = conf['db_enable_archive']

    if not db_disable_journal and conf:
        db_disable_journal = conf['db_disable_journal']

    logging.config.dictConfig(log_conf)
    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(
            async_main(syslog_addr=syslog_addr,
                       syslog_pem=syslog_pem,
                       ui_addr=ui_addr,
                       ui_pem=ui_pem,
                       ui_path=ui_path,
                       db_path=db_path,
                       db_low_size=db_low_size,
                       db_high_size=db_high_size,
                       db_enable_archive=db_enable_archive,
                       db_disable_journal=db_disable_journal))
Example #11
0
def main():
    aio.init_asyncio()
    with contextlib.suppress(asyncio.CancelledError):
        aio.run_asyncio(async_main())