Skip to content

Commit 0b4e7fa

Browse files
authored
Fixup runner-manager config and setup (#358)
Couple of issue encountered while developing with the latest version of the runner-manager.
1 parent c8591b1 commit 0b4e7fa

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

runner_manager/backend/docker.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from importlib.resources import files
23
from pathlib import Path
34
from typing import Dict, List, Literal
@@ -12,6 +13,8 @@
1213
from runner_manager.models.backend import Backends, DockerConfig, DockerInstanceConfig
1314
from runner_manager.models.runner import Runner
1415

16+
log = logging.getLogger(__name__)
17+
1518

1619
class DockerBackend(BaseBackend):
1720
name: Literal[Backends.docker] = Field(default=Backends.docker)
@@ -52,14 +55,16 @@ def _labels(self, runner: Runner) -> Dict[str, str | None]:
5255

5356
def _environment(self, runner: Runner) -> Dict[str, str | None]:
5457
"""Return environment variables for the container."""
55-
environment: Dict[str, str | None] = {
56-
"RUNNER_NAME": runner.name,
57-
"RUNNER_LABELS": ",".join([label.name for label in runner.labels]),
58-
"RUNNER_TOKEN": runner.token,
59-
"RUNNER_ORG": runner.organization,
60-
"RUNNER_GROUP": runner.runner_group_name,
61-
}
62-
environment.update(self.instance_config.environment)
58+
environment: Dict[str, str | None] = self.instance_config.environment
59+
environment.update(
60+
{
61+
"RUNNER_NAME": runner.name,
62+
"RUNNER_LABELS": ", ".join([label.name for label in runner.labels]),
63+
"RUNNER_TOKEN": runner.token,
64+
"RUNNER_ORG": runner.organization,
65+
"RUNNER_GROUP": runner.runner_group_name,
66+
}
67+
)
6368
return environment
6469

6570
def create(self, runner: Runner):
@@ -68,6 +73,7 @@ def create(self, runner: Runner):
6873

6974
labels = self._labels(runner)
7075
environment = self._environment(runner)
76+
log.info(f"Creating container for runner {runner.name}, labels: {labels}")
7177
container: Container = self.client.containers.run(
7278
self.instance_config.image,
7379
command=self.instance_config.command,

runner_manager/jobs/startup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""Jobs to run on startup."""
22

3+
import logging
34
from typing import List
45

56
from redis_om import Migrator
67

78
from runner_manager.clients.github import GitHub
89
from runner_manager.dependencies import get_github, get_settings
9-
from runner_manager.logging import log
1010
from runner_manager.models.runner_group import RunnerGroup
1111
from runner_manager.models.settings import Settings
1212

13+
log = logging.getLogger(__name__)
14+
1315

1416
def create_runner_groups(settings: Settings, github: GitHub):
1517
runner_groups_configs = settings.runner_groups
@@ -25,6 +27,7 @@ def create_runner_groups(settings: Settings, github: GitHub):
2527
existing_groups.remove(runner_group)
2628
runner_group.update(**runner_group_config.dict())
2729
for runner_group in existing_groups:
30+
log.info(f"Deleting runner group {runner_group.name}")
2831
runner_group.delete(pk=runner_group.pk, github=github)
2932

3033

runner_manager/models/runner_group.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import logging
12
from datetime import datetime
23
from typing import Any, List, Optional, Self, Union
34
from uuid import uuid4
45

56
import redis
67
from githubkit import Response
8+
from githubkit.exception import RequestFailed
79
from githubkit.rest.models import AuthenticationToken
810
from githubkit.rest.models import Runner as GitHubRunner
911
from githubkit.webhooks.models import WorkflowJobInProgress
@@ -18,10 +20,11 @@
1820
from runner_manager.backend.gcloud import GCPBackend
1921
from runner_manager.clients.github import GitHub
2022
from runner_manager.clients.github import RunnerGroup as GitHubRunnerGroup
21-
from runner_manager.logging import log
2223
from runner_manager.models.base import BaseModel
2324
from runner_manager.models.runner import Runner, RunnerLabel, RunnerStatus
2425

26+
log = logging.getLogger(__name__)
27+
2528

2629
class BaseRunnerGroup(PydanticBaseModel):
2730
name: str
@@ -51,8 +54,8 @@ class RunnerGroup(BaseModel, BaseRunnerGroup):
5154
name: str = Field(index=True, full_text_search=True)
5255
organization: str = Field(index=True, full_text_search=True)
5356
repository: Optional[str] = Field(index=True, full_text_search=True)
54-
max: Optional[int] = Field(index=True, ge=1, default=20)
55-
min: Optional[int] = Field(index=True, ge=0, default=0)
57+
max: int = Field(index=True, ge=1, default=20)
58+
min: int = Field(index=True, ge=0, default=0)
5659
labels: List[str] = Field(index=True)
5760

5861
def __post_init_post_parse__(self):
@@ -104,9 +107,10 @@ def create_runner(self, token: AuthenticationToken) -> Runner | None:
104107
Runner: Runner instance.
105108
"""
106109
count = len(self.get_runners())
107-
if count < (self.max or 0):
110+
if count < self.max:
108111
runner: Runner = Runner(
109112
name=self.generate_runner_name(),
113+
organization=self.organization,
110114
status=RunnerStatus.offline,
111115
token=token.token,
112116
busy=False,
@@ -116,22 +120,9 @@ def create_runner(self, token: AuthenticationToken) -> Runner | None:
116120
labels=self.runner_labels,
117121
manager=self.manager,
118122
)
119-
runner.save()
123+
runner = runner.save()
120124
return self.backend.create(runner)
121125
return None
122-
runner: Runner = Runner(
123-
name=self.generate_runner_name(),
124-
status=RunnerStatus.offline,
125-
token=token.token,
126-
busy=False,
127-
runner_group_id=self.id,
128-
runner_group_name=self.name,
129-
labels=self.runner_labels,
130-
manager=self.manager,
131-
organization=self.organization,
132-
)
133-
runner.save()
134-
return self.backend.create(runner)
135126

136127
def update_runner(self: Self, webhook: WorkflowJobInProgress) -> Runner:
137128
"""Update a runner instance.
@@ -192,6 +183,9 @@ def save(
192183
Returns:
193184
RunnerGroup: Runner group instance.
194185
"""
186+
# add label "self-hosted" to the list of labels
187+
if "self-hosted" not in self.labels:
188+
self.labels.append("self-hosted")
195189
if github:
196190
github_group: GitHubRunnerGroup = self.create_github_group(github)
197191
self.id = github_group.id
@@ -279,6 +273,24 @@ def find_from_base(cls, basegroup: "BaseRunnerGroup") -> "RunnerGroup":
279273
group = None
280274
return group
281275

276+
def delete_github_group(self, github: GitHub) -> Response[GitHubRunnerGroup] | None:
277+
"""Delete a GitHub runner group."""
278+
if self.id:
279+
# check if runner group exists
280+
try:
281+
github.rest.actions.get_self_hosted_runner_group_for_org(
282+
org=self.organization,
283+
runner_group_id=self.id,
284+
)
285+
except RequestFailed:
286+
log.info("Runner group does not exist")
287+
return None
288+
return github.rest.actions.delete_self_hosted_runner_group_from_org(
289+
org=self.organization, runner_group_id=self.id
290+
)
291+
292+
return None
293+
282294
@classmethod
283295
def delete(
284296
cls,
@@ -303,10 +315,8 @@ def delete(
303315
runners: List[Runner] = group.get_runners()
304316
for runner in runners:
305317
group.delete_runner(runner)
306-
if github and group.id:
307-
github.rest.actions.delete_self_hosted_runner_group_from_org(
308-
org=group.organization, runner_group_id=group.id
309-
)
318+
if github:
319+
group.delete_github_group(github)
310320
db = cls._get_db(pipeline)
311321

312322
return cls._delete(db, cls.make_primary_key(pk))

0 commit comments

Comments
 (0)