2017-12-27 00:55:42 +00:00
|
|
|
let UploadSwarm = require('./upload.js');
|
2018-04-30 05:56:43 +00:00
|
|
|
let utils = require('../../utils/utils.js');
|
|
|
|
let fs = require('../../core/fs.js');
|
2017-12-27 00:55:42 +00:00
|
|
|
|
|
|
|
class Swarm {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.events = embark.events;
|
|
|
|
this.buildDir = options.buildDir;
|
2018-05-25 07:13:57 +00:00
|
|
|
this.storageConfig = options.storageConfig;
|
|
|
|
this.host = options.host || options.storageConfig.upload.host;
|
|
|
|
this.port = options.port || options.storageConfig.upload.port;
|
|
|
|
this.protocol = options.protocol || options.storageConfig.upload.protocol;
|
2018-04-30 05:56:43 +00:00
|
|
|
this.addCheck = options.addCheck;
|
|
|
|
this.embark = embark;
|
|
|
|
this.bzz = options.bzz;
|
2017-12-27 00:55:42 +00:00
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
this.initSwarmProvider();
|
|
|
|
this.commandlineDeploy();
|
|
|
|
this.setServiceCheck();
|
|
|
|
this.addSwarmToEmbarkJS();
|
|
|
|
this.addSetProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
initSwarmProvider(){
|
|
|
|
if(!this.bzz.currentProvider) {
|
2018-05-25 07:13:57 +00:00
|
|
|
this.bzz.setProvider(`${this.protocol}://${this.host}:${this.port}`);
|
2018-04-30 05:56:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commandlineDeploy() {
|
2017-12-27 00:55:42 +00:00
|
|
|
this.upload_swarm = new UploadSwarm({
|
2018-04-30 05:56:43 +00:00
|
|
|
buildDir: this.buildDir || 'dist/',
|
|
|
|
storageConfig: this.storageConfig,
|
|
|
|
bzz: this.bzz
|
2017-12-27 00:55:42 +00:00
|
|
|
});
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
this.embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm));
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
|
|
|
|
setServiceCheck() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
let storageConfig = this.storageConfig;
|
|
|
|
|
|
|
|
if (!storageConfig.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-25 07:13:57 +00:00
|
|
|
if (storageConfig.upload.provider !== 'swarm' || storageConfig.available_providers.indexOf("swarm") < 0) {
|
2018-04-30 05:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.events.on('check:backOnline:Swarm', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('Swarm node detected...'));
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.events.on('check:wentOffline:Swarm', function () {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__('Swarm node is offline...'));
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.addCheck) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add check for console
|
|
|
|
this.addCheck('Swarm', function(cb){
|
|
|
|
self.logger.trace("Checking Swarm availability...");
|
|
|
|
self.bzz.isAvailable().then(result => {
|
|
|
|
return cb({name: "Swarm ", status: result ? 'on':'off'});
|
|
|
|
}).catch(err => {
|
|
|
|
self.logger.trace("Check Swarm availability error: " + err);
|
|
|
|
return cb({name: "Swarm ", status: 'off'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addSwarmToEmbarkJS() {
|
2018-05-25 07:13:57 +00:00
|
|
|
let self = this;
|
2018-04-30 05:56:43 +00:00
|
|
|
// TODO: make this a shouldAdd condition
|
|
|
|
if (this.storageConfig === {}) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
if (this.storageConfig.available_providers.indexOf('swarm') < 0 || this.storageConfig.enabled !== true) {
|
2018-04-30 05:56:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:13:57 +00:00
|
|
|
this.events.request("version:get:p-iteration", function(pIterationVersion) {
|
|
|
|
let currentPIterationVersion = require('../../../package.json').dependencies["p-iteration"];
|
|
|
|
if (pIterationVersion !== currentPIterationVersion) {
|
|
|
|
self.events.request("version:getPackageLocation", "p-iteration", pIterationVersion, function(err, location) {
|
|
|
|
self.embark.registerImportFile("p-iteration", fs.dappPath(location));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-30 05:56:43 +00:00
|
|
|
let code = "";
|
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
|
|
|
code += "\nEmbarkJS.Storage.registerProvider('swarm', __embarkSwarm);";
|
|
|
|
|
|
|
|
this.embark.addCodeToEmbarkJS(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
addSetProvider() {
|
2018-05-25 07:13:57 +00:00
|
|
|
let code = "\nEmbarkJS.Storage.setProviders('swarm'," + JSON.stringify(this.storageConfig.dappConnection) + ");";
|
2018-04-30 05:56:43 +00:00
|
|
|
|
|
|
|
let shouldInit = (storageConfig) => {
|
2018-05-25 07:13:57 +00:00
|
|
|
return (this.storageConfig.dappConnection !== undefined && this.storageConfig.dappConnection.some((dappConn) => dappConn.provider === 'swarm') && storageConfig.enabled === true);
|
2018-04-30 05:56:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.embark.addProviderInit('storage', code, shouldInit);
|
|
|
|
}
|
2017-12-27 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Swarm;
|
|
|
|
|