From c05915b0e9d57566bfadaf48cf30c15d2a0d534c Mon Sep 17 00:00:00 2001 From: emizzle Date: Fri, 20 Apr 2018 17:39:45 +1000 Subject: [PATCH] swarm deploy refactored to use web3.bzz instead of command line * `Embark.upload()` refactored to build own `Engine` and services so `web3` could be passed to `Swarm` module * `Swarm.deploy()` modified to use `web3.bzz.upload()` * needs detection of running swarm node --- lib/index.js | 45 +++++++++++++++++++++++++++----- lib/modules/swarm/upload.js | 51 +++++++++++++++---------------------- 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/lib/index.js b/lib/index.js index c4a03b26..f292c12c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -151,7 +151,7 @@ class Embark { }); } - build(options, continueProcessing) { + build(options) { let engine = new Engine({ env: options.env, version: this.version, @@ -194,9 +194,7 @@ class Embark { engine.logger.info("finished building".underline); } // needed due to child processes - if(err || !continueProcessing){ - process.exit(); - } + process.exit(); }); } @@ -273,6 +271,20 @@ class Embark { // upddate our options with loaded plugins options.plugins = this.plugins; + let engine = new Engine({ + env: options.env, + version: this.version, + embarkConfig: 'embark.json', + interceptLogs: false, + logFile: options.logFile, + logLevel: options.logLevel, + events: options.events, + logger: options.logger, + config: options.config, + plugins: options.plugins + }); + engine.init(); + let cmdPlugin; let self = this; async.waterfall([ @@ -292,17 +304,36 @@ class Embark { callback(); } }, - function buildAndDeployContracts(callback){ + function startServices(callback) { + let pluginList = engine.plugins.listPlugins(); + if (pluginList.length > 0) { + engine.logger.info("loaded plugins: " + pluginList.join(", ")); + } + + engine.startService("libraryManager"); + engine.startService("web3"); + engine.startService("pipeline"); + engine.startService("codeGenerator"); + engine.startService("deployment"); + engine.startService("ipfs"); + callback(); + }, + function deploy(callback) { // 2. upload to storage (outputDone event triggered after webpack finished) self.events.on('outputDone', function () { - cmdPlugin.uploadCmds[0].cb() + cmdPlugin.uploadCmds[0].cb({web3: engine.web3}) .then((success) => { callback(null, success); }) .catch(callback); }); // 1. build the contracts and dapp webpack - self.build(options, true); + engine.deployManager.deployContracts(function (err) { + engine.logger.info("finished building".underline); + if(err){ + callback(err); + } + }); } ], function (err, _result) { if (err) { diff --git a/lib/modules/swarm/upload.js b/lib/modules/swarm/upload.js index 938d9a77..4369e8b6 100644 --- a/lib/modules/swarm/upload.js +++ b/lib/modules/swarm/upload.js @@ -1,6 +1,5 @@ require('colors'); let async = require('async'); -let shelljs = require('shelljs'); class Swarm { constructor(options) { @@ -8,43 +7,35 @@ class Swarm { this.buildDir = options.buildDir || 'dist/'; } - deploy() { + deploy(deployOptions) { return new Promise((resolve, reject) => { console.log("deploying to swarm!"); let self = this; + let web3 = (deployOptions || {}).web3; async.waterfall([ - function findBinary(callback) { - let swarm_bin = shelljs.which('swarm'); - - if (swarm_bin === 'swarm not found' || !swarm_bin) { - console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow); - swarm_bin = "~/go/bin/swarm"; - } - - callback(null, swarm_bin); + function findWeb3(callback){ + if(!web3){ + callback('web3 must be passed in to the swarm deploy() method'); + }else callback(); }, - function runCommand(swarm_bin, callback) { - let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`; + function setProvider(callback){ + web3.bzz.setProvider(`http://${self.options.storageConfig.host}:${self.options.storageConfig.port}`); + callback(); + }, + function runCommand(callback) { console.log(("=== adding " + self.buildDir + " to swarm").green); - console.trace(cmd); - shelljs.exec(cmd, {silent:true}, function(code, stdout, stderr){ // {silent:true}: don't echo cmd output so it can be controlled via logLevel - console.log(stdout.green); - callback(stderr, {code: code, output: stdout}); - }); - }, - function getHashFromOutput(result, callback) { - if (result.code !== 0) { - callback("couldn't upload, is the swarm daemon running?"); - } - else{ - let rows = result.output.split("\n"); - let dir_hash = rows.reverse()[1]; - - callback(null, dir_hash); - } + web3.bzz.upload({ + path: self.buildDir, // path to data / file / directory + kind: "directory", // could also be "file" or "data" + defaultFile: "index.html" // optional, and only for kind === "directory" + }) + .then((success) => { + callback(null, success); + }) + .catch(callback); }, function printUrls(dir_hash, callback) { - console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); + console.log((`=== DApp available at ${self.options.storageConfig.getUrl}${dir_hash}/`).green); callback(); }