Esempio n. 1
0
    def _configure(self, instance_name):
        """Configures and starts the instance based on instance cached event.

        - Runs app_configure --approot <rootdir> cache/<instance>

        :param ``str`` instance_name:
            Name of the instance to configure
        :returns ``bool``:
            True for successfully configured container.
        """
        event_file = os.path.join(
            self.tm_env.cache_dir,
            instance_name
        )

        with lc.LogContext(_LOGGER, instance_name):
            try:
                _LOGGER.info('Configuring')
                container_dir = app_cfg.configure(self.tm_env, event_file)
                app_cfg.schedule(
                    container_dir,
                    os.path.join(self.tm_env.running_dir, instance_name)
                )
                return True

            except Exception as err:  # pylint: disable=W0703
                _LOGGER.exception('Error configuring (%r)', event_file)
                app_abort.abort(self.tm_env, event_file, err)
                fs.rm_safe(event_file)
                return False
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 _register(self, manifest, refresh_interval=None):
        app_presence = presence.EndpointPresence(context.GLOBAL.zk.conn,
                                                 manifest)

        try:
            app_presence.register()

            if manifest.get('tickets', None):
                _get_tickets(manifest['name'], manifest, self.container_dir)

            _start_service_sup(self.container_dir)
        except exc.ContainerSetupError as err:
            app_abort.abort(self.tm_env, manifest['name'], reason=str(err))

        # If tickets are not ok, app will be aborted. Waiting for tickets
        # in the loop is harmless way to wait for that.
        #
        # If tickets acquired successfully, services will start, and
        # tickets will be refreshed after each interval.
        tkts_spool_dir = os.path.join(self.container_dir, 'root', 'var',
                                      'spool', 'tickets')

        while True:
            time.sleep(refresh_interval)
            reply = tickets.request_tickets(context.GLOBAL.zk.conn,
                                            manifest['name'])
            if reply:
                tickets.store_tickets(reply, tkts_spool_dir)
            else:
                _LOGGER.error('Error requesting tickets.')
Esempio n. 4
0
    def test_abort(self):
        """Tests abort sequence."""
        container_dir = os.path.join(self.root, 'apps', 'proid.myapp#001',
                                     'data')
        fs.mkdir_safe(container_dir)

        app_abort.abort(container_dir,
                        why=app_abort.AbortedReason.INVALID_TYPE,
                        payload='test')

        treadmill.appcfg.abort.flag_aborted.assert_called_with(
            container_dir, app_abort.AbortedReason.INVALID_TYPE, 'test')

        treadmill.supervisor.control_service.assert_called_with(
            os.path.join(self.root, 'apps', 'proid.myapp#001'),
            treadmill.supervisor.ServiceControlAction.down)
Esempio n. 5
0
    def test_abort(self):
        """Tests abort sequence."""
        context.GLOBAL.zk.url = 'zookeeper://xxx@hhh:123/treadmill/mycell'
        treadmill.zkutils.connect.return_value = kazoo.client.KazooClient()
        kazoo.client.KazooClient.get_children.return_value = []
        kazoo.client.KazooClient.exists.return_value = True

        # Check abort sequence when name is not part of the manifest, rather
        # derived from the manifest appname.
        manifest_file = os.path.join(self.root, 'schema', 'proid.myapp#001')

        kazoo.client.KazooClient.create.reset()
        kazoo.client.KazooClient.delete.reset()

        app_abort.abort(self.tm_env, manifest_file, exc=Exception('test'))
        treadmill.appevents.post.assert_called_with(
            mock.ANY,
            events.AbortedTraceEvent(
                instanceid='proid.myapp#001',
                why='Exception',
                payload=None,
            ),
        )