From d5f458bca1512e7fa95a7ad9475e9b8b820f7a15 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 02 2023 12:59:00 +0000
Subject: [PATCH 1/3] kiwi: upload log for failed tasks


Related: https://pagure.io/koji/issue/3597

---

diff --git a/builder/kojid b/builder/kojid
index 4d0ad8b..e91545c 100755
--- a/builder/kojid
+++ b/builder/kojid
@@ -436,8 +436,10 @@ class BuildRoot(object):
         with koji._open_text_file(self.rootdir() + destfile, 'wt') as fo:
             fo.write(settings)
 
-    def mock(self, args):
+    def mock(self, args, additional_logs=None):
         """Run mock"""
+        if additional_logs is None:
+            additional_logs = {}
         mockpath = getattr(self.options, "mockpath", "/usr/bin/mock")
         cmd = [mockpath, "-r", self.mockcfg]
         # if self.options.debug_mock:
@@ -457,6 +459,8 @@ class BuildRoot(object):
             resultdir = self.resultdir()
             uploadpath = self.getUploadPath()
             logs = {}
+            for logname, path in additional_logs.items():
+                logs[logname] = (None, None, 0, path)
 
             ts_offsets = {}
             finished = False
diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index eb3b086..30a0c4e 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -371,19 +371,22 @@ class KiwiCreateImageTask(BaseBuildTask):
         desc, types = self.prepareDescription(path, name, version, repos, arch)
         self.uploadFile(desc)
 
+        target_dir = '/builddir/result/image'
+        root_log_path = os.path.join(broot.rootdir(), target_dir[1:], "build/image-root.log")
+        root_log_remote_name = f"image-root.{arch}.log"
         cmd = ['kiwi-ng']
         if self.opts.get('profile'):
             cmd.extend(['--profile', self.opts['profile']])
         if self.opts.get('type'):
             cmd.extend(['--type', self.opts['type']])
-        target_dir = '/builddir/result/image'
         cmd.extend([
             '--kiwi-file', os.path.basename(desc),  # global option for image/system commands
             'system', 'build',
             '--description', os.path.join(os.path.basename(scmsrcdir), base_path),
             '--target-dir', target_dir,
         ])
-        rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd)
+        rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd,
+                        additional_logs={root_log_remote_name: root_log_path})
         if rv:
             raise koji.GenericError("Kiwi failed")
 
@@ -410,12 +413,6 @@ class KiwiCreateImageTask(BaseBuildTask):
             'files': [],
         }
 
-        # TODO: upload detailed log?
-        # build/image-root.log
-        root_log_path = os.path.join(broot.tmpdir(), target_dir[1:], "build/image-root.log")
-        if os.path.exists(root_log_path):
-            self.uploadFile(root_log_path, remoteName=f"image-root.{arch}.log")
-
         bundle_path = os.path.join(broot.rootdir(), bundle_dir[1:])
         for fname in os.listdir(bundle_path):
             self.uploadFile(os.path.join(bundle_path, fname), remoteName=fname)

From dfe15d23fe92402e860f2d6e21aa415d6195ae36 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 02 2023 12:59:00 +0000
Subject: [PATCH 2/3] symlink logs to result dir


---

diff --git a/builder/kojid b/builder/kojid
index e91545c..4d0ad8b 100755
--- a/builder/kojid
+++ b/builder/kojid
@@ -436,10 +436,8 @@ class BuildRoot(object):
         with koji._open_text_file(self.rootdir() + destfile, 'wt') as fo:
             fo.write(settings)
 
-    def mock(self, args, additional_logs=None):
+    def mock(self, args):
         """Run mock"""
-        if additional_logs is None:
-            additional_logs = {}
         mockpath = getattr(self.options, "mockpath", "/usr/bin/mock")
         cmd = [mockpath, "-r", self.mockcfg]
         # if self.options.debug_mock:
@@ -459,8 +457,6 @@ class BuildRoot(object):
             resultdir = self.resultdir()
             uploadpath = self.getUploadPath()
             logs = {}
-            for logname, path in additional_logs.items():
-                logs[logname] = (None, None, 0, path)
 
             ts_offsets = {}
             finished = False
diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index 30a0c4e..2f306b4 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -372,8 +372,10 @@ class KiwiCreateImageTask(BaseBuildTask):
         self.uploadFile(desc)
 
         target_dir = '/builddir/result/image'
-        root_log_path = os.path.join(broot.rootdir(), target_dir[1:], "build/image-root.log")
-        root_log_remote_name = f"image-root.{arch}.log"
+        os.symlink(  # symlink log to resultdir, so it is incrementally uploaded
+            os.path.join(broot.rootdir(), "/tmp/image-root.{arch}.log"),
+            os.path.join(broot.resultdir(), f'image-root.{arch}.log')
+        )
         cmd = ['kiwi-ng']
         if self.opts.get('profile'):
             cmd.extend(['--profile', self.opts['profile']])
@@ -381,18 +383,24 @@ class KiwiCreateImageTask(BaseBuildTask):
             cmd.extend(['--type', self.opts['type']])
         cmd.extend([
             '--kiwi-file', os.path.basename(desc),  # global option for image/system commands
+            '--logfile', f"/tmp/image-root.{arch}.log",
             'system', 'build',
             '--description', os.path.join(os.path.basename(scmsrcdir), base_path),
             '--target-dir', target_dir,
         ])
-        rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd,
-                        additional_logs={root_log_remote_name: root_log_path})
+        rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd)
         if rv:
             raise koji.GenericError("Kiwi failed")
 
         # rename artifacts accordingly to release
+        os.symlink(  # symlink log to resultdir, so it is incrementally uploaded
+            os.path.join(broot.rootdir(), "/tmp/kiwi-result-bundle.{arch}.log"),
+            os.path.join(broot.resultdir(), f'kiwi-result-bundle.{arch}.log')
+        )
         bundle_dir = '/builddir/result/bundle'
-        cmd = ['kiwi-ng', 'result', 'bundle',
+        cmd = ['kiwi-ng',
+               '--logfile', f"/tmp/kiwi-result-bundle.{arch}.log",
+               'result', 'bundle',
                '--target-dir', target_dir,
                '--bundle-dir', bundle_dir,
                '--id', release]

From 95820af2bf7b0f13f86dfbc975e4478f1ca02d6a Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 02 2023 13:09:22 +0000
Subject: [PATCH 3/3] fix typos


---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index 2f306b4..e1c7a0a 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -373,7 +373,7 @@ class KiwiCreateImageTask(BaseBuildTask):
 
         target_dir = '/builddir/result/image'
         os.symlink(  # symlink log to resultdir, so it is incrementally uploaded
-            os.path.join(broot.rootdir(), "/tmp/image-root.{arch}.log"),
+            os.path.join(broot.rootdir(), f'tmp/image-root.{arch}.log'),
             os.path.join(broot.resultdir(), f'image-root.{arch}.log')
         )
         cmd = ['kiwi-ng']
@@ -383,7 +383,7 @@ class KiwiCreateImageTask(BaseBuildTask):
             cmd.extend(['--type', self.opts['type']])
         cmd.extend([
             '--kiwi-file', os.path.basename(desc),  # global option for image/system commands
-            '--logfile', f"/tmp/image-root.{arch}.log",
+            '--logfile', f'/tmp/image-root.{arch}.log',
             'system', 'build',
             '--description', os.path.join(os.path.basename(scmsrcdir), base_path),
             '--target-dir', target_dir,
@@ -394,12 +394,12 @@ class KiwiCreateImageTask(BaseBuildTask):
 
         # rename artifacts accordingly to release
         os.symlink(  # symlink log to resultdir, so it is incrementally uploaded
-            os.path.join(broot.rootdir(), "/tmp/kiwi-result-bundle.{arch}.log"),
+            os.path.join(broot.rootdir(), f'/tmp/kiwi-result-bundle.{arch}.log'),
             os.path.join(broot.resultdir(), f'kiwi-result-bundle.{arch}.log')
         )
         bundle_dir = '/builddir/result/bundle'
         cmd = ['kiwi-ng',
-               '--logfile', f"/tmp/kiwi-result-bundle.{arch}.log",
+               '--logfile', f'/tmp/kiwi-result-bundle.{arch}.log',
                'result', 'bundle',
                '--target-dir', target_dir,
                '--bundle-dir', bundle_dir,

