2021-04-14 13:26:00 +00:00
|
|
|
import { getProsodyFilePaths, writeProsodyConfig } from './config'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import * as util from 'util'
|
2021-04-14 14:14:56 +00:00
|
|
|
import * as child_process from 'child_process'
|
2021-04-13 15:13:41 +00:00
|
|
|
|
2021-04-14 14:14:56 +00:00
|
|
|
const exec = util.promisify(child_process.exec)
|
2021-04-14 13:26:00 +00:00
|
|
|
|
|
|
|
interface ProsodyRunning {
|
2021-04-13 16:00:45 +00:00
|
|
|
ok: boolean
|
|
|
|
messages: string[]
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:10:22 +00:00
|
|
|
async function prosodyCtl (options: RegisterServerOptions, command: string, failOnError: boolean): Promise<string> {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
logger.debug('Calling prosodyCtl with command ' + command)
|
|
|
|
|
2021-04-14 14:14:56 +00:00
|
|
|
const filePaths = await getProsodyFilePaths(options)
|
|
|
|
if (!/^\w+$/.test(command)) {
|
|
|
|
throw new Error(`Invalid prosodyctl command '${command}'`)
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let d: string = ''
|
|
|
|
let e: string = ''
|
|
|
|
const spawned = child_process.spawn('prosodyctl', [
|
|
|
|
'--config',
|
|
|
|
filePaths.config,
|
|
|
|
command
|
|
|
|
], {
|
|
|
|
cwd: filePaths.dir,
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PROSODY_CONFIG: filePaths.config
|
|
|
|
}
|
|
|
|
})
|
|
|
|
spawned.stdout.on('data', (data) => {
|
|
|
|
d += data as string
|
|
|
|
})
|
|
|
|
spawned.stderr.on('data', (data) => {
|
|
|
|
options.peertubeHelpers.logger.error(`Spawned command ${command} has errors: ${data as string}`)
|
|
|
|
e += data as string
|
|
|
|
})
|
|
|
|
spawned.on('close', (code) => {
|
2021-04-14 15:10:22 +00:00
|
|
|
if (code !== 0 && failOnError) {
|
2021-04-14 14:14:56 +00:00
|
|
|
reject(e)
|
|
|
|
} else {
|
2021-04-14 15:10:22 +00:00
|
|
|
if (e !== '') { d += e }
|
2021-04-14 14:14:56 +00:00
|
|
|
resolve(d)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getProsodyAbout (options: RegisterServerOptions): Promise<string> {
|
2021-04-14 15:10:22 +00:00
|
|
|
return prosodyCtl(options, 'about', true)
|
2021-04-14 14:14:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 13:26:00 +00:00
|
|
|
async function testProsodyRunning (options: RegisterServerOptions): Promise<ProsodyRunning> {
|
2021-04-13 15:13:41 +00:00
|
|
|
const { peertubeHelpers } = options
|
2021-04-14 15:10:22 +00:00
|
|
|
const logger = peertubeHelpers.logger
|
|
|
|
logger.info('Checking if Prosody is running')
|
|
|
|
|
2021-04-14 13:26:00 +00:00
|
|
|
const result: ProsodyRunning = {
|
2021-04-13 16:00:45 +00:00
|
|
|
ok: false,
|
|
|
|
messages: []
|
|
|
|
}
|
|
|
|
|
2021-04-14 13:26:00 +00:00
|
|
|
const filePaths = await getProsodyFilePaths(options)
|
|
|
|
try {
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Trying to access the pid file')
|
2021-04-14 13:26:00 +00:00
|
|
|
await fs.promises.access(filePaths.pid, fs.constants.R_OK)
|
2021-04-14 14:14:56 +00:00
|
|
|
result.messages.push(`Pid file ${filePaths.pid} found`)
|
2021-04-14 13:26:00 +00:00
|
|
|
} catch (error) {
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug(`Failed to access pid file: ${error as string}`)
|
2021-04-14 13:26:00 +00:00
|
|
|
result.messages.push(`Pid file ${filePaths.pid} not found`)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ok = true
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Promise<ProsodyRunning> {
|
|
|
|
const { peertubeHelpers } = options
|
|
|
|
peertubeHelpers.logger.info('Checking if Prosody is correctly running')
|
|
|
|
const result = await testProsodyRunning(options)
|
|
|
|
if (!result.ok) { return result }
|
|
|
|
result.ok = false // more tests to come
|
2021-04-13 15:13:41 +00:00
|
|
|
|
|
|
|
// TODO
|
|
|
|
peertubeHelpers.logger.error('testProsodyCorrectlyRunning not implemented yet.')
|
2021-04-13 16:00:45 +00:00
|
|
|
return result
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureProsodyRunning (options: RegisterServerOptions): Promise<void> {
|
|
|
|
const { peertubeHelpers, settingsManager } = options
|
|
|
|
const logger = peertubeHelpers.logger
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Calling ensureProsodyRunning')
|
2021-04-13 15:13:41 +00:00
|
|
|
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Checking if prosody should be active')
|
2021-04-13 15:13:41 +00:00
|
|
|
const setting = await settingsManager.getSetting('chat-use-prosody')
|
|
|
|
if (!setting) {
|
|
|
|
logger.info('Prosody is not activated, we wont launch it')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const r = await testProsodyCorrectlyRunning(options)
|
2021-04-13 16:00:45 +00:00
|
|
|
if (r.ok) {
|
2021-04-14 13:26:00 +00:00
|
|
|
r.messages.forEach(m => logger.debug(m))
|
2021-04-13 15:13:41 +00:00
|
|
|
logger.info('Prosody is already running correctly')
|
2021-04-14 15:10:22 +00:00
|
|
|
// Stop here. Nothing to change.
|
2021-04-13 15:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-14 13:26:00 +00:00
|
|
|
logger.info('Prosody is not running correctly: ')
|
|
|
|
r.messages.forEach(m => logger.info(m))
|
|
|
|
|
2021-04-13 15:13:41 +00:00
|
|
|
// Shutting down...
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Shutting down prosody')
|
2021-04-13 15:13:41 +00:00
|
|
|
await ensureProsodyNotRunning(options)
|
|
|
|
|
|
|
|
// writing the configuration file
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Writing the configuration file')
|
2021-04-13 15:13:41 +00:00
|
|
|
await writeProsodyConfig(options)
|
|
|
|
|
2021-04-14 13:26:00 +00:00
|
|
|
const filePaths = await getProsodyFilePaths(options)
|
|
|
|
|
|
|
|
// launch prosody
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.info('Going to launch prosody')
|
2021-04-14 13:26:00 +00:00
|
|
|
await exec('prosody', {
|
|
|
|
cwd: filePaths.dir,
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PROSODY_CONFIG: filePaths.config
|
|
|
|
}
|
|
|
|
})
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.info('Verifying prosody is launched')
|
|
|
|
const status = await prosodyCtl(options, 'status', true)
|
|
|
|
logger.info(`Prosody status: ${status}`)
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise<void> {
|
|
|
|
const { peertubeHelpers } = options
|
2021-04-14 15:10:22 +00:00
|
|
|
const logger = peertubeHelpers.logger
|
|
|
|
logger.info('Checking if Prosody is running, and shutting it down if so')
|
|
|
|
|
|
|
|
// NB: this function is called on plugin unregister, even if prosody is not used
|
|
|
|
// so we must avoid creating the working dir now
|
|
|
|
const filePaths = await getProsodyFilePaths(options)
|
|
|
|
if (!fs.existsSync(filePaths.dir)) {
|
|
|
|
logger.info(`The working dir ${filePaths.dir} does not exist, assuming there is no prosody on this server`)
|
|
|
|
return
|
|
|
|
}
|
2021-04-13 15:13:41 +00:00
|
|
|
|
2021-04-14 15:10:22 +00:00
|
|
|
logger.debug('Calling prosodyctl to stop the process')
|
|
|
|
const m = await prosodyCtl(options, 'stop', false)
|
|
|
|
logger.info(`ProsodyCtl command returned: ${m}`)
|
2021-04-13 15:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2021-04-14 14:14:56 +00:00
|
|
|
getProsodyAbout,
|
2021-04-14 13:26:00 +00:00
|
|
|
testProsodyRunning,
|
2021-04-13 15:13:41 +00:00
|
|
|
testProsodyCorrectlyRunning,
|
|
|
|
ensureProsodyRunning,
|
|
|
|
ensureProsodyNotRunning
|
|
|
|
}
|