Fix thread lock in AWS, VirtualBox, and VMware (#49)
* Initailize aws support * Add README for the VM server * Refactor OSWorld for supporting more cloud services. * Initialize vmware and aws implementation v1, waiting for verification * Initlize files for azure, gcp and virtualbox support * Debug on the VMware provider * Fix on aws interface mapping * Fix instance type * Refactor * Clean * Add Azure provider * hk region; debug * Fix lock * Remove print * Remove key_name requirements when allocating aws vm * Clean README * Fix reset * Fix bugs * Add VirtualBox and Azure providers * Add VirtualBox OVF link * Raise exception on macOS host * Init RAEDME for VBox * Update VirtualBox VM download link * Update requirements and setup.py; Improve robustness on Windows * Fix network adapter * Go through on Windows machine * Add default adapter option * Fix minor error * Change resolution before creating snapshot * Fix small error * Change default provider option * Fix thread lock * Refactor for more smooth VMware support --------- Co-authored-by: Timothyxxx <384084775@qq.com> Co-authored-by: XinyuanWangCS <xywang626@gmail.com> Co-authored-by: Tianbao Xie <47296835+Timothyxxx@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import time
|
||||
import os
|
||||
|
||||
from desktop_env.providers.base import Provider
|
||||
|
||||
logger = logging.getLogger("desktopenv.providers.vmware.VMwareProvider")
|
||||
@@ -28,12 +29,20 @@ def get_vmrun_type(return_list=False):
|
||||
|
||||
class VMwareProvider(Provider):
|
||||
@staticmethod
|
||||
def _execute_command(command: list):
|
||||
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60, text=True,
|
||||
encoding="utf-8")
|
||||
if result.returncode != 0:
|
||||
raise Exception("\033[91m" + result.stdout + result.stderr + "\033[0m")
|
||||
return result.stdout.strip()
|
||||
def _execute_command(command: list, return_output=False):
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
encoding="utf-8"
|
||||
)
|
||||
|
||||
if return_output:
|
||||
output = process.communicate()[0].strip()
|
||||
return output
|
||||
else:
|
||||
return None
|
||||
|
||||
def start_emulator(self, path_to_vm: str, headless: bool):
|
||||
print("Starting VMware VM...")
|
||||
@@ -51,9 +60,10 @@ class VMwareProvider(Provider):
|
||||
break
|
||||
else:
|
||||
logger.info("Starting VM...")
|
||||
VMwareProvider._execute_command(["vmrun"] + get_vmrun_type(return_list=True) + ["start", path_to_vm]) if not headless else \
|
||||
VMwareProvider._execute_command(
|
||||
["vmrun"] + get_vmrun_type(return_list=True) + ["start", path_to_vm, "nogui"])
|
||||
_command = ["vmrun"] + get_vmrun_type(return_list=True) + ["start", path_to_vm]
|
||||
if headless:
|
||||
_command.append("nogui")
|
||||
VMwareProvider._execute_command(_command)
|
||||
time.sleep(WAIT_TIME)
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
@@ -64,7 +74,8 @@ class VMwareProvider(Provider):
|
||||
while True:
|
||||
try:
|
||||
output = VMwareProvider._execute_command(
|
||||
["vmrun"] + get_vmrun_type(return_list=True) + ["getGuestIPAddress", path_to_vm, "-wait"]
|
||||
["vmrun"] + get_vmrun_type(return_list=True) + ["getGuestIPAddress", path_to_vm, "-wait"],
|
||||
return_output=True
|
||||
)
|
||||
logger.info(f"VMware VM IP address: {output}")
|
||||
return output
|
||||
@@ -75,12 +86,14 @@ class VMwareProvider(Provider):
|
||||
|
||||
def save_state(self, path_to_vm: str, snapshot_name: str):
|
||||
logger.info("Saving VMware VM state...")
|
||||
VMwareProvider._execute_command(["vmrun"] + get_vmrun_type(return_list=True) + ["snapshot", path_to_vm, snapshot_name])
|
||||
VMwareProvider._execute_command(
|
||||
["vmrun"] + get_vmrun_type(return_list=True) + ["snapshot", path_to_vm, snapshot_name])
|
||||
time.sleep(WAIT_TIME) # Wait for the VM to save
|
||||
|
||||
def revert_to_snapshot(self, path_to_vm: str, snapshot_name: str):
|
||||
logger.info(f"Reverting VMware VM to snapshot: {snapshot_name}...")
|
||||
VMwareProvider._execute_command(["vmrun"] + get_vmrun_type(return_list=True) + ["revertToSnapshot", path_to_vm, snapshot_name])
|
||||
VMwareProvider._execute_command(
|
||||
["vmrun"] + get_vmrun_type(return_list=True) + ["revertToSnapshot", path_to_vm, snapshot_name])
|
||||
time.sleep(WAIT_TIME) # Wait for the VM to revert
|
||||
return path_to_vm
|
||||
|
||||
|
||||
Reference in New Issue
Block a user