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.

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