From fbe2092d37af02276a49e335538d8caff3a15e7d Mon Sep 17 00:00:00 2001
From: Christophe Monniez <moc@odoo.com>
Date: Thu, 20 Aug 2020 12:32:27 +0000
Subject: [PATCH] [FIX] packaging: stop docker container when odoo fails

When the Odoo installation is tested, if the test fails an error is
raised but the Docker container is not stopped.

As a result, a ghost container stays alive with a Postgres server
running. Worse, if another package is built on the same host, the
ghost container prevent the other build to succeed.

With this commit, a specific Exception is raised and the container is
stopped in any case.

closes odoo/odoo#56210

Signed-off-by: Christophe Monniez (moc) <moc@odoo.com>
---
 setup/package.py | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/setup/package.py b/setup/package.py
index 4a5d2865ddda..8f6d93edbb9c 100755
--- a/setup/package.py
+++ b/setup/package.py
@@ -40,6 +40,14 @@ USER odoo
 """ % {'group_id': os.getgid(), 'user_id': os.getuid()}
 
 
+class OdooTestTimeoutError(Exception):
+    pass
+
+
+class OdooTestError(Exception):
+    pass
+
+
 def run_cmd(cmd, chdir=None, timeout=None):
     logging.info("Running command %s", cmd)
     return subprocess.run(cmd, cwd=chdir, timeout=timeout)
@@ -60,12 +68,12 @@ def _rpc_count_modules(addr='http://127.0.0.1', port=8069, dbname='mycompany'):
         )
         if toinstallmodules:
             logging.error("Package test: FAILED. Not able to install dependencies of base.")
-            raise Exception("Installation of package failed")
+            raise OdooTestError("Installation of package failed")
         else:
             logging.info("Package test: successfuly installed %s modules" % len(modules))
     else:
         logging.error("Package test: FAILED. Not able to install base.")
-        raise Exception("Package test: FAILED. Not able to install base.")
+        raise OdooTestError("Package test: FAILED. Not able to install base.")
 
 
 def publish(args, pub_type, extensions):
@@ -184,14 +192,6 @@ def _prepare_build_dir(args, win32=False):
 
 
 #  Docker stuffs
-class OdooTestTimeoutError(Exception):
-    pass
-
-
-class OdooTestError(Exception):
-    pass
-
-
 class Docker():
     """Base Docker class. Must be inherited by specific Docker builder class"""
     arch = None
@@ -265,8 +265,10 @@ class Docker():
         while self.is_running() and (time.time() - start_time) < INSTALL_TIMEOUT:
             time.sleep(5)
             if os.path.exists(os.path.join(args.build_dir, 'odoo.pid')):
-                _rpc_count_modules(port=self.exposed_port)
-                self.stop()
+                try:
+                    _rpc_count_modules(port=self.exposed_port)
+                finally:
+                    self.stop()
                 return
         if self.is_running():
             self.stop()
-- 
GitLab