<?php
							 | 
						|
								
							 | 
						|
								namespace App\Commands;
							 | 
						|
								
							 | 
						|
								use Illuminate\Console\Scheduling\Schedule;
							 | 
						|
								use LaravelZero\Framework\Commands\Command;
							 | 
						|
								use Illuminate\Support\Facades\File;
							 | 
						|
								
							 | 
						|
								use App\Facades\Install;
							 | 
						|
								
							 | 
						|
								use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
							 | 
						|
								use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 *  Install Mariadb
							 | 
						|
								 *
							 | 
						|
								 *  @author Björn Hase, Tentakelfabrik
							 | 
						|
								 *  @license http://opensource.org/licenses/MIT The MIT License
							 | 
						|
								 *  @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
							 | 
						|
								 *
							 | 
						|
								 */
							 | 
						|
								class MariadbInstallCommand extends Command
							 | 
						|
								{
							 | 
						|
								    // destination for username and password
							 | 
						|
								    const MCP_LOG_FILE = '/root/mcp.log';
							 | 
						|
								
							 | 
						|
								    // length for password
							 | 
						|
								    const PASSWORD_LENGTH = 40;
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * The signature of the command.
							 | 
						|
								     *
							 | 
						|
								     * @var string
							 | 
						|
								     */
							 | 
						|
								    protected $signature = 'mariadb:install {version=10.4}';
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * The description of the command.
							 | 
						|
								     *
							 | 
						|
								     * @var string
							 | 
						|
								     */
							 | 
						|
								    protected $description = 'Install Mariadb and set configuration';
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Execute the console command.
							 | 
						|
								     *
							 | 
						|
								     * @return mixed
							 | 
						|
								     */
							 | 
						|
								    public function handle()
							 | 
						|
								    {
							 | 
						|
								        $this->info('Mariadb install...');
							 | 
						|
								        $version = $this->argument('version');
							 | 
						|
								
							 | 
						|
								        exec('apt update 2>&1', $output);
							 | 
						|
								
							 | 
						|
								        // @TODO apt add a Warning for no good, in a later version output will be scanned for helpfull infos
							 | 
						|
								        $this->line(implode("\n", Install::filterAptMessages($output)));
							 | 
						|
								
							 | 
						|
								        if ($version === '10.4') {
							 | 
						|
								
							 | 
						|
								            $this->info('Mariadb try install 10.04...');
							 | 
						|
								
							 | 
						|
								            // getting release
							 | 
						|
								            $release = Install::getDistributionRelease();
							 | 
						|
								
							 | 
						|
								            if (Install::getDistributionId() === 'Ubuntu' &&  ($release === '18.04' || $release === '20.04')) {
							 | 
						|
								                $this->info('Mariadb install for Ubuntu '.$release.'...');
							 | 
						|
								
							 | 
						|
								                $output = [];
							 | 
						|
								                exec('apt install -y software-properties-common 2>&1', $output);
							 | 
						|
								                exec('apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 2>&1', $output);
							 | 
						|
								                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);
							 | 
						|
								                exec('apt update 2>&1', $output);
							 | 
						|
								            }
							 | 
						|
								        }
							 | 
						|
								
							 | 
						|
								        exec('apt install -y mariadb-server mariadb-client 2>&1', $output);
							 | 
						|
								
							 | 
						|
								        // @TODO apt add a Warning for no good, in a later version output will be scanned for helpfull infos
							 | 
						|
								        $this->line(implode("\n", Install::filterAptMessages($output)));
							 | 
						|
								
							 | 
						|
								        if (Install::isReady('mariadb-server mariadb-client')) {
							 | 
						|
								
							 | 
						|
								            $this->info('Mariadb setup...');
							 | 
						|
								
							 | 
						|
								            // generate password
							 | 
						|
								            $generator = new ComputerPasswordGenerator();
							 | 
						|
								            $generator->setRandomGenerator(new Php7RandomGenerator())
							 | 
						|
								                ->setUppercase()
							 | 
						|
								                ->setLowercase()
							 | 
						|
								                ->setUppercase()
							 | 
						|
								                ->setNumbers()
							 | 
						|
								                ->setSymbols(false)
							 | 
						|
								                ->setLength(self::PASSWORD_LENGTH);
							 | 
						|
								
							 | 
						|
								            // getting password for root
							 | 
						|
								            $password = $generator->generatePasswords()[0];
							 | 
						|
								
							 | 
						|
								            // make sure root can only access from local
							 | 
						|
								            $this->info('Mariadb make sure root can ony access from local...');
							 | 
						|
								            exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\');"');
							 | 
						|
								
							 | 
						|
								            // delete anonymous user
							 | 
						|
								            $this->info('Mariadb delete anonymous user...');
							 | 
						|
								            exec('sudo mysql -u root -e "DELETE FROM mysql.user WHERE User=\'\';"');
							 | 
						|
								
							 | 
						|
								            // drop test database and anthing familiar
							 | 
						|
								            $this->info('Mariadb drop test and anthing familiar...');
							 | 
						|
								            exec('sudo mysql -u root -e "DROP DATABASE IF EXISTS test;"');
							 | 
						|
								            exec('sudo mysql -u root -e "DELETE FROM mysql.db WHERE Db=\'test\' OR Db=\'test_%\';"');
							 | 
						|
								
							 | 
						|
								            // remove plugin for root and set password
							 | 
						|
								            $this->info('Mariadb add password for root...');
							 | 
						|
								
							 | 
						|
								            if ($version === '10.4') {
							 | 
						|
								                exec('sudo mysql -u root -e "ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD(\''.$password.'\'); FLUSH PRIVILEGES;"');
							 | 
						|
								            } else {
							 | 
						|
								                exec('sudo mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD(\''.$password.'\') WHERE User=\'root\'; FLUSH PRIVILEGES;"');
							 | 
						|
								                exec('sudo mysql -u root -e "UPDATE mysql.user SET plugin=\'\' where User=\'root\';"');
							 | 
						|
								            }
							 | 
						|
								
							 | 
						|
								            file_put_contents(self::MCP_LOG_FILE, "Mariadb installed\nuser: root\npassword: $password\n--\n", FILE_APPEND);
							 | 
						|
								            $this->info('Mariadb installing...Success! \o/ Check '.self::MCP_LOG_FILE);
							 | 
						|
								
							 | 
						|
								        } else {
							 | 
						|
								            $this->error('Failed! Please check log-file!');
							 | 
						|
								        }
							 | 
						|
								    }
							 | 
						|
								}
							 |