Initialize VLC getters and metrics, fix some bugs in infra logic, needs to be refactored later on

This commit is contained in:
Timothyxxx
2024-01-04 17:05:17 +08:00
parent 03e99a68fb
commit ab71ebb2ba
7 changed files with 101 additions and 32 deletions

View File

@@ -10,12 +10,11 @@ from typing import Any
class SetupController:
def __init__( self
, http_server: str
, cache_dir: str
):
self.http_server = http_server + "/setup"
def __init__(self, http_server: str, cache_dir: str):
self.http_server: str = http_server
self.http_server_setup_root = http_server + "/setup"
self.cache_dir: str = cache_dir
def reset_cache_dir(self, cache_dir: str):
self.cache_dir = cache_dir
@@ -48,6 +47,32 @@ class SetupController:
# self._open_setup(config)
# can add other setup steps
def _command_setup(self, command: str):
"""
Directly send a command into the virtual machine os for setting up.
"""
payload = json.dumps({"command": command})
headers = {
'Content-Type': 'application/json'
}
timeout = 5
timout_whitelist = ["vlc"]
try:
response = requests.post(self.http_server + "/execute", headers=headers, data=payload, timeout=timeout)
if response.status_code == 200:
print("Command executed successfully:", response.text)
else:
print("Failed to execute command. Status code:", response.status_code)
except requests.exceptions.Timeout as e:
if command in timout_whitelist:
print("Command executed successfully:", command)
else:
print("An error occurred while trying to execute the command:", e)
except requests.exceptions.RequestException as e:
print("An error occurred while trying to execute the command:", e)
def _download_setup(self, files: List[Dict[str, str]]):
"""
Args:
@@ -66,12 +91,9 @@ class SetupController:
for f in files:
url: str = f["url"]
path: str = f["path"]
cache_path: str = os.path.join( self.cache_dir
, "{:}_{:}".format(
uuid.uuid5(uuid.NAMESPACE_URL, url)
, os.path.basename(path)
)
)
cache_path: str = os.path.join(self.cache_dir, "{:}_{:}".format(
uuid.uuid5(uuid.NAMESPACE_URL, url),
os.path.basename(path)))
if not url or not path:
raise Exception(f"Setup Download - Invalid URL ({url}) or path ({path}).")
@@ -97,21 +119,21 @@ class SetupController:
if not downloaded:
raise requests.RequestException(f"Failed to download {url}. No retries left. Error: {e}")
#payload = json.dumps({"url": url, "path": path})
#headers = {
#'Content-Type': 'application/json'
#}
# payload = json.dumps({"url": url, "path": path})
# headers = {
# 'Content-Type': 'application/json'
# }
form = MultipartEncoder( { "file_path": path
, "file_data": (os.path.basename(path), open(cache_path, "rb"))
}
)
form = MultipartEncoder({
"file_path": path,
"file_data": (os.path.basename(path), open(cache_path, "rb"))
})
headers = {"Content-Type": form.content_type}
print(form.content_type)
# send request to server to upload file
try:
response = requests.post(self.http_server + "/upload", headers=headers, data=form)
response = requests.post(self.http_server_setup_root + "/upload", headers=headers, data=form)
if response.status_code == 200:
print("Command executed successfully:", response.text)
else:
@@ -136,7 +158,7 @@ class SetupController:
# send request to server to change wallpaper
try:
response = requests.post(self.http_server + "/change_wallpaper", headers=headers, data=payload)
response = requests.post(self.http_server_setup_root + "/change_wallpaper", headers=headers, data=payload)
if response.status_code == 200:
print("Command executed successfully:", response.text)
else:
@@ -163,7 +185,7 @@ class SetupController:
# send request to server to open file
try:
response = requests.post(self.http_server + "/open_file", headers=headers, data=payload)
response = requests.post(self.http_server_setup_root + "/open_file", headers=headers, data=payload)
if response.status_code == 200:
print("Command executed successfully:", response.text)
else: