Merge pull request #1755 from ogayot/copy-logs-fix

Make sure journalctl -b is properly captured on shutdown
This commit is contained in:
Olivier Gayot 2023-08-04 09:07:02 +02:00 committed by GitHub
commit 2fdb0bebbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 6 deletions

View File

@ -120,7 +120,10 @@ class ShutdownController(SubiquityController):
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")

View File

@ -70,12 +70,17 @@ class LoggedCommandRunner:
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
@ -128,10 +133,17 @@ class DryRunCommandRunner(LoggedCommandRunner):
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

View File

@ -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
@ -103,6 +104,15 @@ class TestLoggedCommandRunner(SubiTestCase):
]
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):