Skip to content

Commit 4bc7a60

Browse files
committed
#12 : Add shell cmd
1 parent e70ba8b commit 4bc7a60

File tree

5 files changed

+132
-13
lines changed

5 files changed

+132
-13
lines changed

Diff for: bin/kloud

+13-8
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ $app->addCommands([
6464
Command\Environment\InitCommand::$defaultName,
6565
)),
6666

67-
(new Command\Environment\Variable\ListCommand(
68-
Command\Environment\Variable\ListCommand::$defaultName,
67+
(new Command\Environment\Variable\AddCommand(
68+
Command\Environment\Variable\AddCommand::$defaultName,
69+
)),
70+
71+
(new Command\Environment\Variable\UnsetCommand(
72+
Command\Environment\Variable\UnsetCommand::$defaultName,
6973
)),
7074

7175
(new Command\Environment\Variable\GetCommand(
@@ -76,12 +80,8 @@ $app->addCommands([
7680
Command\Environment\Variable\SetCommand::$defaultName,
7781
)),
7882

79-
(new Command\Environment\Variable\AddCommand(
80-
Command\Environment\Variable\AddCommand::$defaultName,
81-
)),
82-
83-
(new Command\Environment\Variable\UnsetCommand(
84-
Command\Environment\Variable\UnsetCommand::$defaultName,
83+
(new Command\Environment\Variable\ListCommand(
84+
Command\Environment\Variable\ListCommand::$defaultName,
8585
)),
8686

8787
(new Command\Environment\DeployCommand(
@@ -123,6 +123,11 @@ $app->addCommands([
123123
Command\Environment\Database\LoadCommand::$defaultName,
124124
$app,
125125
)),
126+
127+
(new Command\Environment\ShellCommand(
128+
Command\Environment\ShellCommand::$defaultName,
129+
$app,
130+
)),
126131
]);
127132

128133
$app->run(new ArgvInput($argv), new ConsoleOutput());

Diff for: src/Platform/Console/Command/Environment/Database/DumpCommand.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
final class DumpCommand extends Command
2626
{
2727
public static $defaultName = 'environment:database:dump';
28-
2928
private Console $console;
3029
private EnvironmentWizard $wizard;
3130

@@ -38,7 +37,7 @@ public function __construct(?string $name, Console $console)
3837

3938
protected function configure()
4039
{
41-
$this->setDescription('Dumps the database in the current state');
40+
$this->setDescription('Dump the database in the current state');
4241

4342
$this->wizard->configureConsoleCommand($this);
4443
}

Diff for: src/Platform/Console/Command/Environment/Database/LoadCommand.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
final class LoadCommand extends Command
2626
{
2727
public static $defaultName = 'environment:database:load';
28-
2928
private Console $console;
3029
private EnvironmentWizard $wizard;
3130

@@ -38,7 +37,7 @@ public function __construct(?string $name, Console $console)
3837

3938
protected function configure()
4039
{
41-
$this->setDescription('Dumps the database in the current state');
40+
$this->setDescription('Load a database dump');
4241

4342
$this->wizard->configureConsoleCommand($this);
4443
}
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

Diff for: src/Platform/Console/Command/Environment/StopCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(?string $name, Console $console)
4242

4343
protected function configure()
4444
{
45-
$this->setDescription('Stoptty docker services on the remote server');
45+
$this->setDescription('Stop docker services on the remote server');
4646

4747
$this->wizard->configureConsoleCommand($this);
4848
}

0 commit comments

Comments
 (0)