Skip to content

Commit ae22456

Browse files
committed
#12 : Add cache clear, start and stop commands
1 parent 890f79b commit ae22456

File tree

11 files changed

+390
-9
lines changed

11 files changed

+390
-9
lines changed

Diff for: bin/kloud

+16
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,26 @@ $app->addCommands([
9090
$app,
9191
)),
9292

93+
(new Command\Environment\StartCommand(
94+
Command\Environment\StartCommand::$defaultName,
95+
$app,
96+
)),
97+
98+
99+
(new Command\Environment\StopCommand(
100+
Command\Environment\StopCommand::$defaultName,
101+
$app,
102+
)),
103+
93104
(new Command\Environment\RsyncCommand(
94105
Command\Environment\RsyncCommand::$defaultName,
95106
$app,
96107
)),
108+
109+
(new Command\Environment\Cache\ClearCommand(
110+
Command\Environment\Cache\ClearCommand::$defaultName,
111+
$app,
112+
)),
97113
]);
98114

99115
$app->run(new ArgvInput($argv), new ConsoleOutput());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Cloud\Platform\Console\Command\Environment\Cache;
6+
7+
use Deployer\Console\Application;
8+
use Deployer\Console\Output\Informer;
9+
use Deployer\Console\Output\OutputWatcher;
10+
use Deployer\Deployer;
11+
use Deployer\Executor\SeriesExecutor;
12+
use Deployer\Host\Host;
13+
use function Deployer\run;
14+
use Deployer\Task\Task;
15+
use Kiboko\Cloud\Domain\Environment\DTO\Context;
16+
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
17+
use Symfony\Component\Console\Application as Console;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Component\Console\Question\ChoiceQuestion;
22+
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\Finder\Finder;
24+
use Symfony\Component\Finder\SplFileInfo;
25+
use Symfony\Component\Serializer\Encoder\YamlEncoder;
26+
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
27+
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
28+
use Symfony\Component\Serializer\Serializer;
29+
30+
final class ClearCommand extends Command
31+
{
32+
public static $defaultName = 'environment:cache:clear';
33+
34+
private Console $console;
35+
private EnvironmentWizard $wizard;
36+
37+
public function __construct(?string $name, Console $console)
38+
{
39+
$this->console = $console;
40+
$this->wizard = new EnvironmentWizard();
41+
parent::__construct($name);
42+
}
43+
44+
protected function configure()
45+
{
46+
$this->setDescription('Clear cache and restart FPM service');
47+
48+
$this->wizard->configureConsoleCommand($this);
49+
}
50+
51+
protected function execute(InputInterface $input, OutputInterface $output)
52+
{
53+
$workingDirectory = $input->getOption('working-directory') ?: getcwd();
54+
55+
$finder = (new Finder())
56+
->files()
57+
->ignoreDotFiles(false)
58+
->in($workingDirectory);
59+
60+
$format = new SymfonyStyle($input, $output);
61+
62+
$serializer = new Serializer(
63+
[
64+
new CustomNormalizer(),
65+
new PropertyNormalizer(),
66+
],
67+
[
68+
new YamlEncoder(),
69+
]
70+
);
71+
72+
if ($finder->hasResults()) {
73+
/** @var SplFileInfo $file */
74+
foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) {
75+
try {
76+
/** @var \Kiboko\Cloud\Domain\Stack\DTO\Context $context */
77+
$context = $serializer->deserialize($file->getContents(), Context::class, 'yaml');
78+
} catch (\Throwable $exception) {
79+
$format->error($exception->getMessage());
80+
continue;
81+
}
82+
83+
break;
84+
}
85+
}
86+
87+
if (!isset($context)) {
88+
$format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command');
89+
90+
return 1;
91+
}
92+
93+
$env = $format->askQuestion(new ChoiceQuestion('For what environment ?', ['prod', 'dev', 'test'], 'prod'));
94+
95+
$application = new Application($this->console->getName());
96+
$deployer = new Deployer($application);
97+
$deployer['output'] = $output;
98+
99+
$hosts = [];
100+
$tasks = [];
101+
102+
/** @var Context $context */
103+
$host = new Host($context->deployment->server->hostname);
104+
$host->port($context->deployment->server->port);
105+
$host->user($context->deployment->server->username);
106+
array_push($hosts, $host);
107+
108+
$directories = explode('/', $workingDirectory);
109+
$projectName = end($directories);
110+
111+
$commands = [
112+
'cache:clear' => 'cd '.$context->deployment->path.'/'.$projectName.' && docker-compose exec -T sh bin/console cache:clear --env='.$env,
113+
'docker:restart-fpm' => 'cd '.$context->deployment->path.'/'.$projectName.' && docker-compose restart fpm',
114+
];
115+
116+
foreach ($commands as $key => $value) {
117+
array_push($tasks, new Task($key, function () use ($value, $host) {
118+
run($value);
119+
}));
120+
}
121+
122+
$seriesExecutor = new SeriesExecutor($input, $output, new Informer(new OutputWatcher($output)));
123+
$seriesExecutor->run($tasks, $hosts);
124+
125+
return 0;
126+
}
127+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Deployer\Logger\Handler\FileHandler;
1414
use Deployer\Logger\Handler\NullHandler;
1515
use Deployer\Logger\Logger;
16+
use function Deployer\run;
1617
use Deployer\Task\Task;
1718
use Deployer\Utility\ProcessOutputPrinter;
1819
use Deployer\Utility\Rsync;
@@ -30,7 +31,6 @@
3031
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
3132
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
3233
use Symfony\Component\Serializer\Serializer;
33-
use function Deployer\run;
3434

