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.

183 lines
7.2 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
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. /**
  20. * The signature of the command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'mariadb:install {version=10.4} {--remote}';
  25. /**
  26. * The description of the command.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'Install Mariadb and set configuration';
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $this->info('Mariadb install...');
  39. $version = $this->argument('version');
  40. exec('apt update 2>&1', $output);
  41. // @TODO apt add a Warning for no good, in a later version output will be scanned for helpfull infos
  42. $this->line(implode("\n", Install::filterAptMessages($output)));
  43. if ($version === '10.4') {
  44. $this->info('Mariadb try install 10.04...');
  45. // getting release
  46. $release = Install::getDistributionRelease();
  47. if (Install::getDistributionId() === 'Ubuntu' && ($release === '18.04' || $release === '20.04')) {
  48. $this->info('Mariadb install for Ubuntu '.$release.'...');
  49. $output = [];
  50. exec('apt install -y software-properties-common 2>&1', $output);
  51. exec('apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 2>&1', $output);
  52. 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);
  53. exec('apt update 2>&1', $output);
  54. }
  55. }
  56. exec('apt install -y mariadb-server mariadb-client 2>&1', $output);
  57. // @TODO apt add a Warning for no good, in a later version output will be scanned for helpfull infos
  58. $this->line(implode("\n", Install::filterAptMessages($output)));
  59. if (Install::isReady('mariadb-server') && Install::isReady('mariadb-client')) {
  60. $this->info('Mariadb setup...');
  61. // setting password for root, repeat until
  62. do {
  63. $password = $this->secret('Root Password');
  64. $passwordRepeat = $this->secret('Repeat Root Password');
  65. if ($password !== $passwordRepeat) {
  66. $this->error('Password not equal! Try again!');
  67. }
  68. if (empty($password)) {
  69. $this->error('Password is empty! Try again!');
  70. }
  71. } while ($password !== $passwordRepeat || empty($password));
  72. // make sure root can only access from local
  73. $this->info('Mariadb make sure root can ony access from local...');
  74. system('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\');"');
  75. // delete anonymous user
  76. $this->info('Mariadb delete anonymous user...');
  77. system('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'\';"');
  78. // drop test database and anthing familiar
  79. $this->info('Mariadb drop test and anthing familiar...');
  80. system('sudo mysql -u root -e "DROP DATABASE IF EXISTS test;"');
  81. system('sudo mysql -u root -e "DELETE FROM mysql.db WHERE Db=\'test\' OR Db=\'test_%\';"');
  82. // remove plugin for root and set password
  83. $this->info('Mariadb add password for root...');
  84. if ($version === '10.4') {
  85. system('sudo mysql -u root -e "ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD(\''.$password.'\'); FLUSH PRIVILEGES;"');
  86. } else {
  87. system('sudo mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD(\''.$password.'\') WHERE User=\'root\'; FLUSH PRIVILEGES;"');
  88. system('sudo mysql -u root -e "UPDATE mysql.user SET plugin=\'\' where User=\'root\';"');
  89. }
  90. $this->info('Mariadb installing...Success! \o/');
  91. if ($this->option('remote') === true) {
  92. $this->remoteAccess();
  93. }
  94. } else {
  95. $this->error('Failed! Please check log-file!');
  96. }
  97. }
  98. /**
  99. *
  100. *
  101. */
  102. private function remoteAccess()
  103. {
  104. $this->info('Mariadb remote...');
  105. system('mkdir -p /etc/mysql/ssl');
  106. system('hostname', $hostname);
  107. $this->info('Generating CA');
  108. system('openssl genrsa 4096 > /etc/mysql/ssl/ca-key.pem');
  109. system('openssl req -new -x509 -nodes -days 365000 -key /etc/mysql/ssl/ca-key.pem -out /etc/mysql/ssl/ca-cert.pem -subj "/CN='.$hostname.'-mysql-ca"');
  110. $this->info('Generating Server Certificate');
  111. system('openssl req -newkey rsa:4096 -days 365000 -nodes -keyout /etc/mysql/ssl/server-key.pem -out /etc/mysql/ssl/server-req.pem -subj "/CN='.$hostname.'-mysql-server"');
  112. system('openssl rsa -in /etc/mysql/ssl/server-key.pem -out /etc/mysql/ssl/server-key.pem');
  113. system('openssl x509 -req -in /etc/mysql/ssl/server-req.pem -days 365000 -CA /etc/mysql/ssl/ca-cert.pem -CAkey /etc/mysql/ssl/ca-key.pem -set_serial 01 -out /etc/mysql/ssl/server-cert.pem');
  114. $this->info('Generating Client Certificate');
  115. system('openssl req -newkey rsa:4096 -days 365000 -nodes -keyout /etc/mysql/ssl/client-key.pem -out /etc/mysql/ssl/client-req.pem -subj "/CN='.$hostname.'-mysql-server"');
  116. system('openssl rsa -in /etc/mysql/ssl/client-key.pem -out /etc/mysql/ssl/client-key.pem');
  117. system('openssl x509 -req -in /etc/mysql/ssl/client-req.pem -days 365000 -CA /etc/mysql/ssl/ca-cert.pem -CAkey /etc/mysql/ssl/ca-key.pem -set_serial 01 -out /etc/mysql/ssl/client-cert.pem');
  118. if (!file_exist('/etc/mysql/ssl/ca-cert.pem') || !file_exist('/etc/mysql/ssl/client-cert.pem') || file_exist('/etc/mysql/ssl/client-key.pem')) {
  119. $this->error('Failed! Certificates not created!');
  120. exit();
  121. }
  122. $this->info('Validate Certificates');
  123. system('openssl verify -CAfile /etc/mysql/ssl/ca-cert.pem /etc/mysql/ssl/server-cert.pem /etc/mysql/ssl/client-cert.pem');
  124. system('cat >> /etc/mysql/my.cnf << EOF
  125. [mysqld]
  126. bind-address = 0.0.0.0
  127. ssl-ca=/etc/mysql/ssl/ca-cert.pem
  128. ssl-cert=/etc/mysql/ssl/server-cert.pem
  129. ssl-key=/etc/mysql/ssl/server-key.pem
  130. [client]
  131. ssl-ca=/etc/mysql/ssl/ca-cert.pem
  132. ssl-cert=/etc/mysql/ssl/client-cert.pem
  133. ssl-key=/etc/mysql/ssl/client-key.pem');
  134. system('chown -R mysql:mysql /etc/mysql/ssl');
  135. system('chmod 644 /etc/mysql/ssl/*cert*');
  136. system('chmod 644 /etc/mysql/ssl/*key*');
  137. system('service restart mysql');
  138. system('ufw allow mysql');
  139. $this->info('Mariadb remote...Success! \o/');
  140. }
  141. }