embark-area-51/lib/core/engine.js

251 lines
8.0 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let http = require('http');
let Web3 = require('web3');
let utils = require('../utils/utils.js');
let Events = require('./events.js');
let Logger = require('./logger.js');
let Config = require('./config.js');
let DeployManager = require('../contracts/deploy_manager.js');
let ABIGenerator = require('../contracts/abi.js');
let ServicesMonitor = require('./services_monitor.js');
let Pipeline = require('../pipeline/pipeline.js');
2017-03-30 11:12:39 +00:00
let Serve = require('../pipeline/server.js');
2017-03-29 17:50:05 +00:00
let Watch = require('../pipeline/watch.js');
2017-03-30 11:12:39 +00:00
let version = require('../../package.json');
class Engine {
constructor(options) {
this.env = options.env;
this.embarkConfig = options.embarkConfig;
this.interceptLogs = options.interceptLogs;
this.version = version;
}
}
2017-03-30 11:12:39 +00:00
Engine.prototype.init = function (_options) {
2017-03-29 17:50:05 +00:00
let self = this;
let options = _options || {};
this.events = new Events();
this.logger = options.logger || new Logger({logLevel: 'debug'});
2017-03-04 17:42:24 +00:00
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
this.plugins = this.config.plugins;
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger});
2017-03-30 11:12:39 +00:00
this.servicesMonitor.addCheck('embarkVersion', function (cb) {
return cb({name: 'Embark ' + self.version, status: 'green'});
}, 0);
};
2017-03-30 11:12:39 +00:00
Engine.prototype.startMonitor = function () {
2017-03-29 17:50:05 +00:00
let self = this;
if (this.plugins) {
2017-03-29 17:50:05 +00:00
let servicePlugins = this.plugins.getPluginsFor('serviceChecks');
2017-03-30 11:12:39 +00:00
servicePlugins.forEach(function (plugin) {
plugin.serviceChecks.forEach(function (pluginCheck) {
self.servicesMonitor.addCheck(pluginCheck.checkName, pluginCheck.checkFn, pluginCheck.time);
});
});
}
this.servicesMonitor.startMonitor();
};
2017-03-30 11:12:39 +00:00
Engine.prototype.startService = function (serviceName, _options) {
2017-03-29 17:50:05 +00:00
let options = _options || {};
2017-03-29 17:50:05 +00:00
let services = {
2017-03-30 11:12:39 +00:00
"pipeline": this.pipelineService,
"abi": this.abiService,
"deployment": this.deploymentService,
2017-03-04 02:48:32 +00:00
"fileWatcher": this.fileWatchService,
2017-03-30 11:12:39 +00:00
"webServer": this.webServerService,
"ipfs": this.ipfsService,
"web3": this.web3Service
2017-03-04 02:48:32 +00:00
};
2017-03-29 17:50:05 +00:00
let service = services[serviceName];
2017-03-04 02:48:32 +00:00
if (!service) {
throw new Error("unknown service: " + serviceName);
}
2017-03-04 02:48:32 +00:00
// need to be careful with circular references due to passing the web3 object
//this.logger.trace("calling: " + serviceName + "(" + JSON.stringify(options) + ")");
2017-03-04 02:48:32 +00:00
return service.apply(this, [options]);
};
2017-03-30 11:12:39 +00:00
Engine.prototype.pipelineService = function (options) {
2017-03-29 17:50:05 +00:00
let self = this;
2017-03-04 02:48:32 +00:00
this.logger.setStatus("Building Assets");
2017-03-29 17:50:05 +00:00
let pipeline = new Pipeline({
2017-03-04 02:48:32 +00:00
buildDir: this.config.buildDir,
contractsFiles: this.config.contractsFiles,
assetFiles: this.config.assetFiles,
logger: this.logger,
plugins: this.plugins
});
2017-03-30 11:12:39 +00:00
this.events.on('abi', function (abi) {
2017-03-04 02:48:32 +00:00
self.currentAbi = abi;
pipeline.build(abi);
self.events.emit('outputDone');
2017-03-04 02:48:32 +00:00
});
// TODO: still need to redeploy contracts because the original contracts
// config is being corrupted
//this.events.on('file-event', function(fileType, path) {
// if (fileType === 'asset') {
// self.config.reloadConfig();
// pipeline.build(self.abi, path);
// self.events.emit('outputDone');
// }
//});
2017-03-04 02:48:32 +00:00
};
2017-03-30 11:12:39 +00:00
Engine.prototype.abiService = function (options) {
2017-03-29 17:50:05 +00:00
let self = this;
2017-03-30 11:12:39 +00:00
let generateABICode = function (contractsManager) {
2017-03-29 17:50:05 +00:00
let abiGenerator = new ABIGenerator({
2017-03-04 02:48:32 +00:00
blockchainConfig: self.config.blockchainConfig,
contractsManager: contractsManager,
plugins: self.plugins,
storageConfig: self.config.storageConfig,
communicationConfig: self.config.communicationConfig
});
2017-03-29 17:50:05 +00:00
let embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
let vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
let vanillaContractsABI = abiGenerator.generateContracts(false);
2017-03-04 02:48:32 +00:00
self.events.emit('abi-contracts-vanila', vanillaContractsABI);
self.events.emit('abi-vanila', vanillaABI);
self.events.emit('abi', embarkJSABI);
};
this.events.on('contractsDeployed', generateABICode);
this.events.on('blockchainDisabled', generateABICode);
};
2017-03-30 11:12:39 +00:00
Engine.prototype.deploymentService = function (options) {
2017-03-29 17:50:05 +00:00
let self = this;
2017-03-04 02:48:32 +00:00
this.deployManager = new DeployManager({
web3: options.web3 || self.web3,
2017-03-04 02:48:32 +00:00
trackContracts: options.trackContracts,
config: this.config,
logger: this.logger,
plugins: this.plugins,
events: this.events
});
2017-03-30 11:12:39 +00:00
this.events.on('file-event', function (fileType, path) {
// TODO: for now need to deploy on asset chanes as well
// because the contractsManager config is corrupted after a deploy
//if (fileType === 'contract' || fileType === 'config') {
2017-03-30 11:12:39 +00:00
self.config.reloadConfig();
self.deployManager.deployContracts(function () {
});
//}
2017-03-04 02:48:32 +00:00
});
};
2017-03-30 11:12:39 +00:00
Engine.prototype.fileWatchService = function (options) {
2017-03-04 02:48:32 +00:00
this.logger.setStatus("Watching for changes");
2017-03-29 17:50:05 +00:00
let watch = new Watch({logger: this.logger, events: this.events});
2017-03-04 02:48:32 +00:00
watch.start();
};
2017-03-30 11:12:39 +00:00
Engine.prototype.webServerService = function (options) {
2017-03-29 17:50:05 +00:00
let self = this;
let webServerConfig = this.config.webServerConfig;
2017-03-30 11:12:39 +00:00
if (!webServerConfig.enabled) {
return;
}
2017-03-29 17:50:05 +00:00
let host = options.host || webServerConfig.host;
2017-03-30 11:12:39 +00:00
let port = options.port || webServerConfig.port;
2017-03-04 02:48:32 +00:00
this.logger.setStatus("Starting Server");
2017-03-29 17:50:05 +00:00
let server = new Server({
2017-03-04 02:48:32 +00:00
logger: this.logger,
host: host,
port: port
});
2017-03-30 11:12:39 +00:00
self.servicesMonitor.addCheck('Webserver', function (cb) {
2017-03-29 17:50:05 +00:00
let devServer = 'Webserver (http://' + host + ':' + port + ')';
return cb({name: devServer, status: 'green'});
});
2017-03-30 11:12:39 +00:00
server.start(function () {
});
};
2017-03-30 11:12:39 +00:00
Engine.prototype.ipfsService = function (options) {
2017-03-29 17:50:05 +00:00
let self = this;
2017-03-30 11:12:39 +00:00
self.servicesMonitor.addCheck('IPFS', function (cb) {
utils.checkIsAvailable('http://localhost:5001', function (available) {
if (available) {
//Ideally this method should be in an IPFS API JSONRPC wrapper
//The URL should also be flexible to accept non-default IPFS url
self.logger.trace("Checking IPFS version...");
2017-03-30 11:12:39 +00:00
http.get('http://localhost:5001/api/v0/version', function (res) {
2017-03-29 17:50:05 +00:00
let body = '';
2017-03-30 11:12:39 +00:00
res.on('data', function (d) {
body += d;
});
2017-03-30 11:12:39 +00:00
res.on('end', function () {
try {
2017-03-29 17:50:05 +00:00
let parsed = JSON.parse(body);
2017-03-30 11:12:39 +00:00
if (parsed.Version) {
return cb({name: ("IPFS " + parsed.Version), status: 'green'});
}
2017-03-30 11:12:39 +00:00
else {
return cb({name: "IPFS ", status: 'green'});
}
}
2017-03-30 11:12:39 +00:00
catch (e) {
return cb({name: "IPFS ", status: 'red'});
}
});
2017-03-30 11:12:39 +00:00
res.on('error', function (err) {
self.logger.trace("Check IPFS version error: " + err);
return cb({name: "IPFS ", status: 'red'});
});
});
}
else {
return cb({name: "IPFS ", status: 'red'});
}
});
2017-03-04 02:48:32 +00:00
});
};
2017-03-30 11:12:39 +00:00
Engine.prototype.web3Service = function (options) {
2017-03-29 17:50:05 +00:00
let self = this;
this.web3 = options.web3;
if (this.web3 === undefined) {
this.web3 = new Web3();
2017-03-29 17:50:05 +00:00
let web3Endpoint = 'http://' + this.config.blockchainConfig.rpcHost + ':' + this.config.blockchainConfig.rpcPort;
this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint));
}
2017-03-30 11:12:39 +00:00
self.servicesMonitor.addCheck('Ethereum', function (cb) {
if (self.web3.isConnected()) {
2017-03-30 11:12:39 +00:00
return cb({
name: (self.web3.version.node.split("/")[0] + " " + self.web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)"),
status: 'green'
});
} else {
return cb({name: "No Blockchain node found", status: 'red'});
}
});
2017-03-30 11:12:39 +00:00
self.servicesMonitor.addCheck('Whisper', function (cb) {
self.web3.version.getWhisper(function (err, res) {
if (err) {
return cb({name: 'Whisper', status: 'red'});
} else {
return cb({name: 'Whisper', status: 'green'});
}
});
});
};
module.exports = Engine;