Esempio n. 1
0
    def run(self):
        """Prepares container environment and exec's container

        The function is intended to be invoked from 'run' script and never
        returns.

        :returns:
            This function never returns
        """
        manifest_file = os.path.join(self.container_dir, appcfg.APP_JSON)
        manifest = app_manifest.read(manifest_file)
        if not self._can_run(manifest):
            raise exc.ContainerSetupError(
                'Runtime {0} does not support {1}.'.format(
                    self.__class__.__name__, manifest.get('type')))

        # Intercept SIGTERM from supervisor, so that initialization is not
        # left in broken state.
        terminated = utils.make_signal_flag(utils.term_signal())

        unique_name = appcfg.manifest_unique_name(manifest)
        watchdog_name = 'app_run-%s' % unique_name
        self.watchdog = self.tm_env.watchdogs.create(
            watchdog_name, self.run_timeout(manifest),
            'Run of {container_dir!r} stalled'.format(
                container_dir=self.container_dir))

        self._run(manifest, self.watchdog, terminated)
Esempio n. 2
0
    def register_cmd(refresh_interval, manifest, container_dir):
        """Register container presence."""
        try:
            _LOGGER.info('Configuring sigterm handler.')
            signal.signal(utils.term_signal(), sigterm_handler)

            app = app_manifest.read(manifest)

            # If tickets are not ok, app will be aborted.
            #
            # If tickets acquired successfully, services will start, and
            # tickets will be refreshed after each interval.
            refresh = False
            try:
                refresh = _get_tickets(app, container_dir)
                _start_service_sup(container_dir)
            except exc.ContainerSetupError as err:
                app_abort.abort(container_dir,
                                why=err.reason,
                                payload=traceback.format_exc())

            while True:
                # Need to sleep anyway even if not refreshing tickets.
                time.sleep(refresh_interval)
                if refresh:
                    _refresh_tickets(app, container_dir)
        finally:
            _LOGGER.info('Stopping zookeeper.')
            context.GLOBAL.zk.conn.stop()
Esempio n. 3
0
    def run(approot, runtime, container_dir):
        """Runs container given a container dir."""
        # Intercept SIGTERM from s6 supervisor, so that initialization is not
        # left in broken state.
        with lc.LogContext(_LOGGER, os.path.basename(container_dir),
                           lc.ContainerAdapter) as log:
            terminated = utils.make_signal_flag(utils.term_signal())
            tm_env = None
            try:
                log.info('run %r %r', approot, container_dir)
                tm_env = appenv.AppEnvironment(approot)

                app_runtime.get_runtime(runtime, tm_env,
                                        container_dir).run(terminated)

                # If we reach here, the application was terminated.

            except Exception as exc:  # pylint: disable=W0703
                if not terminated:
                    log.critical('Failed to start, app will be aborted.',
                                 exc_info=True)
                    app_abort.flag_aborted(tm_env, container_dir, exc)
                else:
                    log.logger.info('Exception while handling term, ignore.',
                                    exc_info=True)
Esempio n. 4
0
    def presence(zkid):
        """Runs the presence service.
        """
        root_dir = local_ctx['root-dir']
        watchdogs_dir = local_ctx['watchdogs-dir']

        # Explicitely create global zk connection, so that zk session id is
        # preserved.
        context.GLOBAL.zk.conn = zkutils.connect(context.GLOBAL.zk.url,
                                                 idpath=zkid)

        def sigterm_handler(_signo, _stack_frame):
            """Handle sigterm.

            On sigterm, stop Zookeeper session and delete Zookeeper session
            id file.
            """
            _LOGGER.info('Got SIGTERM, closing zk session and rm: %s', zkid)
            fs.rm_safe(zkid)
            context.GLOBAL.zk.conn.stop()

        signal.signal(utils.term_signal(), sigterm_handler)

        svc = services.ResourceService(
            service_dir=os.path.join(root_dir, 'presence_svc'),
            impl='presence',
        )

        svc.run(watchdogs_dir=os.path.join(root_dir, watchdogs_dir))
Esempio n. 5
0
    def presence(zkid):
        """Runs the presence service.
        """
        root_dir = local_ctx['root-dir']
        watchdogs_dir = local_ctx['watchdogs-dir']

        context.GLOBAL.zk.idpath = zkid
        context.GLOBAL.zk.add_listener(zkutils.exit_on_lost)

        def sigterm_handler(_signo, _stack_frame):
            """Handle sigterm.

            On sigterm, stop Zookeeper session and delete Zookeeper session
            id file.
            """
            _LOGGER.info('Got SIGTERM, closing zk session and rm: %s', zkid)
            fs.rm_safe(zkid)
            if context.GLOBAL.zk.has_conn():
                context.GLOBAL.zk.conn.stop()

        signal.signal(utils.term_signal(), sigterm_handler)

        svc = services.ResourceService(
            service_dir=os.path.join(root_dir, 'presence_svc'),
            impl='presence',
        )

        svc.run(watchdogs_dir=os.path.join(root_dir, watchdogs_dir))
Esempio n. 6
0
    def finish(self):
        """Frees allocated resources and mark then as available."""

        # Intercept SIGTERM from supervisor, so that finish is not
        # left in broken state.
        terminated = utils.make_signal_flag(utils.term_signal())

        # FIXME(boysson): The watchdog value below is inflated to account for
        #                 the extra archiving time.
        watchdog_name = 'app_finish-%s' % os.path.basename(self.container_dir)
        self.watchdog = self.tm_env.watchdogs.create(
            watchdog_name, self.finish_timeout,
            'Cleanup of {0} stalled'.format(self.container_dir))

        self._finish(self.watchdog, terminated)