Ejemplo n.º 1
0
    def __init__(self) -> None:
        self.config = Config()
        self.file_name = "component_state.json"
        assert self.config.SDK_HOME_DIR is not None
        self.lock_path = self.config.SDK_HOME_DIR / "component_state.json.lock"
        self.component_state_path = self.config.SDK_HOME_DIR / self.file_name

        self.file_lock = FileLock(str(self.lock_path), timeout=5)  # pylint: disable=abstract-class-instantiated
        self.previous_state = self.read_state(
        )  # compare at regularly to detect change
        self.websockets: Set[WebSocketResponse] = set()
        self.websockets_lock: threading.Lock = threading.Lock()

        self.update_thread = threading.Thread(target=self.update_status_thread,
                                              name='status_thread',
                                              daemon=True)
        self.update_thread.start()

        self.push_notification_queue: queue.Queue[
            ComponentTypedDict] = queue.Queue()
        self.update_thread = threading.Thread(
            target=self.push_notifications_thread,
            name='status_thread',
            daemon=True)
        self.update_thread.start()
        self.pong_event = asyncio.Event()
Ejemplo n.º 2
0
def stop_postgres():
    config = Config()
    if SDK_PORTABLE_MODE == 1:
        from .. import _postgres
        postgres_install_path = config.DATADIR / "postgres"
        if not asyncio.run(_postgres.check_running()):
            logger.info(f"stopping postgres port: {SDK_POSTGRES_PORT} at: {postgres_install_path}")
            _postgres.stop()
Ejemplo n.º 3
0
def reset_postgres():
    config = Config()
    if SDK_PORTABLE_MODE == 1:
        from .. import _postgres
        postgres_install_path = config.DATADIR / "postgres"
        if not asyncio.run(_postgres.check_running()):
            logger.info(f"resetting postgres at: {postgres_install_path}")
            _postgres.reset()
Ejemplo n.º 4
0
    def __init__(self, cli_inputs: CLIInputs) -> None:
        self.cli_inputs = cli_inputs
        self.config = Config()
        self.plugin_tools = PluginTools(self, self.cli_inputs)
        self.tools = LocalTools(self)
        self.logger = logging.getLogger(self.COMPONENT_NAME)

        self.src = self.plugin_tools.get_source_dir(dirname="simple-indexer")
        self.datadir = None  # dynamically allocated
        self.id = None  # dynamically allocated
        self.port = None  # dynamically allocated
        self.component_info: Optional[Component] = None
Ejemplo n.º 5
0
    def __init__(self, cli_inputs: CLIInputs):
        self.cli_inputs = cli_inputs
        self.config = Config()
        self.plugin_tools = PluginTools(self, self.cli_inputs)
        self.logger = logging.getLogger(self.COMPONENT_NAME)

        self.src = self.plugin_tools.get_source_dir(dirname="merchant_api")
        self.datadir: Optional[Path] = None  # dynamically allocated
        self.id: Optional[str] = None  # dynamically allocated
        self.port: Optional[int] = None  # dynamically allocated
        self.component_info: Optional[Component] = None
        download_and_init_postgres()  # only if necessary
Ejemplo n.º 6
0
    def __init__(self, cli_inputs: CLIInputs):
        self.cli_inputs = cli_inputs
        self.config = Config()
        self.plugin_tools = PluginTools(self, self.cli_inputs)
        self.tools = LocalTools(self)
        self.logger = logging.getLogger(self.COMPONENT_NAME)

        self.src = self.plugin_tools.get_source_dir("woc-explorer")
        self.datadir = None  # N/A
        self.id = self.plugin_tools.get_id(self.COMPONENT_NAME)
        self.port = None  # N/A
        self.component_info: Optional[Component] = None
