Skip to content

Commit

Permalink
Update testflinger agent charm to work with monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
plars committed Nov 2, 2023
1 parent b8947e6 commit 32c1586
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 63 deletions.
18 changes: 5 additions & 13 deletions agent/charms/testflinger-agent-charm/config.yaml
Original file line number Diff line number Diff line change
@@ -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"

94 changes: 45 additions & 49 deletions agent/charms/testflinger-agent-charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"""
Expand All @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 32c1586

Please sign in to comment.