Example #1
0
def valid_master_address(value, config_context):
    """Validates and normalizes Mesos master address.

    Must be HTTP or not include a scheme, and only include
    a host, without any path components.
    """
    valid_string(value, config_context)

    # Parse with HTTP as default, only HTTP allowed.
    scheme, netloc, path, params, query, fragment = urlparse(value, 'http')
    if scheme != 'http':
        msg = f"Only HTTP supported for Mesos master address, got {value}"
        raise ConfigError(msg)

    if params or query or fragment:
        msg = f"Mesos master address may not contain path components, got {value}"
        raise ConfigError(msg)

    # Only one of netloc or path allowed, and no / except trailing ones.
    # netloc is empty if there's no scheme, then we try the path.
    path = path.rstrip('/')
    if (netloc and path) or '/' in path:
        msg = f"Mesos master address may not contain path components, got {value}"
        raise ConfigError(msg)

    if not netloc:
        netloc = path

    if not netloc:
        msg = f"Mesos master address is missing host, got {value}"
        raise ConfigError(msg)

    return f'{scheme}://{netloc}'
Example #2
0
def valid_time_zone(tz, config_context):
    if tz is None:
        return None
    valid_string(tz, config_context)
    try:
        return pytz.timezone(tz)
    except pytz.exceptions.UnknownTimeZoneError:
        raise ConfigError('%s is not a valid time zone' % tz)
Example #3
0
def valid_time_zone(tz, config_context):
    if tz is None:
        return None
    valid_string(tz, config_context)
    try:
        return pytz.timezone(tz)
    except pytz.exceptions.UnknownTimeZoneError:
        raise ConfigError('%s is not a valid time zone' % tz)
Example #4
0
def valid_known_hosts_file(file_path, config_context):
    valid_string(file_path, config_context)

    if config_context.partial:
        return file_path

    file_path = os.path.expanduser(file_path)
    if not os.path.exists(file_path):
        raise ConfigError("Known hosts file %s doesn't exist" % file_path)
    return file_path
Example #5
0
def valid_known_hosts_file(file_path, config_context):
    valid_string(file_path, config_context)

    if config_context.partial:
        return file_path

    file_path = os.path.expanduser(file_path)
    if not os.path.exists(file_path):
        raise ConfigError("Known hosts file %s doesn't exist" % file_path)
    return file_path
Example #6
0
def valid_identity_file(file_path, config_context):
    valid_string(file_path, config_context)

    if config_context.partial:
        return file_path

    file_path = os.path.expanduser(file_path)
    if not os.path.exists(file_path):
        raise ConfigError("Private key file %s doesn't exist" % file_path)

    public_key_path = file_path + '.pub'
    if not os.path.exists(public_key_path):
        raise ConfigError("Public key file %s doesn't exist" % public_key_path)
    return file_path
Example #7
0
def valid_identity_file(file_path, config_context):
    valid_string(file_path, config_context)

    if config_context.partial:
        return file_path

    file_path = os.path.expanduser(file_path)
    if not os.path.exists(file_path):
        raise ConfigError("Private key file %s doesn't exist" % file_path)

    public_key_path = file_path + '.pub'
    if not os.path.exists(public_key_path):
        raise ConfigError("Public key file %s doesn't exist" % public_key_path)
    return file_path
Example #8
0
def valid_daily_scheduler(config, config_context):
    """Daily scheduler, accepts a time of day and an optional list of days."""
    schedule_config = config.value
    time_string, days = pad_sequence(schedule_config.split(), 2)
    time_string = time_string or '00:00:00'
    time_spec = config_utils.valid_time(time_string, config_context)
    days = config_utils.valid_string(days or "", config_context)

    def valid_day(day):
        if day not in CONVERT_DAYS_INT:
            raise ConfigError(
                "Unknown day %s at %s" % (
                    day,
                    config_context.path,
                )
            )
        return CONVERT_DAYS_INT[day]

    original = "%s %s" % (time_string, days)
    weekdays = {valid_day(day) for day in days or ()}
    return ConfigDailyScheduler(
        original,
        time_spec.hour,
        time_spec.minute,
        time_spec.second,
        weekdays,
        jitter=config.jitter,
    )
