2022-03-28 07:27:18 +00:00
|
|
|
import defaults from '../defaults.mjs'
|
2020-09-08 07:53:42 +00:00
|
|
|
import { formatLog } from './loghelper.mjs'
|
2022-03-29 10:25:25 +00:00
|
|
|
import { getStatus } from '../util.mjs'
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
const stopSpam = {}
|
2020-09-08 07:53:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.restart'
|
|
|
|
*
|
|
|
|
* Restart server
|
|
|
|
*/
|
|
|
|
export async function restart(ctx, data, cb) {
|
2022-03-29 10:25:25 +00:00
|
|
|
if (ctx.db.config.allowRestart) {
|
|
|
|
ctx.core.restart()
|
|
|
|
} else {
|
|
|
|
ctx.log.fatal('Invalid core restart command')
|
|
|
|
ctx.log.event.error('Invalid core restart command')
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.getlastlogs'
|
|
|
|
*
|
|
|
|
* Returns last few log messages from log
|
|
|
|
*/
|
|
|
|
export async function getlastlogs(ctx, data, cb) {
|
2022-03-29 10:25:25 +00:00
|
|
|
if (data.name === 'service-core') {
|
|
|
|
return cb(ctx.core.log.ringbuffer.records.map(formatLog))
|
|
|
|
}
|
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
if (!app) {
|
|
|
|
ctx.log.warn('Invalid getlastlogs command for app ' + data.name)
|
|
|
|
ctx.log.event.warn('Invalid getlastlogs command for app ' + data.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(app.ctx.log.ringbuffer.records.map(formatLog))
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.listenlogs'
|
|
|
|
*
|
|
|
|
* Start listening to new log lines
|
|
|
|
*/
|
2022-03-29 10:25:25 +00:00
|
|
|
export async function listenlogs(ctx, data) {
|
|
|
|
ctx.socket.join('logger.' + (data.name || 'service-core'))
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.unlistenlogs'
|
|
|
|
*
|
|
|
|
* Stop listening to new log lines
|
|
|
|
*/
|
2022-03-29 10:25:25 +00:00
|
|
|
export async function unlistenlogs(ctx, data) {
|
|
|
|
ctx.socket.leave('logger.' + (data.name || 'service-core'))
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.update'
|
|
|
|
*
|
|
|
|
* Update specific software
|
|
|
|
*/
|
|
|
|
export async function update(ctx, data, cb) {
|
2022-03-29 10:25:25 +00:00
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
if (!app) {
|
2020-09-08 07:53:42 +00:00
|
|
|
ctx.log.warn('Invalid update command for app ' + data.name)
|
|
|
|
ctx.log.event.warn('Invalid update command for app ' + data.name)
|
2020-09-09 15:42:55 +00:00
|
|
|
return
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
|
|
|
|
let d = new Date()
|
|
|
|
if (stopSpam[app.name] && d - stopSpam[app.name] < 1000 * 60 * 5) {
|
|
|
|
ctx.log.warn('Update called too fast for app ' + data.name)
|
|
|
|
ctx.log.event.warn('Update called too fast for app ' + data.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stopSpam[app.name] = d
|
|
|
|
|
|
|
|
ctx.log.info('Checking for updates on app ' + data.name)
|
|
|
|
app.update().then(function(res) {
|
|
|
|
ctx.log.info(res, 'Update completed on app ' + data.name)
|
|
|
|
}, function(err) {
|
|
|
|
ctx.log.err(err, 'Error checking for updates on app ' + data.name)
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.start'
|
|
|
|
*
|
|
|
|
* Start specific software
|
|
|
|
*/
|
|
|
|
export async function start(ctx, data, cb) {
|
2022-03-29 10:25:25 +00:00
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
|
|
|
|
if (!app || (!app.config.scAllowStop && app.running)) {
|
2020-09-08 07:53:42 +00:00
|
|
|
ctx.log.warn('Invalid start command for app ' + data.name)
|
|
|
|
ctx.log.event.warn('Invalid start command for app ' + data.name)
|
2020-09-09 15:42:55 +00:00
|
|
|
return
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
2020-09-09 15:42:55 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
let d = new Date()
|
|
|
|
if (app.running && stopSpam[app.name] && d - stopSpam[app.name] < 1000 * 60 * 5) {
|
|
|
|
ctx.log.warn('Update called too fast for app ' + data.name)
|
|
|
|
ctx.log.event.warn('Update called too fast for app ' + data.name)
|
2020-09-09 15:42:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
stopSpam[app.name] = d
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
ctx.log.info('Checking for updates on app ' + data.name)
|
|
|
|
ctx.core.runApplication(app).then(function(res) {
|
|
|
|
ctx.log.info('Successfully started ' + data.name + ' running ' + app.running)
|
|
|
|
}, function(err) {
|
|
|
|
ctx.log.err(err, 'Error starting app ' + data.name)
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event: 'core.listentoapp'
|
|
|
|
*
|
2022-03-29 10:25:25 +00:00
|
|
|
* Start listening to changes in core application name
|
2020-09-08 07:53:42 +00:00
|
|
|
*/
|
2022-03-29 10:25:25 +00:00
|
|
|
export async function listentoapp(ctx, data) {
|
|
|
|
if (!data.name) {
|
|
|
|
ctx.log.warn(`listento called with missing name`)
|
|
|
|
return
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
ctx.log.warn(`listento called on non-existing app ${data.name}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.socket.join('app.' + data.name)
|
|
|
|
let version = ctx.db.get(ctx.db.data.core[app.name].versions, ctx.db.data.core[app.name].latestInstalled)
|
|
|
|
ctx.socket.emit('app.updatelog', {
|
|
|
|
name: data.name,
|
|
|
|
log: version?.log || ctx.db.data.core[app.name].updater
|
2020-09-09 16:44:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-08 07:53:42 +00:00
|
|
|
/*
|
|
|
|
* Event: 'core.unlistentoapp'
|
|
|
|
*
|
|
|
|
* Stop listening to new log lines
|
|
|
|
*/
|
2022-03-29 10:25:25 +00:00
|
|
|
export async function unlistentoapp(ctx, data) {
|
|
|
|
if (!data.name) {
|
|
|
|
ctx.log.warn(`unlistento called with missing name`)
|
|
|
|
return
|
|
|
|
}
|
2020-09-09 16:44:29 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
ctx.log.warn(`unlistento called on non-existing app ${data.name}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.socket.leave('app.' + data.name)
|
2020-09-09 16:44:29 +00:00
|
|
|
}
|