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-07-10 10:00:41 -04:00
|
|
|
const DATA_DIR = Path.join(
|
|
|
|
__dirname,
|
|
|
|
'data',
|
|
|
|
// Give each shard their own data dir.
|
|
|
|
process.env.CYPRESS_SHARD || 'default'
|
|
|
|
)
|
2024-05-30 03:13:24 -04:00
|
|
|
const PATHS = {
|
2024-06-17 07:05:06 -04:00
|
|
|
DOCKER_COMPOSE_FILE: 'docker-compose.yml',
|
2024-07-10 10:00:41 -04:00
|
|
|
// Give each shard their own override file.
|
|
|
|
DOCKER_COMPOSE_OVERRIDE: `docker-compose.${process.env.CYPRESS_SHARD || 'override'}.yml`,
|
2024-06-17 07:05:06 -04:00
|
|
|
DOCKER_COMPOSE_NATIVE: 'docker-compose.native.yml',
|
2024-07-10 10:00:41 -04:00
|
|
|
DATA_DIR,
|
|
|
|
SANDBOXED_COMPILES_HOST_DIR: Path.join(DATA_DIR, '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-17 07:05:06 -04:00
|
|
|
function runDockerCompose(command, args, callback) {
|
|
|
|
const files = ['-f', PATHS.DOCKER_COMPOSE_FILE]
|
|
|
|
if (process.env.NATIVE_CYPRESS) {
|
|
|
|
files.push('-f', PATHS.DOCKER_COMPOSE_NATIVE)
|
|
|
|
}
|
|
|
|
if (fs.existsSync(PATHS.DOCKER_COMPOSE_OVERRIDE)) {
|
|
|
|
files.push('-f', PATHS.DOCKER_COMPOSE_OVERRIDE)
|
|
|
|
}
|
|
|
|
execFile('docker', ['compose', ...files, command, ...args], callback)
|
|
|
|
}
|
|
|
|
|
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
|
2024-06-17 07:05:06 -04:00
|
|
|
const accessControlAllowOrigin =
|
|
|
|
process.env.ACCESS_CONTROL_ALLOW_ORIGIN || 'http://sharelatex'
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', accessControlAllowOrigin)
|
2024-05-30 03:12:56 -04:00
|
|
|
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
|
|
|
|
|
2024-06-17 07:05:06 -04:00
|
|
|
runDockerCompose(
|
|
|
|
'exec',
|
2024-05-30 03:12:56 -04:00
|
|
|
[
|
|
|
|
'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 09:25:33 -04:00
|
|
|
'OVERLEAF_ALLOW_PUBLIC_ACCESS',
|
|
|
|
'OVERLEAF_ALLOW_ANONYMOUS_READ_AND_WRITE_SHARING',
|
2024-07-01 11:02:37 -04:00
|
|
|
'EXTERNAL_AUTH',
|
|
|
|
'OVERLEAF_SAML_ENTRYPOINT',
|
|
|
|
'OVERLEAF_SAML_CALLBACK_URL',
|
|
|
|
'OVERLEAF_SAML_ISSUER',
|
|
|
|
'OVERLEAF_SAML_IDENTITY_SERVICE_NAME',
|
|
|
|
'OVERLEAF_SAML_EMAIL_FIELD',
|
|
|
|
'OVERLEAF_SAML_FIRST_NAME_FIELD',
|
|
|
|
'OVERLEAF_SAML_LAST_NAME_FIELD',
|
|
|
|
'OVERLEAF_SAML_UPDATE_USER_DETAILS_ON_LOGIN',
|
|
|
|
'OVERLEAF_SAML_CERT',
|
|
|
|
'OVERLEAF_LDAP_URL',
|
|
|
|
'OVERLEAF_LDAP_SEARCH_BASE',
|
|
|
|
'OVERLEAF_LDAP_SEARCH_FILTER',
|
|
|
|
'OVERLEAF_LDAP_BIND_DN',
|
|
|
|
'OVERLEAF_LDAP_BIND_CREDENTIALS',
|
|
|
|
'OVERLEAF_LDAP_EMAIL_ATT',
|
|
|
|
'OVERLEAF_LDAP_NAME_ATT',
|
|
|
|
'OVERLEAF_LDAP_LAST_NAME_ATT',
|
|
|
|
'OVERLEAF_LDAP_UPDATE_USER_DETAILS_ON_LOGIN',
|
2024-06-11 07:01:21 -04:00
|
|
|
// Old branding, used for upgrade tests
|
2024-08-02 08:10:06 -04:00
|
|
|
'SHARELATEX_SITE_URL',
|
2024-06-11 07:01:21 -04:00
|
|
|
'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 = []
|
|
|
|
}
|
|
|
|
|
2024-07-01 11:02:37 -04:00
|
|
|
if (['ldap', 'saml'].includes(vars.EXTERNAL_AUTH)) {
|
|
|
|
cfg.services.sharelatex.depends_on.push(vars.EXTERNAL_AUTH)
|
|
|
|
}
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
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',
|
2024-06-25 07:03:05 -04:00
|
|
|
'--timeout=60',
|
2024-05-30 03:12:56 -04:00
|
|
|
'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
|
|
|
|
}
|
2024-06-17 07:05:06 -04:00
|
|
|
runDockerCompose(cmd, args, (error, stdout, stderr) => {
|
2024-05-30 03:12:56 -04:00
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-10 10:00:41 -04:00
|
|
|
function maybeMongoInit(mongoInit, callback) {
|
|
|
|
if (!mongoInit) return callback()
|
2024-06-17 07:05:06 -04:00
|
|
|
runDockerCompose(
|
|
|
|
'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)
|
|
|
|
|
2024-06-17 07:05:06 -04:00
|
|
|
runDockerCompose(
|
|
|
|
'exec',
|
2024-06-11 07:01:21 -04:00
|
|
|
[
|
|
|
|
'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
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-10 10:00:41 -04:00
|
|
|
function maybeResetData(resetData, callback) {
|
|
|
|
if (!resetData) return callback()
|
|
|
|
|
|
|
|
runDockerCompose(
|
|
|
|
'stop',
|
|
|
|
['--timeout=0', 'sharelatex'],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
if (error) return callback(error, stdout, stderr)
|
|
|
|
|
|
|
|
try {
|
|
|
|
purgeDataDir()
|
|
|
|
} catch (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
mongoIsInitialized = false
|
|
|
|
runDockerCompose(
|
|
|
|
'down',
|
|
|
|
['--timeout=0', '--volumes', 'mongo', 'redis'],
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2024-05-30 03:12:56 -04:00
|
|
|
|
|
|
|
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-07-10 10:00:41 -04:00
|
|
|
resetData: Joi.boolean().optional(),
|
2024-05-30 03:12:56 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{ allowUnknown: false }
|
|
|
|
),
|
|
|
|
(req, res) => {
|
2024-07-10 10:00:41 -04:00
|
|
|
const { pro, version, vars, withDataDir, resetData } = req.body
|
|
|
|
maybeResetData(resetData, (error, stdout, stderr) => {
|
2024-06-11 07:01:21 -04:00
|
|
|
if (error) return res.json({ error, stdout, stderr })
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
try {
|
2024-07-10 10:00:41 -04:00
|
|
|
setVarsDockerCompose({ pro, version, vars, withDataDir })
|
2024-05-30 03:12:56 -04:00
|
|
|
} catch (error) {
|
|
|
|
return res.json({ error })
|
|
|
|
}
|
|
|
|
|
2024-07-10 10:00:41 -04:00
|
|
|
maybeMongoInit(!mongoIsInitialized, (error, stdout, stderr) => {
|
|
|
|
if (error) return res.json({ error, stdout, stderr })
|
|
|
|
runDockerCompose(
|
|
|
|
'up',
|
|
|
|
['--detach', '--wait', 'sharelatex'],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2024-05-30 03:12:56 -04:00
|
|
|
|
2024-06-25 07:03:05 -04:00
|
|
|
app.get('/redis/keys', (req, res) => {
|
|
|
|
runDockerCompose(
|
|
|
|
'exec',
|
|
|
|
['redis', 'redis-cli', 'KEYS', '*'],
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
res.json({ error, stdout, stderr })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
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
|
2024-07-10 10:00:41 -04:00
|
|
|
maybeMongoInit(true, err => {
|
2024-05-30 03:12:56 -04:00
|
|
|
if (err) {
|
|
|
|
console.error('mongo init failed', err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.listen(80)
|
|
|
|
})
|