Ejemplo n.º 7
0
def main() -> None:
    config = Config()

    top_level_parser = argparse.ArgumentParser()
    top_level_parser.add_argument(
        "--command",
        type=str,
        default="",
        help="one contiguous string command (to run a server)")
    # On windows it is not possible to transfer more than 8192 characters via the command line.
    # For MerchantAPI there are too many environment variables and it exceeds the limit therefore
    # the env vars are written to and read from a temp file (that is encrypted with an ephemeral
    # secret key in case the file is not cleaned up and removed as expected)
    top_level_parser.add_argument(
        "--env_vars_encryption_key",
        type=str,
        default="",
        help=
        "ephemeral encryption key to secure environment variables in a temp file"
    )
    top_level_parser.add_argument("--component_info",
                                  type=str,
                                  default="",
                                  help="component_info")
    parsed_args = top_level_parser.parse_args()
    command = unwrap_and_unescape_text(parsed_args.command)
    component_info = json.loads(
        base64.b64decode(parsed_args.component_info).decode())
    component_info = Component.from_dict(component_info)

    component_name = component_info.component_type
    if parsed_args.env_vars_encryption_key:
        infile = config.DATADIR / component_name / "encrypted.env"
        with open(infile, 'r') as f:
            encrypted_message = f.read()
            key = bitcoinx.PrivateKey.from_hex(
                parsed_args.env_vars_encryption_key)
            decrypted_message = key.decrypt_message(encrypted_message)
        env_vars = json.loads(decrypted_message)
        if Path.exists(infile):
            os.remove(infile)
    else:
        env_vars = None

    spawn_inline(command,
                 env_vars,
                 id=component_info.id,
                 component_name=component_info.component_type,
                 src=component_info.location,
                 logfile=component_info.logging_path,
                 status_endpoint=component_info.status_endpoint,
                 metadata=component_info.metadata)
Ejemplo n.º 8
0
    def __init__(self, cli_inputs: CLIInputs):
        self.cli_inputs = cli_inputs
        self.config = Config()
        self.plugin_tools = PluginTools(self, self.cli_inputs)
        self.tools = LocalTools(self)
        self.logger = logging.getLogger(self.COMPONENT_NAME)

        self.src = self.plugin_tools.get_source_dir(dirname="electrumsv")
        self.datadir = None  # dynamically allocated
        self.id = None  # dynamically allocated
        self.port = None  # dynamically allocated
        self.component_info: Optional[Component] = None

        self.network = self.BITCOIN_NETWORK
Ejemplo n.º 9
0
    def __init__(self, cli_inputs: CLIInputs):
        self.cli_inputs = cli_inputs
        self.config = Config()
        self.plugin_tools = PluginTools(self, self.cli_inputs)
        self.tools = LocalTools(self)
        self.logger = logging.getLogger(self.COMPONENT_NAME)

        self.src = Path(electrumsv_node.FILE_PATH).parent
        self.datadir: Optional[Path] = None  # dynamically allocated
        self.id: Optional[str] = None  # dynamically allocated
        self.port: Optional[int] = None  # dynamically allocated
        self.p2p_port: Optional[int] = None  # dynamically allocated
        self.zmq_port: Optional[int] = None  # dynamically allocated
        self.component_info: Optional[Component] = None

        self.network = self.BITCOIN_NETWORK
Ejemplo n.º 10
0
def download_and_init_postgres():
    config = Config()
    if SDK_PORTABLE_MODE == 1:
        postgres_install_path = config.DATADIR / "postgres"
        os.makedirs(postgres_install_path, exist_ok=True)

        # Set this environment variable before importing postgres script
        os.environ['SDK_POSTGRES_INSTALL_DIR'] = str(postgres_install_path)
        from .. import _postgres
        if not _postgres.check_extract_done():
            logger.info(
                f"downloading and extracting embedded postgres to {postgres_install_path}")
            _postgres.download_and_extract()

        # We do not initialise the db in the azure pipeline because there are issues with file
        # permissions
        if not _postgres.check_initdb_done() and SDK_SKIP_POSTGRES_INIT != 1:
            logger.info(f"running initdb for postgres port: {SDK_POSTGRES_PORT} "
                f"at: {postgres_install_path}")
            _postgres.initdb()
Ejemplo n.º 11
0
        os.environ['ConnectionStrings__DBConnectionStringMaster'] = \
            DBConnectionStringMaster.replace("Port=5432", f"Port={SDK_POSTGRES_PORT}")


def load_env_vars() -> None:
    env_vars = {
        "PYTHONUNBUFFERED": "1"
    }
    os.environ.update(env_vars)
    from dotenv import load_dotenv
    env_path = pathlib.Path(MODULE_DIR) / 'exe-config' / '.env'
    load_dotenv(dotenv_path=env_path)
    maybe_change_postgres_port()


def prepare_fresh_postgres():
    check_postgres_db()


if __name__ == "__main__":
    # This will download and extract the correct build to the local `zzz` directory for testing.
    os.environ['SDK_PORTABLE_MODE'] = "1"
    SDK_PORTABLE_MODE = int(os.environ['SDK_PORTABLE_MODE'])
    download_path = pathlib.Path("zzz")
    config = Config()
    download_and_install(download_path)

    run_path = get_run_path(download_path)
    assert run_path.exists(), "executable not found"
    assert run_path.is_file(), "executable is not a file"