Exemplo n.º 1
0
    def _on_log_message(self, phase, json_data):
        """ Tails log messages and updates the build status. """
        # Update the heartbeat.
        self._last_heartbeat = datetime.datetime.utcnow()

        # Parse any of the JSON data logged.
        log_data = {}
        if json_data:
            try:
                log_data = json.loads(json_data)
            except ValueError:
                pass

        # Extract the current status message (if any).
        fully_unwrapped = ''
        keys_to_extract = ['error', 'status', 'stream']
        for key in keys_to_extract:
            if key in log_data:
                fully_unwrapped = log_data[key]
                break

        # Determine if this is a step string.
        current_step = None
        current_status_string = str(fully_unwrapped.encode('utf-8'))

        if current_status_string and phase == BUILD_PHASE.BUILDING:
            current_step = extract_current_step(current_status_string)

        # Parse and update the phase and the status_dict. The status dictionary contains
        # the pull/push progress, as well as the current step index.
        with self._build_status as status_dict:
            try:
                changed_phase = yield From(
                    self._build_status.set_phase(phase,
                                                 log_data.get('status_data')))
                if changed_phase:
                    logger.debug('Build %s has entered a new phase: %s',
                                 self.builder_realm, phase)
                elif self._current_job.repo_build.phase == BUILD_PHASE.CANCELLED:
                    build_id = self._current_job.repo_build.uuid
                    logger.debug(
                        'Trying to move cancelled build into phase: %s with id: %s',
                        phase, build_id)
                    raise Return(False)
            except InvalidRepositoryBuildException:
                build_id = self._current_job.repo_build.uuid
                logger.warning(
                    'Build %s was not found; repo was probably deleted',
                    build_id)
                raise Return(False)

            BuildComponent._process_pushpull_status(status_dict, phase,
                                                    log_data, self._image_info)

            # If the current message represents the beginning of a new step, then update the
            # current command index.
            if current_step is not None:
                status_dict['current_command'] = current_step

            # If the json data contains an error, then something went wrong with a push or pull.
            if 'error' in log_data:
                yield From(self._build_status.set_error(log_data['error']))

        if current_step is not None:
            yield From(self._build_status.set_command(current_status_string))
        elif phase == BUILD_PHASE.BUILDING:
            yield From(self._build_status.append_log(current_status_string))
        raise Return(True)
Exemplo n.º 2
0
def test_extract_current_step(input, expected_step):
    assert extract_current_step(input) == expected_step