Skip to content

Commit

Permalink
Merge pull request #22 from nanbando/feature/optional-global-config
Browse files Browse the repository at this point in the history
Changed requirement of global configuration to optional
  • Loading branch information
wachterjohannes authored Jun 12, 2016
2 parents 552fc63 + 4447501 commit 64a006f
Show file tree
Hide file tree
Showing 15 changed files with 497 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ wget http://nanbando.github.io/core/nanbando.phar.pubkey
chmod +x nanbando.phar
mv nanbando.phar /usr/local/bin/nanbando
mv nanbando.phar.pubkey /usr/local/bin/nanbando.pubkey
nanbando check
```

After first installation you can update the application with a built-in command.
Expand Down
13 changes: 11 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Configuration
=============

The configuration is devided into two parts - global and project configuration.
The configuration is devided into two parts - global (optional) and project configuration.

.. warning::

After changing configuration please run command ``reconfigure`` to be sure that the configuration will be used for
recreating the symfony container.

Global Configuration
--------------------
Expand All @@ -13,7 +18,7 @@ Put this configuration into ``~/.nanbando.yml``.
nanbando:
storage:
local_directory: "%home%/nanbando/local"
local_directory: "%home%/nanbando"
remote_service: filesystem.remote
oneup_flysystem:
Expand All @@ -36,6 +41,10 @@ Put this configuration into ``~/.nanbando.yml``.
For nanbando you have to define the local directory, where the backup command can place the backup archives, and the
remote filesystem-service which can be configured in the ``oneup_flysystem`` extension.

By default the ``local_directory`` will be set to ``%home%/nanbando`` and the ``remote_service`` will be ``null``. This
leads to local backups will work out of the box but all commands (``fetch``, ``push``) which needs the remote-storage
will be disabled.

Local Configuration
-------------------

Expand Down
2 changes: 2 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ After first installation you can update the application with a built-in command.
.. note::

The executable is signed with a OpenSSL private key. This ensures the origin of the build.

Check the configuration state of your application by using the command ``nanbando check``.
114 changes: 114 additions & 0 deletions src/Bundle/Command/CheckCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Nanbando\Bundle\Command;

use Nanbando\Core\Plugin\PluginRegistry;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CheckCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('check')
->setDescription('Checks configuration issues')
->setHelp(
<<<EOT
The <info>{$this->getName()}</info> command looks for configuration issues
EOT
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

$io->title('Configuration Check Report');

$io->writeln('Local directory: ' . $this->getContainer()->getParameter('nanbando.storage.local_directory'));

if (!$this->getContainer()->has('filesystem.remote')) {
$io->warning(
'No remote storage configuration found. This leads into disabled "fetch" and "push" commands.' .
'Please follow the documentation for global configuration.' . PHP_EOL . PHP_EOL .
'http://nanbando.readthedocs.io/en/latest/configuration.html#global-configuration'
);
} else {
$io->writeln('Remote Storage: YES');
}

$backups = $this->getContainer()->getParameter('nanbando.backup');
if (0 === count($backups)) {
$io->warning(
'No backup configuration found. Please follow the documentation for local configuration.' . PHP_EOL . PHP_EOL .
'http://nanbando.readthedocs.io/en/latest/configuration.html#local-configuration'
);
}

$this->checkBackups($io, $backups);

$io->writeln('');
}

/**
* Check backup-configuration.
*
* @param SymfonyStyle $io
* @param array $backups
*/
private function checkBackups(SymfonyStyle $io, array $backups)
{
/** @var PluginRegistry $plugins */
$plugins = $this->getContainer()->get('plugins');
foreach ($backups as $name => $backup) {
$this->checkBackup($plugins, $io, $name, $backup);
}
}

/**
* Check single backup-configuration.
*
* @param PluginRegistry $plugins
* @param SymfonyStyle $io
* @param string $name
* @param array $backup
*
* @return bool
*/
private function checkBackup(PluginRegistry $plugins, SymfonyStyle $io, $name, array $backup)
{
$io->section('Backup: ' . $name);
if (!$plugins->has($backup['plugin'])) {
$io->warning(sprintf('Plugin "%s" not found', $backup['plugin']));

return false;
}

$optionsResolver = new OptionsResolver();
$plugins->getPlugin($backup['plugin'])->configureOptionsResolver($optionsResolver);

try {
$optionsResolver->resolve($backup['parameter']);
} catch (InvalidArgumentException $e) {
$io->warning(sprintf('Parameter not valid' . PHP_EOL . PHP_EOL . 'Message: "%s"', $e->getMessage()));

return false;
}

$io->writeln('OK');

return true;
}
}
8 changes: 8 additions & 0 deletions src/Bundle/Command/FetchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$storage->fetch($file);
}
}

/**
* {@inheritdoc}
*/
public function isEnabled()
{
return $this->getContainer()->has('filesystem.remote');
}
}
8 changes: 8 additions & 0 deletions src/Bundle/Command/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$storage->push($file);
}
}

/**
* {@inheritdoc}
*/
public function isEnabled()
{
return $this->getContainer()->has('filesystem.remote');
}
}
6 changes: 5 additions & 1 deletion src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Webmozart\PathUtil\Path;

class Configuration implements ConfigurationInterface
{
Expand Down Expand Up @@ -31,8 +32,11 @@ public function getConfigTreeBuilder()
->end()
->end()
->arrayNode('storage')
->addDefaultsIfNotSet()
->children()
->scalarNode('local_directory')->end()
->scalarNode('local_directory')
->defaultValue(Path::join([Path::getHomeDirectory(), 'nanbando']))
->end()
->scalarNode('remote_service')->end()
->end()
->end()
Expand Down
7 changes: 6 additions & 1 deletion src/Bundle/DependencyInjection/NanbandoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('nanbando.temp', $config['temp']);
$container->setParameter('nanbando.backup', $config['backup']);
$container->setParameter('nanbando.storage.local_directory', $config['storage']['local_directory']);
$container->setParameter('nanbando.storage.remote_service', $config['storage']['remote_service']);

if (array_key_exists('remote_service', $config['storage'])
&& $config['storage']['remote_service'] !== 'filesystem.remote'
) {
$container->setAlias('filesystem.remote', $config['storage']['remote_service']);
}

$container->prependExtensionConfig(
'oneup_flysystem',
Expand Down
4 changes: 2 additions & 2 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
<service id="storage" class="Nanbando\Core\Storage\LocalStorage">
<argument type="string">%nanbando.name%</argument>
<argument type="service" id="temporary_files"/>
<argument type="service" id="filesystem.local"/>
<argument type="service" id="filesystem.remote"/>
<argument type="service" id="slugify"/>
<argument type="service" id="filesystem.local"/>
<argument type="service" id="filesystem.remote" on-invalid="null"/>
</service>

<!-- core service -->
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Flysystem/PrefixAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PrefixAdapter implements AdapterInterface
private $adapter;

/**
* @param string $root
* @param string $root
* @param AdapterInterface $adapter
*/
public function __construct($root, AdapterInterface $adapter)
Expand Down
18 changes: 15 additions & 3 deletions src/Core/Storage/LocalStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ class LocalStorage implements StorageInterface
/**
* @param string $name
* @param TemporaryFileManager $temporaryFileManager
* @param SlugifyInterface $slugify
* @param Filesystem $localFilesystem
* @param Filesystem $remoteFilesystem
* @param SlugifyInterface $slugify
*/
public function __construct(
$name,
TemporaryFileManager $temporaryFileManager,
SlugifyInterface $slugify,
Filesystem $localFilesystem,
Filesystem $remoteFilesystem,
SlugifyInterface $slugify
Filesystem $remoteFilesystem = null
) {
$this->name = $name;
$this->temporaryFileManager = $temporaryFileManager;
Expand Down Expand Up @@ -121,6 +121,10 @@ public function localListing()
*/
public function remoteListing()
{
if (!$this->remoteFilesystem) {
throw new RemoteStorageNotConfiguredException();
}

return $this->listing($this->remoteFilesystem);
}

Expand All @@ -142,6 +146,10 @@ public function size(Filesystem $filesystem)
*/
public function fetch($file)
{
if (!$this->remoteFilesystem) {
throw new RemoteStorageNotConfiguredException();
}

$path = sprintf('%s/%s.zip', $this->name, $file);

if (false === ($stream = $this->remoteFilesystem->readStream($path))) {
Expand All @@ -156,6 +164,10 @@ public function fetch($file)
*/
public function push($file)
{
if (!$this->remoteFilesystem) {
throw new RemoteStorageNotConfiguredException();
}

$path = sprintf('%s/%s.zip', $this->name, $file);

if (false === ($stream = $this->localFilesystem->readStream($path))) {
Expand Down
11 changes: 11 additions & 0 deletions src/Core/Storage/RemoteStorageNotConfiguredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Nanbando\Core\Storage;

class RemoteStorageNotConfiguredException extends \Exception
{
public function __construct()
{
parent::__construct('RemoteStorage was not configured.');
}
}
Loading

0 comments on commit 64a006f

Please sign in to comment.