Esempio n. 1
0
def test_remove_args():
    test_list = ['tags', 'molecule1', 'platform', 'ubuntu', 'tags',
                 'molecule2']
    test_dict = {'tags': 'molecule1', 'platform': 'ubuntu'}
    expected_list = ['platform', 'ubuntu']
    expected_dict = {'platform': 'ubuntu'}
    actual_list, actual_dict = util.remove_args(test_list, test_dict, ['tags'])

    assert expected_list == actual_list
    assert expected_dict == actual_dict
Esempio n. 2
0
    def execute(self, exit=True):
        """
        Execute the actions necessary to perform a `molecule test` and
        return a tuple.

        :param exit: (Unused) Provided to complete method signature.
        :return: Return a tuple of (`exit status`, `command output`), otherwise
         sys.exit on command failure.
        """
        command_args, args = util.remove_args(self.command_args, self.args,
                                              self.command_args)

        for task in self.molecule.config.config['molecule']['test'][
                'sequence']:
            command_module = getattr(molecule.command, task)
            command = getattr(command_module, task.capitalize())
            c = command(command_args, args, self.molecule)

            for argument in self.command_args:
                if argument in c.args:
                    c.args[argument] = self.args[argument]

            status, output = c.execute(exit=False)

            # Fail fast
            if status is not 0 and status is not None:
                if output:
                    LOG.error(output)
                util.sysexit(status)

        if self.args.get('--destroy') == 'always':
            c = molecule.command.destroy.Destroy(command_args, args)
            c.execute()
            return None, None

        if self.args.get('--destroy') == 'never':
            return None, None

        # passing (default)
        if status is None:
            c = molecule.command.destroy.Destroy(command_args, args)
            c.execute()
            return None, None

        # error encountered during test
        util.sysexit(status)
Esempio n. 3
0
    def execute(self):
        command_args, args = util.remove_args(self.command_args, self.args,
                                              self.command_args)

        for task in self.molecule.config.config['molecule']['test'][
                'sequence']:
            command_module = getattr(molecule.command, task)
            command = getattr(command_module, task.capitalize())
            c = command(command_args, args, self.molecule)

            for argument in self.command_args:
                if argument in c.args:
                    c.args[argument] = self.args[argument]

            status, output = c.execute(exit=False)

            # Fail fast
            if status is not 0 and status is not None:
                LOG.error(output)
                util.sysexit(status)

        if self.args.get('--destroy') == 'always':
            c = molecule.command.destroy.Destroy(command_args, args)
            c.execute()
            return None, None

        if self.args.get('--destroy') == 'never':
            return None, None

        # passing (default)
        if status is None:
            c = molecule.command.destroy.Destroy(command_args, args)
            c.execute()
            return None, None

        # error encountered during test
        util.sysexit(status)
Esempio n. 4
0
    def execute(self):
        command_args, args = util.remove_args(self.command_args, self.args,
                                              self.command_args)

        for task in self.molecule.config.config['molecule']['test'][
                'sequence']:
            command_module = getattr(molecule.command, task)
            command = getattr(command_module, task.capitalize())
            c = command(command_args, args, self.molecule)

            for argument in self.command_args:
                if argument in c.args:
                    c.args[argument] = self.args[argument]

            status, output = c.execute(exit=False)

            # Fail fast
            if status is not 0 and status is not None:
                LOG.error(output)
                util.sysexit(status)

        if self.args.get('--destroy') == 'always':
            c = molecule.command.destroy.Destroy(command_args, args)
            c.execute()
            return None, None

        if self.args.get('--destroy') == 'never':
            return None, None

        # passing (default)
        if status is None:
            c = molecule.command.destroy.Destroy(command_args, args)
            c.execute()
            return None, None

        # error encountered during test
        util.sysexit(status)
