refactor: move base classes to base.py in their directories, move data locations to miners.data, and rename types to models.

This commit is contained in:
UpstreamData
2024-01-25 14:26:53 -07:00
parent aa1d7c1b6f
commit dd4c087749
261 changed files with 779 additions and 741 deletions

47
pyasic/ssh/base.py Normal file
View File

@@ -0,0 +1,47 @@
import asyncio
import logging
from typing import Optional
import asyncssh
class BaseSSH:
def __init__(self, ip: str) -> None:
self.ip = ip
self.pwd = None
self.username = "root"
self.port = 22
async def _get_connection(self) -> asyncssh.connect:
"""Create a new asyncssh connection"""
try:
conn = await asyncssh.connect(
str(self.ip),
port=self.port,
known_hosts=None,
username=self.username,
password=self.pwd,
server_host_key_algs=["ssh-rsa"],
)
return conn
except asyncssh.misc.PermissionDenied as e:
raise ConnectionRefusedError from e
except Exception as e:
raise ConnectionError from e
async def send_command(self, cmd: str) -> Optional[str]:
"""Send an ssh command to the miner"""
try:
conn = await asyncio.wait_for(self._get_connection(), timeout=10)
except (ConnectionError, asyncio.TimeoutError):
return None
try:
async with conn:
resp = await conn.run(cmd)
result = str(max(resp.stdout, resp.stderr, key=len))
return result
except Exception as e:
logging.error(f"{self} command {cmd} error: {e}")
return None