예제 #1
0
async def isolate_communicate(cmdline,
                              limits=None,
                              allowed_dirs=None,
                              **kwargs):
    isolator = isolate.Isolator(limits, allowed_dirs=allowed_dirs)
    async with isolator:
        await isolator.run(cmdline, **kwargs)
    return isolator
예제 #2
0
async def spawn_server(config, rep_addr, pub_addr, nb_players, sockets_dir,
                       map_contents):
    # Build command
    # yapf: disable
    cmd = [
        config['path']['stechec_server'],
        "--rules", config['path']['rules'],
        "--rep_addr", rep_addr,
        "--pub_addr", pub_addr,
        "--nb_clients", str(nb_players),
        "--time", "3000",
        "--socket_timeout", "45000",
        "--dump", "/box/dump.json",
        "--replay", "/box/replay",
        # "--stats", "/box/stats.yaml",
        "--verbose", "1",
    ]
    # yapf: enable

    if map_contents is not None:
        f = tempfile.NamedTemporaryFile(mode='w')
        f.write(map_contents)
        f.flush()
        os.chmod(f.name, 0o644)
        cmd += ['--map', f.name]

    # Create the isolator
    limits = {'wall-time': config['timeout'].get('server', 400)}
    allowed_dirs = [
        '/var',
        '/etc',
        '/tmp',
        sockets_dir + ':rw',
        os.path.dirname(config['path']['stechec_server']),
        os.path.dirname(config['path']['rules']),
    ]
    isolator = isolate.Isolator(limits, allowed_dirs=allowed_dirs)
    async with isolator:
        # Run the isolated server
        await isolator.run(cmd, merge_outputs=True)

        # Retrieve the dump
        gzdump = isolator_compress(isolator, cmd, "dump.json")
        # Retrive the replay
        gzreplay = isolator_compress(isolator, cmd, "replay")
        # # Retrive the stats
        # gzstats = isolator_compress(isolator, cmd, "stats.yaml")
        gzstats = b''

    # Retrieve the output
    output = get_output(isolator)
    if isolator.isolate_retcode != 0:
        raise_isolate_error("server: exited with a non-zero code", cmd,
                            isolator)

    return output, gzdump, gzreplay, gzstats
예제 #3
0
async def spawn_server(config, rep_addr, pub_addr, nb_players, sockets_dir,
                       opts, file_opts):
    # Build command
    cmd = [
        config['path']['stechec_server'],
        "--rules",
        config['path']['rules'],
        "--rep_addr",
        rep_addr,
        "--pub_addr",
        pub_addr,
        "--nb_clients",
        str(nb_players),
        "--time",
        "3000",
        "--socket_timeout",
        "45000",
        "--dump",
        "/box/dump.json",
        "--verbose",
        "1",
    ]

    if opts is not None:
        cmd += opts
    if file_opts is not None:
        fopts, tmp_files = create_file_opts(file_opts)
        cmd.extend(fopts)

    # Create the isolator
    limits = {'wall-time': config['timeout'].get('server', 400)}
    isolator = isolate.Isolator(
        limits, allowed_dirs=['/var', '/tmp', sockets_dir + ':rw'])
    async with isolator:
        # Run the isolated server
        await isolator.run(cmd, merge_outputs=True)

        # Retrieve the dump and gz-compress it
        try:
            dump_path = isolator.path / 'dump.json'
            with dump_path.open('rb') as dump:
                gzdump = gzip.compress(dump.read())
        except FileNotFoundError:
            raise_isolate_error("server: dump.json was not created.\n", cmd,
                                isolator)

    # Retrieve the output
    output = get_output(isolator)
    if isolator.isolate_retcode != 0:
        raise_isolate_error("server: exited with a non-zero code", cmd,
                            isolator)
    return output, gzdump
예제 #4
0
async def compile_champion(config, ctgz):
    """
    Compiles the champion contained in ctgz and returns a tuple TODO

    """
    ctgz = b64decode(ctgz)
    code_dir = os.path.abspath(os.path.dirname(__file__))
    compile_script = os.path.join(code_dir, 'compile-champion.sh')

    limits = {
        'wall-time': config['timeout'].get('compile', 400),
        'fsize': 50 * 1024
    }
    allowed_dirs = ['/tmp:rw', code_dir, '/etc']

    isolator = isolate.Isolator(limits, allowed_dirs=allowed_dirs)
    async with isolator:
        compiled_path = isolator.path / 'champion-compiled.tar.gz'
        log_path = isolator.path / 'compilation.log'
        with (isolator.path / 'champion.tgz').open('wb') as f:
            f.write(ctgz)

        cmd = [compile_script, config['path']['makefiles'], '/box']
        await isolator.run(cmd)
        ret = isolator.isolate_retcode == 0

        compilation_content = b''
        if ret:
            try:
                with compiled_path.open('rb') as f:
                    compilation_content = f.read()
            except FileNotFoundError:
                ret = False

        log = ''
        try:
            with log_path.open('r') as f:
                log = f.read()
        except FileNotFoundError:
            pass

    b64compiled = b64encode(compilation_content).decode()
    return ret, b64compiled, log