Skip to content

Commit

Permalink
Use python instead of awk
Browse files Browse the repository at this point in the history
  • Loading branch information
cbartz committed Nov 16, 2023
1 parent 1b665cd commit d324f21
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src-docs/runner.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Single instance of GitHub self-hosted runner.
- <b>`online`</b> (bool): Whether GitHub marks this runner as online.
- <b>`busy`</b> (bool): Whether GitHub marks this runner as busy.

<a href="../src/runner.py#L84"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L85"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand Down Expand Up @@ -56,7 +56,7 @@ Construct the runner instance.

---

<a href="../src/runner.py#L112"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L113"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `create`

Expand Down Expand Up @@ -88,7 +88,7 @@ Create the runner instance on LXD and register it on GitHub.

---

<a href="../src/runner.py#L155"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/runner.py#L156"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `remove`

Expand Down
4 changes: 4 additions & 0 deletions src/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class RunnerBinaryError(RunnerError):
"""Error of getting runner binary."""


class RunnerAproxyError(RunnerError):
"""Error for setting up aproxy."""


class MissingRunnerBinaryError(Exception):
"""Error for missing runner binary."""

Expand Down
36 changes: 26 additions & 10 deletions src/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from errors import (
CreateSharedFilesystemError,
LxdError,
RunnerAproxyError,
RunnerCreateError,
RunnerError,
RunnerFileLoadError,
Expand All @@ -46,7 +47,7 @@
LXD_PROFILE_YAML = pathlib.Path(__file__).parent.parent / "lxd-profile.yaml"
if not LXD_PROFILE_YAML.exists():
LXD_PROFILE_YAML = LXD_PROFILE_YAML.parent / "lxd-profile.yml"
LXDBR_DNSMASQ_LEASES_FILE = "/var/snap/lxd/common/lxd/networks/lxdbr0/dnsmasq.leases"
LXDBR_DNSMASQ_LEASES_FILE = Path("/var/snap/lxd/common/lxd/networks/lxdbr0/dnsmasq.leases")


@dataclass
Expand Down Expand Up @@ -553,6 +554,27 @@ def _should_render_templates_with_metrics(self) -> bool:
"""
return self._shared_fs is not None

def _get_default_ip(self) -> Optional[str]:
"""Get the default IP of the runner.
Returns:
The default IP of the runner or None if not found.
"""
if self.instance is None:
raise RunnerError("Runner operation called prior to runner creation.")

default_ip = None
# parse LXD dnsmasq leases file to get the default IP of the runner
# format: timestamp mac-address ip-address hostname client-id
lines = LXDBR_DNSMASQ_LEASES_FILE.read_text("utf-8").splitlines(keepends=False)
for line in lines:
columns = line.split()
if len(columns) >= 4 and columns[3] == self.instance.name:
default_ip = columns[2]
break

return default_ip

def _configure_aproxy(self, aproxy_address: str) -> None:
"""Configure aproxy.
Expand All @@ -569,15 +591,9 @@ def _configure_aproxy(self, aproxy_address: str) -> None:

self.instance.execute(["snap", "set", "aproxy", f"proxy={aproxy_address}"])

stdout, _ = execute_command(
[
"/usr/bin/awk",
f'$4 == "{self.instance.name}" {{ print $3 }}',
LXDBR_DNSMASQ_LEASES_FILE,
],
check_exit=True,
)
default_ip = stdout.strip()
default_ip = self._get_default_ip()
if not default_ip:
raise RunnerAproxyError("Unable to find default IP for aproxy configuration.")

nft_input = rf"""define default-ip = {default_ip}
define private-ips = {{ 10.0.0.0/8, 127.0.0.1/8, 172.16.0.0/12, 192.168.0.0/16 }}
Expand Down

0 comments on commit d324f21

Please sign in to comment.