From c195fde0511fce2ed0fa64f4b2174f2d90d9b202 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 23 Jul 2018 10:36:53 +0100 Subject: [PATCH] Adding light monitor --- lib/cmd.js | 2 - lib/constants.json | 1 + lib/dashboard/dashboard.js | 16 ++- lib/dashboard/light_monitor.js | 171 +++++++++++++++++++++++++++++++++ lib/dashboard/monitor.js | 4 +- lib/index.js | 90 +++++++++++++++-- 6 files changed, 266 insertions(+), 18 deletions(-) create mode 100644 lib/dashboard/light_monitor.js diff --git a/lib/cmd.js b/lib/cmd.js index 52a59bc0c..e151e3f77 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -157,8 +157,6 @@ class Cmd { serverHost: options.host, client: options.client || 'geth', locale: options.locale, - runWebserver: !options.noserver, - useDashboard: !options.nodashboard, logFile: options.logfile, logLevel: options.loglevel }); diff --git a/lib/constants.json b/lib/constants.json index caf92da9c..31b779841 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -7,6 +7,7 @@ "run": "run", "upload": "upload", "build": "build", + "console": "console", "graph": "graph", "test": "test", "reset": "reset", diff --git a/lib/dashboard/dashboard.js b/lib/dashboard/dashboard.js index 0bf7a7a55..b402c064d 100644 --- a/lib/dashboard/dashboard.js +++ b/lib/dashboard/dashboard.js @@ -2,6 +2,7 @@ let async = require('async'); let windowSize = require('window-size'); let Monitor = require('./monitor.js'); +let LightMonitor = require('./light_monitor.js'); let Console = require('./console.js'); class Dashboard { @@ -11,6 +12,7 @@ class Dashboard { this.plugins = options.plugins; this.version = options.version; this.env = options.env; + this.light = options.light || false; this.events.on('firstDeploymentDone', this.checkWindowSize.bind(this)); this.events.on('outputDone', this.checkWindowSize.bind(this)); @@ -37,13 +39,17 @@ class Dashboard { callback(); }, function startMonitor(callback) { - monitor = new Monitor({env: self.env, console: console, events: self.events}); + let monitor; + if(self.light) { + monitor = new LightMonitor({env: self.env, console: console, events: self.events}); + } else { + monitor = new Monitor({env: self.env, console: console, events: self.events}); + self.events.on('contractsState', monitor.setContracts); + self.events.on('status', monitor.setStatus.bind(monitor)); + self.events.on('servicesState', monitor.availableServices.bind(monitor)); + } self.logger.logFunction = monitor.logEntry; - self.events.on('contractsState', monitor.setContracts); - self.events.on('status', monitor.setStatus.bind(monitor)); - self.events.on('servicesState', monitor.availableServices.bind(monitor)); - self.events.setCommandHandler("console:command", monitor.executeCmd.bind(monitor)); self.logger.info('========================'.bold.green); diff --git a/lib/dashboard/light_monitor.js b/lib/dashboard/light_monitor.js new file mode 100644 index 000000000..3265b19cd --- /dev/null +++ b/lib/dashboard/light_monitor.js @@ -0,0 +1,171 @@ +let blessed = require("neo-blessed"); +let CommandHistory = require('./command_history.js'); + +class LightMonitor { + constructor(_options) { + let options = _options || {}; + this.env = options.env; + this.console = options.console; + this.history = new CommandHistory(); + this.events = options.events; + + this.color = options.color || "green"; + this.minimal = options.minimal || false; + + this.screen = blessed.screen({ + smartCSR: true, + title: options.title || ("Embark " + options.version), + dockBorders: false, + fullUnicode: true, + autoPadding: true + }); + + this.layoutLog(); + this.layoutCmd(); + + this.screen.key(["C-c"], function () { + process.exit(0); + }); + + this.logEntry = this.logEntry.bind(this); + + this.screen.render(); + this.input.focus(); + } + + logEntry() { + this.logText.log(...arguments); + this.screen.render(); + } + + layoutLog() { + this.log = blessed.box({ + label: __("Logs"), + padding: 1, + width: "100%", + height: "100%", + left: "0%", + top: "0%", + border: { + type: "line" + }, + style: { + fg: -1, + border: { + fg: this.color + } + } + }); + + this.logText = blessed.log({ + parent: this.log, + tags: true, + width: "100%-5", + //height: '90%', + scrollable: true, + input: false, + alwaysScroll: true, + scrollbar: { + ch: " ", + inverse: true + }, + keys: false, + vi: false, + mouse: true + }); + + this.screen.append(this.log); + } + + layoutCmd() { + this.consoleBox = blessed.box({ + label: __('Console'), + tags: true, + padding: 0, + width: '100%', + height: '6%', + left: '0%', + top: '95%', + border: { + type: 'line' + }, + style: { + fg: 'black', + border: { + fg: this.color + } + } + }); + + this.input = blessed.textbox({ + parent: this.consoleBox, + name: 'input', + input: true, + keys: false, + top: 0, + left: 1, + height: '50%', + width: '100%-2', + inputOnFocus: true, + style: { + fg: 'green', + bg: 'black', + focus: { + bg: 'black', + fg: 'green' + } + } + }); + + let self = this; + + this.input.key(["C-c"], function () { + self.events.emit('exit'); + process.exit(0); + }); + + this.input.key(["C-w"], function () { + self.input.clearValue(); + self.input.focus(); + }); + + this.input.key(["up"], function () { + let cmd = self.history.getPreviousCommand(); + self.input.setValue(cmd); + self.input.focus(); + }); + + this.input.key(["down"], function () { + let cmd = self.history.getNextCommand(); + self.input.setValue(cmd); + self.input.focus(); + }); + + this.input.on('submit', this.submitCmd.bind(this)); + + this.screen.append(this.consoleBox); + } + + submitCmd(cmd) { + if (cmd !== '') { + this.history.addCommand(cmd); + this.executeCmd(cmd); + } + this.input.clearValue(); + this.input.focus(); + } + + executeCmd(cmd, cb) { + const self = this; + self.logText.log('console> '.bold.green + cmd); + self.console.executeCmd(cmd, function (result) { + self.logText.log(result); + if (cb) { + cb(result); + } + }); + } + +} + +module.exports = LightMonitor; diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index 9965c9f65..c437599d7 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -1,7 +1,7 @@ let blessed = require("neo-blessed"); let CommandHistory = require('./command_history.js'); -class Dashboard { +class Monitor { constructor(_options) { let options = _options || {}; this.env = options.env; @@ -378,4 +378,4 @@ class Dashboard { } -module.exports = Dashboard; +module.exports = Monitor; diff --git a/lib/index.js b/lib/index.js index 69f3ef47a..f3a74421e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,16 +61,16 @@ class Embark { self.context = options.context || [constants.contexts.run, constants.contexts.build]; let Dashboard = require('./dashboard/dashboard.js'); - let webServerConfig = { - enabled: options.runWebserver - }; + let webServerConfig = { + enabled: options.runWebserver + }; - if (options.serverHost) { - webServerConfig.host = options.serverHost; - } - if (options.serverPort) { - webServerConfig.port = options.serverPort; - } + if (options.serverHost) { + webServerConfig.host = options.serverHost; + } + if (options.serverPort) { + webServerConfig.port = options.serverPort; + } const Engine = require('./core/engine.js'); const engine = new Engine({ @@ -201,6 +201,7 @@ class Embark { engine.startService("deployment", {onlyCompile: options.onlyCompile}); engine.startService("storage"); engine.startService("codeGenerator"); + callback(); }, function deploy(callback) { @@ -235,7 +236,78 @@ class Embark { } console(options) { + this.context = options.context || [constants.contexts.run, constants.contexts.console]; + const Dashboard = require('./dashboard/dashboard.js'); + const Engine = require('./core/engine.js'); + const engine = new Engine({ + env: options.env, + client: options.client, + locale: options.locale, + version: this.version, + embarkConfig: options.embarkConfig || 'embark.json', + logFile: options.logFile, + logLevel: options.logLevel, + context: this.context, + useDashboard: true + }); + engine.init(); + async.parallel([ + function startDashboard(callback) { + let dashboard = new Dashboard({ + events: engine.events, + logger: engine.logger, + plugins: engine.plugins, + version: engine.version, + light: true, + env: engine.env + }); + dashboard.start(function () { + engine.logger.info(__('dashboard start')); + callback(); + }); + }, + function (callback) { + let pluginList = engine.plugins.listPlugins(); + if (pluginList.length > 0) { + engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", ")); + } + engine.startService("serviceMonitor"); + engine.startService("libraryManager"); + engine.startService("codeRunner"); + engine.startService("web3"); + engine.startService("pipeline"); + engine.startService("deployment"); + engine.startService("storage"); + engine.startService("codeGenerator"); + + engine.events.on('check:backOnline:Ethereum', function () { + engine.logger.info(__('Ethereum node detected') + '..'); + engine.config.reloadConfig(); + engine.events.request('deploy:contracts', function (err) { + if (err) { + return; + } + engine.logger.info(__('Deployment Done')); + }); + }); + + engine.events.on('outputDone', function () { + engine.logger.info((__("Looking for documentation? You can find it at") + " ").cyan + "http://embark.status.im/docs/".green.underline + ".".cyan); + engine.logger.info(__("Ready").underline); + engine.events.emit("status", __("Ready").green); + }); + + callback(); + } + ], function (err, _result) { + if (err) { + engine.logger.error(err.message); + engine.logger.info(err.stack); + } else { + engine.events.emit('firstDeploymentDone'); + } + }); } graph(options) {