bug: fix ssh commands not working properly because of error handling inside inner functions.

This commit is contained in:
UpstreamData
2023-05-12 08:23:29 -06:00
parent 8e430b149b
commit 827834a119
5 changed files with 60 additions and 86 deletions

View File

@@ -71,7 +71,7 @@ class BMMiner(BaseMiner):
try: try:
conn = await self._get_ssh_connection() conn = await self._get_ssh_connection()
except (asyncssh.Error, OSError): except ConnectionError:
return None return None
# open an ssh connection # open an ssh connection
@@ -106,11 +106,11 @@ class BMMiner(BaseMiner):
async def reboot(self) -> bool: async def reboot(self) -> bool:
logging.debug(f"{self}: Sending reboot command.") logging.debug(f"{self}: Sending reboot command.")
_ret = await self.send_ssh_command("reboot") ret = await self.send_ssh_command("reboot")
logging.debug(f"{self}: Reboot command completed.") logging.debug(f"{self}: Reboot command completed.")
if isinstance(_ret, str): if ret is None:
return True return False
return False return True
async def send_config(self, config: MinerConfig, user_suffix: str = None) -> None: async def send_config(self, config: MinerConfig, user_suffix: str = None) -> None:
return None return None
@@ -193,8 +193,7 @@ class BMMiner(BaseMiner):
async def get_hostname(self) -> Optional[str]: async def get_hostname(self) -> Optional[str]:
hn = await self.send_ssh_command("cat /proc/sys/kernel/hostname") hn = await self.send_ssh_command("cat /proc/sys/kernel/hostname")
if hn: return hn
return hn
async def get_hashrate(self, api_summary: dict = None) -> Optional[float]: async def get_hashrate(self, api_summary: dict = None) -> Optional[float]:
# get hr from API # get hr from API

View File

@@ -194,7 +194,7 @@ class BOSMiner(BaseMiner):
try: try:
conn = await self._get_ssh_connection() conn = await self._get_ssh_connection()
except (asyncssh.Error, OSError): except ConnectionError:
return None return None
# open an ssh connection # open an ssh connection
@@ -224,9 +224,9 @@ class BOSMiner(BaseMiner):
async def fault_light_on(self) -> bool: async def fault_light_on(self) -> bool:
"""Sends command to turn on fault light on the miner.""" """Sends command to turn on fault light on the miner."""
logging.debug(f"{self}: Sending fault_light on command.") logging.debug(f"{self}: Sending fault_light on command.")
_ret = await self.send_ssh_command("miner fault_light on") ret = await self.send_ssh_command("miner fault_light on")
logging.debug(f"{self}: fault_light on command completed.") logging.debug(f"{self}: fault_light on command completed.")
if isinstance(_ret, str): if isinstance(ret, str):
self.light = True self.light = True
return self.light return self.light
return False return False
@@ -235,9 +235,9 @@ class BOSMiner(BaseMiner):
"""Sends command to turn off fault light on the miner.""" """Sends command to turn off fault light on the miner."""
logging.debug(f"{self}: Sending fault_light off command.") logging.debug(f"{self}: Sending fault_light off command.")
self.light = False self.light = False
_ret = await self.send_ssh_command("miner fault_light off") ret = await self.send_ssh_command("miner fault_light off")
logging.debug(f"{self}: fault_light off command completed.") logging.debug(f"{self}: fault_light off command completed.")
if isinstance(_ret, str): if isinstance(ret, str):
self.light = False self.light = False
return True return True
return False return False
@@ -249,9 +249,9 @@ class BOSMiner(BaseMiner):
async def restart_bosminer(self) -> bool: async def restart_bosminer(self) -> bool:
"""Restart bosminer hashing process.""" """Restart bosminer hashing process."""
logging.debug(f"{self}: Sending bosminer restart command.") logging.debug(f"{self}: Sending bosminer restart command.")
_ret = await self.send_ssh_command("/etc/init.d/bosminer restart") ret = await self.send_ssh_command("/etc/init.d/bosminer restart")
logging.debug(f"{self}: bosminer restart command completed.") logging.debug(f"{self}: bosminer restart command completed.")
if isinstance(_ret, str): if isinstance(ret, str):
return True return True
return False return False
@@ -278,9 +278,9 @@ class BOSMiner(BaseMiner):
async def reboot(self) -> bool: async def reboot(self) -> bool:
"""Reboots power to the physical miner.""" """Reboots power to the physical miner."""
logging.debug(f"{self}: Sending reboot command.") logging.debug(f"{self}: Sending reboot command.")
_ret = await self.send_ssh_command("/sbin/reboot") ret = await self.send_ssh_command("/sbin/reboot")
logging.debug(f"{self}: Reboot command completed.") logging.debug(f"{self}: Reboot command completed.")
if isinstance(_ret, str): if isinstance(ret, str):
return True return True
return False return False
@@ -294,7 +294,7 @@ class BOSMiner(BaseMiner):
conn = None conn = None
try: try:
conn = await self._get_ssh_connection() conn = await self._get_ssh_connection()
except (asyncssh.Error, OSError): except ConnectionError:
try: try:
pools = await self.api.pools() pools = await self.api.pools()
except APIError: except APIError:
@@ -322,7 +322,7 @@ class BOSMiner(BaseMiner):
) )
try: try:
conn = await self._get_ssh_connection() conn = await self._get_ssh_connection()
except (asyncssh.Error, OSError): except ConnectionError:
return None return None
async with conn: async with conn:
# BBB check because bitmain suxx # BBB check because bitmain suxx
@@ -366,12 +366,9 @@ class BOSMiner(BaseMiner):
################################################## ##################################################
async def get_mac(self) -> Optional[str]: async def get_mac(self) -> Optional[str]:
try: result = await self.send_ssh_command("cat /sys/class/net/eth0/address")
result = await self.send_ssh_command("cat /sys/class/net/eth0/address") if result:
if result: return result.upper().strip()
return result.upper().strip()
except (asyncssh.Error, OSError):
pass
async def get_model(self) -> Optional[str]: async def get_model(self) -> Optional[str]:
return self.model + " (BOS)" return self.model + " (BOS)"
@@ -425,7 +422,7 @@ class BOSMiner(BaseMiner):
fw_ver = await self.send_ssh_command("cat /etc/bos_version") fw_ver = await self.send_ssh_command("cat /etc/bos_version")
# if we get the version data, parse it # if we get the version data, parse it
if fw_ver: if fw_ver is not None:
ver = fw_ver.split("-")[5] ver = fw_ver.split("-")[5]
if "." in ver: if "." in ver:
self.fw_ver = ver self.fw_ver = ver
@@ -954,15 +951,12 @@ class BOSMiner(BaseMiner):
pass pass
# get light via ssh if that fails (10x slower) # get light via ssh if that fails (10x slower)
try: data = (
data = ( await self.send_ssh_command("cat /sys/class/leds/'Red LED'/delay_off")
await self.send_ssh_command("cat /sys/class/leds/'Red LED'/delay_off") ).strip()
).strip() self.light = False
self.light = False if data == "50":
if data == "50": self.light = True
self.light = True
except Exception as e:
logging.debug(f"SSH command failed - Fault Light - {e}")
return self.light return self.light
async def get_nominal_hashrate(self, api_devs: dict = None) -> Optional[float]: async def get_nominal_hashrate(self, api_devs: dict = None) -> Optional[float]:

View File

@@ -34,7 +34,7 @@ class BOSMinerOld(BOSMiner):
try: try:
conn = await self._get_ssh_connection() conn = await self._get_ssh_connection()
except (asyncssh.Error, OSError): except ConnectionError:
return None return None
# open an ssh connection # open an ssh connection

View File

