def test_run(self, mock_mkdtemp, mock_swiftutils, mock_os_symlink, mock_os_path_exists, mock_os_unlink): mock_mkdtemp.return_value = '/tmp/tripleo-foo-config' action = config.DownloadConfigAction(self.config_container) action.run(self.ctx) mock_swiftutils.assert_called_once_with(self.swift, self.config_container, '/tmp/tripleo-foo-config')
def test_run_create_latest_symlink(self, mock_mkdtemp, mock_swiftutils, mock_os_symlink, mock_os_path_exists): mock_mkdtemp.return_value = '/var/lib/mistral/uuid' mock_os_path_exists.return_value = False action = config.DownloadConfigAction(self.config_container) action.run(self.ctx) mock_swiftutils.assert_called_once_with(self.swift, self.config_container, mock_mkdtemp()) mock_os_symlink.assert_called_once_with( action.work_dir, os.path.join(os.path.dirname(action.work_dir), 'config-download-latest'))
def config_download(log, clients, stack, ssh_network=None, output_dir=None, override_ansible_cfg=None, timeout=600, verbosity=0, deployment_options=None, in_flight_validations=False, ansible_playbook_name='deploy_steps_playbook.yaml', limit_hosts=None, extra_vars=None, inventory_path=None, ssh_user='******', tags=None, skip_tags=None, deployment_timeout=None): """Run config download. :param log: Logging object :type log: Object :param clients: openstack clients :type clients: Object :param stack: Heat Stack object :type stack: Object :param ssh_network: Network named used to access the overcloud. :type ssh_network: String :param output_dir: Path to the output directory. :type output_dir: String :param override_ansible_cfg: Ansible configuration file location. :type override_ansible_cfg: String :param timeout: Ansible connection timeout in seconds. :type timeout: Integer :param verbosity: Ansible verbosity level. :type verbosity: Integer :param deployment_options: Additional deployment options. :type deployment_options: Dictionary :param in_flight_validations: Enable or Disable inflight validations. :type in_flight_validations: Boolean :param ansible_playbook_name: Name of the playbook to execute. :type ansible_playbook_name: String :param limit_hosts: String of hosts to limit the current playbook to. :type limit_hosts: String :param extra_vars: Set additional variables as a Dict or the absolute path of a JSON or YAML file type. :type extra_vars: Either a Dict or the absolute path of JSON or YAML :param inventory_path: Inventory file or path, if None is provided this function will perform a lookup :type inventory_path: String :param ssh_user: SSH user, defaults to tripleo-admin. :type ssh_user: String :param tags: Ansible inclusion tags. :type tags: String :param skip_tags: Ansible exclusion tags. :type skip_tags: String :param deployment_timeout: Deployment timeout in minutes. :type deployment_timeout: Integer """ def _log_and_print(message, logger, level='info', print_msg=True): """Print and log a given message. :param message: Message to print and log. :type message: String :param log: Logging object :type log: Object :param level: Log level. :type level: String :param print_msg: Print messages to stdout. :type print_msg: Boolean """ if print_msg: print(message) log = getattr(logger, level) log(message) if not output_dir: output_dir = DEFAULT_WORK_DIR if not deployment_options: deployment_options = dict() if not in_flight_validations: if skip_tags: skip_tags = 'opendev-validation,{}'.format(skip_tags) else: skip_tags = 'opendev-validation' with utils.TempDirs() as tmp: utils.run_ansible_playbook(playbook='cli-grant-local-access.yaml', inventory='localhost,', workdir=tmp, playbook_dir=ANSIBLE_TRIPLEO_PLAYBOOKS, verbosity=verbosity, extra_vars={ 'access_path': output_dir, 'execution_user': getpass.getuser() }) stack_work_dir = os.path.join(output_dir, stack.stack_name) context = clients.tripleoclient.create_mistral_context() _log_and_print( message='Checking for blacklisted hosts from stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0)) blacklist_show = stack.output_show('BlacklistedHostnames') blacklist_stack_output = blacklist_show.get('output', dict()) blacklist_stack_output_value = blacklist_stack_output.get('output_value') if blacklist_stack_output_value: if not limit_hosts: limit_hosts = "" limit_hosts += (':'.join( ['!{}'.format(i) for i in blacklist_stack_output_value if i])) _log_and_print(message='Retrieving configuration for stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0)) container_config = '{}-config'.format(stack.stack_name) utils.get_config(clients, container=stack.stack_name, container_config=container_config) _log_and_print(message='Downloading configuration for stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0)) download = config.DownloadConfigAction(work_dir=stack_work_dir, container_config=container_config) work_dir = download.run(context=context) _log_and_print(message='Retrieving keyfile for stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0)) key_file = utils.get_key(stack=stack.stack_name) _log_and_print(message='Generating information for stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0)) inventory_kwargs = { 'ansible_ssh_user': ssh_user, 'work_dir': work_dir, 'plan_name': stack.stack_name, 'undercloud_key_file': key_file } if ssh_network: inventory_kwargs['ssh_network'] = ssh_network python_interpreter = deployment_options.get('ansible_python_interpreter') if python_interpreter: inventory_kwargs['ansible_python_interpreter'] = python_interpreter if not inventory_path: inventory = ansible.AnsibleGenerateInventoryAction(**inventory_kwargs) inventory_path = inventory.run(context=context) _log_and_print( message='Executing deployment playbook for stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0)) if isinstance(ansible_playbook_name, list): playbooks = [ os.path.join(stack_work_dir, p) for p in ansible_playbook_name ] else: playbooks = os.path.join(stack_work_dir, ansible_playbook_name) with utils.TempDirs() as tmp: utils.run_ansible_playbook( playbook=playbooks, inventory=inventory_path, workdir=tmp, playbook_dir=work_dir, skip_tags=skip_tags, tags=tags, ansible_cfg=override_ansible_cfg, verbosity=verbosity, ssh_user=ssh_user, key=key_file, limit_hosts=limit_hosts, ansible_timeout=timeout, reproduce_command=True, extra_env_variables={ 'ANSIBLE_BECOME': True, }, extra_vars=extra_vars, timeout=deployment_timeout, ) _log_and_print( message='Overcloud configuration completed for stack: {}'.format( stack.stack_name), logger=log, print_msg=(verbosity == 0))
def test_run(self, mock_mkdtemp, mock_swiftutils): action = config.DownloadConfigAction(self.config_container) action.run(self.ctx) mock_swiftutils.assert_called_once_with(self.swift, self.config_container, mock_mkdtemp())