Esempio n. 5
0
    def execute(self,
                idempotent=False,
                create_instances=True,
                create_inventory=True,
                exit=True,
                hide_errors=True):
        """
        :param idempotent: Optionally provision servers quietly so output can be parsed for idempotence
        :param create_inventory: Toggle inventory creation
        :param create_instances: Toggle instance creation
        :return: Provisioning output
        """
        if self.molecule._state.created:
            create_instances = False

        if self.molecule._state.converged:
            create_inventory = False

        if self.molecule._state.multiple_platforms:
            self.args['--platform'] = 'all'
        else:
            if self.args[
                    '--platform'] == 'all' and self.molecule._state.created:
                create_instances = True
                create_inventory = True

        if create_instances and not idempotent:
            command_args, args = util.remove_args(self.command_args, self.args,
                                                  ['--tags'])
            c = create.Create(command_args, args, self.molecule)
            c.execute()

        if create_inventory:
            self.molecule._create_inventory_file()

        # install role dependencies only during `molecule converge`
        if not idempotent and 'requirements_file' in self.molecule.config.config[
                'ansible'] and not self.molecule._state.installed_deps:
            galaxy = ansible_galaxy.AnsibleGalaxy(self.molecule.config.config)
            galaxy.install()
            self.molecule._state.change_state('installed_deps', True)

        ansible = ansible_playbook.AnsiblePlaybook(self.molecule.config.config[
            'ansible'])

        # params to work with driver
        for k, v in self.molecule._driver.ansible_connection_params.items():
            ansible.add_cli_arg(k, v)

        # target tags passed in via CLI
        if self.molecule._args.get('--tags'):
            ansible.add_cli_arg('tags', self.molecule._args['--tags'].pop(0))

        if idempotent:
            ansible.remove_cli_arg('_out')
            ansible.remove_cli_arg('_err')
            ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true')
            ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false')

            # Save the previous callback plugin if any.
            callback_plugin = ansible.env.get('ANSIBLE_CALLBACK_PLUGINS', '')

            # Set the idempotence plugin.
            if callback_plugin:
                ansible.add_env_arg(
                    'ANSIBLE_CALLBACK_PLUGINS',
                    callback_plugin + ':' + os.path.join(
                        sys.prefix,
                        'share/molecule/ansible/plugins/callback/idempotence'))
            else:
                ansible.add_env_arg('ANSIBLE_CALLBACK_PLUGINS', os.path.join(
                    sys.prefix,
                    'share/molecule/ansible/plugins/callback/idempotence'))

        ansible.bake()
        if self.molecule._args.get('--debug'):
            ansible_env = {k: v
                           for (k, v) in ansible.env.items() if 'ANSIBLE' in k}
            other_env = {k: v
                         for (k, v) in ansible.env.items()
                         if 'ANSIBLE' not in k}
            util.debug('OTHER ENVIRONMENT',
                       yaml.dump(other_env,
                                 default_flow_style=False,
                                 indent=2))
            util.debug('ANSIBLE ENVIRONMENT',
                       yaml.dump(ansible_env,
                                 default_flow_style=False,
                                 indent=2))
            util.debug('ANSIBLE PLAYBOOK', str(ansible.ansible))

        util.print_info("Starting Ansible Run ...")
        status, output = ansible.execute(hide_errors=hide_errors)
        if status is not None:
            if exit:
                util.sysexit(status)
            return status, None

        if not self.molecule._state.converged:
            self.molecule._state.change_state('converged', True)

        return None, output
Esempio n. 6
0
    def execute(self,
                idempotent=False,
                create_instances=True,
                create_inventory=True,
                exit=True,
                hide_errors=True):
        """
        Execute the actions necessary to perform a `molecule converge` and
        return a tuple.

        :param idempotent: An optional flag to perform the converge again, and
         parse the output for idempotence.
        :param create_inventory: An optional flag to toggle inventory creation.
        :param create_instances: An optional flag to toggle instance creation.
        :return: Return a tuple of (`exit status`, `command output`), otherwise
         sys.exit on command failure.
        """
        if self.molecule.state.created:
            create_instances = False

        if self.molecule.state.converged:
            create_inventory = False

        if self.molecule.state.multiple_platforms:
            self.args['--platform'] = 'all'
        else:
            if self.args['--platform'] == 'all' and self.molecule.state.created:
                create_instances = True
                create_inventory = True

        if create_instances and not idempotent:
            command_args, args = util.remove_args(self.command_args, self.args,
                                                  ['--tags'])
            c = create.Create(command_args, args, self.molecule)
            c.execute()

        if create_inventory:
            self.molecule.create_inventory_file()

        # Install role dependencies only during `molecule converge`
        if not idempotent and 'requirements_file' in self.molecule.config.config[
                'ansible'] and not self.molecule.state.installed_deps:
            galaxy = ansible_galaxy.AnsibleGalaxy(self.molecule.config.config)
            galaxy.install()
            self.molecule.state.change_state('installed_deps', True)

        ansible = ansible_playbook.AnsiblePlaybook(
            self.molecule.config.config['ansible'])

        # Params to work with driver
        for k, v in self.molecule.driver.ansible_connection_params.items():
            ansible.add_cli_arg(k, v)

        # Target tags passed in via CLI
        if self.molecule.args.get('--tags'):
            ansible.add_cli_arg('tags', self.molecule.args['--tags'].pop(0))

        if idempotent:
            # Don't log stdout/err
            ansible.remove_cli_arg('_out')
            ansible.remove_cli_arg('_err')
            # Disable color for regexp
            ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true')
            ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false')

        ansible.bake()
        if self.molecule.args.get('--debug'):
            ansible_env = {
                k: v
                for (k, v) in ansible.env.items() if 'ANSIBLE' in k
            }
            other_env = {
                k: v
                for (k, v) in ansible.env.items() if 'ANSIBLE' not in k
            }
            util.debug(
                'OTHER ENVIRONMENT',
                yaml.dump(other_env, default_flow_style=False, indent=2))
            util.debug(
                'ANSIBLE ENVIRONMENT',
                yaml.dump(ansible_env, default_flow_style=False, indent=2))
            util.debug('ANSIBLE PLAYBOOK', str(ansible._ansible))

        util.print_info('Starting Ansible Run ...')
        status, output = ansible.execute(hide_errors=hide_errors)
        if status is not None:
            if exit:
                util.sysexit(status)
            return status, None

        if not self.molecule.state.converged:
            self.molecule.state.change_state('converged', True)

        return None, output