Browse Source

adding #19

release/0.1
Björn 4 years ago
parent
commit
70244acee5
4 changed files with 238 additions and 25 deletions
  1. +134
    -0
      app/Commands/MariadbAddCommand.php
  2. +6
    -3
      app/Commands/MariadbInstallCommand.php
  3. +75
    -0
      app/Commands/MariadbRemoveCommand.php
  4. +23
    -22
      app/Commands/NginxInstallCommand.php

+ 134
- 0
app/Commands/MariadbAddCommand.php View File

@ -0,0 +1,134 @@
<?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;
/**
*
*
*
*/
class MariadbAddCommand extends Command
{
// destination for username and password
const MCP_LOG_FILE = '/root/mcp.log';
// length for password
const NAME_LENGTH = 15;
// length for password
const PASSWORD_LENGTH = 40;
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'mariadb:add {--ssl}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Add Mariadb User and Database';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// enter 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();
}
$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);
}
}

+ 6
- 3
app/Commands/MariadbInstallCommand.php View File

@ -14,11 +14,14 @@ use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
/** /**
* *
* *
*
*
*/ */
class MariadbInstallCommand extends Command class MariadbInstallCommand extends Command
{ {
// destination for username and password
const MCP_LOG_FILE = '/root/mcp.log'; const MCP_LOG_FILE = '/root/mcp.log';
// length for password
const PASSWORD_LENGTH = 40; const PASSWORD_LENGTH = 40;
/** /**
@ -82,10 +85,10 @@ class MariadbInstallCommand extends Command
exec('sudo mysql -u root -e "FLUSH PRIVILEGES;"'); exec('sudo mysql -u root -e "FLUSH PRIVILEGES;"');
$this->info('Success! \o/ Check /root/mcp.log'); $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 { } else {
$this->error('Failed! /o\\');
$this->error('Failed!');
} }
} }
} }

+ 75
- 0
app/Commands/MariadbRemoveCommand.php View File

@ -0,0 +1,75 @@
<?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;
/**
*
*
*
*/
class MariadbRemoveCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'mariadb:remove {database}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Remove Mariadb Database and User';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->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/ ');
}
}

+ 23
- 22
app/Commands/NginxInstallCommand.php View File

@ -4,14 +4,15 @@ namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command; use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\App;
use App\Facades\Install; use App\Facades\Install;
use App\BladeFile; use App\BladeFile;
use Log;
/**
*
*
*
*/
class NginxInstallCommand extends Command class NginxInstallCommand extends Command
{ {
/** /**
@ -40,34 +41,34 @@ class NginxInstallCommand extends Command
exec('apt update 2>&1'); exec('apt update 2>&1');
exec('apt install -y nginx 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 // adding ufw to nginx
exec('ufw allow "Nginx Full"'); exec('ufw allow "Nginx Full"');
$this->info("Success!");
$this->info('Success! \o/');
} else { } else {
$this->error("failed");
$this->error('Failed! /o\\');
} }
} }
} }

Loading…
Cancel
Save