Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/EventSubscriber/Platform/PlatformSubscriberDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -33,6 +35,11 @@ public function onGetPlatformType(GetPlatformTypeEvent $event) {
$event->stopPropagation();
return;
}

if ($event->getPlatformType() === DockerStackPlatform::getPlatformId()) {
$event->addClass(DockerStackPlatform::class);
$event->stopPropagation();
}
}

}
81 changes: 81 additions & 0 deletions src/Platform/DockerStackPlatform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace EclipseGc\CommonConsole\Platform;

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 DockerStackPlatform.
*
* @package EclipseGc\CommonConsole\Platform
*/
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}
*/
public static function getPlatformId(): string {
return 'DockerStack';
}

/**
* {@inheritdoc}
*/
public static function getQuestions() {
return [
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 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 {$sites[$service]} {$input->__toString()}");
$this->runner->run($process, $this, $output);
}
}

}