From b10d046a17cdc4ee3a4e71498bbeff3b8f18bf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Mon, 3 Aug 2020 20:26:50 +0200 Subject: [PATCH] adding #8 --- app/Commands/NginxVhostsCommand.php | 16 ++--- app/Factories/NginxVhostFactory.php | 94 +++++++++++++++++++---------- 2 files changed, 70 insertions(+), 40 deletions(-) diff --git a/app/Commands/NginxVhostsCommand.php b/app/Commands/NginxVhostsCommand.php index e43954f..87808b0 100644 --- a/app/Commands/NginxVhostsCommand.php +++ b/app/Commands/NginxVhostsCommand.php @@ -103,7 +103,7 @@ class NginxVhostsCommand extends Command $blade = new Blade(base_path().self::TEMPLATES_DIR, base_path().'/storage/cache'); $builder - ->setTitle('Nginx > add') + ->setTitle('Nginx > Add') ->setGoBackButtonText('Back'); foreach($templates as $template) { @@ -155,14 +155,14 @@ class NginxVhostsCommand extends Command $checkboxRedirect->setChecked($configuration['redirect_www']); $builder - ->setTitle('Nginx Vhosts > add > '.$template['name']) + ->setTitle('Nginx Vhosts > Add > '.$template['name']) // input domain ->addItem('domain', function(CliMenu $menu) use (&$configuration) { $result = $menu->askText()->ask(); $configuration['domain'] = $result->fetch(); - $menu->getSelectedItem()->setText('domain -> '.$result->fetch()); + $menu->getSelectedItem()->setText('domain: '.$result->fetch()); $menu->redraw(); }) @@ -171,7 +171,7 @@ class NginxVhostsCommand extends Command $result = $menu->askText()->ask(); $configuration['root'] = $result->fetch(); - $menu->getSelectedItem()->setText('root -> '.$result->fetch()); + $menu->getSelectedItem()->setText('root: '.$result->fetch()); $menu->redraw(); }) @@ -190,8 +190,8 @@ class NginxVhostsCommand extends Command ->addMenuItem($checkboxRedirect) ->addLineBreak('-') - // create - ->addItem('create', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) { + // create + ->addItem('Create', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) { // render configuration $content = $blade->render($template['name'], $configuration); @@ -237,12 +237,14 @@ class NginxVhostsCommand extends Command // add Submenu for select templates ->addLineBreak('-') - ->addSubMenu('add', $submenuSelectTemplate); + ->addSubMenu('Add', $submenuSelectTemplate); $mainmenu->addLineBreak('-'); // apperance $mainmenu->setWidth($mainmenu->getTerminal()->getWidth()); + $mainmenu->setBorderLeftWidth(4); + $mainmenu->setBorderColour('magenta'); $mainmenu->setMargin(2); $mainmenu = $mainmenu->build(); diff --git a/app/Factories/NginxVhostFactory.php b/app/Factories/NginxVhostFactory.php index 3edfb31..f0f1688 100644 --- a/app/Factories/NginxVhostFactory.php +++ b/app/Factories/NginxVhostFactory.php @@ -76,12 +76,14 @@ class NginxVhostFactory ->setUnselectedMarker(' '); }); + $log = ''; + // create checkbox for disable / enabled - $checkbox = self::createVhostCheckbox($vhost); + $checkbox = self::createVhostCheckbox($vhost, $log); - // adding checkbox + // if vhost is enabled if ($vhost['enabled']) { - $checkbox->setChecked(true); + $checkbox->setChecked(); } $builder @@ -96,34 +98,40 @@ class NginxVhostFactory // delete configuration ->addItem('delete', function(CliMenu $menu) use ($vhost) { - // input domain for confirmation - $result = $menu->askText() - ->setPromptText('Enter the domain name as confirmation: '.$vhost['domain'].' / [ESC] for Cancel') - ->ask(); + if ($vhost['enabled'] === true) { + $menu->flash('Please disable first!')->display(); + } else { - // if result matching delete vhost an close menu - if ($result->fetch() === $vhost['domain']) { - unlink('/etc/nginx/sites-available/'.$vhost['file']); - $menu->confirm($vhost['domain'].' is deleted!')->display('OK!'); + // input domain for confirmation + $result = $menu->askText() + ->setPromptText('Enter the domain name as confirmation: '.$vhost['domain'].' / [ESC] for Cancel') + ->ask(); - // get - $parent = $menu->getParent(); - $menu->closeThis(); + // if result matching delete vhost an close menu + if ($result->fetch() === $vhost['domain']) { + unlink('/etc/nginx/sites-available/'.$vhost['file']); + $menu->confirm($vhost['domain'].' is deleted!')->display('OK!'); - // remove current vhost from mainmenu - $parent->removeItem($parent->getSelectedItem()); - $parent->open(); + // get + $parent = $menu->getParent(); + $menu->closeThis(); - // cancel input - } else if (empty($result->fetch())) { - $menu->flash('Cancel')->display(); + // remove current vhost from mainmenu + $parent->removeItem($parent->getSelectedItem()); + $parent->open(); + + // cancel input + } else if (empty($result->fetch())) { + $menu->flash('Cancel')->display(); + + // if domain not matching + } else { + $menu->flash('Not matching! Domain not deleted!')->display(); + } - // if domain not matching - } else { - $menu->flash('Not matching! Domain not deleted!')->display(); } - }, false, $vhost['enabled']) + }) ->addLineBreak('-') ->addMenuItem($checkbox) ->addLineBreak('-'); @@ -159,27 +167,47 @@ class NginxVhostFactory private function createVhostCheckbox($vhost) { // create checkbox for enabled / disabled - $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) { + $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use (&$vhost, &$log) { // check status if ($menu->getSelectedItem()->getChecked()) { symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']); - $status = 'enabled'; + exec('nginx -c /etc/nginx/nginx.conf -t 2>&1', $output); + + if ($output) { + unlink('/etc/nginx/sites-enabled/'.$vhost['file']); + $menu->confirm('Error! Configuration not Working!')->display('OK!'); + + // @TODO: find a way to show logs, https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp/issues/15 + // implode(PHP_EOL, $output); + + $vhost['enabled'] = false; + $status = 'disabled'; + } else { + + // restart + exec('service nginx restart'); + $menu->confirm('Success! ')->display('OK!'); + + $vhost['enabled'] = true; + $status = 'enabled'; + + } } else { unlink('/etc/nginx/sites-enabled/'.$vhost['file']); + + $vhost['enabled'] = false; $status = 'disabled'; } - // restart - exec('service nginx restart'); - exec('service nginx status', $output); - - if (strpos(implode(' ', $output), 'active') !== false) { - $menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!'); + if ($vhost['enabled']) { + $menu->getSelectedItem()->setChecked(); } else { - $menu->confirm('Error! Something not working!')->display('OK!'); + $menu->getSelectedItem()->setUnchecked(); } + $menu->redraw(); + }); return $checkbox;