2024-05-30 03:12:56 -04:00
|
|
|
const fs = require('fs')
|
2024-05-30 03:13:24 -04:00
|
|
|
const Path = require('path')
|
2024-05-30 03:12:56 -04:00
|
|
|
const { execFile } = require('child_process')
|
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const {
|
|
|
|
celebrate: validate,
|
|
|
|
Joi,
|
|
|
|
errors: handleValidationErrors,
|
|
|
|
} = require('celebrate')
|
|
|
|
const YAML = require('js-yaml')
|
|
|
|
|
2024-05-30 03:13:24 -04:00
|
|
|
const PATHS = {
|
|
|
|
DOCKER_COMPOSE_OVERRIDE: 'docker-compose.override.yml',
|
2024-06-11 07:01:21 -04:00
|
|
|
DATA_DIR: Path.join(__dirname, 'data'),
|
|
|
|
SANDBOXED_COMPILES_HOST_DIR: Path.join(__dirname, 'data/compiles'),
|
2024-05-30 03:12:56 -04:00
|
|
|
}
|
|
|
|
const IMAGES = {
|
|
|
|
CE: process.env.IMAGE_TAG_CE.replace(/:.+/, ''),
|
|
|
|
PRO: process.env.IMAGE_TAG_PRO.replace(/:.+/, ''),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mongoIsInitialized = false
|
|
|
|
|
|
|
|
function readDockerComposeOverride() {
|
|
|
|
try {
|
2024-05-30 03:13:24 -04:00
|
|
|
return YAML.load(fs.readFileSync(PATHS.DOCKER_COMPOSE_OVERRIDE, 'utf-8'))
|
2024-05-30 03:12:56 -04:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ENOENT') {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
services: {
|
|
|
|
sharelatex: {
|
|
|
|
environment: {},
|
|
|
|
},
|
2024-05-30 03:13:24 -04:00
|
|
|
'git-bridge': {},
|
2024-05-30 03:12:56 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeDockerComposeOverride(cfg) {
|
2024-05-30 03:13:24 -04:00
|
|
|
fs.writeFileSync(PATHS.DOCKER_COMPOSE_OVERRIDE, YAML.dump(cfg))
|
2024-05-30 03:12:56 -04:00
|
|
|
}
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
function purgeDataDir() {
|
|
|
|
fs.rmSync(PATHS.DATA_DIR, { recursive: true, force: true })
|
|
|
|
}
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
const app = express()
|
|
|
|
app.get('/status', (req, res) => {
|
|
|
|
res.send('host-admin is up')
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
// Basic access logs
|
|
|
|
console.log(req.method, req.url, req.body)
|
|
|
|
// Add CORS headers
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', 'http://sharelatex')
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
|
2024-06-11 07:01:21 -04:00
|
|
|
res.setHeader('Access-Control-Max-Age', '3600')
|
2024-05-30 03:12:56 -04:00
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
'/run/script',
|
|
|
|
validate(
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
cwd: Joi.string().required(),
|
|
|
|
script: Joi.string().required(),
|
|
|
|
args: Joi.array().items(Joi.string()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ allowUnknown: false }
|
|
|
|
),
|
|
|
|
(req, res) => {
|
|
|
|
const { cwd, script, args } = req.body
|
|
|
|
|
|
|
|
execFile(
|
|
|
|
'docker',
|
|
|
|
[
|
|
|
|
'compose',
|
|
|
|
'exec',
|
|
|
|
'sharelatex',
|
|
|
|
'bash',
|
|
|
|
'-c',
|
2024-06-11 07:01:21 -04:00
|
|
|
`source /etc/container_environment.sh && source /etc/overleaf/env.sh || source /etc/sharelatex/env.sh && cd ${JSON.stringify(cwd)} && node ${JSON.stringify(script)} ${args.map(a => JSON.stringify(a)).join(' ')}`,
|
2024-05-30 03:12:56 -04:00
|
|
|
],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
res.json({
|
|
|
|
error,
|
|
|
|
stdout,
|
|
|
|
stderr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-05-30 03:13:24 -04:00
|
|
|
const allowedVars = Joi.object(
|
|
|
|
Object.fromEntries(
|
|
|
|
[
|
|
|
|
'OVERLEAF_APP_NAME',
|
|
|
|
'OVERLEAF_LEFT_FOOTER',
|
|
|
|
'OVERLEAF_RIGHT_FOOTER',
|
|
|
|
'OVERLEAF_PROXY_LEARN',
|
|
|
|
'GIT_BRIDGE_ENABLED',
|
|
|
|
'GIT_BRIDGE_HOST',
|
|
|
|
'GIT_BRIDGE_PORT',
|
|
|
|
'V1_HISTORY_URL',
|
|
|
|
'DOCKER_RUNNER',
|
|
|
|
'SANDBOXED_COMPILES',
|
|
|
|
'SANDBOXED_COMPILES_SIBLING_CONTAINERS',
|
|
|
|
'ALL_TEX_LIVE_DOCKER_IMAGE_NAMES',
|
2024-05-30 03:13:40 -04:00
|
|
|
'OVERLEAF_TEMPLATES_USER_ID',
|
|
|
|
'OVERLEAF_NEW_PROJECT_TEMPLATE_LINKS',
|
2024-06-11 07:01:21 -04:00
|
|
|
// Old branding, used for upgrade tests
|
|
|
|
'SHARELATEX_MONGO_URL',
|
|
|
|
'SHARELATEX_REDIS_HOST',
|
2024-05-30 03:13:24 -04:00
|
|
|
].map(name => [name, Joi.string()])
|
|
|
|
)
|
|
|
|
)
|
2024-05-30 03:12:56 -04:00
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
function setVarsDockerCompose({ pro, vars, version, withDataDir }) {
|
2024-05-30 03:12:56 -04:00
|
|
|
const cfg = readDockerComposeOverride()
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
cfg.services.sharelatex.image = `${pro ? IMAGES.PRO : IMAGES.CE}:${version}`
|
|
|
|
cfg.services['git-bridge'].image = `quay.io/sharelatex/git-bridge:${version}`
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
cfg.services.sharelatex.environment = vars
|
|
|
|
|
2024-05-30 03:13:24 -04:00
|
|
|
if (cfg.services.sharelatex.environment.GIT_BRIDGE_ENABLED === 'true') {
|
|
|
|
cfg.services.sharelatex.depends_on = ['git-bridge']
|
2024-06-11 07:01:21 -04:00
|
|
|
} else {
|
|
|
|
cfg.services.sharelatex.depends_on = []
|
|
|
|
}
|
|
|
|
|
|
|
|
const dataDirInContainer =
|
|
|
|
version === 'latest' || version >= '5.0'
|
|
|
|
? '/var/lib/overleaf/data'
|
|
|
|
: '/var/lib/sharelatex/data'
|
|
|
|
|
|
|
|
cfg.services.sharelatex.volumes = []
|
|
|
|
if (withDataDir) {
|
|
|
|
cfg.services.sharelatex.volumes.push(
|
|
|
|
`${PATHS.DATA_DIR}:${dataDirInContainer}`
|
|
|
|
)
|
2024-05-30 03:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
cfg.services.sharelatex.environment
|
|
|
|
.SANDBOXED_COMPILES_SIBLING_CONTAINERS === 'true'
|
|
|
|
) {
|
|
|
|
cfg.services.sharelatex.environment.SANDBOXED_COMPILES_HOST_DIR =
|
|
|
|
PATHS.SANDBOXED_COMPILES_HOST_DIR
|
|
|
|
cfg.services.sharelatex.environment.TEX_LIVE_DOCKER_IMAGE =
|
|
|
|
process.env.TEX_LIVE_DOCKER_IMAGE
|
|
|
|
cfg.services.sharelatex.environment.ALL_TEX_LIVE_DOCKER_IMAGES =
|
|
|
|
process.env.ALL_TEX_LIVE_DOCKER_IMAGES
|
2024-06-11 07:01:21 -04:00
|
|
|
cfg.services.sharelatex.volumes.push(
|
|
|
|
'/var/run/docker.sock:/var/run/docker.sock'
|
|
|
|
)
|
|
|
|
if (!withDataDir) {
|
|
|
|
cfg.services.sharelatex.volumes.push(
|
|
|
|
`${PATHS.SANDBOXED_COMPILES_HOST_DIR}:${dataDirInContainer}/compiles`
|
|
|
|
)
|
|
|
|
}
|
2024-05-30 03:13:24 -04:00
|
|
|
}
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
writeDockerComposeOverride(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
'/docker/compose/:cmd',
|
|
|
|
validate(
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
args: Joi.array().allow(
|
|
|
|
'--detach',
|
|
|
|
'--wait',
|
|
|
|
'--volumes',
|
|
|
|
'--timeout',
|
|
|
|
'0',
|
|
|
|
'sharelatex',
|
2024-05-30 03:13:24 -04:00
|
|
|
'git-bridge',
|
2024-05-30 03:12:56 -04:00
|
|
|
'mongo',
|
|
|
|
'redis'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
params: {
|
|
|
|
cmd: Joi.allow('up', 'stop', 'down', 'ps', 'logs'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ allowUnknown: false }
|
|
|
|
),
|
|
|
|
(req, res) => {
|
|
|
|
const { cmd } = req.params
|
|
|
|
const { args } = req.body
|
|
|
|
if (['stop', 'down'].includes(cmd)) {
|
|
|
|
mongoIsInitialized = false
|
|
|
|
}
|
|
|
|
execFile('docker', ['compose', cmd, ...args], (error, stdout, stderr) => {
|
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
function mongoInit(callback) {
|
|
|
|
execFile(
|
|
|
|
'docker',
|
2024-06-11 07:01:21 -04:00
|
|
|
['compose', 'up', '--detach', '--wait', 'mongo'],
|
2024-05-30 03:12:56 -04:00
|
|
|
(error, stdout, stderr) => {
|
2024-06-11 07:01:21 -04:00
|
|
|
if (error) return callback(error, stdout, stderr)
|
|
|
|
|
|
|
|
execFile(
|
|
|
|
'docker',
|
|
|
|
[
|
|
|
|
'compose',
|
|
|
|
'exec',
|
|
|
|
'mongo',
|
|
|
|
'mongo',
|
|
|
|
'--eval',
|
|
|
|
'rs.initiate({ _id: "overleaf", members: [ { _id: 0, host: "mongo:27017" } ] })',
|
|
|
|
],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
if (!error) {
|
|
|
|
mongoIsInitialized = true
|
|
|
|
}
|
|
|
|
callback(error, stdout, stderr)
|
|
|
|
}
|
|
|
|
)
|
2024-05-30 03:12:56 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.post('/mongo/init', (req, res) => {
|
|
|
|
mongoInit((error, stdout, stderr) => {
|
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
'/reconfigure',
|
|
|
|
validate(
|
|
|
|
{
|
|
|
|
body: {
|
|
|
|
pro: Joi.boolean().required(),
|
|
|
|
version: Joi.string().required(),
|
|
|
|
vars: allowedVars,
|
2024-06-11 07:01:21 -04:00
|
|
|
withDataDir: Joi.boolean().optional(),
|
2024-05-30 03:12:56 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ allowUnknown: false }
|
|
|
|
),
|
|
|
|
(req, res) => {
|
2024-06-11 07:01:21 -04:00
|
|
|
const { pro, version, vars, withDataDir } = req.body
|
|
|
|
try {
|
|
|
|
setVarsDockerCompose({ pro, version, vars, withDataDir })
|
|
|
|
} catch (error) {
|
|
|
|
return res.json({ error })
|
|
|
|
}
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
const doMongoInit = mongoIsInitialized ? cb => cb() : mongoInit
|
|
|
|
doMongoInit((error, stdout, stderr) => {
|
|
|
|
if (error) return res.json({ error, stdout, stderr })
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
execFile(
|
|
|
|
'docker',
|
|
|
|
['compose', 'up', '--detach', '--wait', 'sharelatex'],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
app.post('/reset/data', (req, res) => {
|
|
|
|
execFile(
|
|
|
|
'docker',
|
|
|
|
['compose', 'stop', '--timeout=0', 'sharelatex'],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
if (error) return res.json({ error, stdout, stderr })
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
try {
|
2024-06-11 07:01:21 -04:00
|
|
|
purgeDataDir()
|
2024-05-30 03:12:56 -04:00
|
|
|
} catch (error) {
|
|
|
|
return res.json({ error })
|
|
|
|
}
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
mongoIsInitialized = false
|
2024-05-30 03:12:56 -04:00
|
|
|
execFile(
|
|
|
|
'docker',
|
2024-06-11 07:01:21 -04:00
|
|
|
['compose', 'down', '--timeout=0', '--volumes', 'mongo', 'redis'],
|
2024-05-30 03:12:56 -04:00
|
|
|
(error, stdout, stderr) => {
|
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
}
|
|
|
|
)
|
2024-06-11 07:01:21 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2024-05-30 03:12:56 -04:00
|
|
|
|
|
|
|
app.use(handleValidationErrors())
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
purgeDataDir()
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
// Init on startup
|
|
|
|
mongoInit(err => {
|
|
|
|
if (err) {
|
|
|
|
console.error('mongo init failed', err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.listen(80)
|
|
|
|
})
|