Skip to content

Commit

Permalink
Fix reboot loop
Browse files Browse the repository at this point in the history
  • Loading branch information
yhaliaw committed Jul 4, 2023
1 parent 7e95373 commit 57e9207
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
15 changes: 9 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ def _on_install(self, _event: InstallEvent) -> None:
"""
self.unit.status = MaintenanceStatus("Installing packages")

# Temporary solution: Upgrade the kernel due to a kernel bug in 5.15. Kernel upgrade
# not needed for container-based end-to-end tests.
if not LXD_PROFILE_YAML.exists():
self.unit.status = MaintenanceStatus("Upgrading kernel")
self._upgrade_kernel()

try:
# The `_start_services`, `_install_deps` includes retry.
self._install_deps()
Expand Down Expand Up @@ -301,11 +307,6 @@ def _on_install(self, _event: InstallEvent) -> None:
self.unit.status = MaintenanceStatus(f"Failed to update runner binary: {err}")
return

# Temporary solution: Upgrade the kernel due to a kernel bug in 5.15. Kernel upgrade
# not needed for container-based end-to-end tests.
if not LXD_PROFILE_YAML.exists():
self._upgrade_kernel()

self.unit.status = MaintenanceStatus("Starting runners")
try:
self._reconcile_runners(runner_manager)
Expand All @@ -320,7 +321,9 @@ def _upgrade_kernel(self) -> None:
"""Upgrade the Linux kernel."""
execute_command(["/usr/bin/apt-get", "update"])
execute_command(["/usr/bin/apt-get", "install", "-qy", "linux-generic-hwe-22.04"])
execute_command(["reboot"])
_, exit_code = execute_command(["ls", "/var/run/reboot-required"], check_exit=False)
if exit_code == 0:
execute_command(["reboot"])

@catch_charm_errors
def _on_upgrade_charm(self, _event: UpgradeCharmEvent) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/firewall.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_host_ip(self) -> str:
Returns:
The host IP address.
"""
address = execute_command(
address, _ = execute_command(
["/snap/bin/lxc", "network", "get", self._network, "ipv4.address"]
)
return str(ipaddress.IPv4Interface(address.strip()).ip)
Expand Down
13 changes: 6 additions & 7 deletions src/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def secure_run_subprocess(cmd: Sequence[str], **kwargs) -> subprocess.CompletedP
return result


def execute_command(cmd: Sequence[str], check_exit: bool = True, **kwargs) -> str:
def execute_command(cmd: Sequence[str], check_exit: bool = True, **kwargs) -> tuple[str, int]:
"""Execute a command on a subprocess.
The command is executed with `subprocess.run`, additional arguments can be passed to it as
Expand All @@ -136,7 +136,7 @@ def execute_command(cmd: Sequence[str], check_exit: bool = True, **kwargs) -> st
kwargs: Additional keyword arguments for the `subprocess.run` call.
Returns:
Output on stdout.
Output on stdout, and the exit code.
"""
result = secure_run_subprocess(cmd, **kwargs)

Expand All @@ -153,11 +153,10 @@ def execute_command(cmd: Sequence[str], check_exit: bool = True, **kwargs) -> st

raise SubprocessError(cmd, err.returncode, err.stdout, err.stderr) from err

return (
result.stdout
if isinstance(result.stdout, str)
else result.stdout.decode(kwargs.get("encoding", "utf-8"))
)
if isinstance(result.stdout, str):
return (result.stdout, result.returncode)

return (result.stdout.decode(kwargs.get("encoding", "utf-8")), result.returncode)


def get_env_var(env_var: str) -> Optional[str]:
Expand Down

0 comments on commit 57e9207

Please sign in to comment.