monitoring/commands: Add `CommandTransport' factory
Instead of auto-detecting which transport class to use, the instances configuration now supports a new setting named `transport' with the possible values 'local' and 'remote'. refs #6593
This commit is contained in:
parent
6324192127
commit
4d353ac7bf
|
@ -0,0 +1,101 @@
|
||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Icinga\Module\Monitoring\Command\Transport;
|
||||||
|
|
||||||
|
use Zend_Config;
|
||||||
|
use Icinga\Application\Config;
|
||||||
|
use Icinga\Exception\ConfigurationError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command transport factory
|
||||||
|
*
|
||||||
|
* This class is subject to change as we do not have environments yet (#4471).
|
||||||
|
*/
|
||||||
|
abstract class CommandTransport
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transport configuration
|
||||||
|
*
|
||||||
|
* @var Config
|
||||||
|
*/
|
||||||
|
protected static $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get transport configuration
|
||||||
|
*
|
||||||
|
* @return Config
|
||||||
|
* @throws ConfigurationError
|
||||||
|
*/
|
||||||
|
public static function getConfig()
|
||||||
|
{
|
||||||
|
if (! isset(self::$config)) {
|
||||||
|
self::$config = Config::module('monitoring', 'instances');
|
||||||
|
if (self::$config->count() === 0) {
|
||||||
|
throw new ConfigurationError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return self::$config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a transport from config
|
||||||
|
*
|
||||||
|
* @param Zend_Config $config
|
||||||
|
*
|
||||||
|
* @return LocalCommandFile|RemoteCommandFile
|
||||||
|
* @throws ConfigurationError
|
||||||
|
*/
|
||||||
|
public static function fromConfig(Zend_Config $config)
|
||||||
|
{
|
||||||
|
switch (strtolower($config->transport)) {
|
||||||
|
case 'remote':
|
||||||
|
$transport = new RemoteCommandFile();
|
||||||
|
break;
|
||||||
|
case 'local':
|
||||||
|
case '': // Casting null to string is the empty string
|
||||||
|
$transport = new LocalCommandFile();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ConfigurationError;
|
||||||
|
}
|
||||||
|
unset($config->transport);
|
||||||
|
foreach ($config as $key => $value) {
|
||||||
|
$method = 'set' . ucfirst($key);
|
||||||
|
if (! method_exists($transport, $method)) {
|
||||||
|
throw new ConfigurationError;
|
||||||
|
}
|
||||||
|
$transport->$method($value);
|
||||||
|
}
|
||||||
|
return $transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a transport by name
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return LocalCommandFile|RemoteCommandFile
|
||||||
|
* @throws ConfigurationError
|
||||||
|
*/
|
||||||
|
public static function create($name)
|
||||||
|
{
|
||||||
|
$config = self::getConfig()->get($name);
|
||||||
|
if ($config === null) {
|
||||||
|
throw new ConfigurationError;
|
||||||
|
}
|
||||||
|
return self::fromConfig($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a transport by the first section of the configuration
|
||||||
|
*
|
||||||
|
* @return LocalCommandFile|RemoteCommandFile
|
||||||
|
*/
|
||||||
|
public static function first()
|
||||||
|
{
|
||||||
|
$config = self::getConfig()->current();
|
||||||
|
return self::fromConfig($config);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue