service-core/core/core.mjs

416 lines
14 KiB
JavaScript
Raw Normal View History

2020-09-07 00:47:53 +00:00
import fs from 'fs'
import { EventEmitter } from 'events'
import { request } from './client.mjs'
2020-09-09 15:41:05 +00:00
import HttpServer from './http.mjs'
2020-09-07 00:47:53 +00:00
const fsp = fs.promises
export default class Core extends EventEmitter{
2020-09-08 08:11:42 +00:00
constructor(util, config, db, log, closeCb) {
2020-09-07 00:47:53 +00:00
super()
2020-09-12 20:31:36 +00:00
process.stdin.resume()
2020-09-09 15:41:05 +00:00
this.http = new HttpServer()
this.util = util
this.config = config
this.db = db
this.log = log
2020-09-07 00:47:53 +00:00
this._close = closeCb
2020-09-12 20:31:36 +00:00
this._activeCrashHandler = null
2020-09-09 15:41:05 +00:00
this.appRunning = false
this.manageRunning = false
2020-09-07 00:47:53 +00:00
this._appUpdating = {
2020-09-12 20:31:36 +00:00
fresh: true,
2020-09-09 15:41:05 +00:00
updating: false,
2020-09-07 18:15:11 +00:00
starting: false,
2020-09-07 00:47:53 +00:00
logs: '',
}
this._manageUpdating = {
2020-09-12 20:31:36 +00:00
fresh: true,
2020-09-09 15:41:05 +00:00
updating: false,
2020-09-07 18:15:11 +00:00
starting: false,
2020-09-07 00:47:53 +00:00
logs: '',
}
}
restart() {
this._close()
}
status() {
return {
2020-09-09 15:41:05 +00:00
app: this.appRunning,
manage: this.manageRunning,
appUpdating: this._appUpdating.updating,
manageUpdating: this._manageUpdating.updating,
appStarting: this._appUpdating.starting,
manageStarting: this._manageUpdating.starting,
2020-09-07 00:47:53 +00:00
}
}
async getLatestVersion(active, name) {
// Example: 'https://api.github.com/repos/thething/sc-helloworld/releases'
2020-09-09 15:41:05 +00:00
this.logActive(name, active, `[Core] Fetching release info from: https://api.github.com/repos/${this.config[name + 'Repository']}/releases\n`)
2020-09-07 00:47:53 +00:00
2020-09-09 15:41:05 +00:00
let result = await request(`https://api.github.com/repos/${this.config[name + 'Repository']}/releases`)
2020-09-07 00:47:53 +00:00
let items = result.body.filter(function(item) {
if (!item.assets.length) return false
for (let i = 0; i < item.assets.length; i++) {
if (item.assets[i].name.endsWith('-sc.zip')) return true
}
})
if (items && items.length) {
for (let x = 0; x < items.length; x++) {
let item = items[x]
for (let i = 0; i < item.assets.length; i++) {
if (item.assets[i].name.endsWith('-sc.zip')) {
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Core] Found version ${item.name} with file ${item.assets[i].name}\n`)
2020-09-07 00:47:53 +00:00
2020-09-09 15:41:05 +00:00
await this.db.set(`core.${name}LatestVersion`, item.name)
2020-09-07 00:47:53 +00:00
.write()
this.emit('dbupdated', {})
return {
name: item.name,
filename: item.assets[i].name,
url: item.assets[i].browser_download_url,
description: item.body,
}
}
}
}
} else {
return null
}
}
logActive(name, active, logline, doNotPrint = false) {
if (!doNotPrint) {
2020-09-09 15:41:05 +00:00
this.log.info(`Log ${name}: ` + logline.replace(/\n/g, ''))
2020-09-07 00:47:53 +00:00
}
active.logs += logline
this.emit(name + 'log', active)
}
getProgramLogs(name) {
if (name === 'app' && this._appUpdating.logs) {
return this._appUpdating.logs
} else if (name === 'manage' && this._manageUpdating.logs) {
return this._manageUpdating.logs
}
2020-09-09 15:41:05 +00:00
let latestInstalled = this.db.get('core.' + name + 'LatestInstalled').value()
let latestVersion = this.db.get('core.' + name + 'LatestVersion').value()
2020-09-07 00:47:53 +00:00
if (latestVersion) {
2020-09-09 15:41:05 +00:00
let value = this.db.get(`core_${name}History`).getById(latestVersion).value()
2020-09-07 00:47:53 +00:00
if (value) return value.logs
}
if (latestInstalled) {
2020-09-09 15:41:05 +00:00
let value = this.db.get(`core_${name}History`).getById(latestInstalled).value()
2020-09-07 00:47:53 +00:00
if (value) return value.logs
}
return '< no logs found >'
}
async installVersion(name, active, version) {
2020-09-09 15:41:05 +00:00
if (fs.existsSync(this.util.getPathFromRoot(`./${name}/` + version.name))) {
await this.util.runCommand('rmdir', ['/S', '/Q', `"${this.util.getPathFromRoot(`./${name}/` + version.name)}"`])
2020-09-07 00:47:53 +00:00
}
2020-09-12 20:31:36 +00:00
if (!fs.existsSync(this.util.getPathFromRoot(`./${name}/`))) {
await fsp.mkdir(this.util.getPathFromRoot(`./${name}/`))
}
2020-09-07 01:25:03 +00:00
try {
2020-09-09 15:41:05 +00:00
await fsp.mkdir(this.util.getPathFromRoot(`./${name}/` + version.name))
2020-09-07 01:25:03 +00:00
} catch(err) {
if (err.code !== 'EEXIST') {
throw err
}
}
2020-09-09 15:41:05 +00:00
// await fsp.mkdir(this.util.getPathFromRoot(`./${name}/` + version.name + '/node_modules'))
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Core] Downloading ${version.name} (${version.url}) to ${version.name + '/' + version.name + '.zip'}\n`)
2020-09-09 15:41:05 +00:00
let filePath = this.util.getPathFromRoot(`./${name}/` + version.name + '/' + version.name + '.zip')
2020-09-07 00:47:53 +00:00
await request(version.url, filePath)
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Core] Downloading finished, starting extraction\n`)
2020-09-09 15:41:05 +00:00
await this.util.runCommand(
2020-09-07 01:25:03 +00:00
'"C:\\Program Files\\7-Zip\\7z.exe"',
['x', `"${filePath}"`],
2020-09-09 15:41:05 +00:00
this.util.getPathFromRoot(`./${name}/` + version.name + '/'),
2020-09-07 01:25:03 +00:00
this.logActive.bind(this, name, active)
)
2020-09-09 15:41:05 +00:00
if (!fs.existsSync(this.util.getPathFromRoot(`./${name}/` + version.name + '/index.mjs'))) {
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `\n[Core] ERROR: Missing index.mjs in the folder, exiting\n`)
2020-09-09 15:41:05 +00:00
throw new Error(`Missing index.mjs in ${this.util.getPathFromRoot(`./${name}/` + version.name + '/index.mjs')}`)
2020-09-07 01:25:03 +00:00
}
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `\n[Core] Starting npm install\n`)
2020-09-07 01:25:03 +00:00
2020-09-09 15:41:05 +00:00
await this.util.runCommand(
2020-09-07 01:25:03 +00:00
'npm.cmd',
['install', '--production', '--no-optional', '--no-package-lock', '--no-audit'],
2020-09-09 15:41:05 +00:00
this.util.getPathFromRoot(`./${name}/` + version.name + '/'),
2020-09-07 01:25:03 +00:00
this.logActive.bind(this, name, active)
)
2020-09-09 15:41:05 +00:00
await this.db.set(`core.${name}LatestInstalled`, version.name)
2020-09-07 01:25:03 +00:00
.write()
this.emit('dbupdated', {})
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `\n[Core] Successfully installed ${version.name}\n`)
}
getActive(name) {
if (name === 'app') {
return this._appUpdating
} else if (name === 'manage') {
return this._manageUpdating
} else {
throw new Error('Invalid name: ' + name)
}
2020-09-07 00:47:53 +00:00
}
2020-09-09 15:41:05 +00:00
async startModule(module, port) {
let out = await module.start(this.config, this.db, this.log, this, this.http, port)
if (out && out.then) {
await out
}
if (!this.http.getCurrentServer()) {
this.log.warn('Module did not call http.createServer')
}
}
2020-09-07 00:47:53 +00:00
2020-09-09 15:41:05 +00:00
async tryStartProgram(name) {
2020-09-07 18:15:11 +00:00
let active = this.getActive(name)
2020-09-09 15:41:05 +00:00
if ((name === 'app' && this.appRunning)
|| (name === 'manage' && this.manageRunning)
2020-09-07 18:15:11 +00:00
|| active.starting) {
2020-09-09 15:41:05 +00:00
this.log.event.warn('Attempting to start ' + name + ' which is already running')
this.log.warn('Attempting to start ' + name + ' which is already running')
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[${name}] Attempting to start it but it is already running\n`, true)
return
}
active.starting = true
2020-09-09 15:41:05 +00:00
let history = this.db.get(`core_${name}History`)
.filter('installed')
.orderBy('installed', 'desc')
.value()
this.logActive(name, active, `[${name}] Finding available version of ${name}\n`)
2020-09-09 15:41:05 +00:00
for (let i = 0; i < history.length; i++) {
2020-09-12 20:31:36 +00:00
if ((history[i].stable === -1 && !active.fresh)
|| (history[i].stable < -1)) {
this.logActive(name, active, `[${name}] Skipping version ${history[i].name} due to marked as unstable\n`)
continue
}
2020-09-07 18:15:11 +00:00
2020-09-09 15:41:05 +00:00
await this.db.set(`core.${name}Active`, history[i].name)
.write()
this.emit('dbupdated', {})
let running = await this.tryStartProgramVersion(name, active, history[i].name)
if (running) {
history[i].stable = 1
} else {
2020-09-12 20:31:36 +00:00
if (active.fresh || history[i].stable === -1) {
history[i].stable = -2
} else {
history[i].stable = -1
}
2020-09-09 15:41:05 +00:00
await this.db.set(`core.${name}Active`, null)
.write()
this.emit('dbupdated', {})
}
2020-09-12 20:31:36 +00:00
active.fresh = false
2020-09-09 15:41:05 +00:00
await this.db.get(`core_${name}History`).updateById(history[i].id, history[i].stable).write()
if (history[i].stable > 0) break
}
if (!this.db.get(`core.${name}Active`).value()) {
this.logActive(name, active, `[${name}] Could not find any available stable version of ${name}\n`)
2020-09-09 15:41:05 +00:00
this.log.error('Unable to start ' + name)
this.log.event.error('Unable to start ' + name)
}
2020-09-07 18:15:11 +00:00
active.starting = false
}
2020-09-12 20:31:36 +00:00
programCrashed(name, version, active, oldStable) {
let newStable = -2
console.log('EXITING:', oldStable, active)
if (oldStable === 0 && !active.fresh) {
newStable = -1
}
let temp = this.db.get(`core_${name}History`).getById(version).set('stable', newStable )
temp.value() // Trigger update on __wrapped__
fs.writeFileSync(this.db.adapterFilePath, JSON.stringify(temp.__wrapped__, null, 2))
}
2020-09-09 15:41:05 +00:00
async tryStartProgramVersion(name, active, version) {
2020-09-07 18:15:11 +00:00
if (!version) return false
this.logActive(name, active, `[${name}] Attempting to start ${version}\n`)
2020-09-09 15:41:05 +00:00
let indexPath = this.util.getUrlFromRoot(`./${name}/` + version + '/index.mjs')
2020-09-07 18:15:11 +00:00
let module
try {
this.logActive(name, active, `[${name}] Loading ${indexPath}\n`)
module = await import(indexPath)
} catch (err) {
this.logActive(name, active, `[${name}] Error importing module\n`, true)
this.logActive(name, active, `[${name}] ${err.stack}\n`, true)
2020-09-09 15:41:05 +00:00
this.log.error(err, `Failed to load ${indexPath}`)
2020-09-07 18:15:11 +00:00
return false
}
2020-09-12 20:31:36 +00:00
2020-09-07 18:15:11 +00:00
let checkTimeout = null
2020-09-12 20:31:36 +00:00
let oldStable = this.db.get(`core_${name}History`).getById(version).value().stable
this._activeCrashHandler = this.programCrashed.bind(this, name, version, active, oldStable)
process.once('exit', this._activeCrashHandler)
2020-09-07 18:15:11 +00:00
try {
2020-09-12 20:31:36 +00:00
let port = name === 'app' ? this.config.port : this.config.managePort
2020-09-07 18:15:11 +00:00
await new Promise((res, rej) => {
2020-09-12 20:31:36 +00:00
checkTimeout = setTimeout(function() {
2020-09-09 15:41:05 +00:00
rej(new Error('Program took longer than 60 seconds to resolve promise'))
}, 60 * 1000)
this.logActive(name, active, `[${name}] Starting module\n`)
2020-09-07 18:15:11 +00:00
try {
2020-09-09 15:41:05 +00:00
this.http.setContext(name)
2020-09-12 20:31:36 +00:00
this.startModule(module, port)
2020-09-09 15:41:05 +00:00
.then(res, rej)
2020-09-07 18:15:11 +00:00
} catch (err) {
rej(err)
}
})
2020-09-12 20:31:36 +00:00
clearTimeout(checkTimeout)
this.logActive(name, active, `[${name}] Testing out module port ${version}\n`)
await this.checkProgramRunning(name, active, port)
process.off('exit', this._activeCrashHandler)
2020-09-07 18:15:11 +00:00
} catch (err) {
clearTimeout(checkTimeout)
2020-09-12 20:31:36 +00:00
process.off('exit', this._activeCrashHandler)
2020-09-09 15:41:05 +00:00
await this.http.closeServer(name)
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[${name}] Error starting\n`, true)
this.logActive(name, active, `[${name}] ${err.stack}\n`, true)
2020-09-09 15:41:05 +00:00
this.log.error(err, `Failed to start ${name}`)
2020-09-07 18:15:11 +00:00
return false
}
2020-09-12 20:31:36 +00:00
this._activeCrashHandler = null
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[${name}] Successfully started version ${version}\n`)
2020-09-09 15:41:05 +00:00
await this.db.set(`core.${name}Active`, version)
2020-09-07 18:15:11 +00:00
.write()
if (name === 'app') {
2020-09-09 15:41:05 +00:00
this.appRunning = true
2020-09-07 18:15:11 +00:00
} else {
2020-09-09 15:41:05 +00:00
this.manageRunning = true
2020-09-07 18:15:11 +00:00
}
2020-09-09 15:41:05 +00:00
this.emit('statusupdated', {})
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[${name}] Module is running successfully\n`)
return true
2020-09-07 00:47:53 +00:00
}
2020-09-12 20:31:36 +00:00
async checkProgramRunning(name, active, port) {
let start = new Date()
let error = null
let success = false
while (new Date() - start < 10 * 1000) {
try {
let check = await request(`http://localhost:${port}`, null, 0, true)
success = true
break
} catch(err) {
this.logActive(name, active, `[${name}:${port}] ${err.message}, retrying in 3 seconds\n`)
error = err
await new Promise(function(res) { setTimeout(res, 3000)})
}
}
if (success) return true
throw error || new Error('Checking server failed')
}
2020-09-07 00:47:53 +00:00
async updateProgram(name) {
2020-09-09 15:41:05 +00:00
if (!this.config[name + 'Repository']) {
2020-09-07 00:47:53 +00:00
if (name === 'app') {
2020-09-09 15:41:05 +00:00
this.log.error(name + 'Repository was missing from config')
this.log.event.error(name + 'Repository was missing from config')
2020-09-07 00:47:53 +00:00
} else {
2020-09-09 15:41:05 +00:00
this.log.warn(name + 'Repository was missing from config')
this.log.event.warn(name + 'Repository was missing from config')
2020-09-07 00:47:53 +00:00
}
return
}
2020-09-07 18:15:11 +00:00
let active = this.getActive(name)
2020-09-12 20:31:36 +00:00
let oldLogs = active.logs || ''
if (oldLogs) {
oldLogs += '\n'
}
active.logs = ''
2020-09-09 15:41:05 +00:00
active.updating = true
2020-09-07 00:47:53 +00:00
this.emit('statusupdated', {})
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Core] Time: ${new Date().toISOString().replace('T', ' ').split('.')[0]}\n`)
this.logActive(name, active, '[Core] Checking for updates...\n')
2020-09-07 00:47:53 +00:00
let version = null
2020-09-07 18:15:11 +00:00
let installed = false
let found = false
2020-09-07 00:47:53 +00:00
try {
version = await this.getLatestVersion(active, name)
2020-09-09 15:41:05 +00:00
let core = this.db.get('core').value()
let fromDb = this.db.get(`core_${name}History`).getById(version.name).value()
2020-09-07 18:15:11 +00:00
if (!fromDb || !fromDb.installed) {
2020-09-07 00:47:53 +00:00
let oldVersion = core[name + 'Current'] || '<none>'
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Core] Updating from ${oldVersion} to ${version.name}\n`)
2020-09-07 00:47:53 +00:00
await this.installVersion(name, active, version)
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Core] Finished: ${new Date().toISOString().replace('T', ' ').split('.')[0]}\n`)
installed = new Date()
} else {
found = true
2020-09-09 15:41:05 +00:00
this.logActive(name, active, `[Core] Version ${version.name} already installed\n`)
2020-09-07 00:47:53 +00:00
}
} catch(err) {
this.logActive(name, active, '\n', true)
2020-09-07 18:15:11 +00:00
this.logActive(name, active, `[Error] Exception occured while updating ${name}\n`, true)
2020-09-07 00:47:53 +00:00
this.logActive(name, active, err.stack, true)
2020-09-12 20:31:36 +00:00
this.log.error('Error while updating ' + name, err)
2020-09-07 00:47:53 +00:00
}
2020-09-09 15:41:05 +00:00
active.updating = false
2020-09-07 18:15:11 +00:00
if (version && !found) {
2020-09-09 15:41:05 +00:00
await this.db.get(`core_${name}History`).upsert({
2020-09-07 00:47:53 +00:00
id: version.name,
name: version.name,
filename: version.filename,
url: version.url,
description: version.description,
logs: active.logs,
2020-09-07 18:15:11 +00:00
stable: 0,
2020-09-12 20:31:36 +00:00
installed: installed && installed.toISOString(),
2020-09-07 00:47:53 +00:00
}).write()
}
2020-09-12 20:31:36 +00:00
active.logs = oldLogs + active.logs
this.emit(name + 'log', active)
2020-09-07 00:47:53 +00:00
this.emit('statusupdated', {})
}
async start(name) {
await this.updateProgram(name)
2020-09-09 15:41:05 +00:00
var version = this.db.get('core.' + name + 'LatestVersion').value()
if (version) {
await this.tryStartProgram(name)
2020-09-07 00:47:53 +00:00
}
}
}