diff --git a/src/helpers/docker_queue.ts b/src/helpers/docker_queue.ts index b72f4894..b1883a17 100644 --- a/src/helpers/docker_queue.ts +++ b/src/helpers/docker_queue.ts @@ -284,7 +284,8 @@ const docker_cron = async () => { HostConfig: { Binds: [ `${sub_base_dir}/${queue_front.room_id}/output:/usr/local/output`, - `${sub_base_dir}/${queue_front.room_id}/source/${team_label_bind.team_id}:/usr/local/code` + `${sub_base_dir}/${queue_front.room_id}/source/${team_label_bind.team_id}:/usr/local/code`, + `${base_directory}/${contest_name}/code/${team_label_bind.team_id}/source:${base_directory}/${contest_name}/code/${team_label_bind.team_id}/source` ], AutoRemove: false, Memory: client_memory_limit * 1024 * 1024 * 1024, diff --git a/src/routes/arena.ts b/src/routes/arena.ts index 668c6174..2b6858a9 100644 --- a/src/routes/arena.ts +++ b/src/routes/arena.ts @@ -138,7 +138,7 @@ router.post("/create", authenticate(), async (req, res) => { const base_directory = await utils.get_base_directory(); const mkdir_promises = team_ids.map(team_id => { - return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}`, { recursive: true }) + return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}/source`, { recursive: true }) .then(() => { return Promise.resolve(true); }) @@ -157,7 +157,7 @@ router.post("/create", authenticate(), async (req, res) => { const files_exist_promises = player_codes_flat.map((player_code, index) => { const language = code_languages_flat[index]; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; - return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`) + return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`) .then(() => { return true; }) @@ -183,8 +183,14 @@ router.post("/create", authenticate(), async (req, res) => { const language = code_languages_flat[index_map[index]]; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; console.debug("code_file_name: ", code_file_name); - return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, - `${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, cos, config) + return fs.mkdir(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source`, { recursive: true }) + .then(() => { + return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, + `${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, cos, config) + }) + .then(() => { + return fs.chmod(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, 0o755); + }) .then(() => { return Promise.resolve(true); }) @@ -201,7 +207,7 @@ router.post("/create", authenticate(), async (req, res) => { } } - console.log("Files downloaded!") + console.log("Files downloaded!"); const map_files_count = await fs.readdir(`${base_directory}/${contest_name}/map/${map_id}`) .then(files => { @@ -233,7 +239,7 @@ router.post("/create", authenticate(), async (req, res) => { console.log("Map downloaded!"); const files_count_promises = team_ids.map(team_id => { - return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}`) + return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}/source`) .then(files => { return files.length; }) @@ -250,13 +256,13 @@ router.post("/create", authenticate(), async (req, res) => { } const files_clean_promises = team_ids.map((team_id, index) => { - if (files_count[index] < 18) { + if (files_count[index] < 10) { return Promise.resolve(true); } - return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}`) + return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}/source`) .then(files => { const files_stat_promises = files.map(file => { - return fs.stat(`${base_directory}/${contest_name}/code/${team_id}/${file}`) + return fs.stat(`${base_directory}/${contest_name}/code/${team_id}/source/${file}`) .then(stat => { return { file, stat }; }); @@ -269,9 +275,9 @@ router.post("/create", authenticate(), async (req, res) => { return !players_codes[index].includes(file_stat.file.split(".")[0]); }); - const files_stat_to_delete = files_stat_filtered.slice(0, files_stat_filtered.length - 6); + const files_stat_to_delete = files_stat_filtered.slice(0, files_stat_filtered.length - 3); const delete_promises = files_stat_to_delete.map(file_stat => { - return fs.unlink(`${base_directory}/${contest_name}/code/${team_id}/${file_stat.file}`); + return fs.unlink(`${base_directory}/${contest_name}/code/${team_id}/source/${file_stat.file}`); }); return Promise.all(delete_promises); } @@ -308,19 +314,17 @@ router.post("/create", authenticate(), async (req, res) => { console.log("Room created!") - await fs.mkdir(`${base_directory}/${contest_name}/arena/${room_id}/source`, { recursive: true }); const copy_promises = player_codes_flat.map((player_code, index) => { const language = code_languages_flat[index]; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; const arena_file_name = language === "cpp" ? `${player_labels_flat[index]}` : `${player_labels_flat[index]}.py`; return fs.mkdir(`${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}`, { recursive: true }) .then(() => { - return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`, + // return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`, + // `${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}/${arena_file_name}`) + return fs.symlink(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`, `${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}/${arena_file_name}`) }) - .then(() => { - return fs.chmod(`${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}/${arena_file_name}`, 0o755); - }) .then(() => { return Promise.resolve(true); }) diff --git a/src/routes/code.ts b/src/routes/code.ts index 9e2623ab..a1b2dc2f 100644 --- a/src/routes/code.ts +++ b/src/routes/code.ts @@ -287,7 +287,8 @@ router.post("/compile-finish", async (req, res) => { localFilePath = `${base_directory}/${contest_name}/code/${team_id}/${code_id}/output/${code_id}`; await utils.uploadObject(localFilePath, key, cos, config); try { - await fs.copyFile(localFilePath, `${base_directory}/${contest_name}/code/${team_id}/${code_id}`); + await fs.copyFile(localFilePath, `${base_directory}/${contest_name}/code/${team_id}/source/${code_id}`); + await fs.chmod(`${base_directory}/${contest_name}/code/${team_id}/source/${code_id}`, 0o755); } catch (err) { console.log("copy file failed: ", err); } diff --git a/src/routes/competition.ts b/src/routes/competition.ts index c2debb33..e1464b4b 100644 --- a/src/routes/competition.ts +++ b/src/routes/competition.ts @@ -105,7 +105,7 @@ router.post("/start-all", authenticate(), async (req, res) => { const base_directory = await utils.get_base_directory(); const mkdir_promises = team_list_available.map(team_id => { - return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}`, { recursive: true }) + return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}/source`, { recursive: true }) .then(() => { return Promise.resolve(true); }) @@ -126,7 +126,7 @@ router.post("/start-all", authenticate(), async (req, res) => { const files_exist_promises = details_list_available.map(detail => { const language = detail.language; const code_file_name = language === "cpp" ? `${detail.code_id}` : `${detail.code_id}.py`; - return fs.access(`${base_directory}/${contest_name}/code/${detail.team_id}/${code_file_name}`) + return fs.access(`${base_directory}/${contest_name}/code/${detail.team_id}/source/${code_file_name}`) .then(() => { return true; }) @@ -151,8 +151,14 @@ router.post("/start-all", authenticate(), async (req, res) => { const language = details_list_available[index_map[index]].language; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; console.debug("code_file_name: ", code_file_name); - return utils.downloadObject(`${contest_name}/code/${details_list_available[index_map[index]].team_id}/${code_file_name}`, - `${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/${code_file_name}`, cos, config) + return fs.mkdir(`${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/source`, { recursive: true }) + .then(() => { + return utils.downloadObject(`${contest_name}/code/${details_list_available[index_map[index]].team_id}/${code_file_name}`, + `${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/source/${code_file_name}`, cos, config) + }) + .then(() => { + return fs.chmod(`${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/source/${code_file_name}`, 0o755); + }) .then(() => { return Promise.resolve(true); }) @@ -264,12 +270,11 @@ router.post("/start-all", authenticate(), async (req, res) => { const competition_file_name = language === "cpp" ? `${player_labels_flat[index]}` : `${player_labels_flat[index]}.py`; return fs.mkdir(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}`, { recursive: true }) .then(() => { - return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`, + // return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`, + // `${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`) + return fs.symlink(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`, `${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`) }) - .then(() => { - return fs.chmod(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`, 0o755); - }) .then(() => { return Promise.resolve(true); }) @@ -440,7 +445,7 @@ router.post("/start-one", authenticate(), async (req, res) => { const base_directory = await utils.get_base_directory(); const mkdir_promises = team_ids.map(team_id => { - return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}`, { recursive: true }) + return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}/source`, { recursive: true }) .then(() => { return Promise.resolve(true); }) @@ -459,7 +464,7 @@ router.post("/start-one", authenticate(), async (req, res) => { const files_exist_promises = player_codes_flat.map((player_code, index) => { const language = code_languages_flat[index]; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; - return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`) + return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`) .then(() => { return true; }) @@ -485,8 +490,14 @@ router.post("/start-one", authenticate(), async (req, res) => { const language = code_languages_flat[index_map[index]]; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; console.debug("code_file_name: ", code_file_name); - return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, - `${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, cos, config) + return fs.mkdir(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source`, { recursive: true }) + .then(() => { + return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, + `${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, cos, config); + }) + .then(() => { + return fs.chmod(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, 0o755); + }) .then(() => { return Promise.resolve(true); }) @@ -604,19 +615,17 @@ router.post("/start-one", authenticate(), async (req, res) => { console.log("Room created!") - await fs.mkdir(`${base_directory}/${contest_name}/competition/${room_id}/source`, { recursive: true }); const copy_promises = player_codes_flat.map((player_code, index) => { const language = code_languages_flat[index]; const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`; const competition_file_name = language === "cpp" ? `${player_labels_flat[index]}` : `${player_labels_flat[index]}.py`; return fs.mkdir(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}`, { recursive: true }) .then(() => { - return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`, + // return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`, + // `${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`) + return fs.symlink(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`, `${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`) }) - .then(() => { - return fs.chmod(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`, 0o755); - }) .then(() => { return Promise.resolve(true); })