Exemplo n.º 1
0
def create_jobs(settings):
    jobs = []
    if "jobs" in settings:
        for job in settings["jobs"]:
            required_files = []
            commands = []
            return_files = []

            if "required_files" in job:
                for required_file in job["required_files"]:
                    req_file = TransferableFile(local_path=required_file["local_path"],
                                                remote_path=required_file["remote_path"])
                    required_files.append(req_file)

            if "return_files" in job:
                for return_file in job["return_files"]:
                    ret_file = TransferableFile(local_path=return_file["local_path"],
                                                remote_path=return_file["remote_path"])
                    return_files.append(ret_file)

            if "commands" in job:
                for command in job["commands"]:
                    commands.append(command)

            if "id" in job:
                job_id = job["id"]
            else:
                job_id = "job_{id}".format(id=random_hex_string(10))

            jobs.append(
                Job(commands=commands,
                    required_files=required_files,
                    return_files=return_files,
                    id=job_id))
    return jobs
    def _generate_job(self, red_team, blue_team, layout):
        """
        Generates a job command to play red_team against blue team in a layout. This job is run inside the sandbox
        folder for this particular game (e.g., /tmp/cluster_instance_xxxx)

        :param red_team: the path to the red team (e.g., teams/targethdplus/myTeam.py)
        :param blue_team: the path to the blue team (e.g., teams/targethdplus/myTeam.py)
        :param layout: the name of the layout (e.g., RANDOM2737)
        :return: a Job() object with the job to be scheduled in cluster
        """
        red_team_name, _ = red_team
        blue_team_name, _ = blue_team

        game_command = self._generate_command(red_team, blue_team, layout)

        deflate_command = "mkdir -p {contest_dir} ; unzip -o {zip_file} -d {contest_dir} ; chmod +x -R *".format(
            zip_file=os.path.join("/tmp", CORE_CONTEST_TEAM_ZIP_FILE),
            contest_dir=self.tmp_dir,
        )

        command = "{deflate_command} ; cd {contest_dir} ; {game_command} ; touch {replay_filename}".format(
            deflate_command=deflate_command,
            contest_dir=self.tmp_dir,
            game_command=game_command,
            replay_filename="replay-0",
        )

        replay_file_name = "{red_team_name}_vs_{blue_team_name}_{layout}.replay".format(
            layout=layout,
            run_id=self.contest_timestamp_id,
            red_team_name=red_team_name,
            blue_team_name=blue_team_name,
        )

        log_file_name = "{red_team_name}_vs_{blue_team_name}_{layout}.log".format(
            layout=layout,
            run_id=self.contest_timestamp_id,
            red_team_name=red_team_name,
            blue_team_name=blue_team_name,
        )

        ret_file_replay = TransferableFile(
            local_path=os.path.join(self.tmp_replays_dir, replay_file_name),
            remote_path=os.path.join(self.tmp_dir, "replay-0"),
        )
        ret_file_log = TransferableFile(
            local_path=os.path.join(self.tmp_logs_dir, log_file_name),
            remote_path=os.path.join(self.tmp_dir, "log-0"),
        )

        return Job(
            command=command,
            required_files=[],
            return_files=[ret_file_replay, ret_file_log],
            data=(red_team, blue_team, layout),
            id="{}-vs-{}-in-{}".format(red_team_name, blue_team_name, layout),
        )
Exemplo n.º 3
0
def config_host(sudoer_user, hostname):
    # print('Connecting to {hostname} as {user}'.format(hostname=hostname, user=sudoer_user))
    host_as_sudoer = Host(no_cpu=4,
                          hostname=hostname,
                          username=sudoer_user,
                          password=None,
                          key_filename=private_key_filename)
    worker_as_sudoer = create_worker(host_as_sudoer)
    job1 = Job(command='sudo bash {config_pt1_filename}'.format(
        config_pt1_filename=CONFIG_PT1_FILENAME, user=USER),
               required_files=[
                   TransferableFile(local_path=CONFIG_PT1_FILENAME,
                                    remote_path=CONFIG_PT1_FILENAME),
                   TransferableFile(
                       local_path=USER_PUBLIC_KEY_FILE_PATH,
                       remote_path=os.path.basename(USER_PUBLIC_KEY_FILE_PATH))
               ],
               return_files=[],
               id=None)
    _, _, out_pt1, err_pt1, _ = run_job_on_worker(worker_as_sudoer, job1)
    with open('log_{hostname}_pt1.txt'.format(hostname=hostname), 'w') as f:
        f.write(out_pt1 + err_pt1)

    # print('Connecting to {hostname} as {user}'.format(hostname=hostname, user=USER))
    host_as_new_user = Host(no_cpu=4,
                            hostname=hostname,
                            username=USER,
                            password=None,
                            key_filename=USER_PRIVATE_KEY_FILE_PATH)
    worker_as_new_user = create_worker(host_as_new_user)
    job2 = Job(command='bash {config_pt2_filename}'.format(
        config_pt2_filename=CONFIG_PT2_FILENAME, user=USER),
               required_files=[
                   TransferableFile(local_path=CONFIG_PT2_FILENAME,
                                    remote_path=CONFIG_PT2_FILENAME)
               ],
               return_files=[],
               id=None)
    _, _, out_pt2, err_pt2, _ = run_job_on_worker(worker_as_new_user, job2)
    with open('log_{hostname}_pt2.txt'.format(hostname=hostname), 'w') as f:
        f.write(out_pt2 + err_pt2)
    def _generate_empty_job(self, red_team, blue_team, layout):
        red_team_name, _ = red_team
        blue_team_name, _ = blue_team

        command = ""

        return Job(
            command=command,
            required_files=[],
            return_files=[],
            data=(red_team, blue_team, layout),
            id="{}-vs-{}-in-{}".format(red_team_name, blue_team_name, layout),
        )