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

Commit 9ae702a

Browse files
committed
Config manager setup
1 parent b06889f commit 9ae702a

8 files changed

+392
-4
lines changed

.jshintrc

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"bitwise": false,
3+
"browser": true,
4+
"camelcase": false,
5+
"curly": true,
6+
"devel": false,
7+
"eqeqeq": true,
8+
"esnext": true,
9+
"freeze": true,
10+
"immed": true,
11+
"indent": 2,
12+
"latedef": true,
13+
"newcap": false,
14+
"noarg": true,
15+
"node": true,
16+
"noempty": true,
17+
"nonew": true,
18+
"quotmark": "single",
19+
"regexp": true,
20+
"smarttabs": false,
21+
"strict": true,
22+
"trailing": true,
23+
"undef": true,
24+
"unused": true,
25+
"maxparams": 4,
26+
"maxstatements": 14,
27+
"maxcomplexity": 6,
28+
"maxdepth": 3,
29+
"maxlen": 80,
30+
"multistr": true,
31+
"predef": [
32+
"after",
33+
"afterEach",
34+
"before",
35+
"beforeEach",
36+
"describe",
37+
"exports",
38+
"it",
39+
"module",
40+
"require"
41+
]
42+
}

lib/config.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
'use strict';
3+
4+
var path = require('path');
5+
var platform = require('os').platform();
6+
var HOME = platform !== 'win32' ? process.env.HOME : process.env.USERPROFILE;
7+
var CONFDIR = path.join(HOME, '.storjshare');
8+
var utils = require('./utils');
9+
var merge = require('merge');
10+
var fs = require('fs');
11+
12+
function ConfigManager(farmerId, options) {
13+
if (!(this instanceof ConfigManager)) {
14+
return new ConfigManager(farmerId, options);
15+
}
16+
17+
this.id = farmerId;
18+
this.confPath = path.join(CONFDIR, 'settings.json');
19+
this.config = this._readConf();
20+
21+
if (options) {
22+
this.config = merge.recursive(
23+
Object.create(ConfigManager.DEFAULTS),
24+
options.config
25+
);
26+
}
27+
28+
}
29+
30+
ConfigManager.prototype._readConf = function() {
31+
var self = this;
32+
var parsed;
33+
if (!utils.existsSync(this.confPath)) {
34+
fs.writeFileSync(this.confPath, JSON.stringify(ConfigManager.DEFAULTS));
35+
}
36+
37+
try {
38+
parsed = JSON.parse(fs.readFileSync(self.confPath).toString());
39+
} catch (err) {
40+
throw err;
41+
}
42+
43+
return merge.recursive(Object.create(ConfigManager.DEFAULTS), parsed);
44+
};
45+
46+
/**
47+
* Save the configuration at the given index
48+
*/
49+
ConfigManager.prototype.saveConfigSync = function() {
50+
try {
51+
fs.writeFileSync(this.confPath, JSON.stringify(this.toObject(), null, 2));
52+
} catch (err) {
53+
throw err;
54+
}
55+
};
56+
57+
/**
58+
* Save the configuration at the given index
59+
*/
60+
ConfigManager.prototype.saveConfig = function(callback) {
61+
fs.writeFile(
62+
this.confPath, JSON.stringify(this.toObject(), null, 2), function(err
63+
) {
64+
if (err) {
65+
return callback(err);
66+
}
67+
68+
return callback(null);
69+
}
70+
);
71+
};
72+
73+
/**
74+
* Returns a trimmed address
75+
* #getAddress
76+
*/
77+
ConfigManager.prototype.getAddress = function() {
78+
return this.address.trim();
79+
};
80+
81+
ConfigManager.DEFAULTS = {
82+
contracts: {
83+
total: 0
84+
},
85+
usedspace: {
86+
size: 0,
87+
unit: 'B'
88+
},
89+
remainingspace: {
90+
size: 0,
91+
unit: 'B'
92+
},
93+
key: '',
94+
address: '',
95+
storage: {
96+
path: '',
97+
dataDir: '',
98+
size: 0,
99+
unit: 'GB'
100+
},
101+
network: {
102+
hostname: '127.0.0.1',
103+
port: 0,
104+
seed: '',
105+
nat: true
106+
},
107+
tunnels: {
108+
numcx: 0,
109+
tcpPort: 0,
110+
startPort: 0,
111+
endPort: 0
112+
},
113+
connetedPeers: 0,
114+
telemetry: false,
115+
opcodes: ['0f01020202', '0f02020202', '0f03020202']
116+
};
117+
118+
module.exports = ConfigManager;

