finish hiding logs

This commit is contained in:
Jonathan Rainville 2018-05-22 14:13:56 -04:00
parent 2062f9032c
commit 7f45b6724a
5 changed files with 143 additions and 61 deletions

View File

@ -1,8 +1,9 @@
var shelljs = require('shelljs'); const async = require('async');
const shelljs = require('shelljs');
var fs = require('../../core/fs.js'); const fs = require('../../core/fs.js');
var GethCommands = require('./geth_commands.js'); const GethCommands = require('./geth_commands.js');
/*eslint complexity: ["error", 35]*/ /*eslint complexity: ["error", 35]*/
var Blockchain = function(options) { var Blockchain = function(options) {
@ -10,6 +11,7 @@ var Blockchain = function(options) {
this.env = options.env || 'development'; this.env = options.env || 'development';
this.client = options.client; this.client = options.client;
this.isDev = options.isDev; this.isDev = options.isDev;
this.onReadyCallback = options.onReadyCallback;
if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') { if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') {
console.log("===> " + __("warning: running default config on a non-development environment")); console.log("===> " + __("warning: running default config on a non-development environment"));
@ -55,15 +57,12 @@ var Blockchain = function(options) {
this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev}); this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev});
}; };
Blockchain.prototype.runCommand = function(cmd, options) { Blockchain.prototype.runCommand = function(cmd, options, callback) {
console.log(__("running: %s", cmd.underline).green); console.log(__("running: %s", cmd.underline).green);
return shelljs.exec(cmd, options, (err, stdout, _stderr) => { if (this.blockchainConfig.silent) {
if (err && this.env === 'development' && stdout.indexOf('Failed to unlock') > 0) { options.silent = true;
console.warn('\n' + __('Development blockchain has changed to use the --dev option.').yellow); }
console.warn(__('You can reset your workspace to fix the problem with').yellow + ' embark reset'.cyan); return shelljs.exec(cmd, options, callback);
console.warn(__('Otherwise, you can change your data directory in blockchain.json (datadir)').yellow);
}
});
}; };
Blockchain.prototype.run = function() { Blockchain.prototype.run = function() {
@ -73,62 +72,121 @@ Blockchain.prototype.run = function() {
console.log(__("Embark Blockchain Using: %s", this.client.name.underline).magenta); console.log(__("Embark Blockchain Using: %s", this.client.name.underline).magenta);
console.log("===============================================================================".magenta); console.log("===============================================================================".magenta);
console.log("===============================================================================".magenta); console.log("===============================================================================".magenta);
if (!this.isClientInstalled()) {
console.log(__("could not find {{geth_bin}} command; is {{client_name}} installed or in the PATH?", {geth_bin: this.config.geth_bin, client_name: this.client.name}).green);
return;
}
let address = ''; let address = '';
if (!this.isDev) { async.waterfall([
address = this.initChainAndGetAddress(); function checkInstallation(next) {
} self.isClientInstalled((err) => {
this.client.mainCommand(address, function(cmd) { if (err) {
self.runCommand(cmd, {async: true}); console.log(__("could not find {{geth_bin}} command; is {{client_name}} installed or in the PATH?", {geth_bin: this.config.geth_bin, client_name: this.client.name}).green);
return next(err);
}
next();
});
},
function init(next) {
if (!self.isDev) {
return self.initChainAndGetAddress((err, addr) => {
address = addr;
next(err);
});
}
next();
},
function getMainCommand(next) {
self.client.mainCommand(address, function(cmd) {
console.log(cmd);
next(null, cmd);
});
}
], function (err, cmd) {
if (err) {
console.error(err);
return;
}
const child = self.runCommand(cmd, {}, (err, stdout, _stderr) => {
if (err && self.env === 'development' && stdout.indexOf('Failed to unlock') > 0) {
console.warn('\n' + __('Development blockchain has changed to use the --dev option.').yellow);
console.warn(__('You can reset your workspace to fix the problem with').yellow + ' embark reset'.cyan);
console.warn(__('Otherwise, you can change your data directory in blockchain.json (datadir)').yellow);
}
});
if (self.onReadyCallback) {
// Geth logs appear in stderr somehow
child.stderr.on('data', (data) => {
if (!self.readyCalled && data.indexOf('Mapped network port') > -1) {
self.readyCalled = true;
return self.onReadyCallback();
}
console.log('Geth:' + data);
});
}
}); });
}; };
Blockchain.prototype.isClientInstalled = function() { Blockchain.prototype.isClientInstalled = function(callback) {
let versionCmd = this.client.determineVersion(); let versionCmd = this.client.determineVersion();
let result = this.runCommand(versionCmd); this.runCommand(versionCmd, {}, (err, stdout, stderr) => {
if (err || stderr || !stdout || stdout.indexOf("not found") >= 0) {
if (result.output === undefined || result.output.indexOf("not found") >= 0) { return callback('Geth not found');
return false; }
} callback();
return true; });
}; };
Blockchain.prototype.initChainAndGetAddress = function() { Blockchain.prototype.initChainAndGetAddress = function(callback) {
const self = this;
var address = null, result; var address = null, result;
// ensure datadir exists, bypassing the interactive liabilities prompt. // ensure datadir exists, bypassing the interactive liabilities prompt.
this.datadir = '.embark/' + this.env + '/datadir'; self.datadir = '.embark/' + self.env + '/datadir';
fs.mkdirpSync(this.datadir);
// copy mining script async.waterfall([
fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true}); function makeDir(next) {
fs.mkdirp(self.datadir, next);
},
function copy(next) {
// copy mining script
fs.copy(fs.embarkPath("js"), ".embark/" + self.env + "/js", {overwrite: true}, next);
},
function listAccounts(next) {
self.runCommand(self.client.listAccountsCommand(), {}, (err, stdout, stderr) => {
if (err || stderr || stdout === undefined || stdout.match(/{(\w+)}/) === null || stdout.indexOf("Fatal") >= 0) {
console.log(__("no accounts found").green);
return next();
// check if an account already exists, create one if not, return address } else {
result = this.runCommand(this.client.listAccountsCommand()); console.log(__("already initialized").green);
if (result.output === undefined || result.output.match(/{(\w+)}/) === null || result.output.indexOf("Fatal") >= 0) { address = result.output.match(/{(\w+)}/)[1];
console.log(__("no accounts found").green); }
if (this.config.genesisBlock) { });
},
function genesisBlock(next) {
if (!self.config.genesisBlock) {
return next();
}
console.log(__("initializing genesis block").green); console.log(__("initializing genesis block").green);
result = this.runCommand(this.client.initGenesisCommmand()); self.runCommand(self.client.initGenesisCommmand(), {}, (err, _stdout, _stderr) => {
next(err);
});
},
function newAccount(next) {
result = self.runCommand(self.client.newAccountCommand(), {}, (err, stdout, _stderr) => {
if (err) {
return next(err);
}
address = stdout.match(/{(\w+)}/)[1];
});
} }
], (err) => {
result = this.runCommand(this.client.newAccountCommand()); callback(err, address);
address = result.output.match(/{(\w+)}/)[1]; });
} else {
console.log(__("already initialized").green);
address = result.output.match(/{(\w+)}/)[1];
}
return address;
}; };
var BlockchainClient = function(blockchainConfig, client, env, isDev) { var BlockchainClient = function(blockchainConfig, client, env, isDev, onReadyCallback) {
// TODO add other clients at some point // TODO add other clients at some point
if (client === 'geth') { if (client === 'geth') {
return new Blockchain({blockchainConfig, client: GethCommands, env, isDev}); return new Blockchain({blockchainConfig, client: GethCommands, env, isDev, onReadyCallback});
} else { } else {
throw new Error('unknown client'); throw new Error('unknown client');
} }

View File

@ -1,6 +1,9 @@
const ProcessWrapper = require('../../process/processWrapper'); const ProcessWrapper = require('../../process/processWrapper');
const BlockchainClient = require('./blockchain'); const BlockchainClient = require('./blockchain');
const i18n = require('../../i18n/i18n.js'); const i18n = require('../../i18n/i18n.js');
const constants = require('../../constants');
let blockchainProcess;
class BlockchainProcess extends ProcessWrapper { class BlockchainProcess extends ProcessWrapper {
constructor(options) { constructor(options) {
@ -12,15 +15,26 @@ class BlockchainProcess extends ProcessWrapper {
i18n.setOrDetectLocale(options.locale); i18n.setOrDetectLocale(options.locale);
this.blockchainConfig.verbosity = 0; this.blockchainConfig.silent = true;
this.blockchain = BlockchainClient(this.blockchainConfig, this.client, this.env, this.isDev); this.blockchain = BlockchainClient(
this.blockchainConfig,
this.client,
this.env,
this.isDev,
this.blockchainReady.bind(this)
);
this.blockchain.run(); this.blockchain.run();
} }
blockchainReady() {
blockchainProcess.send({result: constants.blockchain.blockchainReady});
}
} }
process.on('message', (msg) => { process.on('message', (msg) => {
if (msg.action === 'init') { if (msg.action === constants.blockchain.init) {
const blockchainProcess = new BlockchainProcess(msg.options); blockchainProcess = new BlockchainProcess(msg.options);
return process.send({result: 'initiated'}); return blockchainProcess.send({result: constants.blockchain.initiated});
} }
}); });

View File

@ -29,5 +29,10 @@
"build": "build", "build": "build",
"initiated": "initiated", "initiated": "initiated",
"built": "built" "built": "built"
},
"blockchain": {
"blockchainReady": "blockchainReady",
"init": "init",
"initiated": "initiated"
} }
} }

