From 794174f0641dbce203122e90e948625c91f65aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Sat, 1 Aug 2020 02:31:31 +0200 Subject: [PATCH] adding #2 --- app/Commands/LetsEncryptInstallCommand.php | 55 ++++++++++++++++++++ app/Commands/MonitoringStateCommand.php | 2 +- app/Commands/NginxVhostsCommand.php | 58 +++++++++++++++++----- app/Helpers/NginxVhostHelper.php | 41 +++++++++------ 4 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 app/Commands/LetsEncryptInstallCommand.php diff --git a/app/Commands/LetsEncryptInstallCommand.php b/app/Commands/LetsEncryptInstallCommand.php new file mode 100644 index 0000000..2f855d9 --- /dev/null +++ b/app/Commands/LetsEncryptInstallCommand.php @@ -0,0 +1,55 @@ +info('LetsEncrypt install...'); + + exec('apt update 2>&1'); + exec('apt-get install software-properties-common 2>&1'); + exec('add-apt-repository universe 2>&1'); + exec('add-apt-repository ppa:certbot/certbot 2>&1'); + exec('apt-get update 2>&1'); + exec('apt-get install certbot python3-certbot-nginx 2>&1'); + + if (Install::isReady('nginx')) { + + // get status of nginx + exec('nginx -v 2>&1', $output); + $status = "$output[0] installed"; + + $this->info($status); + Log::info($status); + } + } +} diff --git a/app/Commands/MonitoringStateCommand.php b/app/Commands/MonitoringStateCommand.php index 896e2b9..b6c2c46 100644 --- a/app/Commands/MonitoringStateCommand.php +++ b/app/Commands/MonitoringStateCommand.php @@ -57,6 +57,6 @@ class MonitoringStateCommand extends Command } } - echo (implode("\n", $upgrades)); + $result = implode("\n", $upgrades); } } \ No newline at end of file diff --git a/app/Commands/NginxVhostsCommand.php b/app/Commands/NginxVhostsCommand.php index a995ee5..ab4cf47 100644 --- a/app/Commands/NginxVhostsCommand.php +++ b/app/Commands/NginxVhostsCommand.php @@ -112,7 +112,7 @@ class NginxVhostsCommand extends Command } /** - * create Configuration, add for each template a submenu + * create Configuration, add for each template a submenu * * * @param array $template @@ -121,7 +121,9 @@ class NginxVhostsCommand extends Command */ private function createConfiguration($template, $blade) { - $menu = function(CliMenuBuilder $builder) use ($template, $blade) + $self = $this; + + $menu = function(CliMenuBuilder $builder) use ($template, $blade, $self) { $configuration = [ 'domain' => '', @@ -172,9 +174,15 @@ class NginxVhostsCommand extends Command ->addMenuItem($checkboxSSL) ->addMenuItem($checkboxRedirect) ->addLineBreak('-') - ->addItem('save', function() use (&$configuration, $template, $blade) { + ->addItem('save', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) { $content = $blade->render($template['name'], $configuration); file_put_contents('/etc/nginx/sites-available/'.$configuration['domain'].'.conf', $content); + + $parent = $menu->getParent(); + $menu->closeThis(); + + $mainmenu = $parent->getParent(); + $mainmenu->open(); }) ->addLineBreak('-'); }; @@ -182,6 +190,13 @@ class NginxVhostsCommand extends Command return $menu; } + /** + * + * + * + */ + + /** * create submenu for vhost * @@ -192,7 +207,7 @@ class NginxVhostsCommand extends Command */ private function vhost($vhost) { - $menu = function(CliMenuBuilder $builder) use ($vhost) + $currentMenu = function(CliMenuBuilder $builder) use ($vhost) { $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) { @@ -234,6 +249,14 @@ class NginxVhostsCommand extends Command } else { unlink('/etc/nginx/sites-available/'.$vhost['file']); $menu->confirm($vhost['domain'].' is deleted!')->display('OK!'); + + // get + $parent = $menu->getParent(); + $menu->closeThis(); + + // remove current vhost from mainmenu + $parent->removeItem($parent->getSelectedItem()); + $parent->open(); } }) ->addLineBreak('-') @@ -241,6 +264,24 @@ class NginxVhostsCommand extends Command ->addLineBreak('-'); }; + return $currentMenu; + } + + /** + * + * + */ + protected function updateVhosts($menu) + { + // getting vhosts + $vhosts = NginxVhost::find(); + + // add submenu for each vhost + foreach($vhosts as $vhost) { + $submenuEditVhost = $this->vhost($vhost); + $menu->addSubMenu($vhost['domain'], $submenuEditVhost); + } + return $menu; } @@ -260,14 +301,7 @@ class NginxVhostsCommand extends Command ->addSubMenu('add', $submenuSelectTemplate) ->addLineBreak('-'); - // getting vhosts - $vhosts = NginxVhost::find(); - - // add submenu for each vhost - foreach($vhosts as $vhost) { - $submenuEditVhost = $this->vhost($vhost); - $main->addSubMenu($vhost['domain'], $submenuEditVhost); - } + $main = $this->updateVhosts($main); $main->addLineBreak('-'); $main->open(); diff --git a/app/Helpers/NginxVhostHelper.php b/app/Helpers/NginxVhostHelper.php index d5fe23f..910f86b 100644 --- a/app/Helpers/NginxVhostHelper.php +++ b/app/Helpers/NginxVhostHelper.php @@ -91,6 +91,31 @@ class NginxVhostHelper return $result; } + private function getVhost($filename, $enabled) + { + // getting full path + $path = self::SITES_AVAILABLE.'/'.$filename; + + // getting certificates from a configuration + $certificate = $this->getCertificate($path); + + $result = array_merge([ + 'domain' => str_replace('.conf', '', $filename), + 'path' => $path, + 'file' => $filename, + 'enabled' => in_array($filename, $enabled), + ], $certificate); + + return $result; + } + + public function findOneByFilename($filename) + { + // getting enabled + $enabled = $this->getSitesEnabled(); + return $this->getVhost($filename, $enabled); + } + /** * getting vhosts * @@ -108,21 +133,7 @@ class NginxVhostHelper foreach($available as $filename) { if (!in_array($filename, self::IGNORE_FILES)) { - - // getting full path - $path = self::SITES_AVAILABLE.'/'.$filename; - - // getting certificates from a configuration - $certificate = $this->getCertificate($path); - - $result = array_merge([ - 'domain' => str_replace('.conf', '', $filename), - 'path' => $path, - 'file' => $filename, - 'enabled' => in_array($filename, $enabled), - ], $certificate); - - $results[] = $result; + $results[] = $this->getVhost($filename, $enabled); } }