2022-05-10 17:32:56 +00:00
|
|
|
# piker: trading gear for hackers
|
|
|
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of pikers)
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
'''
|
2020-05-23 19:39:17 +00:00
|
|
|
CLI commons.
|
2022-05-10 17:32:56 +00:00
|
|
|
|
|
|
|
'''
|
2020-05-23 19:39:17 +00:00
|
|
|
import os
|
2023-09-28 16:13:34 +00:00
|
|
|
# from contextlib import AsyncExitStack
|
2023-06-13 00:19:18 +00:00
|
|
|
from types import ModuleType
|
2020-05-23 19:39:17 +00:00
|
|
|
|
|
|
|
import click
|
2021-03-22 01:56:06 +00:00
|
|
|
import trio
|
2020-05-23 19:39:17 +00:00
|
|
|
import tractor
|
|
|
|
|
2023-03-08 20:14:39 +00:00
|
|
|
from ..log import (
|
|
|
|
get_console_log,
|
|
|
|
get_logger,
|
|
|
|
colorize_json,
|
|
|
|
)
|
2021-09-07 01:26:28 +00:00
|
|
|
from ..brokers import get_brokermod
|
2023-03-08 20:14:39 +00:00
|
|
|
from ..service import (
|
2023-01-09 23:22:10 +00:00
|
|
|
_default_registry_host,
|
|
|
|
_default_registry_port,
|
2022-11-07 15:21:52 +00:00
|
|
|
)
|
2021-09-07 01:26:28 +00:00
|
|
|
from .. import config
|
2020-05-23 19:39:17 +00:00
|
|
|
|
2020-07-15 12:28:13 +00:00
|
|
|
|
2023-06-24 21:24:05 +00:00
|
|
|
log = get_logger('piker.cli')
|
2020-05-23 19:39:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
2022-02-17 19:29:41 +00:00
|
|
|
@click.option(
|
2023-09-28 16:13:34 +00:00
|
|
|
'--loglevel',
|
|
|
|
'-l',
|
|
|
|
default='warning',
|
|
|
|
help='Logging level',
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'--tl',
|
2022-02-17 19:29:41 +00:00
|
|
|
is_flag=True,
|
2023-09-28 16:13:34 +00:00
|
|
|
help='Enable tractor-runtime logs',
|
2022-02-17 19:29:41 +00:00
|
|
|
)
|
2023-02-15 17:08:23 +00:00
|
|
|
@click.option(
|
2023-09-28 16:13:34 +00:00
|
|
|
'--pdb',
|
2023-02-15 17:08:23 +00:00
|
|
|
is_flag=True,
|
2023-09-28 16:13:34 +00:00
|
|
|
help='Enable tractor debug mode',
|
2023-02-15 17:08:23 +00:00
|
|
|
)
|
2023-09-28 16:13:34 +00:00
|
|
|
@click.option(
|
|
|
|
'--maddr',
|
|
|
|
'-m',
|
|
|
|
default=None,
|
|
|
|
help='Multiaddrs to bind or contact',
|
|
|
|
)
|
|
|
|
# @click.option(
|
|
|
|
# '--tsdb',
|
|
|
|
# is_flag=True,
|
|
|
|
# help='Enable local ``marketstore`` instance'
|
|
|
|
# )
|
|
|
|
# @click.option(
|
|
|
|
# '--es',
|
|
|
|
# is_flag=True,
|
|
|
|
# help='Enable local ``elasticsearch`` instance'
|
|
|
|
# )
|
2022-11-07 15:21:52 +00:00
|
|
|
def pikerd(
|
2023-09-28 16:13:34 +00:00
|
|
|
maddr: str | None,
|
2022-11-07 15:21:52 +00:00
|
|
|
loglevel: str,
|
|
|
|
tl: bool,
|
|
|
|
pdb: bool,
|
2023-09-28 16:13:34 +00:00
|
|
|
# tsdb: bool,
|
|
|
|
# es: bool,
|
2022-11-07 15:21:52 +00:00
|
|
|
):
|
2022-02-17 19:29:41 +00:00
|
|
|
'''
|
|
|
|
Spawn the piker broker-daemon.
|
|
|
|
|
|
|
|
'''
|
2023-09-28 16:13:34 +00:00
|
|
|
from cornerboi._debug import open_crash_handler
|
|
|
|
with open_crash_handler():
|
|
|
|
log = get_console_log(loglevel, name='cli')
|
|
|
|
|
|
|
|
if pdb:
|
|
|
|
log.warning((
|
|
|
|
"\n"
|
|
|
|
"!!! YOU HAVE ENABLED DAEMON DEBUG MODE !!!\n"
|
|
|
|
"When a `piker` daemon crashes it will block the "
|
|
|
|
"task-thread until resumed from console!\n"
|
|
|
|
"\n"
|
|
|
|
))
|
|
|
|
|
|
|
|
# service-actor registry endpoint socket-address
|
|
|
|
regaddrs: list[tuple[str, int]] | None = None
|
|
|
|
|
|
|
|
conf, _ = config.load(
|
|
|
|
conf_name='conf',
|
2022-11-07 15:21:52 +00:00
|
|
|
)
|
2023-09-28 16:13:34 +00:00
|
|
|
network: dict = conf.get('network')
|
|
|
|
if network is None:
|
|
|
|
regaddrs = [(
|
|
|
|
_default_registry_host,
|
|
|
|
_default_registry_port,
|
|
|
|
)]
|
|
|
|
|
|
|
|
from .. import service
|
2023-09-29 19:20:56 +00:00
|
|
|
from tractor._multiaddr import parse_maddr
|
2023-09-28 16:13:34 +00:00
|
|
|
|
|
|
|
# transport-oriented endpoint multi-addresses
|
|
|
|
eps: dict[
|
|
|
|
str, # service name, eg. `pikerd`, `emsd`..
|
|
|
|
|
|
|
|
# libp2p style multi-addresses parsed into prot layers
|
|
|
|
list[dict[str, str | int]]
|
|
|
|
] = {}
|
|
|
|
|
|
|
|
if (
|
|
|
|
not maddr
|
|
|
|
and network
|
2022-02-17 19:29:41 +00:00
|
|
|
):
|
2023-09-28 16:13:34 +00:00
|
|
|
# load network section and (attempt to) connect all endpoints
|
|
|
|
# which are reachable B)
|
|
|
|
for key, maddrs in network.items():
|
|
|
|
match key:
|
|
|
|
|
|
|
|
# TODO: resolve table across multiple discov
|
|
|
|
# prots Bo
|
|
|
|
case 'resolv':
|
|
|
|
pass
|
|
|
|
|
|
|
|
case 'pikerd':
|
|
|
|
dname: str = key
|
|
|
|
for maddr in maddrs:
|
2023-09-29 19:20:56 +00:00
|
|
|
layers: dict = parse_maddr(maddr)
|
2023-09-28 16:13:34 +00:00
|
|
|
eps.setdefault(
|
|
|
|
dname,
|
|
|
|
[],
|
|
|
|
).append(layers)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# presume user is manually specifying the root actor ep.
|
2023-09-29 19:20:56 +00:00
|
|
|
eps['pikerd'] = [parse_maddr(maddr)]
|
2023-09-28 16:13:34 +00:00
|
|
|
|
|
|
|
regaddrs: list[tuple[str, int]] = []
|
|
|
|
for layers in eps['pikerd']:
|
|
|
|
regaddrs.append((
|
|
|
|
layers['ipv4']['addr'],
|
|
|
|
layers['tcp']['port'],
|
|
|
|
))
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
service_mngr: service.Services
|
|
|
|
|
|
|
|
async with (
|
|
|
|
service.open_pikerd(
|
|
|
|
registry_addrs=regaddrs,
|
|
|
|
loglevel=loglevel,
|
|
|
|
debug_mode=pdb,
|
|
|
|
|
|
|
|
) as service_mngr, # normally delivers a ``Services`` handle
|
|
|
|
|
|
|
|
# AsyncExitStack() as stack,
|
|
|
|
):
|
|
|
|
# TODO: spawn all other sub-actor daemons according to
|
|
|
|
# multiaddress endpoint spec defined by user config
|
|
|
|
assert service_mngr
|
|
|
|
|
|
|
|
# if tsdb:
|
|
|
|
# dname, conf = await stack.enter_async_context(
|
|
|
|
# service.marketstore.start_ahab_daemon(
|
|
|
|
# service_mngr,
|
|
|
|
# loglevel=loglevel,
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
# log.info(f'TSDB `{dname}` up with conf:\n{conf}')
|
|
|
|
|
|
|
|
# if es:
|
|
|
|
# dname, conf = await stack.enter_async_context(
|
|
|
|
# service.elastic.start_ahab_daemon(
|
|
|
|
# service_mngr,
|
|
|
|
# loglevel=loglevel,
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
# log.info(f'DB `{dname}` up with conf:\n{conf}')
|
|
|
|
|
|
|
|
await trio.sleep_forever()
|
|
|
|
|
|
|
|
trio.run(main)
|
2020-05-23 19:39:17 +00:00
|
|
|
|
|
|
|
|
2022-02-15 19:03:24 +00:00
|
|
|
@click.group(context_settings=config._context_defaults)
|
2021-05-18 12:38:13 +00:00
|
|
|
@click.option(
|
|
|
|
'--brokers', '-b',
|
2022-11-15 23:07:02 +00:00
|
|
|
default=None,
|
2021-05-18 12:38:13 +00:00
|
|
|
multiple=True,
|
|
|
|
help='Broker backend to use'
|
|
|
|
)
|
2020-05-23 19:39:17 +00:00
|
|
|
@click.option('--loglevel', '-l', default='warning', help='Logging level')
|
2020-08-02 02:23:19 +00:00
|
|
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
2020-05-23 19:39:17 +00:00
|
|
|
@click.option('--configdir', '-c', help='Configuration directory')
|
2023-09-28 16:13:34 +00:00
|
|
|
@click.option('--maddr', '-m', default=None, help='Multiaddr to bind')
|
|
|
|
@click.option('--raddr', '-r', default=None, help='Registrar addr to contact')
|
2020-05-23 19:39:17 +00:00
|
|
|
@click.pass_context
|
2022-11-07 16:22:38 +00:00
|
|
|
def cli(
|
|
|
|
ctx: click.Context,
|
|
|
|
brokers: list[str],
|
|
|
|
loglevel: str,
|
|
|
|
tl: bool,
|
|
|
|
configdir: str,
|
2023-09-28 16:13:34 +00:00
|
|
|
|
|
|
|
# TODO: make these list[str] with multiple -m maddr0 -m maddr1
|
|
|
|
maddr: str,
|
|
|
|
raddr: str,
|
2022-11-07 16:22:38 +00:00
|
|
|
|
|
|
|
) -> None:
|
2020-05-23 19:39:17 +00:00
|
|
|
if configdir is not None:
|
|
|
|
assert os.path.isdir(configdir), f"`{configdir}` is not a valid path"
|
|
|
|
config._override_config_dir(configdir)
|
|
|
|
|
2023-05-29 21:41:40 +00:00
|
|
|
# TODO: for typer see
|
|
|
|
# https://typer.tiangolo.com/tutorial/commands/context/
|
2020-05-23 19:39:17 +00:00
|
|
|
ctx.ensure_object(dict)
|
2021-05-18 12:38:13 +00:00
|
|
|
|
2022-11-15 23:07:02 +00:00
|
|
|
if not brokers:
|
|
|
|
# (try to) load all (supposedly) supported data/broker backends
|
|
|
|
from piker.brokers import __brokers__
|
|
|
|
brokers = __brokers__
|
|
|
|
|
2023-06-13 00:19:18 +00:00
|
|
|
brokermods: dict[str, ModuleType] = {
|
|
|
|
broker: get_brokermod(broker) for broker in brokers
|
|
|
|
}
|
2022-11-15 23:07:02 +00:00
|
|
|
assert brokermods
|
2021-05-18 12:38:13 +00:00
|
|
|
|
2023-09-28 16:13:34 +00:00
|
|
|
regaddr: tuple[str, int] = (
|
|
|
|
_default_registry_host,
|
|
|
|
_default_registry_port,
|
|
|
|
)
|
2022-11-07 16:22:38 +00:00
|
|
|
|
2020-05-23 19:39:17 +00:00
|
|
|
ctx.obj.update({
|
2021-05-18 12:38:13 +00:00
|
|
|
'brokers': brokers,
|
|
|
|
'brokermods': brokermods,
|
2020-05-23 19:39:17 +00:00
|
|
|
'loglevel': loglevel,
|
2020-08-02 02:23:19 +00:00
|
|
|
'tractorloglevel': None,
|
2020-05-23 19:39:17 +00:00
|
|
|
'log': get_console_log(loglevel),
|
2022-02-15 19:03:24 +00:00
|
|
|
'confdir': config._config_dir,
|
|
|
|
'wl_path': config._watchlists_data_path,
|
2023-09-28 16:13:34 +00:00
|
|
|
'registry_addr': regaddr,
|
2020-05-23 19:39:17 +00:00
|
|
|
})
|
|
|
|
|
2020-08-02 02:23:19 +00:00
|
|
|
# allow enabling same loglevel in ``tractor`` machinery
|
|
|
|
if tl:
|
|
|
|
ctx.obj.update({'tractorloglevel': loglevel})
|
|
|
|
|
2020-05-23 19:39:17 +00:00
|
|
|
|
2020-08-04 01:31:56 +00:00
|
|
|
@cli.command()
|
|
|
|
@click.option('--tl', is_flag=True, help='Enable tractor logging')
|
2023-01-10 21:41:20 +00:00
|
|
|
@click.argument('ports', nargs=-1, required=False)
|
2020-08-04 01:31:56 +00:00
|
|
|
@click.pass_obj
|
2023-01-10 21:41:20 +00:00
|
|
|
def services(config, tl, ports):
|
2020-08-04 01:31:56 +00:00
|
|
|
|
2023-03-08 20:14:39 +00:00
|
|
|
from ..service import (
|
2023-01-10 21:41:20 +00:00
|
|
|
open_piker_runtime,
|
|
|
|
_default_registry_port,
|
|
|
|
_default_registry_host,
|
|
|
|
)
|
|
|
|
|
|
|
|
host = _default_registry_host
|
|
|
|
if not ports:
|
|
|
|
ports = [_default_registry_port]
|
2021-06-08 19:57:01 +00:00
|
|
|
|
2022-09-29 22:04:44 +00:00
|
|
|
async def list_services():
|
2023-01-10 21:41:20 +00:00
|
|
|
nonlocal host
|
2022-09-29 22:04:44 +00:00
|
|
|
async with (
|
|
|
|
open_piker_runtime(
|
|
|
|
name='service_query',
|
|
|
|
loglevel=config['loglevel'] if tl else None,
|
|
|
|
),
|
|
|
|
tractor.get_arbiter(
|
2023-01-10 21:41:20 +00:00
|
|
|
host=host,
|
|
|
|
port=ports[0]
|
2022-09-29 22:04:44 +00:00
|
|
|
) as portal
|
|
|
|
):
|
2021-12-10 16:51:56 +00:00
|
|
|
registry = await portal.run_from_ns('self', 'get_registry')
|
2020-08-04 01:31:56 +00:00
|
|
|
json_d = {}
|
2022-02-28 16:56:55 +00:00
|
|
|
for key, socket in registry.items():
|
2020-08-04 01:31:56 +00:00
|
|
|
host, port = socket
|
2022-02-28 16:56:55 +00:00
|
|
|
json_d[key] = f'{host}:{port}'
|
|
|
|
click.echo(f"{colorize_json(json_d)}")
|
2020-08-04 01:31:56 +00:00
|
|
|
|
2022-09-29 22:04:44 +00:00
|
|
|
trio.run(list_services)
|
2020-08-04 01:31:56 +00:00
|
|
|
|
|
|
|
|
2020-07-15 12:28:13 +00:00
|
|
|
def _load_clis() -> None:
|
2023-09-28 16:13:34 +00:00
|
|
|
# from ..service import elastic # noqa
|
2021-04-14 15:01:00 +00:00
|
|
|
from ..brokers import cli # noqa
|
|
|
|
from ..ui import cli # noqa
|
|
|
|
from ..watchlists import cli # noqa
|
2020-07-15 12:28:13 +00:00
|
|
|
|
2023-05-29 21:41:40 +00:00
|
|
|
# typer implemented
|
|
|
|
from ..storage import cli # noqa
|
|
|
|
from ..accounting import cli # noqa
|
|
|
|
|
2020-07-15 12:28:13 +00:00
|
|
|
|
2020-05-23 19:39:17 +00:00
|
|
|
# load downstream cli modules
|
2020-07-15 12:28:13 +00:00
|
|
|
_load_clis()
|