Skip to content

Commit

Permalink
#12 : Add shell cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas43000 committed Nov 20, 2020
1 parent e70ba8b commit 4bc7a60
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 13 deletions.
21 changes: 13 additions & 8 deletions bin/kloud
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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());
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
final class DumpCommand extends Command
{
public static $defaultName = 'environment:database:dump';

private Console $console;
private EnvironmentWizard $wizard;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
final class LoadCommand extends Command
{
public static $defaultName = 'environment:database:load';

private Console $console;
private EnvironmentWizard $wizard;

Expand All @@ -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);
}
Expand Down
116 changes: 116 additions & 0 deletions src/Platform/Console/Command/Environment/ShellCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace Kiboko\Cloud\Platform\Console\Command\Environment;

use Deployer\Host\Host;
use Kiboko\Cloud\Domain\Environment\DTO\Context;
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
use Symfony\Component\Console\Application as Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Process;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

final class ShellCommand extends Command
{
public static $defaultName = 'environment:shell';
private Console $console;
private EnvironmentWizard $wizard;

public function __construct(?string $name, Console $console)
{
$this->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;
}
}
2 changes: 1 addition & 1 deletion src/Platform/Console/Command/Environment/StopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4bc7a60

Please sign in to comment.