Exemplo n.º 1
0
def replog(ucr, var, old_value, value=None):
    """
	This function writes a new entry to replication logfile if
	this feature has been enabled.
	"""
    if ucr.is_true('ucr/replog/enabled', False):
        if value is not None:
            method = 'set'
            varvalue = "%s=%s" % (var, escape_value(value))
        else:
            method = 'unset'
            varvalue = "'%s'" % var

        scope_arg = {
            ConfigRegistry.LDAP: '--ldap-policy ',
            ConfigRegistry.FORCED: '--force ',
            ConfigRegistry.SCHEDULE: '--schedule ',
        }.get(ucr.scope, '')

        if old_value is None:
            old_value = "[Previously undefined]"

        log = '%s: %s %s%s old:%s\n' % (time.strftime("%Y-%m-%d %H:%M:%S"),
                                        method, scope_arg, varvalue, old_value)
        try:
            if not os.path.isfile(REPLOG_FILE):
                os.close(os.open(REPLOG_FILE, os.O_CREAT, 0o640))
            logfile = open(REPLOG_FILE, "a+")
            logfile.write(log)
            logfile.close()
        except EnvironmentError as ex:
            print >> sys.stderr, ("E: exception occurred while writing to " +
                                  "replication log: %s" % (ex, ))
            exception_occured()
Exemplo n.º 2
0
def filter_shell(args, text):  # pylint: disable-msg=W0613
    """Filter output for shell: escape keys."""
    out = []
    for line in text:
        try:
            var, value = line.split(': ', 1)
        except ValueError:
            var = line
            value = ''
        out.append('%s=%s' % (key_shell_escape(var), escape_value(value)))
    return out
def filter_shell(args, text):  # pylint: disable-msg=W0613
    # type: (Any, Iterable[str]) -> Iterable[str]
    """
	Filter output for shell: escape keys.

	:param args: UNUSED.
	:param text: Text as list of lines.
	:returns: Filteres list of lines.
	"""
    out = []
    for line in text:
        try:
            var, value = line.split(': ', 1)
        except ValueError:
            var = line
            value = ''
        out.append('%s=%s' % (key_shell_escape(var), escape_value(value)))
    return out
def replog(ucr, var, old_value, value=None):
    # type: (ConfigRegistry, str, Optional[str], Optional[str]) -> None
    """
	This function writes a new entry to replication logfile if
	this feature has been enabled.

	:param ucr: UCR instance.
	:param var: UCR variable name.
	:param old_value: Old UCR variable value.
	:param value: New UCR variable value. `None` is now unset.
	"""
    if ucr.is_true('ucr/replog/enabled', False):
        if value is not None:
            method = 'set'
            varvalue = "%s=%s" % (var, escape_value(value))
        else:
            method = 'unset'
            varvalue = "'%s'" % var

        scope_arg = {
            ConfigRegistry.LDAP: '--ldap-policy ',
            ConfigRegistry.FORCED: '--force ',
            ConfigRegistry.SCHEDULE: '--schedule ',
        }.get(ucr.scope, '')

        if old_value is None:
            old_value = "[Previously undefined]"

        log = u'%s: %s %s%s old:%s\n' % (time.strftime("%Y-%m-%d %H:%M:%S"),
                                         method, scope_arg, varvalue,
                                         old_value)
        try:
            if not os.path.isfile(REPLOG_FILE):
                os.close(os.open(REPLOG_FILE, os.O_CREAT, 0o640))

            with open(REPLOG_FILE, "a+", encoding='utf-8') as logfile:
                logfile.write(log)
        except EnvironmentError as ex:
            print(
                "E: exception occurred while writing to replication log: %s" %
                (ex, ),
                file=sys.stderr)
            exception_occured()