fixed a lot of bugs and optimized code in cgminer handler
This commit is contained in:
@@ -10,28 +10,30 @@ class BOSminer(BaseMiner):
|
|||||||
api = BOSMinerAPI(ip)
|
api = BOSMinerAPI(ip)
|
||||||
super().__init__(ip, api)
|
super().__init__(ip, api)
|
||||||
self.config = None
|
self.config = None
|
||||||
|
self.uname = 'root'
|
||||||
|
self.pwd = 'admin'
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"BOSminer: {str(self.ip)}"
|
return f"BOSminer: {str(self.ip)}"
|
||||||
|
|
||||||
async def get_ssh_connection(self, username: str, password: str) -> asyncssh.connect:
|
async def _get_ssh_connection(self) -> asyncssh.connect:
|
||||||
"""Create a new asyncssh connection"""
|
"""Create a new asyncssh connection"""
|
||||||
conn = await asyncssh.connect(str(self.ip), known_hosts=None, username=username, password=password,
|
conn = await asyncssh.connect(str(self.ip), known_hosts=None, username=self.uname, password=self.pwd,
|
||||||
server_host_key_algs=['ssh-rsa'])
|
server_host_key_algs=['ssh-rsa'])
|
||||||
# return created connection
|
# return created connection
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
async def send_ssh_command(self, cmd: str) -> None:
|
async def send_ssh_command(self, cmd: str) -> None:
|
||||||
result = None
|
result = None
|
||||||
conn = await self.get_ssh_connection('root', 'admin')
|
async with self._get_ssh_connection() as conn:
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
try:
|
try:
|
||||||
result = await conn.run(cmd)
|
result = await conn.run(cmd)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"{cmd} error: {e}")
|
print(f"{cmd} error: {e}")
|
||||||
if i == 3:
|
if i == 3:
|
||||||
return
|
return
|
||||||
continue
|
continue
|
||||||
# let the user know the result of the command
|
# let the user know the result of the command
|
||||||
if result is not None:
|
if result is not None:
|
||||||
if result.stdout != "":
|
if result.stdout != "":
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ class CGMiner(BaseMiner):
|
|||||||
return f"CGMiner: {str(self.ip)}"
|
return f"CGMiner: {str(self.ip)}"
|
||||||
|
|
||||||
async def send_config(self):
|
async def send_config(self):
|
||||||
return None # ignore for now
|
return None # ignore for now
|
||||||
|
|
||||||
async def get_ssh_connection(self) -> asyncssh.connect:
|
async def _get_ssh_connection(self) -> asyncssh.connect:
|
||||||
conn = await asyncssh.connect(str(self.ip),
|
conn = await asyncssh.connect(str(self.ip),
|
||||||
known_hosts=None,
|
known_hosts=None,
|
||||||
username=self.uname,
|
username=self.uname,
|
||||||
@@ -25,7 +25,31 @@ class CGMiner(BaseMiner):
|
|||||||
server_host_key_algs=['ssh-rsa'])
|
server_host_key_algs=['ssh-rsa'])
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
async def result_handler(self, result: asyncssh.process.SSHCompletedProcess) -> None:
|
async def send_ssh_command(self, cmd):
|
||||||
|
result = None
|
||||||
|
async with self._get_ssh_connection() as conn:
|
||||||
|
for i in range(3):
|
||||||
|
try:
|
||||||
|
result = await conn.run(cmd)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"{cmd} error: {e}")
|
||||||
|
if i == 3:
|
||||||
|
return
|
||||||
|
continue
|
||||||
|
# handle result
|
||||||
|
self._result_handler(result)
|
||||||
|
if result is not None:
|
||||||
|
if result.stdout != "":
|
||||||
|
print(result.stdout)
|
||||||
|
if result.stderr != "":
|
||||||
|
print("ERROR: " + result.stderr)
|
||||||
|
elif result.stderr != "":
|
||||||
|
print("ERROR: " + result.stderr)
|
||||||
|
else:
|
||||||
|
print(cmd)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _result_handler(result: asyncssh.process.SSHCompletedProcess) -> None:
|
||||||
if result is not None:
|
if result is not None:
|
||||||
if len(result.stdout) > 0:
|
if len(result.stdout) > 0:
|
||||||
print("ssh stdout: \n" + result.stdout)
|
print("ssh stdout: \n" + result.stdout)
|
||||||
@@ -33,53 +57,38 @@ class CGMiner(BaseMiner):
|
|||||||
print("ssh stderr: \n" + result.stderrr)
|
print("ssh stderr: \n" + result.stderrr)
|
||||||
if len(result.stdout) <= 0 and len(result.stderr) <= 0:
|
if len(result.stdout) <= 0 and len(result.stderr) <= 0:
|
||||||
print("ssh stdout stderr empty")
|
print("ssh stdout stderr empty")
|
||||||
return None
|
|
||||||
|
|
||||||
async def restart_cgminer(self) -> None:
|
async def restart_cgminer(self) -> None:
|
||||||
async with get_ssh_connection() as conn:
|
commands = ['cgminer-api restart',
|
||||||
commands = ['cgminer-api restart',
|
'/usr/bin/cgminer-monitor >/dev/null 2>&1']
|
||||||
'/usr/bin/cgminer-monitor >/dev/null 2>&1']
|
commands = ';'.join(commands)
|
||||||
commands = ';'.join(commands)
|
await self.send_ssh_command(commands)
|
||||||
result = await conn.run(commands, check=True)
|
|
||||||
result_handler(result)
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def reboot(self) -> None:
|
async def reboot(self) -> None:
|
||||||
async with get_ssh_connection() conn:
|
commands = ['reboot']
|
||||||
commands = ['reboot']
|
commands = ';'.join(commands)
|
||||||
commands = ';'.join(commands)
|
await self.send_ssh_command(commands)
|
||||||
result = await conn.run(commands, check=True)
|
|
||||||
result_handler(result)
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def start_cgminer(self) -> None:
|
async def start_cgminer(self) -> None:
|
||||||
async with get_ssh_connection() conn:
|
commands = ['mkdir -p /etc/tmp/',
|
||||||
commands = ['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)
|
await self.send_ssh_command(commands)
|
||||||
result = await conn.run(commands, check=True)
|
|
||||||
result_handler(result)
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def stop_cgminer(self) -> None:
|
async def stop_cgminer(self) -> None:
|
||||||
async with get_ssh_connection() conn:
|
commands = ['mkdir -p /etc/tmp/',
|
||||||
commands = ['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)
|
await self.send_ssh_command(commands)
|
||||||
result = await conn.run(commands, check=True)
|
|
||||||
result_handler(result)
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def get_config(self) -> None:
|
async def get_config(self) -> None:
|
||||||
async with get_ssh_connection() as conn:
|
async with self._get_ssh_connection() as conn:
|
||||||
commands = ['cat /etc/config/cgminer']
|
command = 'cat /etc/config/cgminer'
|
||||||
commands = ';'.join(commands)
|
result = await conn.run(command, check=True)
|
||||||
result = await conn.run(commands, check=True)
|
self._result_handler(result)
|
||||||
result_handler(result)
|
|
||||||
self.config = result.stdout
|
self.config = result.stdout
|
||||||
print(str(self.config))
|
print(str(self.config))
|
||||||
return None
|
|
||||||
Reference in New Issue
Block a user