From 32c1586a765a9ea23781b234c096a97167db7d96 Mon Sep 17 00:00:00 2001 From: Paul Larson Date: Thu, 2 Nov 2023 14:58:19 -0500 Subject: [PATCH] Update testflinger agent charm to work with monorepo --- .../testflinger-agent-charm/config.yaml | 18 +--- .../testflinger-agent-charm/src/charm.py | 94 +++++++++---------- .../templates/testflinger-agent.service.j2 | 2 +- 3 files changed, 51 insertions(+), 63 deletions(-) diff --git a/agent/charms/testflinger-agent-charm/config.yaml b/agent/charms/testflinger-agent-charm/config.yaml index c73126c2..557ff74b 100644 --- a/agent/charms/testflinger-agent-charm/config.yaml +++ b/agent/charms/testflinger-agent-charm/config.yaml @@ -1,18 +1,10 @@ options: - testflinger-agent-repo: + testflinger-repo: type: string - description: git repo for testflinger-agent - default: "https://github.com/canonical/testflinger-agent" - testflinger-agent-branch: + description: git repo for testflinger + default: "https://github.com/canonical/testflinger" + testflinger-branch: type: string - description: git branch for testflinger-agent - default: "main" - device-agent-repo: - type: string - description: git repo for device-agent - default: "https://github.com/canonical/snappy-device-agents" - device-agent-branch: - type: string - description: git branch for device-agent + description: git branch for testflinger default: "main" diff --git a/agent/charms/testflinger-agent-charm/src/charm.py b/agent/charms/testflinger-agent-charm/src/charm.py index 51eb79fd..3ca0759b 100755 --- a/agent/charms/testflinger-agent-charm/src/charm.py +++ b/agent/charms/testflinger-agent-charm/src/charm.py @@ -47,10 +47,8 @@ def __init__(self, *args): self.framework.observe(self.on.remove, self.on_remove) self.framework.observe(self.on.update_action, self.on_update_action) self._stored.set_default( - testflinger_agent_repo="", - testflinger_agent_branch="", - device_agent_repo="", - device_agent_branch="", + testflinger_repo="", + testflinger_branch="", unit_path=( f"/etc/systemd/system/testflinger-agent-{self.app.name}" ".service" @@ -127,57 +125,57 @@ def check_update_repos_needed(self): an update to the git repos """ update_needed = False - repo = self.config.get("testflinger-agent-repo") - if repo != self._stored.testflinger_agent_repo: - self._stored.testflinger_agent_repo = repo + repo = self.config.get("testflinger-repo") + if repo != self._stored.testflinger_repo: + self._stored.testflinger_repo = repo update_needed = True - branch = self.config.get("testflinger-agent-branch") - if branch != self._stored.testflinger_agent_branch: - self._stored.testflinger_agent_branch = branch - update_needed = True - repo = self.config.get("device-agent-repo") - if repo != self._stored.device_agent_repo: - self._stored.device_agent_repo = repo - update_needed = True - branch = self.config.get("device-agent-branch") - if branch != self._stored.device_agent_branch: - self._stored.device_agent_branch = branch + branch = self.config.get("testflinger-branch") + if branch != self._stored.testflinger_branch: + self._stored.testflinger_branch = branch update_needed = True if update_needed: self.update_repos() def update_repos(self): """Recreate the git repos and reinstall everything needed""" - tf_agent_dir = f"{self._stored.agent_path}/testflinger-agent" - device_agent_dir = f"{self._stored.agent_path}/snappy-device-agents" - shutil.rmtree(tf_agent_dir, ignore_errors=True) - shutil.rmtree(device_agent_dir, ignore_errors=True) + self.cleanup_agent_dirs() + repo_path = f"{self._stored.agent_path}/testflinger" Repo.clone_from( - self._stored.testflinger_agent_repo, - tf_agent_dir, - multi_options=[f"-b {self._stored.testflinger_agent_branch}"], - ) - self.run_with_logged_errors( - [ - f"{self._stored.venv_path}/bin/pip3", - "install", - "-I", - tf_agent_dir, - ] + url=self._stored.testflinger_repo, + to_path=repo_path, + no_checkout=True, + depth=1, ) - Repo.clone_from( - self._stored.device_agent_repo, - device_agent_dir, - multi_options=[f"-b {self._stored.device_agent_branch}"], + repo = Repo(repo_path) + # do a sparse checkout of only agent and device-connectors + repo.git.checkout( + f"origin/{self._stored.testflinger_branch}", + "--", + "agent", + "device-connectors", ) - self.run_with_logged_errors( - [ - f"{self._stored.venv_path}/bin/pip3", - "install", - "-I", - device_agent_dir, - ] + # Install the agent and device-connectors + for dir in ("agent", "device-connectors"): + self.run_with_logged_errors( + [ + f"{self._stored.venv_path}/bin/pip3", + "install", + "-I", + f"{repo_path}/{dir}", + ] + ) + + def cleanup_agent_dirs(self): + """Remove old agent dirs before checking out again""" + dirs_to_remove = ( + "testflinger", + "testflinger-agent", + "snappy-device-agents", ) + for dir in dirs_to_remove: + shutil.rmtree( + f"{self._stored.agent_path}/{dir}", ignore_errors=True + ) def signal_restart_agent(self): """Signal testflinger-agent to restart when it's not busy""" @@ -192,14 +190,11 @@ def signal_restart_agent(self): def write_config_files(self): """Overwrite the config files if they were changed""" tf_agent_config_path = ( - f"{self._stored.agent_path}/testflinger-agent/" - "testflinger-agent.conf" + f"{self._stored.agent_path}/testflinger-agent.conf" ) tf_agent_config = self.read_resource("testflinger_agent_configfile") self.write_file(tf_agent_config_path, tf_agent_config) - device_config_path = ( - f"{self._stored.agent_path}/" "snappy-device-agents/default.yaml" - ) + device_config_path = f"{self._stored.agent_path}/default.yaml" device_config = self.read_resource("device_configfile") self.write_file(device_config_path, device_config) @@ -233,6 +228,7 @@ def on_config_changed(self, _): self.unit.status = MaintenanceStatus("Handling config_changed hook") self.check_update_repos_needed() self.write_config_files() + self.render_systemd_unit() self.signal_restart_agent() self.unit.status = ActiveStatus() diff --git a/agent/charms/testflinger-agent-charm/templates/testflinger-agent.service.j2 b/agent/charms/testflinger-agent-charm/templates/testflinger-agent.service.j2 index 2d6e69e6..8598f1a0 100644 --- a/agent/charms/testflinger-agent-charm/templates/testflinger-agent.service.j2 +++ b/agent/charms/testflinger-agent-charm/templates/testflinger-agent.service.j2 @@ -6,7 +6,7 @@ After=network.target User=ubuntu Group=ubuntu WorkingDirectory={{ project_root }} -ExecStart=/bin/sh -c ". env/bin/activate && PYTHONIOENCODING=utf-8 testflinger-agent -c testflinger-agent/testflinger-agent.conf" +ExecStart=/bin/sh -c ". env/bin/activate && PYTHONIOENCODING=utf-8 testflinger-agent -c testflinger-agent.conf" Restart=always [Install]