install: nix

pull/405/head
undefined 2 years ago
parent 1ba52c5a18
commit c21adfb99a

@ -55,6 +55,11 @@ const locales = {
'info.skip': 'Step skipped.', 'info.skip': 'Step skipped.',
}, },
}; };
let locale = __env.LANG?.includes('zh') ? 'zh' : 'en';
if (__env.TERM === 'linux') locale = 'en';
log.info = ((orig) => (str, ...args) => orig(locales[locale][str] || str, ...args) && 0)(log.info);
log.warn = ((orig) => (str, ...args) => orig(locales[locale][str] || str, ...args) && 0)(log.warn);
log.fatal = ((orig) => (str, ...args) => orig(locales[locale][str] || str, ...args) && 0)(log.fatal);
if (__user !== 'root') log.fatal('error.rootRequired'); if (__user !== 'root') log.fatal('error.rootRequired');
if (__arch !== 'amd64') log.fatal('error.unsupportedArch', __arch); if (__arch !== 'amd64') log.fatal('error.unsupportedArch', __arch);
@ -69,8 +74,7 @@ for (const line of lines) {
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].substr(1, d[1].length - 2);
else values[d[0].toLowerCase()] = d[1]; else values[d[0].toLowerCase()] = d[1];
} }
if (!['ubuntu', 'arch', 'debian'].includes(values.id)) log.fatal('error.unsupportedOS', values.id); const apt = ['ubuntu', 'debian'].includes(values.id);
const Arch = values.id === 'arch';
const cpuInfoFile = fs.readfile('/proc/cpuinfo'); const cpuInfoFile = fs.readfile('/proc/cpuinfo');
let mongodbVersion = __env.MONGODB_VERSION || '5.0'; let mongodbVersion = __env.MONGODB_VERSION || '5.0';
if (!cpuInfoFile.includes('avx2')) { if (!cpuInfoFile.includes('avx2')) {
@ -111,12 +115,6 @@ function getMirror(target) {
res.push(...Object.keys(mirrors[target]).map((i) => mirrors[target][i])); res.push(...Object.keys(mirrors[target]).map((i) => mirrors[target][i]));
return res[retry % res.length]; return res[retry % res.length];
} }
let locale = __env.LANG?.includes('zh') ? 'zh' : 'en';
if (__env.TERM === 'linux') locale = 'en';
log.info = ((orig) => (str, ...args) => orig(locales[locale][str] || str, ...args) && 0)(log.info);
log.warn = ((orig) => (str, ...args) => orig(locales[locale][str] || str, ...args) && 0)(log.warn);
log.fatal = ((orig) => (str, ...args) => orig(locales[locale][str] || str, ...args) && 0)(log.fatal);
log.info('install.start'); log.info('install.start');
const MINIO_ACCESS_KEY = randomstring(32); const MINIO_ACCESS_KEY = randomstring(32);
const MINIO_SECRET_KEY = randomstring(32); const MINIO_SECRET_KEY = randomstring(32);
@ -131,13 +129,11 @@ export NVM_DIR="$HOME/.nvm"
const steps = [ const steps = [
{ {
init: 'install.preparing', init: 'install.preparing',
operations: [ operations: apt ? [
() => log.info('info.mirror', preferredMirror), () => log.info('info.mirror', preferredMirror),
'mkdir -p /data/db /data/file ~/.hydro', 'mkdir -p /data/db /data/file ~/.hydro',
Arch ? 'pacman --needed --quiet --noconfirm -Sy' : 'apt-get -qq update', 'apt-get -qq update',
Arch 'apt-get install -qy unzip zip curl wget gnupg qrencode ca-certificates',
? 'pacman --needed --quiet --noconfirm -S gnupg curl qrencode'
: 'apt-get install -qy unzip zip curl wget gnupg qrencode ca-certificates',
() => { () => {
if (locale === 'zh') { if (locale === 'zh') {
log.info('扫码加入QQ群'); log.info('扫码加入QQ群');
@ -159,35 +155,37 @@ const steps = [
if (res.toLowerCase().trim() === 'y') migration = 'qduoj'; if (res.toLowerCase().trim() === 'y') migration = 'qduoj';
} }
}, },
] : [
'mkdir -p /data/db /data/file ~/.hydro',
'bash -c "bash <(curl https://hydro.ac/nix.sh)"',
() => {
setenv('PATH', `/root/.nix-profile/bin:${__env.PATH}`);
},
'nix-env -iA nixpkgs.unzip nixpkgs.zip nixpkgs.diffutils',
], ],
}, },
{ {
init: 'install.mongodb', init: 'install.mongodb',
skip: () => fs.exist('/usr/bin/mongo'), skip: () => !exec('mongod --version').code,
operations: Arch operations: apt ? [
? [ // https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
['curl -fSLO https://s3.undefined.moe/hydro/arch/libcurl-openssl-1.0-7.76.0-1-x86_64.pkg.tar.zst', { retry: true }], ['apt-get upgrade openssl ca-certificates -y', { retry: true }],
['curl -fSLO https://s3.undefined.moe/hydro/arch/mongodb-bin-4.4.5-1-x86_64.pkg.tar.zst', { retry: true }], [`wget -qO - https://www.mongodb.org/static/pgp/server-${mongodbVersion}.asc | apt-key add -`, { retry: true }],
['curl -fSLO https://s3.undefined.moe/hydro/arch/mongodb-tools-bin-100.3.1-1-x86_64.pkg.tar.zst', { retry: true }], [`echo "deb ${getMirror('mongodb')} ${values.ubuntu_codename}\
'pacman --noconfirm -U libcurl-openssl-1.0-7.76.0-1-x86_64.pkg.tar.zst'
+ 'mongodb-bin-4.4.5-1-x86_64.pkg.tar.zst mongodb-tools-bin-100.3.1-1-x86_64.pkg.tar.zst',
]
: [
// https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021/
['apt-get upgrade openssl ca-certificates -y', { retry: true }],
[`wget -qO - https://www.mongodb.org/static/pgp/server-${mongodbVersion}.asc | apt-key add -`, { retry: true }],
[`echo "deb ${getMirror('mongodb')} ${values.ubuntu_codename}\
/mongodb-org/${mongodbVersion} multiverse" >/etc/apt/sources.list.d/mongodb-org-${mongodbVersion}.list && \ /mongodb-org/${mongodbVersion} multiverse" >/etc/apt/sources.list.d/mongodb-org-${mongodbVersion}.list && \
apt-get -qq update && apt-get -q install -y mongodb-org`, { retry: true }], apt-get -qq update && apt-get -q install -y mongodb-org`, { retry: true }],
], ] : [
`nix-env -iA hydro.mongodb${mongodbVersion.split('.')[0]} hydro.mongosh${mongodbVersion.split('.')[0]}`,
],
}, },
{ {
init: 'install.nvm', init: 'install.nvm',
skip: () => { skip: () => {
if (!apt) return true;
const nvm = fs.exist('/root/.nvm'); const nvm = fs.exist('/root/.nvm');
const node = !exec('node -v').code; const node = !exec('node -v').code;
if (node && !nvm) log.warn('error.nodeWithoutNVMDetected'); if (node && !nvm) log.warn('error.nodeWithoutNVMDetected');
return nvm; return nvm || node;
}, },
operations: [ operations: [
() => { () => {
@ -202,7 +200,8 @@ apt-get -qq update && apt-get -q install -y mongodb-org`, { retry: true }],
}, },
{ {
init: 'install.nodejs', init: 'install.nodejs',
operations: [ skip: () => !exec('node -v').code && !exec('yarn -v').code,
operations: apt ? [
() => { () => {
const res = exec1('bash -c "source /root/.nvm/nvm.sh && nvm install 14"', { const res = exec1('bash -c "source /root/.nvm/nvm.sh && nvm install 14"', {
NVM_NODEJS_ORG_MIRROR: getMirror('node'), NVM_NODEJS_ORG_MIRROR: getMirror('node'),
@ -224,56 +223,40 @@ apt-get -qq update && apt-get -q install -y mongodb-org`, { retry: true }],
} }
}, },
['npm i yarn -g', { retry: true }], ['npm i yarn -g', { retry: true }],
] : [
'nix-env -iA nixpkgs.nodejs nixpkgs.yarn',
], ],
}, },
{ {
init: 'install.pm2', init: 'install.pm2',
skip: () => fs.exist('/usr/local/bin/pm2'), skip: () => !exec('pm2 -v').code,
operations: ['yarn global add pm2'], operations: ['yarn global add pm2'],
}, },
{
init: 'install.createDatabaseUser',
skip: () => fs.exist('/root/.hydro/config.json'),
operations: [
'pm2 start mongod',
() => sleep(5000),
() => fs.writefile('/tmp/createUser.js', `\
db.createUser({
user: 'hydro',
pwd: '${DATABASE_PASSWORD}',
roles: [{ role: 'readWrite', db: 'hydro' }]
})`),
'mongo 127.0.0.1:27017/hydro /tmp/createUser.js',
() => fs.writefile('/root/.hydro/config.json', JSON.stringify({
host: '127.0.0.1',
port: 27017,
name: 'hydro',
username: 'hydro',
password: DATABASE_PASSWORD,
})),
'pm2 stop mongod',
'pm2 del mongod',
],
},
{ {
init: 'install.minio', init: 'install.minio',
skip: () => __env.SKIP_MINIO || fs.exist('/root/.hydro/env'), skip: () => !exec('minio -v').code,
operations: [ operations: apt ? [
[`curl -fSL ${getMirror('minio')} -o /usr/bin/minio`, { retry: true }], [`curl -fSL ${getMirror('minio')} -o /usr/bin/minio`, { retry: true }],
'chmod +x /usr/bin/minio', 'chmod +x /usr/bin/minio',
] : [
'nix-env -iA nixpkgs.minio',
], ],
}, },
{ {
init: 'install.compiler', init: 'install.compiler',
operations: [ operations: apt ? [
Arch ? 'pacman --needed --quiet --noconfirm -S gcc fpc' : 'apt-get install -y g++ fp-compiler >/dev/null', 'apt-get install -y g++ fp-compiler >/dev/null',
] : [
'nix-env -iA nixpkgs.gcc nixpkgs.fpc',
], ],
}, },
{ {
init: 'install.sandbox', init: 'install.sandbox',
operations: [ operations: apt ? [
[`curl -fSL ${getMirror('sandbox')} -o /usr/bin/hydro-sandbox`, { retry: true }], [`curl -fSL ${getMirror('sandbox')} -o /usr/bin/hydro-sandbox`, { retry: true }],
'chmod +x /usr/bin/hydro-sandbox', 'chmod +x /usr/bin/hydro-sandbox',
] : [
'nix-env -iA hydro.sandbox',
], ],
}, },
{ {
@ -290,6 +273,30 @@ apt-get -qq update && apt-get -q install -y mongodb-org`, { retry: true }],
() => fs.writefile('/root/.hydro/addon.json', '["@hydrooj/ui-default","@hydrooj/hydrojudge"]'), () => fs.writefile('/root/.hydro/addon.json', '["@hydrooj/ui-default","@hydrooj/hydrojudge"]'),
], ],
}, },
{
init: 'install.createDatabaseUser',
skip: () => fs.exist('/root/.hydro/config.json'),
operations: [
'pm2 start mongod',
() => sleep(5000),
() => fs.writefile('/tmp/createUser.js', `\
db.createUser({
user: 'hydro',
pwd: '${DATABASE_PASSWORD}',
roles: [{ role: 'readWrite', db: 'hydro' }]
})`),
'mongo 127.0.0.1:27017/hydro /tmp/createUser.js',
() => fs.writefile('/root/.hydro/config.json', JSON.stringify({
host: '127.0.0.1',
port: 27017,
name: 'hydro',
username: 'hydro',
password: DATABASE_PASSWORD,
})),
'pm2 stop mongod',
'pm2 del mongod',
],
},
{ {
init: 'install.starting', init: 'install.starting',
operations: [ operations: [

@ -1,17 +1,27 @@
{ pkgs ? import <nixpkgs> { system = "x86_64-linux"; } }: { pkgs ? import <nixpkgs> { system = "x86_64-linux"; } }:
let let
mongo = pkgs.callPackage ./mongo.nix {}; hydro = import (pkgs.fetchFromGitHub {
owner = "hydro-dev";
repo = "nix-channel";
rev = "master";
sha256 = "sha256-EqPU9n4H3EteJqFFv6Seeo9DZxFc3Mdu8Y1y/fjZJ80=";
}) {};
in pkgs.dockerTools.buildImage { in pkgs.dockerTools.buildImage {
name = "hydrooj/web-base"; name = "hydrooj/web-base";
tag = "latest"; tag = "latest";
contents = [ copyToRoot = pkgs.buildEnv {
mongo name = "hydro-web";
pkgs.minio paths = [
pkgs.nodejs hydro.mongodb4
pkgs.yarn pkgs.minio
]; pkgs.nodejs
pkgs.yarn
];
ignoreCollisions = true;
pathsToLink = [ "/bin" ];
};
config = { config = {
WorkingDir = "/data"; WorkingDir = "/data";

@ -1,35 +0,0 @@
{ stdenv, fetchurl, dpkg, glibc, xz, curl, openssl, gcc-unwrapped, autoPatchelfHook }:
stdenv.mkDerivation {
name = "mongodb-5.0.6";
system = "x86_64-linux";
src = fetchurl {
url = "https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/5.0/multiverse/binary-amd64/mongodb-org-server_5.0.6_amd64.deb";
hash = "sha256-Rk43PNQN8p2/3XDDjWOzJmzBjs39CR06kLrTtr+5ngo=";
};
nativeBuildInputs = [
autoPatchelfHook
dpkg
];
buildInputs = [
glibc
openssl # libcrypto.so.1.1 libssl.so.1.1
xz # liblzma.so.5
curl # libcurl.so.4
gcc-unwrapped
];
unpackPhase = "true";
installPhase = ''
mkdir -p $out
dpkg -x $src $out
mkdir $out/bin
mv $out/usr/bin/mongod $out/bin/mongod
'';
meta = {
description = "MongoDB";
homepage = https://www.mongodb.com/;
maintainers = [ "undefined <i@undefined.moe>" ];
platforms = [ "x86_64-linux" ];
};
}
Loading…
Cancel
Save