lib/farmer.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11

22
'use strict';
33

4-
var fs = require('fs');
4+
var ConfigManager = require('./config');
55

6-
function Farmer(driveConf, storageDir) {
6+
function Farmer(id, storageDir, options) {
77
if (!(this instanceof Farmer)) {
8-
return new Farmer(driveConf, storageDir);
8+
return new Farmer(id, storageDir, options);
9+
}
10+
11+
this.storageDir = storageDir;
12+
13+
try {
14+
this.confManager = new ConfigManager(id, options);
15+
} catch (err) {
16+
throw err;
917
}
1018

1119
}

lib/utils.js

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var storj = require('storj-lib');
5+
var bitcore = storj.deps.bitcore;
6+
var du = require('du');
7+
var diskspace = require('fd-diskspace').diskSpace;
8+
9+
10+
/**
11+
* Validate the given payout address
12+
* @param {String} address
13+
*/
14+
module.exports._isValidPayoutAddress = function(address) {
15+
return bitcore.Address.isValid(address) ||
16+
bitcore.Address.isValid(address, bitcore.Networks.testnet);
17+
};
18+
19+
/**
20+
* Validate the given dataserv directory
21+
* @param {String} directory
22+
*/
23+
module.exports._isValidDirectory = function(directory) {
24+
return this.existsSync(directory);
25+
};
26+
27+
/**
28+
* Validate the given size
29+
* @param {String} size
30+
*/
31+
module.exports._isValidSize = function(size) {
32+
return Number(size) > 0 && typeof size !== 'undefined';
33+
};
34+
35+
/**
36+
* Validates the space being allocated exists
37+
* #validateAllocation
38+
* @param {Object} tab
39+
*/
40+
module.exports.validateAllocation = function(conf, callback) {
41+
this.getFreeSpace(conf.storage.path, function(err, free) {
42+
var allocatedSpace = this.manualConvert(
43+
{ size: conf.storage.size, unit: conf.storage.unit }, 'B', 0
44+
);
45+
46+
this.getDirectorySize(conf.storage.dataDir, function(err, usedspacebytes) {
47+
if(err) {
48+
return callback(err);
49+
}
50+
51+
var usedspace = this.autoConvert(
52+
{ size: usedspacebytes, unit: 'B' }, 0
53+
);
54+
55+
conf.usedspace = usedspace;
56+
57+
if(allocatedSpace.size > free + usedspacebytes) {
58+
return callback(new Error('Invalid storage size'));
59+
}
60+
return callback(null);
61+
});
62+
});
63+
};
64+
65+
/**
66+
* Check if file exists
67+
* @param {String} file - Path to file
68+
*/
69+
module.exports.existsSync = function(file) {
70+
try {
71+
fs.statSync(file);
72+
} catch(err) {
73+
return !(err);
74+
}
75+
76+
return true;
77+
};
78+
79+
/**
80+
* Converts to a reasonable unit of bytes
81+
* @param {Object} bytes
82+
* @param {Number} precision
83+
*/
84+
module.exports.autoConvert = function(object, precision) {
85+
var kilobyte = 1000;
86+
var megabyte = kilobyte * 1000;
87+
var gigabyte = megabyte * 1000;
88+
var terabyte = gigabyte * 1000;
89+
90+
var byteobject = (this.manualConvert(object, 'B'));
91+
var bytes = byteobject.size;
92+
93+
if ((bytes >= kilobyte) && (bytes < megabyte)) {
94+
return this.manualConvert(byteobject, 'KB', (precision || 1));
95+
} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
96+
return this.manualConvert(byteobject, 'MB', (precision || 2));
97+
} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
98+
return this.manualConvert(byteobject, 'GB', (precision || 3));
99+
} else if (bytes >= terabyte) {
100+
return this.manualConvert(byteobject, 'TB', (precision || 4));
101+
}
102+
103+
return byteobject;
104+
105+
};
106+
107+
/**
108+
* Converts units of bytes to other units
109+
* @param {Object} object to be converted
110+
* @param {String} Unit Object will be converted to
111+
*/
112+
module.exports.manualConvert = function(object, unit, precision) {
113+
var table = {
114+
'B': 0,
115+
'KB': 1,
116+
'MB': 2,
117+
'GB': 3,
118+
'TB': 4
119+
};
120+
121+
precision = (!precision) ? (table[unit] ? table[unit] : 6) : precision;
122+
123+
var diff = table[object.unit] - table[unit];
124+
125+
if (diff < 0) {
126+
return {
127+
size: (object.size / Math.pow(1000, Math.abs(diff))).toFixed(precision),
128+
unit: unit
129+
};
130+
} else if (diff > 0) {
131+
return {
132+
size: (object.size * Math.pow(1000, Math.abs(diff))).toFixed(precision),
133+
unit: unit
134+
};
135+
} else {
136+
return object;
137+
}
138+
};
139+
140+
/**
141+
* find the difference between two file sizes
142+
* @param {Object} object
143+
* @param {Object} object
144+
*/
145+
module.exports.subtract = function(object1, object2) {
146+
var bytes1 = this.manualConvert(object1, 'B');
147+
var bytes2 = this.manualConvert(object2, 'B');
148+
149+
var difference = bytes1.size - bytes2.size;
150+
151+
return this.autoConvert({size: difference, unit: 'B'});
152+
};
153+
154+
/**
155+
* Recursively determines the size of a directory
156+
* @param {String} dir - Directory to traverse
157+
* @param {Function} callback
158+
*/
159+
module.exports.getDirectorySize = function(dir, callback) {
160+
du(
161+
dir,
162+
{
163+
filter: function(f) {
164+
return (
165+
f.indexOf('contracts.db') !== -1 ||
166+
f.indexOf('sharddata.kfs') !== -1
167+
);
168+
}
169+
},
170+
function (err, size) {
171+
callback(err,size);
172+
}
173+
);
174+
};
175+
176+
/**
177+
* get free space on disk of path
178+
* @param {String} path
179+
*/
180+
module.exports.getFreeSpace = function(path, callback) {
181+
var self = this;
182+
183+
if (!this.existsSync(path)) {
184+
return callback(null, 0);
185+
}
186+
187+
diskspace(function(err, result) {
188+
if (err) {
189+
return callback(err);
190+
}
191+
192+
var free = 0;
193+
194+
for (var disk in result.disks) {
195+
var diskDrive = disk;
196+
197+
if (process.platform === 'win32') {
198+
diskDrive += ':\\';
199+
}
200+
201+
if (self.existsSync(diskDrive)) {
202+
if (fs.statSync(path).dev === fs.statSync(diskDrive).dev) {
203+
// The `df` command on linux returns KB by default, so we need to
204+
// convert to bytes.
205+
free = process.platform === 'win32' ?
206+
result.disks[disk].free :
207+
result.disks[disk].free * 1000;
208+
}
209+
}
210+
}
211+
212+
return callback(null, free);
213+
});
214+
};

0 commit comments

Comments
 (0)