Add databox mount command

This commit is contained in:
Thibaud Fabre
2016-10-17 13:28:06 +02:00
parent 43d0599eba
commit 32fe54ac35
6 changed files with 93 additions and 5 deletions

View File

@@ -26,7 +26,7 @@ use Alchemy\Phrasea\Command\WebsocketServer;
use Alchemy\Phrasea\Core\Version;
use Alchemy\Phrasea\Command\BuildMissingSubdefs;
use Alchemy\Phrasea\Command\CreateCollection;
use Alchemy\Phrasea\Command\CreateDataboxCommand;
use Alchemy\Phrasea\Command\Databox\CreateDataboxCommand;
use Alchemy\Phrasea\Command\MailTest;
use Alchemy\Phrasea\Command\Compile\Configuration;
use Alchemy\Phrasea\Command\RecordAdd;

View File

@@ -1,7 +1,8 @@
<?php
namespace Alchemy\Phrasea\Command;
namespace Alchemy\Phrasea\Command\Databox;
use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Databox\DataboxConnectionSettings;
use Alchemy\Phrasea\Databox\DataboxService;
use Alchemy\Phrasea\Model\Repositories\UserRepository;

View File

@@ -0,0 +1,54 @@
<?php
namespace Alchemy\Phrasea\Command\Databox;
use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Databox\DataboxConnectionSettings;
use Alchemy\Phrasea\Databox\DataboxService;
use Alchemy\Phrasea\Model\Repositories\UserRepository;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class MountDataboxCommand extends Command
{
protected function configure()
{
$this->setName('databox:mount')
->addArgument('databox', InputArgument::REQUIRED, 'Database name for the databox', null)
->addArgument('owner', InputArgument::REQUIRED, 'Email of the databox admin user', null)
->addOption('connection', 'c', InputOption::VALUE_NONE, 'Flag to set new database settings')
->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host', 'localhost')
->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port', 3306)
->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user', 'phrasea')
->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'MySQL server password', null);
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$databoxName = $input->getArgument('databox');
$connectionSettings = $input->getOption('connection') == false ? null : new DataboxConnectionSettings(
$input->getOption('db-host'),
$input->getOption('db-port'),
$input->getOption('db-user'),
$input->getOption('db-password')
);
/** @var UserRepository $userRepository */
$userRepository = $this->container['repo.users'];
/** @var DataboxService $databoxService */
$databoxService = $this->container['databox.service'];
$owner = $userRepository->findByEmail($input->getArgument('owner'));
$databoxService->mountDatabox(
$databoxName,
$owner,
$connectionSettings
);
$output->writeln('Databox mounted');
}
}

View File

@@ -200,6 +200,7 @@ class DataboxesController extends Controller
&& (null !== $passwordDb = $request->request->get('new_password'))
) {
$connection = $this->getApplicationBox()->get_connection();
try {
$connection->beginTransaction();
$base = \databox::mount($this->app, $hostname, $port, $userDb, $passwordDb, $dbName);

View File

@@ -2,6 +2,7 @@
namespace Alchemy\Phrasea\Core\Provider;
use Alchemy\Phrasea\Application as PhraseaApplication;
use Alchemy\Phrasea\Databox\DataboxService;
use Silex\Application;
use Silex\ServiceProviderInterface;
@@ -11,9 +12,10 @@ class DataboxServiceProvider implements ServiceProviderInterface
public function register(Application $app)
{
$app['databox.service'] = $app->share(function (Application $app) {
$app['databox.service'] = $app->share(function (PhraseaApplication $app) {
return new DataboxService(
$app,
$app->getApplicationBox(),
$app['dbal.provider'],
$app['repo.databoxes'],
$app['conf'],

View File

@@ -18,6 +18,11 @@ class DataboxService
*/
private $app;
/**
* @var \appbox
*/
private $applicationBox;
/**
* @var PropertyAccess
*/
@@ -40,6 +45,7 @@ class DataboxService
/**
* @param Application $application
* @param \appbox $appbox
* @param callable $connectionFactory
* @param DataboxRepository $databoxRepository
* @param PropertyAccess $defaultDbConfiguration
@@ -47,12 +53,14 @@ class DataboxService
*/
public function __construct(
Application $application,
\appbox $appbox,
callable $connectionFactory,
DataboxRepository $databoxRepository,
PropertyAccess $defaultDbConfiguration,
$rootPath
) {
$this->app = $application;
$this->applicationBox = $appbox;
$this->connectionFactory = $connectionFactory;
$this->databoxRepository = $databoxRepository;
$this->configuration = $defaultDbConfiguration;
@@ -98,14 +106,36 @@ class DataboxService
}
/**
* @param $databaseName
* @param string $databaseName
* @param User $owner
* @param DataboxConnectionSettings $connectionSettings
* @return \databox
*/
public function mountDatabox($databaseName, DataboxConnectionSettings $connectionSettings = null)
public function mountDatabox($databaseName, User $owner, DataboxConnectionSettings $connectionSettings = null)
{
$connectionSettings = $connectionSettings ?: DataboxConnectionSettings::fromArray(
$this->configuration->get(['main', 'database'])
);
$this->applicationBox->get_connection()->beginTransaction();
try {
$databox = \databox::mount(
$this->app,
$connectionSettings->getHost(),
$connectionSettings->getPort(),
$connectionSettings->getUser(),
$connectionSettings->getPassword(),
$databaseName
);
$databox->registerAdmin($owner);
$this->applicationBox->get_connection()->commit();
}
catch (\Exception $exception) {
$this->applicationBox->get_connection()->rollBack();
throw new \RuntimeException($exception->getMessage(), 0, $exception);
}
}
}