-
Notifications
You must be signed in to change notification settings - Fork 3
/
__main__.py
94 lines (73 loc) · 2.15 KB
/
__main__.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
import argparse as ap
from sys import argv
import uvicorn
parser = ap.ArgumentParser(description="fractal-server commands")
subparsers = parser.add_subparsers(title="Commands", dest="cmd", required=True)
# fractalctl start
startserver = subparsers.add_parser(
"start", description="Start the server (with uvicorn)"
)
startserver.add_argument(
"--host",
default="127.0.0.1",
type=str,
help="bind socket to this host (default: 127.0.0.1)",
)
startserver.add_argument(
"-p",
"--port",
default=8000,
type=int,
help="bind socket to this port (default: 8000)",
)
startserver.add_argument(
"--reload", default=False, action="store_true", help="enable auto-reload"
)
# fractalctl openapi
openapi_parser = subparsers.add_parser(
"openapi", description="Save the `openapi.json` file"
)
openapi_parser.add_argument(
"-f",
"--openapi-file",
type=str,
help="Filename for OpenAPI schema dump",
default="openapi.json",
)
# fractalctl set-db
subparsers.add_parser("set-db", description="Initialise the database")
def save_openapi(dest="openapi.json"):
from fractal_server.main import start_application
import json
app = start_application()
openapi_schema = app.openapi()
with open(dest, "w") as f:
json.dump(openapi_schema, f)
def set_db():
"""
Set-up / Upgrade database schema
Call alembic to upgrade to the latest migration.
Ref: https://stackoverflow.com/a/56683030/283972
"""
import alembic.config
from pathlib import Path
import fractal_server
alembic_ini = Path(fractal_server.__file__).parent / "alembic.ini"
alembic_args = ["-c", alembic_ini.as_posix(), "upgrade", "head"]
print(f"Run alembic.config, with argv={alembic_args}")
alembic.config.main(argv=alembic_args)
def run():
args = parser.parse_args(argv[1:])
if args.cmd == "openapi":
save_openapi(dest=args.openapi_file)
elif args.cmd == "set-db":
set_db()
else:
uvicorn.run(
"fractal_server.main:app",
host=args.host,
port=args.port,
reload=args.reload,
)
if __name__ == "__main__":
run()