runroot: Log different commands to different files

JIRA: COMPOSE-3980
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2019-11-28 15:12:05 +01:00
parent 7f35ac622a
commit 51b1144b70
2 changed files with 49 additions and 15 deletions

View File

@ -97,6 +97,9 @@ class Runroot(kobo.log.LoggingBase):
ssh_cmd = ["ssh", "-oBatchMode=yes", "-n", "-l", user, hostname, formatted_cmd] ssh_cmd = ["ssh", "-oBatchMode=yes", "-n", "-l", user, hostname, formatted_cmd]
return run(ssh_cmd, show_cmd=True, logfile=log_file)[1] return run(ssh_cmd, show_cmd=True, logfile=log_file)[1]
def _log_file(self, base, suffix):
return base.replace(".log", "." + suffix + ".log")
def _run_openssh(self, command, log_file=None, arch=None, packages=None, def _run_openssh(self, command, log_file=None, arch=None, packages=None,
chown_paths=None, **kwargs): chown_paths=None, **kwargs):
""" """
@ -128,7 +131,12 @@ class Runroot(kobo.log.LoggingBase):
if init_template: if init_template:
fmt_dict = {"runroot_tag": runroot_tag} fmt_dict = {"runroot_tag": runroot_tag}
runroot_key = self._ssh_run( runroot_key = self._ssh_run(
hostname, user, init_template, fmt_dict, log_file=log_file) hostname,
user,
init_template,
fmt_dict,
log_file=self._log_file(log_file, "init"),
)
runroot_key = runroot_key.rstrip("\n\r") runroot_key = runroot_key.rstrip("\n\r")
else: else:
runroot_key = None runroot_key = None
@ -139,7 +147,11 @@ class Runroot(kobo.log.LoggingBase):
if runroot_key: if runroot_key:
fmt_dict["runroot_key"] = runroot_key fmt_dict["runroot_key"] = runroot_key
self._ssh_run( self._ssh_run(
hostname, user, install_packages_template, fmt_dict, log_file=log_file hostname,
user,
install_packages_template,
fmt_dict,
log_file=self._log_file(log_file, "install_packages"),
) )
# Run the runroot task and get the buildroot RPMs. # Run the runroot task and get the buildroot RPMs.
@ -151,7 +163,11 @@ class Runroot(kobo.log.LoggingBase):
fmt_dict["command"] = "rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'" fmt_dict["command"] = "rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"
buildroot_rpms = self._ssh_run( buildroot_rpms = self._ssh_run(
hostname, user, run_template, fmt_dict, log_file=log_file hostname,
user,
run_template,
fmt_dict,
log_file=self._log_file(log_file, "rpms"),
) )
else: else:
self._ssh_run(hostname, user, command, log_file=log_file) self._ssh_run(hostname, user, command, log_file=log_file)
@ -159,7 +175,7 @@ class Runroot(kobo.log.LoggingBase):
hostname, hostname,
user, user,
"rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'", "rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'",
log_file=log_file, log_file=self._log_file(log_file, "rpms"),
) )
# Parse the buildroot_rpms and store it in self._result. # Parse the buildroot_rpms and store it in self._result.

View File

@ -29,13 +29,14 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
method = self.runroot.get_runroot_method() method = self.runroot.get_runroot_method()
self.assertEqual(method, "openssh") self.assertEqual(method, "openssh")
def _ssh_call(self, cmd): def _ssh_call(self, cmd, suffix=None):
""" """
Helper method returning default SSH mock.call with given command `cmd`. Helper method returning default SSH mock.call with given command `cmd`.
""" """
logfile = ("/foo/runroot." + suffix + ".log") if suffix else "/foo/runroot.log"
return mock.call( return mock.call(
['ssh', '-oBatchMode=yes', '-n', '-l', 'root', 'localhost', cmd], ['ssh', '-oBatchMode=yes', '-n', '-l', 'root', 'localhost', cmd],
logfile='/foo/runroot.log', logfile=logfile,
show_cmd=True, show_cmd=True,
) )
@ -45,7 +46,9 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64") self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64")
run.assert_has_calls([ run.assert_has_calls([
self._ssh_call('df -h'), self._ssh_call('df -h'),
self._ssh_call("rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"), self._ssh_call(
"rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'", suffix="rpms"
),
]) ])
@mock.patch("pungi.runroot.run") @mock.patch("pungi.runroot.run")
@ -69,10 +72,13 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64", self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64",
packages=["lorax", "automake"]) packages=["lorax", "automake"])
run.assert_has_calls([ run.assert_has_calls([
self._ssh_call('/usr/sbin/init_runroot f28-build'), self._ssh_call('/usr/sbin/init_runroot f28-build', suffix="init"),
self._ssh_call('install key lorax automake'), self._ssh_call('install key lorax automake', suffix="install_packages"),
self._ssh_call('run key df -h'), self._ssh_call('run key df -h'),
self._ssh_call("run key rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"), self._ssh_call(
"run key rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'",
suffix="rpms",
),
]) ])
@mock.patch("pungi.runroot.run") @mock.patch("pungi.runroot.run")
@ -85,9 +91,12 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64", self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64",
packages=["lorax", "automake"]) packages=["lorax", "automake"])
run.assert_has_calls([ run.assert_has_calls([
self._ssh_call('install lorax automake'), self._ssh_call('install lorax automake', suffix="install_packages"),
self._ssh_call('run df -h'), self._ssh_call('run df -h'),
self._ssh_call("run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"), self._ssh_call(
"run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'",
suffix="rpms",
),
]) ])
@mock.patch("pungi.runroot.run") @mock.patch("pungi.runroot.run")
@ -100,7 +109,10 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64") self.runroot.run("df -h", log_file="/foo/runroot.log", arch="x86_64")
run.assert_has_calls([ run.assert_has_calls([
self._ssh_call('run df -h'), self._ssh_call('run df -h'),
self._ssh_call("run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"), self._ssh_call(
"run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'",
suffix="rpms",
),
]) ])
@mock.patch("pungi.runroot.run") @mock.patch("pungi.runroot.run")
@ -112,7 +124,10 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
packages=["lorax", "automake"]) packages=["lorax", "automake"])
run.assert_has_calls([ run.assert_has_calls([
self._ssh_call('run df -h'), self._ssh_call('run df -h'),
self._ssh_call("run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"), self._ssh_call(
"run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'",
suffix="rpms",
),
]) ])
@mock.patch("pungi.runroot.run") @mock.patch("pungi.runroot.run")
@ -127,5 +142,8 @@ class TestRunrootOpenSSH(helpers.PungiTestCase):
self._ssh_call( self._ssh_call(
"run df -h && chmod -R a+r /mnt/foo/compose /mnt/foo/x && " "run df -h && chmod -R a+r /mnt/foo/compose /mnt/foo/x && "
"chown -R %d /mnt/foo/compose /mnt/foo/x" % os.getuid()), "chown -R %d /mnt/foo/compose /mnt/foo/x" % os.getuid()),
self._ssh_call("run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'"), self._ssh_call(
"run rpm -qa --qf='%{name}-%{version}-%{release}.%{arch}\n'",
suffix="rpms",
),
]) ])