def get_master_state(states, now=None): """Returns the latest state earlier than the current (or specified) time. If there are three items, each with transition times of 100, 200 and 300: * calling when 'now' is 50 will return None * calling when 'now' is 150 will return the first item * calling when 'now' is 400 will return the third item """ now = now or timestamp.utcnow_ts() times = [parse_transition_time(x['transition_time_utc']) for x in states] index = bisect.bisect_left(times, now) if index > 0: # An index of 0 means all timestamps are in the future. return states[index - 1] return None
def get_master_state(states, now=None): """Returns the latest state earlier than the current (or specified) time. If there are three items, each with transition times of 100, 200 and 300: * calling when 'now' is 50 will return None * calling when 'now' is 150 will return the first item * calling when 'now' is 400 will return the third item """ now = now or timestamp.utcnow_ts() times = [parse_transition_time(x['transition_time_utc']) for x in states] index = bisect.bisect_left(times, now) if index > 0: # An index of 0 means all timestamps are in the future. return states[index - 1] return None
def collect_evidence(master_directory, connection_timeout=30): """Collects evidence from the OS for late state determination.""" evidence = {} evidence['now'] = timestamp.utcnow_ts() evidence['last_boot'] = master.get_last_boot(master_directory) evidence['last_no_new_builds'] = master.get_last_no_new_builds( master_directory) evidence['buildbot_is_running'] = master.buildbot_is_running(master_directory) if evidence['buildbot_is_running']: accepting_builds, current_running_builds = master.get_buildstate( master_directory, timeout=connection_timeout) evidence['accepting_builds'] = accepting_builds evidence['current_running_builds'] = current_running_builds return evidence
def desired_master_state_is_valid(desired_state): """Verify that the desired_master_state file is valid.""" now = timestamp.utcnow_ts() for mastername, states in desired_state.iteritems(): # Verify desired_state and timestamp are valid. for state in states: # Verify desired_state and transition_time_utc are present. for k in ('desired_state', 'transition_time_utc'): if not k in state: LOGGER.error( 'one or more states for master %s do not contain %s', mastername, k) return False # Verify the desired state is in the allowed set. if (state['desired_state'] not in buildbot_state.STATES['desired_buildbot_state']): LOGGER.error('desired_state \'%s\' is not one of %s', state['desired_state'], buildbot_state.STATES['desired_buildbot_state']) return False # Verify the timestamp is a number or Zulu time. if parse_transition_time(state['transition_time_utc']) is None: LOGGER.error( 'transition_time_utc \'%s\' is not an int, float, or Zulu time', state['transition_time_utc']) return False # Verify the list is properly sorted. sorted_states = sorted(states, key=operator.itemgetter('transition_time_utc')) if sorted_states != states: LOGGER.error('master %s does not have states sorted by timestamp', mastername) LOGGER.error('should be:\n%s', json.dumps(sorted_states, indent=2)) return False # Verify there is at least one state in the past. if not get_master_state(states, now=now): LOGGER.error('master %s does not have a state older than %s', mastername, now) return False return True
def validate_desired_master_state(desired_state): """Verify that the desired_master_state file is valid.""" now = timestamp.utcnow_ts() for mastername, states in desired_state.iteritems(): # Verify desired_state and timestamp are valid. for state in states: # Verify desired_state and transition_time_utc are present. for k in ('desired_state', 'transition_time_utc'): if not k in state: raise InvalidDesiredMasterState( 'one or more states for master %s do not contain %s' % ( mastername, k)) # Verify the desired state is in the allowed set. if (state['desired_state'] not in buildbot_state.STATES['desired_buildbot_state']): raise InvalidDesiredMasterState( 'desired_state \'%s\' is not one of %s' %( state['desired_state'], buildbot_state.STATES['desired_buildbot_state'])) # Verify the timestamp is a number or Zulu time. if parse_transition_time(state['transition_time_utc']) is None: raise InvalidDesiredMasterState( 'transition_time_utc \'%s\' is not an int, float, or Zulu time' % ( state['transition_time_utc'])) # Verify the list is properly sorted. sorted_states = sorted( states, key=operator.itemgetter('transition_time_utc')) if sorted_states != states: raise InvalidDesiredMasterState( 'master %s does not have states sorted by timestamp\n' 'should be:\n%s' % ( mastername, json.dumps(sorted_states, indent=2))) # Verify there is at least one state in the past. if not get_master_state(states, now=now): raise InvalidDesiredMasterState( 'master %s does not have a state older than %s' % (mastername, now))