-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·285 lines (238 loc) · 10.5 KB
/
run.py
File metadata and controls
executable file
·285 lines (238 loc) · 10.5 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
# s2m watchdog with virtual environment management
import subprocess
from time import sleep
import logging, os, sys
import venv
def setup_logging():
"""Setup basic logging for the watchdog"""
logging.basicConfig(
level=logging.INFO,
format='[%(levelname)s] %(asctime)s - %(message)s',
handlers=[logging.StreamHandler()]
)
def get_venv_path():
"""Get the path to the virtual environment directory"""
script_dir = os.path.dirname(os.path.realpath(__file__))
return os.path.join(script_dir, "venv")
def get_python_executable():
"""Get the Python executable path for the virtual environment"""
venv_path = get_venv_path()
if os.name == 'nt': # Windows
return os.path.join(venv_path, "Scripts", "python.exe")
else: # Unix/Linux/macOS
return os.path.join(venv_path, "bin", "python")
def get_pip_executable():
"""Get the pip executable path for the virtual environment"""
venv_path = get_venv_path()
if os.name == 'nt': # Windows
return os.path.join(venv_path, "Scripts", "pip")
else: # Unix/Linux/macOS
return os.path.join(venv_path, "bin", "pip")
def create_venv():
"""Create a virtual environment"""
venv_path = get_venv_path()
logging.info(f"Creating virtual environment at: {venv_path}")
try:
# Create the virtual environment
venv.create(venv_path, with_pip=True)
logging.info("Virtual environment created successfully")
return True
except Exception as e:
logging.error(f"Failed to create virtual environment: {e}")
return False
def install_dependencies():
"""Install dependencies in the virtual environment"""
deps_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "deps.txt")
pip_executable = get_pip_executable()
if not os.path.exists(deps_file):
logging.error(f"Dependencies file not found: {deps_file}")
return False
logging.info(f"Installing dependencies from: {deps_file}")
try:
# Upgrade pip first
logging.info("Upgrading pip...")
result = subprocess.run([pip_executable, "install", "--upgrade", "pip"],
capture_output=True, text=True, check=True)
logging.info("pip upgraded successfully")
# Install dependencies with verbose output
logging.info("Installing dependencies...")
result = subprocess.run([pip_executable, "install", "-r", deps_file, "-v"],
capture_output=True, text=True, check=True)
logging.info("Dependencies installed successfully")
# Verify installation by checking each package
if verify_dependencies():
logging.info("All dependencies verified successfully")
return True
else:
logging.error("Dependency verification failed")
return False
except subprocess.CalledProcessError as e:
logging.error(f"Failed to install dependencies: {e}")
if e.stdout:
logging.error(f"STDOUT: {e.stdout}")
if e.stderr:
logging.error(f"STDERR: {e.stderr}")
return False
except Exception as e:
logging.error(f"Unexpected error during dependency installation: {e}")
return False
def verify_dependencies():
"""Verify that all dependencies are properly installed"""
deps_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "deps.txt")
python_executable = get_python_executable()
try:
# Read the dependencies file
with open(deps_file, 'r') as f:
deps = [line.strip() for line in f if line.strip() and not line.startswith('#')]
# Test each dependency
for dep in deps:
# Extract package name (handle cases like "package>=1.0.0")
package_name = dep.split('>=')[0].split('==')[0].split('<')[0].split('>')[0].strip()
logging.info(f"Verifying {package_name}...")
try:
result = subprocess.run([python_executable, "-c", f"import {package_name}"],
capture_output=True, text=True, check=True)
logging.info(f"✓ {package_name} verified")
except subprocess.CalledProcessError:
# Some packages have different import names, try common alternatives
alt_names = {
'python-dotenv': 'dotenv',
'paho-mqtt': 'paho.mqtt'
}
if package_name in alt_names:
try:
result = subprocess.run([python_executable, "-c", f"import {alt_names[package_name]}"],
capture_output=True, text=True, check=True)
logging.info(f"✓ {package_name} verified (as {alt_names[package_name]})")
continue
except:
pass
logging.error(f"✗ Failed to verify {package_name}")
return False
return True
except Exception as e:
logging.error(f"Error verifying dependencies: {e}")
return False
def setup_venv():
"""Setup virtual environment and install dependencies"""
venv_path = get_venv_path()
python_executable = get_python_executable()
# Check if virtual environment already exists and is valid
if os.path.exists(venv_path) and os.path.exists(python_executable):
logging.info("Virtual environment already exists")
# Verify it's working by trying to run python
try:
result = subprocess.run([python_executable, "--version"],
capture_output=True, text=True, check=True)
logging.info(f"Found virtual environment: {result.stdout.strip()}")
# Check if dependencies are installed
if verify_dependencies():
logging.info("All dependencies verified, using existing virtual environment")
return True
else:
logging.warning("Dependencies missing or broken, reinstalling...")
if not install_dependencies():
logging.error("Failed to install dependencies in existing venv")
# Try recreating the entire venv
logging.info("Recreating virtual environment...")
import shutil
try:
shutil.rmtree(venv_path)
except Exception as cleanup_error:
logging.error(f"Failed to remove broken venv: {cleanup_error}")
return False
else:
return True
except Exception as e:
logging.warning(f"Existing virtual environment appears broken: {e}")
logging.info("Recreating virtual environment...")
# Remove broken venv
import shutil
try:
shutil.rmtree(venv_path)
except Exception as cleanup_error:
logging.error(f"Failed to remove broken venv: {cleanup_error}")
return False
# Create new virtual environment
if not create_venv():
return False
# Install dependencies
if not install_dependencies():
return False
logging.info("Virtual environment setup completed successfully")
return True
def get_hostname():
"""Get hostname for logging purposes"""
try:
# Import the function from the libs module if available
from libs.system_info import get_hostname
return get_hostname()
except ImportError:
# Fallback to socket.gethostname if libs not available
import socket
return socket.gethostname()
def start_system2mqtt(envfile):
"""Start the main s2m.py script in the virtual environment, restarting on crash."""
python_executable = get_python_executable()
script_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "s2m.py")
hostname = get_hostname()
logging.info(f"Starting '{hostname}' script: '{script_path}' with config: '{envfile}'")
logging.info(f"Watchdog PID: {os.getpid()}")
while True:
try:
if envfile:
result = subprocess.call([python_executable, script_path, envfile])
else:
result = subprocess.call([python_executable, script_path])
if result == 0:
logging.info("Script exited normally")
return
else:
logging.warning(f"Script exited with code: {result}")
except KeyboardInterrupt:
logging.info("Received keyboard interrupt, shutting down...")
return
except Exception as e:
logging.error(f"Error running script: {e}")
logging.error(f"Script crashed! Restarting in {restart_timer} seconds")
sleep(restart_timer)
# Global variables
restart_timer = 2
if __name__ == '__main__':
# Setup logging first
setup_logging()
# Check for force reinstall flag
force_reinstall = False
args = sys.argv[1:]
if '--force-reinstall' in args:
force_reinstall = True
args.remove('--force-reinstall')
logging.info("Force reinstall requested - will recreate virtual environment")
# Remove existing venv
venv_path = get_venv_path()
if os.path.exists(venv_path):
import shutil
try:
shutil.rmtree(venv_path)
logging.info("Removed existing virtual environment")
except Exception as e:
logging.error(f"Failed to remove existing venv: {e}")
sys.exit(1)
# Setup virtual environment and dependencies
if not setup_venv():
logging.error("Failed to setup virtual environment. Exiting.")
logging.error("You can try running with --force-reinstall to recreate the virtual environment")
sys.exit(1)
# Determine config file
if len(args) > 0:
config_file = args[0]
else:
config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "s2m.conf")
# Validate config file exists
if not os.path.exists(config_file):
logging.error(f"Configuration file not found: {config_file}")
sys.exit(1)
logging.info(f"Using configuration file: {config_file}")
# Start the main application
start_system2mqtt(config_file)