|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Cloud\Platform\Console\Command\Environment\Database; |
| 6 | + |
| 7 | +use Deployer\Console\Application; |
| 8 | +use Deployer\Deployer; |
| 9 | +use Deployer\Host\Host; |
| 10 | +use Deployer\Logger\Handler\FileHandler; |
| 11 | +use Deployer\Logger\Handler\NullHandler; |
| 12 | +use Deployer\Logger\Logger; |
| 13 | +use Kiboko\Cloud\Domain\Environment\DTO\Context as EnvironmentContext; |
| 14 | +use Kiboko\Cloud\Domain\Stack\DTO\Context as StackContext; |
| 15 | +use Kiboko\Cloud\Platform\Console\EnvironmentWizard; |
| 16 | +use Symfony\Component\Console\Application as Console; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 21 | +use Symfony\Component\Console\Question\Question; |
| 22 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 | +use Symfony\Component\Finder\Finder; |
| 24 | +use Symfony\Component\Process\Process; |
| 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 DumpCommand extends Command |
| 31 | +{ |
| 32 | + public static $defaultName = 'environment:database:dump'; |
| 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('Dumps the database in the current state'); |
| 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 | + foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $environmentFile) { |
| 74 | + try { |
| 75 | + /** @var EnvironmentContext $environementContext */ |
| 76 | + $environementContext = $serializer->deserialize($environmentFile->getContents(), EnvironmentContext::class, 'yaml'); |
| 77 | + } catch (\Throwable $exception) { |
| 78 | + $format->error($exception->getMessage()); |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + break; |
| 83 | + } |
| 84 | + foreach ($finder->name('/^\.?kloud.ya?ml$/') as $stackFile) { |
| 85 | + try { |
| 86 | + /** @var StackContext $stackContext */ |
| 87 | + $stackContext = $serializer->deserialize($stackFile->getContents(), StackContext::class, 'yaml'); |
| 88 | + } catch (\Throwable $exception) { |
| 89 | + $format->error($exception->getMessage()); |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (!isset($environementContext)) { |
| 98 | + $format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command'); |
| 99 | + |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + $application = new Application($this->console->getName()); |
| 104 | + $deployer = new Deployer($application); |
| 105 | + $deployer['output'] = $output; |
| 106 | + $deployer['log_handler'] = function ($deployer) { |
| 107 | + return !empty($deployer->config['log_file']) |
| 108 | + ? new FileHandler($deployer->config['log_file']) |
| 109 | + : new NullHandler(); |
| 110 | + }; |
| 111 | + $deployer['logger'] = function ($deployer) { |
| 112 | + return new Logger($deployer['log_handler']); |
| 113 | + }; |
| 114 | + |
| 115 | + $host = new Host($environementContext->deployment->server->hostname); |
| 116 | + $host->port($environementContext->deployment->server->port); |
| 117 | + $host->user($environementContext->deployment->server->username); |
| 118 | + |
| 119 | + $directories = explode('/', $workingDirectory); |
| 120 | + $projectName = end($directories); |
| 121 | + $remoteProjectPath = $environementContext->deployment->path.'/'.$projectName; |
| 122 | + |
| 123 | + $sqlService = $format->askQuestion(new Question('What is the name of your SQL service?', 'sql')); |
| 124 | + $process = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker-compose', 'ps', '-q', $sqlService]); |
| 125 | + |
| 126 | + try { |
| 127 | + $process->mustRun(); |
| 128 | + $containerIds = rtrim($process->getOutput(), PHP_EOL); |
| 129 | + } catch (\Exception $exception) { |
| 130 | + $format->error($exception->getMessage()); |
| 131 | + |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | + if (!empty($stackContext->dbms)) { |
| 136 | + $dbms = $stackContext->dbms; |
| 137 | + } else { |
| 138 | + $dbms = strtolower($format->askQuestion(new ChoiceQuestion('Is it a MySQL or PostgreSQL database?', ['MySQL', 'PostgreSQL']))); |
| 139 | + } |
| 140 | + |
| 141 | + $dumpName = $format->askQuestion(new Question('How do you want to name it?', 'dump.sql')); |
| 142 | + $dumpPath = $remoteProjectPath.'/.docker/'.$dumpName; |
| 143 | + $databaseName = $environementContext->database->databaseName; |
| 144 | + $username = $environementContext->database->username; |
| 145 | + $password = $environementContext->database->password; |
| 146 | + |
| 147 | + if ('postgresql' === $dbms) { |
| 148 | + $process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'docker', 'exec', '-i', $containerIds, 'pg_dump', '-U', $username, $databaseName, '>', $dumpPath]); |
| 149 | + try { |
| 150 | + $process2->mustRun(); |
| 151 | + $format->success('Dump well created at '.$host->getUser().'@'.$host->getHostname().':'.$dumpPath); |
| 152 | + |
| 153 | + return 0; |
| 154 | + } catch (\Exception $exception) { |
| 155 | + $format->error($exception->getMessage()); |
| 156 | + |
| 157 | + return 1; |
| 158 | + } |
| 159 | + } else { |
| 160 | + $process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'docker', 'exec', $containerIds, '/usr/bin/mysqldump', '-u', $username, '--password='.$password, $databaseName, '>', $dumpPath]); |
| 161 | + try { |
| 162 | + $process2->mustRun(); |
| 163 | + if (!file_exists($host->getUser().'@'.$host->getHostname().':'.$dumpPath)) { |
| 164 | + $format->error('Dump couldn\'t be created'); |
| 165 | + |
| 166 | + return 1; |
| 167 | + } |
| 168 | + $format->success('Dump well created at '.$host->getUser().'@'.$host->getHostname().':'.$dumpPath); |
| 169 | + |
| 170 | + return 0; |
| 171 | + } catch (\Exception $exception) { |
| 172 | + $format->error($exception->getMessage()); |
| 173 | + |
| 174 | + return 1; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments