Ejemplo n.º 1
0
def check_cooldowns(log, state, config, policy, policy_id):
    """
    Check the global cooldowns (when was the last time any policy was executed?)
    and the policy specific cooldown (when was the last time THIS policy was
    executed?)

    :param log: A twiggy bound log for logging
    :param dict state: the state dictionary
    :param dict config: the config dictionary
    :param dict policy: the policy dictionary
    :param str policy_id: the policy id that matches ``policy``

    :return: C{int}
    """
    this_now = datetime.now(iso8601.iso8601.UTC)

    timestamp_and_cooldowns = [
        (state.policy_touched.get(policy_id), policy['cooldown'], 'policy'),
        (state.group_touched, config['cooldown'], 'group'),
    ]

    for last_time, cooldown, cooldown_type in timestamp_and_cooldowns:
        if last_time is not None:
            delta = this_now - from_timestamp(last_time)
            if delta.total_seconds() < cooldown:
                log.bind(time_since_last_touched=delta.total_seconds(),
                         cooldown_type=cooldown_type,
                         cooldown_seconds=cooldown).msg("cooldown not reached")
                return False

    return True
Ejemplo n.º 2
0
def check_cooldowns(log, state, config, policy, policy_id):
    """
    Check the global cooldowns (when was the last time any policy was executed?)
    and the policy specific cooldown (when was the last time THIS policy was
    executed?)

    :param log: A twiggy bound log for logging
    :param dict state: the state dictionary
    :param dict config: the config dictionary
    :param dict policy: the policy dictionary
    :param str policy_id: the policy id that matches ``policy``

    :return: C{int}
    """
    this_now = datetime.now(iso8601.iso8601.UTC)

    timestamp_and_cooldowns = [
        (state.policy_touched.get(policy_id), policy['cooldown'], 'policy'),
        (state.group_touched, config['cooldown'], 'group'),
    ]

    for last_time, cooldown, cooldown_type in timestamp_and_cooldowns:
        if last_time is not None:
            delta = this_now - from_timestamp(last_time)
            if delta.total_seconds() < cooldown:
                log.bind(time_since_last_touched=delta.total_seconds(),
                         cooldown_type=cooldown_type,
                         cooldown_seconds=cooldown).msg("cooldown not reached")
                return False

    return True
Ejemplo n.º 3
0
    def test_from_timestamp_can_read_min_timestamp(self):
        """
        ``from_timestamp`` can parse timestamps produced by ``MIN``
        """
        parsed = timestamp.from_timestamp(timestamp.MIN)

        # can't compare naive and timestamp-aware datetimes, so check that
        # the parsed timezone is not None.  Then replace with None to compare.
        self.assertTrue(parsed.tzinfo is not None)
        self.assertEqual(parsed.replace(tzinfo=None), datetime.min)
Ejemplo n.º 4
0
def find_pending_jobs_to_cancel(log, state, delta):
    """
    Identify some pending jobs to cancel (usually for a scale down event)
    """
    if delta >= len(state.pending):  # don't bother sorting - return everything
        return state.pending.keys()

    sorted_jobs = sorted(state.pending.items(), key=lambda (_id, s): from_timestamp(s['created']),
                         reverse=True)
    return [job_id for job_id, _job_info in sorted_jobs[:delta]]
Ejemplo n.º 5
0
def find_pending_jobs_to_cancel(log, state, delta):
    """
    Identify some pending jobs to cancel (usually for a scale down event)
    """
    if delta >= len(state.pending):  # don't bother sorting - return everything
        return state.pending.keys()

    sorted_jobs = sorted(state.pending.items(), key=lambda (_id, s): from_timestamp(s['created']),
                         reverse=True)
    return [job_id for job_id, _job_info in sorted_jobs[:delta]]
Ejemplo n.º 6
0
    def test_from_timestamp_can_read_min_timestamp(self):
        """
        ``from_timestamp`` can parse timestamps produced by ``MIN``
        """
        parsed = timestamp.from_timestamp(timestamp.MIN)

        # can't compare naive and timestamp-aware datetimes, so check that
        # the parsed timezone is not None.  Then replace with None to compare.
        self.assertTrue(parsed.tzinfo is not None)
        self.assertEqual(parsed.replace(tzinfo=None), datetime.min)
Ejemplo n.º 7
0
def validate_datetime(dt_str):
    """
    Validate date-time string in json. Return True if valid and raise exceptions if invalid
    """
    if dt_str[-1] != 'Z':
        raise ValueError('Expecting Zulu-format UTC time')
    dt = from_timestamp(dt_str)
    # Ensure time is in future
    if datetime.utcfromtimestamp(calendar.timegm(dt.utctimetuple())) <= datetime.utcnow():
        raise ValueError('time must be in future')
    return True
Ejemplo n.º 8
0
def _build_schedule_policy(policy, event_table, queries, data, polname):
    """
    Build schedule-type policy
    """
    if 'at' in policy["args"]:
        queries.append(_cql_insert_event.format(cf=event_table, name=':' + polname))
        data[polname + "Trigger"] = timestamp.from_timestamp(policy["args"]["at"])
    elif 'cron' in policy["args"]:
        queries.append(_cql_insert_event_with_cron.format(cf=event_table, name=':' + polname))
        cron = policy["args"]["cron"]
        data[polname + "Trigger"] = next_cron_occurrence(cron)
        data[polname + 'cron'] = cron
Ejemplo n.º 9
0
def find_servers_to_evict(log, state, delta):
    """
    Find the servers most appropriate to evict from the scaling group

    Returns list of server ``dict``
    """
    if delta >= len(state.active):  # don't bother sorting - return everything
        return state.active.values()

    # return delta number of oldest server
    sorted_servers = sorted(state.active.values(), key=lambda s: from_timestamp(s['created']))
    return sorted_servers[:delta]
Ejemplo n.º 10
0
def find_servers_to_evict(log, state, delta):
    """
    Find the servers most appropriate to evict from the scaling group

    Returns list of server ``dict``
    """
    if delta >= len(state.active):  # don't bother sorting - return everything
        return state.active.values()

    # return delta number of oldest server
    sorted_servers = sorted(state.active.values(), key=lambda s: from_timestamp(s['created']))
    return sorted_servers[:delta]
Ejemplo n.º 11
0
    def test_from_timestamp_can_read_now_timestamp(self, mock_datetime):
        """
        ``from_timestamp`` can parse timestamps produced by ``now()``
        """
        mock_datetime.utcnow.return_value = datetime(
            2000, 01, 01, 12, 0, 0, 0, None)

        parsed = timestamp.from_timestamp(timestamp.now())

        # can't compare naive and timestamp-aware datetimes, so check that
        # the parsed timezone is not None.  Then replace with None to compare.
        self.assertTrue(parsed.tzinfo is not None)
        self.assertEqual(parsed.replace(tzinfo=None),
                         mock_datetime.utcnow.return_value)
Ejemplo n.º 12
0
    def test_from_timestamp_can_read_now_timestamp(self, mock_datetime):
        """
        ``from_timestamp`` can parse timestamps produced by ``now()``
        """
        mock_datetime.utcnow.return_value = datetime(
            2000, 01, 01, 12, 0, 0, 0, None)

        parsed = timestamp.from_timestamp(timestamp.now())

        # can't compare naive and timestamp-aware datetimes, so check that
        # the parsed timezone is not None.  Then replace with None to compare.
        self.assertTrue(parsed.tzinfo is not None)
        self.assertEqual(parsed.replace(tzinfo=None),
                         mock_datetime.utcnow.return_value)
Ejemplo n.º 13
0
def _build_schedule_policy(policy, event_table, queries, data, polname):
    """
    Build schedule-type policy
    """
    if 'at' in policy["args"]:
        queries.append(
            _cql_insert_event.format(cf=event_table, name=':' + polname))
        data[polname + "Trigger"] = timestamp.from_timestamp(
            policy["args"]["at"])
    elif 'cron' in policy["args"]:
        queries.append(
            _cql_insert_event_with_cron.format(cf=event_table,
                                               name=':' + polname))
        cron = policy["args"]["cron"]
        data[polname + "Trigger"] = next_cron_occurrence(cron)
        data[polname + 'cron'] = cron
Ejemplo n.º 14
0
def validate_datetime(dt_str):
    """
    Validate date-time string in json. Return True if valid and raise ValueError if invalid
    """
    if dt_str and dt_str[-1] != 'Z':
        raise ValueError('Expecting Zulu-format UTC time')
    try:
        dt = from_timestamp(dt_str)
    except:
        # It is checking for any exception since from_timestamp throws
        # TypeError instead of ParseError with certain invalid inputs like only date or time.
        # This issue has been raised and tracked http://code.google.com/p/pyiso8601/issues/detail?id=8
        # and http://code.google.com/p/pyiso8601/issues/detail?id=24
        raise ValueError('Error parsing datetime str')
    # Ensure time is in future
    if datetime.utcfromtimestamp(calendar.timegm(dt.utctimetuple())) <= datetime.utcnow():
        raise ValidationError('Invalid "{}" datetime: It must be in the future'.format(dt_str))
    return True
Ejemplo n.º 15
0
def validate_datetime(dt_str):
    """
    Validate date-time string in json. Return True if valid and raise ValueError if invalid
    """
    if dt_str and dt_str[-1] != 'Z':
        raise ValueError('Expecting Zulu-format UTC time')
    try:
        dt = from_timestamp(dt_str)
    except:
        # It is checking for any exception since from_timestamp throws
        # TypeError instead of ParseError with certain invalid inputs like only date or time.
        # This issue has been raised and tracked http://code.google.com/p/pyiso8601/issues/detail?id=8
        # and http://code.google.com/p/pyiso8601/issues/detail?id=24
        raise ValueError('Error parsing datetime str')
    # Ensure time is in future
    if datetime.utcfromtimestamp(calendar.timegm(
            dt.utctimetuple())) <= datetime.utcnow():
        raise ValidationError(
            'Invalid "{}" datetime: It must be in the future'.format(dt_str))
    return True