read storage and communication config files; use & initialize storage depending on config

This commit is contained in:
Iuri Matias 2017-02-10 07:44:06 -05:00
parent 629b9aa2a6
commit 6984c13ac6
11 changed files with 138 additions and 12 deletions

View File

@ -0,0 +1,5 @@
{
"default": {
"provider": "whisper"
}
}

View File

@ -0,0 +1,15 @@
{
"default": {
"enabled": true,
"ipfs_bin": "ipfs",
"provider": "ipfs",
"host": "localhost",
"port": 5001
},
"development": {
"enabled": true,
"provider": "ipfs",
"host": "localhost",
"port": 5001
}
}

View File

@ -0,0 +1,5 @@
{
"default": {
"provider": "whisper"
}
}

15
demo/config/storage.json Normal file
View File

@ -0,0 +1,15 @@
{
"default": {
"enabled": true,
"ipfs_bin": "ipfs",
"provider": "ipfs",
"host": "localhost",
"port": 5001
},
"development": {
"enabled": true,
"provider": "ipfs",
"host": "localhost",
"port": 5001
}
}

View File

@ -2,6 +2,7 @@ var Plugins = require('./plugins.js');
var ABIGenerator = function(options) {
this.blockchainConfig = options.blockchainConfig || {};
this.storageConfig = options.storageConfig || {};
this.contractsManager = options.contractsManager;
this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost;
this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort;
@ -60,11 +61,26 @@ ABIGenerator.prototype.generateContracts = function(useEmbarkJS) {
return result;
};
ABIGenerator.prototype.generateStorageInitialiation = function(useEmbarkJS) {
var self = this;
var result = "\n";
if (!useEmbarkJS || self.storageConfig === {}) return;
if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) {
result += "\n" + "EmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "'});";
}
return result;
};
ABIGenerator.prototype.generateABI = function(options) {
var result = "";
result += this.generateProvider();
result += this.generateContracts(options.useEmbarkJS);
result += this.generateStorageInitialiation(options.useEmbarkJS);
return result;
};

View File

@ -119,12 +119,12 @@ Cmd.prototype.test = function() {
Cmd.prototype.upload = function() {
var self = this;
program
.command('upload [platform]')
.command('upload [platform] [environment]')
.description('upload your dapp to a decentralized storage. possible options: ipfs, swarm (e.g embark upload swarm)')
.action(function(platform ,options) {
.action(function(platform, env, options) {
// TODO: get env in cmd line as well
self.Embark.initConfig('development', {
embarkConfig: 'embark.json'
self.Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json', interceptLogs: false
});
self.Embark.upload(platform);
});

View File

@ -33,6 +33,8 @@ Config.prototype.loadConfigFiles = function(options) {
this.loadPipelineConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadContractsConfigFile();
this.loadChainTrackerFile();
@ -42,6 +44,8 @@ Config.prototype.loadConfigFiles = function(options) {
Config.prototype.reloadConfig = function() {
this.loadPipelineConfigFile();
this.loadBlockchainConfigFile();
this.loadStorageConfigFile();
this.loadCommunicationConfigFile();
this.loadContractsConfigFile();
this.loadChainTrackerFile();
};
@ -73,6 +77,49 @@ Config.prototype.loadContractsConfigFile = function() {
this.contractsConfig = mergedConfig;
};
Config.prototype.loadStorageConfigFile = function() {
var configObject = {};
//var configPlugins = this.plugins.getPluginsFor('storageConfig');
//if (configPlugins.length > 0) {
// configPlugins.forEach(function(plugin) {
// plugin.contractsConfigs.forEach(function(pluginConfig) {
// configObject = merge.recursive(configObject, pluginConfig);
// });
// });
//}
var storageConfig = JSON.parse(fs.readFileSync(this.configDir + "storage.json"));
configObject = merge.recursive(configObject, storageConfig);
var defaultStorageConfig = configObject['default'];
var envStorageConfig = configObject[this.env];
var mergedConfig = merge.recursive(defaultStorageConfig, envStorageConfig);
this.storageConfig = mergedConfig;
};
Config.prototype.loadCommunicationConfigFile = function() {
var configObject = {};
//var configPlugins = this.plugins.getPluginsFor('communicationConfig');
//if (configPlugins.length > 0) {
// configPlugins.forEach(function(plugin) {
// plugin.contractsConfigs.forEach(function(pluginConfig) {
// configObject = merge.recursive(configObject, pluginConfig);
// });
// });
//}
var communicationConfig = JSON.parse(fs.readFileSync(this.configDir + "communication.json"));
configObject = merge.recursive(configObject, communicationConfig);
var defaultCommunicationConfig = configObject['default'];
var envCommunicationConfig = configObject[this.env];
var mergedConfig = merge.recursive(defaultCommunicationConfig, envCommunicationConfig);
this.communicationConfig = mergedConfig;
};
Config.prototype.loadPipelineConfigFile = function() {
var contracts = this.embarkConfig.contracts;
this.contractsFiles = this.loadFiles(contracts);

View File

@ -259,7 +259,7 @@ var Embark = {
});
},
function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins});
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig});
callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
}
], function(err, result) {
@ -285,13 +285,13 @@ var Embark = {
});
},
function generateConsoleABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager});
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, storageConfig: self.config.storageConfig});
var consoleABI = abiGenerator.generateABI({useEmbarkJS: false});
Embark.console.runCode(consoleABI);
callback(null, contractsManager);
},
function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins});
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig});
callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
},
function buildPipeline(abi, callback) {
@ -325,10 +325,10 @@ var Embark = {
// TODO: should deploy if it hasn't already
upload: function(platform) {
if (platform === 'ipfs') {
var ipfs = new IPFS({buildDir: 'dist/'});
var ipfs = new IPFS({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
ipfs.deploy();
} else if (platform === 'swarm') {
var swarm = new Swarm({buildDir: 'dist/'});
var swarm = new Swarm({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
swarm.deploy();
} else {
console.log(("unknown platform: " + platform).red);

View File

@ -4,16 +4,19 @@ var async = require('async');
var IPFS = function(options) {
this.options = options;
this.buildDir = options.buildDir || 'dist/';
this.plugins = options.plugins;
this.storageConfig = options.storageConfig;
this.configIpfsBin = this.storageConfig.ipfs_bin || "ipfs";
};
IPFS.prototype.deploy = function() {
var self = this;
async.waterfall([
function findBinary(callback) {
var ipfs_bin = exec('which ipfs').output.split("\n")[0];
var ipfs_bin = exec('which ' + self.configIpfsBin).output.split("\n")[0];
if (ipfs_bin==='ipfs not found'){
console.log('=== WARNING: IPFS not in an executable path. Guessing ~/go/bin/ipfs for path'.yellow);
if (ipfs_bin === 'ipfs not found' || ipfs_bin === ''){
console.log(('=== WARNING: ' + self.configIpfsBin + ' not found or not in the path. Guessing ~/go/bin/ipfs for path').yellow);
ipfs_bin = "~/go/bin/ipfs";
}

View File

@ -0,0 +1,5 @@
{
"default": {
"provider": "whisper"
}
}

View File

@ -0,0 +1,15 @@
{
"default": {
"enabled": true,
"ipfs_bin": "ipfs",
"provider": "ipfs",
"host": "localhost",
"port": 5001
},
"development": {
"enabled": true,
"provider": "ipfs",
"host": "localhost",
"port": 5001
}
}