Beispiel #1
0
def install_galaxy_roles(parsed_args, force=False):
    """Install Ansible Galaxy role dependencies.

    Installs dependencies specified in kayobe, and if present, in kayobe
    configuration.

    :param parsed_args: Parsed command line arguments.
    :param force: Whether to force reinstallation of roles.
    """
    LOG.info("Installing galaxy role dependencies from kayobe")
    requirements = utils.get_data_files_path("requirements.yml")
    roles_destination = utils.get_data_files_path('ansible', 'roles')
    utils.galaxy_install(requirements, roles_destination, force=force)

    # Check for requirements in kayobe configuration.
    kc_reqs_path = os.path.join(parsed_args.config_path, "ansible",
                                "requirements.yml")
    if not utils.is_readable_file(kc_reqs_path)["result"]:
        LOG.info("Not installing galaxy role dependencies from kayobe config "
                 "- requirements.yml not present")
        return

    LOG.info("Installing galaxy role dependencies from kayobe config")
    # Ensure a roles directory exists in kayobe-config.
    kc_roles_path = os.path.join(parsed_args.config_path, "ansible", "roles")
    try:
        os.makedirs(kc_roles_path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise exception.Error("Failed to create directory ansible/roles/ "
                                  "in kayobe configuration at %s: %s" %
                                  (parsed_args.config_path, str(e)))

    # Install roles from kayobe-config.
    utils.galaxy_install(kc_reqs_path, kc_roles_path, force=force)
Beispiel #2
0
def config_dump(parsed_args, host=None, hosts=None, var_name=None,
                facts=None, extra_vars=None, tags=None, verbose_level=None):
    dump_dir = tempfile.mkdtemp()
    try:
        if not extra_vars:
            extra_vars = {}
        extra_vars["dump_path"] = dump_dir
        if host or hosts:
            extra_vars["dump_hosts"] = host or hosts
        if var_name:
            extra_vars["dump_var_name"] = var_name
        if facts is not None:
            extra_vars["dump_facts"] = facts
        # Don't use check mode for configuration dumps as we won't get any
        # results back.
        playbook_path = utils.get_data_files_path("ansible", "dump-config.yml")
        run_playbook(parsed_args, playbook_path,
                     extra_vars=extra_vars, tags=tags, check_output=True,
                     verbose_level=verbose_level, check=False)
        hostvars = {}
        for path in os.listdir(dump_dir):
            LOG.debug("Found dump file %s", path)
            inventory_hostname, ext = os.path.splitext(path)
            if ext == ".yml":
                hvars = utils.read_yaml_file(os.path.join(dump_dir, path))
                if host:
                    return hvars
                else:
                    hostvars[inventory_hostname] = hvars
            else:
                LOG.warning("Unexpected extension on config dump file %s",
                            path)
        return hostvars
    finally:
        shutil.rmtree(dump_dir)
Beispiel #3
0
    def test_install_galaxy_roles(self, mock_mkdirs, mock_is_readable,
                                  mock_install):
        parser = argparse.ArgumentParser()
        ansible.add_args(parser)
        parsed_args = parser.parse_args([])
        mock_is_readable.return_value = {"result": False}

        ansible.install_galaxy_roles(parsed_args)

        mock_install.assert_called_once_with(
            utils.get_data_files_path("requirements.yml"),
            utils.get_data_files_path("ansible", "roles"),
            force=False)
        mock_is_readable.assert_called_once_with(
            "/etc/kayobe/ansible/requirements.yml")
        self.assertFalse(mock_mkdirs.called)
Beispiel #4
0
 def test_config_dump(self, mock_mkdtemp, mock_run, mock_listdir, mock_read,
                      mock_rmtree):
     parser = argparse.ArgumentParser()
     parsed_args = parser.parse_args([])
     dump_dir = "/path/to/dump"
     mock_mkdtemp.return_value = dump_dir
     mock_listdir.return_value = ["host1.yml", "host2.yml"]
     mock_read.side_effect = [{"var1": "value1"}, {"var2": "value2"}]
     result = ansible.config_dump(parsed_args)
     expected_result = {
         "host1": {
             "var1": "value1"
         },
         "host2": {
             "var2": "value2"
         },
     }
     self.assertEqual(result, expected_result)
     dump_config_path = utils.get_data_files_path("ansible",
                                                  "dump-config.yml")
     mock_run.assert_called_once_with(parsed_args,
                                      dump_config_path,
                                      extra_vars={
                                          "dump_path": dump_dir,
                                      },
                                      check_output=True,
                                      tags=None,
                                      verbose_level=None,
                                      check=False)
     mock_rmtree.assert_called_once_with(dump_dir)
     mock_listdir.assert_any_call(dump_dir)
     mock_read.assert_has_calls([
         mock.call(os.path.join(dump_dir, "host1.yml")),
         mock.call(os.path.join(dump_dir, "host2.yml")),
     ])
Beispiel #5
0
    def test_install_galaxy_roles_with_kayobe_config_mkdirs_failure(
            self, mock_mkdirs, mock_is_readable, mock_install):
        parser = argparse.ArgumentParser()
        ansible.add_args(parser)
        parsed_args = parser.parse_args([])
        mock_is_readable.return_value = {"result": True}
        mock_mkdirs.side_effect = OSError(errno.EPERM)

        self.assertRaises(exception.Error, ansible.install_galaxy_roles,
                          parsed_args)

        mock_install.assert_called_once_with(
            utils.get_data_files_path("requirements.yml"),
            utils.get_data_files_path("ansible", "roles"),
            force=False)
        mock_is_readable.assert_called_once_with(
            "/etc/kayobe/ansible/requirements.yml")
        mock_mkdirs.assert_called_once_with("/etc/kayobe/ansible/roles")
Beispiel #6
0
    def test_install_galaxy_roles_with_kayobe_config_forced(
            self, mock_mkdirs, mock_is_readable, mock_install):
        parser = argparse.ArgumentParser()
        ansible.add_args(parser)
        parsed_args = parser.parse_args([])
        mock_is_readable.return_value = {"result": True}

        ansible.install_galaxy_roles(parsed_args, force=True)

        expected_calls = [
            mock.call(utils.get_data_files_path("requirements.yml"),
                      utils.get_data_files_path("ansible", "roles"),
                      force=True),
            mock.call("/etc/kayobe/ansible/requirements.yml",
                      "/etc/kayobe/ansible/roles",
                      force=True)
        ]
        self.assertEqual(expected_calls, mock_install.call_args_list)
        mock_is_readable.assert_called_once_with(
            "/etc/kayobe/ansible/requirements.yml")
        mock_mkdirs.assert_called_once_with("/etc/kayobe/ansible/roles")