diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index a7b0af8a..90e18534 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -14,6 +14,7 @@ from ractl_cmds import ( list_devices, reserve_scsi_ids, ) +from pi_cmds import run_async from socket_cmds import send_pb_command from settings import CFG_DIR, CONFIG_FILE_SUFFIX, PROPERTIES_SUFFIX, RESERVATIONS import rascsi_interface_pb2 as proto @@ -175,42 +176,42 @@ def unzip_file(file_name, member=False, members=False): members contains all of the full paths to each of the zip archive members Returns (dict) with (boolean) status and (list of str) msg """ - from subprocess import run - from re import escape + from asyncio import run server_info = get_server_info() prop_flag = False if not member: - unzip_proc = run( - ["unzip", "-d", server_info["image_dir"], "-n", "-j", \ - f"{server_info['image_dir']}/{file_name}"], capture_output=True - ) + unzip_proc = run(run_async( + f"unzip -d {server_info['image_dir']} -n -j " + f"{server_info['image_dir']}/{file_name}" + )) for path in members: if path.endswith(PROPERTIES_SUFFIX): name = PurePath(path).name rename_file(f"{server_info['image_dir']}/{name}", f"{CFG_DIR}/{name}") prop_flag = True else: - unzip_proc = run( - ["unzip", "-d", server_info["image_dir"], "-n", "-j", \ - f"{server_info['image_dir']}/{file_name}", escape(member)], capture_output=True - ) + from re import escape + member = escape(member) + unzip_proc = run(run_async( + f"unzip -d {server_info['image_dir']} -n -j " + f"{server_info['image_dir']}/{file_name} {member}" + )) # Attempt to unzip a properties file in the same archive dir - unzip_prop = run( - ["unzip", "-d", CFG_DIR, "-n", "-j", \ - f"{server_info['image_dir']}/{file_name}", escape(member) + "." + PROPERTIES_SUFFIX], capture_output=False - ) - if unzip_prop.returncode == 0: + unzip_prop = run(run_async( + f"unzip -d {CFG_DIR} -n -j " + f"{server_info['image_dir']}/{file_name} {member}.{PROPERTIES_SUFFIX}" + )) + if unzip_prop["returncode"] == 0: prop_flag = True - if unzip_proc.returncode != 0: - stderr = unzip_proc.stderr.decode("utf-8") - logging.warning("Unzipping failed: %s", stderr) - return {"status": False, "msg": stderr} + if unzip_proc["returncode"] != 0: + logging.warning("Unzipping failed: %s", unzip_proc["stderr"]) + return {"status": False, "msg": unzip_proc["stderr"]} from re import findall unzipped = findall( "(?:inflating|extracting):(.+)\n", - unzip_proc.stdout.decode("utf-8") + unzip_proc["stdout"] ) return {"status": True, "msg": unzipped, "prop_flag": prop_flag} @@ -404,7 +405,6 @@ def write_drive_properties(file_name, conf): return {"status": False, "msg": f"Could not write to file: {file_path}"} - def read_drive_properties(path_name): """ Reads drive properties from json formatted file. diff --git a/src/web/pi_cmds.py b/src/web/pi_cmds.py index 85cf87f9..4ed04965 100644 --- a/src/web/pi_cmds.py +++ b/src/web/pi_cmds.py @@ -3,6 +3,8 @@ Module for methods controlling and getting information about the Pi's Linux syst """ import subprocess +import asyncio +import logging from settings import AUTH_GROUP @@ -11,10 +13,8 @@ def systemd_service(service, action): Takes (str) service and (str) action Action can be one of start/stop/restart """ - return ( - subprocess.run(["sudo", "/bin/systemctl", action, service]).returncode - == 0 - ) + proc = asyncio.run(run_async("sudo /bin/systemctl {action} {service}")) + return proc["returncode"] == 0 def reboot_pi(): @@ -119,6 +119,30 @@ def introspect_file(file_path, re_term): return False +async def run_async(cmd): + """ + Takes (str) cmd with the shell command to execute + Executes shell command and captures output + Returns (dict) with (int) returncode, (str) stdout, (str) stderr + """ + proc = await asyncio.create_subprocess_shell( + cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE) + + stdout, stderr = await proc.communicate() + + logging.info("Executed command \"%s\" with status code %d", cmd, proc.returncode) + if stdout: + stdout = stdout.decode() + logging.info("stdout: %s", stdout) + if stderr: + stderr = stderr.decode() + logging.info("stderr: %s", stderr) + + return {"returncode": proc.returncode, "stdout": stdout, "stderr": stderr} + + def auth_active(): """ Inspects if the group defined in AUTH_GROUP exists on the system. @@ -132,4 +156,4 @@ def auth_active(): "status": True, "msg": "You must log in to use this function!", } - return {"status": False, "msg": ""} + return {"status": False, "msg": ""} \ No newline at end of file diff --git a/src/web/templates/base.html b/src/web/templates/base.html index 30be44ad..33b0f581 100644 --- a/src/web/templates/base.html +++ b/src/web/templates/base.html @@ -59,7 +59,7 @@ -
{{ message }}diff --git a/src/web/templates/index.html b/src/web/templates/index.html index edaac483..f7cf5019 100644 --- a/src/web/templates/index.html +++ b/src/web/templates/index.html @@ -201,7 +201,7 @@