From 50f10723725679ffedd50ea3037de4522cf83ac6 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 26 Dec 2017 19:55:42 -0500 Subject: [PATCH 1/2] move upload to its own module --- lib/cmd.js | 2 +- lib/core/plugin.js | 6 +++++ lib/index.js | 27 ++++++++++++------- lib/modules/ipfs/index.js | 19 +++++++++++++ .../ipfs.js => modules/ipfs/upload.js} | 2 +- lib/modules/swarm/index.js | 19 +++++++++++++ .../swarm.js => modules/swarm/upload.js} | 0 7 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 lib/modules/ipfs/index.js rename lib/{upload/ipfs.js => modules/ipfs/upload.js} (98%) create mode 100644 lib/modules/swarm/index.js rename lib/{upload/swarm.js => modules/swarm/upload.js} (100%) diff --git a/lib/cmd.js b/lib/cmd.js index 5a9f804e2..2f3b6d8cf 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -148,7 +148,7 @@ class Cmd { upload() { program .command('upload [platform] [environment]') - .description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)') + .description('upload your dapp to a decentralized storage (e.g embark upload ipfs)') .action(function (platform, env, _options) { // TODO: get env in cmd line as well embark.initConfig(env || 'development', { diff --git a/lib/core/plugin.js b/lib/core/plugin.js index b53e57085..94e9a5099 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -20,6 +20,7 @@ var Plugin = function(options) { this.compilers = []; this.serviceChecks = []; this.pluginTypes = []; + this.uploadCmds = []; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -136,6 +137,11 @@ Plugin.prototype.registerCompiler = function(extension, cb) { this.pluginTypes.push('compilers'); }; +Plugin.prototype.registerUploadCommand = function(cmd, cb) { + this.uploadCmds.push({cmd: cmd, cb: cb}); + this.pluginTypes.push('uploadCmds'); +}; + Plugin.prototype.runCommands = function(cmd, options) { return this.console.map(function(cb) { return cb.call(this, cmd, options); diff --git a/lib/index.js b/lib/index.js index 944da8ca8..9e922ff98 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,9 +6,6 @@ require('colors'); let Engine = require('./core/engine.js'); -let IPFS = require('./upload/ipfs.js'); -let Swarm = require('./upload/swarm.js'); - let version = require('../package.json').version; class Embark { @@ -191,12 +188,24 @@ class Embark { // TODO: should deploy if it hasn't already upload(platform) { - if (platform === 'ipfs') { - let ipfs = new IPFS({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig}); - ipfs.deploy(); - } else if (platform === 'swarm') { - let swarm = new Swarm({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig}); - swarm.deploy(); + let options = { + buildDir: 'dist/', + storageConfig: this.config.storageConfig + }; + + this.plugins.loadInternalPlugin('ipfs', options); + this.plugins.loadInternalPlugin('swarm', options); + + let cmdPlugins = this.plugins.getPluginsFor('uploadCmds'); + let cmdPlugin; + if (cmdPlugins.length > 0) { + cmdPlugin = cmdPlugins.find((pluginCmd) => { + return pluginCmd.name == platform; + }); + } + + if (cmdPlugin) { + cmdPlugin.uploadCmds[0].cb(); } else { console.log(("unknown platform: " + platform).red); console.log('try "embark upload ipfs" or "embark upload swarm"'.green); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js new file mode 100644 index 000000000..58616301f --- /dev/null +++ b/lib/modules/ipfs/index.js @@ -0,0 +1,19 @@ +let UploadIPFS = require('./upload.js'); + +class IPFS { + + constructor(embark, options) { + this.logger = embark.logger; + + this.upload_ipfs = new UploadIPFS({ + buildDir: options.buildDir || 'dist/', + storageConfig: options.storageConfig, + configIpfsBin: options.storageConfig.ipfs_bin || "ipfs" + }); + + embark.registerUploadCommand('ipfs', this.upload_ipfs.deploy.bind(this.upload_ipfs)); + } + +} + +module.exports = IPFS; diff --git a/lib/upload/ipfs.js b/lib/modules/ipfs/upload.js similarity index 98% rename from lib/upload/ipfs.js rename to lib/modules/ipfs/upload.js index 8bf42c98a..ea9432f30 100644 --- a/lib/upload/ipfs.js +++ b/lib/modules/ipfs/upload.js @@ -7,12 +7,12 @@ class IPFS { constructor(options) { this.options = options; this.buildDir = options.buildDir || 'dist/'; - this.plugins = options.plugins; this.storageConfig = options.storageConfig; this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs"; } deploy() { + console.log("deploying!"); let self = this; async.waterfall([ function findBinary(callback) { diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js new file mode 100644 index 000000000..452f08bd8 --- /dev/null +++ b/lib/modules/swarm/index.js @@ -0,0 +1,19 @@ +let UploadSwarm = require('./upload.js'); + +class Swarm { + + constructor(embark, options) { + this.logger = embark.logger; + + this.upload_swarm = new UploadSwarm({ + buildDir: options.buildDir || 'dist/', + storageConfig: options.storageConfig + }); + + embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm)); + } + +} + +module.exports = Swarm; + diff --git a/lib/upload/swarm.js b/lib/modules/swarm/upload.js similarity index 100% rename from lib/upload/swarm.js rename to lib/modules/swarm/upload.js From eaf9016c79440b5d2820516fadc4f23c3347afac Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 26 Dec 2017 20:32:51 -0500 Subject: [PATCH 2/2] move ipfs service check to its module --- lib/core/engine.js | 52 +++----------------------- lib/modules/ipfs/index.js | 78 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 52 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index e69f6aefc..83f12d050 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,5 +1,4 @@ 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'); @@ -169,52 +168,11 @@ class Engine { } ipfsService(_options) { - let self = this; - - let storageConfig = this.config.storageConfig; - if (!storageConfig.enabled) { - return; - } - if (storageConfig.provider !== 'ipfs' && storageConfig.available_providers.indexOf("ipfs") < 0) { - return; - } - - let host = _options.host || storageConfig.host; - let port = _options.port || storageConfig.port; - - self.events.on('check:backOnline:IPFS', function () { - self.logger.info('IPFS node detected..'); - }); - - self.servicesMonitor.addCheck('IPFS', function (cb) { - utils.checkIsAvailable('http://' + host + ':' + port, 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..."); - utils.httpGet('http://' + host + ':' + port + '/api/v0/version', function (err, body) { - if (err) { - self.logger.trace("Check IPFS version error: " + err); - return cb({name: "IPFS ", status: 'off'}); - } - try { - let parsed = JSON.parse(body); - if (parsed.Version) { - return cb({name: ("IPFS " + parsed.Version), status: 'on'}); - } - else { - return cb({name: "IPFS ", status: 'on'}); - } - } - catch (e) { - return cb({name: "IPFS ", status: 'off'}); - } - }); - } - else { - return cb({name: "IPFS ", status: 'off'}); - } - }); + this.registerModule('ipfs', { + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + storageConfig: this.config.storageConfig, + host: _options.host, + port: _options.port }); } diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 58616301f..76bf90ea4 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -1,17 +1,85 @@ let UploadIPFS = require('./upload.js'); +let utils = require('../../utils/utils.js'); class IPFS { constructor(embark, options) { this.logger = embark.logger; + this.events = embark.events; + this.buildDir = options.buildDir; + this.storageConfig = options.storageConfig; + this.host = options.host || this.storageConfig.host; + this.port = options.port || this.storageConfig.port; + this.addCheck = options.addCheck; + this.embark = embark; - this.upload_ipfs = new UploadIPFS({ - buildDir: options.buildDir || 'dist/', - storageConfig: options.storageConfig, - configIpfsBin: options.storageConfig.ipfs_bin || "ipfs" + this.commandlineDeploy(); + this.setServiceCheck(); + } + + commandlineDeploy() { + let upload_ipfs = new UploadIPFS({ + buildDir: this.buildDir || 'dist/', + storageConfig: this.storageConfig, + configIpfsBin: this.storageConfig.ipfs_bin || "ipfs" }); - embark.registerUploadCommand('ipfs', this.upload_ipfs.deploy.bind(this.upload_ipfs)); + this.embark.registerUploadCommand('ipfs', upload_ipfs.deploy.bind(upload_ipfs)); + } + + setServiceCheck() { + let self = this; + + let storageConfig = this.storageConfig; + + if (!storageConfig.enabled) { + return; + } + if (storageConfig.provider !== 'ipfs' && storageConfig.available_providers.indexOf("ipfs") < 0) { + return; + } + + self.events.on('check:backOnline:IPFS', function () { + self.logger.info('IPFS node detected..'); + }); + + self.events.on('check:wentOffline:IPFS', function () { + self.logger.info('IPFS node is offline..'); + }); + + let server = 'http://' + this.host + ':' + this.port; + self.logger.info(server); + + this.addCheck('IPFS', function (cb) { + utils.checkIsAvailable(server, 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..."); + utils.httpGet(server + '/api/v0/version', function (err, body) { + if (err) { + self.logger.trace("Check IPFS version error: " + err); + return cb({name: "IPFS ", status: 'off'}); + } + try { + let parsed = JSON.parse(body); + if (parsed.Version) { + return cb({name: ("IPFS " + parsed.Version), status: 'on'}); + } + else { + return cb({name: "IPFS ", status: 'on'}); + } + } + catch (e) { + return cb({name: "IPFS ", status: 'off'}); + } + }); + } + else { + return cb({name: "IPFS ", status: 'off'}); + } + }); + }); } }