added send command option in the window
This commit is contained in:
@@ -17,6 +17,7 @@ def disable_buttons(func):
|
||||
"send_config",
|
||||
"light",
|
||||
"generate_config",
|
||||
"send_miner_ssh_command_window",
|
||||
]
|
||||
|
||||
# handle the inner function that the decorator is wrapping
|
||||
|
||||
@@ -78,6 +78,52 @@ async def flip_light(ip):
|
||||
await miner.fault_light_off()
|
||||
|
||||
|
||||
async def send_command_generator(miners: list, command: str):
|
||||
loop = asyncio.get_event_loop()
|
||||
command_tasks = []
|
||||
for miner in miners:
|
||||
if len(command_tasks) >= CONFIG_THREADS:
|
||||
cmd_sent = asyncio.as_completed(command_tasks)
|
||||
command_tasks = []
|
||||
for done in cmd_sent:
|
||||
yield await done
|
||||
command_tasks.append(loop.create_task(send_ssh_command(miner, command)))
|
||||
cmd_sent = asyncio.as_completed(command_tasks)
|
||||
for done in cmd_sent:
|
||||
yield await done
|
||||
|
||||
|
||||
async def send_ssh_command(miner, command: str):
|
||||
proc = await miner.send_ssh_command(command)
|
||||
return {"IP": miner.ip, "proc": proc}
|
||||
|
||||
|
||||
async def send_miners_ssh_commands(ips: list, command: str, ssh_cmd_window):
|
||||
get_miner_genenerator = miner_factory.get_miner_generator(ips)
|
||||
all_miners = []
|
||||
async for miner in get_miner_genenerator:
|
||||
all_miners.append(miner)
|
||||
|
||||
data = []
|
||||
send_cmd_generator = send_command_generator(all_miners, command)
|
||||
async for command_sent in send_cmd_generator:
|
||||
data.append(command_sent)
|
||||
|
||||
proc_table_data = [[ip, ""] for ip in ips]
|
||||
for item in data:
|
||||
|
||||
if item["proc"].returncode == 0:
|
||||
return_data = item["proc"].stdout
|
||||
else:
|
||||
return_data = item["proc"].stderr
|
||||
if str(item["IP"]) in ips:
|
||||
proc_table_index = ips.index(str(item["IP"]))
|
||||
proc_table_data[proc_table_index] = [
|
||||
str(item["IP"]), return_data.replace("\n", " "),
|
||||
]
|
||||
ssh_cmd_window["ssh_cmd_table"].update(proc_table_data)
|
||||
|
||||
|
||||
async def reboot_generator(miners: list):
|
||||
loop = asyncio.get_event_loop()
|
||||
reboot_tasks = []
|
||||
|
||||
@@ -19,6 +19,20 @@ def copy_from_table(table):
|
||||
pyperclip.copy(copy_string)
|
||||
|
||||
|
||||
def copy_from_ssh_table(table):
|
||||
selection = table.selection()
|
||||
copy_values = []
|
||||
for each in selection:
|
||||
try:
|
||||
value = ", ".join(table.item(each)["values"])
|
||||
copy_values.append(str(value))
|
||||
except:
|
||||
pass
|
||||
copy_string = "\n".join(copy_values)
|
||||
pyperclip.copy(copy_string)
|
||||
|
||||
|
||||
|
||||
async def update_ui_with_data(key, message, append=False):
|
||||
if append:
|
||||
message = window[key].get_text() + message
|
||||
|
||||
@@ -40,7 +40,8 @@ layout = [
|
||||
sg.Button("REFRESH DATA", key='refresh_data'),
|
||||
sg.Button("OPEN IN WEB", key='open_in_web'),
|
||||
sg.Button("REBOOT", key='reboot_miners'),
|
||||
sg.Button("RESTART BACKEND", key='restart_miner_backend')],
|
||||
sg.Button("RESTART BACKEND", key='restart_miner_backend'),
|
||||
sg.Button("SEND SSH COMMAND", key='send_miner_ssh_command_window')],
|
||||
|
||||
[sg.Text("HR Total: ", pad=(0, 0)), sg.Text("", key="hr_total")],
|
||||
])],
|
||||
@@ -102,4 +103,31 @@ def generate_config_layout():
|
||||
return config_layout
|
||||
|
||||
|
||||
def send_ssh_cmd_layout(miner_list: list):
|
||||
cmd_layout = [
|
||||
[sg.Text('Command:', size=(9, 1)),
|
||||
sg.InputText(key='ssh_command_window_cmd', do_not_clear=True, size=(95, 1)),
|
||||
sg.Button("Send Command", key='ssh_command_window_send_cmd')
|
||||
],
|
||||
[sg.Text("")],
|
||||
[sg.Table(
|
||||
values=[[ip, ""] for ip in miner_list],
|
||||
headings=["IP",
|
||||
"Result"],
|
||||
auto_size_columns=False,
|
||||
max_col_width=15,
|
||||
justification="center",
|
||||
key="ssh_cmd_table",
|
||||
col_widths=[15, 90],
|
||||
background_color="white",
|
||||
text_color="black",
|
||||
size=(105, 27),
|
||||
expand_x=True,
|
||||
enable_click_events=True,
|
||||
)]
|
||||
|
||||
]
|
||||
return cmd_layout
|
||||
|
||||
|
||||
window = sg.Window('Upstream Config Util', layout, icon=icon_of_window)
|
||||
|
||||
@@ -3,12 +3,13 @@ import sys
|
||||
import PySimpleGUI as sg
|
||||
import tkinter as tk
|
||||
|
||||
from tools.cfg_util.cfg_util_sg.layout import window, generate_config_layout
|
||||
from tools.cfg_util.cfg_util_sg.layout import window, generate_config_layout, send_ssh_cmd_layout
|
||||
from tools.cfg_util.cfg_util_sg.func.miners import send_config, miner_light, refresh_data, generate_config, import_config, \
|
||||
scan_and_get_data, restart_miners_backend, reboot_miners
|
||||
scan_and_get_data, restart_miners_backend, reboot_miners, send_miners_ssh_commands
|
||||
from tools.cfg_util.cfg_util_sg.func.files import import_iplist, \
|
||||
import_config_file, export_iplist, export_config_file, export_csv
|
||||
from tools.cfg_util.cfg_util_sg.func.ui import sort_data, copy_from_table
|
||||
from tools.cfg_util.cfg_util_sg.func.decorators import disable_buttons
|
||||
from tools.cfg_util.cfg_util_sg.func.ui import sort_data, copy_from_table, copy_from_ssh_table
|
||||
|
||||
from network import MinerNetwork
|
||||
|
||||
@@ -54,6 +55,12 @@ async def ui():
|
||||
if event == "reboot_miners":
|
||||
if len(window["ip_table"].Values) > 0:
|
||||
asyncio.create_task(reboot_miners([window['ip_table'].Values[item][0] for item in value['ip_table']]))
|
||||
if event == "send_miner_ssh_command_window":
|
||||
ips = [window['ip_table'].Values[item][0] for item in value['ip_table']]
|
||||
if len(ips) == 0:
|
||||
ips = [item[0] for item in window["ip_table"].Values]
|
||||
if not len(ips) == 0:
|
||||
await generate_ssh_cmd_ui(ips)
|
||||
if event == 'light':
|
||||
if len(window["ip_table"].Values) > 0:
|
||||
asyncio.create_task(miner_light([window['ip_table'].Values[item][0] for item in value['ip_table']]))
|
||||
@@ -92,3 +99,21 @@ async def generate_config_ui():
|
||||
values['generate_config_window_allow_v2'])
|
||||
generate_config_window.close()
|
||||
break
|
||||
|
||||
|
||||
@disable_buttons
|
||||
async def generate_ssh_cmd_ui(selected_miners: list):
|
||||
ssh_cmd_window = sg.Window("Send Command", send_ssh_cmd_layout(selected_miners), modal=True)
|
||||
ssh_cmd_window.read(timeout=0)
|
||||
table = ssh_cmd_window["ssh_cmd_table"].Widget
|
||||
table.bind("<Control-Key-c>", lambda x: copy_from_ssh_table(table))
|
||||
# left justify the results
|
||||
table.column(1, anchor=tk.W)
|
||||
while True:
|
||||
event, values = ssh_cmd_window.read(timeout=0)
|
||||
if event in (None, 'Close', sg.WIN_CLOSED):
|
||||
break
|
||||
if event == "ssh_command_window_send_cmd":
|
||||
asyncio.create_task(send_miners_ssh_commands(selected_miners, values["ssh_command_window_cmd"], ssh_cmd_window))
|
||||
if event == "__TIMEOUT__":
|
||||
await asyncio.sleep(0)
|
||||
|
||||
Reference in New Issue
Block a user