From: Andrew Schoen Date: Thu, 18 Jun 2015 16:57:45 +0000 (-0500) Subject: Enable the Ansible task to optionally cleanup after itself. X-Git-Tag: 1.1.0~908^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F528%2Fhead;p=teuthology.git Enable the Ansible task to optionally cleanup after itself. If given the config value of ``cleanup`` the ansible task will rerun the same playbook during the teardown stage with a ``teardown`` var added with a value of True. This will allow the playbook to cleanup after itself during teardown, if the playbook supports this feature. Signed-off-by: Andrew Schoen --- diff --git a/teuthology/task/ansible.py b/teuthology/task/ansible.py index 89bd2829..f3b73288 100644 --- a/teuthology/task/ansible.py +++ b/teuthology/task/ansible.py @@ -62,6 +62,10 @@ class Ansible(Task): directly to ansible-playbook. vars: A dict of vars to be passed to ansible-playbook via the --extra-vars flag + cleanup: If present, the given or generated playbook will be run + again during teardown with a 'cleanup' var set to True. + This will allow the playbook to clean up after itself, + if the playbook supports this feature. Examples: @@ -250,12 +254,31 @@ class Ansible(Task): return args def teardown(self): + self._cleanup() if self.generated_inventory: os.remove(self.inventory) if self.generated_playbook: os.remove(self.playbook_file.name) super(Ansible, self).teardown() + def _cleanup(self): + """ + If the ``cleanup`` key exists in config the same playbook will be + run again during the teardown step with the var ``cleanup`` given with + a value of ``True``. If supported, this will allow the playbook to + cleanup after itself during teardown. + """ + if self.config.get("cleanup"): + log.info("Running ansible cleanup...") + extra = dict(cleanup=True) + if self.config.get('vars'): + self.config.get('vars').update(extra) + else: + self.config['vars'] = extra + self.execute_playbook() + else: + log.info("Skipping ansible cleanup...") + class CephLab(Ansible): __doc__ = """ diff --git a/teuthology/test/task/test_ansible.py b/teuthology/test/task/test_ansible.py index edb1eb70..51bb2ddc 100644 --- a/teuthology/test/task/test_ansible.py +++ b/teuthology/test/task/test_ansible.py @@ -344,6 +344,42 @@ class TestAnsibleTask(TestTask): task.teardown() assert m_remove.called_once_with('fake') + def test_teardown_cleanup_with_vars(self): + task_config = dict( + playbook=[], + cleanup=True, + vars=dict(yum_repos="testing"), + ) + task = Ansible(self.ctx, task_config) + task.inventory = "fake" + task.generated_playbook = True + task.playbook_file = Mock() + task.playbook_file.name = 'fake' + with patch.object(Ansible, 'execute_playbook') as m_execute: + with patch.object(ansible.os, 'remove'): + task.teardown() + task._build_args() + assert m_execute.called + assert 'cleanup' in task.config['vars'] + assert 'yum_repos' in task.config['vars'] + + def test_teardown_cleanup_with_no_vars(self): + task_config = dict( + playbook=[], + cleanup=True, + ) + task = Ansible(self.ctx, task_config) + task.inventory = "fake" + task.generated_playbook = True + task.playbook_file = Mock() + task.playbook_file.name = 'fake' + with patch.object(Ansible, 'execute_playbook') as m_execute: + with patch.object(ansible.os, 'remove'): + task.teardown() + task._build_args() + assert m_execute.called + assert 'cleanup' in task.config['vars'] + class TestCephLabTask(TestTask): def setup(self):