2021-06-02 21:51:40 +02:00
# standard imports
import os
import logging
import argparse
import uuid
import sys
import stat
import socket
# external imports
import confini
from xdg . BaseDirectory import get_runtime_dir
logging . basicConfig ( level = logging . WARNING )
logg = logging . getLogger ( )
2021-11-26 12:55:20 +01:00
config_dir = os . environ . get ( ' CONFINI_DIR ' , ' ./config ' )
default_queue_runtime_path = os . path . join ( get_runtime_dir ( ) , ' chaind ' )
2021-06-02 21:51:40 +02:00
argparser = argparse . ArgumentParser ( ' chainqueue transaction submission and trigger server ' )
2021-11-26 12:55:20 +01:00
argparser . add_argument ( ' -c ' , ' --config ' , dest = ' c ' , type = str , help = ' configuration directory ' )
2021-06-02 21:51:40 +02:00
argparser . add_argument ( ' --session-id ' , dest = ' session_id ' , type = str , default = str ( uuid . uuid4 ( ) ) , help = ' session id to use for session ' )
argparser . add_argument ( ' -s ' , ' --socket-path ' , dest = ' s ' , type = str , help = ' socket path ' )
argparser . add_argument ( ' --env-prefix ' , default = os . environ . get ( ' CONFINI_ENV_PREFIX ' ) , dest = ' env_prefix ' , type = str , help = ' environment prefix for variables to overwrite configuration ' )
argparser . add_argument ( ' -v ' , action = ' store_true ' , help = ' be verbose ' )
argparser . add_argument ( ' -vv ' , action = ' store_true ' , help = ' be very verbose ' )
2021-11-26 12:55:20 +01:00
argparser . add_argument ( ' input_dir ' , type = str , nargs = ' ? ' , default = ' output ' , help = ' directory with transaction files ' )
2021-06-02 21:51:40 +02:00
args = argparser . parse_args ( sys . argv [ 1 : ] )
if args . vv :
logg . setLevel ( logging . DEBUG )
elif args . v :
logg . setLevel ( logging . INFO )
2021-11-26 12:55:20 +01:00
config = confini . Config ( config_dir , override_dirs = args . c )
2021-06-02 21:51:40 +02:00
socket_path = getattr ( args , ' s ' )
2021-11-26 10:56:36 +01:00
config . process ( )
2021-06-02 21:51:40 +02:00
args_override = {
' QUEUE_SOCKET_PATH ' : getattr ( args , ' s ' ) ,
}
config . dict_override ( args_override , ' cli args ' )
2021-11-26 12:55:20 +01:00
2021-06-02 21:51:40 +02:00
config . add ( getattr ( args , ' session_id ' ) , ' _SESSION_ID ' , True )
2021-11-26 12:55:20 +01:00
input_basedir = getattr ( args , ' input_dir ' )
input_dir = os . path . join ( input_basedir , config . get ( ' _SESSION_ID ' ) )
config . add ( input_dir , ' _INPUT_DIR ' , True )
2021-06-02 21:51:40 +02:00
if not config . get ( ' QUEUE_SOCKET_PATH ' ) :
2021-11-26 12:55:20 +01:00
config . add ( os . path . join ( default_queue_runtime_path , ' eth ' , config . get ( ' _SESSION_ID ' ) , ' chaind.sock ' ) , ' QUEUE_SOCKET_PATH ' , True )
2021-06-02 21:51:40 +02:00
logg . debug ( ' config loaded: \n {} ' . format ( config ) )
def main ( ) :
for p in os . listdir ( config . get ( ' _INPUT_DIR ' ) ) :
cli = socket . socket ( family = socket . AF_UNIX , type = socket . SOCK_STREAM )
cli . connect ( config . get ( ' QUEUE_SOCKET_PATH ' ) )
if p [ 0 ] == ' . ' :
continue
fpath = os . path . join ( config . get ( ' _INPUT_DIR ' ) , p )
f = open ( fpath , ' r ' )
tx = f . read ( )
f . close ( )
r = cli . send ( tx . encode ( ' utf-8 ' ) )
logg . debug ( ' sent {} bytes ' . format ( r ) )
r = cli . recv ( 4 )
cli . close ( )
if int . from_bytes ( r , byteorder = ' big ' ) :
sys . stderr . write ( ' error {} ' . format ( r ) )
sys . exit ( 1 )
2021-06-02 21:57:28 +02:00
os . unlink ( fpath )
2021-06-02 21:51:40 +02:00
logg . debug ( ' submitted {} {} status {} ' . format ( fpath , tx , r ) )
if __name__ == ' __main__ ' :
main ( )