Remove redundancy in _launch_ansible* methods.

This patch tries to reuse the same function _launch_ansible
for the multiple ansible executions in tripleo_deploy.py by
passing a list with the arguments to be used by the
ansible-playbook command.

Change-Id: Iad2c9b82478ae05d68fcfd5880270105c68abefb
This commit is contained in:
Jose Luis Franco Arza 2019-02-14 11:42:42 +01:00
parent 828cfe02bb
commit 28702d18be
4 changed files with 67 additions and 42 deletions

View File

@ -86,3 +86,15 @@ CTLPLANE_DHCP_START_DEFAULT = ['192.168.24.5']
CTLPLANE_DHCP_END_DEFAULT = ['192.168.24.24']
CTLPLANE_INSPECTION_IPRANGE_DEFAULT = '192.168.24.100,192.168.24.120'
CTLPLANE_GATEWAY_DEFAULT = '192.168.24.1'
# Ansible parameters used for the actions being
# executed during tripleo deploy/upgrade.
DEPLOY_ANSIBLE_ACTIONS = {
'deploy': 'deploy_steps_playbook.yaml',
'upgrade': 'upgrade_steps_playbook.yaml --skip-tags '
'validation',
'post-upgrade': 'post_upgrade_steps_playbook.yaml '
'--skip-tags validation',
'online-upgrade': 'external_upgrade_steps_playbook.yaml '
'--tags online_upgrade',
}

View File

@ -841,12 +841,32 @@ class TestDeployUndercloud(TestPluginV1):
@mock.patch('os.execvp')
def test_launch_ansible_deploy(self, mock_execvp, mock_chdir, mock_run):
self.cmd._launch_ansible_deploy('/tmp')
self.cmd._launch_ansible('/tmp')
mock_chdir.assert_called_once()
mock_run.assert_called_once_with(self.cmd.log, [
self.ansible_playbook_cmd, '-i', '/tmp/inventory.yaml',
'deploy_steps_playbook.yaml'])
@mock.patch('tripleoclient.utils.'
'run_command_and_log', autospec=True)
@mock.patch('os.chdir')
@mock.patch('os.execvp')
def test_launch_ansible_with_args(self, mock_execvp, mock_chdir, mock_run):
args = ['deploy_steps_playbook.yaml', '--skip-tags',
'validation']
self.cmd._launch_ansible('/tmp', args, operation='deploy')
mock_chdir.assert_called_once()
mock_run.assert_called_once_with(self.cmd.log, [
self.ansible_playbook_cmd, '-i', '/tmp/inventory.yaml',
'deploy_steps_playbook.yaml', '--skip-tags', 'validation'])
@mock.patch('os.execvp')
def test_launch_ansible_invalid_op(self, mock_execvp):
self.assertRaises(exceptions.DeploymentError, self.cmd._launch_ansible,
'/tmp', operation='unploy')
@mock.patch('tripleo_common.image.kolla_builder.'
'container_images_prepare_multi')
def test_prepare_container_images(self, mock_cipm):
@ -884,7 +904,7 @@ class TestDeployUndercloud(TestPluginV1):
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'
'_create_install_artifact', return_value='/tmp/foo.tar.bzip2')
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'
'_launch_ansible_deploy', return_value=0)
'_launch_ansible', return_value=0)
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'
'_cleanup_working_dirs')
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.'

View File

