initdb.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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:ffffffffff111111:~": {
  13. "id": "ffffffffff111111", // 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, // unuxed 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": "28.0.0.0/7", // 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. config.redisDb = 2;
  29. var async = require('async');
  30. var redis = require('redis');
  31. var DB = redis.createClient();
  32. DB.on("error",function(err) { console.error('redis query error: '+err); });
  33. DB.select(config.redisDb,function() {});
  34. DB.get("zt1",function(err,value) {
  35. if ((value)&&(!err)) {
  36. console.log("Redis database #"+config.redisDb+" appears to already contain data; flush it first!");
  37. return process.exit(0);
  38. }
  39. async.eachSeries(Object.keys(INIT_DATA),function(key,next) {
  40. var value = INIT_DATA[key];
  41. if (typeof value === 'object') {
  42. console.log(key);
  43. async.eachSeries(Object.keys(value),function(hkey,next2) {
  44. var hvalue = value[hkey];
  45. if (hvalue === true)
  46. hvalue = 1;
  47. if (hvalue === false)
  48. hvalue = 0;
  49. if (typeof hvalue !== 'string')
  50. hvalue = hvalue.toString();
  51. console.log('\t'+hkey+': '+hvalue);
  52. DB.hset(key,hkey,hvalue,next2);
  53. },next);
  54. } else if ((typeof value !== 'undefined')&&(value !== null)) {
  55. if (value === true)
  56. value = 1;
  57. if (value === false)
  58. value = 0;
  59. if (typeof value !== 'string')
  60. value = value.toString();
  61. console.log(key+': '+value);
  62. DB.set(key,value,next);
  63. } else return next(null);
  64. },function(err) {
  65. console.log('Done!');
  66. return process.exit(0);
  67. });
  68. });