diff --git a/bin/rr b/bin/rr index 6156730..fae58ed 100644 --- a/bin/rr +++ b/bin/rr @@ -12,6 +12,7 @@ declare(strict_types=1); use Spiral\RoadRunner\Console\DownloadProtocBinaryCommand; use Spiral\RoadRunner\Console\GetBinaryCommand; +use Spiral\RoadRunner\Console\MakeConfigCommand; use Spiral\RoadRunner\Console\VersionsCommand; use Spiral\RoadRunner\Version; @@ -70,5 +71,6 @@ $app = new Symfony\Component\Console\Application('RoadRunner CLI', Version::curr $app->add(new GetBinaryCommand()); $app->add(new VersionsCommand()); $app->add(new DownloadProtocBinaryCommand()); +$app->add(new MakeConfigCommand()); $app->run(); diff --git a/src/Configuration/Generator.php b/src/Configuration/Generator.php index 0abfaf6..76b3dc8 100644 --- a/src/Configuration/Generator.php +++ b/src/Configuration/Generator.php @@ -24,7 +24,7 @@ public function generate(Plugins $plugins): string { $this->collectSections($plugins->getPlugins()); - return $this->getHeaderComment() . PHP_EOL . Yaml::dump($this->getContent(), 10); + return Yaml::dump($this->getContent(), 10); } protected function getContent(): array @@ -57,23 +57,4 @@ protected function fromSection(SectionInterface $section): void $this->fromSection(new $required()); } } - - protected function getHeaderComment(): string - { - $comment = [ - '########################################################################################', - '# THIS IS SAMPLE OF THE CONFIGURATION #', - '# IT\'S NOT A DEFAULT CONFIGURATION, IT\'S JUST A SIMPLE SAMPLE #', - '# MORE DOCS CAN BE FOUND HERE: #', - '########################################################################################', - '', - '# Hint: RR will replace any config options using reference to environment variables,', - '# eg.: `option_key: ${ENVIRONMENT_VARIABLE_NAME}`.', - '', - '# Important: TCP port numbers for each plugin (rpc, http, etc) must be unique!', - '' - ]; - - return \implode(PHP_EOL, $comment); - } } diff --git a/src/Configuration/Section/Broadcast.php b/src/Configuration/Section/Broadcast.php index c7de66e..117f0d1 100644 --- a/src/Configuration/Section/Broadcast.php +++ b/src/Configuration/Section/Broadcast.php @@ -16,34 +16,34 @@ public function render(): array 'driver' => 'memory', 'config' => [] ], - 'default-redis' => [ - 'driver' => 'redis', - 'config' => [ - 'addrs' => [ - 'localhost:6379' - ], - 'master_name' => '', - 'username' => '', - 'password' => '', - 'db' => 0, - 'sentinel_password' => '', - 'route_by_latency' => false, - 'route_randomly' => false, - 'dial_timeout' => 0, - 'max_retries' => 1, - 'min_retry_backoff' => 0, - 'max_retry_backoff' => 0, - 'pool_size' => 0, - 'min_idle_conns' => 0, - 'max_conn_age' => 0, - 'read_timeout' => 0, - 'write_timeout' => 0, - 'pool_timeout' => 0, - 'idle_timeout' => 0, - 'idle_check_freq' => 0, - 'read_only' => false - ] - ] +// 'default-redis' => [ +// 'driver' => 'redis', +// 'config' => [ +// 'addrs' => [ +// 'localhost:6379' +// ], +// 'master_name' => '', +// 'username' => '', +// 'password' => '', +// 'db' => 0, +// 'sentinel_password' => '', +// 'route_by_latency' => false, +// 'route_randomly' => false, +// 'dial_timeout' => 0, +// 'max_retries' => 1, +// 'min_retry_backoff' => 0, +// 'max_retry_backoff' => 0, +// 'pool_size' => 0, +// 'min_idle_conns' => 0, +// 'max_conn_age' => 0, +// 'read_timeout' => 0, +// 'write_timeout' => 0, +// 'pool_timeout' => 0, +// 'idle_timeout' => 0, +// 'idle_check_freq' => 0, +// 'read_only' => false +// ] +// ] ] ]; } diff --git a/src/Configuration/Section/Fileserver.php b/src/Configuration/Section/Fileserver.php index e50038f..5b25643 100644 --- a/src/Configuration/Section/Fileserver.php +++ b/src/Configuration/Section/Fileserver.php @@ -16,24 +16,24 @@ public function render(): array 'calculate_etag' => true, 'weak' => false, 'stream_request_body' => true, - 'serve' => [ - [ - 'prefix' => '/foo', - 'root' => '../../../tests', - 'compress' => false, - 'cache_duration' => 10, - 'max_age' => 10, - 'bytes_range' => true - ], - [ - 'prefix' => '/foo/bar', - 'root' => '../../../tests', - 'compress' => false, - 'cache_duration' => 10, - 'max_age' => 10, - 'bytes_range' => true - ] - ] +// 'serve' => [ +// [ +// 'prefix' => '/foo', +// 'root' => '../../../tests', +// 'compress' => false, +// 'cache_duration' => 10, +// 'max_age' => 10, +// 'bytes_range' => true +// ], +// [ +// 'prefix' => '/foo/bar', +// 'root' => '../../../tests', +// 'compress' => false, +// 'cache_duration' => 10, +// 'max_age' => 10, +// 'bytes_range' => true +// ] +// ] ] ]; } diff --git a/src/Configuration/Section/Grpc.php b/src/Configuration/Section/Grpc.php index 70fb06e..514ecbb 100644 --- a/src/Configuration/Section/Grpc.php +++ b/src/Configuration/Section/Grpc.php @@ -12,31 +12,31 @@ public function render(): array { return [ self::NAME => [ - 'listen' => 'tcp://localhost:9001', + 'listen' => 'tcp://127.0.0.1:9001', 'proto' => [ 'first.proto', 'second.proto' ], - 'tls' => [ - 'key' => '', - 'cert' => '', - 'root_ca' => '', - 'client_auth_type' => 'no_client_certs' - ], - 'max_send_msg_size' => 50, - 'max_recv_msg_size' => 50, - 'max_connection_idle' => '0s', - 'max_connection_age' => '0s', - 'max_connection_age_grace' => '0s8h', - 'max_concurrent_streams' => 10, - 'ping_time' => '1s', - 'timeout' => '200s', - 'pool' => [ - 'num_workers' => 2, - 'max_jobs' => 0, - 'allocate_timeout' => '60s', - 'destroy_timeout' => 60 - ] +// 'tls' => [ +// 'key' => '', +// 'cert' => '', +// 'root_ca' => '', +// 'client_auth_type' => 'no_client_certs' +// ], +// 'max_send_msg_size' => 50, +// 'max_recv_msg_size' => 50, +// 'max_connection_idle' => '0s', +// 'max_connection_age' => '0s', +// 'max_connection_age_grace' => '0s8h', +// 'max_concurrent_streams' => 10, +// 'ping_time' => '1s', +// 'timeout' => '200s', +// 'pool' => [ +// 'num_workers' => 2, +// 'max_jobs' => 0, +// 'allocate_timeout' => '60s', +// 'destroy_timeout' => 60 +// ] ] ]; } diff --git a/src/Configuration/Section/Kv.php b/src/Configuration/Section/Kv.php index aac3b5b..9f11d4b 100644 --- a/src/Configuration/Section/Kv.php +++ b/src/Configuration/Section/Kv.php @@ -18,14 +18,14 @@ public function render(): array 'interval' => 60 ] ], - 'redis' => [ - 'driver' => 'redis', - 'config' => [ - 'addrs' => [ - 'localhost:6379' - ] - ] - ] +// 'redis' => [ +// 'driver' => 'redis', +// 'config' => [ +// 'addrs' => [ +// 'localhost:6379' +// ] +// ] +// ] ] ]; } diff --git a/src/Configuration/Section/Logs.php b/src/Configuration/Section/Logs.php index 24faf4f..36fdaf2 100644 --- a/src/Configuration/Section/Logs.php +++ b/src/Configuration/Section/Logs.php @@ -14,40 +14,40 @@ public function render(): array self::NAME => [ 'mode' => 'development', 'level' => 'debug', - 'encoding' => 'console', - 'line_ending' => '\n', - 'output' => 'stderr', - 'err_output' => 'stderr', - 'file_logger_options' => [ - 'log_output' => '/tmp/my.log', - 'max_size' => 100, - 'max_age' => 1, - 'max_backups' => 5, - 'compress' => false - ], - 'channels' => [ - 'http' => [ - 'mode' => 'development', - 'level' => 'panic', - 'encoding' => 'console', - 'output' => 'stdout', - 'err_output' => 'stderr' - ], - 'server' => [ - 'mode' => 'production', - 'level' => 'info', - 'encoding' => 'json', - 'output' => 'stdout', - 'err_output' => 'stdout' - ], - 'rpc' => [ - 'mode' => 'raw', - 'level' => 'debug', - 'encoding' => 'console', - 'output' => 'stderr', - 'err_output' => 'stdout' - ] - ] +// 'encoding' => 'console', +// 'line_ending' => '\n', +// 'output' => 'stderr', +// 'err_output' => 'stderr', +// 'file_logger_options' => [ +// 'log_output' => '/tmp/my.log', +// 'max_size' => 100, +// 'max_age' => 1, +// 'max_backups' => 5, +// 'compress' => false +// ], +// 'channels' => [ +// 'http' => [ +// 'mode' => 'development', +// 'level' => 'panic', +// 'encoding' => 'console', +// 'output' => 'stdout', +// 'err_output' => 'stderr' +// ], +// 'server' => [ +// 'mode' => 'production', +// 'level' => 'info', +// 'encoding' => 'json', +// 'output' => 'stdout', +// 'err_output' => 'stdout' +// ], +// 'rpc' => [ +// 'mode' => 'raw', +// 'level' => 'debug', +// 'encoding' => 'console', +// 'output' => 'stderr', +// 'err_output' => 'stdout' +// ] +// ] ] ]; } diff --git a/src/Configuration/Section/Metrics.php b/src/Configuration/Section/Metrics.php index 1564893..f059870 100644 --- a/src/Configuration/Section/Metrics.php +++ b/src/Configuration/Section/Metrics.php @@ -12,7 +12,7 @@ public function render(): array { return [ self::NAME => [ - 'address' => 'localhost:2112' + 'address' => '127.0.0.1:2112' ] ]; } diff --git a/src/Configuration/Section/Redis.php b/src/Configuration/Section/Redis.php index 4300f33..7def40b 100644 --- a/src/Configuration/Section/Redis.php +++ b/src/Configuration/Section/Redis.php @@ -15,26 +15,26 @@ public function render(): array 'addrs' => [ 'localhost:6379' ], - 'master_name' => '', - 'username' => '', - 'password' => '', - 'db' => 0, - 'sentinel_password' => '', - 'route_by_latency' => false, - 'route_randomly' => false, - 'dial_timeout' => 0, - 'max_retries' => 1, - 'min_retry_backoff' => 0, - 'max_retry_backoff' => 0, - 'pool_size' => 0, - 'min_idle_conns' => 0, - 'max_conn_age' => 0, - 'read_timeout' => 0, - 'write_timeout' => 0, - 'pool_timeout' => 0, - 'idle_timeout' => 0, - 'idle_check_freq' => 0, - 'read_only' => false +// 'master_name' => '', +// 'username' => '', +// 'password' => '', +// 'db' => 0, +// 'sentinel_password' => '', +// 'route_by_latency' => false, +// 'route_randomly' => false, +// 'dial_timeout' => 0, +// 'max_retries' => 1, +// 'min_retry_backoff' => 0, +// 'max_retry_backoff' => 0, +// 'pool_size' => 0, +// 'min_idle_conns' => 0, +// 'max_conn_age' => 0, +// 'read_timeout' => 0, +// 'write_timeout' => 0, +// 'pool_timeout' => 0, +// 'idle_timeout' => 0, +// 'idle_check_freq' => 0, +// 'read_only' => false ] ]; } diff --git a/src/Configuration/Section/Tcp.php b/src/Configuration/Section/Tcp.php index c122a21..773b597 100644 --- a/src/Configuration/Section/Tcp.php +++ b/src/Configuration/Section/Tcp.php @@ -15,18 +15,18 @@ public function render(): array 'servers' => [ 'server1' => [ 'addr' => '127.0.0.1:7778', - 'delimiter' => '\r\n', - 'read_buf_size' => 1 +// 'delimiter' => '\r\n', +// 'read_buf_size' => 1 ], - 'server2' => [ - 'addr' => '127.0.0.1:8811', - 'read_buf_size' => 10 - ], - 'server3' => [ - 'addr' => '127.0.0.1:8812', - 'delimiter' => '\r\n', - 'read_buf_size' => 1 - ] +// 'server2' => [ +// 'addr' => '127.0.0.1:8811', +// 'read_buf_size' => 10 +// ], +// 'server3' => [ +// 'addr' => '127.0.0.1:8812', +// 'delimiter' => '\r\n', +// 'read_buf_size' => 1 +// ] ], 'pool' => [ 'command' => '', diff --git a/src/Configuration/Section/Temporal.php b/src/Configuration/Section/Temporal.php index f98dcc6..a025b49 100644 --- a/src/Configuration/Section/Temporal.php +++ b/src/Configuration/Section/Temporal.php @@ -13,30 +13,30 @@ public function render(): array return [ self::NAME => [ 'address' => '127.0.0.1:7233', - 'cache_size' => 10000, - 'namespace' => 'default', - 'codec' => 'proto', - 'debug_level' => 2, - 'metrics' => [ - 'address' => '127.0.0.1:9091', - 'type' => 'summary', - 'prefix' => 'foobar' - ], - 'activities' => [ - 'debug' => false, - 'command' => 'php my-super-app.php', - 'num_workers' => 0, - 'max_jobs' => 64, - 'allocate_timeout' => '60s', - 'destroy_timeout' => '60s', - 'supervisor' => [ - 'watch_tick' => '1s', - 'ttl' => '0s', - 'idle_ttl' => '10s', - 'max_worker_memory' => 128, - 'exec_ttl' => '60s' - ] - ] +// 'cache_size' => 10000, +// 'namespace' => 'default', +// 'codec' => 'proto', +// 'debug_level' => 2, +// 'metrics' => [ +// 'address' => '127.0.0.1:9091', +// 'type' => 'summary', +// 'prefix' => 'foobar' +// ], +// 'activities' => [ +// 'debug' => false, +// 'command' => 'php my-super-app.php', +// 'num_workers' => 0, +// 'max_jobs' => 64, +// 'allocate_timeout' => '60s', +// 'destroy_timeout' => '60s', +// 'supervisor' => [ +// 'watch_tick' => '1s', +// 'ttl' => '0s', +// 'idle_ttl' => '10s', +// 'max_worker_memory' => 128, +// 'exec_ttl' => '60s' +// ] +// ] ] ]; } diff --git a/src/MakeConfigCommand.php b/src/MakeConfigCommand.php new file mode 100644 index 0000000..aafb98f --- /dev/null +++ b/src/MakeConfigCommand.php @@ -0,0 +1,71 @@ +location = new InstallationLocationOption($this); + } + + protected function configure(): void + { + $this->addOption( + 'plugin', + 'p', + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'Generate configuration with selected plugins.' + ); + + $this->addOption( + 'preset', + null, + InputOption::VALUE_OPTIONAL, + 'Generate configuration with plugins in a selected preset.' + ); + } + + /** + * {@inheritDoc} + * @throws \Throwable + */ + public function execute(InputInterface $input, OutputInterface $output): int + { + $io = $this->io($input, $output); + + + $target = $this->location->get($input, $io) . '/.rr.yaml'; + + if (\is_file($target) || \is_file(\getcwd() . '/.rr.yaml')) { + return self::FAILURE; + } + + $generator = new Generator(); + $plugins = $input->getOption('preset') ? + Plugins::fromPreset($input->getOption('preset')) : + Plugins::fromPlugins($input->getOption('plugin')); + + try { + $config = $generator->generate($plugins); + \file_put_contents($target, $config); + } catch (\Throwable $e) { + $io->error($e->getMessage()); + return self::FAILURE; + } + + return self::SUCCESS; + } +} \ No newline at end of file