From 489a374445c9b4beaaa4c244a0478516c32400b0 Mon Sep 17 00:00:00 2001 From: undefined Date: Thu, 10 Nov 2022 19:37:45 +0800 Subject: [PATCH] judge: use new config path by default --- install/install.sh | 2 +- install/install.ts | 82 ++++++++++++++++++++++++------- packages/hydrojudge/package.json | 2 +- packages/hydrojudge/src/config.ts | 5 +- 4 files changed, 71 insertions(+), 20 deletions(-) diff --git a/install/install.sh b/install/install.sh index d74f5cac..895eeb27 100644 --- a/install/install.sh +++ b/install/install.sh @@ -23,5 +23,5 @@ cat >/tmp/install.b64 << EOF123 %PLACEHOLDER% EOF123 cat /tmp/install.b64 | base64 -d >>/tmp/install.js -node /tmp/install.js +node /tmp/install.js "$@" set +e diff --git a/install/install.ts b/install/install.ts index ce168821..0d556881 100644 --- a/install/install.ts +++ b/install/install.ts @@ -34,6 +34,7 @@ const locales = { 'install.hydro': '正在安装 Hydro...', 'install.done': 'Hydro 安装成功!', 'install.alldone': '安装已全部完成。', + 'install.editJudgeConfigAndStart': '请编辑 ~/.hydro/judge.yaml 后使用 pm2 start hydrojudge && pm2 save 启动。', 'extra.dbUser': '数据库用户名: hydro', 'extra.dbPassword': '数据库密码: %s', 'info.skip': '步骤已跳过。', @@ -51,11 +52,20 @@ const locales = { 'install.compiler': 'Installing compiler...', 'install.hydro': 'Installing Hydro...', 'install.done': 'Hydro installation completed!', + 'install.editJudgeConfigAndStart': 'Please edit config at ~/.hydro/judge.yaml than start hydrojudge with:\npm2 start hydrojudge && pm2 save.', 'extra.dbUser': 'Database username: hydro', 'extra.dbPassword': 'Database password: %s', 'info.skip': 'Step skipped.', }, }; + +const installAsJudge = process.argv.includes('--judge'); +const noCaddy = process.argv.includes('--no-caddy'); +const installTarget = installAsJudge + ? '@hydrooj/hydrojudge' + : 'hydrooj @hydrooj/hydrojudge @hydrooj/ui-default @hydrooj/fps-importer'; +const addons = ['@hydrooj/ui-default', '@hydrooj/hydrojudge', '@hydrooj/fps-importer']; + let locale = process.env.LANG?.includes('zh') ? 'zh' : 'en'; if (process.env.TERM === 'linux') locale = 'en'; const processLog = (orig) => (str, ...args) => (orig(locales[locale][str] || str, ...args), 0); @@ -76,12 +86,12 @@ const values = {}; for (const line of lines) { if (!line.trim()) continue; const d = line.split('='); - if (d[1].startsWith('"')) values[d[0].toLowerCase()] = d[1].substr(1, d[1].length - 2); + if (d[1].startsWith('"')) values[d[0].toLowerCase()] = d[1].substring(1, d[1].length - 2); else values[d[0].toLowerCase()] = d[1]; } let avx2 = true; const cpuInfoFile = readFileSync('/proc/cpuinfo', 'utf-8'); -if (!cpuInfoFile.includes('avx2')) { +if (!cpuInfoFile.includes('avx2') && !installAsJudge) { avx2 = false; log.warn('warn.avx2'); } @@ -139,6 +149,31 @@ const Caddyfile = `\ } `; +const judgeYaml = `\ +hosts: + local: + host: localhost + type: hydro + server_url: https://hydro.ac/ + uname: judge + password: examplepassword + detail: true +tmpfs_size: 512m +stdio_size: 256m +memoryMax: 1024m +processLimit: 128 +testcases_max: 60 +total_time_limit: 300 +retry_delay_sec: 3 +parallelism: 10 +rate: 1.00 +rerun: 0 +secret: examplesecret +env: | + PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/nodejs/bin + HOME=/w +`; + const isPortFree = async (port: number) => { const server = net.createServer(); const res = await new Promise((resolve) => { @@ -216,6 +251,8 @@ connect-timeout = 10`); }, { init: 'install.mongodb', + skip: () => installAsJudge, + hidden: installAsJudge, operations: [ `nix-env -iA hydro.mongodb${avx2 ? 5 : 4}${CN ? '-cn' : ''} hydro.mongosh${avx2 ? 5 : 4}${CN ? '-cn' : ''} nixpkgs.mongodb-tools`, ], @@ -235,7 +272,8 @@ connect-timeout = 10`); }, { init: 'install.caddy', - skip: () => !exec('caddy version').code, + skip: () => !exec('caddy version').code || installAsJudge || noCaddy, + hidden: installAsJudge, operations: [ 'nix-env -iA nixpkgs.caddy', () => writeFileSync(`${process.env.HOME}/.hydro/Caddyfile`, Caddyfile), @@ -249,27 +287,31 @@ connect-timeout = 10`); let res: any = null; try { exec('yarn config set registry https://registry.npmmirror.com/', { stdio: 'inherit' }); - res = exec('yarn global add hydrooj @hydrooj/ui-default @hydrooj/hydrojudge', { stdio: 'inherit' }); + res = exec(`yarn global add ${installTarget}`, { stdio: 'inherit' }); } catch (e) { console.log('Failed to install from npmmirror, fallback to yarnpkg'); } finally { exec('yarn config set registry https://registry.yarnpkg.com', { stdio: 'inherit' }); } try { - exec('yarn global add hydrooj @hydrooj/ui-default @hydrooj/hydrojudge', { timeout: 60000 }); + exec(`yarn global add ${installTarget}`, { timeout: 60000 }); } catch (e) { console.warn('Failed to check update from yarnpkg'); if (res?.code !== 0) return 'retry'; } return null; - } : ['yarn global add hydrooj @hydrooj/ui-default @hydrooj/hydrojudge', { retry: true }]), - () => writeFileSync(`${process.env.HOME}/.hydro/addon.json`, '["@hydrooj/ui-default","@hydrooj/hydrojudge"]'), + } : [`yarn global add ${installTarget}`, { retry: true }]), + () => { + if (installAsJudge) writeFileSync(`${process.env.HOME}/.hydro/judge.yaml`, judgeYaml); + else writeFileSync(`${process.env.HOME}/.hydro/addon.json`, JSON.stringify(addons)); + }, () => rollbackResolveField(), ], }, { init: 'install.createDatabaseUser', - skip: () => existsSync(`${process.env.HOME}/.hydro/config.json`), + skip: () => existsSync(`${process.env.HOME}/.hydro/config.json`) || installAsJudge, + hidden: installAsJudge, operations: [ 'pm2 start mongod', () => sleep(3000), @@ -295,16 +337,19 @@ connect-timeout = 10`); operations: [ ['pm2 stop all', { ignore: true }], () => writeFileSync(`${process.env.HOME}/.hydro/mount.yaml`, mount), - 'pm2 start mongod --name mongodb -- --auth --bind_ip 0.0.0.0', - () => sleep(1000), `pm2 start bash --name hydro-sandbox -- -c "ulimit -s unlimited && hydro-sandbox -mount-conf ${process.env.HOME}/.hydro/mount.yaml"`, - 'pm2 start hydrooj', - async () => { - if (!await isPortFree(80)) log.warn('port.80'); - exec('pm2 start caddy -- run', { cwd: `${process.env.HOME}/.hydro` }); - exec('hydrooj cli system set server.xff x-forwarded-for'); - exec('hydrooj cli system set server.xhost x-forwarded-host'); - }, + ...installAsJudge ? [] : [ + 'pm2 start mongod --name mongodb -- --auth --bind_ip 0.0.0.0', + () => sleep(1000), + 'pm2 start hydrooj', + async () => { + if (noCaddy) return; + if (!await isPortFree(80)) log.warn('port.80'); + exec('pm2 start caddy -- run', { cwd: `${process.env.HOME}/.hydro` }); + exec('hydrooj cli system set server.xff x-forwarded-for'); + exec('hydrooj cli system set server.xhost x-forwarded-host'); + }, + ], 'pm2 startup', 'pm2 save', ], @@ -334,6 +379,7 @@ connect-timeout = 10`); }, { init: 'install.done', + skip: () => installAsJudge, operations: [ () => { password = require(`${process.env.HOME}/.hydro/config.json`).password; @@ -345,6 +391,7 @@ connect-timeout = 10`); { init: 'install.postinstall', operations: [ + ...installAsJudge ? [] : ['hydrooj install https://hydro.ac/language-server-0.0.1.tgz'], ['pm2 install pm2-logrotate', { retry: true }], 'pm2 set pm2-logrotate:max_size 64M', ], @@ -353,6 +400,7 @@ connect-timeout = 10`); init: 'install.alldone', operations: [ () => log.info('install.alldone'), + () => installAsJudge && log.info('install.editJudgeConfigAndStart'), ], }, ]; diff --git a/packages/hydrojudge/package.json b/packages/hydrojudge/package.json index 1e08ba4e..b7a722a7 100644 --- a/packages/hydrojudge/package.json +++ b/packages/hydrojudge/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/hydrojudge", "bin": "bin/hydrojudge.js", - "version": "3.1.4", + "version": "3.1.5", "main": "package.json", "author": "undefined ", "repository": "https://github.com/hydro-dev/Hydro.git", diff --git a/packages/hydrojudge/src/config.ts b/packages/hydrojudge/src/config.ts index fa7311fb..7a98490d 100644 --- a/packages/hydrojudge/src/config.ts +++ b/packages/hydrojudge/src/config.ts @@ -29,13 +29,16 @@ const JudgeSettings = Schema.object({ detail: Schema.boolean().description('Show diff detail').default(true), }); +const oldPath = path.resolve(os.homedir(), '.config', 'hydro', 'judge.yaml'); +const newPath = path.resolve(os.homedir(), '.hydro', 'judge.yaml'); + const config = global.Hydro ? JudgeSettings({}) : (() => { const cfg = JudgeSettings({}); const configFilePath = (process.env.CONFIG_FILE || argv.options.config) ? path.resolve(process.env.CONFIG_FILE || argv.options.config) - : path.resolve(os.homedir(), '.config', 'hydro', 'judge.yaml'); + : fs.existsSync(oldPath) ? oldPath : newPath; if (process.env.TEMP_DIR || argv.options.tmp) { cfg.tmp_dir = path.resolve(process.env.TEMP_DIR || argv.options.tmp);