Skip to content

Commit

Permalink
Improvements to list method on aws and gcp (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmet authored Apr 22, 2024
1 parent a8d6d33 commit 98968a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
19 changes: 11 additions & 8 deletions runner_manager/backend/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ def delete(self, runner: Runner):
raise e
return super().delete(runner)

def _get_instance_name(self, instance: InstanceTypeDef) -> str:
"""Get the instance name."""
name = ""
def _get_tag_value(
self, instance: InstanceTypeDef, key: str, default: str = ""
) -> str:
"""Get the tag value."""
for tag in instance.get("Tags", []):
if tag.get("Key") == "Name":
name = tag.get("Value", "")
return name
if tag.get("Key") == key:
return tag.get("Value", default)
return default

def list(self) -> List[Runner]:
"""List runners."""
Expand All @@ -81,7 +82,7 @@ def list(self) -> List[Runner]:
for reservation in reservations:
for instance in reservation.get("Instances", []):
instance_id = instance.get("InstanceId", "")
name = self._get_instance_name(instance)
name = self._get_tag_value(instance, "Name", instance_id)
try:
runner = Runner.find(
Runner.instance_id == instance_id,
Expand All @@ -91,8 +92,10 @@ def list(self) -> List[Runner]:
name=name,
instance_id=instance_id,
runner_group_name=self.runner_group,
busy=False,
busy=bool(self._get_tag_value(instance, "busy")),
status=self._get_tag_value(instance, "status", "online"),
created_at=instance.get("LaunchTime"),
started_at=instance.get("LaunchTime"),
)
runners.append(runner)
return runners
Expand Down
22 changes: 15 additions & 7 deletions runner_manager/backend/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ZoneOperationsClient,
)
from pydantic import Field
from redis_om import NotFoundError

from runner_manager.backend.base import BaseBackend
from runner_manager.models.backend import Backends, GCPConfig, GCPInstanceConfig
Expand Down Expand Up @@ -256,13 +257,20 @@ def list(self) -> List[Runner]:
manager = instance.labels.get("manager", "")
runner_group = instance.labels.get("runner_group", "")
if manager == self.manager and runner_group == self.runner_group:
runner: Runner = Runner(
name=instance.name,
instance_id=instance.name,
runner_group_name=self.runner_group,
busy=bool(instance.labels.get("busy", False)),
created_at=instance.creation_timestamp,
)
try:
runner = Runner.find(
Runner.name == instance.name,
).first()
except NotFoundError:
runner: Runner = Runner(
name=instance.name,
instance_id=instance.name,
runner_group_name=self.runner_group,
busy=bool(instance.labels.get("busy", False)),
status=instance.labels.get("status", "online"),
created_at=instance.creation_timestamp,
started_at=instance.creation_timestamp,
)
runners.append(runner)

except Exception as e:
Expand Down

0 comments on commit 98968a4

Please sign in to comment.