Factor out commons to chaind package

This commit is contained in:
nolash 2021-06-03 14:49:32 +02:00
parent dbf4108d62
commit ccabbc8ecc
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 13 additions and 26 deletions

View File

@ -1,5 +1,4 @@
# standard imports # standard imports
import syslog
import sys import sys
import time import time
import socket import socket
@ -11,12 +10,8 @@ import argparse
import uuid import uuid
# external imports # external imports
from chaind import Environment
import confini import confini
from xdg.BaseDirectory import (
xdg_data_dirs,
get_runtime_dir,
load_first_config,
)
from hexathon import strip_0x from hexathon import strip_0x
from chainlib.chain import ChainSpec from chainlib.chain import ChainSpec
from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.connection import EthHTTPConnection
@ -29,19 +24,15 @@ from chainqueue.adapters.eth import EthAdapter
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
default_config_dir = load_first_config('chainqueue') env = Environment(domain='eth', env=os.environ)
config_dir = os.environ.get('CONFINI_DIR', default_config_dir)
default_runtime_dir = os.path.join(get_runtime_dir(), 'chainqueue')
default_data_dir = os.path.join(xdg_data_dirs[0], 'chainqueue')
argparser = argparse.ArgumentParser('chainqueue transaction submission and trigger server') argparser = argparse.ArgumentParser('chainqueue transaction submission and trigger server')
argparser.add_argument('-c', '--config', dest='c', type=str, default=config_dir, help='configuration directory') argparser.add_argument('-c', '--config', dest='c', type=str, default=env.config_dir, help='configuration directory')
argparser.add_argument('-p', type=str, help='rpc endpoint') argparser.add_argument('-p', type=str, help='rpc endpoint')
argparser.add_argument('-i', type=str, help='chain spec') argparser.add_argument('-i', type=str, help='chain spec')
argparser.add_argument('--runtime-dir', dest='runtime_dir', type=str, default=default_runtime_dir, help='runtime directory') argparser.add_argument('--runtime-dir', dest='runtime_dir', type=str, default=env.runtime_dir, help='runtime directory')
argparser.add_argument('--data-dir', dest='data_dir', type=str, default=default_data_dir, help='data directory') argparser.add_argument('--data-dir', dest='data_dir', type=str, default=env.data_dir, help='data directory')
argparser.add_argument('--session-id', dest='session_id', type=str, default=str(uuid.uuid4()), help='session id to use for session') argparser.add_argument('--session-id', dest='session_id', type=str, default=env.session, help='session id to use for session')
argparser.add_argument('-v', action='store_true', help='be verbose') argparser.add_argument('-v', action='store_true', help='be verbose')
argparser.add_argument('-vv', action='store_true', help='be very verbose') argparser.add_argument('-vv', action='store_true', help='be very verbose')
args = argparser.parse_args(sys.argv[1:]) args = argparser.parse_args(sys.argv[1:])
@ -58,7 +49,7 @@ args_override = {
'SESSION_RUNTIME_DIR': getattr(args, 'runtime_dir'), 'SESSION_RUNTIME_DIR': getattr(args, 'runtime_dir'),
'SESSION_CHAIN_SPEC': getattr(args, 'i'), 'SESSION_CHAIN_SPEC': getattr(args, 'i'),
'RPC_ENDPOINT': getattr(args, 'p'), 'RPC_ENDPOINT': getattr(args, 'p'),
'PATH_DATA': getattr(args, 'data_dir'), 'SESSION_DATA_DIR': getattr(args, 'data_dir'),
} }
config.dict_override(args_override, 'cli args') config.dict_override(args_override, 'cli args')
config.add(getattr(args, 'session_id'), '_SESSION_ID', True) config.add(getattr(args, 'session_id'), '_SESSION_ID', True)
@ -68,9 +59,9 @@ if not config.get('SESSION_SOCKET_PATH'):
config.add(socket_path, 'SESSION_SOCKET_PATH', True) config.add(socket_path, 'SESSION_SOCKET_PATH', True)
if config.get('DATABASE_ENGINE') == 'sqlite': if config.get('DATABASE_ENGINE') == 'sqlite':
config.add(os.path.join(config.get('PATH_DATA'), config.get('DATABASE_NAME')), 'DATABASE_NAME', True) config.add(os.path.join(config.get('SESSION_DATA_DIR'), config.get('DATABASE_NAME')), 'DATABASE_NAME', True)
config.censor('PASSWORD', 'DATABASE')
logg.debug('config loaded:\n{}'.format(config)) logg.debug('config loaded:\n{}'.format(config))
@ -78,7 +69,7 @@ logg.debug('config loaded:\n{}'.format(config))
try: try:
os.stat(config.get('DATABASE_NAME')) os.stat(config.get('DATABASE_NAME'))
except FileNotFoundError: except FileNotFoundError:
sys.stderr.write('database file {} not found. please run database migration script first'.format(config.get('DATABASE_NAME'))) sys.stderr.write('database file {} not found. please run database migration script first\n'.format(config.get('DATABASE_NAME')))
sys.exit(1) sys.exit(1)
@ -102,9 +93,9 @@ class SessionController:
return return
self.dead = True self.dead = True
if signo != None: if signo != None:
syslog.syslog('closing on {}'.format(signo)) logg.info('closing on {}'.format(signo))
else: else:
syslog.syslog('explicit shutdown') logg.info('explicit shutdown')
sockname = self.srv.getsockname() sockname = self.srv.getsockname()
self.srv.close() self.srv.close()
try: try:

View File

@ -1,6 +1,2 @@
chainlib>0.0.2,<=0.0.3 chaind<=0.0.1,>=0.0.1a2
chainqueue>0.0.1,<=.0.0.2
chainsyncer>0.0.1,<=0.0.2
confini>0.3.5,<=0.3.6
pyxdg~=0.26
hexathon~=0.0.1a7 hexathon~=0.0.1a7