Example #9
0
def valid_daily_scheduler(config, config_context):
    """Daily scheduler, accepts a time of day and an optional list of days."""
    schedule_config = config.value
    time_string, days = pad_sequence(schedule_config.split(), 2)
    time_string = time_string or '00:00:00'
    time_spec = config_utils.valid_time(time_string, config_context)
    days = config_utils.valid_string(days or "", config_context)

    def valid_day(day):
        if day not in CONVERT_DAYS_INT:
            raise ConfigError("Unknown day %s at %s" % (
                day,
                config_context.path,
            ))
        return CONVERT_DAYS_INT[day]

    original = "%s %s" % (time_string, days)
    weekdays = {valid_day(day) for day in days or ()}
    return ConfigDailyScheduler(
        original,
        time_spec.hour,
        time_spec.minute,
        time_spec.second,
        weekdays,
        jitter=config.jitter,
    )
Example #10
0
def valid_output_stream_dir(output_dir, config_context):
    """Returns a valid string for the output directory, or raises ConfigError
    if the output_dir is not valid.
    """
    if not output_dir:
        return

    if config_context.partial:
        return output_dir

    valid_string(output_dir, config_context)
    if not os.path.isdir(output_dir):
        msg = "output_stream_dir '%s' is not a directory"
        raise ConfigError(msg % output_dir)

    if not os.access(output_dir, os.W_OK):
        raise ConfigError("output_stream_dir '%s' is not writable" % output_dir)

    return output_dir
Example #11
0
def valid_output_stream_dir(output_dir, config_context):
    """Returns a valid string for the output directory, or raises ConfigError
    if the output_dir is not valid.
    """
    if not output_dir:
        return

    if config_context.partial:
        return output_dir

    valid_string(output_dir, config_context)
    if not os.path.isdir(output_dir):
        msg = "output_stream_dir '%s' is not a directory"
        raise ConfigError(msg % output_dir)

    if not os.access(output_dir, os.W_OK):
        raise ConfigError("output_stream_dir '%s' is not writable" % output_dir)

    return output_dir
Example #12
0
    def validator(value, config_context):
        if config_context.partial:
            return valid_string(value, config_context)

        context = command_context.CommandContext(context_object, config_context.command_context)

        try:
            value % context
            return value
        except (KeyError, ValueError), e:
            error_msg = "Unknown context variable %s at %s: %s"
            raise ConfigError(error_msg % (e, config_context.path, value))
Example #13
0
    def validator(value, config_context):
        if config_context.partial:
            return valid_string(value, config_context)

        context = command_context.CommandContext(
            context_object, config_context.command_context)

        try:
            value % context
            return value
        except (KeyError, ValueError), e:
            error_msg = "Unknown context variable %s at %s: %s"
            raise ConfigError(error_msg % (e, config_context.path, value))
Example #14
0
    def validator(value, config_context):
        if config_context.partial:
            return valid_string(value, config_context)

        context = command_context.CommandContext(
                    context_object, config_context.command_context)

        try:
            value % context
            return value
        except (KeyError, ValueError):
            error_msg = "Invalid template string at %s: %s"
            raise ConfigError(error_msg % (config_context.path, value))
Example #15
0
def valid_daily_scheduler(time_string, days, config_context):
    """Daily scheduler, accepts a time of day and an optional list of days."""
    time_string = time_string or '00:00:00'
    time_spec   = config_utils.valid_time(time_string, config_context)
    days        = config_utils.valid_string(days or "", config_context)

    def valid_day(day):
        if day not in CONVERT_DAYS_INT:
            raise ConfigError("Unknown day %s at %s" % (day, config_context.path))
        return CONVERT_DAYS_INT[day]

    original = "%s %s" % (time_string, days)
    weekdays = set(valid_day(day) for day in days or ())
    return ConfigDailyScheduler(original,
        time_spec.hour, time_spec.minute, time_spec.second, weekdays)
Example #16
0
    def validator(value, config_context):
        if config_context.partial:
            return valid_string(value, config_context)

        context = command_context.CommandContext(
            context_object,
            config_context.command_context,
        )

        try:
            StringFormatter(context).format(value)
            return value
        except (KeyError, ValueError) as e:
            error_msg = "Unknown context variable %s at %s: %s"
            raise ConfigError(error_msg % (e, config_context.path, value))
        except (TypeError) as e:
            error_msg = "Wrong command format %s: %s at %s"
            raise ConfigError(error_msg % (value, e, config_context.path))