From 4cc14c685039cf3ebf21e5d14975b1b44900e8a5 Mon Sep 17 00:00:00 2001 From: Sean Marlow Date: Wed, 29 May 2019 09:03:23 -0500 Subject: [PATCH 1/2] Attempt to cleanup instance on ssh fail. If instance is not accessible after boot attempt to cleanup based on arguments. Move cleanup clause to new instance method. --- ipa/ipa_cloud.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/ipa/ipa_cloud.py b/ipa/ipa_cloud.py index 855a86b5..a4f4c787 100644 --- a/ipa/ipa_cloud.py +++ b/ipa/ipa_cloud.py @@ -598,6 +598,19 @@ def put_file(self, client, source_file): else: return file_name + def _cleanup_instance(self, status): + """ + Cleanup instance based on arguments. + + If tests pass and cleanup flag is none, or + cleanup flag is true, terminate instance. + """ + if status == 0 and self.cleanup is None or self.cleanup: + self.logger.info( + 'Terminating instance %s' % self.running_instance_id + ) + self._terminate_instance() + def test_image(self): """ The entry point for testing an image. @@ -638,10 +651,14 @@ def test_image(self): client ) except IpaSSHException as error: + self._cleanup_instance(1) + raise IpaCloudException( 'Unable to connect to instance: %s' % error ) except Exception as error: + self._cleanup_instance(1) + raise IpaCloudException( 'An error occurred retrieving host key: %s' % error ) @@ -761,14 +778,7 @@ def test_image(self): if self.collect_vm_info: self._collect_vm_info() - # If tests pass and cleanup flag is none, or - # cleanup flag is true, terminate instance. - if status == 0 and self.cleanup is None or self.cleanup: - self.logger.info( - 'Terminating instance %s' % self.running_instance_id - ) - self._terminate_instance() - + self._cleanup_instance(status) self._save_results() self._update_history() From ef971a6987d1a36cefb687be9866928a62a61af1 Mon Sep 17 00:00:00 2001 From: Sean Marlow Date: Thu, 30 May 2019 12:00:15 -0500 Subject: [PATCH 2/2] Wrap first clause in brackets. In cleanup instance method to make the order clear. --- ipa/ipa_cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipa/ipa_cloud.py b/ipa/ipa_cloud.py index a4f4c787..6f9b7598 100644 --- a/ipa/ipa_cloud.py +++ b/ipa/ipa_cloud.py @@ -605,7 +605,7 @@ def _cleanup_instance(self, status): If tests pass and cleanup flag is none, or cleanup flag is true, terminate instance. """ - if status == 0 and self.cleanup is None or self.cleanup: + if (status == 0 and self.cleanup is None) or self.cleanup: self.logger.info( 'Terminating instance %s' % self.running_instance_id )