From de8bc475a9f73c6d5541fedc07378d369e0aec57 Mon Sep 17 00:00:00 2001 From: Bradley Weston Date: Tue, 24 Jan 2017 19:22:45 +0000 Subject: [PATCH] Different syntax to remove GOTO and also made sure a release always happens after a lock. --- src/Command/CreatePhpServiceCommand.php | 336 ++++++++++++------------ 1 file changed, 166 insertions(+), 170 deletions(-) diff --git a/src/Command/CreatePhpServiceCommand.php b/src/Command/CreatePhpServiceCommand.php index 96a8934..80f0b75 100644 --- a/src/Command/CreatePhpServiceCommand.php +++ b/src/Command/CreatePhpServiceCommand.php @@ -50,78 +50,22 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - if (! file_exists($this->getRootDir() . '/docker-compose.yml')) { - $io->warning('docker-compose.yml does not exist. Run ./bin/micro micro:setup. Aborted.'); + try { + if (! file_exists($this->getRootDir() . '/docker-compose.yml')) { + $io->warning('docker-compose.yml does not exist. Run ./bin/micro micro:setup. Aborted.'); - $this->release(); - - return 1; - } + $this->release(); - $currentConfig = Yaml::parse(file_get_contents($this->getRootDir() . '/docker-compose.yml')); - - $question = new Question('Name of the service: '); - $question->setValidator(function ($answer) { - if (! is_string($answer) || ! preg_match('/^[a-z-0-9]+$/', $answer)) { - throw new \RuntimeException( - 'Invalid service name' - ); - } - - return $answer; - }); - $question->setMaxAttempts(2); - - $serviceName = $io->askQuestion($question); - - $question = new ChoiceQuestion('Which PHP image to use?', [ - 'prooph/php:7.1-cli', - 'prooph/php:7.1-fpm', - 'prooph/php:7.0-cli', - 'prooph/php:7.0-fpm', - 'prooph/php:5.6-cli', - 'prooph/php:5.6-fpm', - ]); - $question->setMaxAttempts(2); - - $image = $io->askQuestion($question); - - $question = new Question('Give the directory in which to mount the service (defaults to: "/var/www")', '/var/www'); - $question->setValidator(function ($answer) { - if (! is_string($answer) || strlen($answer) === 0) { - throw new \RuntimeException( - 'Invalid service name' - ); + return 1; } - return $answer; - }); - $question->setMaxAttempts(2); - - $volume = $io->askQuestion($question); + $currentConfig = Yaml::parse(file_get_contents($this->getRootDir() . '/docker-compose.yml')); - $services = array_merge(['[NONE]'], array_keys($currentConfig['services'])); - $question = new ChoiceQuestion('On which services is the new service dependend? (comma separated)', $services); - $question->setMultiselect(true); - - $dependsOn = $io->askQuestion($question); - - if ($dependsOn === ['[NONE]']) { - $dependsOn = []; - } - - $useNginxConfig = false; - - if ('-fpm' === substr($image, -4, 4)) { - $useNginxConfig = true; - - $io->section('Nginx configuration'); - upstream: - $question = new Question('Add upstream (f.e. "php-user-GET"): '); + $question = new Question('Name of the service: '); $question->setValidator(function ($answer) { - if (! is_string($answer) || strlen($answer) === 0) { + if (! is_string($answer) || ! preg_match('/^[a-z-0-9]+$/', $answer)) { throw new \RuntimeException( - 'Invalid upstream' + 'Invalid service name' ); } @@ -129,18 +73,25 @@ protected function execute(InputInterface $input, OutputInterface $output) }); $question->setMaxAttempts(2); - $upStreams[] = $io->askQuestion($question); + $serviceName = $io->askQuestion($question); - if ($io->confirm('Add more upstreams?', false)) { - goto upstream; - } + $question = new ChoiceQuestion('Which PHP image to use?', [ + 'prooph/php:7.1-cli', + 'prooph/php:7.1-fpm', + 'prooph/php:7.0-cli', + 'prooph/php:7.0-fpm', + 'prooph/php:5.6-cli', + 'prooph/php:5.6-fpm', + ]); + $question->setMaxAttempts(2); + + $image = $io->askQuestion($question); - nginxlocation: - $question = new Question('Add location (f.e. "= /api/v1/user-$request_method"): '); + $question = new Question('Give the directory in which to mount the service (defaults to: "/var/www")', '/var/www'); $question->setValidator(function ($answer) { - if (! is_string($answer) || 0 === strlen($answer)) { + if (! is_string($answer) || strlen($answer) === 0) { throw new \RuntimeException( - 'Invalid location' + 'Invalid service name' ); } @@ -148,136 +99,181 @@ protected function execute(InputInterface $input, OutputInterface $output) }); $question->setMaxAttempts(2); - $locations[] = $io->askQuestion($question); + $volume = $io->askQuestion($question); - $question = new ChoiceQuestion('Add fastcgi_pass: ', $upStreams); + $services = array_merge(['[NONE]'], array_keys($currentConfig['services'])); + $question = new ChoiceQuestion('On which services is the new service dependend? (comma separated)', $services); + $question->setMultiselect(true); - $fastcgiPasses[] = $io->askQuestion($question); + $dependsOn = $io->askQuestion($question); - $question = new Question('Add fastcgi_index (defaults to "index.php"): ', 'index.php'); - $fastcgiIndexes[] = $io->askQuestion($question); + if ($dependsOn === ['[NONE]']) { + $dependsOn = []; + } - $question = new Question('Add fastcgi_param SCRIPT_FILENAME (defaults to "\'/var/www/public/index.php\': "): ', '\'/var/www/public/index.php\''); - $scriptFileNames[] = $io->askQuestion($question); + $useNginxConfig = false; - $question = new Question('Add fastcgi_param PATH_INFO (defaults to "\'/\'"): ', '\'/\''); - $pathInfos[] = $io->askQuestion($question); + if ('-fpm' === substr($image, -4, 4)) { + $useNginxConfig = true; - $question = new Question('Add fastcgi_param SCRIPT_NAME (defaults to "\'/index.php\'"): ', '\'/index.php\''); - $scriptNames[] = $io->askQuestion($question); + $io->section('Nginx configuration'); + do { + $question = new Question('Add upstream (f.e. "php-user-GET"): '); + $question->setValidator(function ($answer) { + if (! is_string($answer) || strlen($answer) === 0) { + throw new \RuntimeException( + 'Invalid upstream' + ); + } - if ($io->confirm('Do you want to add another nginx location?', false)) { - goto nginxlocation; - } - } else { - $question = new Question('Enter the start command (f.e. "php run.php"): ', ''); - $question->setValidator(function ($answer) { - if (! is_string($answer) || strlen($answer) === 0) { - throw new \RuntimeException( - 'Invalid command' - ); - } + return $answer; + }); + $question->setMaxAttempts(2); - return $answer; - }); - $question->setMaxAttempts(2); + $upStreams[] = $io->askQuestion($question); + } while ($io->confirm('Add more upstreams?', false)); - $command = $io->askQuestion($question); + do { + $question = new Question('Add location (f.e. "= /api/v1/user-$request_method"): '); + $question->setValidator(function ($answer) { + if (! is_string($answer) || 0 === strlen($answer)) { + throw new \RuntimeException( + 'Invalid location' + ); + } - $restart = $io->confirm('Always restart container if it exists?', false); - } + return $answer; + }); + $question->setMaxAttempts(2); - $dependsOnString = ''; - foreach ($it = new \CachingIterator(new \ArrayIterator($dependsOn), \CachingIterator::FULL_CACHE) as $dependend) { - $dependsOnString .= " - $dependend"; - if ($it->hasNext()) { - $dependsOnString .= "\n"; - } - } + $locations[] = $io->askQuestion($question); - if ($useNginxConfig) { - $gatewayFile = $this->getRootDir() . '/gateway/www.conf'; + $question = new ChoiceQuestion('Add fastcgi_pass: ', $upStreams); - $parser = new Parser(); + $fastcgiPasses[] = $io->askQuestion($question); - $nginxConfig = $parser->parseFile($gatewayFile); + $question = new Question('Add fastcgi_index (defaults to "index.php"): ', 'index.php'); + $fastcgiIndexes[] = $io->askQuestion($question); - $builder = new Builder(); - $builder->append($nginxConfig); + $question = new Question('Add fastcgi_param SCRIPT_FILENAME (defaults to "\'/var/www/public/index.php\': "): ', '\'/var/www/public/index.php\''); + $scriptFileNames[] = $io->askQuestion($question); - $servers = $nginxConfig->search(function (Node $node) { - return $node instanceof Server; - }); + $question = new Question('Add fastcgi_param PATH_INFO (defaults to "\'/\'"): ', '\'/\''); + $pathInfos[] = $io->askQuestion($question); + + $question = new Question('Add fastcgi_param SCRIPT_NAME (defaults to "\'/index.php\'"): ', '\'/index.php\''); + $scriptNames[] = $io->askQuestion($question); + } while ($io->confirm('Do you want to add another nginx location?', false)); + } else { + $question = new Question('Enter the start command (f.e. "php run.php"): ', ''); + $question->setValidator(function ($answer) { + if (! is_string($answer) || strlen($answer) === 0) { + throw new \RuntimeException( + 'Invalid command' + ); + } + + return $answer; + }); + $question->setMaxAttempts(2); - $count = count($servers); + $command = $io->askQuestion($question); - if ($count === 0) { - throw new \RuntimeException('No server section found in gateway config.'); + $restart = $io->confirm('Always restart container if it exists?', false); } - if ($count > 1) { - throw new \RuntimeException('More than one server section found in gateway config.'); + $dependsOnString = ''; + foreach ($it = new \CachingIterator(new \ArrayIterator($dependsOn), \CachingIterator::FULL_CACHE) as $dependend) { + $dependsOnString .= " - $dependend"; + if ($it->hasNext()) { + $dependsOnString .= "\n"; + } } - $server = $servers->getIterator()->current(); + if ($useNginxConfig) { + $gatewayFile = $this->getRootDir() . '/gateway/www.conf'; - /* @var Server $server */ - foreach ($upStreams as $upStream) { - $builder->append(new Upstream(new Param($upStream), [ - new Directive('server', [new Param("$serviceName:9000")]), - ])); - } + $parser = new Parser(); - while ($location = array_shift($locations)) { - $fastcgiPass = array_shift($fastcgiPasses); - $fastcgiIndex = array_shift($fastcgiIndexes); - $scriptFileName = array_shift($scriptFileNames); - $pathInfo = array_shift($pathInfos); - $scriptName = array_shift($scriptNames); - - $server->append(new Location(new Param($location), null, [ - new Directive('fastcgi_split_path_info', [new Param('^(.+\.php)(/.+)$')]), - new Directive('fastcgi_pass', [new Param($fastcgiPass)]), - new Directive('fastcgi_index', [new Param($fastcgiIndex)]), - new Directive('include', [new Param('fastcgi_params')]), - new Directive('fastcgi_param', [new Param('SCRIPT_FILENAME ' . $scriptFileName)]), - new Directive('fastcgi_param', [new Param('PATH_INFO ' . $pathInfo)]), - new Directive('fastcgi_param', [new Param('SCRIPT_NAME ' . $scriptName)]), - ])); - } + $nginxConfig = $parser->parseFile($gatewayFile); - $builder->dumpFile($gatewayFile); - } + $builder = new Builder(); + $builder->append($nginxConfig); + + $servers = $nginxConfig->search(function (Node $node) { + return $node instanceof Server; + }); + + $count = count($servers); + + if ($count === 0) { + throw new \RuntimeException('No server section found in gateway config.'); + } + + if ($count > 1) { + throw new \RuntimeException('More than one server section found in gateway config.'); + } + + $server = $servers->getIterator()->current(); - $config = [ - 'services' => [ - $serviceName => [ - 'image' => $image, - 'volumes' => [ - "./service/$serviceName:$volume", + /* @var Server $server */ + foreach ($upStreams as $upStream) { + $builder->append(new Upstream(new Param($upStream), [ + new Directive('server', [new Param("$serviceName:9000")]), + ])); + } + + while ($location = array_shift($locations)) { + $fastcgiPass = array_shift($fastcgiPasses); + $fastcgiIndex = array_shift($fastcgiIndexes); + $scriptFileName = array_shift($scriptFileNames); + $pathInfo = array_shift($pathInfos); + $scriptName = array_shift($scriptNames); + + $server->append(new Location(new Param($location), null, [ + new Directive('fastcgi_split_path_info', [new Param('^(.+\.php)(/.+)$')]), + new Directive('fastcgi_pass', [new Param($fastcgiPass)]), + new Directive('fastcgi_index', [new Param($fastcgiIndex)]), + new Directive('include', [new Param('fastcgi_params')]), + new Directive('fastcgi_param', [new Param('SCRIPT_FILENAME ' . $scriptFileName)]), + new Directive('fastcgi_param', [new Param('PATH_INFO ' . $pathInfo)]), + new Directive('fastcgi_param', [new Param('SCRIPT_NAME ' . $scriptName)]), + ])); + } + + $builder->dumpFile($gatewayFile); + } + + $config = [ + 'services' => [ + $serviceName => [ + 'image' => $image, + 'volumes' => [ + "./service/$serviceName:$volume", + ], ], ], - ], - ]; - - if (! empty($dependsOn)) { - $config['services'][$serviceName]['depends_on'] = $dependsOn; - } + ]; - if (isset($restart)) { - $config['services'][$serviceName]['restart'] = 'always'; - } + if (! empty($dependsOn)) { + $config['services'][$serviceName]['depends_on'] = $dependsOn; + } - if (isset($command)) { - $config['services'][$serviceName]['command'] = $command; - } + if (isset($restart)) { + $config['services'][$serviceName]['restart'] = 'always'; + } - $this->updateConfig($serviceName, $config); + if (isset($command)) { + $config['services'][$serviceName]['command'] = $command; + } - $io->success('Successfully updated microservice settings'); + $this->updateConfig($serviceName, $config); - $this->release(); + $io->success('Successfully updated microservice settings'); - return 0; + return 0; + } finally { + $this->release(); + } } }