Merge pull request #465 from embark-framework/features/ipfs-process

Processes everywhere
This commit is contained in:
Iuri Matias 2018-05-30 13:33:17 -04:00 committed by GitHub
commit 4aa33935b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 1018 additions and 486 deletions

View File

@ -1,11 +1,12 @@
const async = require('async');
const shelljs = require('shelljs');
const child_process = require('child_process');
const _ = require('underscore');
const fs = require('../../core/fs.js');
const GethCommands = require('./geth_commands.js');
/*eslint complexity: ["error", 35]*/
/*eslint complexity: ["error", 36]*/
var Blockchain = function(options) {
this.blockchainConfig = options.blockchainConfig;
this.env = options.env || 'development';
@ -54,6 +55,20 @@ var Blockchain = function(options) {
this.config.datadir = fs.embarkPath(".embark/development/datadir");
}
const spaceMessage = 'The path for %s in blockchain config contains spaces, please remove them';
if (this.config.datadir && this.config.datadir.indexOf(' ') > 0) {
console.error(__(spaceMessage, 'datadir'));
process.exit();
}
if (this.config.account.password && this.config.account.password.indexOf(' ') > 0) {
console.error(__(spaceMessage, 'account.password'));
process.exit();
}
if (this.config.genesisBlock && this.config.genesisBlock.indexOf(' ') > 0) {
console.error(__(spaceMessage, 'genesisBlock'));
process.exit();
}
this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev});
};
@ -62,7 +77,7 @@ Blockchain.prototype.runCommand = function(cmd, options, callback) {
if (this.blockchainConfig.silent) {
options.silent = true;
}
return shelljs.exec(cmd, options, callback);
return child_process.exec(cmd, options, callback);
};
Blockchain.prototype.run = function() {
@ -95,44 +110,53 @@ Blockchain.prototype.run = function() {
next();
},
function getMainCommand(next) {
self.client.mainCommand(address, function(cmd) {
next(null, cmd);
});
self.client.mainCommand(address, function(cmd, args) {
next(null, cmd, args);
}, true);
}
], function (err, cmd) {
], function (err, cmd, args) {
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.error is captured and sent to the console output regardless of silent setting
args = _.compact(args);
self.child = child_process.spawn(cmd, args, {cwd: process.cwd()});
self.child.on('error', (err) => {
err = err.toString();
console.error('Blockchain error: ', err);
if (self.env === 'development' && err.indexOf('Failed to unlock') > 0) {
console.error('\n' + __('Development blockchain has changed to use the --dev option.').yellow);
console.error(__('You can reset your workspace to fix the problem with').yellow + ' embark reset'.cyan);
console.error(__('Otherwise, you can change your data directory in blockchain.json (datadir)').yellow);
}
});
if (self.onReadyCallback) {
// Geth logs appear in stderr somehow
let lastMessage;
child.stderr.on('data', (data) => {
if (!self.readyCalled && data.indexOf('Mapped network port') > -1) {
self.readyCalled = true;
self.onReadyCallback();
}
lastMessage = data;
console.log('Geth: ' + data);
});
child.on('exit', (code) => {
if (code) {
console.error('Geth exited with error code ' + 1);
console.error(lastMessage);
}
});
}
self.child.stdout.on('data', (data) => {
console.log(`Geth error: ${data}`);
});
// Geth logs appear in stderr somehow
self.child.stderr.on('data', (data) => {
data = data.toString();
if (self.onReadyCallback && !self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
self.readyCalled = true;
self.onReadyCallback();
}
console.log('Geth: ' + data);
});
self.child.on('exit', (code) => {
if (code) {
console.error('Geth exited with error code ' + code);
}
});
});
};
Blockchain.prototype.kill = function() {
if (this.child) {
this.child.kill();
}
};
Blockchain.prototype.checkPathLength = function() {
let dappPath = fs.dappPath('');
if (dappPath.length > 66) {
@ -145,7 +169,7 @@ Blockchain.prototype.checkPathLength = function() {
};
Blockchain.prototype.isClientInstalled = function(callback) {
let versionCmd = this.client.determineVersion();
let versionCmd = this.client.determineVersionCommand();
this.runCommand(versionCmd, {}, (err, stdout, stderr) => {
if (err || stderr || !stdout || stdout.indexOf("not found") >= 0) {
return callback('Geth not found');

View File

@ -30,9 +30,16 @@ class BlockchainProcess extends ProcessWrapper {
blockchainReady() {
blockchainProcess.send({result: constants.blockchain.blockchainReady});
}
kill() {
this.blockchain.kill();
}
}
process.on('message', (msg) => {
if (msg === 'exit') {
return blockchainProcess.kill();
}
if (msg.action === constants.blockchain.init) {
blockchainProcess = new BlockchainProcess(msg.options);
return blockchainProcess.send({result: constants.blockchain.initiated});

View File

@ -12,75 +12,75 @@ class GethCommands {
commonOptions() {
let config = this.config;
let cmd = "";
let cmd = [];
cmd += this.determineNetworkType(config);
cmd.push(this.determineNetworkType(config));
if (config.datadir) {
cmd += "--datadir=\"" + config.datadir + "\" ";
cmd.push(`--datadir=${config.datadir}`);
}
if (config.light) {
cmd += "--light ";
cmd.push("--light");
}
if (config.fast) {
cmd += "--fast ";
cmd.push("--fast");
}
if (config.account && config.account.password) {
cmd += "--password " + config.account.password + " ";
cmd.push(`--password=${config.account.password}`);
}
if (Number.isInteger(config.verbosity) && config.verbosity >=0 && config.verbosity <= 5) {
cmd += "--verbosity " + config.verbosity + " ";
cmd.push("--verbosity=" + config.verbosity);
}
return cmd;
}
determineVersion() {
determineVersionCommand() {
return this.geth_bin + " version";
}
determineNetworkType(config) {
let cmd = "";
let cmd;
if (config.networkType === 'testnet') {
cmd += "--testnet ";
cmd = "--testnet ";
} else if (config.networkType === 'olympic') {
cmd += "--olympic ";
cmd = "--olympic ";
} else if (config.networkType === 'custom') {
cmd += "--networkid " + config.networkId + " ";
cmd = "--networkid=" + config.networkId;
}
return cmd;
}
initGenesisCommmand() {
let config = this.config;
let cmd = this.geth_bin + " " + this.commonOptions();
let cmd = this.geth_bin + " " + this.commonOptions().join(' ');
if (config.genesisBlock) {
cmd += "init \"" + config.genesisBlock + "\" ";
cmd += " init \"" + config.genesisBlock + "\" ";
}
return cmd;
}
newAccountCommand() {
return this.geth_bin + " " + this.commonOptions() + "account new ";
return this.geth_bin + " " + this.commonOptions().join(' ') + " account new ";
}
listAccountsCommand() {
return this.geth_bin + " " + this.commonOptions() + "account list ";
return this.geth_bin + " " + this.commonOptions().join(' ') + " account list ";
}
determineRpcOptions(config) {
let cmd = "";
let cmd = [];
cmd += "--port " + config.port + " ";
cmd += "--rpc ";
cmd += "--rpcport " + config.rpcPort + " ";
cmd += "--rpcaddr " + config.rpcHost + " ";
cmd.push("--port=" + config.port);
cmd.push("--rpc");
cmd.push("--rpcport=" + config.rpcPort);
cmd.push("--rpcaddr=" + config.rpcHost);
if (config.rpcCorsDomain) {
if (config.rpcCorsDomain === '*') {
console.log('==================================');
@ -88,7 +88,7 @@ class GethCommands {
console.log(__('make sure you know what you are doing'));
console.log('==================================');
}
cmd += "--rpccorsdomain=\"" + config.rpcCorsDomain + "\" ";
cmd.push("--rpccorsdomain=" + config.rpcCorsDomain);
} else {
console.log('==================================');
console.log(__('warning: cors is not set'));
@ -99,12 +99,12 @@ class GethCommands {
}
determineWsOptions(config) {
let cmd = "";
let cmd = [];
if (config.wsRPC) {
cmd += "--ws ";
cmd += "--wsport " + config.wsPort + " ";
cmd += "--wsaddr " + config.wsHost + " ";
cmd.push("--ws");
cmd.push("--wsport=" + config.wsPort);
cmd.push("--wsaddr=" + config.wsHost);
if (config.wsOrigins) {
if (config.wsOrigins === '*') {
console.log('==================================');
@ -112,7 +112,7 @@ class GethCommands {
console.log(__('make sure you know what you are doing'));
console.log('==================================');
}
cmd += "--wsorigins \"" + config.wsOrigins + "\" ";
cmd.push("--wsorigins=" + config.wsOrigins);
} else {
console.log('==================================');
console.log(__('warning: wsOrigins is not set'));
@ -129,44 +129,54 @@ class GethCommands {
let rpc_api = (this.config.rpcApi || ['eth', 'web3', 'net']);
let ws_api = (this.config.wsApi || ['eth', 'web3', 'net']);
let args = [];
async.series([
function commonOptions(callback) {
let cmd = self.commonOptions();
args = args.concat(cmd);
callback(null, cmd);
},
function rpcOptions(callback) {
let cmd = self.determineRpcOptions(self.config);
args = args.concat(cmd);
callback(null, cmd);
},
function wsOptions(callback) {
let cmd = self.determineWsOptions(self.config);
args = args.concat(cmd);
callback(null, cmd);
},
function dontGetPeers(callback) {
if (config.nodiscover) {
args.push("--nodiscover");
return callback(null, "--nodiscover");
}
callback(null, "");
},
function vmDebug(callback) {
if (config.vmdebug) {
args.push("--vmdebug");
return callback(null, "--vmdebug");
}
callback(null, "");
},
function maxPeers(callback) {
let cmd = "--maxpeers " + config.maxpeers;
let cmd = "--maxpeers=" + config.maxpeers;
args.push(cmd);
callback(null, cmd);
},
function mining(callback) {
if (config.mineWhenNeeded || config.mine) {
return callback(null, "--mine ");
args.push("--mine");
return callback(null, "--mine");
}
callback("");
},
function bootnodes(callback) {
if (config.bootnodes && config.bootnodes !== "" && config.bootnodes !== []) {
return callback(null, "--bootnodes " + config.bootnodes);
args.push("--bootnodes=" + config.bootnodes);
return callback(null, "--bootnodes=" + config.bootnodes);
}
callback("");
},
@ -176,15 +186,18 @@ class GethCommands {
if (ws_api.indexOf('shh') === -1) {
ws_api.push('shh');
}
args.push("--shh");
return callback(null, "--shh ");
}
callback("");
},
function rpcApi(callback) {
callback(null, '--rpcapi "' + rpc_api.join(',') + '"');
args.push('--rpcapi=' + rpc_api.join(','));
callback(null, '--rpcapi=' + rpc_api.join(','));
},
function wsApi(callback) {
callback(null, '--wsapi "' + ws_api.join(',') + '"');
args.push('--wsapi=' + ws_api.join(','));
callback(null, '--wsapi=' + ws_api.join(','));
},
function accountToUnlock(callback) {
let accountAddress = "";
@ -194,33 +207,37 @@ class GethCommands {
accountAddress = address;
}
if (accountAddress && !self.isDev) {
args.push("--unlock=" + accountAddress);
return callback(null, "--unlock=" + accountAddress);
}
callback(null, "");
},
function gasLimit(callback) {
if (config.targetGasLimit) {
return callback(null, "--targetgaslimit " + config.targetGasLimit);
args.push("--targetgaslimit=" + config.targetGasLimit);
return callback(null, "--targetgaslimit=" + config.targetGasLimit);
}
callback(null, "");
},
function mineWhenNeeded(callback) {
if (config.mineWhenNeeded && !self.isDev) {
args.push("js .embark/" + self.env + "/js/mine.js");
return callback(null, "js .embark/" + self.env + "/js/mine.js");
}
callback(null, "");
},
function isDev(callback) {
if (self.isDev) {
args.push('--dev');
return callback(null, '--dev');
}
callback(null, '');
}
], function (err, results) {
], function (err) {
if (err) {
throw new Error(err.message);
}
done(self.geth_bin + " " + results.join(" "));
return done(self.geth_bin, args);
});
}
}

View File

@ -17,6 +17,8 @@
"contractConfigChanged": "contractConfigChanged"
},
"process": {
"processLaunchRequest": "process:launch-request",
"processLaunchComplete": "process:launch-complete",
"log": "log",
"events": {
"on": "on",
@ -34,5 +36,9 @@
"blockchainReady": "blockchainReady",
"init": "init",
"initiated": "initiated"
},
"storage": {
"init": "init",
"initiated": "initiated"
}
}

View File

@ -109,13 +109,14 @@ class Blockchain {
registerServiceCheck() {
const self = this;
const NO_NODE = 'noNode';
this.addCheck('Ethereum', function (cb) {
async.waterfall([
function checkNodeConnection(next) {
self.assertNodeConnection(true, (err) => {
if (err) {
return next(true, {name: "No Blockchain node found", status: 'off'});
return next(NO_NODE, {name: "No Blockchain node found", status: 'off'});
}
next();
});
@ -137,13 +138,12 @@ class Blockchain {
});
}
], (err, statusObj) => {
if (err && err !== true) {
self.logger.error(err);
return;
if (err && err !== NO_NODE) {
return cb(err);
}
cb(statusObj);
});
});
}, 5000, 'off');
}
registerRequests() {

View File

@ -45,7 +45,7 @@ class Engine {
}, 0);
if (this.interceptLogs || this.interceptLogs === undefined) {
// this.doInterceptLogs();
this.doInterceptLogs();
}
}

View File

@ -44,9 +44,9 @@ ServicesMonitor.prototype.initCheck = function (checkName) {
});
};
ServicesMonitor.prototype.addCheck = function (checkName, checkFn, time) {
ServicesMonitor.prototype.addCheck = function (checkName, checkFn, time, initialState) {
this.logger.trace('add check: ' + checkName);
this.checkList[checkName] = {fn: checkFn, interval: time || 5000};
this.checkList[checkName] = {fn: checkFn, interval: time || 5000, status: initialState};
if (this.working) {
this.initCheck(checkName);

View File

@ -330,6 +330,7 @@ class Dashboard {
let self = this;
this.input.key(["C-c"], function () {
self.events.emit('exit');
process.exit(0);
});

View File

@ -138,5 +138,9 @@
"open another console in the same directory and run": "open another console in the same directory and run",
"For more info go to http://embark.status.im": "For more info go to http://embark.status.im",
"%s is not installed on your machine": "%s is not installed on your machine",
"You can install it by visiting: %s": "You can install it by visiting: %s"
"You can install it by visiting: %s": "You can install it by visiting: %s",
"IPFS node is offline": "IPFS node is offline",
"Starting ipfs process": "Starting ipfs process",
"ipfs process started": "ipfs process started",
"IPFS node detected": "IPFS node detected"
}

View File

@ -1,6 +1,7 @@
let async = require('async');
const constants = require('./constants');
const _ = require('underscore');
const StorageProcessesLauncher = require('./processes/storageProcesses/storageProcessesLauncher');
// require("./utils/debug_util.js")(__filename, async);
require('colors');
@ -66,6 +67,42 @@ class Embark {
templateGenerator.generate(destinationFolder, name);
}
_checkStorageEndpoint(engine, platform, callback) {
let checkFn;
_.find(engine.servicesMonitor.checkList, (value, key) => {
if(key.toLowerCase() === platform.toLowerCase()){
checkFn = value;
return true;
}
});
if (!checkFn || typeof checkFn.fn !== 'function') {
return callback();
}
checkFn.fn(function (serviceCheckResult) {
if (!serviceCheckResult.status || serviceCheckResult.status === 'off') {
return callback('No node');
}
callback();
});
}
_startStorageNode(engine, platform, callback) {
const storageProcessesLauncher = new StorageProcessesLauncher({
logger: engine.logger,
events: engine.events,
storageConfig: engine.config.storageConfig,
webServerConfig: engine.config.webServerConfig
});
return storageProcessesLauncher.launchProcess(platform.toLowerCase(), (err) => {
if (err) {
engine.logger.error(err);
return callback(err);
}
callback();
});
}
run(options) {
let self = this;
self.context = options.context || [constants.contexts.run, constants.contexts.build];
@ -135,21 +172,33 @@ class Embark {
});
});
// Check storage
const platform = engine.config.storageConfig.provider;
self._checkStorageEndpoint(engine, platform, (err) => {
if (!err) {
return;
}
self._startStorageNode(engine, platform, (err) => {
if (err) {
engine.logger.error('Error while starting a storage process for ' + platform);
engine.logger.error(err);
}
});
});
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);
});
engine.deployManager.deployContracts(function (err) {
engine.startService("fileWatcher");
if (options.runWebserver) {
engine.startService("webServer", {
host: options.serverHost,
port: options.serverPort
});
}
callback(err);
});
if (options.runWebserver) {
engine.startService("webServer", {
host: options.serverHost,
port: options.serverPort
});
}
engine.startService("fileWatcher");
callback();
}
], function (err, _result) {
if (err) {
@ -285,7 +334,7 @@ class Embark {
}
upload(options) {
const self = this;
this.context = options.context || [constants.contexts.upload, constants.contexts.build];
let engine = new Engine({
@ -324,21 +373,26 @@ class Embark {
callback();
},
function checkStorageService(callback){
let checkFn;
_.find(engine.servicesMonitor.checkList, (value, key) => {
if(key.toLowerCase() === platform.toLowerCase()){
checkFn = value;
return true;
const errorObj = {message: __('Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}:{{port}}.', {platform: platform, protocol: engine.config.storageConfig.protocol, host: engine.config.storageConfig.host, port: engine.config.storageConfig.port})};
self._checkStorageEndpoint(engine, platform, function (err) {
if (!err) {
return callback();
}
});
if (!checkFn || typeof checkFn.fn !== 'function') {
return callback();
}
checkFn.fn(function (serviceCheckResult) {
if (!serviceCheckResult.status || serviceCheckResult.status === 'off') {
return callback({message: __('Cannot upload: {{platform}} node is not running on {{protocol}}://{{host}}:{{port}}.', {platform: platform, protocol: engine.config.storageConfig.protocol, host: engine.config.storageConfig.host, port: engine.config.storageConfig.port})});
}
callback();
self._startStorageNode(engine, platform, (err) => {
if (err) {
engine.logger.error(err);
return callback(errorObj);
}
// Check endpoint again to see if really did start
self._checkStorageEndpoint(engine, platform, (err) => {
if (err) {
return callback(errorObj);
}
callback();
});
});
});
},
function setupStoragePlugin(callback){

View File

@ -101,8 +101,7 @@ class Pipeline {
const webpackProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, 'webpackProcess.js'),
logger: self.logger,
events: self.events,
normalizeInput: utils.normalizeInput
events: self.events
});
webpackProcess.send({action: constants.pipeline.init, options: {}});
webpackProcess.send({action: constants.pipeline.build, file, importsList});

View File

@ -1,6 +1,7 @@
const child_process = require('child_process');
const constants = require('../constants');
const path = require('path');
const utils = require('../utils/utils');
class ProcessLauncher {
@ -9,7 +10,6 @@ class ProcessLauncher {
* @param {Object} options Options tp start the process
* * modulePath {String} Absolute path to the module to fork
* * logger {Object} Logger
* * normalizeInput {Function} Function to normalize logs
* * events {Function} Events Emitter instance
* @return {ProcessLauncher} The ProcessLauncher instance
*/
@ -17,7 +17,6 @@ class ProcessLauncher {
this.name = path.basename(options.modulePath);
this.process = child_process.fork(options.modulePath);
this.logger = options.logger;
this.normalizeInput = options.normalizeInput;
this.events = options.events;
this.silent = options.silent;
this.exitCallback = options.exitCallback;
@ -55,9 +54,9 @@ class ProcessLauncher {
return;
}
if (this.logger[msg.type]) {
return this.logger[msg.type](this.normalizeInput(msg.message));
return this.logger[msg.type](utils.normalizeInput(msg.message));
}
this.logger.debug(this.normalizeInput(msg.message));
this.logger.debug(utils.normalizeInput(msg.message));
}
// Handle event calls from the child process

View File

@ -30,6 +30,7 @@ class ProcessWrapper {
context.console.log = this._log.bind(this, 'log');
context.console.warn = this._log.bind(this, 'warn');
context.console.error = this._log.bind(this, 'error');
context.console.info = this._log.bind(this, 'info');
context.console.debug = this._log.bind(this, 'debug');
context.console.trace = this._log.bind(this, 'trace');

View File

@ -14,17 +14,16 @@ class BlockchainProcessLauncher {
}
processEnded(code) {
this.logger.error('Blockchain process ended before the end of this process. Code: ' + code);
this.logger.error(__('Blockchain process ended before the end of this process. Code: %s', code));
}
startBlockchainNode() {
this.logger.info('Starting Blockchain node in another process'.cyan);
this.logger.info(__('Starting Blockchain node in another process').cyan);
this.blockchainProcess = new ProcessLauncher({
modulePath: utils.joinPath(__dirname, '../cmds/blockchain/blockchainProcess.js'),
logger: this.logger,
events: this.events,
normalizeInput: this.normalizeInput,
silent: this.logger.logLevel !== 'trace',
exitCallback: this.processEnded.bind(this)
});
@ -41,9 +40,13 @@ class BlockchainProcessLauncher {
});
this.blockchainProcess.once('result', constants.blockchain.blockchainReady, () => {
this.logger.info('Blockchain node is ready'.cyan);
this.logger.info(__('Blockchain node is ready').cyan);
this.events.emit(constants.blockchain.blockchainReady);
});
this.events.on('exit', () => {
this.blockchainProcess.send('exit');
});
}
}

View File

@ -0,0 +1,73 @@
const child_process = require('child_process');
const ProcessWrapper = require('../../process/processWrapper');
const constants = require('../../constants');
let ipfsProcess; // eslint-disable-line no-unused-vars
class IPFSProcess extends ProcessWrapper {
constructor(_options) {
super();
this.checkIPFSVersion();
this.startIPFSDaemon();
}
checkIPFSVersion() {
child_process.exec('ipfs --version', {silent: true}, (err, stdout, _stderr) => {
if (err) {
console.error(err);
return;
}
const match = stdout.match(/[0-9]+\.[0-9]+\.[0-9]+/);
if (match[0]) {
const versions = match[0].split('.');
if (versions[0] <= 0 && versions[1] <= 4 && versions[2] <= 14) {
console.error(`You are using IPFS version ${match[0]} which has an issue with processes.`);
console.error(`Please update to IPFS version 0.4.15 or more recent: https://github.com/ipfs/ipfs-update`);
}
}
});
}
startIPFSDaemon() {
const self = this;
this.child = child_process.spawn('ipfs', ['daemon']);
this.child.on('error', (err) => {
err = err.toString();
console.error('IPFS error: ', err);
});
this.child.stderr.on('data', (data) => {
data = data.toString();
console.log(`IPFS error: ${data}`);
});
this.child.stdout.on('data', (data) => {
data = data.toString();
if (!self.readyCalled && data.indexOf('Daemon is ready') > -1) {
self.readyCalled = true;
self.send({result: constants.storage.initiated});
}
console.log('IPFS: ' + data);
});
this.child.on('exit', (code) => {
if (code) {
console.error('IPFS exited with error code ' + code);
}
});
}
kill() {
if (this.child) {
this.child.kill();
}
}
}
process.on('message', (msg) => {
if (msg === 'exit') {
return ipfsProcess.kill();
}
if (msg.action === constants.storage.init) {
ipfsProcess = new IPFSProcess(msg.options);
}
});

View File

@ -0,0 +1,63 @@
const fs = require('../../core/fs');
const utils = require('../../utils/utils');
const ProcessLauncher = require('../../process/processLauncher');
const constants = require('../../constants');
class StorageProcessesLauncher {
constructor(options) {
this.logger = options.logger;
this.events = options.events;
this.storageConfig = options.storageConfig;
this.webServerConfig = options.webServerConfig;
this.processes = {};
this.events.on('exit', () => {
Object.keys(this.processes).forEach(processName => {
this.processes[processName].send('exit');
});
});
}
processExited(storageName, code) {
this.logger.error(__(`Storage process for ${storageName} ended before the end of this process. Code: ${code}`));
}
launchProcess(storageName, callback) {
const self = this;
if (self.processes[storageName]) {
return callback(__('Storage process already started'));
}
const filePath = utils.joinPath(__dirname, `./${storageName}.js`);
fs.access(filePath, (err) => {
if (err) {
return callback(__('No process file for this storage type exists. Please start the process locally.'));
}
self.logger.info(__(`Starting ${storageName} process`).cyan);
self.processes[storageName] = new ProcessLauncher({
modulePath: filePath,
logger: self.logger,
events: self.events,
silent: true,
exitCallback: self.processExited.bind(this, storageName)
});
self.processes[storageName].send({
action: constants.blockchain.init, options: {
storageConfig: self.storageConfig,
webServerConfig: self.webServerConfig
}
});
self.processes[storageName].on('result', constants.storage.initiated, (msg) => {
if (msg.error) {
self.processes[storageName].disconnect();
delete self.processes[storageName];
return callback(msg.error);
}
self.logger.info(__(`${storageName} process started`).cyan);
callback();
});
});
}
}
module.exports = StorageProcessesLauncher;

View File

@ -0,0 +1,75 @@
const child_process = require('child_process');
const ProcessWrapper = require('../../process/processWrapper');
const constants = require('../../constants');
const fs = require('../../core/fs');
let swarmProcess;
class SwarmProcess extends ProcessWrapper {
constructor(options) {
super();
this.storageConfig = options.storageConfig;
this.webServerConfig = options.webServerConfig;
}
startSwarmDaemon() {
const self = this;
if (!this.storageConfig.account || !this.storageConfig.account.address || !this.storageConfig.account.password) {
return 'Account address and password are needed in the storage config to start the Swarm process';
}
let corsDomain = 'http://localhost:8000';
if (self.webServerConfig && self.webServerConfig && self.webServerConfig.host && self.webServerConfig.port) {
corsDomain = `http://${self.webServerConfig.host}:${self.webServerConfig.port}`;
}
const args = [
`--bzzaccount=${this.storageConfig.account.address}`,
`--password=${fs.dappPath(this.storageConfig.account.password)}`,
`--corsdomain=${corsDomain}`
];
this.child = child_process.spawn(this.storageConfig.swarmPath || 'swarm', args);
this.child.on('error', (err) => {
err = err.toString();
console.error('Swarm error: ', err);
});
this.child.stdout.on('data', (data) => {
data = data.toString();
console.log(`Swarm error: ${data}`);
});
// Swarm logs appear in stderr somehow
this.child.stderr.on('data', (data) => {
data = data.toString();
if (!self.readyCalled && data.indexOf('Swarm http proxy started') > -1) {
self.readyCalled = true;
self.send({result: constants.storage.initiated});
}
console.log('Swarm: ' + data);
});
this.child.on('exit', (code) => {
if (code) {
console.error('Swarm exited with error code ' + code);
}
});
}
kill() {
if (this.child) {
this.child.kill();
}
}
}
process.on('message', (msg) => {
if (msg === 'exit') {
return swarmProcess.kill();
}
if (msg.action === constants.storage.init) {
swarmProcess = new SwarmProcess(msg.options);
const error = swarmProcess.startSwarmDaemon();
if (error) {
swarmProcess.send({result: constants.storage.initiated, error});
}
}
});

962
package-lock.json generated

File diff suppressed because it is too large Load Diff