From 70244acee5128d8cf5e3d0e2f9a34d8e385de6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Fri, 7 Aug 2020 19:16:56 +0200 Subject: [PATCH] adding #19 --- app/Commands/MariadbAddCommand.php | 134 +++++++++++++++++++++++++ app/Commands/MariadbInstallCommand.php | 9 +- app/Commands/MariadbRemoveCommand.php | 75 ++++++++++++++ app/Commands/NginxInstallCommand.php | 45 +++++---- 4 files changed, 238 insertions(+), 25 deletions(-) create mode 100644 app/Commands/MariadbAddCommand.php create mode 100644 app/Commands/MariadbRemoveCommand.php diff --git a/app/Commands/MariadbAddCommand.php b/app/Commands/MariadbAddCommand.php new file mode 100644 index 0000000..3a5e333 --- /dev/null +++ b/app/Commands/MariadbAddCommand.php @@ -0,0 +1,134 @@ +secret('Enter root password'); + + // connect database + try { + $mysqli = new \mysqli('127.0.0.1', 'root', $password); + } catch(\ErrorException $exception) { + $this->error('Failed! '.$exception->getMessage()); + exit(); + } + + $this->info('Mariadb Create Database...'); + + // generate password + $generator = new ComputerPasswordGenerator(); + $generator->setRandomGenerator(new Php7RandomGenerator()) + ->setUppercase() + ->setLowercase() + ->setUppercase() + ->setNumbers() + ->setSymbols(false) + ->setLength(self::PASSWORD_LENGTH); + + // getting password + $password = $generator->generatePasswords(1)[0]; + + $generator = new ComputerPasswordGenerator(); + $generator->setRandomGenerator(new Php7RandomGenerator()) + ->setUppercase(false) + ->setLowercase(false) + ->setUppercase(false) + ->setNumbers() + ->setSymbols(false) + ->setLength(self::NAME_LENGTH); + + // getting names + $names = $generator->generatePasswords(2); + + $database = 'db'.$names[0]; + $username = 'u'.$names[1]; + + // getting option for ssl + $ssl = $this->option('ssl'); + + $mysqli->query("CREATE DATABASE $database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci"); + + + if ($ssl === false) { + $mysqli->query("CREATE USER $username@localhost IDENTIFIED BY '$password'"); + } else { + $mysqli->query("CREATE USER $username@'%' IDENTIFIED BY '$password'"); + } + + if ($mysqli->error) { + $this->error('Failed! '.$mysqli->error); + $mysqli->query("DROP DATABASE IF EXISTS $database"); + $mysqli->query("DROP USER $username@localhost"); + $mysqli->query("DROP USER $username@'%'"); + + exit(); + } + + $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@localhost"); + + if ($ssl === true) { + $mysqli->query("GRANT ALL PRIVILEGES ON $database.* TO $username@'%' require ssl"); + } + + if ($mysqli->error) { + $this->error('Failed! '.$mysqli->error); + $mysqli->query("DROP DATABASE IF EXISTS $database"); + $mysqli->query("DROP USER $username@localhost"); + $mysqli->query("DROP USER $username@'%'"); + + exit(); + } + + $mysqli->query("FLUSH PRIVILEGES"); + $mysqli->close(); + + $this->info('Success! \o/ Check /root/mcp.log'); + file_put_contents(self::MCP_LOG_FILE, "Mariadb $database created\nuser: $username\npassword: $password\n--\n", FILE_APPEND); + } +} diff --git a/app/Commands/MariadbInstallCommand.php b/app/Commands/MariadbInstallCommand.php index a94d556..b2d4d93 100644 --- a/app/Commands/MariadbInstallCommand.php +++ b/app/Commands/MariadbInstallCommand.php @@ -14,11 +14,14 @@ use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator; /** * * - * + * */ class MariadbInstallCommand extends Command { + // destination for username and password const MCP_LOG_FILE = '/root/mcp.log'; + + // length for password const PASSWORD_LENGTH = 40; /** @@ -82,10 +85,10 @@ class MariadbInstallCommand extends Command exec('sudo mysql -u root -e "FLUSH PRIVILEGES;"'); $this->info('Success! \o/ Check /root/mcp.log'); - file_put_contents(self::MCP_LOG_FILE, "Mariadb installed\nuser:root\npassword:".$password."\n--\n", FILE_APPEND); + file_put_contents(self::MCP_LOG_FILE, "Mariadb installed\nuser: root\npassword: $password\n--\n", FILE_APPEND); } else { - $this->error('Failed! /o\\'); + $this->error('Failed!'); } } } diff --git a/app/Commands/MariadbRemoveCommand.php b/app/Commands/MariadbRemoveCommand.php new file mode 100644 index 0000000..fee98a4 --- /dev/null +++ b/app/Commands/MariadbRemoveCommand.php @@ -0,0 +1,75 @@ +info('Mariadb Remove Database...'); + + // getting root password + $password = $this->secret('Enter root password'); + + // connect database + try { + $mysqli = new \mysqli('127.0.0.1', 'root', $password); + } catch(\ErrorException $exception) { + $this->error('Failed! '.$exception->getMessage()); + exit(); + } + + $database = $this->argument('database'); + + // check for user of database + $result = $mysqli->query("SELECT User FROM mysql.db WHERE Db = '$database'"); + while ($row = $result->fetch_assoc()) { + + // get user from row + $user = $row['User']; + + // remove if exists + $mysqli->query("DROP USER IF EXISTS $user@localhost"); + $mysqli->query("DROP USER IS EXISTS $user@'%'"); + } + + // drop database + $mysqli->query("DROP DATABASE IF EXISTS $database"); + $mysqli->close(); + + $this->info('Success! \o/ '); + } +} diff --git a/app/Commands/NginxInstallCommand.php b/app/Commands/NginxInstallCommand.php index 96cf066..d97c31f 100644 --- a/app/Commands/NginxInstallCommand.php +++ b/app/Commands/NginxInstallCommand.php @@ -4,14 +4,15 @@ namespace App\Commands; use Illuminate\Console\Scheduling\Schedule; use LaravelZero\Framework\Commands\Command; -use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\App; use App\Facades\Install; use App\BladeFile; -use Log; - +/** + * + * + * + */ class NginxInstallCommand extends Command { /** @@ -40,34 +41,34 @@ class NginxInstallCommand extends Command exec('apt update 2>&1'); exec('apt install -y nginx 2>&1'); - // copy snippets - exec('cp '.base_path().'/resources/nginx/snippets/*.conf /etc/nginx/snippets'); + // check if nginx is ready and installed + if (Install::isReady('nginx')) { - $configuration = [ - 'user' => $this->option('user'), - 'env' => App::environment() - ]; + // copy snippets + exec('cp '.base_path().'/resources/nginx/snippets/*.conf /etc/nginx/snippets'); - // get workers - exec('echo $(grep ^processor /proc/cpuinfo | wc -l)', $output); - $configuration['processes'] = $output[0]; + $configuration = [ + 'user' => $this->option('user'), + 'env' => App::environment() + ]; - // get connections - exec('echo $(ulimit -n)', $output); - $configuration['connections'] = $output[1]; + // get workers + exec('echo $(grep ^processor /proc/cpuinfo | wc -l)', $output); + $configuration['processes'] = $output[0]; - $bladeFile = new BladeFile('/resources/nginx'); - $bladeFile->put('nginx', '/etc/nginx/nginx.conf', $configuration); + // get connections + exec('echo $(ulimit -n)', $output); + $configuration['connections'] = $output[1]; - // check if nginx is ready and installed - if (Install::isReady('nginx')) { + $bladeFile = new BladeFile('/resources/nginx'); + $bladeFile->put('nginx', '/etc/nginx/nginx.conf', $configuration); // adding ufw to nginx exec('ufw allow "Nginx Full"'); - $this->info("Success!"); + $this->info('Success! \o/'); } else { - $this->error("failed"); + $this->error('Failed! /o\\'); } } }