Skip to content

Commit 391eece

Browse files
committed
cont.
1 parent 663e968 commit 391eece

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

custom-domain/dstack-ingress/scripts/certman.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def install_plugin(self) -> bool:
3030
print(f"No certbot package defined for {self.provider_type}")
3131
return False
3232

33+
# First ensure certbot is installed in the current environment
34+
self._ensure_certbot_in_env()
35+
3336
# Check if plugin is already installed
3437
try:
3538
if self.provider.CERTBOT_PLUGIN == "dns-namecheap":
@@ -87,6 +90,10 @@ def install_plugin(self) -> bool:
8790
import pkg_resources
8891
print(f"Installed to Python: {sys.executable}")
8992

93+
# Show certbot location
94+
certbot_exec = self._get_certbot_executable()
95+
print(f"Using certbot: {certbot_exec}")
96+
9097
try:
9198
dist = pkg_resources.get_distribution("certbot-dns-namecheap")
9299
print(f"Package version: {dist.version} at {dist.location}")
@@ -102,7 +109,8 @@ def install_plugin(self) -> bool:
102109
print(f"Plugin {self.provider.CERTBOT_PLUGIN} successfully imported")
103110

104111
# Test if plugin is recognized by certbot
105-
test_cmd = ["certbot", "plugins"]
112+
certbot_exec = self._get_certbot_executable()
113+
test_cmd = [certbot_exec, "plugins"]
106114
test_result = subprocess.run(test_cmd, capture_output=True, text=True, timeout=10)
107115

108116
if test_result.returncode == 0 and "dns-namecheap" in test_result.stdout:
@@ -127,7 +135,8 @@ def install_plugin(self) -> bool:
127135

128136
if force_result.returncode == 0:
129137
# Test again after reinstall
130-
retest_result = subprocess.run(test_cmd, capture_output=True, text=True, timeout=10)
138+
retest_cmd = [certbot_exec, "plugins"]
139+
retest_result = subprocess.run(retest_cmd, capture_output=True, text=True, timeout=10)
131140
if retest_result.returncode == 0 and "dns-namecheap" in retest_result.stdout:
132141
print(f"✓ Plugin registration fixed after reinstall")
133142
return True
@@ -147,12 +156,75 @@ def install_plugin(self) -> bool:
147156

148157
return True
149158

159+
def _ensure_certbot_in_env(self) -> None:
160+
"""Ensure certbot is installed in the current Python environment."""
161+
import sys
162+
import shutil
163+
164+
# Check if certbot is available in current environment
165+
python_dir = os.path.dirname(sys.executable)
166+
potential_certbot = os.path.join(python_dir, "certbot")
167+
168+
if os.path.exists(potential_certbot):
169+
print(f"Certbot already available in current environment: {potential_certbot}")
170+
return
171+
172+
# Try to import certbot to check if it's installed
173+
try:
174+
import certbot
175+
print(f"Certbot module available in current environment")
176+
return
177+
except ImportError:
178+
pass
179+
180+
print(f"Installing certbot in current environment...")
181+
try:
182+
install_cmd = [sys.executable, "-m", "pip", "install", "certbot"]
183+
print(f"Running: {' '.join(install_cmd)}")
184+
result = subprocess.run(install_cmd, capture_output=True, text=True)
185+
186+
if result.returncode == 0:
187+
print(f"✓ Certbot installed successfully in current environment")
188+
else:
189+
print(f"Failed to install certbot: {result.stderr}")
190+
# Continue anyway - system certbot might still work
191+
except Exception as e:
192+
print(f"Error installing certbot: {e}")
193+
# Continue anyway - system certbot might still work
194+
195+
def _get_certbot_executable(self) -> str:
196+
"""Get the correct certbot executable path that matches the Python environment."""
197+
import sys
198+
import shutil
199+
200+
# Try to find certbot in the same environment as the current Python
201+
python_dir = os.path.dirname(sys.executable)
202+
potential_certbot = os.path.join(python_dir, "certbot")
203+
204+
if os.path.exists(potential_certbot):
205+
print(f"Found certbot in same environment: {potential_certbot}")
206+
return potential_certbot
207+
208+
# Try which certbot
209+
certbot_path = shutil.which("certbot")
210+
if certbot_path:
211+
print(f"Found certbot via which: {certbot_path}")
212+
return certbot_path
213+
214+
# Fall back to system certbot
215+
print(f"Falling back to system certbot command")
216+
return "certbot"
217+
150218
def _debug_plugin_registration(self) -> None:
151219
"""Debug why plugin is not being registered by certbot."""
152220
try:
153221
import pkg_resources
154222
print("=== Plugin Registration Debug ===")
155223

224+
# Show which certbot we're using
225+
certbot_exec = self._get_certbot_executable()
226+
print(f"Using certbot: {certbot_exec}")
227+
156228
# Check entry points
157229
try:
158230
entry_points = list(pkg_resources.iter_entry_points('certbot.plugins'))
@@ -220,7 +292,9 @@ def _build_certbot_command(self, action: str, domain: str, email: str) -> List[s
220292
if not plugin:
221293
raise ValueError(f"No certbot plugin configured for {self.provider_type}")
222294

223-
base_cmd = ["certbot", action, "-a", plugin, "--non-interactive", "-v"]
295+
# Use the same certbot executable as the Python environment
296+
certbot_exec = self._get_certbot_executable()
297+
base_cmd = [certbot_exec, action, "-a", plugin, "--non-interactive", "-v"]
224298

225299
# Add credentials file if configured
226300
if self.provider.CERTBOT_CREDENTIALS_FILE:

0 commit comments

Comments
 (0)