コード例 #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)
コード例 #2
0
ファイル: presence.py プロジェクト: linbai/treadmill
    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()
コード例 #3
0
    def start_container(container_root, manifest):
        """Treadmill container boot process.
        """
        _LOGGER.info('Initializing container: %s', container_root)
        app = app_manifest.read(manifest)

        try:
            pivot_root.make_root(container_root)
            os.chdir('/')
        except Exception as err:  # pylint: disable=broad-except
            event = traceevents.AbortedTraceEvent(
                instanceid=app['name'],
                why=app_abort.AbortedReason.PIVOT_ROOT.value,
                payload=str(err),
            )

            _abort(event, container_root)

            # reraise err to exit start_container
            raise err

        # XXX: Debug info
        _LOGGER.debug('Current mounts: %s',
                      pprint.pformat(fs_linux.list_mounts()))

        # Clean the environ
        # TODO: Remove me once clean environment management is merged in.
        os.environ.pop('PYTHONPATH', None)
        os.environ.pop('LC_ALL', None)
        os.environ.pop('LANG', None)

        # Clear aliases path.
        os.environ.pop('TREADMILL_ALIASES_PATH', None)

        subproc.safe_exec(['s6_svscan', '-s', '/services'])
コード例 #4
0
    def start_container(ctx, container_root, manifest):
        """Treadmill container boot process.
        """
        _LOGGER.info('Initializing container: %s', container_root)
        app = app_manifest.read(manifest)

        cgroup = ctx.obj.get('CGROUP')
        try:
            # if cgroups set, we need to remount cgroup path
            # so that from cgroup directory we only see container pids
            # <container_root>/sys/fs/cgroup/memory =>
            #   /sys/fs/cgroup/memory/treadmill/apps/<app-inst-unique>/services
            if cgroup:
                remount_cgroup(container_root, cgroup, ctx.obj['ROOT_CGROUP'])

            pivot_root.make_root(container_root)
            os.chdir('/')
        except Exception as err:  # pylint: disable=broad-except
            event = traceevents.AbortedTraceEvent(
                instanceid=app['name'],
                why=app_abort.AbortedReason.PIVOT_ROOT.value,
                payload=str(err),
            )

            _abort(event, container_root)

            # reraise err to exit start_container
            raise err

        # XXX: Debug info
        _LOGGER.debug('Current mounts: %s',
                      pprint.pformat(fs_linux.list_mounts()))

        subproc.safe_exec(['/services/{}'.format(supervisor.SVC_INIT_FILE)])
コード例 #5
0
    def run(self, terminated):
        """Prepares container environment and exec's container

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

        :param terminated:
            Flag where terminated signal will accumulate.
        :param terminated:
            ``set``
        :returns:
            This function never returns
        """
        manifest_file = os.path.join(self.container_dir, _APP_YML)
        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')
                )
            )

        watchdog_name = 'app_run-%s' % os.path.basename(self.container_dir)
        self.watchdog = self.tm_env.watchdogs.create(
            watchdog_name, '60s',
            'Run of {0} stalled'.format(self.container_dir))

        self._run(manifest, self.watchdog, terminated)
コード例 #6
0
def load_app(container_dir, app_json=STATE_JSON):
    """Load app manifest as object."""
    manifest_file = os.path.join(container_dir, app_json)

    try:
        manifest = app_manifest.read(manifest_file)
        _LOGGER.debug('Manifest: %r', manifest)
        return utils.to_obj(manifest)

    except IOError as err:
        if err.errno != errno.ENOENT:
            raise

        _LOGGER.info('Manifest file does not exist: %s', manifest_file)
        return None
コード例 #7
0
def _load_app(container_dir, app_yml):
    """Load app from original manifest, pre-configured."""
    manifest_file = os.path.join(container_dir, app_yml)

    try:
        manifest = app_manifest.read(manifest_file)
        _LOGGER.debug('Manifest: %r', manifest)
        return utils.to_obj(manifest)

    except IOError as err:
        if err.errno != errno.ENOENT:
            raise

        _LOGGER.critical('Manifest file does not exit: %r', manifest_file)
        return None
コード例 #8
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._service.data_dir, appcfg.APP_JSON)
        manifest = app_manifest.read(manifest_file)
        if not self._can_run(manifest):
            raise exc.ContainerSetupError('invalid_type',
                                          app_abort.AbortedReason.INVALID_TYPE)

        self._run(manifest)
コード例 #9
0
 def monitor(self, manifest_file):
     """Monitor container services."""
     manifest = app_manifest.read(manifest_file)
     self._monitor(manifest)
コード例 #10
0
 def register(self, manifest_file, refresh_interval=None):
     """Register/Start container presence."""
     manifest = app_manifest.read(manifest_file)
     self._register(manifest, refresh_interval)