示例#1
0
    def test_create_script_linux(self):
        """this tests the create_script function.

        the function creates executable scripts from templates that exist
        in the template directory.
        """
        script_file = os.path.join(self.root, 'script')
        # Function we are testing
        templates.create_script(script_file,
                                's6.run',
                                user='******',
                                home='home',
                                shell='shell',
                                _alias={
                                    's6_setuidgid': '/test/s6-setuidgid',
                                })

        # Read the output from the mock filesystem
        with io.open(script_file) as script:
            data = script.read()

        # Validate that data is what it should be
        self.assertTrue(data.index('/test/s6-setuidgid testproid') > 0)

        # Validate that the file is +x
        self.assertEqual(utils.os.stat(script_file).st_mode, 33261)
示例#2
0
    def test_create_script_perms_linux(self):
        """this tests the create_script function (permissions).
        """
        script_file = os.path.join(self.root, 'script')
        # Test non-default mode (+x)
        mode = (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)

        templates.create_script(script_file,
                                's6.run',
                                mode=mode,
                                user='******',
                                home='home',
                                shell='shell',
                                _alias={
                                    's6_setuidgid': '/test/s6-setuidgid',
                                })

        self.assertEqual(utils.os.stat(script_file).st_mode, 33060)
示例#3
0
    def _create_instance(self, path):
        """Create an spawn instance."""
        job, bucket, running = spawn_utils.get_instance_path(path, self.paths)

        _LOGGER.debug('Creating - (%r, %r)', job, running)

        if os.path.exists(running):
            _LOGGER.debug('Create %r failed - already exists', running)
            return

        inst = instance.Instance(path)
        data_dir = os.path.join(job, spawn.JOB_DATA_DIR)

        fs.mkdir_safe(job)
        fs.mkdir_safe(data_dir)

        templates.create_script(os.path.join(job, 'run'),
                                'spawn.run',
                                id=inst.id,
                                name=inst.name,
                                service_exit=inst.settings.get(
                                    'service_exit', False),
                                **subproc.get_aliases())

        templates.create_script(
            os.path.join(job, 'finish'),
            'spawn.finish',
            id=inst.id,
            stop=inst.settings.get('stop', False),
            reconnect=inst.settings.get('reconnect', False),
            reconnect_timeout=inst.settings.get('reconnect_timeout', 0),
            **subproc.get_aliases())

        with io.open(os.path.join(data_dir, 'manifest'), 'w') as f:
            f.writelines(utils.json_genencode(inst.manifest))

        with io.open(os.path.join(job, 'timeout-finish'), 'w') as f:
            f.write(six.text_type(spawn.JOB_FINISH_TIMEOUT))

        fs.symlink_safe(running, job)

        self._scan(bucket)
示例#4
0
    def create(self):
        """Create the directory structure for the tree."""
        env = {
            'TREADMILL_SPAWN_ZK2FS': self.paths.zk_mirror_dir,
            'TREADMILL_SPAWN_ZK2FS_SHARDS':
            str(zknamespace.TRACE_SHARDS_COUNT),
            'TREADMILL_SPAWN_ZK2FS_SOW': app_zk.TRACE_SOW_DIR,
            'TREADMILL_SPAWN_CELLAPI_SOCK': self.paths.cellapi_sock,
            'TREADMILL_SPAWN_CLEANUP': self.paths.cleanup_dir,
        }
        supervisor.create_environ_dir(self.paths.env_dir, env)

        dirs = set()
        for bucket in range(self.buckets):
            bucket_formatted = spawn_utils.format_bucket(bucket)
            dirs.add(bucket_formatted)

            _LOGGER.debug('Adding bucket %r.', bucket_formatted)

            app = os.path.join(self.paths.svscan_tree_dir, bucket_formatted)
            log = os.path.join(app, 'log')

            running = os.path.join(self.paths.running_dir, bucket_formatted)
            svscan = os.path.join(running, '.s6-svscan')

            fs.mkdir_safe(app)
            fs.mkdir_safe(log)
            fs.mkdir_safe(running)
            fs.mkdir_safe(svscan)

            templates.create_script(os.path.join(app, 'run'),
                                    's6.svscan.run',
                                    max=self.max_per_bucket,
                                    service_dir=running,
                                    _alias=subproc.get_aliases())

            templates.create_script(os.path.join(log, 'run'),
                                    's6.logger.run',
                                    logdir='.',
                                    logger_args='-b T n20 s1000000',
                                    _alias=subproc.get_aliases())

            templates.create_script(os.path.join(svscan, 'finish'),
                                    's6.svscan.finish',
                                    timeout=4800,
                                    _alias=subproc.get_aliases())

        for app_dir in os.listdir(self.paths.svscan_tree_dir):
            if not app_dir.startswith('.') and app_dir not in dirs:
                path = os.path.join(self.paths.svscan_tree_dir, app_dir)
                _LOGGER.debug('Removing bucket (%r) %r.',
                              spawn.SVSCAN_TREE_DIR, path)
                shutil.rmtree(path, ignore_errors=True)

        for run_dir in os.listdir(self.paths.running_dir):
            if not run_dir.startswith('.') and run_dir not in dirs:
                path = os.path.join(self.paths.running_dir, run_dir)
                _LOGGER.debug('Removing bucket (%r))  %r.', spawn.RUNNING_DIR,
                              path)
                shutil.rmtree(path, ignore_errors=True)
