diff --git a/bin/kloud b/bin/kloud index df8d656..8dd1b6b 100755 --- a/bin/kloud +++ b/bin/kloud @@ -64,8 +64,12 @@ $app->addCommands([ Command\Environment\InitCommand::$defaultName, )), - (new Command\Environment\Variable\ListCommand( - Command\Environment\Variable\ListCommand::$defaultName, + (new Command\Environment\Variable\AddCommand( + Command\Environment\Variable\AddCommand::$defaultName, + )), + + (new Command\Environment\Variable\UnsetCommand( + Command\Environment\Variable\UnsetCommand::$defaultName, )), (new Command\Environment\Variable\GetCommand( @@ -76,12 +80,8 @@ $app->addCommands([ Command\Environment\Variable\SetCommand::$defaultName, )), - (new Command\Environment\Variable\AddCommand( - Command\Environment\Variable\AddCommand::$defaultName, - )), - - (new Command\Environment\Variable\UnsetCommand( - Command\Environment\Variable\UnsetCommand::$defaultName, + (new Command\Environment\Variable\ListCommand( + Command\Environment\Variable\ListCommand::$defaultName, )), (new Command\Environment\DeployCommand( @@ -123,6 +123,11 @@ $app->addCommands([ Command\Environment\Database\LoadCommand::$defaultName, $app, )), + + (new Command\Environment\ShellCommand( + Command\Environment\ShellCommand::$defaultName, + $app, + )), ]); $app->run(new ArgvInput($argv), new ConsoleOutput()); diff --git a/src/Platform/Console/Command/Environment/Database/DumpCommand.php b/src/Platform/Console/Command/Environment/Database/DumpCommand.php index 4747008..7bce4d6 100644 --- a/src/Platform/Console/Command/Environment/Database/DumpCommand.php +++ b/src/Platform/Console/Command/Environment/Database/DumpCommand.php @@ -25,7 +25,6 @@ final class DumpCommand extends Command { public static $defaultName = 'environment:database:dump'; - private Console $console; private EnvironmentWizard $wizard; @@ -38,7 +37,7 @@ public function __construct(?string $name, Console $console) protected function configure() { - $this->setDescription('Dumps the database in the current state'); + $this->setDescription('Dump the database in the current state'); $this->wizard->configureConsoleCommand($this); } diff --git a/src/Platform/Console/Command/Environment/Database/LoadCommand.php b/src/Platform/Console/Command/Environment/Database/LoadCommand.php index b6b89e8..ca37149 100644 --- a/src/Platform/Console/Command/Environment/Database/LoadCommand.php +++ b/src/Platform/Console/Command/Environment/Database/LoadCommand.php @@ -25,7 +25,6 @@ final class LoadCommand extends Command { public static $defaultName = 'environment:database:load'; - private Console $console; private EnvironmentWizard $wizard; @@ -38,7 +37,7 @@ public function __construct(?string $name, Console $console) protected function configure() { - $this->setDescription('Dumps the database in the current state'); + $this->setDescription('Load a database dump'); $this->wizard->configureConsoleCommand($this); } diff --git a/src/Platform/Console/Command/Environment/ShellCommand.php b/src/Platform/Console/Command/Environment/ShellCommand.php new file mode 100644 index 0000000..9df1656 --- /dev/null +++ b/src/Platform/Console/Command/Environment/ShellCommand.php @@ -0,0 +1,116 @@ +console = $console; + $this->wizard = new EnvironmentWizard(); + parent::__construct($name); + } + + protected function configure() + { + $this->setDescription('Start a shell session for a service'); + + $this->wizard->configureConsoleCommand($this); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $workingDirectory = $input->getOption('working-directory') ?: getcwd(); + + $finder = (new Finder()) + ->files() + ->ignoreDotFiles(false) + ->in($workingDirectory); + + $format = new SymfonyStyle($input, $output); + + $serializer = new Serializer( + [ + new CustomNormalizer(), + new PropertyNormalizer(), + ], + [ + new YamlEncoder(), + ] + ); + + if ($finder->hasResults()) { + /** @var SplFileInfo $file */ + foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) { + try { + /** @var Context $context */ + $context = $serializer->deserialize($file->getContents(), Context::class, 'yaml'); + } catch (\Throwable $exception) { + $format->error($exception->getMessage()); + continue; + } + + break; + } + } + + if (!isset($context)) { + $format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command'); + + return 1; + } + + $host = new Host($context->deployment->server->hostname); + $host->port($context->deployment->server->port); + $host->user($context->deployment->server->username); + + $directories = explode('/', $workingDirectory); + $projectName = end($directories); + $remoteProjectPath = $context->deployment->path.'/'.$projectName; + + $service = $format->askQuestion(new Question('For what service you want to start a shell session?')); + $process = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker-compose', 'ps', '-q', $service]); + try { + $process->mustRun(); + $containerIds = rtrim($process->getOutput(), PHP_EOL); + } catch (\Exception $exception) { + $format->error($exception->getMessage()); + + return 1; + } + + $process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker', 'exec', '-ti', $containerIds, 'sh']); + try { + $process2->setTty(Process::isTtySupported())->setTimeout(0)->mustRun(); + } catch (\Exception $exception) { + $format->error($exception->getMessage()); + + return 1; + } + + return 0; + } +} diff --git a/src/Platform/Console/Command/Environment/StopCommand.php b/src/Platform/Console/Command/Environment/StopCommand.php index 3164bed..c4684a7 100644 --- a/src/Platform/Console/Command/Environment/StopCommand.php +++ b/src/Platform/Console/Command/Environment/StopCommand.php @@ -42,7 +42,7 @@ public function __construct(?string $name, Console $console) protected function configure() { - $this->setDescription('Stoptty docker services on the remote server'); + $this->setDescription('Stop docker services on the remote server'); $this->wizard->configureConsoleCommand($this); }