-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcli.py
More file actions
186 lines (172 loc) · 6.22 KB
/
cli.py
File metadata and controls
186 lines (172 loc) · 6.22 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2026 Canonical Ltd.
# See LICENSE file for licensing details.
"""The CLI entrypoint for github-runner-manager application."""
import getpass
import grp
import importlib.metadata
import logging
import os
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, UserInfo
from github_runner_manager.http_server import FlaskArgs, start_http_server
from github_runner_manager.manager.pressure_reconciler import (
PressureReconciler,
PressureReconcilerConfig,
)
from github_runner_manager.manager.runner_manager import RunnerManager
from github_runner_manager.openstack_cloud.models import OpenStackServerConfig
from github_runner_manager.openstack_cloud.openstack_runner_manager import (
OpenStackRunnerManager,
OpenStackRunnerManagerConfig,
)
from github_runner_manager.planner_client import PlannerClient, PlannerConfiguration
from github_runner_manager.platform.github_provider import GitHubRunnerPlatform
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.",
)
# 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,
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.
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()))
thread_manager = ThreadManager()
thread_manager.add_thread(
target=partial(
start_http_server,
config,
Lock(),
FlaskArgs(host=host, port=port, debug=debug),
),
daemon=True,
)
pressure_reconciler = PressureReconciler(
manager=RunnerManager(
manager_name=config.name,
platform_provider=GitHubRunnerPlatform.build(
prefix=config.openstack_configuration.vm_prefix,
github_configuration=config.github_config,
),
cloud_runner_manager=OpenStackRunnerManager(
config=OpenStackRunnerManagerConfig(
allow_external_contributor=config.allow_external_contributor,
prefix=config.openstack_configuration.vm_prefix,
credentials=config.openstack_configuration.credentials,
server_config=(
None
if not config.non_reactive_configuration.combinations
else OpenStackServerConfig(
image=config.non_reactive_configuration.combinations[0].image.name,
flavor=config.non_reactive_configuration.combinations[0].flavor.name,
network=config.openstack_configuration.network,
)
),
service_config=config.service_config,
),
user=UserInfo(getpass.getuser(), grp.getgrgid(os.getgid()).gr_name),
),
labels=(
list(config.extra_labels)
+ (
[]
if not config.non_reactive_configuration.combinations
else (
config.non_reactive_configuration.combinations[0].image.labels
+ config.non_reactive_configuration.combinations[0].flavor.labels
)
)
),
),
planner_client=PlannerClient(
PlannerConfiguration(base_url=config.planner_url, token=config.planner_token)
),
config=PressureReconcilerConfig(
flavor_name=(
config.non_reactive_configuration.combinations[0].flavor.name
if config.non_reactive_configuration.combinations
else ""
)
),
)
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)
thread_manager.start()
thread_manager.raise_on_error()