-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_utils.py
More file actions
127 lines (105 loc) · 3.92 KB
/
port_utils.py
File metadata and controls
127 lines (105 loc) · 3.92 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
#!/usr/bin/env python3
"""
Port Management Utilities
Handles port discovery, cleanup, and management for the GPU server application.
"""
import subprocess
import sys
def find_available_local_port(port_range):
"""
Find an available local port for forwarding.
Args:
port_range: Range of ports to check (can be range object or tuple)
Returns:
int or None: Available port number or None if none found
"""
# Convert tuple to range if needed
if isinstance(port_range, tuple):
port_range = range(port_range[0], port_range[1] + 1)
print(f"🔍 Checking ports in range: {port_range}")
for port in port_range:
try:
# Check if port is available
result = subprocess.run(f"lsof -i:{port}", shell=True, capture_output=True)
if result.returncode != 0:
print(f"✅ Found available port: {port}")
return port
else:
print(f"❌ Port {port} is in use")
except Exception as e:
print(f"⚠️ Error checking port {port}: {e}")
continue
print(f"❌ No available ports found in range {port_range}")
return None
def find_available_flask_port():
"""
Find an available port for Flask app.
Returns:
int or None: Available port number or None if none found
"""
try:
from config import DYNAMIC_PORT_RANGE
port_range = DYNAMIC_PORT_RANGE
except ImportError:
port_range = range(2344, 2400)
for port in port_range:
try:
# Check if port is available
result = subprocess.run(f"lsof -i:{port}", shell=True, capture_output=True)
if result.returncode != 0:
return port
except:
continue
return None
def cleanup_all_ports(port_range):
"""
Clean up all SSH tunnels and processes on local ports.
Args:
port_range: Range of ports to clean up
"""
print(f"🧹 Global port cleanup initiated...")
try:
# More efficient bulk cleanup approach
port_list = list(port_range)
if not port_list:
return
# Create a single command to check all ports at once
ports_str = ','.join(map(str, port_list))
bulk_cleanup_cmd = f"lsof -ti:{ports_str} | xargs kill -9 2>/dev/null || true"
# Run bulk cleanup with timeout
result = subprocess.run(bulk_cleanup_cmd, shell=True, capture_output=True, text=True, timeout=5)
if result.returncode == 0 and result.stdout.strip():
print(f"✅ Bulk cleaned up ports in range")
# Also kill any ssh processes that might be hanging (with timeout)
ssh_cleanup = "timeout 3 pkill -f 'ssh.*-L.*localhost' 2>/dev/null || true"
subprocess.run(ssh_cleanup, shell=True, capture_output=True, timeout=3)
print(f"✅ Cleaned up SSH tunnel processes")
except subprocess.TimeoutExpired:
print(f"⚠️ Port cleanup timeout - continuing...")
except Exception as e:
print(f"⚠️ Error during global port cleanup: {e}")
def cleanup_specific_port(port):
"""
Clean up a specific port.
Args:
port (int): Port number to clean up
"""
try:
cleanup_cmd = f"lsof -ti:{port} | xargs kill -9 2>/dev/null || true"
subprocess.run(cleanup_cmd, shell=True, capture_output=True)
print(f"✅ Cleaned up port {port}")
except Exception as e:
print(f"⚠️ Error cleaning up port {port}: {e}")
def check_port_usage(port):
"""
Check if a specific port is in use.
Args:
port (int): Port number to check
Returns:
bool: True if port is in use, False otherwise
"""
try:
result = subprocess.run(f"lsof -i:{port}", shell=True, capture_output=True)
return result.returncode == 0
except Exception:
return False