Ejemplo n.º 1
0
 def handle(self, *args, **options):
     from django.conf import settings
     from docker_sandboxer.scheduler import CPUScheduler
     cpu_scheduler = CPUScheduler(settings.CPU_MANAGER_REDIS_HOST,
                                  settings.CPU_MANAGER_REDIS_PORT,
                                  options["redis_db"])
     cpu_scheduler.add_ases_available_cpus(options["cores"])
Ejemplo n.º 2
0
 def handle(self, *args, **options):
     from django.conf import settings
     from docker_sandboxer.scheduler import CPUScheduler
     cpu_scheduler = CPUScheduler(settings.CPU_MANAGER_REDIS_HOST,
                                  settings.CPU_MANAGER_REDIS_PORT,
                                  options["redis_db"])
     cpu_scheduler.print_status()
Ejemplo n.º 3
0
 def handle(self, *args, **options):
     from django.conf import settings
     from docker_sandboxer.scheduler import CPUScheduler
     cpu_scheduler = CPUScheduler(
             settings.CPU_MANAGER_REDIS_HOST,
             settings.CPU_MANAGER_REDIS_PORT,
             options["redis_db"]
     )
     cpu_scheduler.add_ases_available_cpus(options["cores"])
Ejemplo n.º 4
0
def run_game_unsafe(self, game):
    competition = game.players.all(
    )[0].team.competition  # this is the worst modeling I've ever seen! :D
    competition_dir = os.path.join(GAMES_ROOT, 'competitions',
                                   str(competition.id))
    game_dir = os.path.join(competition_dir, str(game.id))
    log_dir = os.path.join(game_dir, 'logs')

    # yml context
    print('preparing yml context')

    context = {
        'server': {
            'image_id': competition.server.get_image_id(),
            'sandboxer': competition.server.get_sandboxer(),
            'game_config': open_and_get_path(game.game_config.config),
        },
        'logger': {
            'image_id': competition.logger.get_image_id(),
            'sandboxer': competition.logger.get_sandboxer(),
            'token': generate_random_token(),
            'log_root': log_dir,
            'log_file': os.path.join(log_dir, 'game.log'),
            'scores_file': os.path.join(log_dir, 'game.scr')
        },
        'clients': [{
            'image_id': submit.lang.execute_container.get_image_id(),
            'sandboxer': submit.lang.execute_container.get_sandboxer(),
            'id': submit.team.id,
            'token': generate_random_token(),
            'code': open_and_get_path(submit.compiled_code),
            'submit': submit,
        } for submit in game.players.all()],
        'additional_containers': [{
            'image_id': container.get_image_id(),
            'sandboxer': container.get_sandboxer(),
            'tag': container.tag,
            'token': generate_random_token(),
        } for container in competition.additional_containers.all()],
    }

    # prepare game files
    print('preparing game files')
    make_dir(game_dir)
    try:
        for client in context['clients']:
            code = client['submit'].compiled_code
            code.open()
            code.close()
    except IOError:
        raise self.retry(countdown=settings.FILE_SYNC_DELAY_SECONDS,
                         max_retries=settings.FILE_SYNC_DELAY_MAX_RETRIES)

    # run!
    print('creating cpu scheduler & parser')
    cpu_scheduler = CPUScheduler(db=settings.CPU_MANAGER_REDIS_GAME_RUNNER_DB)
    parser = Parser(cpu_scheduler, settings.GAME_DOCKER_COMPOSE_YML_ROOT,
                    settings.GAME_DOCKER_COMPOSE_YML_LOG_ROOT)

    def set_game_running():
        game.status = 2
        game.save()

    try:
        print('waiting for resources')
        parser.create_yml_and_run(str(game.id),
                                  "run_game.yml",
                                  context,
                                  timeout=competition.execution_time_limit,
                                  callback_before_run=set_game_running)
        print('game finished, saving the results')
    except TimeoutError:
        print('game timeout exceeded')

    try:
        with open(context['logger']['log_file']) as log_file:
            game.log_file.save('games/logs/game_%d.log' % game.id,
                               File(log_file),
                               save=True)
    except IOError as e:
        print('game log file does not exists.')
        raise e

    try:
        with open(context['logger']['scores_file']) as scores_file:
            scores = json.load(scores_file)
            for i, client in enumerate(context['clients']):
                gts = GameTeamSubmit.objects.get(game=game,
                                                 submit=client['submit'])
                gts.score = scores[i]
                gts.save()
    except IOError as e:
        print('game score file does not exists.')
        raise e

    game.status = 3
    game.error_log = ''
    game.save()
    print('saving completed')
Ejemplo n.º 5
0
def compile_code(self, submit_id):
    submit = Submit.objects.get(pk=submit_id)
    submit.status = 1
    submit.save()

    container = submit.lang.compile_container
    cpu_scheduler = CPUScheduler(
        db=settings.CPU_MANAGER_REDIS_CODE_COMPILER_DB)
    parser = Parser(cpu_scheduler, settings.COMPILE_DOCKER_YML_ROOT,
                    settings.COMPILE_DOCKER_YML_LOG_ROOT)

    # make sure submitted code is synced
    try:
        submit.code.open()
    except IOError as exc:
        raise self.retry(countdown=settings.FILE_SYNC_DELAY_SECONDS,
                         max_retries=settings.FILE_SYNC_DELAY_MAX_RETRIES)
    submit_code = os.path.join(settings.MEDIA_ROOT, str(submit.code))
    submit.code.close()

    compile_context = {
        'code_image': container.get_image_id(),
        'code_zip': submit_code,
        'code_log': submit_code + '_log',
        'code_compile': submit_code + '_compiled',
        'sandboxer': container.get_sandboxer(),
    }

    def set_submit_compiling():
        submit.status = 2
        submit.save()

    try:
        parser.create_yml_and_run(
            str(submit_id),
            "compile.yml",
            compile_context,
            timeout=submit.team.competition.compile_time_limit,
            callback_before_run=set_submit_compiling)

        with open(compile_context['code_log'] + '/status.log') as data_file:
            data = json.load(data_file)

        print('errors: %s' % str(data['errors']))
        submit.status = 4 if data['errors'] else 3
        if data['errors']:
            error = 'Error at stage %d of compile:\n' % data['stage']
            error += '\n'.join([str(err) for err in data['errors']])
        else:
            error = 'OK'
            compile_code_name = submit_code + '_compiled' + '/compiled.zip'
            submit.team.final_submission = submit
            submit.team.save()
            with open(compile_code_name, 'rb') as compile_code_file:
                submit.compiled_code.save(str(submit.id) + '_compiled.zip',
                                          File(compile_code_file),
                                          save=True)
            print(submit.compiled_code)
        submit.compile_log_file = error[:1000]
    except TimeoutError:
        submit.status = 4
        submit.compile_log_file = 'Compile time limit exceeded.'
        data = 'Compile time limit exceeded.'

    submit.save()
    print("Compile end %s" % data)