Esempio n. 1
0
    def remove_host(self, hostname, groupname=None):
        """remove host

        if groupname is none, delete host
        if group name is not none, remove host from group
        """
        changed = False
        if groupname and groupname not in self._groups:
            raise NotInInventory(u._('Group'), groupname)

        if hostname not in self._hosts:
            return changed

        changed = True
        host = self._hosts[hostname]
        groups = self.get_groups(host)
        for group in groups:
            if not groupname or groupname == group.name:
                group.remove_host(host)

        host_vars = os.path.join(get_host_vars_dir(), hostname)
        if os.path.exists(host_vars):
            os.remove(host_vars)

        if not groupname:
            del self._hosts[hostname]
        return changed
Esempio n. 2
0
 def _load_properties_hostvars(self):
     host_dir = get_host_vars_dir()
     hostnames = self._inventory.get_hostnames()
     for hostfile in os.listdir(host_dir):
         if hostfile not in hostnames:
             # skip any host files that don't match existing hosts
             continue
         self.host_props[hostfile] = []
         with open(os.path.join(host_dir, hostfile)) as host_data:
             host_contents = yaml.safe_load(host_data)
             if host_contents is None:
                 continue
             props = []
             for key, value in host_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_host = True
                     self.unique_override_flags[key] = override_flags
                     orig_value = self.unique_global_props[key].value
                 ansible_prop = AnsibleProperty(key, value, hostfile,
                                                overrides, orig_value,
                                                'host', hostfile)
                 props.append(ansible_prop)
         self.host_props[hostfile] = props
Esempio n. 3
0
 def set_host_property(self, property_dict, hosts):
     # if hosts is None set the property on all hosts
     self._load_inventory()
     host_list = []
     if hosts is None:
         host_list = self._inventory.get_hosts()
     else:
         for host_name in hosts:
             host = self._inventory.get_host(host_name)
             if host is None:
                 raise NotInInventory(u._('Host'), host_name)
             host_list.append(host)
     try:
         for host in host_list:
             file_path = os.path.join(get_host_vars_dir(), host.name)
             change_property(file_path, property_dict, clear=False)
     except Exception as e:
         raise e
Esempio n. 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))