-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbase.py
More file actions
283 lines (220 loc) · 9.55 KB
/
base.py
File metadata and controls
283 lines (220 loc) · 9.55 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright 2026 Canonical Ltd.
# See LICENSE file for licensing details.
"""Base configuration for the Application."""
import logging
from dataclasses import dataclass
from typing import Optional, TextIO
import yaml
from pydantic import AnyHttpUrl, BaseModel, Field, IPvAnyAddress, MongoDsn, root_validator
from github_runner_manager.configuration import github
from github_runner_manager.openstack_cloud.configuration import OpenStackConfiguration
logger = logging.getLogger(__name__)
# The github-runner-manager is being refactor from a library to an application.
# Once the charm no longer rely on the github-runner-manager as a library this will be removed.
# The github-runner-manager needs a input representing the user for process execution due to as a
# library the user needs to be a hardcoded value. With the github-runner-manager as application,
# user would be the current user running the application.
@dataclass
class UserInfo:
"""The user to run the reactive process.
Attributes:
user: The user for running the reactive processes.
group: The user group for running the reactive processes.
"""
user: str
group: str
class ApplicationConfiguration(BaseModel):
"""Main entry point for the Application Configuration.
Attributes:
allow_external_contributor: Whether to allow runs from forked repository from an external
contributor. Enabling this option will enable all runs from forked repositories. By
default, runs from contribution authors being in COLLABORATOR, MEMBER or OWNER status
is allowed. See \
https://docs.github.com/en/graphql/reference/enums#commentauthorassociation.
name: Name to identify the manager. Used for metrics.
extra_labels: Extra labels to add to the runner.
github_config: GitHub configuration.
service_config: The configuration for supporting services.
non_reactive_configuration: Configuration for non-reactive mode.
reactive_configuration: Configuration for reactive mode.
openstack_configuration: Configuration for authorization to a OpenStack host.
planner_url: Base URL of the planner service.
planner_token: Bearer token to authenticate against the planner service.
reconcile_interval: Seconds to wait between reconciliation.
"""
allow_external_contributor: bool = False
name: str
extra_labels: list[str]
github_config: github.GitHubConfiguration
service_config: "SupportServiceConfig"
non_reactive_configuration: "NonReactiveConfiguration"
reactive_configuration: "ReactiveConfiguration | None"
openstack_configuration: OpenStackConfiguration
planner_url: AnyHttpUrl
planner_token: str
reconcile_interval: int
@staticmethod
def from_yaml_file(file: TextIO) -> "ApplicationConfiguration":
"""Initialize configuration from a YAML formatted file.
Args:
file: The file object to parse the configuration from.
Returns:
The configuration.
"""
config = yaml.safe_load(file)
return ApplicationConfiguration.validate(config)
class SupportServiceConfig(BaseModel):
"""Configuration for supporting services for runners.
Attributes:
manager_proxy_command: ProxyCommand to use for the ssh connection to the runner.
proxy_config: The proxy configuration.
runner_proxy_config: The proxy configuration for the runner.
use_aproxy: Whether aproxy should be used for the runners.
aproxy_exclude_addresses: A list of addresses to exclude from the aproxy proxy.
aproxy_redirect_ports: A list of ports to redirect to the aproxy proxy.
dockerhub_mirror: The dockerhub mirror to use for runners.
ssh_debug_connections: The information on the ssh debug services.
custom_pre_job_script: The custom pre-job script to run before the job.
"""
manager_proxy_command: str | None = None
proxy_config: "ProxyConfig | None"
runner_proxy_config: "ProxyConfig | None"
use_aproxy: bool
aproxy_exclude_addresses: list[str] = []
aproxy_redirect_ports: list[str] = []
dockerhub_mirror: str | None
ssh_debug_connections: "list[SSHDebugConnection]"
custom_pre_job_script: str | None
@root_validator(pre=False, skip_on_failure=True)
@classmethod
def check_use_aproxy(cls, values: dict) -> dict:
"""Validate the proxy configuration required if aproxy is enabled.
Args:
values: Values in the pydantic model.
Raises:
ValueError: if use_aproxy was set but no http/https was passed.
Returns:
Values in the pydantic model.
"""
runner_proxy_enabled = False
runner_proxy_config = values.get("runner_proxy_config")
if runner_proxy_config and runner_proxy_config.proxy_address:
runner_proxy_enabled = True
if values.get("use_aproxy") and not runner_proxy_enabled:
raise ValueError("aproxy requires the runner http or https to be set")
return values
class ProxyConfig(BaseModel):
"""Proxy configuration.
Attributes:
http: HTTP proxy address.
https: HTTPS proxy address.
no_proxy: Comma-separated list of hosts that should not be proxied.
proxy_address: The address of the proxy.
proxy_host: The host of the proxy.
proxy_port: The port of the proxy.
"""
http: Optional[AnyHttpUrl]
https: Optional[AnyHttpUrl]
no_proxy: Optional[str]
@property
def proxy_address(self) -> Optional[str]:
"""Return the address of the proxy."""
proxy = self.http or self.https
if proxy:
proxy_address = proxy.host if not proxy.port else f"{proxy.host}:{proxy.port}"
return proxy_address
return None
@property
def proxy_host(self) -> Optional[str]:
"""Return the host of the proxy."""
proxy_address = self.http or self.https
return proxy_address.host if proxy_address else None
@property
def proxy_port(self) -> Optional[str]:
"""Return the port of the proxy."""
proxy_address = self.http or self.https
return proxy_address.port if proxy_address else None
def __bool__(self) -> bool:
"""Return whether the proxy config is set.
Returns:
Whether the proxy config is set.
"""
return bool(self.http or self.https)
class SSHDebugConnection(BaseModel):
"""SSH connection information for debug workflow.
Attributes:
host: The SSH relay server host IP address inside the VPN.
port: The SSH relay server port.
rsa_fingerprint: The host SSH server public RSA key fingerprint.
ed25519_fingerprint: The host SSH server public ed25519 key fingerprint.
use_runner_http_proxy: Whether to use runner proxy for the SSH connection.
local_proxy_host: Local host to use for proxying.
local_proxy_port: Local port to use for proxying.
"""
host: IPvAnyAddress
port: int = Field(0, gt=0, le=65535)
rsa_fingerprint: str = Field(pattern="^SHA256:.*")
ed25519_fingerprint: str = Field(pattern="^SHA256:.*")
use_runner_http_proxy: bool = False
local_proxy_host: str = "127.0.0.1"
local_proxy_port: int = 3129
class NonReactiveConfiguration(BaseModel):
"""Configuration for non-reactive mode.
Attributes:
combinations: Different combinations of flavor and image to spawn in non-reactive mode.
"""
combinations: "list[NonReactiveCombination]"
class NonReactiveCombination(BaseModel):
"""Combination of image and flavor that the application can spawn in non-reactive mode.
Attributes:
image: Information about the image to spawn.
flavor: Information about the flavor to spawn.
base_virtual_machines: Number of instances to spawn for this combination.
"""
image: "Image"
flavor: "Flavor"
base_virtual_machines: int
class ReactiveConfiguration(BaseModel):
"""Configuration for reactive mode.
Attributes:
queue: Queue to listen for reactive requests to spawn runners.
max_total_virtual_machines: Maximum number of instances to spawn by the application.
This value will be only checked in reactive mode, and will include all the instances
(reactive and non-reactive) spawned by the application.
images: List of valid images to spawn in reactive mode.
flavors: List of valid flavors to spawn in reactive mode.
"""
queue: "QueueConfig"
max_total_virtual_machines: int
images: "list[Image]"
flavors: "list[Flavor]"
class QueueConfig(BaseModel):
"""The configuration for the message queue.
Attributes:
mongodb_uri: The URI of the MongoDB database.
queue_name: The name of the queue.
"""
mongodb_uri: MongoDsn
queue_name: str
class Image(BaseModel):
"""Information for an image with its associated labels.
Attributes:
name: Image name or id.
labels: List of labels associated to the image.
"""
name: str
labels: list[str]
class Flavor(BaseModel):
"""Information for a flavor with its associated labels.
Attributes:
name: Flavor name of id.
labels: List of labels associated to the flavor.
"""
name: str
labels: list[str]
# For pydantic to work with forward references.
ApplicationConfiguration.update_forward_refs()
SupportServiceConfig.update_forward_refs()
NonReactiveConfiguration.update_forward_refs()
NonReactiveCombination.update_forward_refs()
ReactiveConfiguration.update_forward_refs()