Skip to content

Commit

Permalink
Updated based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofvancoillie committed Jul 25, 2024
1 parent 18fb22e commit 35fe151
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ def __init__(self, address: str, username: str, certificate_path: str, port: int
self.username = username
self.certificate = certificate_path
self.port = port
self.ssh_client = None
self.ssh_client = self.connect()

def connect(self) -> Optional[paramiko.SSHClient]:
def connect(self) -> paramiko.SSHClient:
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Expand All @@ -29,29 +28,26 @@ def connect(self) -> Optional[paramiko.SSHClient]:
logging.info(f"Connected to {self.address} over SSH")

except Exception as e:
ssh_client = None
logging.error(f"Unable to connect to {self.address} over SSH: {e}")
return ssh_client
logging.error(f"Failed to connect to {self.address} over SSH")
raise e
else:
return ssh_client

def send_command(self, command: str) -> None:
if self.ssh_client:
stdin, stdout, stderr = self.ssh_client.exec_command(command)
while not stdout.channel.exit_status_ready():
# Print data when available
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
prevdata = b"1"
while prevdata:
prevdata = stdout.channel.recv(1024)
alldata += prevdata
logging.info(str(alldata))
else:
logging.error(f"Connection to Edge server {self.address} not opened.")
stdin, stdout, stderr = self.ssh_client.exec_command(command)
while not stdout.channel.exit_status_ready():
# Print data when available
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
prevdata = b"1"
while prevdata:
prevdata = stdout.channel.recv(1024)
alldata += prevdata
logging.info(str(alldata))

def upload_folder(self, source_folder: str, target_folder: str) -> None:
if self.ssh_client:
scp = SCPClient(self.ssh_client.get_transport())
scp.put(files=source_folder, remote_path=target_folder, recursive=True)
scp = SCPClient(self.ssh_client.get_transport())
scp.put(files=source_folder, remote_path=target_folder, recursive=True)

def upload_edge_shared_folder(self, edge_directory: str, shared_connection_folder: str) -> None:
self.send_command(
Expand Down

0 comments on commit 35fe151

Please sign in to comment.