automate vm ip address

This commit is contained in:
Jing Hua
2023-12-03 17:17:25 +08:00
parent 8cb31f1bb0
commit 58e9807a70
2 changed files with 12 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ def _execute_command(command: List[str]) -> None:
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=60, text=True)
if result.returncode != 0:
raise Exception("\033[91m" + result.stdout + result.stderr + "\033[0m")
return result.stdout
class DesktopEnv(gym.Env):
@@ -23,18 +24,17 @@ class DesktopEnv(gym.Env):
def __init__(
self,
path_to_vm: str,
host: str = "192.168.7.128:5000",
snapshot_path: str = "base",
action_space: str = "pyautogui",
):
# Initialize environment variables
self.path_to_vm = path_to_vm
self.host = host
self.snapshot_path = snapshot_path # todo: handling the logic of snapshot directory
# Initialize emulator and controller
print("Initializing...")
self._start_emulator()
self.host = self._get_vm_ip()
self.controller = PythonController(http_server=self.host)
# mode: human or machine
@@ -57,6 +57,16 @@ class DesktopEnv(gym.Env):
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e.output.decode().strip()}")
def _get_vm_ip(self):
max_retries = 3
for _ in range(max_retries):
try:
output = _execute_command(["vmrun", "-T", "ws", "getGuestIPAddress", self.path_to_vm])
return output.strip()
except:
time.sleep(2)
print("Retrying...")
def _save_state(self):
_execute_command(["vmrun", "-T", "ws" "snapshot", self.path_to_vm, self.snapshot_path])

View File

@@ -8,8 +8,6 @@ def human_agent():
env = DesktopEnv(
path_to_vm=r"""C:\Users\tianbaox\Documents\Virtual Machines\Win10\Win10.vmx""",
# path_to_vm="/home/yuri/vmware/Ubuntu 64-bit/Ubuntu 64-bit.vmx",
# host="192.168.7.128",
host="http://192.168.13.128:5000",
snapshot_path="base3",
)