RASCSI/src/web/file_cmds.py

74 lines
1.8 KiB
Python
Raw Normal View History

import fnmatch
import os
import subprocess
import time
from ractl_cmds import attach_image
from settings import *
2021-02-01 17:39:50 +00:00
def create_new_image(file_name, type, size):
if file_name == "":
file_name = "new_image." + str(int(time.time())) + "." + type
else:
file_name = file_name + "." + type
2021-02-01 17:39:50 +00:00
return subprocess.run(
2021-05-13 17:20:36 +00:00
["truncate", "--size", f"{size}m", f"{base_dir}{file_name}"],
2021-02-01 17:39:50 +00:00
capture_output=True,
)
def delete_image(file_name):
full_path = base_dir + file_name
if os.path.exists(full_path):
os.remove(full_path)
return True
else:
return False
def unzip_file(file_name):
import zipfile
2021-02-01 17:39:50 +00:00
with zipfile.ZipFile(base_dir + file_name, "r") as zip_ref:
zip_ref.extractall(base_dir)
return True
2021-02-01 17:39:50 +00:00
def rascsi_service(action):
# start/stop/restart
2021-02-01 17:39:50 +00:00
return (
subprocess.run(["sudo", "/bin/systemctl", action, "rascsi.service"]).returncode
== 0
)
def download_file_to_iso(scsi_id, url):
import urllib.request
2021-02-01 17:39:50 +00:00
file_name = url.split("/")[-1]
tmp_ts = int(time.time())
tmp_dir = "/tmp/" + str(tmp_ts) + "/"
os.mkdir(tmp_dir)
tmp_full_path = tmp_dir + file_name
iso_filename = base_dir + file_name + ".iso"
urllib.request.urlretrieve(url, tmp_full_path)
# iso_filename = make_cd(tmp_full_path, None, None) # not working yet
2021-02-01 17:39:50 +00:00
iso_proc = subprocess.run(
["genisoimage", "-hfs", "-o", iso_filename, tmp_full_path], capture_output=True
)
if iso_proc.returncode != 0:
return iso_proc
return attach_image(scsi_id, iso_filename, "SCCD")
def download_image(url):
import urllib.request
2021-02-01 17:39:50 +00:00
file_name = url.split("/")[-1]
full_path = base_dir + file_name
urllib.request.urlretrieve(url, full_path)