Skip to content

Commit

Permalink
migrate: add lock
Browse files Browse the repository at this point in the history
  • Loading branch information
undefined-moe committed Jan 10, 2025
1 parent 71ffb24 commit a63a20c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions install/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ ${nixConfBase}`);
skip: () => migration !== 'hustoj',
silent: true,
operations: [
'pm2 restart hydrooj',
() => {
const dbInc = readFileSync('/home/judge/src/web/include/db_info.inc.php', 'utf-8');
const l = dbInc.split('\n');
Expand Down
22 changes: 16 additions & 6 deletions packages/migrate/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import crypto from 'crypto';
import {
Context, md5, Schema, sha1,
SystemError,
SystemModel,
} from 'hydrooj';

const RE_MD5 = /^[\da-f]{32}$/;

function checkLock(innerCall) {
return async (args) => {
const cur = await SystemModel.get('migrate.lock');
if (cur) throw new SystemError(`migrate lock already exists: ${cur}, possible another migration is running`);
return innerCall(args);
};
}

export function apply(ctx: Context) {
ctx.addScript(
'migrateHustoj', 'migrate from hustoj',
Expand All @@ -19,7 +29,7 @@ export function apply(ctx: Context) {
dataDir: Schema.string().required(),
uploadDir: Schema.string().default('/home/judge/src/web/upload/'),
}),
(...args) => require('./scripts/hustoj').run(...args),
checkLock((...args) => require('./scripts/hustoj').run(...args)),
);
ctx.addScript(
'migrateSyzoj', 'migrate from syzoj',
Expand All @@ -33,7 +43,7 @@ export function apply(ctx: Context) {
dataDir: Schema.string().default('/opt/syzoj/web/uploads'),
randomMail: Schema.union(['never', 'needed', 'always']).default('never'),
}),
(...args) => require('./scripts/syzoj').run(...args),
checkLock((...args) => require('./scripts/syzoj').run(...args)),
);
ctx.addScript(
'migrateVijos', 'migrate from vijos',
Expand All @@ -44,7 +54,7 @@ export function apply(ctx: Context) {
username: Schema.string().required(),
password: Schema.string().required(),
}),
(...args) => require('./scripts/vijos').run(...args),
checkLock((...args) => require('./scripts/vijos').run(...args)),
);
ctx.addScript(
'migrateUniversaloj', 'migrate from universaloj',
Expand All @@ -57,7 +67,7 @@ export function apply(ctx: Context) {
domainId: Schema.string().default('system'),
dataDir: Schema.string().required(),
}),
(...args) => require('./scripts/universaloj').run(...args),
checkLock((...args) => require('./scripts/universaloj').run(...args)),
);
ctx.addScript(
'migratePoj', 'migrate from poj',
Expand All @@ -72,14 +82,14 @@ export function apply(ctx: Context) {
dataDir: Schema.string().required(),
imageDir: Schema.string().required(),
}),
(...args) => require('./scripts/poj').run(...args),
checkLock((...args) => require('./scripts/poj').run(...args)),
);

ctx.provideModule('hash', 'hust', ($password, $saved) => {
$password = md5($password);
if (RE_MD5.test($saved)) return $password === $saved;
const $svd = Buffer.from($saved, 'base64').toString('hex');
const $salt = Buffer.from($svd.substr(40), 'hex').toString();
const $salt = Buffer.from($svd.substring(40), 'hex').toString();
const $hash = Buffer.concat([
Buffer.from(sha1($password + $salt), 'hex'),
Buffer.from($salt),
Expand Down
2 changes: 2 additions & 0 deletions packages/migrate/scripts/hustoj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export async function run({
const target = await DomainModel.get(domainId);
if (!target) throw new NotFoundError(domainId);
report({ message: 'Connected to database' });
await SystemModel.set('migrate.lock', 'hustoj');
/*
user_id varchar 20 N 用户id(主键)
email varchar 100 Y 用户E-mail
Expand Down Expand Up @@ -349,5 +350,6 @@ hydrooj install https://hydro.ac/hydroac-client.zip
}
await ProblemModel.addTestdata(domainId, pdoc.docId, 'config.yaml', Buffer.from(pdoc.config as string));
}
await SystemModel.set('migrate.lock', 0);
return true;
}

0 comments on commit a63a20c

Please sign in to comment.