Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Windows + Other Path Related Bugs #4

Merged
merged 21 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions bin/storjshare-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ const config = require('../lib/config/daemon');
const storjshare_create = require('commander');

storjshare_create
.description('generates a new share configuration')
.option('-a, --sjcx <addr>', 'specify the sjcx address (required)')
.option('-s, --storage <path>', 'specify the storage path')
.option('-l, --logfile <path>', 'specify the logfile path')
.option('-k, --privkey <privkey>', 'specify the private key')
.option('-o, --outfile <writepath>', 'write config to path')
.option('-n, --noedit', 'do not open generated config in editor')
.parse(process.argv);

if (!storjshare_create.sjcx) {
Expand Down Expand Up @@ -45,10 +47,7 @@ if (!storjshare_create.logfile) {
);
}

let isWritingToTemp = false;

if (!storjshare_create.outfile) {
isWritingToTemp = true;
storjshare_create.outfile = path.join(
tmpdir(),
storj.KeyPair(storjshare_create.privkey).getNodeID() + '.json'
Expand All @@ -59,6 +58,7 @@ let exampleConfigPath = path.join(__dirname, '../example/farmer.config.json');
let exampleConfigString = fs.readFileSync(exampleConfigPath).toString();

function replaceEmptyConfig(prop, value) {
value = value.split('\\').join('\\\\'); // NB: Hack windows paths into JSON
exampleConfigString = exampleConfigString.replace(
`"${prop}": ""`,
`"${prop}": "${value}"`
Expand All @@ -67,12 +67,13 @@ function replaceEmptyConfig(prop, value) {

replaceEmptyConfig('paymentAddress', storjshare_create.sjcx);
replaceEmptyConfig('networkPrivateKey', storjshare_create.privkey);
replaceEmptyConfig('loggerOutputFile', storjshare_create.logfile);
replaceEmptyConfig('storagePath', storjshare_create.storage);
replaceEmptyConfig('storagePath', path.normalize(storjshare_create.storage));
replaceEmptyConfig('loggerOutputFile',
path.normalize(storjshare_create.logfile));

let outfile = isWritingToTemp ?
storjshare_create.outfile :
path.join(process.cwd(), storjshare_create.outfile);
let outfile = path.isAbsolute(storjshare_create.outfile) ?
path.normalize(storjshare_create.outfile) :
path.join(process.cwd(), storjshare_create.outfile);

try {
fs.writeFileSync(outfile, exampleConfigString);
Expand All @@ -82,8 +83,14 @@ try {
}

console.log(`\n * configuration written to ${outfile}`);
console.log(' * opening in your favorite editor to tweak before running');
editor(outfile, () => {
console.log(' ...');
console.log(` * use new config: storjshare start --config ${outfile}`);
});

if (!storjshare_create.noedit) {
console.log(' * opening in your favorite editor to tweak before running');
editor(outfile, {
// NB: Not all distros ship with vim, so let's use GNU Nano
editor: process.platform === 'win32' ? null : 'nano'
}, () => {
console.log(' ...');
console.log(` * use new config: storjshare start --config ${outfile}`);
});
}
4 changes: 3 additions & 1 deletion bin/storjshare-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ if (!storjshare_daemon.foreground) {
api.logger.pipe(process.stdout);
}

dnode(api.methods).listen(config.daemonRpcPort, config.daemonRpcAddress);
dnode(api.methods)
.on('error', (err) => api.logger.warn(err.message))
.listen(config.daemonRpcPort, config.daemonRpcAddress);
4 changes: 2 additions & 2 deletions bin/storjshare-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ sock.on('remote', function(rpc) {
rpc.destroy(storjshare_destroy.nodeid, (err) => {
if (err) {
console.error(`\n cannot destroy node, reason: ${err.message}`);
process.exit(1);
return sock.end();
}
console.info(`\n * share ${storjshare_destroy.nodeid} destroyed`);
process.exit(0);
sock.end();
});
});
2 changes: 1 addition & 1 deletion bin/storjshare-killall.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ sock.on('end', function() {
});

sock.on('remote', function(rpc) {
rpc.killall();
rpc.killall(() => sock.end());
});
9 changes: 7 additions & 2 deletions bin/storjshare-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ if (!storjshare_logs.nodeid) {

const sock = dnode.connect(config.daemonRpcPort);

process.on('exit', () => {
sock.end();
process.exit(0);
});

sock.on('remote', function(rpc) {
rpc.status((err, shares) => {
if (err) {
console.error(`\n cannot get status, reason: ${err.message}`);
process.exit(1);
return sock.end();
}

let logFilePath = null;
Expand All @@ -41,7 +46,7 @@ sock.on('remote', function(rpc) {

if (!utils.existsSync(logFilePath)) {
console.error(`\n no logs to show for ${storjshare_logs.nodeid}`);
process.exit(0);
return sock.end();
}

let logTail = new Tail(logFilePath);
Expand Down
4 changes: 2 additions & 2 deletions bin/storjshare-restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ sock.on('remote', function(rpc) {
rpc.restart(storjshare_restart.nodeid, (err) => {
if (err) {
console.error(`\n cannot restart node, reason: ${err.message}`);
process.exit(1);
return sock.end();
}
console.info(`\n * share ${storjshare_restart.nodeid} restarted`);
process.exit(0);
sock.end();
});
});
10 changes: 5 additions & 5 deletions bin/storjshare-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ if (!storjshare_start.config) {
process.exit(1);
}

const configPath = storjshare_start.config[0] === '/' ?
storjshare_start.config :
path.join(process.cwd(), storjshare_start.config);
const configPath = path.isAbsolute(storjshare_start.config) ?
path.normalize(storjshare_start.config) :
path.join(process.cwd(), storjshare_start.config);
const sock = dnode.connect(config.daemonRpcPort);

sock.on('remote', function(rpc) {
rpc.start(configPath, (err) => {
if (err) {
console.error(`\n failed to start share, reason: ${err.message}`);
process.exit(1);
return sock.end();
}
console.info(`\n * starting share with config at ${configPath}`);
process.exit(0);
sock.end();
});
});
9 changes: 5 additions & 4 deletions bin/storjshare-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ storjshare_status
sock.on('remote', function(rpc) {
rpc.status(function(err, shares) {
let table = new Table({
head: ['Node ID', 'Status', 'Uptime', 'Peers', '% Shared'],
head: ['Node ID', 'Status', 'Uptime', 'Restarts', 'Peers', '% Shared'],
style: {
head: ['cyan', 'bold'],
border: []
Expand All @@ -41,11 +41,12 @@ sock.on('remote', function(rpc) {
}

table.push([
share.id,
share.id || '?',
status,
prettyMs(share.meta.uptimeMs),
share.meta.farmerState.totalPeers,
share.meta.farmerState.percentUsed
share.meta.numRestarts || 0,
share.meta.farmerState.totalPeers || 0,
share.meta.farmerState.percentUsed || '...'
]);
});
console.log('\n' + table.toString());
Expand Down
4 changes: 2 additions & 2 deletions bin/storjshare-stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ sock.on('remote', function(rpc) {
rpc.stop(storjshare_stop.nodeid, (err) => {
if (err) {
console.error(`\n cannot stop node, reason: ${err.message}`);
process.exit(1);
return sock.end();
}
console.info(`\n * share ${storjshare_stop.nodeid} stopped`);
process.exit(0);
return sock.end();
});
});
30 changes: 22 additions & 8 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class RPC {
config: null,
meta: {
uptimeMs: 0,
numRestarts: 0,
farmerState: {}
},
process: null,
Expand Down Expand Up @@ -106,24 +107,35 @@ class RPC {
);
share.readyState = RPC.SHARE_STARTED;

let logFile = fs.createWriteStream(share.config.loggerOutputFile, {
flags: 'a'
});

// NB: Pipe the stdio to the configured log file
share.process.stdout.pipe(
fs.createWriteStream(share.config.loggerOutputFile, {
flags: 'a'
})
);
share.process.stdout.pipe(logFile);
share.process.stderr.pipe(logFile);

// NB: Listen for state changes to update the share's record
share.process.on('error', (err) => {
share.readyState = RPC.SHARE_ERRORED;
self._log(err.message, 'error');
clearInterval(uptimeCounter);
});
share.process.on('exit', (code) => {

// NB: Listen for exits and restart the share if not stopped manually
share.process.on('exit', (code, signal) => {
let maxRestartsReached = share.meta.numRestarts >= RPC.MAX_RESTARTS;
share.readyState = RPC.SHARE_STOPPED;

self._log(`share ${nodeId} exited with code ${code}`);
clearInterval(uptimeCounter);

if (signal !== 'SIGINT' && !maxRestartsReached) {
share.meta.numRestarts++;
self.restart(nodeId, () => null);
}
});

share.process.on('message', (msg) => self._processShareIpc(share, msg));
self.shares.set(nodeId, share);
callback(null);
Expand Down Expand Up @@ -199,14 +211,15 @@ class RPC {
/**
* Simply kills the daemon and all managed proccesses
*/
killall() {
killall(callback) {
this._log(`received kill signal, destroying running shares`);

for (let nodeId of this.shares.keys()) {
this.destroy(nodeId, () => null);
}

process.exit(0);
callback();
setTimeout(() => process.exit(0), 1000);
}

/**
Expand Down Expand Up @@ -246,5 +259,6 @@ class RPC {
RPC.SHARE_STARTED = 1;
RPC.SHARE_STOPPED = 0;
RPC.SHARE_ERRORED = 2;
RPC.MAX_RESTARTS = 30;

module.exports = RPC;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storjshare-daemon",
"version": "2.0.0",
"version": "2.0.1",
"description": "daemon + process manager for sharing space on the storj network",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -54,7 +54,7 @@
"pretty-ms": "^2.1.0",
"rc": "^1.1.6",
"readable-stream": "^2.2.2",
"storj-lib": "^6.0.11",
"storj-lib": "^6.0.12",
"strip-json-comments": "^2.0.1",
"tail": "^1.2.1"
},
Expand Down
12 changes: 8 additions & 4 deletions test/api.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('class:RPC', function() {
it('should fork the share and setup listeners', function(done) {
let _proc = new EventEmitter();
_proc.stdout = new Readable({ read: () => null });
_proc.stderr = new Readable({ read: () => null });
let _RPC = proxyquire('../lib/api', {
fs: {
createWriteStream: sinon.stub().returns(new Writable({
Expand Down Expand Up @@ -237,10 +238,13 @@ describe('class:RPC', function() {
rpc.shares.set('test1', {});
rpc.shares.set('test2', {});
rpc.shares.set('test3', {});
rpc.killall();
expect(destroy.callCount).to.equal(3);
expect(exit.called).to.equal(true);
exit.restore();
rpc.killall(() => {
expect(destroy.callCount).to.equal(3);
setTimeout(() => {
expect(exit.called).to.equal(true);
exit.restore();
}, 1200);
});
});

});
Expand Down