From 06794fdc6b6bba7030866c2828376d07f3596343 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Andr=C3=A9?= <m.andre@redhat.com>
Date: Fri, 11 Dec 2015 17:45:03 +0900
Subject: [PATCH] Catch exception when directory creation fails

The build script now creates now creates a plugins directory for source
builds to store plugins required for the container, and creates a tar
of all plugins that is extracted at runtime.

However, the creation of the 'plugins' directory is not handled
correctly and generate an exception in the running thread if the
directory exists. This is the case for example if the build script
retries a failing image.

This commit fixes the timeout issues we've experienced with the gate.

Change-Id: Ic5d4fd1ddf71f01c547130518e311fded911c445
Closes-Bug: 1524897
---
 kolla/cmd/build.py | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/kolla/cmd/build.py b/kolla/cmd/build.py
index dba153458c..216d9d48bc 100755
--- a/kolla/cmd/build.py
+++ b/kolla/cmd/build.py
@@ -16,6 +16,7 @@
 
 import argparse
 import datetime
+import errno
 import json
 import logging
 import os
@@ -168,6 +169,7 @@ class WorkerThread(Thread):
                 return
 
         plugin_archives = list()
+        plugins_path = os.path.join(image['path'], 'plugins')
         for plugin in image['plugins']:
             archive_path = self.process_source(image, plugin)
             if image['status'] == "error":
@@ -176,13 +178,22 @@ class WorkerThread(Thread):
         if plugin_archives:
             for plugin_archive in plugin_archives:
                 with tarfile.open(plugin_archive, 'r') as plugin_archive_tar:
-                    plugin_archive_tar.extractall(
-                        path=os.path.join(image['path'], 'plugins'))
+                    plugin_archive_tar.extractall(path=plugins_path)
         else:
-            os.mkdir(os.path.join(image['path'], 'plugins'))
+            try:
+                os.mkdir(plugins_path)
+            except OSError as e:
+                if e.errno == errno.EEXIST:
+                    LOG.info('Directory {} already exist. Skipping.'.format(
+                        plugins_path))
+                else:
+                    LOG.error('Failed to create directory {}: {}'.format(
+                        plugins_path, e))
+                    image['status'] = "error"
+                    return
         with tarfile.open(os.path.join(image['path'], 'plugins-archive'),
                           'w') as tar:
-            tar.add(os.path.join(image['path'], 'plugins'), arcname='plugins')
+            tar.add(plugins_path, arcname='plugins')
 
         # Pull the latest image for the base distro only
         pull = True if image['parent'] is None else False