Skip to content

Commit

Permalink
Merge pull request #1755 from ogayot/copy-logs-fix
Browse files Browse the repository at this point in the history
Make sure journalctl -b is properly captured on shutdown
  • Loading branch information
ogayot authored Aug 4, 2023
2 parents 03e9a40 + 3424284 commit 2fdb0be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
5 changes: 4 additions & 1 deletion subiquity/server/controllers/shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ async def copy_logs_to_target(self, context):
try:
with open_perms(journal_txt) as output:
await self.app.command_runner.run(
["journalctl", "-b"], stdout=output, stderr=subprocess.STDOUT
["journalctl", "-b"],
capture=True,
stdout=output,
stderr=subprocess.STDOUT,
)
except Exception:
log.exception("saving journal failed")
Expand Down
20 changes: 16 additions & 4 deletions subiquity/server/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ def _forge_systemd_cmd(
return prefix + cmd

async def start(
self, cmd: List[str], *, private_mounts: bool = False, capture: bool = False
self,
cmd: List[str],
*,
private_mounts: bool = False,
capture: bool = False,
**astart_kwargs,
) -> asyncio.subprocess.Process:
forged: List[str] = self._forge_systemd_cmd(
cmd, private_mounts=private_mounts, capture=capture
)
proc = await astart_command(forged)
proc = await astart_command(forged, **astart_kwargs)
proc.args = forged
return proc

Expand Down Expand Up @@ -128,10 +133,17 @@ def _get_delay_for_cmd(self, cmd: List[str]) -> float:
return self.delay

async def start(
self, cmd: List[str], *, private_mounts: bool = False, capture: bool = False
self,
cmd: List[str],
*,
private_mounts: bool = False,
capture: bool = False,
**astart_kwargs,
) -> asyncio.subprocess.Process:
delay = self._get_delay_for_cmd(cmd)
proc = await super().start(cmd, private_mounts=private_mounts, capture=capture)
proc = await super().start(
cmd, private_mounts=private_mounts, capture=capture, **astart_kwargs
)
await asyncio.sleep(delay)
return proc

Expand Down
12 changes: 11 additions & 1 deletion subiquity/server/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
from unittest.mock import patch
import subprocess
from unittest.mock import ANY, patch

from subiquity.server.runner import DryRunCommandRunner, LoggedCommandRunner
from subiquitycore.tests import SubiTestCase
Expand Down Expand Up @@ -103,6 +104,15 @@ def test_forge_systemd_cmd(self):
]
self.assertEqual(cmd, expected)

async def test_start(self):
runner = LoggedCommandRunner(ident="my-id", use_systemd_user=False)

with patch("subiquity.server.runner.astart_command") as astart_mock:
await runner.start(["/bin/ls"], stdout=subprocess.PIPE)

expected_cmd = ANY
astart_mock.assert_called_once_with(expected_cmd, stdout=subprocess.PIPE)


class TestDryRunCommandRunner(SubiTestCase):
def setUp(self):
Expand Down

0 comments on commit 2fdb0be

Please sign in to comment.