diff --git a/proxy/environment.py b/proxy/environment.py index 0c639a515..ec49247b9 100644 --- a/proxy/environment.py +++ b/proxy/environment.py @@ -1,3 +1,4 @@ +import sys from decimal import Decimal import json import os @@ -5,7 +6,7 @@ from logged_groups import logged_group, LogMng from solana.publickey import PublicKey from solana.account import Account as SolanaAccount -from typing import Optional +from typing import Optional, List SOLANA_URL = os.environ.get("SOLANA_URL", "http://localhost:8899") PP_SOLANA_URL = os.environ.get("PP_SOLANA_URL", SOLANA_URL) @@ -45,15 +46,26 @@ GET_WHITE_LIST_BALANCE_MAX_RETRIES = int(os.environ.get("GET_WHITE_LIST_BALANCE_MAX_RETRIES", 3)) GET_WHITE_LIST_BALANCE_RETRY_INTERVAL_S = int(os.environ.get("GET_WHITE_LIST_BALANCE_RETRY_INTERVAL_S", 1)) + +class CliBase: + + def run_cli(self, cmd: List[str], **kwargs) -> bytes: + self.debug("Calling: " + " ".join(cmd)) + proc_result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs) + if proc_result.stderr is not None: + print(proc_result.stderr, file=sys.stderr) + return proc_result.stdout + + @logged_group("neon.Proxy") -class solana_cli: +class solana_cli(CliBase): def call(self, *args): try: cmd = ["solana", "--url", SOLANA_URL, ] + list(args) self.debug("Calling: " + " ".join(cmd)) - return subprocess.check_output(cmd, universal_newlines=True) + return self.run_cli(cmd, universal_newlines=True) except subprocess.CalledProcessError as err: self.error("ERR: solana error {}".format(err)) raise @@ -101,7 +113,8 @@ def read_sol_account(name) -> Optional[SolanaAccount]: @logged_group("neon.Proxy") -class neon_cli: +class neon_cli(CliBase): + def call(self, *args): try: ctx = json.dumps(LogMng.get_logging_context()) @@ -113,18 +126,15 @@ def call(self, *args): ]\ + (["-vvv"] if LOG_NEON_CLI_DEBUG else [])\ + list(args) - self.debug("Calling: " + " ".join(cmd)) - return subprocess.check_output(cmd, timeout=neon_cli_timeout, universal_newlines=True) + return self.run_cli(cmd, timeout=neon_cli_timeout, universal_newlines=True) except subprocess.CalledProcessError as err: self.error("ERR: neon-cli error {}".format(err)) raise def version(self): try: - cmd = ["neon-cli", - "--version"] - self.debug("Calling: " + " ".join(cmd)) - return subprocess.check_output(cmd, timeout=neon_cli_timeout, universal_newlines=True).split()[1] + cmd = ["neon-cli", "--version"] + return self.run_cli(cmd, timeout=neon_cli_timeout, universal_newlines=True).split()[1] except subprocess.CalledProcessError as err: self.error("ERR: neon-cli error {}".format(err)) raise