UNPKG

cloud-log-collector

Version:

Collect log from mutiple servers

177 lines (152 loc) 8.97 kB
////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////// CLCwizard ////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////// function CLCwizard() { // Load external var readlineSync = require('readline-sync'); var fs = require('fs'); var crypto = require('crypto'); // custom function ensureDirSync (dirpath) { try { fs.mkdirSync(dirpath, { recursive: true }) } catch (err) { } } // init var var q; var config={}; // wizard console.log('---------------------------------------------------------------'); console.log('---------------------------------------------------------------'); console.log('Wizard for Cloud Log Collector configuration file'); console.log('---------------------------------------------------------------'); // conf params config['server']={}; config['server']['hostname'] = readlineSync.question('Server hostname :'); config['server']['port'] = readlineSync.question('Server port (1095) :'); if (config['server']['port']=="") config['server']['port']="1095"; config['server']['ssl']={}; config['server']['ssl']['certificate'] = readlineSync.question('SSL Certificate file on the server :'); config['server']['ssl']['key'] = readlineSync.question('SSL Certificate Key file on the server :'); config['server']['data']={}; config['server']['data']['path'] = readlineSync.question('Folder used to store all log files on the server (/var/clc/data) :'); if (config['server']['data']['path']=="") config['server']['data']['path']="/var/clc/data"; config['server']['tokens']={}; config['server']['tokens']['path'] = readlineSync.question('Folder used to store client credential data on the server (/var/clc/tokens) :'); if (config['server']['tokens']['path']=="") config['server']['tokens']['path']="/var/clc/tokens"; q=''; do { q=readlineSync.question('Want to use a magic token ? (automatically generates token from client side at first connection) (y/n) :'); } while ((q != 'n') &&(q != 'y')); if (q=='y') { var tempo = readlineSync.question('Enter magic token (empty = auto generated) :'); if (tempo=="") { tempo=Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); console.log("Magic token : "+tempo); } config['server']['tokens']['magic_salt']= Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)+Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); var hash = crypto.createHash('sha512'); data = hash.update(config['server']['tokens']['magic_salt']+tempo, 'utf-8'); gen_hash= data.digest('hex'); config['server']['tokens']['magic_hash']=gen_hash; } console.log('---------------------------------------------------------------'); config['community']={}; q=''; do { q=readlineSync.question('In order to help community, send anonymous data on system usage ? (To help future dev) (y/n) :'); } while ((q != 'n') &&(q != 'y')); config['community']['send_anonymous']=q; var config_server=JSON.stringify(config,null,4); console.log('---------------------------------------------------------------'); // client config var config2={}; config2['server']={}; config2['server']['hostname'] = config['server']['hostname']; config2['client']={}; config2['server']['port'] = config['server']['port']; config2['client']['SecretClientKey'] = "XXXSecretClientKeyXXX"; config2['client']['watcher']=[]; // log to process q=''; do { if (q!='') config2['client']['watcher'].push(q); q=readlineSync.question('Which log do you want to watch on the client ? (empty to continue) :'); } while (q != ''); var config_client=JSON.stringify(config2,null,4); console.log('---------------------------------------------------------------'); // where to save conf var config_path = readlineSync.question('Folder to save config file(s) (./conf) :'); if (config_path=="") config_path="./conf"; ensureDirSync(config_path); fs.writeFileSync(config_path+"/server.conf",config_server); fs.writeFileSync(config_path+"/client.conf",config_client); console.log('Once everything is configured, you should delete this folder for security reasons'); console.log('---------------------------------------------------------------'); console.log('---------------------------------------------------------------'); // process server part console.log('Process to install Cloud Log Collector on server :'); console.log('- copy \''+config_path+"/server.conf"+'\' to the server in file \'/as/you/want/server.conf\''); console.log('- run these commands in root :'); console.log('\tnpm i -g cloud-log-collector'); console.log('\tcloud-log-collector-install-server /as/you/want/server.conf'); console.log('delete file \'/as/you/want/server.conf\''); console.log('---------------------------------------------------------------'); var server_installed=false; do { q=readlineSync.question('Do you want me to install server side on this server now ? (y/n) :'); } while ((q != 'n') &&(q != 'y')); if (q=='y') { server_installed=true; var res = require('child_process').execSync("cloud-log-collector-install-server "+config_path+"/server.conf").toString(); console.log(res); require('child_process').execSync('systemctl restart CLCServer'); console.log('Done !'); } console.log('---------------------------------------------------------------'); // process client part console.log('Process to install Cloud Log Collector on client without magic token :'); console.log('- copy \''+config_path+"/client.conf"+'\' to the server in file \'/as/you/want/client.conf\''); console.log('\tReplace XXXSecretClientKeyXXX by your secret key in \'/as/you/want/client.conf\''); console.log('- run these commands :'); console.log('\tnpm i -g cloud-log-collector'); console.log('\tcloud-log-collector-install-client /as/you/want/client.conf'); console.log('delete file \'/as/you/want/client.conf\''); console.log('To get a secret client key, use the command \'cloud-log-collector-client-key-new XXXClientNameXXX\' on the server after installation'); console.log('To revoke a secret client key, use the command \'cloud-log-collector-client-key-revoke XXXClientNameXXX\' on the server after installation'); console.log('\'XXXClientNameXXX\' must only contain A-Za-z0-9.-_'); console.log('---------------------------------------------------------------'); // process client part console.log('Process to install Cloud Log Collector on client with magic token :'); console.log('- copy \''+config_path+"/client.conf"+'\' to the server in file \'/as/you/want/client.conf\''); console.log('- run these commands :'); console.log('\tnpm i -g cloud-log-collector'); console.log('\tcloud-log-collector-install-client /as/you/want/client.conf'); console.log('delete file \'/as/you/want/client.conf\''); console.log('\tCLCMagicToken="My Magic Token" CLCHostname="myhostname.com" cloud-log-collector-install-client-get-token'); console.log('---------------------------------------------------------------'); if (server_installed) { do { q=readlineSync.question('Do you want me to install client side on this server now ? (y/n) :'); } while ((q != 'n') &&(q != 'y')); if (q=='y') { var hostname=""; do { if (hostname!="") console.log('error - bad char in hostname !'); hostname=readlineSync.question('Server hostname in cloud-log-collector (could be different from hostname used to connect client and server) (a-zA-Z0-9.-_ only) :'); if (hostname=="") hostname="!"; } while (!(/^[a-zA-Z0-9\/.\-_]+$/g.test(hostname))); var res = require('child_process').execSync("cloud-log-collector-install-client "+config_path+"/client.conf").toString(); console.log(res); var to = require('child_process').execSync('cloud-log-collector-client-key-new '+hostname).toString(); var conf=JSON.parse(fs.readFileSync('/etc/clc/client.conf')); conf['client']['SecretClientKey'] = to; conf=JSON.stringify(conf,null,4); fs.writeFileSync("/etc/clc/client.conf",conf); console.log('Token saved in config file'); require('child_process').execSync('systemctl restart CLCServer'); require('child_process').execSync('systemctl restart CLCClient'); console.log('Done !'); } } console.log('Wizard end !'); } module.exports = { CLCwizard: CLCwizard }