-
Notifications
You must be signed in to change notification settings - Fork 24
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
Open
yhaliaw
wants to merge
12
commits into
main
Choose a base branch
from
feat/app-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+502
−123
Open
Feat/app integration #530
Changes from all commits
Commits
Show all changes
12 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 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,146 @@ | ||
# Copyright 2025 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""Manage the service of github-runner-manager.""" | ||
|
||
import json | ||
import logging | ||
from pathlib import Path | ||
|
||
import jinja2 | ||
from charms.operator_libs_linux.v1 import systemd | ||
from charms.operator_libs_linux.v1.systemd import SystemdError | ||
from github_runner_manager import constants | ||
from yaml import safe_dump as yaml_safe_dump | ||
|
||
from charm_state import CharmState | ||
from errors import ( | ||
RunnerManagerApplicationInstallError, | ||
RunnerManagerApplicationStartupError, | ||
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" | ||
|
||
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_file = _setup_config_file(state, app_name, unit_name) | ||
_setup_service_file(config_file) | ||
_enable_service() | ||
|
||
|
||
def install_package() -> None: | ||
"""Install the GitHub runner manager package. | ||
|
||
Raises: | ||
RunnerManagerApplicationInstallError: Unable to install the application. | ||
""" | ||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe keep in mind for adaptions later, I think having an abstraction to interact with pip (similar as in https://github.com/canonical/github-runner-image-builder-operator/blob/main/src/pipx.py ) would make it the code easier testable and less coupled. |
||
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: | ||
RunnerManagerApplicationStartupError: 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 RunnerManagerApplicationStartupError(_SERVICE_SETUP_ERROR_MESSAGE) from err | ||
|
||
|
||
def _setup_config_file(state: CharmState, app_name: str, unit_name: str) -> Path: | ||
"""Write the configuration to file. | ||
|
||
Args: | ||
state: The charm state. | ||
app_name: The Juju application name. | ||
unit_name: The Juju unit. | ||
""" | ||
config = create_application_configuration(state, app_name, unit_name) | ||
# 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. | ||
""" | ||
jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("templates"), autoescape=True) | ||
|
||
service_file_content = jinja.get_template("github-runner-manager.service.j2").render( | ||
user=constants.RUNNER_MANAGER_USER, | ||
group=constants.RUNNER_MANAGER_GROUP, | ||
config=str(config_file), | ||
host=GITHUB_RUNNER_MANAGER_ADDRESS, | ||
port=GITHUB_RUNNER_MANAGER_PORT, | ||
) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[Unit] | ||
Description=Runs the github-runner-manager service | ||
|
||
[Service] | ||
Type=simple | ||
User={{user}} | ||
Group={{group}} | ||
ExecStart=github-runner-manager --config-file {{config}} --host {{host}} --port {{port}} | ||
Restart=on-failure | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about upgrading? How is this handled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a local python package.
Installing it with code changes would update the installation.
The
_common_install_code
function is called on charm upgrade as well. So it should cover the charm upgrade event.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So due to the
--ignore-installed
flag it would just install without caring about a previous installation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the
install_package
method to uninstall the packages prior to installing them.