예제 #1
0
 def _load_properties_groupvars(self):
     group_dir = get_group_vars_dir()
     groupnames = self._inventory.get_groupnames()
     for groupfile in os.listdir(group_dir):
         if groupfile not in groupnames:
             # skip any files that don't match existing groups
             continue
         with open(os.path.join(group_dir, groupfile)) as group_data:
             group_contents = yaml.safe_load(group_data)
             if group_contents is None:
                 continue
             props = []
             for key, value in group_contents.items():
                 overrides = False
                 override_flags = OverrideFlags()
                 if key in self.unique_override_flags:
                     override_flags = self.unique_override_flags[key]
                 orig_value = None
                 if key in self.unique_global_props:
                     overrides = True
                     override_flags.ovr_group = True
                     self.unique_override_flags[key] = override_flags
                     orig_value = self.unique_global_props[key].value
                 ansible_prop = AnsibleProperty(key, value, groupfile,
                                                overrides, orig_value,
                                                'group', groupfile)
                 props.append(ansible_prop)
         self.group_props[groupfile] = props
예제 #2
0
    def remove_group(self, groupname):
        if groupname in PROTECTED_GROUPS:
            raise InvalidArgument(
                u._('Cannot remove {group} group. It is required by kolla.').
                format(group=groupname))

        # remove group from services
        for service in self._services.values():
            service.remove_groupname(groupname)

        group_vars = os.path.join(get_group_vars_dir(), groupname)
        if os.path.exists(group_vars) and groupname != '__GLOBAL__':
            os.remove(group_vars)

        if groupname in self._groups:
            del self._groups[groupname]
예제 #3
0
 def set_group_property(self, property_dict, groups):
     # if groups is None set the property on all hosts
     self._load_inventory()
     group_list = []
     if groups is None:
         group_list = self._inventory.get_groups()
     else:
         for group_name in groups:
             group = self._inventory.get_group(group_name)
             if group is None:
                 raise NotInInventory(u._('Group'), group_name)
             group_list.append(group)
     try:
         for group in group_list:
             tmp_dict = copy.copy(property_dict)
             file_path = os.path.join(get_group_vars_dir(), group.name)
             change_property(file_path, tmp_dict, clear=False)
     except Exception as e:
         raise e
예제 #4
0
    def test_properties(self):
        # test global properties
        self._properties_test()

        # test single group vars
        group = 'prop_test_group1'
        self.run_cli_cmd('group add %s' % group)
        self._properties_test(groups=['control'])

        # test single host vars
        host = 'prop_test_host1'
        self.run_cli_cmd('host add %s' % host)
        self._properties_test(hosts=[host])

        # test multiple group vars
        groups = [group]
        group = 'prop_test_group2'
        groups.append(group)
        self.run_cli_cmd('group add %s' % group)
        self._properties_test(groups=groups)

        # test multiple host vars
        hosts = [host]
        host = 'prop_test_host2'
        hosts.append(host)
        self.run_cli_cmd('host add %s' % host)
        self._properties_test(hosts=hosts)

        # test all group vars
        self._properties_test(groups=['all'])

        # test all host vars
        self._properties_test(hosts=['all'])

        # test property override output
        ovr_key = 'enable_haproxy'
        ovr_value = 'no'

        # clear property values before test
        self.run_cli_cmd('property clear %s' % ovr_key)
        self.run_cli_cmd('property clear %s --host=all' % ovr_key)
        self.run_cli_cmd('property clear %s --group=all' % ovr_key)

        # global property override test
        self.run_cli_cmd('property set %s %s' % (ovr_key, ovr_value))
        json_str = self.run_cli_cmd('property list -f json')
        msg = self._override_test(json_str, ovr_key, ovr_value, '*--')
        self.assertEqual(msg, '', 'override check failed: %s' % msg)

        # host property override test
        self.run_cli_cmd('property set %s %s --host=%s' %
                         (ovr_key, ovr_value, host))
        json_str = self.run_cli_cmd('property list -f json --host=%s' % host)
        msg = self._override_test(json_str,
                                  ovr_key,
                                  ovr_value,
                                  '*-H',
                                  host=host)
        self.assertEqual(msg, '', 'host override check failed: %s' % msg)

        # group property override test
        self.run_cli_cmd('property set %s %s --group=%s' %
                         (ovr_key, ovr_value, group))
        json_str = self.run_cli_cmd('property list -f json --group=%s' % group)
        msg = self._override_test(json_str,
                                  ovr_key,
                                  ovr_value,
                                  '*GH',
                                  group=group)
        self.assertEqual(msg, '', 'group override check failed: %s' % msg)

        # check that group_var files are deleted
        # when groups are deleted
        for group in groups:
            path = os.path.join(get_group_vars_dir(), group)
            self.assertTrue(os.path.exists(path))
            self.run_cli_cmd('group remove %s' % group)
            self.assertFalse(os.path.exists(path))

        # check that host_var files are deleted
        # when hosts are deleted
        for host in hosts:
            path = os.path.join(get_host_vars_dir(), host)
            self.assertTrue(os.path.exists(path))
            self.run_cli_cmd('host remove %s' % host)
            self.assertFalse(os.path.exists(path))