View File

@ -17,6 +17,7 @@ const request = require('request');
const ProcessLauncher = require('../process/processLauncher'); const ProcessLauncher = require('../process/processLauncher');
const utils = require('../utils/utils'); const utils = require('../utils/utils');
const constants = require('../constants');
class Engine { class Engine {
constructor(options) { constructor(options) {
@ -339,7 +340,8 @@ class Engine {
} }
startBlockchainNode() { startBlockchainNode() {
self.isRunningBlockchain = true; this.logger.info('Starting Blockchain node in another process'.cyan);
this.isRunningBlockchain = true;
this.blockchainProcess = new ProcessLauncher({ this.blockchainProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, '../cmds/blockchain/blockchainProcess.js'), modulePath: utils.joinPath(__dirname, '../cmds/blockchain/blockchainProcess.js'),
@ -349,7 +351,7 @@ class Engine {
silent: true silent: true
}); });
this.blockchainProcess.send({ this.blockchainProcess.send({
action: 'init', options: { action: constants.blockchain.init, options: {
blockchainConfig: this.config.blockchainConfig, blockchainConfig: this.config.blockchainConfig,
client: this.client, client: this.client,
env: this.env, env: this.env,
@ -358,11 +360,10 @@ class Engine {
} }
}); });
// TODO use event from process to know when node is ready this.blockchainProcess.once('result', constants.blockchain.blockchainReady, () => {
setTimeout(() => { this.logger.info('Blockchain node is ready'.cyan);
// Wait a couple seconds for process to start this.startService('web3');
this.startService("web3"); });
}, 2000);
} }
libraryManagerService(_options) { libraryManagerService(_options) {

View File

@ -45,6 +45,10 @@ class ProcessWrapper {
} }
process.send({result: constants.process.log, message: messages, type}); process.send({result: constants.process.log, message: messages, type});
} }
send() {
process.send(...arguments);
}
} }
process.on('exit', () => { process.on('exit', () => {