Skip to content

Commit

Permalink
#12 : Add database:load cmd and timeout(0) to database:dump cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas43000 committed Nov 20, 2020
1 parent 3b874f2 commit e70ba8b
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 3 deletions.
5 changes: 5 additions & 0 deletions bin/kloud
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ $app->addCommands([
Command\Environment\Database\DumpCommand::$defaultName,
$app,
)),

(new Command\Environment\Database\LoadCommand(
Command\Environment\Database\LoadCommand::$defaultName,
$app,
)),
]);

$app->run(new ArgvInput($argv), new ConsoleOutput());
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

$sqlService = $format->askQuestion(new Question('What is the name of your SQL service?', 'sql'));
$process = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker-compose', 'ps', '-q', $sqlService]);

try {
$process->mustRun();
$containerIds = rtrim($process->getOutput(), PHP_EOL);
Expand All @@ -130,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ('postgresql' === $dbms) {
$process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'docker', 'exec', '-i', $containerIds, 'pg_dump', '-U', $username, $databaseName, '>', $dumpPath]);
try {
$process2->mustRun();
$process2->setTimeout(0)->mustRun();
$format->success('Dump well created at '.$host->getUser().'@'.$host->getHostname().':'.$dumpPath);

return 0;
Expand All @@ -142,7 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
$process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'docker', 'exec', $containerIds, '/usr/bin/mysqldump', '-u', $username, '--password='.$password, $databaseName, '>', $dumpPath]);
try {
$process2->mustRun();
$process2->setTimeout(0)->mustRun();
$format->success('Dump well created at '.$host->getUser().'@'.$host->getHostname().':'.$dumpPath);

return 0;
Expand Down
155 changes: 155 additions & 0 deletions src/Platform/Console/Command/Environment/Database/LoadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

declare(strict_types=1);

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

use Deployer\Host\Host;
use Kiboko\Cloud\Domain\Environment\DTO\Context as EnvironmentContext;
use Kiboko\Cloud\Domain\Stack\DTO\Context as StackContext;
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\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
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 LoadCommand extends Command
{
public static $defaultName = 'environment:database:load';

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('Dumps the database in the current state');

$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()) {
foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $environmentFile) {
try {
/** @var EnvironmentContext $environementContext */
$environementContext = $serializer->deserialize($environmentFile->getContents(), EnvironmentContext::class, 'yaml');
} catch (\Throwable $exception) {
$format->error($exception->getMessage());
continue;
}

break;
}
foreach ($finder->name('/^\.?kloud.ya?ml$/') as $stackFile) {
try {
/** @var StackContext $stackContext */
$stackContext = $serializer->deserialize($stackFile->getContents(), StackContext::class, 'yaml');
} catch (\Throwable $exception) {
$format->error($exception->getMessage());
continue;
}

break;
}
}

if (!isset($environementContext)) {
$format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command');

return 1;
}

$host = new Host($environementContext->deployment->server->hostname);
$host->port($environementContext->deployment->server->port);
$host->user($environementContext->deployment->server->username);

$directories = explode('/', $workingDirectory);
$projectName = end($directories);
$remoteProjectPath = $environementContext->deployment->path.'/'.$projectName;

$sqlService = $format->askQuestion(new Question('What is the name of your SQL service?', 'sql'));
$process = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'cd', $remoteProjectPath, '&&', 'docker-compose', 'ps', '-q', $sqlService]);
try {
$process->mustRun();
$containerIds = rtrim($process->getOutput(), PHP_EOL);
} catch (\Exception $exception) {
$format->error($exception->getMessage());

return 1;
}

if (!empty($stackContext->dbms)) {
$dbms = $stackContext->dbms;
} else {
$dbms = strtolower($format->askQuestion(new ChoiceQuestion('Is it a MySQL or PostgreSQL database?', ['MySQL', 'PostgreSQL'])));
}

$dumpName = $format->askQuestion(new Question('Name of your SQL dump to load'));
$dumpPath = $remoteProjectPath.'/.docker/'.$dumpName;
$databaseName = $environementContext->database->databaseName;
$username = $environementContext->database->username;
$password = $environementContext->database->password;

if ('postgresql' === $dbms) {
$process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'docker', 'exec', '-i', $containerIds, 'psql', '-U', $username, $databaseName, '<', $dumpPath]);
try {
$process2->setTimeout(0)->mustRun();
$format->success('Dump well loaded');

return 0;
} catch (\Exception $exception) {
$format->error($exception->getMessage());

return 1;
}
} else {
$process2 = new Process(['ssh', '-t', $host->getUser().'@'.$host->getHostname(), 'docker', 'exec', '-i', $containerIds, 'mysql', '-u', $username, '--password='.$password, $databaseName, '<', $dumpPath]);
try {
$process2->setTimeout(0)->mustRun();
$format->success('Dump well loaded');

return 0;
} catch (\Exception $exception) {
$format->error($exception->getMessage());

return 1;
}
}
}
}

0 comments on commit e70ba8b

Please sign in to comment.