mirror of
https://github.com/envoyr/nginx-configurator.git
synced 2026-04-28 04:06:18 +00:00
Extracted NginxConfigrator library with feature plan for CLI manipulation
This commit is contained in:
16
.gitignore
vendored
16
.gitignore
vendored
@@ -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
|
||||
12
README.md
12
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
|
||||
<?php
|
||||
|
||||
use Madkom\NginxConfigurator\Builder;
|
||||
use Madkom\NginxConfigurator\Config\Server;
|
||||
use Madkom\NginxConfigurator\Parser;
|
||||
@@ -92,8 +91,6 @@ if (count($defaultServers) > 0) {
|
||||
Generating configuration string:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Madkom\NginxConfigurator\Builder;
|
||||
use Madkom\NginxConfigurator\Config\Location;
|
||||
use Madkom\NginxConfigurator\Node\Directive;
|
||||
@@ -105,7 +102,10 @@ require __DIR__ . '/../vendor/autoload.php';
|
||||
$builder = new Builder();
|
||||
|
||||
$server = $builder->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
|
||||
|
||||
|
||||
35
bin/ngxconf
Executable file
35
bin/ngxconf
Executable file
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
namespace Madkom\NginxConfigurator;
|
||||
|
||||
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
|
||||
// Called from local git clone.
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
|
||||
// Called from your project's vendor dir.
|
||||
require __DIR__ . '/../../../autoload.php';
|
||||
} else {
|
||||
die(
|
||||
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
|
||||
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
|
||||
'php composer.phar install' . PHP_EOL
|
||||
);
|
||||
}
|
||||
|
||||
use Madkom\NginxConfigurator\Command\AddLocationCommand;
|
||||
use Madkom\NginxConfigurator\Command\AddServerCommand;
|
||||
use Madkom\NginxConfigurator\Command\AddUpstreamServerCommand;
|
||||
use Madkom\NginxConfigurator\Command\RemoveLocationCommand;
|
||||
use Madkom\NginxConfigurator\Command\RemoveServerCommand;
|
||||
use Madkom\NginxConfigurator\Command\RemoveUpstreamServerCommand;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
$app = new Application('ngxconf', '1.0.0');
|
||||
$app->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();
|
||||
@@ -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": [
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
47
src/Command/AddLocationCommand.php
Normal file
47
src/Command/AddLocationCommand.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:25
|
||||
*/
|
||||
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class AddLocationCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->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 <comment>(eg. http://proxy/)</comment>');
|
||||
$this->addOption('proxy_bind', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_bind directive url or variable <comment>(eg. $server_addr)</comment>');
|
||||
$this->addOption('proxy_redirect', null, InputOption::VALUE_OPTIONAL ^ InputOption::VALUE_IS_ARRAY, 'Adds proxy_redirect directive <comment>(eg. http://$host or https://$host)</comment>');
|
||||
$this->addOption('proxy_set_header', null, InputOption::VALUE_OPTIONAL ^ InputOption::VALUE_IS_ARRAY, 'Adds proxy_set_header directive <comment>(eg. "Content-Type: text/html"</comment>');
|
||||
$this->addOption('proxy_pass_request_body', null, InputOption::VALUE_OPTIONAL, 'Adds proxy_pass_requeest_body directive <comment>(on|off)</comment>', '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');
|
||||
|
||||
}
|
||||
}
|
||||
55
src/Command/AddServerCommand.php
Normal file
55
src/Command/AddServerCommand.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 13:38
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
use Madkom\NginxConfigurator\Builder;
|
||||
use Madkom\NginxConfigurator\Config\Server;
|
||||
use Madkom\NginxConfigurator\Node\Directive;
|
||||
use Madkom\NginxConfigurator\Node\Param;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Class AddServerCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
22
src/Command/AddUpstreamCommand.php
Normal file
22
src/Command/AddUpstreamCommand.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:25
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
/**
|
||||
* Class AddUpstreamCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
class AddUpstreamCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->setName('upstream:add');
|
||||
}
|
||||
}
|
||||
23
src/Command/AddUpstreamServerCommand.php
Normal file
23
src/Command/AddUpstreamServerCommand.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:31
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
/**
|
||||
* Class AddUpstreamServerCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
class AddUpstreamServerCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->setName('upstream:server:add');
|
||||
$this->setDescription("Adds server directive to upstream context and configuration");
|
||||
}
|
||||
}
|
||||
55
src/Command/BaseCommand.php
Normal file
55
src/Command/BaseCommand.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:01
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
use Exception;
|
||||
use Madkom\NginxConfigurator\Builder;
|
||||
use Madkom\NginxConfigurator\Node\Node;
|
||||
use Madkom\NginxConfigurator\Node\RootNode;
|
||||
use Madkom\NginxConfigurator\Parser;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
/**
|
||||
* Class BaseCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
23
src/Command/RemoveLocationCommand.php
Normal file
23
src/Command/RemoveLocationCommand.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:25
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
/**
|
||||
* Class RemoveLocationCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
class RemoveLocationCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->setName('location:remove');
|
||||
$this->setDescription("Remove location context and it's configuration");
|
||||
}
|
||||
}
|
||||
52
src/Command/RemoveServerCommand.php
Normal file
52
src/Command/RemoveServerCommand.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 13:38
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
use Madkom\NginxConfigurator\Builder;
|
||||
use Madkom\NginxConfigurator\Config\Server;
|
||||
use Madkom\NginxConfigurator\Node\Directive;
|
||||
use Madkom\NginxConfigurator\Node\Param;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Class RemoveServerCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
22
src/Command/RemoveUpstream.php
Normal file
22
src/Command/RemoveUpstream.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:25
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
/**
|
||||
* Class RemoveUpstreamCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
class RemoveUpstreamCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->setName('upstream:remove');
|
||||
}
|
||||
}
|
||||
23
src/Command/RemoveUpstreamServerCommand.php
Normal file
23
src/Command/RemoveUpstreamServerCommand.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: mbrzuchalski
|
||||
* Date: 10.06.16
|
||||
* Time: 14:31
|
||||
*/
|
||||
namespace Madkom\NginxConfigurator\Command;
|
||||
|
||||
/**
|
||||
* Class RemoveUpstreamServerCommand
|
||||
* @package Madkom\NginxConfigurator\Command
|
||||
* @author Michał Brzuchalski <m.brzuchalski@madkom.pl>
|
||||
*/
|
||||
class RemoveUpstreamServerCommand extends BaseCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->setName('upstream:server:remove');
|
||||
$this->setDescription("Removes server directive from upstream context and configuration");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user