@@ -69,7 +69,7 @@ class CGMiner(BaseMiner):
try: try:
conn = await self._get_ssh_connection() conn = await self._get_ssh_connection()
except (asyncssh.Error, OSError): except ConnectionError:
return None return None
# open an ssh connection # open an ssh connection
@@ -100,57 +100,44 @@ class CGMiner(BaseMiner):
"""Restart cgminer hashing process.""" """Restart cgminer hashing process."""
commands = ["cgminer-api restart", "/usr/bin/cgminer-monitor >/dev/null 2>&1"] commands = ["cgminer-api restart", "/usr/bin/cgminer-monitor >/dev/null 2>&1"]
commands = ";".join(commands) commands = ";".join(commands)
try: ret = await self.send_ssh_command(commands)
_ret = await self.send_ssh_command(commands) if ret is None:
except (asyncssh.Error, OSError):
return False return False
else: return True
if isinstance(_ret, str):
return True
return False
async def reboot(self) -> bool: async def reboot(self) -> bool:
"""Reboots power to the physical miner.""" """Reboots power to the physical miner."""
logging.debug(f"{self}: Sending reboot command.") logging.debug(f"{self}: Sending reboot command.")
try: ret = await self.send_ssh_command("reboot")
_ret = await self.send_ssh_command("reboot") if ret is None:
except (asyncssh.Error, OSError):
return False return False
else: return True
logging.debug(f"{self}: Reboot command completed.")
if isinstance(_ret, str):
return True
return False
async def resume_mining(self) -> bool: async def resume_mining(self) -> bool:
try: commands = [
commands = [ "mkdir -p /etc/tmp/",
"mkdir -p /etc/tmp/", 'echo "*/3 * * * * /usr/bin/cgminer-monitor" > /etc/tmp/root',
'echo "*/3 * * * * /usr/bin/cgminer-monitor" > /etc/tmp/root', "crontab -u root /etc/tmp/root",
"crontab -u root /etc/tmp/root", "/usr/bin/cgminer-monitor >/dev/null 2>&1",
"/usr/bin/cgminer-monitor >/dev/null 2>&1", ]
] commands = ";".join(commands)
commands = ";".join(commands) ret = await self.send_ssh_command(commands)
await self.send_ssh_command(commands) if ret is None:
except (asyncssh.Error, OSError):
return False return False
else: return True
return True
async def stop_mining(self) -> bool: async def stop_mining(self) -> bool:
try: commands = [
commands = [ "mkdir -p /etc/tmp/",
"mkdir -p /etc/tmp/", 'echo "" > /etc/tmp/root',
'echo "" > /etc/tmp/root', "crontab -u root /etc/tmp/root",
"crontab -u root /etc/tmp/root", "killall cgminer",
"killall cgminer", ]
] commands = ";".join(commands)
commands = ";".join(commands) ret = await self.send_ssh_command(commands)
await self.send_ssh_command(commands) if ret is None:
except (asyncssh.Error, OSError):
return False return False
else: return True
return True
async def get_config(self) -> MinerConfig: async def get_config(self) -> MinerConfig:
api_pools = await self.api.pools() api_pools = await self.api.pools()
@@ -224,12 +211,8 @@ class CGMiner(BaseMiner):
return self.fw_ver return self.fw_ver
async def get_hostname(self) -> Optional[str]: async def get_hostname(self) -> Optional[str]:
try: hn = await self.send_ssh_command("cat /proc/sys/kernel/hostname")
hn = await self.send_ssh_command("cat /proc/sys/kernel/hostname") return hn
except (asyncssh.Error, OSError):
return None
if hn:
return hn
async def get_hashrate(self, api_summary: dict = None) -> Optional[float]: async def get_hashrate(self, api_summary: dict = None) -> Optional[float]:
# get hr from API # get hr from API

View File

@@ -14,7 +14,6 @@
# limitations under the License. - # limitations under the License. -
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
import inspect
import ipaddress import ipaddress
import logging import logging
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
@@ -25,7 +24,6 @@ import asyncssh
from pyasic.config import MinerConfig from pyasic.config import MinerConfig
from pyasic.data import Fan, HashBoard, MinerData from pyasic.data import Fan, HashBoard, MinerData
from pyasic.data.error_codes import MinerErrorData from pyasic.data.error_codes import MinerErrorData
from pyasic.errors import APIError
class BaseMiner(ABC): class BaseMiner(ABC):
@@ -95,12 +93,12 @@ class BaseMiner(ABC):
) )
return conn return conn
except Exception as e: except Exception as e:
raise e raise ConnectionError from e
except OSError as e: except OSError as e:
logging.warning(f"Connection refused: {self}") logging.warning(f"Connection refused: {self}")
raise e raise ConnectionError from e
except Exception as e: except Exception as e:
raise e raise ConnectionError from e
async def check_light(self) -> bool: async def check_light(self) -> bool:
return await self.get_fault_light() return await self.get_fault_light()