示例#1
0
    def test_configure(self, mock_load):
        """Tests that appcfg.configure creates necessary s6 layout."""
        manifest = {
            'proid':
            'foo',
            'environment':
            'dev',
            'shared_network':
            False,
            'cpu':
            '100',
            'memory':
            '100M',
            'disk':
            '100G',
            'services': [
                {
                    'name': 'web_server',
                    'command': '/bin/true',
                    'restart': {
                        'limit': 5,
                        'interval': 60,
                    },
                },
            ],
            'endpoints': [
                {
                    'name': 'http',
                    'port': '8000',
                },
            ],
            'name':
            'proid.myapp#0',
            'uniqueid':
            'AAAAA',
        }
        mock_load.return_value = manifest
        app_unique_name = 'proid.myapp-0-00000000AAAAA'
        app_dir = os.path.join(self.root, 'apps', app_unique_name)

        app_cfg.configure(self.tm_env, '/some/event')

        mock_load.assert_called_with(self.tm_env, '/some/event')
        shutil.copyfile.assert_called_with(
            '/some/event', os.path.join(app_dir, 'manifest.yml'))
        self.assertTrue(os.path.exists(os.path.join(app_dir, 'run')))
        self.assertTrue(os.path.exists(os.path.join(app_dir, 'finish')))
        self.assertTrue(os.path.exists(os.path.join(app_dir, 'log', 'run')))

        treadmill.appevents.post.assert_called_with(
            mock.ANY,
            events.ConfiguredTraceEvent(instanceid='proid.myapp#0',
                                        uniqueid='AAAAA',
                                        payload=None))
示例#2
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
示例#3
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,
                                                  self._runtime)
                if container_dir is None:
                    # configure step failed, skip.
                    fs.rm_safe(event_file)
                    return False

                # symlink_safe(link, target)
                fs.symlink_safe(
                    os.path.join(self.tm_env.running_dir, instance_name),
                    container_dir
                )
                return True

            except exc.ContainerSetupError as err:  # pylint: disable=W0703
                _LOGGER.exception('Error configuring (%r)', instance_name)
                app_abort.report_aborted(self.tm_env, instance_name,
                                         why=err.reason,
                                         payload=traceback.format_exc())
                fs.rm_safe(event_file)
                return False
            except Exception as err:  # pylint: disable=W0703
                _LOGGER.exception('Error configuring (%r)', instance_name)
                app_abort.report_aborted(self.tm_env, instance_name,
                                         why=app_abort.AbortedReason.UNKNOWN,
                                         payload=traceback.format_exc())
                fs.rm_safe(event_file)
                return False
示例#4
0
    def test_configure_linux(self, mock_create_svc, mock_load):
        """Tests that appcfg.configure creates necessary s6 layout."""
        manifest = {
            'proid':
            'foo',
            'environment':
            'dev',
            'shared_network':
            False,
            'cpu':
            '100',
            'memory':
            '100M',
            'disk':
            '100G',
            'services': [
                {
                    'name': 'web_server',
                    'command': '/bin/true',
                    'restart': {
                        'limit': 5,
                        'interval': 60,
                    },
                },
            ],
            'environ': [
                {
                    'name': 'Hello',
                    'value': 'World!',
                },
            ],
            'zookeeper':
            'foo',
            'cell':
            'cell',
            'system_services': [],
            'endpoints': [
                {
                    'name': 'http',
                    'port': '8000',
                },
            ],
            'name':
            'proid.myapp#0',
            'uniqueid':
            'AAAAA',
        }
        mock_load.return_value = manifest
        app_unique_name = 'proid.myapp-0-00000000AAAAA'
        app_dir = os.path.join(self.root, 'apps', app_unique_name)
        mock_create_svc.return_value.data_dir = os.path.join(app_dir, 'data')

        app_cfg.configure(self.tm_env, '/some/event', 'linux')

        mock_load.assert_called_with('/some/event')
        mock_create_svc.assert_called_with(
            self.tm_env.apps_dir,
            name=app_unique_name,
            app_run_script=mock.ANY,
            downed=False,
            monitor_policy={
                'limit': 0,
                'interval': 60,
                'tombstone': {
                    'uds': False,
                    'path': self.tm_env.running_tombstone_dir,
                    'id': 'proid.myapp#0'
                }
            },
            userid='root',
            environ={},
            environment='dev')
        treadmill.fs.write_safe.assert_called_with(os.path.join(
            app_dir, 'data', 'app.json'),
                                                   mock.ANY,
                                                   mode='w',
                                                   permission=0o644)

        shutil.copyfile.assert_called_with(
            '/some/event', os.path.join(app_dir, 'data', 'manifest.yml'))

        treadmill.appevents.post.assert_called_with(
            mock.ANY,
            events.ConfiguredTraceEvent(instanceid='proid.myapp#0',
                                        uniqueid='AAAAA',
                                        payload=None))
示例#5
0
    def test_configure_linux_event_rm(self, mock_create_svc, mock_load):
        """Tests when event file is removed when copied."""
        manifest = {
            'proid':
            'foo',
            'environment':
            'dev',
            'shared_network':
            False,
            'cpu':
            '100',
            'memory':
            '100M',
            'disk':
            '100G',
            'services': [
                {
                    'name': 'web_server',
                    'command': '/bin/true',
                    'restart': {
                        'limit': 5,
                        'interval': 60,
                    },
                },
            ],
            'system_services': [],
            'endpoints': [
                {
                    'name': 'http',
                    'port': '8000',
                },
            ],
            'environ': [
                {
                    'name': 'Hello',
                    'value': 'World!',
                },
            ],
            'cell':
            'cell',
            'zookeeper':
            'foo',
            'name':
            'proid.myapp#0',
            'uniqueid':
            'AAAAA',
        }
        mock_load.return_value = manifest
        app_unique_name = 'proid.myapp-0-00000000AAAAA'
        app_dir = os.path.join(self.root, 'apps', app_unique_name)
        mock_create_svc.return_value.directory = app_dir
        mock_create_svc.return_value.data_dir = os.path.join(app_dir, 'data')

        shutil.copyfile.side_effect = IOError(2, 'No such file or directory')

        app_cfg.configure(self.tm_env, '/some/event', 'linux')

        mock_load.assert_called_with('/some/event')
        mock_create_svc.assert_called_with(
            self.tm_env.apps_dir,
            name=app_unique_name,
            app_run_script=mock.ANY,
            downed=False,
            monitor_policy={
                'limit': 0,
                'interval': 60,
                'tombstone': {
                    'uds': False,
                    'path': self.tm_env.running_tombstone_dir,
                    'id': 'proid.myapp#0'
                }
            },
            userid='root',
            environ={},
            environment='dev')

        shutil.copyfile.assert_called_with(
            '/some/event', os.path.join(app_dir, 'data', 'manifest.yml'))

        treadmill.fs.write_safe.assert_not_called()
        shutil.rmtree.assert_called_with(app_dir)

        treadmill.appevents.post.assert_not_called()
示例#6
0
    def configure(approot, runtime, eventfile):
        """Configure local manifest and schedule app to run."""
        tm_env = appenv.AppEnvironment(root=approot)

        container_dir = app_cfg.configure(tm_env, eventfile, runtime)
        _LOGGER.info('Configured %r', container_dir)