@ -41,7 +41,7 @@ class TestUpgrade(utils.TestCommand):
@mock.patch('os.execvp')
def test_launch_ansible_upgrade(self, mock_execvp, mock_chdir, mock_run):
self.cmd._launch_ansible_upgrade('/tmp')
self.cmd._launch_ansible('/tmp', operation='upgrade')
mock_chdir.assert_called_once()
mock_run.assert_called_once_with(self.cmd.log, [
self.ansible_playbook_cmd, '-i', '/tmp/inventory.yaml',
@ -54,14 +54,26 @@ class TestUpgrade(utils.TestCommand):
@mock.patch('os.execvp')
def test_launch_ansible_post_upgrade(self, mock_execvp, mock_chdir,
mock_run):
self.cmd._launch_ansible_post_upgrade('/tmp')
self.cmd._launch_ansible('/tmp', operation='post-upgrade')
mock_chdir.assert_called_once()
mock_run.assert_called_once_with(self.cmd.log, [
self.ansible_playbook_cmd, '-i', '/tmp/inventory.yaml',
'post_upgrade_steps_playbook.yaml',
'--skip-tags', 'validation'])
@mock.patch('tripleoclient.utils.'
'run_command_and_log', autospec=True)
@mock.patch('os.chdir')
@mock.patch('os.execvp')
def test_launch_ansible_online_upgrade(self, mock_execvp, mock_chdir,
mock_run):
self.cmd._launch_ansible('/tmp', operation='online-upgrade')
mock_chdir.assert_called_once()
mock_run.assert_called_once_with(self.cmd.log, [
self.ansible_playbook_cmd, '-i', '/tmp/inventory.yaml',
'external_upgrade_steps_playbook.yaml',
'--tags', 'online_upgrade'])
@mock.patch('tripleoclient.v1.tripleo_deploy.Deploy.take_action',
autospec=True)
def test_take_action(self, mock_deploy):

View File

@ -866,43 +866,21 @@ class Deploy(command.Command):
return self.tmp_ansible_dir
# Never returns, calls exec()
def _launch_ansible_deploy(self, ansible_dir):
self.log.warning(_('** Running ansible deploy tasks **'))
os.chdir(ansible_dir)
playbook_inventory = os.path.join(ansible_dir, 'inventory.yaml')
cmd = [self.ansible_playbook_cmd, '-i', playbook_inventory,
'deploy_steps_playbook.yaml']
self.log.debug('Running Ansible Deploy tasks: %s' % (' '.join(cmd)))
return utils.run_command_and_log(self.log, cmd)
def _launch_ansible(self, ansible_dir, list_args=None, operation="deploy"):
def _launch_ansible_upgrade(self, ansible_dir):
self.log.warning('** Running ansible upgrade tasks **')
os.chdir(ansible_dir)
playbook_inventory = os.path.join(ansible_dir, 'inventory.yaml')
cmd = [self.ansible_playbook_cmd, '-i', playbook_inventory,
'upgrade_steps_playbook.yaml', '--skip-tags', 'validation']
self.log.debug('Running Ansible Upgrade tasks: %s' % (' '.join(cmd)))
return utils.run_command_and_log(self.log, cmd)
if list_args is None:
if operation not in constants.DEPLOY_ANSIBLE_ACTIONS.keys():
self.log.error(_('Operation %s is not allowed') % operation)
raise exceptions.DeploymentError('Invalid operation to run in '
'ansible.')
list_args = constants.DEPLOY_ANSIBLE_ACTIONS[operation].split()
def _launch_ansible_post_upgrade(self, ansible_dir):
self.log.warning('** Running ansible post-upgrade tasks **')
self.log.warning(_('** Running ansible %s tasks **') % operation)
os.chdir(ansible_dir)
playbook_inventory = os.path.join(ansible_dir, 'inventory.yaml')
cmd = [self.ansible_playbook_cmd, '-i', playbook_inventory,
'post_upgrade_steps_playbook.yaml', '--skip-tags', 'validation']
self.log.debug('Running Ansible Post Upgrade '
'tasks: %s' % (' '.join(cmd)))
return utils.run_command_and_log(self.log, cmd)
def _launch_ansible_online_upgrade(self, ansible_dir):
self.log.warning('** Running ansible online upgrade tasks **')
os.chdir(ansible_dir)
playbook_inventory = os.path.join(ansible_dir, 'inventory.yaml')
cmd = [self.ansible_playbook_cmd, '-i', playbook_inventory,
'external_upgrade_steps_playbook.yaml', '--tags',
'online_upgrade']
self.log.debug('Running Ansible Online Upgrade '
'tasks: %s' % (' '.join(cmd)))
cmd = [self.ansible_playbook_cmd, '-i', playbook_inventory] + list_args
self.log.debug('Running Ansible %s tasks: %s' % (operation, ' '
.join(cmd)))
return utils.run_command_and_log(self.log, cmd)
def get_parser(self, prog_name):
@ -1276,19 +1254,22 @@ class Deploy(command.Command):
if not parsed_args.output_only:
if parsed_args.upgrade:
# Run Upgrade tasks before the deployment
rc = self._launch_ansible_upgrade(self.ansible_dir)
rc = self._launch_ansible(self.ansible_dir,
operation='upgrade')
if rc != 0:
raise exceptions.DeploymentError('Upgrade failed')
rc = self._launch_ansible_deploy(self.ansible_dir)
rc = self._launch_ansible(self.ansible_dir)
if rc != 0:
raise exceptions.DeploymentError('Deployment failed')
if parsed_args.upgrade:
# Run Post Upgrade tasks after the deployment
rc = self._launch_ansible_post_upgrade(self.ansible_dir)
rc = self._launch_ansible(self.ansible_dir,
operation='post-upgrade')
if rc != 0:
raise exceptions.DeploymentError('Post Upgrade failed')
# Run Online Upgrade tasks after the deployment
rc = self._launch_ansible_online_upgrade(self.ansible_dir)
rc = self._launch_ansible(self.ansible_dir,
operation='online-upgrade')
if rc != 0:
raise exceptions.DeploymentError(
'Online Upgrade failed')