2017-03-29 15:37:30 +00:00
|
|
|
let async = require('async');
|
2018-07-24 12:29:06 +00:00
|
|
|
|
2018-07-19 20:31:28 +00:00
|
|
|
const utils = require('../utils/utils.js');
|
|
|
|
//require("../utils/debug_util.js")(__filename, async);
|
2017-03-01 01:42:03 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class DeployManager {
|
2018-07-19 20:31:28 +00:00
|
|
|
constructor(options) {
|
2018-05-29 21:23:29 +00:00
|
|
|
const self = this;
|
2018-07-19 20:31:28 +00:00
|
|
|
this.config = options.config;
|
|
|
|
this.logger = options.logger;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.blockchainConfig = this.config.blockchainConfig;
|
2017-12-21 16:21:36 +00:00
|
|
|
|
2018-07-19 20:31:28 +00:00
|
|
|
this.events = options.events;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-05-18 22:31:47 +00:00
|
|
|
this.blockchain = options.blockchain;
|
2018-01-13 16:38:10 +00:00
|
|
|
this.gasLimit = false;
|
|
|
|
this.fatalErrors = false;
|
2018-03-11 12:28:03 +00:00
|
|
|
this.deployOnlyOnConfig = false;
|
2018-03-22 19:09:01 +00:00
|
|
|
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
|
2018-05-29 21:23:29 +00:00
|
|
|
|
|
|
|
this.events.setCommandHandler('deploy:contracts', (cb) => {
|
|
|
|
self.deployContracts(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deployAll(done) {
|
|
|
|
let self = this;
|
|
|
|
|
2018-07-19 20:31:28 +00:00
|
|
|
self.events.request('contracts:list', (err, contracts) => {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2018-09-22 13:43:10 +00:00
|
|
|
|
2018-07-19 20:31:28 +00:00
|
|
|
self.logger.info(__("deploying contracts"));
|
|
|
|
self.events.emit("deploy:beforeAll");
|
2018-08-09 16:58:41 +00:00
|
|
|
|
2018-09-22 13:43:10 +00:00
|
|
|
try {
|
2018-09-26 22:44:36 +00:00
|
|
|
async.auto(contractDeploys, 1, function(_err, _results) {
|
2018-09-22 13:43:10 +00:00
|
|
|
if (errors.length) {
|
|
|
|
_err = __("Error deploying contracts. Please fix errors to continue.");
|
|
|
|
self.logger.error(_err);
|
2018-10-01 20:55:45 +00:00
|
|
|
self.events.emit("outputError", __("Error deploying contracts, please check console"));
|
2018-09-22 13:43:10 +00:00
|
|
|
return done(_err);
|
|
|
|
}
|
|
|
|
if (contracts.length === 0) {
|
|
|
|
self.logger.info(__("no contracts found"));
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
self.logger.info(__("finished deploying contracts"));
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
self.logger.error(e.message || e);
|
|
|
|
done(__('Error deploying'));
|
2018-09-12 15:55:29 +00:00
|
|
|
}
|
2018-09-22 14:08:50 +00:00
|
|
|
}
|
2018-07-19 20:31:28 +00:00
|
|
|
self.logger.info(__("finished deploying contracts"));
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
2018-09-22 14:08:50 +00:00
|
|
|
});
|
2017-03-01 01:42:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
deployContracts(done) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__("Blockchain component is disabled in the config").underline);
|
2017-03-31 11:34:43 +00:00
|
|
|
this.events.emit('blockchainDisabled', {});
|
2017-03-30 11:12:39 +00:00
|
|
|
return done();
|
2017-03-11 15:29:45 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function buildContracts(callback) {
|
2018-06-08 11:07:27 +00:00
|
|
|
self.events.request("contracts:build", self.deployOnlyOnConfig, (err) => {
|
|
|
|
callback(err);
|
2018-05-20 13:15:28 +00:00
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
},
|
2018-05-20 14:46:36 +00:00
|
|
|
|
|
|
|
// TODO: shouldn't be necessary
|
2018-07-19 20:31:28 +00:00
|
|
|
function checkCompileOnly(callback){
|
|
|
|
if(self.onlyCompile){
|
2018-05-30 10:56:51 +00:00
|
|
|
self.events.emit('contractsDeployed');
|
2018-03-22 19:09:01 +00:00
|
|
|
return done();
|
2018-08-09 16:58:41 +00:00
|
|
|
}
|
2018-05-20 13:15:28 +00:00
|
|
|
return callback();
|
2018-03-22 19:09:01 +00:00
|
|
|
},
|
2018-05-20 10:46:12 +00:00
|
|
|
|
2018-07-26 17:15:20 +00:00
|
|
|
// TODO: could be implemented as an event (beforeDeployAll)
|
|
|
|
function checkIsConnectedToBlockchain(callback) {
|
|
|
|
self.blockchain.onReady((err) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
2018-05-20 13:08:03 +00:00
|
|
|
|
|
|
|
// TODO: this can be done on the fly or as part of the initialization
|
2018-05-20 13:15:28 +00:00
|
|
|
function determineDefaultAccount(callback) {
|
2018-05-20 13:08:03 +00:00
|
|
|
self.blockchain.determineDefaultAccount((err) => {
|
2018-05-20 13:15:28 +00:00
|
|
|
callback(err);
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
|
|
|
},
|
2018-05-20 10:46:12 +00:00
|
|
|
|
2018-05-20 13:15:28 +00:00
|
|
|
function deployAllContracts(callback) {
|
2018-05-29 21:23:29 +00:00
|
|
|
self.deployAll(function (err) {
|
2018-05-19 02:40:47 +00:00
|
|
|
if (!err) {
|
2018-05-30 10:56:51 +00:00
|
|
|
self.events.emit('contractsDeployed');
|
2018-05-19 02:40:47 +00:00
|
|
|
}
|
|
|
|
if (err && self.fatalErrors) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-05-20 13:15:28 +00:00
|
|
|
callback();
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
2017-12-21 16:21:36 +00:00
|
|
|
},
|
2018-05-20 14:46:36 +00:00
|
|
|
function runAfterDeploy(callback) {
|
2018-05-30 12:00:31 +00:00
|
|
|
self.plugins.emitAndRunActionsForEvent('contracts:deploy:afterAll', callback);
|
2017-02-24 13:20:03 +00:00
|
|
|
}
|
2018-05-20 13:15:28 +00:00
|
|
|
], function (err, _result) {
|
|
|
|
done(err);
|
2017-02-24 13:20:03 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
}
|
2017-02-24 13:20:03 +00:00
|
|
|
|
|
|
|
module.exports = DeployManager;
|