Browse Source

adding letsencrypt

release/0.1
Björn 4 years ago
parent
commit
b3e38d9bdf
4 changed files with 108 additions and 140 deletions
  1. +0
    -126
      app/Commands/Certs.php
  2. +40
    -0
      app/Commands/LetsEncryptAddCommand.php
  3. +10
    -14
      app/Commands/LetsEncryptInstallCommand.php
  4. +58
    -0
      app/Commands/LetsEncryptRemoveCommand.php

+ 0
- 126
app/Commands/Certs.php View File

@ -1,126 +0,0 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Facades\File;
use App\Facades\Install;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
use PhpSchool\CliMenu\Style\SelectableStyle;
use Log;
use App\Facades\Nginx;
class NginxCertsCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'nginx:certs';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Create and Manage Nginx Vhosts';
/**
*
*
*/
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
var_dump(Nginx::getVhosts());
// create menu
//$main = $this->menu('Nginx Let\'s Encrypt');
//$main
// ->addLineBreak('-')
// ->open();
/***
$itemCallable = function(CliMenu $menu) {
// getting sites-available
$sitesAvailable = scandir('/etc/nginx/sites-available');
// getting sites-endabled
$sitesEnabled = scandir('/etc/nginx/sites-enabled');
foreach($sitesAvailable as $site) {
if ($site !== '.' && $site !== '..') {
// getting title
$title = str_replace('.conf', '', $site);
$checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($site, $title) {
// check status
if ($menu->getSelectedItem()->getChecked()) {
symlink('/etc/nginx/sites-available/'.$site, '/etc/nginx/sites-enabled/'.$site);
$status = 'enabled';
} else {
unlink('/etc/nginx/sites-enabled/'.$site);
$status = 'disabled';
}
// starting
exec('service nginx restart');
exec('service nginx status', $output);
if (strpos(implode(' ', $output), 'active') !== false) {
$menu->confirm($title.' is '.$status.'!')->display('OK!');
} else {
$menu->confirm('Error! Something not working!')->display('OK!');
}
});
// adding checkbox
if (in_array($site, $sitesEnabled)) {
$checkbox->setChecked(true);
}
$menuMain
->addSubmenu($title, function(CliMenuBuilder $builder) use ($site, $title, $checkbox) {
$builder->setTitle("Nginx > $title")
->addItem('edit', function(CliMenu $menu) use ($site, $title) {
system('nano /etc/nginx/sites-available/'.$site.' > `tty`');
})
->addItem('delete', function(CliMenu $menu) use ($site) {
if (file_exists('/etc/nginx/sites-enabled/'.$site)) {
$menu->confirm('Error! Please disable '.$title.' first!')->display('OK!');
} else {
unlink('/etc/nginx/sites-available/'.$site);
$menu->confirm("$site is deleted!")->display('OK!');
}
})
->addLineBreak('-')
->addMenuItem($checkbox)
->addLineBreak('-');
});
}
}
};*/
}
}

+ 40
- 0
app/Commands/LetsEncryptAddCommand.php View File

@ -0,0 +1,40 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
/**
*
*
*
*/
class LetsEncryptAddCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'lets-encrypt:add {domain*}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Get Certificates from LetsEncrypt';
/**
* Execute the console command.
*
* @return mixed
*
*/
public function handle()
{
$domains = $this->argument('domain');
exec('certbot --nginx '.implode('-d ', $domains), $output);
}
}

app/Commands/CertsInstallCommand.php → app/Commands/LetsEncryptInstallCommand.php View File

@ -4,12 +4,12 @@ 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 App\Facades\Install;
use Log;
/**
*
*
*
*/
class LetsEncryptInstallCommand extends Command class LetsEncryptInstallCommand extends Command
{ {
/** /**
@ -24,7 +24,7 @@ class LetsEncryptInstallCommand extends Command
* *
* @var string * @var string
*/ */
protected $description = 'Install LetsEncrypt';
protected $description = 'Install LetsEncrypt for nginx';
/** /**
* Execute the console command. * Execute the console command.
@ -33,7 +33,7 @@ class LetsEncryptInstallCommand extends Command
*/ */
public function handle() public function handle()
{ {
$this->info('LetsEncrypt install...');
$this->info('LetsEncrypt installing...');
exec('apt update 2>&1'); exec('apt update 2>&1');
exec('apt-get install software-properties-common 2>&1'); exec('apt-get install software-properties-common 2>&1');
@ -43,13 +43,9 @@ class LetsEncryptInstallCommand extends Command
exec('apt-get install certbot python3-certbot-nginx 2>&1'); exec('apt-get install certbot python3-certbot-nginx 2>&1');
if (Install::isReady('certbot')) { if (Install::isReady('certbot')) {
// get status of nginx
exec('nginx -v 2>&1', $output);
$status = "$output[0] installed";
$this->info($status);
Log::info($status);
$this->info("Success!");
} else {
$this->error("Failed! Please check log-file!");
} }
} }
} }

+ 58
- 0
app/Commands/LetsEncryptRemoveCommand.php View File

@ -0,0 +1,58 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
/**
*
*
*
*/
class LetsEncryptRemoveCommand extends Command
{
const CERT_DIR = '/etc/certbot/';
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'lets-encrypt:remove {domain*}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Remove Certificates from LetsEncrypt';
/**
* Execute the console command.
*
* @return mixed
*
*/
public function handle()
{
$domains = $this->argument('domain');
foreach($domains as $domain) {
$archive = self::CERT_DIR.'archive/'.$domain;
$live = self::CERT_DIR.'live/'.$domain;
$renewal = self::CERT_DIR.'renewal/'.$domain;
rmdir($archiv);
rmdir($live);
rmdir($renewal);
if (file_exists($archiv) && file_exists($live) && file_exists($renewal)) {
$this->info('Certifikate for '.$domain.' ...deleted!');
} else {
$this->error('Certifikate for '.$domain.' ...not deleted!');
}
}
}
}

Loading…
Cancel
Save