示例#5
0
def create_supervision_tree(tm_env, container_dir, root_dir, app,
                            cgroups_path):
    """Creates s6 supervision tree."""
    uniq_name = appcfg.app_unique_name(app)
    ctl_uds = os.path.join(os.sep, 'run', 'tm_ctl')
    tombstone_ctl_uds = os.path.join(ctl_uds, 'tombstone')

    sys_dir = os.path.join(container_dir, 'sys')

    try:
        old_system_services = [
            svc_name for svc_name in os.listdir(sys_dir)
            if (not svc_name.startswith('.') and
                os.path.isdir(os.path.join(sys_dir, svc_name)))
        ]
    except FileNotFoundError:
        old_system_services = []

    new_system_services = [svc_def.name for svc_def in app.system_services]

    for svc_name in set(old_system_services) - set(new_system_services):
        _LOGGER.info('Removing old system service: %s', svc_name)
        fs.rmtree_safe(os.path.join(sys_dir, svc_name))

    sys_scandir = supervisor.create_scan_dir(
        sys_dir,
        finish_timeout=6000,
        wait_cgroups=cgroups_path,
    )
    for svc_def in app.system_services:
        if svc_def.restart is not None:
            monitor_policy = {
                'limit': svc_def.restart.limit,
                'interval': svc_def.restart.interval,
                'tombstone': {
                    'uds': False,
                    'path': tm_env.services_tombstone_dir,
                    'id': '{},{}'.format(uniq_name, svc_def.name)
                }
            }
        else:
            monitor_policy = None

        supervisor.create_service(
            sys_scandir,
            name=svc_def.name,
            app_run_script=svc_def.command,
            userid='root',
            environ_dir=os.path.join(container_dir, _CONTAINER_ENV_DIR),
            environ={
                envvar.name: envvar.value
                for envvar in svc_def.environ
            },
            environment=app.environment,
            downed=svc_def.downed,
            trace=None,
            monitor_policy=monitor_policy
        )
    sys_scandir.write()

    services_dir = os.path.join(container_dir, 'services')
    finish_commands = getattr(app, 'finish_commands', [])
    services_scandir = supervisor.create_scan_dir(
        services_dir,
        finish_timeout=5000,
        finish_commands=finish_commands
    )

    for svc_def in app.services:

        if svc_def.restart is not None:
            monitor_policy = {
                'limit': svc_def.restart.limit,
                'interval': svc_def.restart.interval,
                'tombstone': {
                    'uds': True,
                    'path': tombstone_ctl_uds,
                    'id': '{},{}'.format(uniq_name, svc_def.name)
                }
            }
        else:
            monitor_policy = None

        if svc_def.trace is not None:
            trace = {
                'instanceid': app.name,
                'uniqueid': app.uniqueid,
                'service': svc_def.name,
                'path': os.path.join(ctl_uds, 'appevents')
            }
        else:
            trace = None

        logger_template = getattr(svc_def, 'logger', 's6.app-logger.run')
        _LOGGER.info('Using logger: %s', logger_template)

        supervisor.create_service(
            services_scandir,
            name=svc_def.name,
            app_run_script=svc_def.command,
            userid=svc_def.proid,
            environ_dir='/' + _CONTAINER_ENV_DIR,
            environ={
                envvar.name: envvar.value
                for envvar in svc_def.environ
            },
            environment=app.environment,
            downed=svc_def.downed,
            trace=trace if svc_def.trace else None,
            log_run_script=logger_template,
            monitor_policy=monitor_policy
        )
    services_scandir.write()

    # Create services startup script.
    boot_commands = getattr(app, 'boot_commands', [])
    templates.create_script(
        os.path.join(container_dir, 'services', supervisor.SVC_INIT_FILE),
        's6.init',
        boot_commands=boot_commands,
        _alias=subproc.get_aliases(),
    )

    # Bind the service directory in the container volume
    fs.mkdir_safe(os.path.join(root_dir, 'services'))
    fs_linux.mount_bind(
        root_dir, os.path.join(os.sep, 'services'),
        source=os.path.join(container_dir, 'services'),
        recursive=False, read_only=False
    )

    # Bind the ctrl directory in the container volume which has all the
    # unix domain sockets to communicate outside the container to treadmill
    fs.mkdir_safe(os.path.join(root_dir, 'run', 'tm_ctl'))
    fs_linux.mount_bind(
        root_dir, os.path.join(os.sep, 'run', 'tm_ctl'),
        source=tm_env.ctl_dir,
        recursive=False, read_only=False
    )