Created singleton for opening and closing SSH tunnel.
This commit is contained in:
parent
2f42d0e527
commit
7af07b3e11
1 changed files with 33 additions and 0 deletions
|
|
@ -4,6 +4,39 @@ import mysql.connector
|
||||||
import trino.dbapi
|
import trino.dbapi
|
||||||
from trino.auth import BasicAuthentication
|
from trino.auth import BasicAuthentication
|
||||||
from trino.dbapi import connect
|
from trino.dbapi import connect
|
||||||
|
from sshtunnel import SSHTunnelForwarder
|
||||||
|
|
||||||
|
|
||||||
|
def singleton(class_):
|
||||||
|
instances = {}
|
||||||
|
|
||||||
|
def getinstance(*args, **kwargs):
|
||||||
|
if class_ not in instances:
|
||||||
|
instances[class_] = class_(*args, **kwargs)
|
||||||
|
return instances[class_]
|
||||||
|
|
||||||
|
return getinstance
|
||||||
|
|
||||||
|
|
||||||
|
@singleton
|
||||||
|
class MySSHTunnel:
|
||||||
|
def __init__(
|
||||||
|
self, ssh_host, ssh_port, ssh_username, ssh_pkey, remote_host, remote_port
|
||||||
|
):
|
||||||
|
|
||||||
|
self.tunnel = SSHTunnelForwarder(
|
||||||
|
ssh_host=(ssh_host, ssh_port),
|
||||||
|
ssh_username=ssh_username,
|
||||||
|
ssh_pkey=ssh_pkey,
|
||||||
|
remote_bind_address=(remote_host, remote_port),
|
||||||
|
local_bind_address=("127.0.0.1", remote_port),
|
||||||
|
)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.tunnel.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.tunnel.stop()
|
||||||
|
|
||||||
|
|
||||||
def get_connection(connection_config: dict) -> Union[trino.dbapi.Connection]:
|
def get_connection(connection_config: dict) -> Union[trino.dbapi.Connection]:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue