From 1be12e5d4c3452ae5eefc134fd8cb88117d6225a Mon Sep 17 00:00:00 2001 From: UpstreamData Date: Wed, 16 Mar 2022 13:34:18 -0600 Subject: [PATCH] moved _get_ssh_connection to the base miner class --- miners/__init__.py | 35 +++++++++++++++++++++++++++++++---- miners/bmminer.py | 27 --------------------------- miners/bosminer.py | 17 ----------------- miners/cgminer.py | 30 ++---------------------------- 4 files changed, 33 insertions(+), 76 deletions(-) diff --git a/miners/__init__.py b/miners/__init__.py index 4937cff3..ab0bab2c 100644 --- a/miners/__init__.py +++ b/miners/__init__.py @@ -4,15 +4,46 @@ from API.cgminer import CGMinerAPI from API.btminer import BTMinerAPI from API.unknown import UnknownAPI import ipaddress +import asyncssh +import logging class BaseMiner: def __init__(self, ip: str, api: BMMinerAPI | BOSMinerAPI | CGMinerAPI | BTMinerAPI | UnknownAPI) -> None: self.ip = ipaddress.ip_address(ip) + self.uname = None + self.pwd = None self.api = api self.api_type = None self.model = None + async def _get_ssh_connection(self) -> asyncssh.connect: + """Create a new asyncssh connection""" + try: + conn = await asyncssh.connect(str(self.ip), + known_hosts=None, + username=self.uname, + password=self.pwd, + server_host_key_algs=['ssh-rsa']) + return conn + except asyncssh.misc.PermissionDenied: + try: + conn = await asyncssh.connect(str(self.ip), + known_hosts=None, + username="admin", + password="admin", + server_host_key_algs=['ssh-rsa']) + return conn + except Exception as e: + logging.warning(f"{self} raised an exception: {e}") + raise e + except OSError: + logging.warning(f"Connection refused: {self}") + return None + except Exception as e: + logging.warning(f"{self} raised an exception: {e}") + raise e + async def get_board_info(self): return None @@ -33,7 +64,3 @@ class BaseMiner: async def send_config(self, yaml_config): return None - - - - diff --git a/miners/bmminer.py b/miners/bmminer.py index d48fa4af..d512e2d7 100644 --- a/miners/bmminer.py +++ b/miners/bmminer.py @@ -1,6 +1,5 @@ from API.bmminer import BMMinerAPI from miners import BaseMiner -import asyncssh import logging @@ -43,32 +42,6 @@ class BMMiner(BaseMiner): logging.warning(f"Failed to get hostname for miner: {self}") return "?" - async def _get_ssh_connection(self) -> asyncssh.connect: - try: - conn = await asyncssh.connect(str(self.ip), - known_hosts=None, - username=self.uname, - password=self.pwd, - server_host_key_algs=['ssh-rsa']) - return conn - except asyncssh.misc.PermissionDenied: - try: - conn = await asyncssh.connect(str(self.ip), - known_hosts=None, - username="admin", - password="admin", - server_host_key_algs=['ssh-rsa']) - return conn - except Exception as e: - logging.warning(f"{self} raised an exception: {e}") - raise e - except OSError: - logging.warning(f"Connection refused: {self} ") - return None - except Exception as e: - logging.warning(f"{self} raised an exception: {e}") - raise e - async def send_ssh_command(self, cmd): result = None async with (await self._get_ssh_connection()) as conn: diff --git a/miners/bosminer.py b/miners/bosminer.py index eaf69c57..bdf841e8 100644 --- a/miners/bosminer.py +++ b/miners/bosminer.py @@ -1,6 +1,5 @@ from miners import BaseMiner from API.bosminer import BOSMinerAPI -import asyncssh import toml from config.bos import bos_config_convert, general_config_convert_bos import logging @@ -18,20 +17,6 @@ class BOSMiner(BaseMiner): def __repr__(self) -> str: return f"BOSminer: {str(self.ip)}" - async def _get_ssh_connection(self) -> asyncssh.connect: - """Create a new asyncssh connection""" - try: - conn = await asyncssh.connect(str(self.ip), known_hosts=None, username=self.uname, password=self.pwd, - server_host_key_algs=['ssh-rsa']) - except OSError: - logging.warning(f"Connection refused: {self} ") - return None - except Exception as e: - logging.warning(f"{self} raised an exception: {e}") - raise e - # return created connection - return conn - async def send_ssh_command(self, cmd: str): """Sends SSH command to miner.""" # creates result variable @@ -51,7 +36,6 @@ class BOSMiner(BaseMiner): continue return result - async def fault_light_on(self) -> None: """Sends command to turn on fault light on the miner.""" logging.debug(f"{self}: Sending fault_light on command.") @@ -170,7 +154,6 @@ class BOSMiner(BaseMiner): bad_boards[board].append(chain) return bad_boards - async def check_good_boards(self) -> str: """Checks for and provides list for working boards.""" devs = await self.api.devdetails() diff --git a/miners/cgminer.py b/miners/cgminer.py index 019680bf..0626febc 100644 --- a/miners/cgminer.py +++ b/miners/cgminer.py @@ -1,7 +1,6 @@ from miners import BaseMiner from API.cgminer import CGMinerAPI from API import APIError -import asyncssh class CGMiner(BaseMiner): @@ -24,7 +23,8 @@ class CGMiner(BaseMiner): except APIError: return None if version_data: - self.model = version_data["DEVDETAILS"][0]["Model"].replace("Antminer ", "") + self.model = version_data["DEVDETAILS"][0]["Model"].replace( + "Antminer ", "") return self.model return None @@ -39,32 +39,6 @@ class CGMiner(BaseMiner): except Exception: return "?" - async def _get_ssh_connection(self) -> asyncssh.connect: - try: - conn = await asyncssh.connect(str(self.ip), - known_hosts=None, - username=self.uname, - password=self.pwd, - server_host_key_algs=['ssh-rsa']) - return conn - except asyncssh.misc.PermissionDenied: - try: - conn = await asyncssh.connect(str(self.ip), - known_hosts=None, - username="admin", - password="admin", - server_host_key_algs=['ssh-rsa']) - return conn - except Exception as e: - print("Exception raised:", self.ip) - raise e - except OSError: - print(str(self.ip) + " Connection refused.") - return None - except Exception as e: - print("Exception raised:", self.ip) - raise e - async def send_ssh_command(self, cmd): result = None async with (await self._get_ssh_connection()) as conn: