From d077cb33d881c67009e6234c927c6f2be7cb3710 Mon Sep 17 00:00:00 2001 From: James Slagle Date: Fri, 20 Jul 2018 09:17:06 -0400 Subject: [PATCH] Don't assume ansible_dir in finally clause As an exception can occur before ansible_dir is ever defined, we shouldn't assume it exists in the main finally clause, otherwise an Undefined exception could be thrown. Instead, save ansbie_dir as a class variable and default it to None, that way we can first check if it's been set before trying to use it in finally. Change-Id: I90e9718324b2f5e8420b89e8c00b5e366b82e96b --- tripleoclient/v1/tripleo_deploy.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tripleoclient/v1/tripleo_deploy.py b/tripleoclient/v1/tripleo_deploy.py index b805c0831..7c6b1af44 100644 --- a/tripleoclient/v1/tripleo_deploy.py +++ b/tripleoclient/v1/tripleo_deploy.py @@ -89,6 +89,7 @@ class Deploy(command.Command): stack_update_mark = None stack_action = 'CREATE' deployment_user = None + ansible_dir = None # NOTE(cjeanner) Quick'n'dirty way before we have proper # escalation support through oslo.privsep @@ -1083,7 +1084,7 @@ class Deploy(command.Command): raise exceptions.DeploymentError(message) # download the ansible playbooks and execute them. - ansible_dir = \ + self.ansible_dir = \ self._download_ansible_playbooks(orchestration_client, parsed_args.stack, parsed_args.standalone_role) @@ -1098,14 +1099,14 @@ class Deploy(command.Command): # FIXME(bogdando): unhardcode key/transport for future # multi-node ansible.write_default_ansible_cfg( - ansible_dir, + self.ansible_dir, parsed_args.deployment_user, ssh_private_key=None, transport='local') else: self.log.warning( _('Using the existing %s for deployment') % ansible_config) - shutil.copy(ansible_config, ansible_dir) + shutil.copy(ansible_config, self.ansible_dir) # Kill heat, we're done with it now. if not parsed_args.keep_running: @@ -1113,10 +1114,10 @@ class Deploy(command.Command): if not parsed_args.output_only: # Run Upgrade tasks before the deployment if parsed_args.upgrade: - rc = self._launch_ansible_upgrade(ansible_dir) + rc = self._launch_ansible_upgrade(self.ansible_dir) if rc != 0: raise exceptions.DeploymentError('Upgrade failed') - rc = self._launch_ansible_deploy(ansible_dir) + rc = self._launch_ansible_deploy(self.ansible_dir) except Exception as e: self.log.error("Exception: %s" % six.text_type(e)) raise exceptions.DeploymentError(six.text_type(e)) @@ -1125,10 +1126,11 @@ class Deploy(command.Command): self._kill_heat(parsed_args) tar_filename = \ self._create_install_artifact(parsed_args.deployment_user) - self._dump_ansible_errors( - os.path.join(ansible_dir, - tc_constants.ANSIBLE_ERRORS_FILE), - parsed_args.stack) + if self.ansible_dir: + self._dump_ansible_errors( + os.path.join(self.ansible_dir, + tc_constants.ANSIBLE_ERRORS_FILE), + parsed_args.stack) self._cleanup_working_dirs( cleanup=parsed_args.cleanup, user=parsed_args.deployment_user