gluon-remote-node

Node Version of gluon remote
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-remote-node.git
Log | Files | Refs | README | LICENSE

commands.js (5425B)


      1 var sshclient = require('sshclient');
      2 var request = require('request');
      3 var laeh = require('laeh2').leanStacks(true);
      4 var _x = laeh._x;
      5 var changeName = function (ip, key, newname, token, cb) {
      6     var opts = {
      7 
      8       host: ip,
      9       port: 22,
     10       username: 'root',
     11       privateKey: key,
     12 
     13       debug: true, // optional
     14       console: console // optional, allows logger overriding
     15     };
     16 
     17     sshclient.session(opts, _x(cb, true, function(err, ses) {
     18       _x(null, false, function(cb) {
     19         sshclient.exec('uci set system.@system[0].hostname="'+ newname + '"', cb);
     20         sshclient.exec('uci commit system', cb);
     21         sshclient.exec('/etc/init.d/system reload', cb);
     22       })
     23     }));
     24   }
     25 var changeContact = function (ip, key, name, email, token, cb) {
     26     var opts = {
     27 
     28       host: ip,
     29       port: 22,
     30       username: 'root',
     31       privateKey: key,
     32 
     33       debug: true, // optional
     34       console: console // optional, allows logger overriding
     35     };
     36 
     37     sshclient.session(opts, _x(cb, true, function(err, ses) {
     38       _x(null, false, function(cb) {
     39         sshclient.exec('uci get gluon-node-info.@owner[0] || uci add gluon-node-info owner', cb);
     40         sshclient.exec('uci set gluon-node-info.@owner[0].contact="' + name + ' <' + email + '>', cb);
     41         sshclient.exec('uci commit gluon-node-info', cb);
     42       })
     43     }));
     44   }
     45 var activateMOL = function (ip, key, cb) {
     46     var opts = {
     47 
     48       host: ip,
     49       port: 22,
     50       username: 'root',
     51       privateKey: key,
     52 
     53       debug: true, // optional
     54       console: console // optional, allows logger overriding
     55     };
     56 
     57     sshclient.session(opts, _x(cb, true, function(err, ses) {
     58       _x(null, false, function(cb) {
     59         sshclient.exec('for iface in $(cat /lib/gluon/core/sysconfig/lan_ifname); do uci del_list network.client.ifname=$iface; done', cb);
     60         sshclient.exec('uci set network.mesh_lan.auto=1', cb);
     61         sshclient.exec('uci commit network', cb);
     62         sshclient.exec('/etc/init.d/network restart', cb);
     63       })
     64     }));
     65   }
     66 var deactivateMOL = function (ip, key, cb) {
     67     var opts = {
     68 
     69       host: ip,
     70       port: 22,
     71       username: 'root',
     72       privateKey: key,
     73 
     74       debug: true, // optional
     75       console: console // optional, allows logger overriding
     76     };
     77 
     78     sshclient.session(opts, _x(cb, true, function(err, ses) {
     79       _x(null, false, function(cb) {
     80         sshclient.exec('for iface in $(cat /lib/gluon/core/sysconfig/lan_ifname); do uci add_list network.client.ifname=$iface; done', cb);
     81         sshclient.exec('uci set network.mesh_lan.auto=0', cb);
     82         sshclient.exec('uci commit network', cb);
     83         sshclient.exec('/etc/init.d/network restart', cb);
     84       })
     85     }));
     86   }
     87 var activateMOW = function (ip, key, cb) {
     88     var opts = {
     89 
     90       host: ip,
     91       port: 22,
     92       username: 'root',
     93       privateKey: key,
     94 
     95       debug: true, // optional
     96       console: console // optional, allows logger overriding
     97     };
     98 
     99     sshclient.session(opts, _x(cb, true, function(err, ses) {
    100       _x(null, false, function(cb) {
    101         sshclient.exec('uci set network.mesh_wan.auto=1', cb);
    102         sshclient.exec('uci commit network', cb);
    103         sshclient.exec('/etc/init.d/network restart', cb);
    104       })
    105     }));
    106   }
    107 var deactivateMOW = function (ip, key, cb) {
    108     var opts = {
    109 
    110       host: ip,
    111       port: 22,
    112       username: 'root',
    113       privateKey: key,
    114 
    115       debug: true, // optional
    116       console: console // optional, allows logger overriding
    117     };
    118 
    119     sshclient.session(opts, _x(cb, true, function(err, ses) {
    120       _x(null, false, function(cb) {
    121         sshclient.exec('uci set network.mesh_wan.auto=0', cb);
    122         sshclient.exec('uci commit network', cb);
    123         sshclient.exec('/etc/init.d/network restart', cb);
    124       })
    125     }));
    126   }
    127 var changeGeo = function (ip, key, lat, long, alt, token, ccode, cb) {
    128     if (ccode === "ffnord" || ccode === "ffhh") {
    129       request.get('https://formular.nord.freifunk.net/api/node/' + token, function (error, response, body) {
    130         if (!error && response.statusCode == 200) {
    131           var form_data_old = body
    132           console.log(body[1])
    133           request.post('https://formular.nord.freifunk.net/api/node/' + token, function (error, response, body) {
    134             if (!error && response.statusCode == 200) {
    135               console.log(body) // Show the HTML for the Google homepage.
    136             }
    137           })
    138         }
    139       })
    140     } else {
    141       var opts = {
    142 
    143         host: ip,
    144         port: 22,
    145         username: 'root',
    146         privateKey: key,
    147 
    148         debug: true, // optional
    149         console: console // optional, allows logger overriding
    150       };
    151 
    152       sshclient.session(opts, _x(cb, true, function(err, ses) {
    153         _x(null, false, function(cb) {
    154           sshclient.exec('uci set gluon-node-info.@location[0].latitude=' + lat, cb);
    155           sshclient.exec('uci set gluon-node-info.@location[0].longitude=' + long, cb);
    156           sshclient.exec('uci set gluon-node-info.@location[0].altitude=' + alt, cb);
    157           sshclient.exec('uci set gluon-node-info.@location[0].share_location=1', cb);
    158           sshclient.exec('uci commit gluon-node-info', cb);
    159         })
    160       }));
    161     }
    162 
    163   }
    164 
    165 exports.changeName = changeName;
    166 exports.changeContact = changeContact;
    167 exports.activateMOL = activateMOL;
    168 exports.deactivateMOL = deactivateMOL;
    169 exports.activateMOW = activateMOW;
    170 exports.deactivateMOW = deactivateMOW;
    171 exports.changeGeo = changeGeo;