Merge pull request #372 from embark-framework/bug_fix/upload-to-swarm

swarm deploy refactored to use web3.bzz instead of command line
This commit is contained in:
Iuri Matias 2018-04-26 05:58:41 -04:00 committed by GitHub
commit acf1fa427b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 62 deletions

View File

@ -181,11 +181,7 @@ class Cmd {
.option('--loglevel [loglevel]', 'level of logging to display ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.description('Upload your dapp to a decentralized storage (e.g embark upload ipfs).')
.action(function (platform, env, _options) {
let environment = env || 'development';
embark.initConfig(environment, {
embarkConfig: 'embark.json', interceptLogs: false
});
_options.env = environment;
_options.env = env || 'development';
_options.logFile = _options.logfile; // fix casing
_options.logLevel = _options.loglevel; // fix casing
embark.upload(platform, _options);

View File

@ -68,7 +68,8 @@ class Engine {
"webServer": this.webServerService,
"ipfs": this.ipfsService,
"web3": this.web3Service,
"libraryManager": this.libraryManagerService
"libraryManager": this.libraryManagerService,
"swarm": this.swarmService
};
let service = services[serviceName];
@ -195,6 +196,14 @@ class Engine {
});
}
swarmService(_options) {
this.registerModule('swarm', {
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
storageConfig: this.config.storageConfig,
web3: _options.web3
});
}
web3Service(options) {
let self = this;
this.web3 = options.web3;

View File

@ -157,8 +157,9 @@ class Embark {
});
}
build(options, continueProcessing) {
build(options) {
this.context = options.context || [constants.contexts.build];
let engine = new Engine({
env: options.env,
version: this.version,
@ -202,9 +203,7 @@ class Embark {
engine.logger.info("finished building".underline);
}
// needed due to child processes
if(err || !continueProcessing){
process.exit();
}
process.exit();
});
}
@ -271,28 +270,45 @@ class Embark {
}
upload(platform, options) {
this.context = options.context || [constants.contexts.upload, constants.contexts.build];
// populate options that were instantiated with initConfig to pass around
options.buildDir = 'dist/';
options.storageConfig = this.config.storageConfig;
options.events = this.events;
options.logger = this.logger;
options.config = this.config;
this.context = options.context || [constants.contexts.upload, constants.contexts.build];
// load plugins
this.plugins.loadInternalPlugin('ipfs', options);
this.plugins.loadInternalPlugin('swarm', options);
// 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([
function startServices(callback) {
engine.startService("libraryManager");
engine.startService("web3");
engine.startService("pipeline");
engine.startService("codeGenerator");
engine.startService("deployment");
engine.startService("ipfs");
engine.startService("swarm", {buildDir:'dist/',web3: engine.web3});
callback();
},
function setupStoragePlugin(callback){
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}
// check use has input existing storage plugin
let cmdPlugins = self.plugins.getPluginsFor('uploadCmds');
let cmdPlugins = engine.plugins.getPluginsFor('uploadCmds');
if (cmdPlugins.length > 0) {
cmdPlugin = cmdPlugins.find((pluginCmd) => {
@ -300,15 +316,15 @@ class Embark {
});
}
if (!cmdPlugin) {
self.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green);
callback({message: 'unknown platform: ' + platform});
} else {
callback();
}
},
function buildAndDeployContracts(callback){
function deploy(callback) {
// 2. upload to storage (outputDone event triggered after webpack finished)
self.events.on('outputDone', function () {
engine.events.on('outputDone', function () {
cmdPlugin.uploadCmds[0].cb()
.then((success) => {
callback(null, success);
@ -316,14 +332,19 @@ class Embark {
.catch(callback);
});
// 1. build the contracts and dapp webpack
self.build(options, true);
engine.deployManager.deployContracts(function (err) {
engine.logger.info("finished deploying".underline);
if(err){
callback(err);
}
});
}
], function (err, _result) {
if (err) {
self.logger.error(err.message);
self.logger.debug(err.stack);
engine.logger.error(err.message);
engine.logger.debug(err.stack);
} else {
self.logger.info("finished building dapp and deploying to " + platform.underline);
engine.logger.info(`finished building DApp and deploying to ${platform}`.underline);
}
// needed due to child processes

View File

@ -7,7 +7,8 @@ class Swarm {
this.upload_swarm = new UploadSwarm({
buildDir: options.buildDir || 'dist/',
storageConfig: options.storageConfig
storageConfig: options.storageConfig,
web3: options.web3
});
embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm));

View File

@ -1,50 +1,38 @@
require('colors');
let async = require('async');
let shelljs = require('shelljs');
class Swarm {
constructor(options) {
this.options = options;
this.buildDir = options.buildDir || 'dist/';
this.web3 = options.web3;
this.storageConfig = options.storageConfig;
}
deploy() {
return new Promise((resolve, reject) => {
console.log("deploying to swarm!");
let self = this;
let web3 = this.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 setProvider(callback){
web3.bzz.setProvider(`http://${self.storageConfig.host}:${self.storageConfig.port}`);
callback();
},
function runCommand(swarm_bin, callback) {
let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`;
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.storageConfig.getUrl}${dir_hash}/`).green);
callback();
}