OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use Illuminate\Support\Facades\File;
  6. use App\Facades\Install;
  7. use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
  8. use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
  9. /**
  10. *
  11. *
  12. *
  13. */
  14. class MariadbInstallCommand extends Command
  15. {
  16. // destination for username and password
  17. const MCP_LOG_FILE = '/root/mcp.log';
  18. // length for password
  19. const PASSWORD_LENGTH = 40;
  20. /**
  21. * The signature of the command.
  22. *
  23. * @var string
  24. */
  25. protected $signature = 'mariadb:install';
  26. /**
  27. * The description of the command.
  28. *
  29. * @var string
  30. */
  31. protected $description = 'Install Mariadb and set configuration';
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $this->info('Mariadb install...');
  40. exec('apt update 2>&1', $output);
  41. exec('apt install -y mariadb-server mariadb-client 2>&1', $output);
  42. // @TODO apt add a Warning for no good, in a later version output will be scanned for helpfull infos
  43. $this->line(implode("\n", Install::filterAptMessages($output)));
  44. if (Install::isReady('mariadb-server mariadb-client')) {
  45. $this->info('Mariadb setup...');
  46. // generate password
  47. $generator = new ComputerPasswordGenerator();
  48. $generator->setRandomGenerator(new Php7RandomGenerator())
  49. ->setUppercase()
  50. ->setLowercase()
  51. ->setUppercase()
  52. ->setNumbers()
  53. ->setSymbols(false)
  54. ->setLength(self::PASSWORD_LENGTH);
  55. // getting password for root
  56. $password = $generator->generatePasswords()[0];
  57. // remove plugin for root and set password
  58. $this->info('Mariadb add password for root...');
  59. exec('sudo mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD(\''.$password.'\') WHERE User=\'root\';"');
  60. exec('sudo mysql -u root -e "UPDATE mysql.user SET plugin=\'\' where User=\'root\';"');
  61. // make sure root can only access from local
  62. $this->info('Mariadb make sure root can ony access from local...');
  63. exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\');"');
  64. // delete anonymous user
  65. $this->info('Mariadb delete anonymous user...');
  66. exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'\';"');
  67. // drop test database and anthing familiar
  68. $this->info('Mariadb drop test and anthing familiar...');
  69. exec('sudo mysql -u root -e "DROP DATABASE IF EXISTS test;"');
  70. exec('sudo mysql -u root -e "DELETE FROM mysql.db WHERE Db=\'test\' OR Db=\'test_%\';"');
  71. // update privileges
  72. exec('sudo mysql -u root -e "FLUSH PRIVILEGES;"');
  73. $this->info('Mariadb installing...Success! \o/ Check '.self::MCP_LOG_FILE);
  74. file_put_contents(self::MCP_LOG_FILE, "Mariadb installed\nuser: root\npassword: $password\n--\n", FILE_APPEND);
  75. } else {
  76. $this->error('Failed! Please check log-file!');
  77. }
  78. }
  79. }