Updated to latest service-core, fixed some bugs and made it runnable
This commit is contained in:
parent
7e127cd58f
commit
f64acc95eb
10 changed files with 226 additions and 150 deletions
|
@ -1,3 +1,4 @@
|
|||
import defaults from '../defaults.mjs'
|
||||
import { formatLog } from './loghelper.mjs'
|
||||
|
||||
/*
|
||||
|
@ -6,7 +7,16 @@ import { formatLog } from './loghelper.mjs'
|
|||
* Get config
|
||||
*/
|
||||
export async function config(ctx, data, cb) {
|
||||
cb(ctx.config)
|
||||
let merge = {
|
||||
applications: []
|
||||
}
|
||||
for (let app of ctx.core.applications) {
|
||||
merge[app.name] = app.config
|
||||
merge.applications.push(app.name)
|
||||
}
|
||||
let out = defaults(ctx.db.config, merge)
|
||||
console.log(out)
|
||||
cb(out)
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -94,8 +104,8 @@ export async function updatestart(ctx, data, cb) {
|
|||
*/
|
||||
export async function listencore(ctx) {
|
||||
ctx.socket.join('core')
|
||||
ctx.socket.emit('core.db', ctx.db.get('core').value())
|
||||
ctx.socket.emit('core.status', ctx.core.status())
|
||||
ctx.socket.emit('core.db', ctx.db.data.core)
|
||||
ctx.socket.emit('core.status', {})
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import _ from 'lodash'
|
||||
import defaults from '../defaults.mjs'
|
||||
import { format } from 'util'
|
||||
|
||||
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
||||
|
@ -64,7 +64,7 @@ function indent(s) {
|
|||
}
|
||||
|
||||
export function formatLog(data) {
|
||||
let rec = _.cloneDeep(data)
|
||||
let rec = defaults(data)
|
||||
|
||||
delete rec.v;
|
||||
|
||||
|
|
34
api/defaults.mjs
Normal file
34
api/defaults.mjs
Normal file
|
@ -0,0 +1,34 @@
|
|||
|
||||
// taken from isobject npm library
|
||||
function isObject(val) {
|
||||
return val != null && typeof val === 'object' && Array.isArray(val) === false
|
||||
}
|
||||
|
||||
export default function defaults(options, def) {
|
||||
let out = { }
|
||||
|
||||
if (options) {
|
||||
Object.keys(options || {}).forEach(key => {
|
||||
out[key] = options[key]
|
||||
|
||||
if (Array.isArray(out[key])) {
|
||||
out[key] = out[key].map(item => {
|
||||
if (isObject(item)) return defaults(item)
|
||||
return item
|
||||
})
|
||||
} else if (out[key] && typeof out[key] === 'object') {
|
||||
out[key] = defaults(options[key], def && def[key])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (def) {
|
||||
Object.keys(def).forEach(function(key) {
|
||||
if (typeof out[key] === 'undefined') {
|
||||
out[key] = def[key]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
|
@ -21,32 +21,31 @@ function register(ctx, name, method) {
|
|||
}
|
||||
|
||||
|
||||
function onConnection(server, config, db, log, coreService, data) {
|
||||
function onConnection(server, ctx, data) {
|
||||
const io = server
|
||||
const socket = data
|
||||
|
||||
const child = log.child({
|
||||
const child = ctx.log.child({
|
||||
id: socket.id,
|
||||
})
|
||||
child.event = log.event
|
||||
child.event = ctx.log.event
|
||||
|
||||
child.info('Got new socket connection')
|
||||
|
||||
let ctx = {
|
||||
config,
|
||||
io,
|
||||
socket,
|
||||
let ioCtx = {
|
||||
io: io,
|
||||
socket: socket,
|
||||
log: child,
|
||||
db,
|
||||
core: coreService,
|
||||
logroot: log,
|
||||
db: ctx.db,
|
||||
core: ctx.core,
|
||||
logroot: ctx.log,
|
||||
}
|
||||
|
||||
ctx.socket.on('disconnect', function() {
|
||||
ioCtx.socket.on('disconnect', function() {
|
||||
child.info('Closed connection')
|
||||
})
|
||||
|
||||
register(ctx, 'core', core)
|
||||
register(ioCtx, 'core', core)
|
||||
}
|
||||
|
||||
export default onConnection
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import socket from 'socket.io-serveronly'
|
||||
import nStatic from 'node-static'
|
||||
import coremonitor from './core/coremonitor.mjs'
|
||||
// import coremonitor from './core/coremonitor.mjs'
|
||||
|
||||
import onConnection from './routerio.mjs'
|
||||
|
||||
export function run(config, db, log, core, http, port) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
core.startMonitor()
|
||||
export function run(http, port, ctx) {
|
||||
let localUtil = new ctx.sc.Util(import.meta.url)
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const staticRoot = path.join(__dirname,'../public')
|
||||
const staticRoot = localUtil.getPathFromRoot('../public')
|
||||
|
||||
const fileServer = new nStatic.Server(staticRoot)
|
||||
const server = http.createServer(function (req, res) {
|
||||
const child = log.child({})
|
||||
const child = ctx.log.child({})
|
||||
|
||||
const d1 = new Date().getTime()
|
||||
|
||||
|
@ -57,7 +53,7 @@ export function run(config, db, log, core, http, port) {
|
|||
fileServer.serve(req, res, function (err) {
|
||||
if (err) {
|
||||
if (err.status !== 404) {
|
||||
log.error(err, req.url);
|
||||
ctx.log.error(err, req.url);
|
||||
}
|
||||
|
||||
res.writeHead(err.status, err.headers);
|
||||
|
@ -68,17 +64,12 @@ export function run(config, db, log, core, http, port) {
|
|||
})
|
||||
|
||||
const io = new socket(server)
|
||||
io.on('connection', onConnection.bind(this, io, config, db, log, core))
|
||||
io.on('connection', onConnection.bind(this, io, ctx))
|
||||
|
||||
coremonitor(io, config, db, log, core)
|
||||
// coremonitor(io, config, db, log, core)
|
||||
|
||||
server.listen(port, '0.0.0.0', function(err) {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
log.event.info(`Server is listening on ${port} serving files on ${staticRoot}`)
|
||||
log.info(`Server is listening on ${port} serving files on ${staticRoot}`)
|
||||
resolve()
|
||||
})
|
||||
return server.listenAsync(port)
|
||||
.then(function() {
|
||||
ctx.log.info('Server is listening on port ' + port)
|
||||
})
|
||||
}
|
|
@ -5,61 +5,43 @@ const Module = require('../module')
|
|||
const Status = Module({
|
||||
init: function() {
|
||||
this._name = '...loading...'
|
||||
this._management = {
|
||||
name: 'manage',
|
||||
port: null,
|
||||
repository: null,
|
||||
active: null,
|
||||
latestInstalled: null,
|
||||
latestVersion: null,
|
||||
running: null,
|
||||
updating: null,
|
||||
starting: null,
|
||||
}
|
||||
this._app = {
|
||||
name: 'app',
|
||||
port: null,
|
||||
repository: null,
|
||||
active: null,
|
||||
latestInstalled: null,
|
||||
latestVersion: null,
|
||||
running: null,
|
||||
updating: null,
|
||||
starting: null,
|
||||
}
|
||||
this._apps = []
|
||||
|
||||
this._socketOn(() => this.loadData())
|
||||
},
|
||||
|
||||
loadData: function() {
|
||||
socket.emit('core.config', {}, (res) => {
|
||||
this._name = res.name + ' - ' + res.serviceName
|
||||
this._app.port = res.port
|
||||
this._app.repository = res.appRepository
|
||||
this._management.port = res.managePort
|
||||
this._management.repository = res.manageRepository
|
||||
this._apps = []
|
||||
console.log('config', res)
|
||||
this._name = res.name
|
||||
if (res.title) {
|
||||
this._name += ' - ' + res.title
|
||||
}
|
||||
for (let appName of res.applications) {
|
||||
this._apps.push({
|
||||
name: appName,
|
||||
config: res[appName],
|
||||
})
|
||||
}
|
||||
console.log(this._apps)
|
||||
m.redraw()
|
||||
})
|
||||
|
||||
this.on('core.db', (res) => {
|
||||
this._management.active = res.manageActive
|
||||
this._management.latestInstalled = res.manageLatestInstalled
|
||||
this._management.latestVersion = res.manageLatestVersion
|
||||
this._app.active = res.appActive
|
||||
this._app.latestInstalled = res.appLatestInstalled
|
||||
this._app.latestVersion = res.appLatestVersion
|
||||
console.log('db', res)
|
||||
|
||||
m.redraw()
|
||||
})
|
||||
|
||||
this.on('core.status', (res) => {
|
||||
console.log(res)
|
||||
this._management.running = res.manage
|
||||
/*this._management.running = res.manage
|
||||
this._management.updating = res.manageUpdating
|
||||
this._management.starting = res.manageStarting
|
||||
this._app.running = res.app
|
||||
this._app.updating = res.appUpdating
|
||||
this._app.starting = res.appStarting
|
||||
this._app.starting = res.appStarting*/
|
||||
|
||||
m.redraw()
|
||||
})
|
||||
|
@ -97,36 +79,36 @@ const Status = Module({
|
|||
return m('div#status', [
|
||||
m('h1.header', this._name),
|
||||
m('div.split', [
|
||||
loopOver.map((group) => {
|
||||
this._apps.map((app) => {
|
||||
return m('div.item', [
|
||||
m('h4', group[0]),
|
||||
m('p', this[group[1]].port
|
||||
? `Port: ${this[group[1]].port}`
|
||||
m('h4', app.name),
|
||||
m('p', app.config.port
|
||||
? `Port: ${app.config.port}`
|
||||
: ''),
|
||||
m('p', this[group[1]].repository
|
||||
? `${this[group[1]].repository}`
|
||||
m('p', app.config.repository
|
||||
? `${app.config.repository}`
|
||||
: '< no repository >'),
|
||||
m('p', this[group[1]].active
|
||||
? `Running version: ${this[group[1]].active}`
|
||||
m('p', app.config.active
|
||||
? `Running version: ${app.config.active}`
|
||||
: '< no running version >'),
|
||||
m('p', this[group[1]].latestInstalled
|
||||
? `Latest installed: ${this[group[1]].latestInstalled}`
|
||||
m('p', app.config.latestInstalled
|
||||
? `Latest installed: ${app.config.latestInstalled}`
|
||||
: '< no version installed >'),
|
||||
m('p', this[group[1]].latestVersion
|
||||
? `Latest version: ${this[group[1]].latestVersion}`
|
||||
m('p', app.config.latestVersion
|
||||
? `Latest version: ${app.config.latestVersion}`
|
||||
: '< no version found >'),
|
||||
this[group[1]].running !== null && this[group[1]].repository
|
||||
app.config.running !== null && app.config.repository
|
||||
? m('p',
|
||||
{ class: this[group[1]].running ? 'running' : 'notrunning' },
|
||||
this[group[1]].running ? 'Running' : 'Not Running'
|
||||
{ class: app.config.running ? 'running' : 'notrunning' },
|
||||
app.config.running ? 'Running' : 'Not Running'
|
||||
)
|
||||
: null,
|
||||
!this[group[1]].running && (this[group[1]].updating || this[group[1]].starting)
|
||||
? m('div.status', this.getStatus(this[group[1]]))
|
||||
!app.config.running && (app.config.updating || app.config.starting)
|
||||
? m('div.status', this.getStatus(app.config))
|
||||
: null,
|
||||
m('button', {
|
||||
hidden: this[group[1]].running || this[group[1]].updating || this[group[1]].starting || !this[group[1]].repository,
|
||||
onclick: () => this.start(this[group[1]].name),
|
||||
hidden: app.config.running || app.config.updating || app.config.starting || !app.config.repository,
|
||||
onclick: () => this.start(app.config.name),
|
||||
}, 'Update/Start')
|
||||
])
|
||||
}),
|
||||
|
|
42
dev.mjs
Normal file
42
dev.mjs
Normal file
|
@ -0,0 +1,42 @@
|
|||
import { ServiceCore, Application, getLog, Util, Core } from 'service-core'
|
||||
import * as index from './index.mjs'
|
||||
|
||||
let util = new Util(import.meta.url)
|
||||
|
||||
const helloWorldConfig = {
|
||||
"provider": "git",
|
||||
"url": "https://git.nfp.is/api/v1/repos/thething/sc-helloworld/releases",
|
||||
"port": 8888,
|
||||
}
|
||||
|
||||
var core = new ServiceCore('sc-manager', import.meta.url)
|
||||
core.config['hello-world'] = helloWorldConfig
|
||||
core.setConfig({
|
||||
port: 4271,
|
||||
})
|
||||
|
||||
core.init(index).then(function() {
|
||||
let provConstructor = Core.providers.get(core.db.config['hello-world'].provider)
|
||||
let provider = new provConstructor(core.db.config['hello-world'])
|
||||
|
||||
let helloWorld = new Application({
|
||||
db: core.db,
|
||||
util: util,
|
||||
log: getLog('hello-world'),
|
||||
core: core.core,
|
||||
}, provider, 'hello-world')
|
||||
|
||||
core.core.applications.push(helloWorld)
|
||||
core.core.applicationMap.set('hello-world', helloWorld)
|
||||
|
||||
helloWorld.startAutoupdater()
|
||||
|
||||
return core.run()
|
||||
}).then(function() {
|
||||
return core.core.applicationMap.get('hello-world').update()
|
||||
}).then(function() {
|
||||
return core.core.runApplication(core.core.applicationMap.get('hello-world'))
|
||||
}).then(function() {
|
||||
let helloApp = core.core.applicationMap.get('hello-world')
|
||||
helloApp.on('updated', core.core.runApplication.bind(core.core, helloApp))
|
||||
})
|
0
hello-world/.gitkeep
Normal file
0
hello-world/.gitkeep
Normal file
|
@ -1,5 +1,5 @@
|
|||
export function start(config, db, log, core, http, port) {
|
||||
return import('./api/server.mjs').then(function(module) {
|
||||
return module.run(config, db, log, core, http, port)
|
||||
})
|
||||
import { run } from "./api/server.mjs"
|
||||
|
||||
export function start(http, port, ctx) {
|
||||
return run(http, port, ctx)
|
||||
}
|
34
package.json
34
package.json
|
@ -4,24 +4,42 @@
|
|||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"js:build:main": "asbundle client/index.js public/main.js",
|
||||
"dev:server": "nodemon --watch runner.mjs --watch api runner.mjs | bunyan",
|
||||
"dev:client": "nodemon --watch client --exec \"npm run js:build:main\"",
|
||||
"build": "npm run js:build:main",
|
||||
"build:main": "asbundle client/index.js public/main.js",
|
||||
"build:main:watch": "npm-watch build:main",
|
||||
"dev:server": "node dev.mjs | bunyan",
|
||||
"dev:server:watch": "npm-watch dev:server",
|
||||
"dev": "npm run dev:server && npm run dev:client",
|
||||
"build": "npm run build:main",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"watch": {
|
||||
"dev:server": {
|
||||
"patterns": [
|
||||
"api/*"
|
||||
],
|
||||
"extensions": "js,mjs",
|
||||
"quiet": true,
|
||||
"inherit": true
|
||||
},
|
||||
"build:main": {
|
||||
"patterns": [
|
||||
"client/*"
|
||||
],
|
||||
"extensions": "js,mjs",
|
||||
"quiet": true,
|
||||
"inherit": true
|
||||
}
|
||||
},
|
||||
"author": "",
|
||||
"license": "WTFPL",
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.20",
|
||||
"flaska": "^1.2.1",
|
||||
"node-static": "^0.7.11",
|
||||
"socket.io-serveronly": "^2.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"asbundle": "^2.6.1",
|
||||
"bunyan-lite": "^1.0.1",
|
||||
"mithril": "^2.0.4",
|
||||
"nodemon": "^2.0.4",
|
||||
"service-core": "^2.0.0"
|
||||
"service-core": "^3.0.0-beta.11"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue