2020-12-31 01:39:32 +00:00
|
|
|
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
|
|
|
|
2020-12-31 01:39:32 +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,
|
|
|
|
)
|
2020-12-31 01:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2020-12-31 01:39:32 +00:00
|
|
|
zip_ref.extractall(base_dir)
|
|
|
|
return True
|
|
|
|
|
2021-02-01 17:39:50 +00:00
|
|
|
|
2020-12-31 01:39:32 +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
|
|
|
|
)
|
2020-12-31 01:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def download_file_to_iso(scsi_id, url):
|
|
|
|
import urllib.request
|
2021-02-01 17:39:50 +00:00
|
|
|
|
|
|
|
file_name = url.split("/")[-1]
|
2020-12-31 01:39:32 +00:00
|
|
|
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
|
|
|
|
)
|
2020-12-31 01:39:32 +00:00
|
|
|
if iso_proc.returncode != 0:
|
|
|
|
return iso_proc
|
2021-02-01 18:24:59 +00:00
|
|
|
return attach_image(scsi_id, iso_filename, "SCCD")
|
2020-12-31 01:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def download_image(url):
|
|
|
|
import urllib.request
|
2021-02-01 17:39:50 +00:00
|
|
|
|
|
|
|
file_name = url.split("/")[-1]
|
2020-12-31 01:39:32 +00:00
|
|
|
full_path = base_dir + file_name
|
|
|
|
|
|
|
|
urllib.request.urlretrieve(url, full_path)
|