Use static inventory YAML file for saving execution time

The validations execution will use now a static inventory file to speed
up things a bit. Note that this file will be removed after execution.

Change-Id: Ibdc3fc7ea9d3dec9531a2ce29ec8a1477c33c780
Signed-off-by: Gael Chamoulaud <gchamoul@redhat.com>
This commit is contained in:
Gael Chamoulaud 2019-07-19 10:41:00 +02:00
parent 82887ef228
commit 4f55cdf59a
3 changed files with 43 additions and 11 deletions

View File

@ -73,13 +73,17 @@ class TestValidatorRun(utils.TestCommand):
'validation_inputs': {}
})
@mock.patch('sys.exit')
@mock.patch('logging.getLogger')
@mock.patch('pwd.getpwuid')
@mock.patch('os.getuid')
@mock.patch('tripleoclient.utils.get_tripleo_ansible_inventory',
return_value='/home/stack/inventory.yaml')
@mock.patch('tripleoclient.utils.run_ansible_playbook',
autospec=True)
def test_validation_run_with_ansible(self, plan_mock, mock_getuid,
mock_getpwuid, mock_logger):
def test_validation_run_with_ansible(self, plan_mock, mock_inventory,
mock_getuid, mock_getpwuid,
mock_logger, mock_sysexit):
mock_pwuid = mock.Mock()
mock_pwuid.pw_dir = '/home/stack'
mock_getpwuid.return_value = mock_pwuid
@ -100,7 +104,7 @@ class TestValidatorRun(utils.TestCommand):
plan_mock.assert_called_once_with(
logger=mock_log,
plan='overcloud',
inventory='/usr/bin/tripleo-ansible-inventory',
inventory='/home/stack/inventory.yaml',
workdir=playbooks_dir,
log_path_dir='/home/stack',
playbook='check-ftype.yaml',
@ -109,3 +113,5 @@ class TestValidatorRun(utils.TestCommand):
extra_vars={},
python_interpreter='/usr/bin/python{}'.format(sys.version_info[0])
)
assert mock_sysexit.called

View File

@ -1190,9 +1190,10 @@ def load_environment_directories(directories):
return environments
def get_tripleo_ansible_inventory(inventory_file='',
def get_tripleo_ansible_inventory(inventory_file=None,
ssh_user='tripleo-admin',
stack='overcloud'):
stack='overcloud',
return_inventory_file_path=False):
if not inventory_file:
inventory_file = '%s/%s' % (os.path.expanduser('~'),
'tripleo-ansible-inventory.yaml')
@ -1204,9 +1205,12 @@ def get_tripleo_ansible_inventory(inventory_file='',
'--undercloud-connection', 'ssh',
'--static-yaml-inventory', inventory_file)
except processutils.ProcessExecutionError as e:
message = _("Failed to generate inventory: %s") % str(e)
raise exceptions.InvalidConfiguration(message)
message = _("Failed to generate inventory: %s") % str(e)
raise exceptions.InvalidConfiguration(message)
if os.path.exists(inventory_file):
if return_inventory_file_path:
return inventory_file
with open(inventory_file, "r") as f:
inventory = f.read()
return inventory
@ -1215,6 +1219,12 @@ def get_tripleo_ansible_inventory(inventory_file='',
"Inventory file %s can not be found.") % inventory_file)
def cleanup_tripleo_ansible_inventory_file(path):
"""Remove the static tripleo-ansible-inventory file from disk"""
if os.path.exists(path):
processutils.execute('/usr/bin/rm', '-f', path)
def process_multiple_environments(created_env_files, tht_root,
user_tht_root, cleanup=True):
log = logging.getLogger(__name__ + ".process_multiple_environments")

View File

@ -337,6 +337,12 @@ class TripleOValidatorRun(command.Command):
python_interpreter = \
"/usr/bin/python{}".format(sys.version_info[0])
static_inventory = oooutils.get_tripleo_ansible_inventory(
ssh_user='heat-admin', stack=parsed_args.plan,
return_inventory_file_path=True)
failed_val = False
for playbook in playbooks:
try:
LOG.debug(_('Running the validations with Ansible'))
@ -346,17 +352,27 @@ class TripleOValidatorRun(command.Command):
workdir=constants.ANSIBLE_VALIDATION_DIR,
log_path_dir=pwd.getpwuid(os.getuid()).pw_dir,
playbook=playbook,
inventory='/usr/bin/tripleo-ansible-inventory',
inventory=static_inventory,
retries=False,
output_callback='validation_output',
extra_vars=extra_vars_input,
python_interpreter=python_interpreter)
print('[SUCCESS] - {}\n{}'.format(
playbook, oooutils.indent(output)))
print('[SUCCESS] - {}\n{}'.format(playbook,
oooutils.indent(output)))
except Exception as e:
print('[FAILED] - {}\n{}'.format(
failed_val = True
LOG.error('[FAILED] - {}\n{}'.format(
playbook, oooutils.indent(e.args[0])))
LOG.debug(_('Removing static tripleo ansible inventory file'))
oooutils.cleanup_tripleo_ansible_inventory_file(
static_inventory)
if failed_val:
LOG.error(_('One or more validations have failed!'))
sys.exit(1)
sys.exit(0)
def take_action(self, parsed_args):
if parsed_args.use_mistral:
self._run_validation_run_with_mistral(parsed_args)