From b8bd71e26507b0eb7400de77ee93963edd74b476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Brzuchalski?= Date: Mon, 13 Jun 2016 15:03:18 +0200 Subject: [PATCH] Extracted NginxConfigrator library with feature plan for CLI manipulation --- .gitignore | 16 +----- README.md | 12 ++--- bin/ngxconf | 35 +++++++++++++ composer.json | 10 +++- src/Builder.php | 22 +++------ src/Command/AddLocationCommand.php | 47 ++++++++++++++++++ src/Command/AddServerCommand.php | 55 +++++++++++++++++++++ src/Command/AddUpstreamCommand.php | 22 +++++++++ src/Command/AddUpstreamServerCommand.php | 23 +++++++++ src/Command/BaseCommand.php | 55 +++++++++++++++++++++ src/Command/RemoveLocationCommand.php | 23 +++++++++ src/Command/RemoveServerCommand.php | 52 +++++++++++++++++++ src/Command/RemoveUpstream.php | 22 +++++++++ src/Command/RemoveUpstreamServerCommand.php | 23 +++++++++ 14 files changed, 379 insertions(+), 38 deletions(-) create mode 100755 bin/ngxconf create mode 100644 src/Command/AddLocationCommand.php create mode 100644 src/Command/AddServerCommand.php create mode 100644 src/Command/AddUpstreamCommand.php create mode 100644 src/Command/AddUpstreamServerCommand.php create mode 100644 src/Command/BaseCommand.php create mode 100644 src/Command/RemoveLocationCommand.php create mode 100644 src/Command/RemoveServerCommand.php create mode 100644 src/Command/RemoveUpstream.php create mode 100644 src/Command/RemoveUpstreamServerCommand.php diff --git a/.gitignore b/.gitignore index bfc3e08..2666558 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,7 @@ -/app/config/parameters.yml -/build/ -/var/* -!/var/.gitkeep /vendor/ -/web/bundles/ /.sonar/ /.idea/ /bin/* -!/bin/console -!/bin/symfony_requirements +!/bin/ngxconf /tests/coverage/* !tests/coverage/.gitkeep -/tests/pdepend/* -!tests/pdepend/.gitkeep -/docker/rebuild/build/* -!/docker/rebuild/build/.gitkeep -/docker/rebuild/partials/* -!/docker/rebuild/partials/.gitkeep -/docker/rebuild/resources/* -!/docker/rebuild/resources/.gitkeep \ No newline at end of file diff --git a/README.md b/README.md index aa3346e..36a8087 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ PHP Library for NGINX configuration parser/generator ## Features This library can parse and generate NGINX configuration files. +In near future will provide CLI commands for NGINX configuration. ## Installation @@ -36,8 +37,6 @@ This library requires *PHP* in `~7` version. Parsing configuration string: ```php - 0) { Generating configuration string: ```php -addServerNode(80); -$server->append(new Directive('error_log', [new Param('/var/log/nginx/error.log'), new Param('debug')])); +$server->append(new Directive('error_log', [ + new Param('/var/log/nginx/error.log'), + new Param('debug'), +])); $server->append(new Location(new Param('/test'), null, [ new Directive('error_page', [new Param('401'), new Param('@unauthorized')]), new Directive('set', [new Param('$auth_user'), new Literal('none')]), @@ -175,7 +175,6 @@ server { There are also methods to read and dump file: ```php - use Madkom\NginxConfigurator\Builder; use Madkom\NginxConfigurator\Config\Location; use Madkom\NginxConfigurator\Config\Server; @@ -207,6 +206,7 @@ $builder->dumpFile('generated.conf'); ## TODO * [ ] Implement comments parsing +* [ ] Implement commands for config manipulation from CLI ## License diff --git a/bin/ngxconf b/bin/ngxconf new file mode 100755 index 0000000..b6075d9 --- /dev/null +++ b/bin/ngxconf @@ -0,0 +1,35 @@ +#!/usr/bin/env php +add(new AddServerCommand()); +$app->add(new RemoveServerCommand()); +$app->add(new AddLocationCommand()); +$app->add(new RemoveLocationCommand()); +$app->add(new AddUpstreamServerCommand()); +$app->add(new RemoveUpstreamServerCommand()); +$app->run(); diff --git a/composer.json b/composer.json index a99bad2..05d8b1a 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,15 @@ }, "require-dev": { "phpspec/phpspec": "^2.5", - "phpunit/phpunit": "~4" + "phpunit/phpunit": "~4", + "knplabs/phpspec-welldone-extension": "dev-master", + "henrikbjorn/phpspec-code-coverage": "1.0.*", + "squizlabs/php_codesniffer": "^2.3", + "phpunit/phpcov": "*", + "jakub-onderka/php-parallel-lint": "0.*", + "jakub-onderka/php-console-highlighter": "0.*", + "satooshi/php-coveralls": "dev-master", + "clover/dump": "dev-master" }, "repositories": [ { diff --git a/src/Builder.php b/src/Builder.php index 4bfbe00..ae66a72 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -10,6 +10,7 @@ namespace Madkom\NginxConfigurator; use Madkom\NginxConfigurator\Config\Server; use Madkom\NginxConfigurator\Config\Upstream; use Madkom\NginxConfigurator\Node\Directive; +use Madkom\NginxConfigurator\Node\Node; use Madkom\NginxConfigurator\Node\Param; use Madkom\NginxConfigurator\Node\RootNode; @@ -53,25 +54,14 @@ class Builder } /** - * @param Server $server - * @return Server + * @param Node $node + * @return Node */ - public function appendServerNode(Server $server) : Server + public function append(Node $node) : Node { - $this->rootNode->append($server); + $this->rootNode->append($node); - return $server; - } - - /** - * @param Upstream $upstream - * @return Upstream - */ - public function appendUpstreamNode(Upstream $upstream) : Upstream - { - $this->rootNode->append($upstream); - - return $upstream; + return $node; } /** diff --git a/src/Command/AddLocationCommand.php b/src/Command/AddLocationCommand.php new file mode 100644 index 0000000..a70462c --- /dev/null +++ b/src/Command/AddLocationCommand.php @@ -0,0 +1,47 @@ +setName('location:add'); + $this->setDescription("Adds location context and configuration"); + $this->addArgument('name', InputArgument::OPTIONAL, 'Server hostname:port', 'localhost:80'); + $this->addOption('internal', null, InputOption::VALUE_NONE, 'Adds internal directive'); + $this->addOption('proxy_pass', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_pass url (eg. http://proxy/)'); + $this->addOption('proxy_bind', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_bind directive url or variable (eg. $server_addr)'); + $this->addOption('proxy_redirect', null, InputOption::VALUE_OPTIONAL ^ InputOption::VALUE_IS_ARRAY, 'Adds proxy_redirect directive (eg. http://$host or https://$host)'); + $this->addOption('proxy_set_header', null, InputOption::VALUE_OPTIONAL ^ InputOption::VALUE_IS_ARRAY, 'Adds proxy_set_header directive (eg. "Content-Type: text/html"'); + $this->addOption('proxy_pass_request_body', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_pass_requeest_body directive (on|off)', 'on'); + +// // new Directive('internal'), +// new Directive('expires', [new Param('-1')]), +// new Directive('proxy_pass', [new Param('http://172.17.0.1:7777')]), +// new Directive('proxy_bind', [new Param('$server_addr')]), +// new Directive('proxy_redirect', [new Param('http://$host'), new Param('https://$host')]), +// new Directive('proxy_set_header', [new Param('Content-Length'), new Literal("")]), +// new Directive('proxy_pass_request_body', [new Param('off')]), + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + + $proxy_pass = $input->getOption('proxy_pass'); + + } +} diff --git a/src/Command/AddServerCommand.php b/src/Command/AddServerCommand.php new file mode 100644 index 0000000..b6d8da9 --- /dev/null +++ b/src/Command/AddServerCommand.php @@ -0,0 +1,55 @@ + + */ +class AddServerCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('server:add'); + $this->setDescription("Adds server context and configuration of port and name"); + $this->addArgument('name', InputArgument::OPTIONAL, 'Server hostname:port', 'localhost:80'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $filename = $input->getOption('file'); + $config = $this->getConfig($input); + + list($name, $port) = explode(':', $input->getArgument('name') . ':80'); + + $listenIPv4 = new Directive('listen', [new Param($port)]); + $listenIPv6 = new Directive('listen', [new Param("[::]:{$port}"), new Param('default'), new Param('ipv6only=on')]); + + // TODO: Find server by name + $server = new Server([$listenIPv4, $listenIPv6]); + if ($name != 'localhost' && !empty($name)) { + $server->append(new Directive('server_name', [new Param($name)])); + } + $config->append($server); + + $builder = new Builder(); + $builder->appendServerNode($server); + $builder->dumpFile($filename); + } +} diff --git a/src/Command/AddUpstreamCommand.php b/src/Command/AddUpstreamCommand.php new file mode 100644 index 0000000..6e1ac38 --- /dev/null +++ b/src/Command/AddUpstreamCommand.php @@ -0,0 +1,22 @@ + + */ +class AddUpstreamCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('upstream:add'); + } +} diff --git a/src/Command/AddUpstreamServerCommand.php b/src/Command/AddUpstreamServerCommand.php new file mode 100644 index 0000000..cf9eefa --- /dev/null +++ b/src/Command/AddUpstreamServerCommand.php @@ -0,0 +1,23 @@ + + */ +class AddUpstreamServerCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('upstream:server:add'); + $this->setDescription("Adds server directive to upstream context and configuration"); + } +} \ No newline at end of file diff --git a/src/Command/BaseCommand.php b/src/Command/BaseCommand.php new file mode 100644 index 0000000..a9c68f1 --- /dev/null +++ b/src/Command/BaseCommand.php @@ -0,0 +1,55 @@ + + */ +abstract class BaseCommand extends Command +{ + protected function configure() + { + $this->addOption('file', 'f', InputOption::VALUE_OPTIONAL, 'Output filename', 'php://stdout'); + } + + /** + * @param InputInterface $input + * @return RootNode + * @throws Exception + */ + protected function getConfig(InputInterface $input) : RootNode + { + $filename = $input->getOption('file'); + if ($filename != 'php://stdout' && !file_exists($filename)) { + @touch($filename); + } + if ($filename != 'php://stdout' && file_exists($filename)) { + if (!is_writable($filename)) { + throw new Exception('Given filename is not writable!'); + } + } + if ($filename != 'php://stdout' && file_exists($filename)) { + $parser = new Parser(); + return $parser->parseFile($filename); + } + + return new RootNode(); + } +} diff --git a/src/Command/RemoveLocationCommand.php b/src/Command/RemoveLocationCommand.php new file mode 100644 index 0000000..3df1731 --- /dev/null +++ b/src/Command/RemoveLocationCommand.php @@ -0,0 +1,23 @@ + + */ +class RemoveLocationCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('location:remove'); + $this->setDescription("Remove location context and it's configuration"); + } +} diff --git a/src/Command/RemoveServerCommand.php b/src/Command/RemoveServerCommand.php new file mode 100644 index 0000000..6c6cec0 --- /dev/null +++ b/src/Command/RemoveServerCommand.php @@ -0,0 +1,52 @@ + + */ +class RemoveServerCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('server:remove'); + $this->setDescription("Removes server context and it's configuration"); + $this->addArgument('name', InputArgument::OPTIONAL, 'Server hostname:port', 'localhost:80'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $filename = $input->getOption('file'); + $builder = $this->getConfig($input); + + list($name, $port) = explode(':', $input->getArgument('name') . ':80'); + + $listenIPv4 = new Directive('listen', [new Param($port)]); + $listenIPv6 = new Directive('listen', [new Param("[::]:{$port}"), new Param('default'), new Param('ipv6only=on')]); + + $server = new Server([$listenIPv4, $listenIPv6]); + if ($name != 'localhost' && !empty($name)) { + $server->append(new Directive('server_name', [new Param($name)])); + } + + $builder->appendServerNode($server); + $builder->dumpFile($filename); + } +} diff --git a/src/Command/RemoveUpstream.php b/src/Command/RemoveUpstream.php new file mode 100644 index 0000000..4128f8b --- /dev/null +++ b/src/Command/RemoveUpstream.php @@ -0,0 +1,22 @@ + + */ +class RemoveUpstreamCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('upstream:remove'); + } +} diff --git a/src/Command/RemoveUpstreamServerCommand.php b/src/Command/RemoveUpstreamServerCommand.php new file mode 100644 index 0000000..98aa560 --- /dev/null +++ b/src/Command/RemoveUpstreamServerCommand.php @@ -0,0 +1,23 @@ + + */ +class RemoveUpstreamServerCommand extends BaseCommand +{ + protected function configure() + { + parent::configure(); + $this->setName('upstream:server:remove'); + $this->setDescription("Removes server directive from upstream context and configuration"); + } +} \ No newline at end of file