- Notifications
You must be signed in to change notification settings - Fork35
Make use of the default SSH user#129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
88 changes: 39 additions & 49 deletionstestgres/operations/remote_ops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import os | ||
import socket | ||
import subprocess | ||
import tempfile | ||
import platform | ||
@@ -45,46 +45,44 @@ def __init__(self, conn_params: ConnectionParams): | ||
self.conn_params = conn_params | ||
self.host = conn_params.host | ||
self.ssh_key = conn_params.ssh_key | ||
self.port = conn_params.port | ||
self.ssh_args = [] | ||
if self.ssh_key: | ||
self.ssh_args += ["-i", self.ssh_key] | ||
if self.port: | ||
self.ssh_args += ["-p", self.port] | ||
self.remote = True | ||
self.username = conn_params.username | ||
self.ssh_dest = f"{self.username}@{self.host}" if self.username else self.host | ||
self.add_known_host(self.host) | ||
self.tunnel_process = None | ||
self.tunnel_port = None | ||
def __enter__(self): | ||
return self | ||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.close_ssh_tunnel() | ||
@staticmethod | ||
def is_port_open(host, port): | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | ||
sock.settimeout(1) # Таймаут для попытки соединения | ||
Contributor
| ||
try: | ||
sock.connect((host, port)) | ||
return True | ||
except socket.error: | ||
return False | ||
def close_ssh_tunnel(self): | ||
if self.tunnel_process: | ||
self.tunnel_process.terminate() | ||
self.tunnel_process.wait() | ||
print("SSH tunnel closed.") | ||
del self.tunnel_process | ||
else: | ||
print("No active tunnel to close.") | ||
def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False, | ||
encoding=None, shell=True, text=False, input=None, stdin=None, stdout=None, | ||
stderr=None, get_process=None, timeout=None): | ||
@@ -95,9 +93,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False, | ||
""" | ||
ssh_cmd = [] | ||
if isinstance(cmd, str): | ||
ssh_cmd = ['ssh'] + self.ssh_args + [self.ssh_dest,cmd] | ||
elif isinstance(cmd, list): | ||
ssh_cmd = ['ssh'] +self.ssh_args +[self.ssh_dest] + cmd | ||
process = subprocess.Popen(ssh_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
if get_process: | ||
return process | ||
@@ -172,10 +170,6 @@ def set_env(self, var_name: str, var_val: str): | ||
""" | ||
return self.exec_command("export {}={}".format(var_name, var_val)) | ||
def get_name(self): | ||
cmd = 'python3 -c "import os; print(os.name)"' | ||
return self.exec_command(cmd, encoding=get_default_encoding()).strip() | ||
@@ -246,9 +240,9 @@ def mkdtemp(self, prefix=None): | ||
- prefix (str): The prefix of the temporary directory name. | ||
""" | ||
if prefix: | ||
command = ["ssh"] + self.ssh_args + [self.ssh_dest, f"mktemp -d {prefix}XXXXX"] | ||
else: | ||
command = ["ssh"] + self.ssh_args + [self.ssh_dest, "mktemp -d"] | ||
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
@@ -291,8 +285,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal | ||
mode = "r+b" if binary else "r+" | ||
with tempfile.NamedTemporaryFile(mode=mode, delete=False) as tmp_file: | ||
# Because in scp we set up port using -P option | ||
scp_args = ['-P' if x == '-p' else x for x in self.ssh_args] | ||
if not truncate: | ||
scp_cmd = ['scp'] +scp_args + [f"{self.ssh_dest}:{filename}", tmp_file.name] | ||
subprocess.run(scp_cmd, check=False) # The file might not exist yet | ||
tmp_file.seek(0, os.SEEK_END) | ||
@@ -308,11 +305,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal | ||
tmp_file.write(data) | ||
tmp_file.flush() | ||
scp_cmd = ['scp'] +scp_args + [tmp_file.name, f"{self.ssh_dest}:{filename}"] | ||
subprocess.run(scp_cmd, check=True) | ||
remote_directory = os.path.dirname(filename) | ||
mkdir_cmd = ['ssh'] + self.ssh_args + [self.ssh_dest, f"mkdir -p {remote_directory}"] | ||
subprocess.run(mkdir_cmd, check=True) | ||
os.remove(tmp_file.name) | ||
@@ -377,7 +374,7 @@ def get_pid(self): | ||
return int(self.exec_command("echo $$", encoding=get_default_encoding())) | ||
def get_process_children(self, pid): | ||
command = ["ssh"] + self.ssh_args + [self.ssh_dest, f"pgrep -P {pid}"] | ||
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
@@ -389,18 +386,11 @@ def get_process_children(self, pid): | ||
# Database control | ||
def db_connect(self, dbname, user, password=None, host="localhost", port=5432): | ||
conn = pglib.connect( | ||
host=host, | ||
port=port, | ||
database=dbname, | ||
user=user, | ||
password=password, | ||
) | ||
return conn |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.