Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: yq #77

Merged
merged 10 commits into from
Jul 4, 2023
Merged
7 changes: 2 additions & 5 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import shutil
import urllib.error
from pathlib import Path
from typing import TYPE_CHECKING, Callable, Dict, Optional, TypeVar
from typing import Any, Callable, Dict, Optional, TypeVar

import jinja2
from ops.charm import (
Expand All @@ -36,9 +36,6 @@
from runner_type import GitHubOrg, GitHubRepo, ProxySetting, VirtualMachineResources
from utilities import bytes_with_unit_to_kib, execute_command, get_env_var, retry

if TYPE_CHECKING:
from ops.model import JsonObject # type: ignore

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -546,7 +543,7 @@ def _on_stop(self, _: StopEvent) -> None:
# Log but ignore error since we're stopping anyway.
logger.exception("Failed to clear runners")

def _reconcile_runners(self, runner_manager: RunnerManager) -> Dict[str, "JsonObject"]:
def _reconcile_runners(self, runner_manager: RunnerManager) -> Dict[str, Any]:
"""Reconcile the current runners state and intended runner state.

Args:
Expand Down
41 changes: 32 additions & 9 deletions src/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import pathlib
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable, Optional, Sequence

Expand All @@ -40,6 +41,19 @@
LXD_PROFILE_YAML = LXD_PROFILE_YAML.parent / "lxd-profile.yml"


@dataclass
class WgetExecutable:
"""The executable to be installed through wget.

Args:
url: The URL of the executable binary.
cmd: Executable command name. E.g. yq_linux_amd64 -> yq
"""

url: str
cmd: str


class Runner:
"""Single instance of GitHub self-hosted runner.

Expand All @@ -53,7 +67,7 @@ class Runner:
busy (bool): Whether GitHub marks this runner as busy.
"""

runner_application = Path("/opt/github-runner")
runner_application = Path("/home/ubuntu/github-runner")
env_file = runner_application / ".env"
config_script = runner_application / "config.sh"
runner_script = runner_application / "start.sh"
Expand Down Expand Up @@ -373,8 +387,15 @@ def _install_binary(self, binary: Path) -> None:
# TEMP: Install common tools used in GitHub Actions. This will be removed once virtual
# machines are created from custom images/GitHub runner image.

self._apt_install(["docker.io", "npm", "python3-pip", "shellcheck", "jq"])
self._snap_install(["yq"])
self._apt_install(["docker.io", "npm", "python3-pip", "shellcheck", "jq", "wget"])
self._wget_install(
[
WgetExecutable(
url="https://github.com/mikefarah/yq/releases/download/v4.34.1/yq_linux_amd64",
cmd="yq",
)
]
)

# Add the user to docker group.
self.instance.execute(["/usr/sbin/usermod", "-aG", "docker", "ubuntu"])
Expand Down Expand Up @@ -599,18 +620,20 @@ def _apt_install(self, packages: Iterable[str]) -> None:
logger.info("Installing %s via APT...", pkg)
self.instance.execute(["/usr/bin/apt-get", "install", "-yq", pkg])

def _snap_install(self, packages: Iterable[str]) -> None:
"""Installs the given snap packages.
def _wget_install(self, executables: Iterable[WgetExecutable]) -> None:
"""Installs the given binaries.

This is a temporary solution to provide tools not offered by the base ubuntu image. Custom
images based on the GitHub action runner image will be used in the future.

Args:
packages: Packages to be install via snap.
executables: The executables to download.
"""
if self.instance is None:
raise RunnerError("Runner operation called prior to runner creation.")

for pkg in packages:
logger.info("Installing %s via snap...", pkg)
self.instance.execute(["/usr/bin/snap", "install", pkg])
for executable in executables:
executable_path = f"/usr/bin/{executable.cmd}"
logger.info("Downloading %s via wget to %s...", executable.url, executable_path)
self.instance.execute(["/usr/bin/wget", executable.url, "-O", executable_path])
self.instance.execute(["/usr/bin/chmod", "+x", executable_path])
2 changes: 1 addition & 1 deletion tests/unit/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_create(

if runner.config.proxies:
instance = instances[0]
env_proxy = instance.files.read_file("/opt/github-runner/.env")
env_proxy = instance.files.read_file("/home/ubuntu/github-runner/.env")
yanksyoon marked this conversation as resolved.
Show resolved Hide resolved
systemd_docker_proxy = instance.files.read_file(
"/etc/systemd/system/docker.service.d/http-proxy.conf"
)
Expand Down