From 58e9807a702b53cdca5cf5e20e9e1690e0771387 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sun, 3 Dec 2023 17:17:25 +0800 Subject: [PATCH] automate vm ip address --- desktop_env/envs/desktop_env.py | 14 ++++++++++++-- main.py | 2 -- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/desktop_env/envs/desktop_env.py b/desktop_env/envs/desktop_env.py index b4347cb..b06abbb 100644 --- a/desktop_env/envs/desktop_env.py +++ b/desktop_env/envs/desktop_env.py @@ -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]) diff --git a/main.py b/main.py index 9ef3982..bbfd8d2 100644 --- a/main.py +++ b/main.py @@ -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", )