From 022708fe90cc99b4a8c53c7bb507d77e18107d25 Mon Sep 17 00:00:00 2001 From: Peter Pajor Date: Mon, 25 May 2020 11:19:38 +0200 Subject: [PATCH 1/3] Add initial work of DockerStack platform type. --- .../Platform/PlatformSubscriberDefaults.php | 7 +++ src/Platform/DockerStackPlatform.php | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/Platform/DockerStackPlatform.php diff --git a/src/EventSubscriber/Platform/PlatformSubscriberDefaults.php b/src/EventSubscriber/Platform/PlatformSubscriberDefaults.php index c51f107..ab917f7 100644 --- a/src/EventSubscriber/Platform/PlatformSubscriberDefaults.php +++ b/src/EventSubscriber/Platform/PlatformSubscriberDefaults.php @@ -6,6 +6,7 @@ use EclipseGc\CommonConsole\Event\GetPlatformTypeEvent; use EclipseGc\CommonConsole\Event\GetPlatformTypesEvent; use EclipseGc\CommonConsole\Platform\DdevPlatform; +use EclipseGc\CommonConsole\Platform\DockerStackPlatform; use EclipseGc\CommonConsole\Platform\SshPlatform; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -20,6 +21,7 @@ public static function getSubscribedEvents() { public function onGetPlatformTypes(GetPlatformTypesEvent $event) { $event->addPlatformType(SshPlatform::getPlatformId()); $event->addPlatformType(DdevPlatform::getPlatformId()); + $event->addPlatformType(DockerStackPlatform::getPlatformId()); } public function onGetPlatformType(GetPlatformTypeEvent $event) { @@ -33,6 +35,11 @@ public function onGetPlatformType(GetPlatformTypeEvent $event) { $event->stopPropagation(); return; } + + if ($event->getPlatformType() === DockerStackPlatform::getPlatformId()) { + $event->addClass(DockerStackPlatform::class); + $event->stopPropagation(); + } } } diff --git a/src/Platform/DockerStackPlatform.php b/src/Platform/DockerStackPlatform.php new file mode 100644 index 0000000..4e5edaf --- /dev/null +++ b/src/Platform/DockerStackPlatform.php @@ -0,0 +1,52 @@ + new Question("Docker Service Names (spearate them with colon): "), + 'docker.compose_file' => new Question("Location of docker-compose file: "), + ]; + } + + /** + * {@inheritdoc} + */ + public function execute(Command $command, InputInterface $input, OutputInterface $output) : void { + $services = explode(',', $this->get('docker.services')); + $location = $this->get('docker.compose_file'); + + foreach ($services as $service) { + $uriProcess = Process::fromShellCommandline("cd $location; docker-compose config | grep -A 11 $service | grep hostname | grep -v database | awk '{print $2}'"); + $uriProcess->run(); + $uri = trim($uriProcess->getOutput()); + $process = Process::fromShellCommandline("cd $location; docker-compose exec -T $service ./vendor/bin/commoncli --uri $uri {$input->__toString()}"); + $this->runner->run($process, $this, $output); + } + } + +} From a64b785d79877fe35efb8aab913e7561e1275d4b Mon Sep 17 00:00:00 2001 From: Peter Pajor Date: Mon, 25 May 2020 14:08:38 +0200 Subject: [PATCH 2/3] Make output more exact. --- src/Platform/DockerStackPlatform.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Platform/DockerStackPlatform.php b/src/Platform/DockerStackPlatform.php index 4e5edaf..19fbc82 100644 --- a/src/Platform/DockerStackPlatform.php +++ b/src/Platform/DockerStackPlatform.php @@ -5,12 +5,13 @@ use Consolidation\Config\Config; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; use Symfony\Component\Process\Process; /** - * Class SshPlatform + * Class DockerStackPlatform. * * @package EclipseGc\CommonConsole\Platform */ @@ -44,6 +45,7 @@ public function execute(Command $command, InputInterface $input, OutputInterface $uriProcess = Process::fromShellCommandline("cd $location; docker-compose config | grep -A 11 $service | grep hostname | grep -v database | awk '{print $2}'"); $uriProcess->run(); $uri = trim($uriProcess->getOutput()); + $output->writeln("Executed on '$service'"); $process = Process::fromShellCommandline("cd $location; docker-compose exec -T $service ./vendor/bin/commoncli --uri $uri {$input->__toString()}"); $this->runner->run($process, $this, $output); } From 943f50a06e492fe36008892ac935c87f3549f5b2 Mon Sep 17 00:00:00 2001 From: Peter Pajor Date: Mon, 15 Jun 2020 15:18:54 +0200 Subject: [PATCH 3/3] Move config keys into constants, implement PlatformSitesInterface. --- src/Platform/DockerStackPlatform.php | 43 ++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Platform/DockerStackPlatform.php b/src/Platform/DockerStackPlatform.php index 19fbc82..41bb447 100644 --- a/src/Platform/DockerStackPlatform.php +++ b/src/Platform/DockerStackPlatform.php @@ -15,7 +15,17 @@ * * @package EclipseGc\CommonConsole\Platform */ -class DockerStackPlatform extends PlatformBase { +class DockerStackPlatform extends PlatformBase implements PlatformSitesInterface { + + /** + * Services added to the platform. + */ + public const CONFIG_SERVICES = 'docker.services'; + + /** + * The path to the docker-compose.yml file, from where the command could run. + */ + public const CONFIG_COMPOSE_FILE_PATH = 'docker.compose_file'; /** * {@inheritdoc} @@ -29,24 +39,41 @@ public static function getPlatformId(): string { */ public static function getQuestions() { return [ - 'docker.services' => new Question("Docker Service Names (spearate them with colon): "), - 'docker.compose_file' => new Question("Location of docker-compose file: "), + static::CONFIG_SERVICES => new Question("Docker Service Names (spearate them with colon): "), + static::CONFIG_COMPOSE_FILE_PATH => new Question("Location of docker-compose file: "), ]; } /** * {@inheritdoc} + * + * This implementation relies on the hostname. Overwrite the method for + * custom site retrieving logic. */ - public function execute(Command $command, InputInterface $input, OutputInterface $output) : void { - $services = explode(',', $this->get('docker.services')); - $location = $this->get('docker.compose_file'); - + public function getPlatformSites(): array { + $services = explode(',', $this->get(static::CONFIG_SERVICES)); + $location = $this->get(static::CONFIG_COMPOSE_FILE_PATH); + $sites = []; foreach ($services as $service) { $uriProcess = Process::fromShellCommandline("cd $location; docker-compose config | grep -A 11 $service | grep hostname | grep -v database | awk '{print $2}'"); $uriProcess->run(); $uri = trim($uriProcess->getOutput()); + $sites[trim($service)] = $uri; + } + + return $sites; + } + + /** + * {@inheritdoc} + */ + public function execute(Command $command, InputInterface $input, OutputInterface $output) : void { + $location = $this->get(static::CONFIG_COMPOSE_FILE_PATH); + $services = explode(',', $this->get(static::CONFIG_SERVICES)); + $sites = $this->getPlatformSites(); + foreach ($services as $service) { $output->writeln("Executed on '$service'"); - $process = Process::fromShellCommandline("cd $location; docker-compose exec -T $service ./vendor/bin/commoncli --uri $uri {$input->__toString()}"); + $process = Process::fromShellCommandline("cd $location; docker-compose exec -T $service ./vendor/bin/commoncli --uri {$sites[$service]} {$input->__toString()}"); $this->runner->run($process, $this, $output); } }