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.

129 lines
4.7 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
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 {version=10.4}';
  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. $version = $this->argument('version');
  44. exec('apt update 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 ($version === '10.4') {
  48. $this->info('Mariadb try install 10.04...');
  49. // getting release
  50. $release = Install::getDistributionRelease();
  51. if (Install::getDistributionId() === 'Ubuntu' && ($release === '18.04' || $release === '20.04')) {
  52. $this->info('Mariadb install for Ubuntu '.$release.'...');
  53. $output = [];
  54. exec('apt install -y software-properties-common 2>&1', $output);
  55. exec('apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 2>&1', $output);
  56. exec('add-apt-repository -y "deb [arch=amd64,arm64,ppc64el] http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu '.Install::getDistributionCodename().' main" 2>&1', $output);
  57. exec('apt update 2>&1', $output);
  58. }
  59. }
  60. exec('apt install -y mariadb-server mariadb-client 2>&1', $output);
  61. // @TODO apt add a Warning for no good, in a later version output will be scanned for helpfull infos
  62. $this->line(implode("\n", Install::filterAptMessages($output)));
  63. if (Install::isReady('mariadb-server mariadb-client')) {
  64. $this->info('Mariadb setup...');
  65. // generate password
  66. $generator = new ComputerPasswordGenerator();
  67. $generator->setRandomGenerator(new Php7RandomGenerator())
  68. ->setUppercase()
  69. ->setLowercase()
  70. ->setUppercase()
  71. ->setNumbers()
  72. ->setSymbols(false)
  73. ->setLength(self::PASSWORD_LENGTH);
  74. // getting password for root
  75. $password = $generator->generatePasswords()[0];
  76. // make sure root can only access from local
  77. $this->info('Mariadb make sure root can ony access from local...');
  78. exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\');"');
  79. // delete anonymous user
  80. $this->info('Mariadb delete anonymous user...');
  81. exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'\';"');
  82. // drop test database and anthing familiar
  83. $this->info('Mariadb drop test and anthing familiar...');
  84. exec('sudo mysql -u root -e "DROP DATABASE IF EXISTS test;"');
  85. exec('sudo mysql -u root -e "DELETE FROM mysql.db WHERE Db=\'test\' OR Db=\'test_%\';"');
  86. // remove plugin for root and set password
  87. $this->info('Mariadb add password for root...');
  88. if ($version === '10.4') {
  89. exec('sudo mysql -u root -e "ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD(\''.$password.'\'); FLUSH PRIVILEGES;"');
  90. } else {
  91. exec('sudo mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD(\''.$password.'\') WHERE User=\'root\'; FLUSH PRIVILEGES;"');
  92. exec('sudo mysql -u root -e "UPDATE mysql.user SET plugin=\'\' where User=\'root\';"');
  93. }
  94. file_put_contents(self::MCP_LOG_FILE, "Mariadb installed\nuser: root\npassword: $password\n--\n", FILE_APPEND);
  95. $this->info('Mariadb installing...Success! \o/ Check '.self::MCP_LOG_FILE);
  96. } else {
  97. $this->error('Failed! Please check log-file!');
  98. }
  99. }
  100. }