|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Cloud\Platform\Console\Command\Environment; |
| 6 | + |
| 7 | +use Deployer\Host\Host; |
| 8 | +use Kiboko\Cloud\Domain\Environment\DTO\Context; |
| 9 | +use Kiboko\Cloud\Platform\Console\EnvironmentWizard; |
| 10 | +use Symfony\Component\Console\Application as Console; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Symfony\Component\Console\Question\Question; |
| 15 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 16 | +use Symfony\Component\Finder\Finder; |
| 17 | +use Symfony\Component\Finder\SplFileInfo; |
| 18 | +use Symfony\Component\Process\Process; |
| 19 | +use Symfony\Component\Serializer\Encoder\YamlEncoder; |
| 20 | +use Symfony\Component\Serializer\Normalizer\CustomNormalizer; |
| 21 | +use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
| 22 | +use Symfony\Component\Serializer\Serializer; |
| 23 | + |
| 24 | +final class ShellCommand extends Command |
| 25 | +{ |
| 26 | + public static $defaultName = 'environment:shell'; |
| 27 | + private Console $console; |
| 28 | + private EnvironmentWizard $wizard; |
| 29 | + |
| 30 | + public function __construct(?string $name, Console $console) |
| 31 | + { |
| 32 | + $this->console = $console; |
| 33 | + $this->wizard = new EnvironmentWizard(); |
| 34 | + parent::__construct($name); |
| 35 | + } |
| 36 | + |
| 37 | + protected function configure() |
| 38 | + { |
| 39 | + $this->setDescription('Start a shell session for a service'); |
| 40 | + |
| 41 | + $this->wizard->configureConsoleCommand($this); |
| 42 | + } |
| 43 | + |
| 44 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 45 | + { |
| 46 | + $workingDirectory = $input->getOption('working-directory') ?: getcwd(); |
| 47 | + |
| 48 | + $finder = (new Finder()) |
| 49 | + ->files() |
| 50 | + ->ignoreDotFiles(false) |
| 51 | + ->in($workingDirectory); |
| 52 | + |
| 53 | + $format = new SymfonyStyle($input, $output); |
| 54 | + |
| 55 | + $serializer = new Serializer( |
| 56 | + [ |
| 57 | + new CustomNormalizer(), |
| 58 | + new PropertyNormalizer(), |
| 59 | + ], |
| 60 | + [ |
| 61 | + new YamlEncoder(), |
| 62 | + ] |
| 63 | + ); |
| 64 | + |
| 65 | + if ($finder->hasResults()) { |
| 66 | + /** @var SplFileInfo $file */ |
| 67 | + foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) { |
| 68 | + try { |
| 69 | + /** @var Context $context */ |
| 70 | + $context = $serializer->deserialize($file->getContents(), Context::class, 'yaml'); |
| 71 | + } catch (\Throwable $exception) { |
| 72 | + $format->error($exception->getMessage()); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (!isset($context)) { |
| 81 | + $format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command'); |
| 82 | + |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + $host = new Host($context->deployment->server->hostname); |
| 87 | + $host->port($context->deployment->server->port); |
| 88 | + $host->user($context->deployment->server->username); |
| 89 | + |
| 90 | + $directories = explode('/', $workingDirectory); |
| 91 | + $projectName = end($directories); |
| 92 | + $remoteProjectPath = $context->deployment->path.'/'.$projectName; |
| 93 | + |
| 94 | + $service = $format->askQuestion(new Question('For what service you want to start a shell session?')); |
| 95 | + $process = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker-compose', 'ps', '-q', $service]); |
| 96 | + try { |
| 97 | + $process->mustRun(); |
| 98 | + $containerIds = rtrim($process->getOutput(), PHP_EOL); |
| 99 | + } catch (\Exception $exception) { |
| 100 | + $format->error($exception->getMessage()); |
| 101 | + |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + $process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker', 'exec', '-ti', $containerIds, 'sh']); |
| 106 | + try { |
| 107 | + $process2->setTty(Process::isTtySupported())->setTimeout(0)->mustRun(); |
| 108 | + } catch (\Exception $exception) { |
| 109 | + $format->error($exception->getMessage()); |
| 110 | + |
| 111 | + return 1; |
| 112 | + } |
| 113 | + |
| 114 | + return 0; |
| 115 | + } |
| 116 | +} |
0 commit comments