Ejemplo n.º 1
0
def finish(tm_env, container_dir):
    """Frees allocated resources and mark then as available.
    """
    with lc.LogContext(_LOGGER, os.path.basename(container_dir),
                       lc.ContainerAdapter):
        _LOGGER.info('finishing %r', container_dir)

        data_dir = os.path.join(container_dir, 'data')

        app = runtime.load_app(data_dir)
        if app is not None:
            _cleanup(tm_env, data_dir, app)
        else:
            app = runtime.load_app(data_dir, appcfg.APP_JSON)

        if app is not None:
            # Check if application reached restart limit inside the container.
            #
            # The container directory will be moved, this check is done first.
            #
            # If restart limit was reached, application node will be removed
            # from Zookeeper at the end of the cleanup process, indicating to
            # the scheduler that the server is ready to accept new load.
            exitinfo, aborted, oom, terminated = _collect_exit_info(data_dir)

            # All resources are cleaned up. If the app terminated inside the
            # container, remove the node from Zookeeper, which will notify the
            # scheduler that it is safe to reuse the host for other load.
            if aborted is not None:
                app_abort.report_aborted(tm_env,
                                         app.name,
                                         why=aborted.get('why'),
                                         payload=aborted.get('payload'))

            elif oom:
                _post_oom_event(tm_env, app)

            elif terminated:
                # Terminated (or evicted).
                # Don't post event, this is handled by the master.
                _LOGGER.info('Terminated: %s', app.name)

            else:
                # Container finished because service exited.
                # It is important that this is checked last.
                if exitinfo is not None:
                    _post_exit_event(tm_env, app, exitinfo)

        # cleanup monitor with container information
        if app:
            apphook.cleanup(tm_env, app, container_dir)
Ejemplo n.º 2
0
def finish(tm_env, zkclient, container_dir, watchdog):
    """Frees allocated resources and mark then as available.
    """
    with lc.LogContext(_LOGGER, os.path.basename(container_dir),
                       lc.ContainerAdapter) as log:
        log.info('finishing %r', container_dir)

        _stop_container(container_dir)

        # Check if application reached restart limit inside the container.
        #
        # The container directory will be moved, this check is done first.
        #
        # If restart limit was reached, application node will be removed from
        # Zookeeper at the end of the cleanup process, indicating to the
        # scheduler that the server is ready to accept new load.
        exitinfo, aborted, aborted_reason = _collect_exit_info(container_dir)

        app = runtime.load_app(container_dir)
        if app:
            _cleanup(tm_env, zkclient, container_dir, app)
        else:
            app = runtime.load_app(container_dir, appcfg.APP_JSON)

        if app:
            # All resources are cleaned up. If the app terminated inside the
            # container, remove the node from Zookeeper, which will notify the
            # scheduler that it is safe to reuse the host for other load.
            if aborted:
                appevents.post(
                    tm_env.app_events_dir,
                    events.AbortedTraceEvent(
                        instanceid=app.name,
                        why=None,  # TODO(boysson): extract this info
                        payload=aborted_reason
                    )
                )

            if exitinfo:
                _post_exit_event(tm_env, app, exitinfo)

        # cleanup monitor with container information
        if app:
            apphook.cleanup(tm_env, app)

        # Delete the app directory (this includes the tarball, if any)
        shutil.rmtree(container_dir)

        # cleanup was succesful, remove the watchdog
        watchdog.remove()
        log.logger.info('Finished cleanup: %s', container_dir)
Ejemplo n.º 3
0
    def kill(self):
        app = runtime.load_app(self._service.data_dir, runtime.STATE_JSON)
        if not app:
            return

        name = appcfg.app_unique_name(app)
        try:
            client = self._get_client()
            container = client.containers.get(name)
            container.kill()
        except docker.errors.NotFound:
            pass
Ejemplo n.º 4
0
    def _finish(self):
        app = runtime.load_app(self._service.data_dir, runtime.STATE_JSON)

        if app:
            client = self._get_client()
            container = state = None
            name = appcfg.app_unique_name(app)
            try:
                container = client.containers.get(name)
                state = container.attrs.get('State')
            except docker.errors.NotFound:
                pass

            if container is not None:
                try:
                    container.remove(force=True)
                except docker.errors.APIError:
                    _LOGGER.error('Failed to remove %s', container.id)

            aborted = _check_aborted(self._service.data_dir)
            if aborted is not None:
                app_abort.report_aborted(self._tm_env,
                                         app.name,
                                         why=aborted.get('why'),
                                         payload=aborted.get('payload'))

            elif state is not None:
                if state.get('OOMKilled', False):
                    event = events.KilledTraceEvent(
                        instanceid=app.name,
                        is_oom=True,
                    )
                else:
                    event = events.FinishedTraceEvent(instanceid=app.name,
                                                      rc=state.get(
                                                          'ExitCode', 256),
                                                      signal=0,
                                                      payload=state)

                trace.post(self._tm_env.app_events_dir, event)

            if os.name == 'nt':
                credential_spec.cleanup(name, client)

            try:
                runtime.archive_logs(self._tm_env, name,
                                     self._service.data_dir)
            except Exception:  # pylint: disable=W0703
                _LOGGER.exception('Unexpected exception storing local logs.')