Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2621724

Browse files
committed
Do not use a tunnel
1 parent 0611d10 commit 2621724

File tree

1 file changed

+8
-44
lines changed

1 file changed

+8
-44
lines changed

testgres/operations/remote_ops.py

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import platform
66
import time
77

8-
from ..utils import reserve_port
9-
108
# we support both pg8000 and psycopg2
119
try:
1210
import psycopg2 as pglib
@@ -17,7 +15,6 @@
1715
raise ImportError("You must have psycopg2 or pg8000 modules installed")
1816

1917
from ..exceptions import ExecUtilException
20-
from ..utils import reserve_port
2118
from .os_ops import OsOperations, ConnectionParams, get_default_encoding
2219

2320
error_markers = [b'error', b'Permission denied', b'fatal', b'No such file or directory']
@@ -76,24 +73,6 @@ def is_port_open(host, port):
7673
except socket.error:
7774
return False
7875

79-
def establish_ssh_tunnel(self, local_port, remote_port, host):
80-
"""
81-
Establish an SSH tunnel from a local port to a remote PostgreSQL port.
82-
"""
83-
if host != 'localhost':
84-
ssh_cmd = ['-N', '-L', f"localhost:{local_port}:{host}:{remote_port}"]
85-
else:
86-
ssh_cmd = ['-N', '-L', f"{local_port}:{host}:{remote_port}"]
87-
self.tunnel_process = self.exec_command(ssh_cmd, get_process=True, timeout=300)
88-
timeout = 10
89-
start_time = time.time()
90-
while time.time() - start_time < timeout:
91-
if self.is_port_open('localhost', local_port):
92-
print("SSH tunnel established.")
93-
return
94-
time.sleep(0.5)
95-
raise Exception("Failed to establish SSH tunnel within the timeout period.")
96-
9776
def close_ssh_tunnel(self):
9877
if self.tunnel_process:
9978
self.tunnel_process.terminate()
@@ -410,26 +389,11 @@ def get_process_children(self, pid):
410389

411390
# Database control
412391
def db_connect(self, dbname, user, password=None, host="localhost", port=5432):
413-
"""
414-
Establish SSH tunnel and connect to a PostgreSQL database.
415-
"""
416-
local_port = reserve_port()
417-
self.tunnel_port = local_port
418-
self.establish_ssh_tunnel(local_port=local_port, remote_port=port, host=host)
419-
try:
420-
conn = pglib.connect(
421-
host='localhost',
422-
port=local_port,
423-
database=dbname,
424-
user=user,
425-
password=password,
426-
timeout=10
427-
)
428-
print("Database connection established successfully.")
429-
return conn
430-
except Exception as e:
431-
print(f"Error connecting to the database: {str(e)}")
432-
if self.tunnel_process:
433-
self.tunnel_process.terminate()
434-
print("SSH tunnel closed due to connection failure.")
435-
raise
392+
conn = pglib.connect(
393+
host=host,
394+
port=port,
395+
database=dbname,
396+
user=user,
397+
password=password,
398+
)
399+
return conn

0 commit comments

Comments
 (0)