|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
1 | 4 | import tempfile
|
2 | 5 | from pathlib import Path
|
3 | 6 |
|
|
16 | 19 | NAMESPACES_FILE,
|
17 | 20 | NETWORK_FILE,
|
18 | 21 | )
|
19 |
| -from .k8s import get_default_namespace |
| 22 | +from .k8s import get_default_namespace, wait_for_caddy_ready |
20 | 23 | from .process import stream_command
|
21 | 24 |
|
22 | 25 |
|
@@ -102,14 +105,18 @@ def deploy_caddy(directory: Path, debug: bool):
|
102 | 105 | return
|
103 | 106 |
|
104 | 107 | namespace = get_default_namespace()
|
105 |
| - cmd = f"{HELM_COMMAND} 'caddy' {CADDY_CHART} --namespace {namespace}" |
| 108 | + name = "caddy" |
| 109 | + cmd = f"{HELM_COMMAND} {name} {CADDY_CHART} --namespace {namespace}" |
106 | 110 | if debug:
|
107 | 111 | cmd += " --debug"
|
108 | 112 |
|
109 | 113 | if not stream_command(cmd):
|
110 | 114 | click.echo(f"Failed to run Helm command: {cmd}")
|
111 | 115 | return
|
112 | 116 |
|
| 117 | + wait_for_caddy_ready(name, namespace) |
| 118 | + _port_start_internal() |
| 119 | + |
113 | 120 |
|
114 | 121 | def deploy_fork_observer(directory: Path, debug: bool):
|
115 | 122 | network_file_path = directory / NETWORK_FILE
|
@@ -242,3 +249,43 @@ def deploy_namespaces(directory: Path):
|
242 | 249 | finally:
|
243 | 250 | if temp_override_file_path.exists():
|
244 | 251 | temp_override_file_path.unlink()
|
| 252 | + |
| 253 | + |
| 254 | +def is_windows(): |
| 255 | + return sys.platform.startswith("win") |
| 256 | + |
| 257 | + |
| 258 | +def run_detached_process(command): |
| 259 | + if is_windows(): |
| 260 | + # For Windows, use CREATE_NEW_PROCESS_GROUP and DETACHED_PROCESS |
| 261 | + subprocess.Popen( |
| 262 | + command, |
| 263 | + shell=True, |
| 264 | + stdin=None, |
| 265 | + stdout=None, |
| 266 | + stderr=None, |
| 267 | + close_fds=True, |
| 268 | + creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS, |
| 269 | + ) |
| 270 | + else: |
| 271 | + # For Unix-like systems, use nohup and redirect output |
| 272 | + command = f"nohup {command} > /dev/null 2>&1 &" |
| 273 | + subprocess.Popen(command, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True) |
| 274 | + |
| 275 | + print(f"Started detached process: {command}") |
| 276 | + |
| 277 | + |
| 278 | +def _port_start_internal(): |
| 279 | + command = "kubectl port-forward service/caddy 2019:80" |
| 280 | + run_detached_process(command) |
| 281 | + click.echo( |
| 282 | + "Port forwarding on port 2019 started in the background. To access landing page visit localhost:2019." |
| 283 | + ) |
| 284 | + |
| 285 | + |
| 286 | +def _port_stop_internal(): |
| 287 | + if is_windows(): |
| 288 | + os.system("taskkill /F /IM kubectl.exe") |
| 289 | + else: |
| 290 | + os.system("pkill -f 'kubectl port-forward service/caddy-service 2019:80'") |
| 291 | + click.echo("Port forwarding stopped.") |
0 commit comments