-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmware_server.py
More file actions
180 lines (148 loc) · 6.83 KB
/
vmware_server.py
File metadata and controls
180 lines (148 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Manages the lifecycle of the VMware Workstation REST server.
Provides methods to start, stop, check if running and configure the server.
"""
import subprocess
import logging
import sys
import os
import platform
import time
import psutil
import requests
logging.basicConfig(level=logging.INFO)
class VMWareServer:
"""
Manages the lifecycle of the VMware Workstation REST server.
Provides methods to start, stop, check if running and configure the server.
Attributes:
state (str): Current state of the server (e.g., STARTING, RUNNING, STOPPED)
VMWARE_REST_EXE (str): Path to the VMware Workstation REST executable
is_server_running (method): Checks if the server is currently running
Methods:
start_server(): Starts the VMware Workstation REST server.
stop_server(): Stops the VMware Workstation REST server.
is_server_running(): Checks if the VMware Workstation REST server is running.
configure_vmware_server(): Configures the VMware Workstation REST server (not implemented in this example).
"""
RUNNING = "running"
STOPPED = "stopped"
def __init__(self, base_url, VMWARE_REST_EXE):
self.VMWARE_REST_EXE = VMWARE_REST_EXE
self.VMWARE_REST_PROCESS = "vmrest.exe"
self.BASE_URL = base_url
self.state = self.STOPPED if not self.is_server_running() else self.RUNNING
def configure_vmware_server(self):
"""
Configure the VMware Workstation REST server.
This method checks if the server executable exists in the specified location. If it does, the server is started with the -C option.
If the executable does not exist or an error occurs during startup, an error message is printed and False is returned.
Returns:
bool: True if the server was successfully configured, False otherwise.
"""
if not os.path.exists(self.VMWARE_REST_EXE):
print(f"Error: Server executable not found: {self.VMWARE_REST_EXE}.")
return False
try:
subprocess.run([self.VMWARE_REST_EXE,"-C"],text=True,check=False)
except FileNotFoundError:
print(f"Error: Server executable not found: {self.VMWARE_REST_EXE}.")
return False
except subprocess.SubprocessError as e:
print(f"Error starting server: {e}")
return False
def is_server_running(self, check_rest=False) -> bool:
"""
Check if the VMware Workstation REST server is running.
This method checks the current state of the server and returns True if it's running, False otherwise.
Returns:
bool: True if the server is running, False otherwise.
"""
# Check if the process is running
logging.info("Checking VMware Workstation REST server Power State.")
for proc in psutil.process_iter(["name"]):
if proc.info["name"] == self.VMWARE_REST_PROCESS:
self.state = self.RUNNING
return True
# Optionally check if the REST API is reachable
if check_rest:
try:
response = requests.get(self.BASE_URL, timeout=5)
if response.status_code == 200:
self.state = self.RUNNING
return True
except requests.exceptions.ConnectionError as e:
print(f"Error checking server using REST: {e}")
self.state = self.STOPPED
return False
def start_server(self) -> bool:
"""
Starts the VMware Workstation REST server.
This method checks if the server executable exists in the specified location. If it does, the server is started with the -C option.
If the executable does not exist or an error occurs during startup, an error message is printed and False is returned.
Returns:
bool: True if the server was successfully started, False otherwise.
"""
if self.state == self.RUNNING:
print("VMware Workstation REST server is already running.")
return True
if not os.path.exists(self.VMWARE_REST_EXE):
print(f"Error: Server executable not found: {self.VMWARE_REST_EXE}.")
return False
try:
logging.info("Starting VMware Workstation REST server.")
if platform.system() == "Windows":
# Start the server in a new process group to allow for termination
self.process = subprocess.Popen(
[self.VMWARE_REST_EXE],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
)
else:
subprocess.Popen(
[self.VMWARE_REST_EXE],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
preexec_fn=os.setpgrp,
)
time.sleep(3) # Allow the server time to start
if self.is_server_running(check_rest=True):
print("VMware Workstation REST server started successfully.")
self.state = self.RUNNING
return True
else:
print("Failed to start VMware Workstation REST server.")
return False
except FileNotFoundError:
print(f"Error: Server executable not found: {self.VMWARE_REST_EXE}.")
sys.exit(1)
except subprocess.SubprocessError as e:
print(f"Error starting server: {e}")
return False
def stop_server(self) -> bool:
"""
Stop the VMware Workstation REST server.
This method checks if the server is already stopped, and returns True if it is.
If the server is not stopped, an error message is printed indicating that the server is not running.
Returns:
bool: True if the server was successfully stopped, False otherwise.
"""
if self.state == self.STOPPED:
print("VMware Workstation REST server is not running.")
return True
logging.info("Stopping VMware Workstation REST server.")
for proc in psutil.process_iter(["pid", "name"]):
if proc.info["name"] == self.VMWARE_REST_PROCESS:
print(
f"Terminating process {proc.info['name']} (PID {proc.info['pid']})..."
)
proc.terminate()
proc.wait() # Wait for process to terminate
time.sleep(3) # Allow time for the termination
if not self.is_server_running():
print("VMware Workstation REST server stopped successfully.")
self.state = self.STOPPED
return True
print(f"No running process found for {self.VMWARE_REST_PROCESS}.")
return False