install: auto install caddy

pull/454/head
undefined 2 years ago
parent d3046f8ead
commit bf8e6e95b9

@ -1,4 +1,12 @@
#!/bin/bash
if [ $EUID != 0 ]; then
echo "This script requires root however you are currently running under another user."
echo "We will call sudo directly for you."
echo "Please input your account password below:"
echo "安装脚本需要使用 root 权限,请在下方输入此账号的密码确认授权:"
sudo "$0" "$@"
exit $?
fi
set -e
echo "Executing Hydro install script v3.0.0"
echo "Hydro includes anonymous system telemetry,

@ -1,7 +1,9 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-sequences */
import { execSync, ExecSyncOptions } from 'child_process';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import net from 'net';
const exec = (command: string, args?: ExecSyncOptions) => {
try {
@ -31,6 +33,7 @@ const locales = {
'install.compiler': '正在安装编译器...',
'install.hydro': '正在安装 Hydro...',
'install.done': 'Hydro 安装成功!',
'install.alldone': '安装已全部完成。',
'extra.dbUser': '数据库用户名: hydro',
'extra.dbPassword': '数据库密码: %s',
'info.skip': '步骤已跳过。',
@ -119,6 +122,30 @@ domainName: executor_server
uid: 1536
gid: 1536
`;
const Caddyfile = `\
# 使使 :80 ~/.hydro 使 caddy reload
# http://你的域名/ 正常访问到网站,若需开启 ssl
# :80 hydro.ac ssl
# /
# For more information, refer to caddy v2 documentation.
:80 {
reverse_proxy http://127.0.0.1:8888 {
header_up x-forwarded-for {remote_host}
header_up x-forwarded-host {hostport}
}
}
`;
const isPortFree = async (port: number) => {
const server = net.createServer();
const res = await new Promise((resolve) => {
server.once('error', () => resolve(false));
server.once('listening', () => resolve(true));
server.listen(port);
});
server.close();
return res;
};
function removeOptionalEsbuildDeps() {
const yarnGlobalPath = exec('yarn global dir').output?.trim() || '';
@ -176,9 +203,7 @@ const steps = [
{
init: 'install.mongodb',
operations: [
`nix-env -iA hydro.mongodb${avx2 ? 5 : 4}${CN ? '-cn' : ''}`,
'nix-env -iA nixpkgs.mongodb-tools',
`nix-env -iA hydro.mongosh${avx2 ? 5 : 4}${CN ? '-cn' : ''}`,
`nix-env -iA hydro.mongodb${avx2 ? 5 : 4}${CN ? '-cn' : ''} hydro.mongosh${avx2 ? 5 : 4}${CN ? '-cn' : ''} nixpkgs.mongodb-tools`,
],
},
{
@ -194,6 +219,14 @@ const steps = [
'nix-env -iA hydro.sandbox',
],
},
{
init: 'install.caddy',
skip: () => !exec('caddy version').code,
operations: [
'nix-env -iA nixpkgs.caddy',
() => writeFileSync(`${process.env.HOME}/.hydro/Caddyfile`, Caddyfile),
],
},
{
init: 'install.hydro',
operations: [
@ -234,6 +267,12 @@ const steps = [
() => 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');
},
'pm2 startup',
'pm2 save',
],
@ -272,6 +311,19 @@ const steps = [
() => rollbackResolveField(),
],
},
{
init: 'install.postinstall',
operations: [
['pm2 install pm2-logrotate', { retry: true }],
'pm2 set pm2-logrotate:max_size 64M',
],
},
{
init: 'install.alldone',
operations: [
() => log.info('install.alldone'),
],
},
];
async function main() {

Loading…
Cancel
Save