Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improves SetupCommand #18

Merged
merged 4 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"prooph/php-cs-fixer-config": "^0.1.1",
"beberlei/assert": "^2.7",
"symfony/console": "^3.2.2",
"symfony/filesystem": ">=2.6",
"symfony/yaml": "^3.2.2",
"romanpitak/nginx-config-processor": "^0.2.1"
},
Expand Down
146 changes: 77 additions & 69 deletions src/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Yaml\Yaml;

class SetupCommand extends AbstractCommand
{
Expand All @@ -27,30 +29,30 @@ protected function configure()
$this
->setName('micro:setup')
->setDescription('Setup a prooph-micro application')
->setHelp('This command creates the skeleton for a prooph-micro(services) application.');
->setHelp('This command creates the skeleton for a prooph-micro(services) application.')
->addOption('no-ports', null, InputOption::VALUE_NONE, 'Do not publish ports on docker host.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

if (! $this->lock()) {
$output->writeln('The command is already running in another process.');
$io->warning('The command is already running in another process.');

return 0;
}

if (file_exists($this->getRootDir() . '/docker-compose.yml')) {
$output->writeln('docker-compose.yml exists already. Aborted.');
$io->warning('docker-compose.yml exists already. Aborted.');

return;
}

$output->writeln('This setup will use the following docker-images:');
$output->writeln('prooph/nginx:www (as webserver)');
$output->writeln('');

$helper = $this->getHelper('question');
$io->section('This setup will use the following docker-images:');
$io->listing(['prooph/nginx:www (as webserver)']);

$question = new Question('Gateway directory (defaults to "gateway"): ', 'gateway');
$question = new Question('Gateway directory', 'gateway');
$question->setValidator(function ($answer) {
if (! is_string($answer) || strlen($answer) === 0) {
throw new \RuntimeException(
Expand All @@ -60,55 +62,62 @@ protected function execute(InputInterface $input, OutputInterface $output)

return $answer;
});

$question->setMaxAttempts(2);

$gatewayDirectory = $helper->ask($input, $output, $question);
$gatewayDirectory = $io->askQuestion($question);
$configMessages[] = "<info>Gateway directory:</info> $gatewayDirectory";

$question = new Question('HTTP port (defaults to "80"): ', '80');
$question->setValidator(function ($answer) {
if (! is_int((int) $answer) || $answer === 0) {
throw new \RuntimeException(
'Invalid HTTP port'
);
}
$httpPort = null;
$httpsPort = null;

return $answer;
});
$question->setMaxAttempts(2);
if (! $input->getOption('no-ports')) {
$question = new Question('HTTP port: ', 'random');
$question->setValidator(function ($answer) {
if ('random' === $answer) {
return '';
}

$httpPort = $helper->ask($input, $output, $question);
if (! is_int((int) $answer) || $answer === 0) {
throw new \RuntimeException(
'Invalid HTTP port'
);
}

$question = new Question('HTTPS port (defaults to "443"): ', '443');
$question->setValidator(function ($answer) {
if (! is_int((int) $answer) || $answer === 0) {
throw new \RuntimeException(
'Invalid HTTPS port'
);
}
return $answer;
});

return $answer;
});
$question->setMaxAttempts(2);
$question->setMaxAttempts(2);

$httpsPort = $helper->ask($input, $output, $question);
$httpPort = $io->askQuestion($question);
$configMessages[] = '<info>HTTP port:</info> '.($httpPort ?: 'random');

$question = <<<EOT

####################################################
$question = new Question('HTTPS port: ', 'random');
$question->setValidator(function ($answer) {
if ('random' === $answer) {
return '';
}

Setup will be done with the following configuration:
if (! is_int((int) $answer) || $answer === 0) {
throw new \RuntimeException(
'Invalid HTTPS port'
);
}

Gateway directory: $gatewayDirectory
HTTP port: $httpPort
HTTPS port: $httpsPort
return $answer;
});

Are those settings correct? (y/n):
EOT;
$question->setMaxAttempts(2);

$confirmation = new ConfirmationQuestion($question, false);
$httpsPort = $io->askQuestion($question);
$configMessages[] = '<info>HTTPS port:</info> '.($httpsPort ?: 'random');
}

$io->section('Setup will be done with the following configuration:');
$io->listing($configMessages);

if (! $helper->ask($input, $output, $confirmation)) {
$output->writeln('Aborted');
if (! $io->confirm('Are those settings correct?', false)) {
$io->writeln('<comment>Aborted</comment>');

return;
}
Expand Down Expand Up @@ -148,40 +157,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
$gatewayConfig
);

$output->writeln('Successfully created microservice settings');
$io->success('Successfully created microservice settings');

$this->release();
}

private function generateConfigFile(
string $gatewayDirectory,
string $httpPort,
string $httpsPort
string $httpPort = null,
string $httpsPort = null
): string {
$config = <<<EOT
version: '2'

services:
nginx:
image: prooph/nginx:www
volumes:
- ./$gatewayDirectory:/etc/nginx/sites-enabled:ro
labels:
prooph-gateway-directory: ./$gatewayDirectory
EOT;

if ($httpPort !== '80' || $httpsPort !== '443') {
$config .= "\n ports:\n";
}

if ($httpPort !== '80') {
$config .= " - $httpPort:80\n";
$config = [
'version' => '2',
'services' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it was already discussed, but we should use version 3 (requires Docker 1.13 and Docker Composer 1.10) but then we can deploy the stuff to a Swarm Cluster with docker stack deploy --compose-file=docker-compose.yml my_stack.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for version 3. But not available for docker for Mac currently.

'nginx' => [
'image' => 'prooph/nginx:www',
'volumes' => [
"./$gatewayDirectory:/etc/nginx/sites-enabled:ro",
],
'labels' => [
'prooph-gateway-directory' => "./$gatewayDirectory",
],
],
],
];

if (null !== $httpPort) {
$config['services']['nginx']['ports'][] = '' !== $httpPort ? "$httpPort:80" : '80';
}

if ($httpsPort !== '443') {
$config .= " - $httpsPort:443\n";
if (null !== $httpsPort) {
$config['services']['nginx']['ports'][] = '' !== $httpsPort ? "$httpsPort:443" : '443';
}

return $config;
return Yaml::dump($config, 4);
}
}
66 changes: 66 additions & 0 deletions tests/Command/SetupCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* This file is part of the prooph/micro.
* (c) 2017-2017 prooph software GmbH <contact@prooph.de>
* (c) 2017-2017 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ProophTest\Micro\Command;

use PHPUnit\Framework\TestCase;
use Prooph\Micro\Command\SetupCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

final class SetupCommandTest extends TestCase
{
/**
* @var Application
*/
private $application;

public function setUp(): void
{
$this->application = new Application();

$setupCommand = $this->getMockBuilder(SetupCommand::class)
->setMethods(['getRootDir'])
->getMock();

$setupCommand->method('getRootDir')->willReturn(sys_get_temp_dir());

$this->application->add($setupCommand);
}

public function tearDown(): void
{
unlink(sys_get_temp_dir() . '/gateway/www.conf');
unlink(sys_get_temp_dir() . '/docker-compose.yml');
}

/**
* @test
*/
public function it_creates_config_files(): void
{
$command = $this->application->find('micro:setup');
$commandTester = new CommandTester($command);

$commandTester->setInputs([
'gateway',
'80',
'443',
'y',
]);

$commandTester->execute(['command' => $command->getName()]);

$this->assertTrue(file_exists(sys_get_temp_dir() . '/gateway/www.conf'));
$this->assertTrue(file_exists(sys_get_temp_dir() . '/docker-compose.yml'));
}
}