WIP on prosody spawning.

This commit is contained in:
John Livingston 2021-04-14 17:10:22 +02:00
parent a018ef5293
commit 8a73d93aac
2 changed files with 78 additions and 22 deletions

View File

@ -4,15 +4,11 @@ import { pluginName } from '../helpers'
type LogMode = 'debug' | 'info'
/**
* Creates the working dir if needed, and returns it.
* NB: for now, I try to create a directory in /tmp/.
* To ensure that there is no conflict with another peertube instance,
* I used a randomly generated id that will be stored in database.
*/
async function getWorkingDir ({ peertubeHelpers, storageManager }: RegisterServerOptions): Promise<string> {
const logger = peertubeHelpers.logger
logger.debug('Calling getWorkingDir')
const tmpBaseDir = '/tmp/'
await fs.promises.access(tmpBaseDir, fs.constants.W_OK) // will throw an error if no access
let value: string = await storageManager.getData('tempDirId')
function getPath (value: string): string {
@ -31,12 +27,31 @@ async function getWorkingDir ({ peertubeHelpers, storageManager }: RegisterServe
await storageManager.storeData('tempDirId', value)
}
const name = getPath(value)
if (!fs.existsSync(name)) {
await fs.promises.mkdir(name)
const dir = getPath(value)
logger.debug('getWorkingDir will return ' + dir)
return dir
}
/**
* Creates the working dir if needed, and returns it.
* NB: for now, I try to create a directory in /tmp/.
* To ensure that there is no conflict with another peertube instance,
* I used a randomly generated id that will be stored in database.
*/
async function ensureWorkingDir (options: RegisterServerOptions): Promise<string> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling ensureworkingDir')
const dir = await getWorkingDir(options)
if (!fs.existsSync(dir)) {
logger.info(`The working dir ${dir} does not exists, trying to create it`)
await fs.promises.mkdir(dir)
logger.debug(`Working dir ${dir} was created`)
}
await fs.promises.access(name, fs.constants.W_OK) // will throw an error if no access
return name
logger.debug(`Testing write access on ${dir}`)
await fs.promises.access(dir, fs.constants.W_OK) // will throw an error if no access
logger.debug(`Write access ok on ${dir}`)
return dir
}
interface ProsodyFilePaths {
@ -47,6 +62,9 @@ interface ProsodyFilePaths {
config: string
}
async function getProsodyFilePaths (options: RegisterServerOptions): Promise<ProsodyFilePaths> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyFilePaths')
const dir = await getWorkingDir(options)
return {
dir: dir,
@ -58,6 +76,9 @@ async function getProsodyFilePaths (options: RegisterServerOptions): Promise<Pro
}
async function getProsodyConfigContent (options: RegisterServerOptions): Promise<string> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyConfigContent')
const peertubeDomain = 'localhost'
const paths = await getProsodyFilePaths(options)
const logMode: LogMode = 'debug'
@ -135,21 +156,32 @@ Component "room.localhost" "muc"
}
async function getProsodyConfigPath (options: RegisterServerOptions): Promise<string> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling getProsodyConfigPath')
const paths = await getProsodyFilePaths(options)
return paths.config
}
async function writeProsodyConfig (options: RegisterServerOptions): Promise<void> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling writeProsodyConfig')
logger.debug('Ensuring that the working dir exists')
await ensureWorkingDir(options)
logger.debug('Computing the Prosody config content')
const content = await getProsodyConfigContent(options)
const fileName = await getProsodyConfigPath(options)
logger.info(`Writing prosody configuration file to ${fileName}`)
await fs.promises.writeFile(fileName, content)
logger.debug('Prosody configuration file writen')
}
export {
getProsodyConfigContent,
getWorkingDir,
ensureWorkingDir,
getProsodyFilePaths,
getProsodyConfigPath,
writeProsodyConfig

View File

@ -10,7 +10,10 @@ interface ProsodyRunning {
messages: string[]
}
async function prosodyCtl (options: RegisterServerOptions, command: string): Promise<string> {
async function prosodyCtl (options: RegisterServerOptions, command: string, failOnError: boolean): Promise<string> {
const logger = options.peertubeHelpers.logger
logger.debug('Calling prosodyCtl with command ' + command)
const filePaths = await getProsodyFilePaths(options)
if (!/^\w+$/.test(command)) {
throw new Error(`Invalid prosodyctl command '${command}'`)
@ -37,9 +40,10 @@ async function prosodyCtl (options: RegisterServerOptions, command: string): Pro
e += data as string
})
spawned.on('close', (code) => {
if (code !== 0) {
if (code !== 0 && failOnError) {
reject(e)
} else {
if (e !== '') { d += e }
resolve(d)
}
})
@ -47,12 +51,14 @@ async function prosodyCtl (options: RegisterServerOptions, command: string): Pro
}
async function getProsodyAbout (options: RegisterServerOptions): Promise<string> {
return prosodyCtl(options, 'about')
return prosodyCtl(options, 'about', true)
}
async function testProsodyRunning (options: RegisterServerOptions): Promise<ProsodyRunning> {
const { peertubeHelpers } = options
peertubeHelpers.logger.info('Checking if Prosody is running')
const logger = peertubeHelpers.logger
logger.info('Checking if Prosody is running')
const result: ProsodyRunning = {
ok: false,
messages: []
@ -60,9 +66,11 @@ async function testProsodyRunning (options: RegisterServerOptions): Promise<Pros
const filePaths = await getProsodyFilePaths(options)
try {
logger.debug('Trying to access the pid file')
await fs.promises.access(filePaths.pid, fs.constants.R_OK)
result.messages.push(`Pid file ${filePaths.pid} found`)
} catch (error) {
logger.debug(`Failed to access pid file: ${error as string}`)
result.messages.push(`Pid file ${filePaths.pid} not found`)
return result
}
@ -86,7 +94,9 @@ async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Pro
async function ensureProsodyRunning (options: RegisterServerOptions): Promise<void> {
const { peertubeHelpers, settingsManager } = options
const logger = peertubeHelpers.logger
logger.debug('Calling ensureProsodyRunning')
logger.debug('Checking if prosody should be active')
const setting = await settingsManager.getSetting('chat-use-prosody')
if (!setting) {
logger.info('Prosody is not activated, we wont launch it')
@ -97,21 +107,24 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
if (r.ok) {
r.messages.forEach(m => logger.debug(m))
logger.info('Prosody is already running correctly')
// Stop here. Nothing to change.
return
}
logger.info('Prosody is not running correctly: ')
r.messages.forEach(m => logger.info(m))
// Shutting down...
logger.debug('Shutting down prosody')
await ensureProsodyNotRunning(options)
// writing the configuration file
logger.debug('Writing the configuration file')
await writeProsodyConfig(options)
const filePaths = await getProsodyFilePaths(options)
// launch prosody
logger.info('Going to launch prosody...')
logger.info('Going to launch prosody')
await exec('prosody', {
cwd: filePaths.dir,
env: {
@ -119,16 +132,27 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
PROSODY_CONFIG: filePaths.config
}
})
// TODO: listen for kill signal and kill prosody?
logger.info('Verifying prosody is launched')
const status = await prosodyCtl(options, 'status', true)
logger.info(`Prosody status: ${status}`)
}
async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise<void> {
const { peertubeHelpers } = options
peertubeHelpers.logger.info('Checking if Prosody is running, and shutting it down if so')
const logger = peertubeHelpers.logger
logger.info('Checking if Prosody is running, and shutting it down if so')
// TODO: implement this.
peertubeHelpers.logger.error('ensureProsodyNotRunning not implemented yet.')
// 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
}
logger.debug('Calling prosodyctl to stop the process')
const m = await prosodyCtl(options, 'stop', false)
logger.info(`ProsodyCtl command returned: ${m}`)
}
export {