-
Notifications
You must be signed in to change notification settings - Fork 23
Feat/app integration #530
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
Merged
Merged
Feat/app integration #530
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f41f607
Enable github-runner-manager application in charm
yhaliaw 2483a95
Rename a function
yhaliaw 8bb2693
Merge branch 'main' into feat/app-integration
yhaliaw 596eba9
Add tests
yhaliaw 2012d77
Merge branch 'main' into feat/app-integration
yhaliaw 8278a7d
Fix formatting issue from merging
yhaliaw 1a11676
Add debug message
yhaliaw 9f362e2
Fix integraiton test
yhaliaw a68e1a4
Move the start of the service to start of config change
yhaliaw 7bad650
Update code ordering
yhaliaw e3dcfb9
Merge branch 'main' into feat/app-integration
yhaliaw 57ad4c7
Remove previous version of python packages
yhaliaw f79c933
Update according to comments
yhaliaw 869def7
Merge branch 'main' into feat/app-integration
yhaliaw f2f2f71
Fix indentation
yhaliaw 8c46fd4
Use pipx over pip
yhaliaw 4eb1a04
Test pipx command
yhaliaw 771766d
Revert "Test pipx command"
yhaliaw cee1e7b
Revert "Use pipx over pip"
yhaliaw 28c5a61
Add comment on pipx
yhaliaw 5775da1
Remove the re-raise for service error for now
yhaliaw a78b9aa
Merge branch 'main' into feat/app-integration
yhaliaw f3955f7
Format the files
yhaliaw 08acf8b
Fix lint
yhaliaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# Copyright 2025 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""Manage the service of github-runner-manager.""" | ||
|
||
import json | ||
import logging | ||
import textwrap | ||
from pathlib import Path | ||
|
||
from charms.operator_libs_linux.v1 import systemd | ||
from charms.operator_libs_linux.v1.systemd import SystemdError | ||
from github_runner_manager import constants | ||
from github_runner_manager.configuration.base import ApplicationConfiguration | ||
from yaml import safe_dump as yaml_safe_dump | ||
|
||
from charm_state import CharmState | ||
from errors import ( | ||
RunnerManagerApplicationInstallError, | ||
RunnerManagerApplicationStartError, | ||
SubprocessError, | ||
) | ||
from factories import create_application_configuration | ||
from utilities import execute_command | ||
|
||
GITHUB_RUNNER_MANAGER_ADDRESS = "127.0.0.1" | ||
GITHUB_RUNNER_MANAGER_PORT = "55555" | ||
SYSTEMD_SERVICE_PATH = Path("/etc/systemd/system") | ||
GITHUB_RUNNER_MANAGER_SYSTEMD_SERVICE = "github-runner-manager.service" | ||
GITHUB_RUNNER_MANAGER_SYSTEMD_SERVICE_PATH = ( | ||
SYSTEMD_SERVICE_PATH / GITHUB_RUNNER_MANAGER_SYSTEMD_SERVICE | ||
) | ||
GITHUB_RUNNER_MANAGER_PACKAGE = "github_runner_manager" | ||
JOB_MANAGER_PACKAGE = "jobmanager_client" | ||
GITHUB_RUNNER_MANAGER_PACKAGE_PATH = "./github-runner-manager" | ||
JOB_MANAGER_PACKAGE_PATH = "./jobmanager/client" | ||
GITHUB_RUNNER_MANAGER_SERVICE_NAME = "github-runner-manager" | ||
|
||
_INSTALL_ERROR_MESSAGE = "Unable to install github-runner-manager package from source" | ||
_SERVICE_SETUP_ERROR_MESSAGE = "Unable to enable or start the github-runner-manager application" | ||
_SERVICE_STOP_ERROR_MESSAGE = "Unable to stop the github-runner-manager application" | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup(state: CharmState, app_name: str, unit_name: str) -> None: | ||
"""Set up the github-runner-manager service. | ||
|
||
Args: | ||
state: The state of the charm. | ||
app_name: The Juju application name. | ||
unit_name: The Juju unit. | ||
""" | ||
config = create_application_configuration(state, app_name, unit_name) | ||
config_file = _setup_config_file(config) | ||
_setup_service_file(config_file) | ||
_enable_service() | ||
|
||
|
||
# TODO: Use pipx over pip once the version that supports `pipx install --global` lands on apt. | ||
yhaliaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def install_package() -> None: | ||
"""Install the GitHub runner manager package. | ||
|
||
Raises: | ||
RunnerManagerApplicationInstallError: Unable to install the application. | ||
""" | ||
try: | ||
if systemd.service_running(GITHUB_RUNNER_MANAGER_SERVICE_NAME): | ||
systemd.service_stop(GITHUB_RUNNER_MANAGER_SERVICE_NAME) | ||
except SystemdError as err: | ||
raise RunnerManagerApplicationInstallError(_SERVICE_STOP_ERROR_MESSAGE) from err | ||
cbartz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
logger.info("Upgrading pip") | ||
try: | ||
execute_command(["python3", "-m", "pip", "install", "--upgrade", "pip"]) | ||
except SubprocessError as err: | ||
raise RunnerManagerApplicationInstallError(_INSTALL_ERROR_MESSAGE) from err | ||
|
||
logger.info("Uninstalling previous version of packages") | ||
try: | ||
execute_command(["python3", "-m", "pip", "uninstall", GITHUB_RUNNER_MANAGER_PACKAGE]) | ||
yhaliaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
execute_command(["python3", "-m", "pip", "uninstall", JOB_MANAGER_PACKAGE]) | ||
except SubprocessError: | ||
logger.info( | ||
"Unable to uninstall existing packages, likely due to previous version not installed" | ||
) | ||
|
||
try: | ||
# Use `--prefix` to install the package in a location (/usr) all user can use and | ||
# `--ignore-installed` to force all dependencies be to installed under /usr. | ||
execute_command( | ||
[ | ||
"python3", | ||
"-m", | ||
"pip", | ||
"install", | ||
"--prefix", | ||
"/usr", | ||
"--ignore-installed", | ||
GITHUB_RUNNER_MANAGER_PACKAGE_PATH, | ||
JOB_MANAGER_PACKAGE_PATH, | ||
] | ||
) | ||
except SubprocessError as err: | ||
raise RunnerManagerApplicationInstallError(_INSTALL_ERROR_MESSAGE) from err | ||
|
||
|
||
def _enable_service() -> None: | ||
"""Enable the github runner manager service. | ||
|
||
Raises: | ||
RunnerManagerApplicationStartError: Unable to startup the service. | ||
""" | ||
try: | ||
systemd.service_enable(GITHUB_RUNNER_MANAGER_SERVICE_NAME) | ||
if not systemd.service_running(GITHUB_RUNNER_MANAGER_SERVICE_NAME): | ||
systemd.service_start(GITHUB_RUNNER_MANAGER_SERVICE_NAME) | ||
except SystemdError as err: | ||
raise RunnerManagerApplicationStartError(_SERVICE_SETUP_ERROR_MESSAGE) from err | ||
|
||
|
||
def _setup_config_file(config: ApplicationConfiguration) -> Path: | ||
"""Write the configuration to file. | ||
|
||
Args: | ||
config: The application configuration. | ||
""" | ||
# Directly converting to `dict` will have the value be Python objects rather than string | ||
# representations. The values needs to be string representations to be converted to YAML file. | ||
# No easy way to directly convert to YAML file, so json module is used. | ||
config_dict = json.loads(config.json()) | ||
path = Path(f"~{constants.RUNNER_MANAGER_USER}").expanduser() / "config.yaml" | ||
with open(path, "w+", encoding="utf-8") as file: | ||
yaml_safe_dump(config_dict, file) | ||
return path | ||
|
||
|
||
def _setup_service_file(config_file: Path) -> None: | ||
"""Configure the systemd service. | ||
|
||
Args: | ||
config_file: The configuration file for the service. | ||
""" | ||
service_file_content = textwrap.dedent( | ||
f"""\ | ||
[Unit] | ||
Description=Runs the github-runner-manager service | ||
|
||
[Service] | ||
Type=simple | ||
User={constants.RUNNER_MANAGER_USER} | ||
Group={constants.RUNNER_MANAGER_GROUP} | ||
ExecStart=github-runner-manager --config-file {str(config_file)} --host \ | ||
{GITHUB_RUNNER_MANAGER_ADDRESS} --port {GITHUB_RUNNER_MANAGER_PORT} | ||
Restart=on-failure | ||
|
||
[Install] | ||
WantedBy=multi-user.target | ||
""" | ||
) | ||
GITHUB_RUNNER_MANAGER_SYSTEMD_SERVICE_PATH.write_text(service_file_content, "utf-8") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.