Skip to content

Commit

Permalink
#12 : Add cache clear, start and stop commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas43000 committed Oct 6, 2020
1 parent 890f79b commit ae22456
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 9 deletions.
16 changes: 16 additions & 0 deletions bin/kloud
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,26 @@ $app->addCommands([
$app,
)),

(new Command\Environment\StartCommand(
Command\Environment\StartCommand::$defaultName,
$app,
)),


(new Command\Environment\StopCommand(
Command\Environment\StopCommand::$defaultName,
$app,
)),

(new Command\Environment\RsyncCommand(
Command\Environment\RsyncCommand::$defaultName,
$app,
)),

(new Command\Environment\Cache\ClearCommand(
Command\Environment\Cache\ClearCommand::$defaultName,
$app,
)),
]);

$app->run(new ArgvInput($argv), new ConsoleOutput());
127 changes: 127 additions & 0 deletions src/Platform/Console/Command/Environment/Cache/ClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

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

use Deployer\Console\Application;
use Deployer\Console\Output\Informer;
use Deployer\Console\Output\OutputWatcher;
use Deployer\Deployer;
use Deployer\Executor\SeriesExecutor;
use Deployer\Host\Host;
use function Deployer\run;
use Deployer\Task\Task;
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\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
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 ClearCommand extends Command
{
public static $defaultName = 'environment:cache:clear';

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('Clear cache and restart FPM 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 \Kiboko\Cloud\Domain\Stack\DTO\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;
}

$env = $format->askQuestion(new ChoiceQuestion('For what environment ?', ['prod', 'dev', 'test'], 'prod'));

$application = new Application($this->console->getName());
$deployer = new Deployer($application);
$deployer['output'] = $output;

$hosts = [];
$tasks = [];

/** @var Context $context */
$host = new Host($context->deployment->server->hostname);
$host->port($context->deployment->server->port);
$host->user($context->deployment->server->username);
array_push($hosts, $host);

$directories = explode('/', $workingDirectory);
$projectName = end($directories);

$commands = [
'cache:clear' => 'cd '.$context->deployment->path.'/'.$projectName.' && docker-compose exec -T sh bin/console cache:clear --env='.$env,
'docker:restart-fpm' => 'cd '.$context->deployment->path.'/'.$projectName.' && docker-compose restart fpm',
];

foreach ($commands as $key => $value) {
array_push($tasks, new Task($key, function () use ($value, $host) {
run($value);
}));
}

$seriesExecutor = new SeriesExecutor($input, $output, new Informer(new OutputWatcher($output)));
$seriesExecutor->run($tasks, $hosts);

return 0;
}
}
4 changes: 2 additions & 2 deletions src/Platform/Console/Command/Environment/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Deployer\Logger\Handler\FileHandler;
use Deployer\Logger\Handler\NullHandler;
use Deployer\Logger\Logger;
use function Deployer\run;
use Deployer\Task\Task;
use Deployer\Utility\ProcessOutputPrinter;
use Deployer\Utility\Rsync;
Expand All @@ -30,7 +31,6 @@
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use function Deployer\run;

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

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

$this->wizard->configureConsoleCommand($this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Platform/Console/Command/Environment/DestroyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Deployer\Deployer;
use Deployer\Executor\SeriesExecutor;
use Deployer\Host\Host;
use function Deployer\run;
use Deployer\Task\Task;
use Kiboko\Cloud\Domain\Environment\DTO\Context;
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
Expand All @@ -24,7 +25,6 @@
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use function Deployer\run;

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

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

$this->wizard->configureConsoleCommand($this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Platform/Console/Command/Environment/RsyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(?string $name, Console $console)

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

$this->wizard->configureConsoleCommand($this);
}
Expand Down
119 changes: 119 additions & 0 deletions src/Platform/Console/Command/Environment/StartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

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

use Deployer\Console\Application;
use Deployer\Console\Output\Informer;
use Deployer\Console\Output\OutputWatcher;
use Deployer\Deployer;
use Deployer\Executor\SeriesExecutor;
use Deployer\Host\Host;
use function Deployer\run;
use Deployer\Task\Task;
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\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
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 StartCommand extends Command
{
public static $defaultName = 'environment:start';

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 docker services on the remote server');

$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 \Kiboko\Cloud\Domain\Stack\DTO\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;
}

$application = new Application($this->console->getName());
$deployer = new Deployer($application);
$deployer['output'] = $output;

$hosts = [];
$tasks = [];

/** @var Context $context */
$host = new Host($context->deployment->server->hostname);
$host->port($context->deployment->server->port);
$host->user($context->deployment->server->username);
array_push($hosts, $host);

$directories = explode('/', $workingDirectory);
$projectName = end($directories);

$command = 'cd '.$context->deployment->path.'/'.$projectName.' && docker-compose start';

array_push($tasks, new Task('docker:start', function () use ($command, $host) {
run($command);
}));

$seriesExecutor = new SeriesExecutor($input, $output, new Informer(new OutputWatcher($output)));
$seriesExecutor->run($tasks, $hosts);

return 0;
}
}
Loading

0 comments on commit ae22456

Please sign in to comment.