<?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 Client for Remote Access
							 | 
						|
								 *
							 | 
						|
								 *  @author Björn Hase, Tentakelfabrik
							 | 
						|
								 *  @license http://opensource.org/licenses/MIT The MIT License
							 | 
						|
								 *  @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
							 | 
						|
								 *
							 | 
						|
								 */
							 | 
						|
								class MariadbClientInstallCommand extends Command
							 | 
						|
								{
							 | 
						|
								    /**
							 | 
						|
								     * The signature of the command.
							 | 
						|
								     *
							 | 
						|
								     * @var string
							 | 
						|
								     */
							 | 
						|
								    protected $signature = 'mariadb-client:install {remote_user} {remote_host} {version=10.4}';
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * The description of the command.
							 | 
						|
								     *
							 | 
						|
								     * @var string
							 | 
						|
								     */
							 | 
						|
								    protected $description = 'Install Mariadb Client and set configuration';
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Execute the console command.
							 | 
						|
								     *
							 | 
						|
								     * @return mixed
							 | 
						|
								     */
							 | 
						|
								    public function handle()
							 | 
						|
								    {
							 | 
						|
								        $this->info('Mariadb Client 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-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-client')) {
							 | 
						|
								            if (!is_dir('/etc/mysql/ssl')) {
							 | 
						|
								                system('mkdir /etc/mysql/ssl');
							 | 
						|
								            }
							 | 
						|
								
							 | 
						|
								            // getting
							 | 
						|
								            system('rsync -rv --include="ca-cert.pem" --include="client-cert.pem" --include="client-key.pem" --exclude="*" '.$this->argument('remove_user').'@'.$this->argument('remove_host').':/etc/mysql/ssl/ /etc/mysql/ssl/');
							 | 
						|
								
							 | 
						|
								            // checking if certificates are exists from remote server
							 | 
						|
								            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')) {
							 | 
						|
								                $this->error('Failed! Certificates not found!');
							 | 
						|
								                exit();
							 | 
						|
								            }
							 | 
						|
								
							 | 
						|
								            system('cat >> /etc/mysql/my.cnf << EOF
							 | 
						|
								[client]
							 | 
						|
								ssl-ca=/etc/mysql/ssl/ca-cert.pem
							 | 
						|
								ssl-cert=/etc/mysql/ssl/client-cert.pem
							 | 
						|
								ssl-key=/etc/mysql/ssl/client-key.pem
							 | 
						|
								            EOF');
							 | 
						|
								
							 | 
						|
								            system('chown -R mysql:mysql /etc/mysql/ssl');
							 | 
						|
								            system('chmod 644 /etc/mysql/ssl/*cert*');
							 | 
						|
								            system('chmod 644 /etc/mysql/ssl/*key*');
							 | 
						|
								
							 | 
						|
								        } else {
							 | 
						|
								            $this->error('Failed! Please check log-file!');
							 | 
						|
								        }
							 | 
						|
								    }
							 | 
						|
								}
							 |