From ae22456cebbe6e5eb83fd65f08eb7f9e2fc7d1fc Mon Sep 17 00:00:00 2001 From: nicolaschautard Date: Tue, 6 Oct 2020 15:59:23 +0200 Subject: [PATCH] #12 : Add cache clear, start and stop commands --- bin/kloud | 16 +++ .../Environment/Cache/ClearCommand.php | 127 ++++++++++++++++++ .../Command/Environment/DeployCommand.php | 4 +- .../Command/Environment/DestroyCommand.php | 4 +- .../Command/Environment/RsyncCommand.php | 2 +- .../Command/Environment/StartCommand.php | 119 ++++++++++++++++ .../Command/Environment/StopCommand.php | 119 ++++++++++++++++ .../Environment/Variable/GetCommand.php | 2 +- .../Environment/Variable/ListCommand.php | 2 +- .../Environment/Variable/SetCommand.php | 2 +- .../Environment/Variable/UnsetCommand.php | 2 +- 11 files changed, 390 insertions(+), 9 deletions(-) create mode 100644 src/Platform/Console/Command/Environment/Cache/ClearCommand.php create mode 100644 src/Platform/Console/Command/Environment/StartCommand.php create mode 100644 src/Platform/Console/Command/Environment/StopCommand.php diff --git a/bin/kloud b/bin/kloud index 313862c..adc17dd 100755 --- a/bin/kloud +++ b/bin/kloud @@ -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()); diff --git a/src/Platform/Console/Command/Environment/Cache/ClearCommand.php b/src/Platform/Console/Command/Environment/Cache/ClearCommand.php new file mode 100644 index 0000000..b497d2d --- /dev/null +++ b/src/Platform/Console/Command/Environment/Cache/ClearCommand.php @@ -0,0 +1,127 @@ +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; + } +} diff --git a/src/Platform/Console/Command/Environment/DeployCommand.php b/src/Platform/Console/Command/Environment/DeployCommand.php index 4b34de8..00b21c8 100644 --- a/src/Platform/Console/Command/Environment/DeployCommand.php +++ b/src/Platform/Console/Command/Environment/DeployCommand.php @@ -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; @@ -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 { @@ -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); } diff --git a/src/Platform/Console/Command/Environment/DestroyCommand.php b/src/Platform/Console/Command/Environment/DestroyCommand.php index 27faa0a..4ea0488 100644 --- a/src/Platform/Console/Command/Environment/DestroyCommand.php +++ b/src/Platform/Console/Command/Environment/DestroyCommand.php @@ -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; @@ -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 { @@ -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); } diff --git a/src/Platform/Console/Command/Environment/RsyncCommand.php b/src/Platform/Console/Command/Environment/RsyncCommand.php index 2b8bf50..4de23c5 100644 --- a/src/Platform/Console/Command/Environment/RsyncCommand.php +++ b/src/Platform/Console/Command/Environment/RsyncCommand.php @@ -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); } diff --git a/src/Platform/Console/Command/Environment/StartCommand.php b/src/Platform/Console/Command/Environment/StartCommand.php new file mode 100644 index 0000000..c12b3fa --- /dev/null +++ b/src/Platform/Console/Command/Environment/StartCommand.php @@ -0,0 +1,119 @@ +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; + } +} diff --git a/src/Platform/Console/Command/Environment/StopCommand.php b/src/Platform/Console/Command/Environment/StopCommand.php new file mode 100644 index 0000000..3164bed --- /dev/null +++ b/src/Platform/Console/Command/Environment/StopCommand.php @@ -0,0 +1,119 @@ +console = $console; + $this->wizard = new EnvironmentWizard(); + parent::__construct($name); + } + + protected function configure() + { + $this->setDescription('Stoptty 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 stop'; + + array_push($tasks, new Task('docker:stop', function () use ($command, $host) { + run($command); + })); + + $seriesExecutor = new SeriesExecutor($input, $output, new Informer(new OutputWatcher($output))); + $seriesExecutor->run($tasks, $hosts); + + return 0; + } +} diff --git a/src/Platform/Console/Command/Environment/Variable/GetCommand.php b/src/Platform/Console/Command/Environment/Variable/GetCommand.php index 8788fd4..37298a0 100644 --- a/src/Platform/Console/Command/Environment/Variable/GetCommand.php +++ b/src/Platform/Console/Command/Environment/Variable/GetCommand.php @@ -36,7 +36,7 @@ public function __construct(?string $name) protected function configure() { - $this->setDescription('Prints an environment variable'); + $this->setDescription('Print an environment variable value'); $this->wizard->configureConsoleCommand($this); } diff --git a/src/Platform/Console/Command/Environment/Variable/ListCommand.php b/src/Platform/Console/Command/Environment/Variable/ListCommand.php index aaae839..64e301d 100644 --- a/src/Platform/Console/Command/Environment/Variable/ListCommand.php +++ b/src/Platform/Console/Command/Environment/Variable/ListCommand.php @@ -34,7 +34,7 @@ public function __construct(?string $name) protected function configure() { - $this->setDescription('Prints the list of environment variable'); + $this->setDescription('Print the list of environment variables and their value'); $this->wizard->configureConsoleCommand($this); } diff --git a/src/Platform/Console/Command/Environment/Variable/SetCommand.php b/src/Platform/Console/Command/Environment/Variable/SetCommand.php index bfbaa58..957bcd4 100644 --- a/src/Platform/Console/Command/Environment/Variable/SetCommand.php +++ b/src/Platform/Console/Command/Environment/Variable/SetCommand.php @@ -39,7 +39,7 @@ public function __construct(?string $name) protected function configure() { - $this->setDescription('Prints an environment variable'); + $this->setDescription('Change an environment variable value'); $this->wizard->configureConsoleCommand($this); } diff --git a/src/Platform/Console/Command/Environment/Variable/UnsetCommand.php b/src/Platform/Console/Command/Environment/Variable/UnsetCommand.php index cdcdfc1..03af391 100644 --- a/src/Platform/Console/Command/Environment/Variable/UnsetCommand.php +++ b/src/Platform/Console/Command/Environment/Variable/UnsetCommand.php @@ -35,7 +35,7 @@ public function __construct(?string $name) protected function configure() { - $this->setDescription('Prints an environment variable'); + $this->setDescription('Unset an environment variable value'); $this->wizard->configureConsoleCommand($this); }