#231 Introduce toplevel_work_dir to get the Pungi working dir no matter what the compose state is.
Merged 7 months ago by jkaluza. Opened 7 months ago by jkaluza.
jkaluza/odcs proper-toplevel-dir  into  master

file modified
+13 -6

@@ -199,18 +199,25 @@ 

          return "latest-%s-1" % self.name



-     def toplevel_dir(self):

+     def toplevel_work_dir(self):

          # In case this compose failed, there won't be latest-* directory,

          # but there might be the odcs-$id-1-$date.n.0 directory.

          # The issue is that we cannot really know the date, because there is

          # a race between we start Pungi and when Pungi generates that dir,

          # so just use `glob` to find out the rigth directory.

+         glob_str = os.path.join(

+             conf.target_dir, "odcs-%d-1-*.n.0" % self.id)

+         toplevel_dirs = glob.glob(glob_str)

+         if toplevel_dirs:

+             return toplevel_dirs[0]

+         return None


+     @property

+     def toplevel_dir(self):

          if self.state == COMPOSE_STATES["failed"]:

-             glob_str = os.path.join(

-                 conf.target_dir, "odcs-%d-1-*.n.0" % self.id)

-             toplevel_dirs = glob.glob(glob_str)

-             if toplevel_dirs:

-                 return toplevel_dirs[0]

+             toplevel_dir = self.toplevel_work_dir

+             if toplevel_dir:

+                 return toplevel_dir

          return os.path.join(conf.target_dir, self.latest_dir)



file modified
+1 -1

@@ -447,7 +447,7 @@ 

          Returns the path to pungi.global.log.


          return os.path.join(

-             self.compose.toplevel_dir, "logs", "global", "pungi.global.log")

+             self.compose.toplevel_work_dir, "logs", "global", "pungi.global.log")


      def _get_global_log_errors(self):


file modified

@@ -21,12 +21,14 @@ 


  # Written by Chenxiong Qi <cqi@redhat.com>


+ import os

  import unittest


  from odcs.server import db

  from sqlalchemy import event

  from odcs.server.events import cache_composes_if_state_changed

  from odcs.server.events import start_to_publish_messages

+ from odcs.server import conf


  from flask_sqlalchemy import SignallingSession

  from mock import patch

@@ -100,6 +102,12 @@ 

              event.listen(SignallingSession, 'after_commit',



+         # Make Compose.toplevel_work_dir to work everytime.

+         self.patch_glob = patch("odcs.server.models.glob.glob")

+         self.glob = self.patch_glob.start()

+         self.glob.return_value = [

+             os.path.join(conf.target_dir, "odcs-1-2018-1")]


      def tearDown(self):

          if not self.disable_event_handlers:

              event.remove(SignallingSession, 'after_flush',

@@ -117,3 +125,5 @@ 


          event.listen(SignallingSession, 'after_commit',



+         self.patch_glob.stop()

The models.Compose.toplevel_dir property returns the latest-odcs-22955-1/ directory if the compose is not failed. If it is failed, it fallbacks to odcs-22955-1-20180919.n.0 directory, because the latest-* directory is created only for non-failed composes..

This is causing problems for PungiLogs class which is looking for Pungi logs to include them in the Compose.state_reason. When The PungiLogs is called, the compose might not be in failed state, because we are calling PungiLogs in the exception handler which will actually move the Compose to that failed state later.

In order to fix that, I've added Compose.toplevel_work_dir which always returns the the odcs-* toplevel directory which is created no matter what the composes state is and where the logs always exist.

Pull-Request has been merged by jkaluza

7 months ago