From 8b38c4d5c9f62b7cb42d8e885cea56f188ab9110 Mon Sep 17 00:00:00 2001 From: Javier Pena Date: Fri, 17 Feb 2017 17:48:47 +0100 Subject: [PATCH] Add work-dir command-line option The working directory was previously generated as a temporary one, but there may be cases when we want to specify a directory, for example if we want to generate the Dockerfiles and then do some parsing on them. Change-Id: Ic332328913924bcb3d0dfa4d473e796050fc14b3 --- kolla/common/config.py | 2 ++ kolla/image/build.py | 15 ++++++++++----- kolla/tests/test_build.py | 7 +++++++ ...-dir-command-line-option-dd83aa934d5c9e3e.yaml | 6 ++++++ 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/Add-work-dir-command-line-option-dd83aa934d5c9e3e.yaml diff --git a/kolla/common/config.py b/kolla/common/config.py index 30f6798806..c47c3c660f 100755 --- a/kolla/common/config.py +++ b/kolla/common/config.py @@ -217,6 +217,8 @@ _CLI_OPTS = [ cfg.StrOpt('logs-dir', help='Path to logs directory'), cfg.BoolOpt('pull', default=True, help='Attempt to pull a newer version of the base image.'), + cfg.StrOpt('work-dir', help='Path to be used as working directory.' + 'By default, a temporary dir is created.'), ] _BASE_OPTS = [ diff --git a/kolla/image/build.py b/kolla/image/build.py index 133063513c..a6736172ec 100755 --- a/kolla/image/build.py +++ b/kolla/image/build.py @@ -633,10 +633,14 @@ class KollaWorker(object): def setup_working_dir(self): """Creates a working directory for use while building""" - ts = time.time() - ts = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H-%M-%S_') - self.temp_dir = tempfile.mkdtemp(prefix='kolla-' + ts) - self.working_dir = os.path.join(self.temp_dir, 'docker') + if self.conf.work_dir: + self.working_dir = os.path.join(self.conf.work_dir, 'docker') + else: + ts = time.time() + ts = datetime.datetime.fromtimestamp(ts).strftime( + '%Y-%m-%d_%H-%M-%S_') + self.temp_dir = tempfile.mkdtemp(prefix='kolla-' + ts) + self.working_dir = os.path.join(self.temp_dir, 'docker') shutil.copytree(self.images_dir, self.working_dir) self.copy_apt_files() LOG.debug('Created working dir: %s', self.working_dir) @@ -754,7 +758,8 @@ class KollaWorker(object): def cleanup(self): """Remove temp files""" - shutil.rmtree(self.temp_dir) + if not self.conf.work_dir: + shutil.rmtree(self.temp_dir) def filter_images(self): """Filter which images to build""" diff --git a/kolla/tests/test_build.py b/kolla/tests/test_build.py index 1a1e019d55..7bcd2de7f2 100644 --- a/kolla/tests/test_build.py +++ b/kolla/tests/test_build.py @@ -349,6 +349,13 @@ class KollaWorkerTest(base.TestCase): results = kolla.summary() self.assertEqual(None, results['failed'][0]['status']) + @mock.patch('shutil.copytree') + def test_work_dir(self, copytree_mock): + self.conf.set_override('work_dir', '/tmp/foo') + kolla = build.KollaWorker(self.conf) + kolla.setup_working_dir() + self.assertEqual('/tmp/foo/docker', kolla.working_dir) + class MainTest(base.TestCase): diff --git a/releasenotes/notes/Add-work-dir-command-line-option-dd83aa934d5c9e3e.yaml b/releasenotes/notes/Add-work-dir-command-line-option-dd83aa934d5c9e3e.yaml new file mode 100644 index 0000000000..329fd4226e --- /dev/null +++ b/releasenotes/notes/Add-work-dir-command-line-option-dd83aa934d5c9e3e.yaml @@ -0,0 +1,6 @@ +--- +features: + - A new work-dir command-line option has been added to + the client, to allow the user to specify a directory + to be used as working dir, instead of the default + temp directory.