|
| 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 | +} |
0 commit comments