From 201a31b54eab8b5ad3bc2eedf0deebea6668c5ef Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 24 May 2015 08:47:06 -0400 Subject: [PATCH] add blockchain task --- tasks/blockchain.coffee | 71 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tasks/blockchain.coffee diff --git a/tasks/blockchain.coffee b/tasks/blockchain.coffee new file mode 100644 index 00000000..8c3f76ea --- /dev/null +++ b/tasks/blockchain.coffee @@ -0,0 +1,71 @@ +module.exports = (grunt) -> + readYaml = require('read-yaml') + require('shelljs/global') + sh = require('execSync') + + grunt.registerTask "blockchain", "deploy ethereum node", (env_) => + env = env_ || "development" + try + blockchainConfig = readYaml.sync("config/blockchain.yml") + catch exception + grunt.log.writeln("==== error reading config/blockchain.yml") + grunt.log.writeln(exception) + + rpcHost = blockchainConfig[env].rpc_host + rpcPort = blockchainConfig[env].rpc_port + rpcWhitelist = blockchainConfig[env].rpc_whitelist + + minerthreads = blockchainConfig[env].minerthreads + datadir = blockchainConfig[env].datadir + networkId = blockchainConfig[env].network_id || Math.floor(((Math.random()*100000)+1000)) + port = blockchainConfig[env].port || "30303" + console = blockchainConfig[env].console || false + mine_when_needed = blockchainConfig[env].mine_when_needed || false + + account = blockchainConfig[env].account + address = account.address + + cmd = "geth " + unless datadir is "default" + cmd += "--datadir=\"#{datadir}\" " + cmd += "--logfile=\"#{datadir}.log\" " + cmd += "--port #{port} " + cmd += "--rpc " + cmd += "--rpcport #{rpcPort} " + cmd += "--networkid #{networkId} " + cmd += "--rpccorsdomain \"#{rpcWhitelist}\" " + unless minerthreads is undefined + cmd += "--minerthreads \"#{minerthreads}\" " + cmd += "--mine " + + if account.password isnt undefined + cmd += "--password #{account.password} " + + if account.init + grunt.log.writeln("=== initializating account") + + result = exec(cmd + "account list") + grunt.log.writeln("running: #{cmd} account list") + grunt.log.writeln("=== output is #{result.output}") + if result.output.indexOf("Fatal") < 0 + grunt.log.writeln("=== already initialized") + address = result.output.match(/{(\w+)}/)[1] + else + grunt.log.writeln("running: #{cmd} account new") + output = sh.exec(cmd + " account new") + address = output.stdout.match(/{(\w+)}/)[1] + + if address isnt undefined + cmd += "--unlock #{address} " + + if console + cmd += "console" + + if mine_when_needed + cmd += "js node_modules/embark-framework/js/mine.js" + + grunt.log.writeln("running: #{cmd}") + grunt.log.writeln("=== running geth") + #sh.run(cmd) + exec(cmd) +