3535
final class DeployCommand extends Command
3636
{
@@ -48,7 +48,7 @@ public function __construct(?string $name, Console $console)
4848

4949
protected function configure()
5050
{
51-
$this->setDescription('Deploy the application to a remote server using rsync and initialize docker containers');
51+
$this->setDescription('Deploy the application to a remote server using rsync and initialize docker services');
5252

5353
$this->wizard->configureConsoleCommand($this);
5454
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Deployer\Deployer;
1111
use Deployer\Executor\SeriesExecutor;
1212
use Deployer\Host\Host;
13+
use function Deployer\run;
1314
use Deployer\Task\Task;
1415
use Kiboko\Cloud\Domain\Environment\DTO\Context;
1516
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
@@ -24,7 +25,6 @@
2425
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
2526
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
2627
use Symfony\Component\Serializer\Serializer;
27-
use function Deployer\run;
2828

2929
final class DestroyCommand extends Command
3030
{
@@ -42,7 +42,7 @@ public function __construct(?string $name, Console $console)
4242

4343
protected function configure()
4444
{
45-
$this->setDescription('Destroy the Docker infrastructure with associated volumes and remove remote directory');
45+
$this->setDescription('Destroy the docker infrastructure with associated volumes and remove remote directory');
4646

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

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

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

4444
protected function configure()
4545
{
46-
$this->setDescription('Deploy the application to a remote server using rsync and initialize docker containers');
46+
$this->setDescription('Synchronize remote directory according to local directory');
4747

4848
$this->wizard->configureConsoleCommand($this);
4949
}
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Cloud\Platform\Console\Command\Environment;
6+
7+
use Deployer\Console\Application;
8+
use Deployer\Console\Output\Informer;
9+
use Deployer\Console\Output\OutputWatcher;
10+
use Deployer\Deployer;
11+
use Deployer\Executor\SeriesExecutor;
12+
use Deployer\Host\Host;
13+
use function Deployer\run;
14+
use Deployer\Task\Task;
15+
use Kiboko\Cloud\Domain\Environment\DTO\Context;
16+
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
17+
use Symfony\Component\Console\Application as Console;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
22+
use Symfony\Component\Finder\Finder;
23+
use Symfony\Component\Finder\SplFileInfo;
24+
use Symfony\Component\Serializer\Encoder\YamlEncoder;
25+
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
26+
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
27+
use Symfony\Component\Serializer\Serializer;
28+
29+
final class StartCommand extends Command
30+
{
31+
public static $defaultName = 'environment:start';
32+
33+
private Console $console;
34+
private EnvironmentWizard $wizard;
35+
36+
public function __construct(?string $name, Console $console)
37+
{
38+
$this->console = $console;
39+
$this->wizard = new EnvironmentWizard();
40+
parent::__construct($name);
41+
}
42+
43+
protected function configure()
44+
{
45+
$this->setDescription('Start docker services on the remote server');
46+
47+
$this->wizard->configureConsoleCommand($this);
48+
}
49+
50+
protected function execute(InputInterface $input, OutputInterface $output)
51+
{
52+
$workingDirectory = $input->getOption('working-directory') ?: getcwd();
53+
54+
$finder = (new Finder())
55+
->files()
56+
->ignoreDotFiles(false)
57+
->in($workingDirectory);
58+
59+
$format = new SymfonyStyle($input, $output);
60+
61+
$serializer = new Serializer(
62+
[
63+
new CustomNormalizer(),
64+
new PropertyNormalizer(),
65+
],
66+
[
67+
new YamlEncoder(),
68+
]
69+
);
70+
71+
if ($finder->hasResults()) {
72+
/** @var SplFileInfo $file */
73+
foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) {
74+
try {
75+
/** @var \Kiboko\Cloud\Domain\Stack\DTO\Context $context */
76+
$context = $serializer->deserialize($file->getContents(), Context::class, 'yaml');
77+
} catch (\Throwable $exception) {
78+
$format->error($exception->getMessage());
79+
continue;
80+
}
81+
82+
break;
83+
}
84+
}
85+
86+
if (!isset($context)) {
87+
$format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command');
88+
89+
return 1;
90+
}
91+
92+
$application = new Application($this->console->getName());
93+
$deployer = new Deployer($application);
94+
$deployer['output'] = $output;
95+
96+
$hosts = [];
97+
$tasks = [];
98+
99+
/** @var Context $context */
100+
$host = new Host($context->deployment->server->hostname);
101+
$host->port($context->deployment->server->port);
102+
$host->user($context->deployment->server->username);
103+
array_push($hosts, $host);
104+
105+
$directories = explode('/', $workingDirectory);
106+
$projectName = end($directories);
107+
108+
$command = 'cd '.$context->deployment->path.'/'.$projectName.' && docker-compose start';
109+
110+
array_push($tasks, new Task('docker:start', function () use ($command, $host) {
111+
run($command);
112+
}));
113+
114+
$seriesExecutor = new SeriesExecutor($input, $output, new Informer(new OutputWatcher($output)));
115+
$seriesExecutor->run($tasks, $hosts);
116+
117+
return 0;
118+
}
119+
}

0 commit comments

Comments
 (0)