initdb.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Populates a new Redis database with data, which can be edited below.
  3. */
  4. var INIT_DATA = {
  5. // Must be present in any database
  6. "zt1": 1,
  7. /* The network ID here must be set to the ZeroTier address of your netconf
  8. * master (the node where netconf-master will be running) plus an arbitrary
  9. * 24-bit network ID. This will create the full 16-digit network ID of the
  10. * network you will join. This must be in the object name and in the "id"
  11. * field within the object itself. */
  12. "zt1:network:31443a1a0a111111:~": {
  13. "id": "31443a1a0a111111", // netconf master ZT address + 24-bit ID
  14. "name": "zerotier-testnet", // short name, no spaces or special chars
  15. "desc": "Test Network", // description
  16. "infrastructure": 0, // unused by netconf-master
  17. "private": 0, // set to '1' to require member approval
  18. "creationTime": 0, // unused by netconf-master
  19. "owner": "", // unused by netconf-master
  20. "etherTypes": "0800,0806", // hex ethernet frame types allowed
  21. "enableBroadcast": 1, // set to '1' to enable ff:ff:ff:ff:ff:ff
  22. "v4AssignMode": "zt", // 'zt' to assign, 'none' to let OS do it
  23. "v4AssignPool": "192.168.123.0/24", // IPv4 net block / netmask bits
  24. "v6AssignMode": "none" // 'zt' to assign, 'none' to let OS do it
  25. }
  26. };
  27. var config = require('./config.js');
  28. var async = require('async');
  29. var redis = require('redis');
  30. var DB = redis.createClient();
  31. DB.on("error",function(err) { console.error('redis query error: '+err); });
  32. DB.select(config.redisDb,function() {});
  33. DB.get("zt1",function(err,value) {
  34. if ((value)&&(!err)) {
  35. console.log("Redis database #"+config.redisDb+" appears to already contain data; flush it first!");
  36. return process.exit(0);
  37. }
  38. async.eachSeries(Object.keys(INIT_DATA),function(key,next) {
  39. var value = INIT_DATA[key];
  40. if (typeof value === 'object') {
  41. console.log(key);
  42. async.eachSeries(Object.keys(value),function(hkey,next2) {
  43. var hvalue = value[hkey];
  44. if (hvalue === true)
  45. hvalue = 1;
  46. if (hvalue === false)
  47. hvalue = 0;
  48. if (typeof hvalue !== 'string')
  49. hvalue = hvalue.toString();
  50. console.log('\t'+hkey+': '+hvalue);
  51. DB.hset(key,hkey,hvalue,next2);
  52. },next);
  53. } else if ((typeof value !== 'undefined')&&(value !== null)) {
  54. if (value === true)
  55. value = 1;
  56. if (value === false)
  57. value = 0;
  58. if (typeof value !== 'string')
  59. value = value.toString();
  60. console.log(key+': '+value);
  61. DB.set(key,value,next);
  62. } else return next(null);
  63. },function(err) {
  64. console.log('Done!');
  65. return process.exit(0);
  66. });
  67. });