Skip to content

Commit

Permalink
Connections lint error (#21)
Browse files Browse the repository at this point in the history
* Added exceptions to SshConnection

* Switched to passing config to devices/connections

* Updated package version
  • Loading branch information
chughes741 authored Aug 4, 2023
1 parent 1a2f3eb commit 2d796cb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
10 changes: 1 addition & 9 deletions openct/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@
logging.info("Backing up device %s", host)
pbar.update(100 / len(devices))

device = RouterOSDevice(
host,
config.identity.username,
config.settings.connection_timeout,
config.identity.key_file,
config.dirs.backup_dir,
)
device = RouterOSDevice(host, config)

if device.is_available():
device.fetch_backup()
else:
logging.error("Could not connect to device %s", host)
54 changes: 36 additions & 18 deletions openct/connections/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
import logging

from fabric import Connection as FabricConnection
from paramiko.ssh_exception import SSHException, NoValidConnectionsError
from invoke import UnexpectedExit
from paramiko.ssh_exception import (
SSHException,
NoValidConnectionsError,
BadHostKeyException,
AuthenticationException,
)
from invoke import UnexpectedExit, Failure

from openct.setup import Config


class DeviceConnection(Protocol):
Expand All @@ -21,19 +28,12 @@ def fetch_backup(self) -> None:
class SshConnection(DeviceConnection):
"""DeviceConnection using Fabric"""

def __init__(
self,
ip_address: str,
username: str,
connection_timeout: int,
key_file: str,
backup_dir: str,
) -> None:
def __init__(self, ip_address: str, config: Config) -> None:
self.ip_address = ip_address
self.username = username
self.connection_timeout = connection_timeout
self.key_file = key_file
self.backup_dir = backup_dir
self.username = config.identity.username
self.connection_timeout = config.settings.connection_timeout
self.key_file = config.identity.key_file
self.backup_dir = config.dirs.backup_dir

def test_connection(self) -> bool:
with FabricConnection(
Expand All @@ -45,8 +45,15 @@ def test_connection(self) -> bool:
try:
connection.open()
return True
except (TimeoutError, SSHException, NoValidConnectionsError):
except (
TimeoutError,
BadHostKeyException,
AuthenticationException,
SSHException,
NoValidConnectionsError,
) as error:
logging.error("Could not connect to device %s", self.ip_address)
logging.info(error)
return False

def fetch_backup(self) -> None:
Expand All @@ -57,12 +64,23 @@ def fetch_backup(self) -> None:
connect_kwargs={"key_filename": self.key_file},
) as connection:
try:
connection.run("/export file=backup", hide=True, warn=False)
connection.run(
"/export file=backup",
hide=True,
warn=False,
timeout=self.connection_timeout,
)
connection.get(
"backup.rsc", f"{self.backup_dir}/backup_{self.ip_address}.rsc"
)
connection.run("file/remove backup.rsc", hide=True, warn=False)
except UnexpectedExit:
connection.run(
"file/remove backup.rsc",
hide=True,
warn=False,
timeout=self.connection_timeout,
)
except (UnexpectedExit, Failure) as error:
logging.error(
"Error while fetching backup from device %s", self.ip_address
)
logging.info(error)
16 changes: 3 additions & 13 deletions openct/devices/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from abc import ABC, abstractmethod

from openct.connections import DeviceConnection, SshConnection
from openct.setup import Config


class Device(ABC):
Expand All @@ -22,19 +23,8 @@ def is_available(self) -> bool:
class RouterOSDevice(Device):
"""RouterOS device"""

def __init__(
self,
ip_address: str,
username: str,
connection_timeout: int,
key_file: str,
backup_dir: str,
) -> None:
super().__init__(
SshConnection(
ip_address, username, connection_timeout, key_file, backup_dir
)
)
def __init__(self, ip_address: str, config: Config) -> None:
super().__init__(SshConnection(ip_address, config))

def fetch_backup(self) -> str:
return self.connection.fetch_backup()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openct"
version = "0.1.3"
version = "0.1.4"
description = "Configuration backup and analysis tools for devices running pfSense and RouterOS"
authors = ["Weehooey <[email protected]>"]
license = "GNU GPL v3.0"
Expand Down

0 comments on commit 2d796cb

Please sign in to comment.