def _commit_files(files, result, module): result['changed'] = len(files) > 0 if not result['changed']: result['message'] = "No files need to be unset" if module.check_mode: if len(files) > 0: result['message'] = "These files will be commited: {}".format(" ".join(files)) return if not result['changed']: return startd = datetime.datetime.now() ucr = ConfigRegistry() ucr.load() ucr_handlers = configHandlers() ucr_handlers.load() ucr_handlers.update() ucr_handlers.commit(ucr, files) endd = datetime.datetime.now() result['start'] = str(startd) result['end'] = str(endd) result['delta'] = str(endd - startd) result['meta']['commited_templates'] = files result['message'] = "These files were be commited: {}".format(" ".join(files)) result['failed'] = 0
def test_context(self, ucr0): with ucr0: ucr0["foo"] = "bar" ucr = ConfigRegistry() ucr.load() assert ucr['foo'] == 'bar'
def handler_filter(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() stdout = sys.stdout if six.PY2 else sys.stdout.buffer # type: IO[bytes] # type: ignore stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
def handler_info(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> Iterator[str] """ Print variable info. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() # Import located here, because on module level, a circular import would be # created import univention.config_registry_info as cri # pylint: disable-msg=W0403 cri.set_language('en') info = cri.ConfigRegistryInfo(install_mode=False) for arg in args: try: yield variable_info_string(arg, ucr.get(arg, None), info.get_variable(arg), details=_SHOW_EMPTY | _SHOW_DESCRIPTION | _SHOW_CATEGORIES) except UnknownKeyException as ex: print(ex, file=sys.stderr)
def test_save_load(self, ucr0): """Save and re-load UCR.""" ucr0['foo'] = 'bar' ucr0.save() ucr = ConfigRegistry() ucr.load() assert ucr['foo'] == 'bar'
def handler_commit(args, opts=dict()): """Commit all registered templated files.""" ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() handlers.load() handlers.commit(ucr, args)
def handler_commit(args, opts=dict()): """Commit all registered templated files.""" ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() handlers.load() handlers.commit(ucr, args)
def handler_unregister(args, opts=dict()): """Unregister old info file.""" ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() cur = handlers.update() # cache must be current obsolete = handlers.unregister(args[0], ucr) handlers.update_divert(cur - obsolete)
def test_save_load(self): """Save and re-load UCR.""" ucr = ConfigRegistry() ucr['foo'] = 'bar' ucr.save() ucr = ConfigRegistry() ucr.load() self.assertEqual(ucr['foo'], 'bar')
def handler_unregister(args, opts=dict()): """Unregister old info file.""" ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() cur = handlers.update() # cache must be current obsolete = handlers.unregister(args[0], ucr) handlers.update_divert(cur - obsolete)
def test_context_error(self, ucr0): ex = ValueError() with pytest.raises(ValueError) as exc_info, ucr0: ucr0["foo"] = "bar" raise ex assert exc_info.value is ex ucr = ConfigRegistry() ucr.load() assert ucr['foo'] is None
def handler_get(args, opts=dict()): """Return config registry variable.""" ucr = ConfigRegistry() ucr.load() if not args[0] in ucr: return if OPT_FILTERS['shell'][2]: print '%s: %s' % (args[0], ucr.get(args[0], '')) else: print ucr.get(args[0], '')
def handler_get(args, opts=dict()): """Return config registry variable.""" ucr = ConfigRegistry() ucr.load() if not args[0] in ucr: return if OPT_FILTERS['shell'][2]: print '%s: %s' % (args[0], ucr.get(args[0], '')) else: print ucr.get(args[0], '')
def handler_register(args, opts=dict()): """Register new info file.""" ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() handlers.update() # cache must be current # Bug #21263: by forcing an update here, the new .info file is already # incorporated. Calling register for multifiles will increment the # def_count a second time, which is not nice, but uncritical, since the # diversion is (re-)done when >= 1. handlers.register(args[0], ucr)
def handler_register(args, opts=dict()): """Register new info file.""" ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() handlers.update() # cache must be current # Bug #21263: by forcing an update here, the new .info file is already # incorporated. Calling register for multifiles will increment the # def_count a second time, which is not nice, but uncritical, since the # diversion is (re-)done when >= 1. handlers.register(args[0], ucr)
def _setup_layers(): ucr = ConfigRegistry(write_registry=ConfigRegistry.FORCED) ucr['foo'] = 'FORCED' ucr['bar'] = 'FORCED' ucr.save() ucr = ConfigRegistry() ucr['bar'] = 'NORMAL' ucr['baz'] = 'NORMAL' ucr.save() ucr = ConfigRegistry() ucr.load() return ucr
def handler_dump(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> Iterator[str] """ Dump all variables. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() for line in str(ucr).split('\n'): yield line
def test_layering(self, levels): """Check layer priorities 50layer-priority""" msb = 0 for layer in range(4): if levels & (1 << layer): ucr = ConfigRegistry(write_registry=layer) ucr["key"] = str(layer) ucr.save() msb = layer ucr = ConfigRegistry() ucr.load() assert ucr["key"] == (str(msb) if levels else None)
def handler_commit(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """ Commit all registered templated files. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() handlers.load() handlers.commit(ucr, args)
def handler_unregister(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """ Unregister old `.info` file. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() cur = handlers.update() # cache must be current obsolete = handlers.unregister(args[0], ucr) handlers.update_divert(cur - obsolete)
def _set_keys(keys, result, module): ucr = ConfigRegistry() ucr.load() def needs_change(key): if key not in ucr: return True if isinstance(keys[key], bool): if keys[key] and not ucr.is_true(key): return True elif not keys[key] and not ucr.is_false(key): return True elif ucr[key] != keys[key]: return True return False to_set = list(filter(needs_change, keys)) result['changed'] = len(to_set) > 0 if not result['changed']: result['message'] = "No keys need to be set" if module.check_mode: if len(to_set) > 0: result['message'] = "These keys need to be set: {}".format(" ".join(to_set)) return if not result['changed']: return args = ["/usr/sbin/univention-config-registry", "set"] + ["{0}={1}".format(key, keys[key]) for key in to_set] startd = datetime.datetime.now() rc, out, err = module.run_command(args) endd = datetime.datetime.now() result['start'] = str(startd) result['end'] = str(endd) result['delta'] = str(endd - startd) result['out'] = out.rstrip("\r\n") result['err'] = err.rstrip("\r\n") result['rc'] = rc result['message'] = "These keys were set: {}".format(" ".join(to_set)) result['meta']['changed_keys'] = to_set result['failed'] = rc != 0 or len(err) > 0 if rc != 0: module.fail_json(msg='non-zero return code', **result)
def handler_info(args, opts=dict()): """Print variable info.""" reg = ConfigRegistry() reg.load() # Import located here, because on module level, a circular import would be # created import univention.config_registry_info as cri # pylint: disable-msg=W0403 cri.set_language('en') info = cri.ConfigRegistryInfo(install_mode=False) for arg in args: try: print_variable_info_string(arg, reg.get(arg, None), info.get_variable(arg), details=_SHOW_EMPTY | _SHOW_DESCRIPTION | _SHOW_CATEGORIES) except UnknownKeyException, ex: print >> sys.stderr, ex
def handler_get(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> Iterator[str] """ Return config registry variable. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() if not args[0] in ucr: return if OPT_FILTERS['shell'][2]: yield '%s: %s' % (args[0], ucr.get(args[0], '')) else: yield ucr.get(args[0], '')
def __init__(self, ucr=None): # type: (ConfigRegistry) -> None if ucr is None: ucr = ConfigRegistry() ucr.load() if isinstance(ucr, ConfigRegistry): ucr = VengefulConfigRegistry(ucr) self.primary = ucr.get('interfaces/primary', 'eth0') try: self.ipv4_gateway = IPv4Address( u"%(gateway)s" % ucr) # type: Union[IPv4Address, None, bool] except KeyError: self.ipv4_gateway = None except ValueError: self.ipv4_gateway = False try: # <https://tools.ietf.org/html/rfc4007#section-11> # As a common notation to specify the scope zone, an # implementation SHOULD support the following format: # <address>%<zone_id> parts = ucr['ipv6/gateway'].rsplit('%', 1) gateway = parts.pop(0) zone_index = parts[0] if parts else None self.ipv6_gateway = IPv6Address( u"%s" % (gateway, )) # type: Union[IPv6Address, None, bool] self.ipv6_gateway_zone_index = zone_index except KeyError: self.ipv6_gateway = None self.ipv6_gateway_zone_index = None except ValueError: self.ipv6_gateway = False self.ipv6_gateway_zone_index = None self._all_interfaces = {} # type: Dict[str, _Iface] for key, value in ucr.items(): if not value: continue match = RE_IFACE.match(key) if not match: continue iface, subkey, ipv6_name = match.groups() data = self._all_interfaces.setdefault(iface, _Iface(name=iface)) data[subkey] = value if ipv6_name: data.ipv6_names.add(ipv6_name)
def handler_register(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """ Register new `.info` file. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() handlers = ConfigHandlers() handlers.update() # cache must be current # Bug #21263: by forcing an update here, the new .info file is already # incorporated. Calling register for multifiles will increment the # def_count a second time, which is not nice, but uncritical, since the # diversion is (re-)done when >= 1. handlers.register(args[0], ucr)
def handler_get(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> Iterator[str] """ Return config registry variable. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() key = args[0] value = ucr.get(key) if value is None: return elif OPT_FILTERS['shell'][2]: yield '%s: %s' % (key, value) else: yield value
def handler_unset(args, opts=dict()): """ Unset config registry variables in args. """ current_scope = ConfigRegistry.NORMAL reg = None if opts.get('ldap-policy', False): current_scope = ConfigRegistry.LDAP reg = ConfigRegistry(write_registry=current_scope) elif opts.get('force', False): current_scope = ConfigRegistry.FORCED reg = ConfigRegistry(write_registry=current_scope) elif opts.get('schedule', False): current_scope = ConfigRegistry.SCHEDULE reg = ConfigRegistry(write_registry=current_scope) else: reg = ConfigRegistry() reg.lock() try: reg.load() handlers = ConfigHandlers() handlers.load() changed = {} for arg in args: if reg.has_key(arg, write_registry_only=True): oldvalue = reg[arg] print 'Unsetting %s' % arg del reg[arg] changed[arg] = (oldvalue, '') k = reg.get(arg, None, getscope=True) replog('unset', current_scope, reg, arg, oldvalue) if k and k[0] > current_scope: print >> sys.stderr, \ 'W: %s is still set in scope "%s"' % \ (arg, SCOPE[k[0]]) else: msg = "W: The config registry variable '%s' does not exist" print >> sys.stderr, msg % (arg,) reg.save() finally: reg.unlock() handlers(changed.keys(), (reg, changed))
def handler_unset(args, opts=dict()): """ Unset config registry variables in args. """ current_scope = ConfigRegistry.NORMAL reg = None if opts.get('ldap-policy', False): current_scope = ConfigRegistry.LDAP reg = ConfigRegistry(write_registry=current_scope) elif opts.get('force', False): current_scope = ConfigRegistry.FORCED reg = ConfigRegistry(write_registry=current_scope) elif opts.get('schedule', False): current_scope = ConfigRegistry.SCHEDULE reg = ConfigRegistry(write_registry=current_scope) else: reg = ConfigRegistry() reg.lock() try: reg.load() handlers = ConfigHandlers() handlers.load() changed = {} for arg in args: if reg.has_key(arg, write_registry_only=True): oldvalue = reg[arg] print 'Unsetting %s' % arg del reg[arg] changed[arg] = (oldvalue, '') k = reg.get(arg, None, getscope=True) replog('unset', current_scope, reg, arg, oldvalue) if k and k[0] > current_scope: print >> sys.stderr, \ 'W: %s is still set in scope "%s"' % \ (arg, SCOPE[k[0]]) else: msg = "W: The config registry variable '%s' does not exist" print >> sys.stderr, msg % (arg, ) reg.save() finally: reg.unlock() handlers(changed.keys(), (reg, changed))
def handler_info(args, opts=dict()): """Print variable info.""" ucr = ConfigRegistry() ucr.load() # Import located here, because on module level, a circular import would be # created import univention.config_registry_info as cri # pylint: disable-msg=W0403 cri.set_language('en') info = cri.ConfigRegistryInfo(install_mode=False) for arg in args: try: print_variable_info_string(arg, ucr.get(arg, None), info.get_variable(arg), details=_SHOW_EMPTY | _SHOW_DESCRIPTION | _SHOW_CATEGORIES) except UnknownKeyException as ex: print >> sys.stderr, ex
def handler_info(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> Iterator[str] """ Print variable info. :param args: Command line arguments. :param opts: Command line options. """ ucr = ConfigRegistry() ucr.load() info = _get_config_registry_info() for arg in args: try: yield variable_info_string(arg, ucr.get(arg, None), info.get_variable(arg), details=_SHOW_EMPTY | _SHOW_DESCRIPTION | _SHOW_CATEGORIES | _SHOW_DEFAULT) except UnknownKeyException as ex: print(ex, file=sys.stderr)
def _register_variable_default_values(ucr): # type: (ConfigRegistry) -> None """Create base-default.conf layer containig all default values""" info = _get_config_registry_info() _ucr = ConfigRegistry(write_registry=ConfigRegistry.DEFAULTS) _ucr.load() defaults = {} # type: Dict[str, Optional[str]] default_variables = info.get_variables() for key, variable in default_variables.items(): value = variable.get('default') if value: defaults[key] = value for key in _ucr: if key not in default_variables: defaults[key] = None changed = dict((key, (old, new)) for key, (old, new) in _ucr.update(defaults).items() if old != new) _ucr.save() ucr.load() _run_changed(ucr, changed, 'I: %s will be set in scope "%s"')
def _unset_keys(keys, result, module): ucr = ConfigRegistry() ucr.load() to_unset = [key for key in keys if key in ucr] result['changed'] = len(to_unset) > 0 if not result['changed']: result['message'] = "No keys need to be unset" if module.check_mode: if len(to_unset) > 0: result['message'] = "These keys need to be unset: {}".format(" ".join(to_unset)) return if not result['changed']: return args = ["/usr/sbin/univention-config-registry", "unset"] + to_unset startd = datetime.datetime.now() rc, out, err = module.run_command(args) endd = datetime.datetime.now() result['start'] = str(startd) result['end'] = str(endd) result['delta'] = str(endd - startd) result['out'] = out.rstrip("\r\n") result['err'] = err.rstrip("\r\n") result['rc'] = rc result['message'] = "These keys were unset: {}".format(" ".join(to_unset)) result['meta']['changed_keys'] = to_unset result['failed'] = rc != 0 if rc != 0: module.fail_json(msg='non-zero return code', **result)
def handler_dump(args, opts=dict()): """Dump all variables.""" ucr = ConfigRegistry() ucr.load() for line in str(ucr).split('\n'): print line
def handler_search(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> Iterator[str] """ Search for registry variable. :param args: Command line arguments. :param opts: Command line options. """ search_keys = opts.get('key', False) search_values = opts.get('value', False) search_all = opts.get('all', False) count_search = int(search_keys) + int(search_values) + int(search_all) if count_search > 1: print('E: at most one out of [--key|--value|--all] may be set', file=sys.stderr) sys.exit(1) elif count_search == 0: search_keys = True search_values |= search_all search_keys |= search_all if not args: regex = [re.compile('')] else: try: regex = [re.compile(_) for _ in args] except re.error as ex: print('E: invalid regular expression: %s' % (ex,), file=sys.stderr) sys.exit(1) # Import located here, because on module level, a circular import would be # created import univention.config_registry_info as cri # pylint: disable-msg=W0403 cri.set_language('en') info = cri.ConfigRegistryInfo(install_mode=False) category = opts.get('category', None) if category and not info.get_category(category): print('E: unknown category: "%s"' % (category,), file=sys.stderr) sys.exit(1) ucr = ConfigRegistry() ucr.load() details = _SHOW_EMPTY | _SHOW_DESCRIPTION if opts.get('non-empty', False): details &= ~_SHOW_EMPTY if opts.get('brief', False) or ucr.is_true('ucr/output/brief', False): details &= ~_SHOW_DESCRIPTION if ucr.is_true('ucr/output/scope', False): details |= _SHOW_SCOPE if opts.get('verbose', False): details |= _SHOW_CATEGORIES | _SHOW_DESCRIPTION all_vars = {} # type: Dict[str, Tuple[Optional[str], Optional[cri.Variable], Optional[str]]] # key: (value, vinfo, scope) for key, var in info.get_variables(category).items(): all_vars[key] = (None, var, None) for key, (scope, value) in ucr.items(getscope=True): try: all_vars[key] = (value, all_vars[key][1], scope) except LookupError: all_vars[key] = (value, None, scope) for key, (value2, vinfo, scope2) in all_vars.items(): for reg in regex: if any(( search_keys and reg.search(key), search_values and value2 and reg.search(value2), search_all and vinfo and reg.search(vinfo.get('description', '')) )): yield variable_info_string(key, value2, vinfo, details=details) break if _SHOW_EMPTY & details and not OPT_FILTERS['shell'][2]: patterns = {} # type: Dict for arg in args or ('',): patterns.update(info.describe_search_term(arg)) for pattern, vinfo in patterns.items(): yield variable_info_string(pattern, None, vinfo, details=details)
def handler_filter(args, opts=dict()): # type: (List[str], Dict[str, Any]) -> None """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() sys.stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
def handler_dump(args, opts=dict()): """Dump all variables.""" ucr = ConfigRegistry() ucr.load() for line in str(ucr).split('\n'): print line
def handler_filter(args, opts=dict()): """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() sys.stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
def handler_set(args, opts=dict(), quiet=False): """ Set config registry variables in args. Args is an array of strings 'key=value' or 'key?value'. """ handlers = ConfigHandlers() handlers.load() current_scope = ConfigRegistry.NORMAL reg = None if opts.get('ldap-policy', False): current_scope = ConfigRegistry.LDAP reg = ConfigRegistry(write_registry=current_scope) elif opts.get('force', False): current_scope = ConfigRegistry.FORCED reg = ConfigRegistry(write_registry=current_scope) elif opts.get('schedule', False): current_scope = ConfigRegistry.SCHEDULE reg = ConfigRegistry(write_registry=current_scope) else: reg = ConfigRegistry() reg.lock() try: reg.load() changed = {} for arg in args: sep_set = arg.find('=') # set sep_def = arg.find('?') # set if not already set if sep_set == -1 and sep_def == -1: print >> sys.stderr, \ "W: Missing value for config registry variable '%s'" % \ (arg,) continue else: if sep_set > 0 and sep_def == -1: sep = sep_set elif sep_def > 0 and sep_set == -1: sep = sep_def else: sep = min(sep_set, sep_def) key = arg[0:sep] value = arg[sep + 1:] old = reg.get(key) if (old is None or sep == sep_set) and validate_key(key): if not quiet: if reg.has_key(key, write_registry_only=True): print 'Setting %s' % key else: print 'Create %s' % key k = reg.get(key, None, getscope=True) if k and k[0] > current_scope: print >> sys.stderr, \ 'W: %s is overridden by scope "%s"' % \ (key, SCOPE[k[0]]) reg[key] = value changed[key] = (old, value) replog('set', current_scope, reg, key, old, value) else: if not quiet: if old is not None: print 'Not updating %s' % key else: print 'Not setting %s' % key reg.save() finally: reg.unlock() handlers(changed.keys(), (reg, changed))
def handler_filter(args, opts=dict()): """Run filter on STDIN to STDOUT.""" ucr = ConfigRegistry() ucr.load() sys.stdout.write(run_filter(sys.stdin.read(), ucr, opts=opts))
print >> sys.stderr, 'E: invalid regular expression: %s' % (ex,) sys.exit(1) # Import located here, because on module level, a circular import would be # created import univention.config_registry_info as cri # pylint: disable-msg=W0403 cri.set_language('en') info = cri.ConfigRegistryInfo(install_mode=False) category = opts.get('category', None) if category and not info.get_category(category): print >> sys.stderr, 'E: unknown category: "%s"' % (category,) sys.exit(1) ucr = ConfigRegistry() ucr.load() details = _SHOW_EMPTY | _SHOW_DESCRIPTION if opts.get('non-empty', False): details &= ~_SHOW_EMPTY if opts.get('brief', False) or ucr.is_true('ucr/output/brief', False): details &= ~_SHOW_DESCRIPTION if ucr.is_true('ucr/output/scope', False): details |= _SHOW_SCOPE if opts.get('verbose', False): details |= _SHOW_CATEGORIES | _SHOW_DESCRIPTION all_vars = {} # key: (value, vinfo, scope) for key, var in info.get_variables(category).items(): all_vars[key] = (None, var, None) for key, (scope, value) in ucr.items(getscope=True):