forked from adammc769/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_undocker.py
More file actions
131 lines (104 loc) · 3.24 KB
/
start_undocker.py
File metadata and controls
131 lines (104 loc) · 3.24 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
import os
import signal
import subprocess
import sys
from pathlib import Path
def _load_env_file(env_path: Path) -> None:
if not env_path.exists():
return
try:
from dotenv import load_dotenv # type: ignore
load_dotenv(env_path)
return
except Exception:
pass
# Minimal fallback loader (no variable expansion)
for raw_line in env_path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = value.strip().strip('"').strip("'")
if key and key not in os.environ:
os.environ[key] = value
def _popen(cmd: list[str], cwd: Path, env: dict[str, str]) -> subprocess.Popen:
kwargs: dict = {
"cwd": str(cwd),
"env": env,
}
# Make Ctrl+C delivery more reliable on Windows by using a new process group.
if os.name == "nt":
kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP # type: ignore[attr-defined]
return subprocess.Popen(cmd, **kwargs)
def main() -> int:
repo_root = Path(__file__).resolve().parent
_load_env_file(repo_root / ".env")
env = os.environ.copy()
env.setdefault("PYTHONUNBUFFERED", "1")
try:
import openai # noqa: F401
except Exception:
print("Missing dependency: openai")
print("Install dependencies, e.g.:")
print(" python -m pip install -r requirements/base.requirements.txt")
return 1
api_host = env.get("API_SERVER_HOST", "0.0.0.0")
api_port = env.get("API_SERVER_PORT", "8000")
api_cmd = [
sys.executable,
"-m",
"calico.workflow.cli",
"serve-api",
"--host",
api_host,
"--port",
str(api_port),
]
cli_cmd = [
sys.executable,
"-m",
"calico.cli.main",
]
api_proc = _popen(api_cmd, cwd=repo_root, env=env)
cli_proc = _popen(cli_cmd, cwd=repo_root, env=env)
try:
while True:
api_rc = api_proc.poll()
cli_rc = cli_proc.poll()
if api_rc is not None:
return api_rc
if cli_rc is not None:
return cli_rc
try:
api_proc.wait(timeout=0.5)
except subprocess.TimeoutExpired:
pass
except KeyboardInterrupt:
pass
finally:
for proc in (cli_proc, api_proc):
if proc.poll() is not None:
continue
try:
if os.name == "nt":
proc.send_signal(signal.CTRL_BREAK_EVENT)
else:
proc.send_signal(signal.SIGINT)
except Exception:
pass
# Give processes a moment to exit gracefully
for proc in (cli_proc, api_proc):
try:
proc.wait(timeout=5)
except Exception:
pass
for proc in (cli_proc, api_proc):
if proc.poll() is None:
try:
proc.kill()
except Exception:
pass
return 130
if __name__ == "__main__":
raise SystemExit(main())