]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Enable the Ansible task to optionally cleanup after itself. 528/head
authorAndrew Schoen <aschoen@redhat.com>
Thu, 18 Jun 2015 16:57:45 +0000 (11:57 -0500)
committerAndrew Schoen <aschoen@redhat.com>
Thu, 18 Jun 2015 21:16:08 +0000 (16:16 -0500)
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 <aschoen@redhat.com>
teuthology/task/ansible.py
teuthology/test/task/test_ansible.py

index 89bd28299662ff002e400294efbed9c472f6b9c8..f3b73288493bd5bd4745dbc812e3d776708f114e 100644 (file)
@@ -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__ = """
index edb1eb70a34329f15aa6fe1b7b443854db52af2d..51bb2ddc2392596cfcc77126b9e22fb379a0fd49 100644 (file)
@@ -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):