Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs   = require('fs');
  2.  
  3. var changeProperty = function(property, value, template){
  4.  
  5.     var pattern = new RegExp('(^|\\n)(# )?(.*)('+property+'): ([^#\\n$]*)(.*)(\\n|$)?');
  6.     var str = pattern.exec(template);
  7.     if (!str){
  8.         template += "\n" +property+": "+value;
  9.     } else{
  10.         if (RegExp.$2)
  11.             template += "\n" +property+": "+value
  12.         else
  13.             template = template.replace(RegExp.$5, value); 
  14.         // template = template.replace(RegExp.$2,"  ");
  15.     };
  16.  
  17.     return template;
  18. };
  19. var changePropertyjs = function(property, value, template){
  20.  
  21.     var pattern = new RegExp('(^|\\n)(.*)('+property+'): ([^\/\/\\n$]*)(.*)(\\n|$)?');
  22.     var str = pattern.exec(template);
  23.     if (!str){
  24.         template += "\n" +property+": "+value;
  25.     } else{
  26.         template = template.replace(RegExp.$4, value); 
  27.         // template = template.replace(RegExp.$2,"  ");
  28.     };
  29.  
  30.     return template;
  31. };
  32.  
  33.  
  34. var makeConfigBox = function (par, cb){
  35.     var template = fs.readFileSync(par.path+"/config.yml.template", 'utf8', function (e){
  36.         if (e)
  37.             cb(e);
  38.     });
  39.     var templatejs = fs.readFileSync(par.path+"/config.js.template", 'utf8', function (e){
  40.         if (e)
  41.             cb(e);
  42.     });
  43.  
  44.     template = changeProperty ('BOX_ENABLED', 'true', template);
  45.     template = changeProperty ('CLUSTER_INSTANCE_ROLE', '\'CLUSTER_MASTER_CLIENT\'', template);
  46.    
  47.     if (par.settings){
  48.         settingsyml = par.settings[0];
  49.             for(var key in settingsyml){
  50.                         template = changeProperty (key, settingsyml[key], template);
  51.                 }
  52.         settingsjs = par.settings[1];
  53.             for(var key in settingsjs){
  54.                         templatejs = changePropertyjs (key, settingsjs[key], templatejs);
  55.                 }
  56.     };
  57.  
  58.     fs.writeFile(par.path+"/config.yml",template, function(err){
  59.         if (err) cb(err);
  60.         fs.writeFile(path+"/config.js",templatejs, function(err){
  61.             if (err) cb(err);
  62.             cb;
  63.         });
  64.     });
  65. };
  66.  
  67. var makeConfigCloud = function (par, cb) {
  68.  
  69.     var template = fs.readFileSync(par.path+"/config.yml.template", 'utf8', function (e){
  70.         if (e) cb(e);
  71.     });
  72.    
  73.     template = changeProperty ('CLUSTER_INSTANCE_ROLE', '\'CLUSTER_MASTER\'', template);
  74.     if (par.settings){
  75.         for(var key in par.settings){
  76.                     template = changeProperty (key, par.settings[key], template);
  77.             };
  78.     };
  79.  
  80.     fs.writeFile(par.path+"/config.yaml",template, function(err){
  81.         if (err) cb(err);
  82.         cb;
  83.     });
  84. };
  85.  
  86. function makeConfig(par, cb){
  87.     switch (par.cfgtype) {
  88.         case 'box':
  89.             return makeConfigBox(par, function (err){
  90.             cb&&cb(err);
  91.         });
  92.        
  93.        
  94.         case 'cloud':
  95.             return makeConfigCloud(par, function (err){
  96.             cb&&cb(err);
  97.         });
  98.        
  99. };
  100.  
  101. var json = [{
  102.     CLUSTER_SELF_TCP_PORT: 5555,
  103.     SERVER_ADDRESS: '\'234234234\''
  104. },
  105. {
  106.     BOX_ID: 'sdfsdfsd',
  107.     SSL_CRT:  'sdfsdfsfsdfs',
  108. }];
  109. var par = {
  110.     path:"/home/nata/3.2nm",
  111.     cfgtype:'box',
  112.     settings: json
  113. };
  114.  
  115. makeConfig (par , function (data){
  116.         console.log(data);
  117.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement