-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcli.py
More file actions
147 lines (133 loc) · 4.32 KB
/
cli.py
File metadata and controls
147 lines (133 loc) · 4.32 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright 2026 Canonical Ltd.
# See LICENSE file for licensing details.
"""The CLI entrypoint for github-runner-manager application."""
import importlib.metadata
import logging
import signal
import sys
from functools import partial
from io import StringIO
from threading import Lock
from types import FrameType
from typing import TextIO
import click
from github_runner_manager.configuration import ApplicationConfiguration
from github_runner_manager.http_server import FlaskArgs, start_http_server
from github_runner_manager.manager.pressure_reconciler import (
PressureReconciler,
build_pressure_reconciler,
)
from github_runner_manager.reconcile_service import start_reconcile_service
from github_runner_manager.thread_manager import ThreadManager
version = importlib.metadata.version("github-runner-manager")
def handle_shutdown(
signum: int, _frame: FrameType | None, pressure_reconciler: PressureReconciler
) -> None: # pragma: no cover
"""Stop reconciler threads on shutdown signals.
Args:
signum: Received POSIX signal number.
_frame: Current stack frame when the signal was received.
pressure_reconciler: The reconciler instance to stop.
"""
logging.info("Received signal %s; stopping pressure reconciler", signum)
pressure_reconciler.stop()
@click.command()
@click.option(
"--config-file",
type=click.File(mode="r", encoding="utf-8"),
help="The file path containing the configurations.",
)
@click.option(
"--host",
type=str,
help="The hostname to listen on for the HTTP server.",
default="127.0.0.1",
)
@click.option(
"--port",
type=int,
help="The port to listen on for the HTTP server.",
default=8080,
)
@click.option(
"--debug",
is_flag=True,
show_default=True,
default=False,
help="Debug mode for testing.",
)
@click.option(
"--log-level",
type=click.Choice(
[
"CRITICAL",
"FATAL",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
]
),
default="INFO",
help="The log level for the application.",
)
@click.option(
"--python-path",
type=str,
required=False,
help="The PYTHONPATH to access the github-runner-manager library.",
)
# The entry point for the CLI will be tested with integration test.
def main( # pylint: disable=too-many-arguments, too-many-positional-arguments
config_file: TextIO,
host: str,
port: int,
debug: bool,
python_path: str | None,
log_level: str,
) -> None: # pragma: no cover
"""Start the reconcile service.
Args:
config_file: The configuration file.
host: The hostname to listen on for the HTTP server
port: The port to listen on the HTTP server.
debug: Whether to start the application in debug mode.
python_path: PYTHONPATH to access the github-runner-manager library.
log_level: The log level.
"""
logging.basicConfig(
level=log_level,
stream=sys.stderr,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
)
logging.info("Starting GitHub runner manager service version: %s", version)
config = ApplicationConfiguration.from_yaml_file(StringIO(config_file.read()))
lock = Lock()
thread_manager = ThreadManager()
thread_manager.add_thread(
target=partial(
start_http_server,
config,
lock,
FlaskArgs(host=host, port=port, debug=debug),
),
daemon=True,
)
if config.planner_url and config.planner_token:
pressure_reconciler = build_pressure_reconciler(config, lock)
signal.signal(
signal.SIGTERM, partial(handle_shutdown, pressure_reconciler=pressure_reconciler)
)
signal.signal(
signal.SIGINT, partial(handle_shutdown, pressure_reconciler=pressure_reconciler)
)
thread_manager.add_thread(target=pressure_reconciler.start_create_loop, daemon=True)
thread_manager.add_thread(target=pressure_reconciler.start_delete_loop, daemon=True)
# Legacy mode is still supported for deployments without planner config.
else:
thread_manager.add_thread(
target=partial(start_reconcile_service, config, python_path, lock),
daemon=True,
)
thread_manager.start()
thread_manager.raise_on_error()