Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const io = require('../tools/io');
  3. const {totalMemory, distVersion} = require('../tools/helpers');
  4. const stub = require('../tools/stub');
  5.  
  6. module.exports = async (config, program) => {
  7.  
  8.   io.header('MySQL Module');
  9.  
  10.   const dist = distVersion().toString();
  11.   const mysql_root_pass = config.answers.mysql_root_pass;
  12.   const db_name = config.answers.db_name;
  13.   const db_user = config.answers.db_user;
  14.   const db_pass = config.answers.db_pass;
  15.  
  16.   const mysql_dir = config.mysql_dir;
  17.  
  18.   io.info('Checking System Memory ...');
  19.   switch (true) {
  20.     case (totalMemory() >= 1200000 && totalMemory() < 3900000):
  21.       io.info('Configuring MySQL for a medium sized server ...');
  22.       fs.copyFileSync('./resources/mysql/my-medium.cnf', `${mysql_dir}/my.cnf`);
  23.       break;
  24.     case (totalMemory() >= 3900000):
  25.       io.info('Configuring MySQL for a large sized server ...');
  26.       fs.copyFileSync('./resources/mysql/my-large.cnf', `${mysql_dir}/my.cnf`);
  27.       break;
  28.     default:
  29.       io.warning('Configuring MySQL for a small sized server ...');
  30.       fs.copyFileSync('./resources/mysql/my-small.cnf', `${mysql_dir}/my.cnf`);
  31.   }
  32.  
  33.   io.info('Setting up MySQL ...');
  34.   if (!dist.includes('16.04') && !dist.includes('18.04')) {
  35.     io.warning(`Your OS version (${dist}) is old. Running mysql_install_db ...`);
  36.     const data = io.spawn('mysql_install_db');
  37.     if (program.debug) io.debug(data);
  38.   }
  39.  
  40.   if (dist.includes('18.04') && !fs.lstatSync('/var/lib/mysql').isDirectory()) {
  41.     // I am assuming that the fs library will be faster and more reliable then spawning a child process.
  42.     // So, I would love to use it where I can.
  43.     fs.mkdirSync('/var/lib/mysql');
  44.     // fs.chownSync(path, uid, gid) todo: how to effectively get the integer value of the mysql:mysql uid:gid ??
  45.     io.spawn('chown', ['mysql:mysql', '/var/lib/mysql']); // todo: Remove IF we resolve the above line!
  46.  
  47.     io.info('Securing MySQL ...');
  48.     const data = io.spawn('mysqld', [' --initialize-insecure']);
  49.     if (program.debug) io.debug(data);
  50.   }
  51.  
  52.   io.info('Reading "mysql/.my.cnf" stub file ...');
  53.   const myStub = stub.ReadFile('mysql/.my.cnf');
  54.  
  55.   io.info('Preparing stub file with user specified values ...');
  56.   const mycnf = stub.Replace({
  57.     'mysql_root_pass': mysql_root_pass
  58.   }, myStub);
  59.  
  60.   io.info('Writing to "/root/.my.cnf" ...');
  61.   if (!stub.WriteFile('/root/.my.cnf', mycnf)) {
  62.     io.error('Writing /root/.my.cnf FAILED. Please report this bug!');
  63.     io.error('You will likely need to reinstall your OS and try again after receiving support!');
  64.     process.exit(1);
  65.   }
  66.  
  67.   io.info('Running "update-rc.d" ...');
  68.   program.debug ?
  69.     io.debug(io.spawn('update-rc.d', ['mysql', 'defaults'])) :
  70.     io.spawn('update-rc.d', ['mysql', 'defaults']);
  71.  
  72.   io.info('Starting mysql service ...');
  73.   program.debug ?
  74.     io.debug(io.spawn('service', ['mysql', 'start'])) :
  75.     io.spawn('service', ['mysql', 'start']);
  76.  
  77.   io.info('Running "mysqladmin" ...');
  78.   program.debug ?
  79.     io.debug(io.spawn('mysqladmin', ['-u', 'root', 'password', mysql_root_pass])) :
  80.     io.spawn('mysqladmin', ['-u', 'root', 'password', mysql_root_pass]);
  81.  
  82.   fs.chmod('/root/.my.cnf', 600);
  83.  
  84.   const mysqlCmds = [
  85.     `"DROP USER IF EXISTS ${db_user}"`,
  86.     `"DROP DATABASE IF EXISTS ${db_name}"`,
  87.     `"CREATE DATABASE ${db_name}"`,
  88.     `"CREATE USER '${db_user}'@'localhost' IDENTIFIED BY '${db_pass}';"`,
  89.     `"GRANT ALL PRIVILEGES ON ${db_name} . * TO '${db_user}'@'localhost'"`,
  90.     `"UPDATE mysql.user SET authentication_string=PASSWORD('${mysql_root_pass}') WHERE User='root'"`,
  91.     `"DELETE FROM mysql.user WHERE User=''"`,
  92.     `"DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"`,
  93.     `"DROP DATABASE test"`,
  94.     `"DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"`,
  95.     `"FLUSH PRIVILEGES"`,
  96.   ];
  97.  
  98.   for (const cmd of mysqlCmds) {
  99.     const data = io.spawn('mysql', ['-e', cmd]);
  100.     if (program.debug) io.debug(data);
  101.   }
  102.  
  103.   return io.success('MySQL Module Completed Successfully');
  104. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement