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.

94 lines
2.8 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
  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');
  41. exec('apt install -y mariadb-server mariadb-client 2>&1');
  42. if (Install::isReady('mariadb-server mariadb-client')) {
  43. $this->info('Mariadb setup...');
  44. // generate password
  45. $generator = new ComputerPasswordGenerator();
  46. $generator->setRandomGenerator(new Php7RandomGenerator())
  47. ->setUppercase()
  48. ->setLowercase()
  49. ->setUppercase()
  50. ->setNumbers()
  51. ->setSymbols(false)
  52. ->setLength(self::PASSWORD_LENGTH);
  53. // getting password for root
  54. $password = $generator->generatePasswords()[0];
  55. // remove plugin for root and set password
  56. exec('sudo mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD(\''.$password.'\') WHERE User=\'root\';"');
  57. exec('sudo mysql -u root -e "UPDATE mysql.user SET plugin=\'\' where User=\'root\';"');
  58. // delete anonymous user
  59. exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'\';"');
  60. // make sure root can only access from local
  61. exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\');"');
  62. // drop test database and anthing familiar
  63. exec('sudo mysql -u root -e "DROP DATABASE IF EXISTS test;"');
  64. exec('sudo mysql -u root -e "DELETE FROM mysql.db WHERE Db=\'test\' OR Db=\'test_%\';"');
  65. // update privileges
  66. exec('sudo mysql -u root -e "FLUSH PRIVILEGES;"');
  67. $this->info('Success! \o/ Check /root/mcp.log');
  68. file_put_contents(self::MCP_LOG_FILE, "Mariadb installed\nuser: root\npassword: $password\n--\n", FILE_APPEND);
  69. } else {
  70. $this->error('Failed!');
  71. }
  72. }
  73. }