-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_server.py
140 lines (128 loc) · 3.83 KB
/
run_server.py
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
"""Script that runs the API server."""
import argparse
import json
import os
import sys
import tempfile
import time
import urllib.request
from base64 import b64encode
from pathlib import Path
from subprocess import Popen, run
REDIS_CONFIG = {
"user": "redis",
"passwd": "secret",
"host": "redis://localhost:6379",
"ssl_cert": "",
"ssl_key": "",
"scheduler_host": "localhost:4000",
"cache_exp": 86000,
}
TEMP_DIR = Path(
os.getenv("TEMP_DIR", os.path.join(tempfile.gettempdir(), "freva-nextgen"))
)
TEMP_DIR.mkdir(exist_ok=True, parents=True)
OIDC_URL = os.getenv(
"OIDC_URL",
"http://localhost:8080/realms/freva/.well-known/openid-configuration",
)
def prep_server(inp_dir: Path) -> None:
"""Prepare the first server startup."""
cert_dir = inp_dir / "certs"
cert_dir.mkdir(exist_ok=True, parents=True)
key_file = cert_dir / "client-key.pem"
cert_file = cert_dir / "client-cert.pem"
if not key_file.is_file() or not cert_file.is_file():
run(
[sys.executable, str(inp_dir / "dev-utils.py"), "gen-certs"],
check=True,
)
REDIS_CONFIG["ssl_key"] = key_file.read_text()
REDIS_CONFIG["ssl_cert"] = cert_file.read_text()
config_file = TEMP_DIR / "data-portal-cluster-config.json"
config_file.write_bytes(b64encode(json.dumps(REDIS_CONFIG).encode("utf-8")))
def kill_proc(proc: str) -> None:
"""Kill a process."""
run(
[
sys.executable,
os.path.join("dev-env", "config", "dev-utils.py"),
"kill",
f'{TEMP_DIR / f"{proc}.pid"}',
],
check=True,
)
def start_server(inp_dir: Path, foreground: bool = False, *args: str) -> None:
"""Set up the server"""
for proc in ("rest-server", "data-portal"):
kill_proc(proc)
prep_server(inp_dir)
config_file = TEMP_DIR / "data-portal-cluster-config.json"
args += ("--redis-ssl-certdir", str(inp_dir.absolute() / "certs"))
python_exe = sys.executable
portal_pid = TEMP_DIR / "data-portal.pid"
rest_pid = TEMP_DIR / "rest-server.pid"
try:
portal_proc = Popen(
[
python_exe,
"-m",
"data_portal_worker",
"-v",
"--dev",
"-c",
f"{config_file}",
]
)
rest_proc = Popen([python_exe, "-m", "freva_rest.cli"] + list(args))
portal_pid.write_text(str(portal_proc.pid))
rest_pid.write_text(str(rest_proc.pid))
if foreground:
portal_proc.communicate()
rest_proc.communicate()
except KeyboardInterrupt:
portal_proc.kill()
portal_pid.unlink()
rest_proc.kill()
rest_pid.unlink()
def cli() -> None:
"""Setup a cli."""
parser = argparse.ArgumentParser(
description=("Start the dev restAPI."),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--script-dir",
type=Path,
help="The the input directory all scripts are located.",
default=Path(__file__).parent / "dev-env" / "config",
)
parser.add_argument(
"--kill",
"-k",
help="Kill any running processes.",
action="store_true",
)
parser.add_argument(
"--foreground",
"-f",
help="Start service in the foreground",
action="store_true",
)
args, server_args = parser.parse_known_args()
if args.kill:
for proc in ("rest-server", "data-portal"):
kill_proc(proc)
return
run(
[
sys.executable,
os.path.join("dev-env", "config", "dev-utils.py"),
"oidc",
OIDC_URL,
],
check=True,
)
start_server(args.script_dir, args.foreground, *server_args)
if __name__ == "__main__":
cli()