Merge branch 'feature/icinga1-config-12919'

This commit is contained in:
Thomas Gelf 2016-11-03 14:23:12 +01:00
commit fb74ac2b39
25 changed files with 336 additions and 45 deletions

View File

@ -88,6 +88,7 @@ class SettingsForm extends QuickForm
array(
'v2' => $this->translate('Icinga v2.x'),
'v1' => $this->translate('Icinga v1.x'),
// Hiding for now 'v1-masterless' => $this->translate('Icinga v1.x (no master)'),
)
),
'description' => $this->translate(

View File

@ -40,8 +40,9 @@ abstract class CustomVariable implements IcingaConfigRenderer
{
if ($this->type === null) {
$parts = explode('\\', get_class($this));
// strlen('CustomVariable') === 9
$this->type = substr(end($parts), 9);
$class = end($parts);
// strlen('CustomVariable') === 14
$this->type = substr($class, 14);
}
return $this->type;

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class CustomVariableArray extends CustomVariable
{
@ -79,4 +80,9 @@ class CustomVariableArray extends CustomVariable
$this->value[$key] = clone($value);
}
}
public function toLegacyConfigString()
{
return c1::renderArray($this->value);
}
}

View File

@ -45,4 +45,9 @@ class CustomVariableBoolean extends CustomVariable
{
return $this->value ? 'true' : 'false';
}
public function toLegacyConfigString()
{
return $this->toConfigString();
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Countable;
class CustomVariableDictionary extends CustomVariable implements Countable
@ -112,4 +113,9 @@ class CustomVariableDictionary extends CustomVariable implements Countable
{
return c::renderDictionary($this->value);
}
public function toLegacyConfigString()
{
return c1::renderDictionary($this->value);
}
}

View File

@ -44,4 +44,9 @@ class CustomVariableNull extends CustomVariable
{
return 'null';
}
public function toLegacyConfigString()
{
return $this->toConfigString();
}
}

View File

@ -65,4 +65,9 @@ class CustomVariableNumber extends CustomVariable
return sprintf(self::PRECISION, $this->value);
}
}
public function toLegacyConfigString()
{
return $this->toConfigString();
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class CustomVariableString extends CustomVariable
{
@ -44,4 +45,9 @@ class CustomVariableString extends CustomVariable
return c::renderString($this->getValue());
}
}
public function toLegacyConfigString()
{
return c1::renderString($this->getValue());
}
}

View File

@ -4,7 +4,9 @@ namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\Objects\IcingaHostVar;
use Icinga\Module\Director\Objects\IcingaObject;
use Countable;
use Exception;
@ -284,6 +286,41 @@ class CustomVariables implements Iterator, Countable, IcingaConfigRenderer
return $out;
}
public function toLegacyConfigString()
{
$out = '';
ksort($this->vars);
foreach ($this->vars as $key => $var) {
/** @var CustomVariable $var */
// TODO: ctype_alnum + underscore?
$value = null;
// vars with ARGn will be handled by IcingaObject::renderLegacyCheck_command
if (substr($key, 0, 3) == 'ARG') {
continue;
}
switch ($type = $var->getType()) {
case 'String':
# TODO: Make Prefetchable
$value = $var->toLegacyConfigString();
break;
default:
$out .= sprintf("# Unsupported var: %s (%s)\n", $key, $type);
}
if ($value !== null) {
$out .= c1::renderKeyValue('_' . $key, $value);
}
}
if ($out !== '') {
$out = "\n".$out;
}
return $out;
}
/**
* @param string $key
* @param CustomVariable $var

View File

@ -90,7 +90,7 @@ class IcingaConfig
public function isLegacy()
{
return $this->configFormat === 'v1';
return strpos($this->configFormat, 'v1') === 0;
}
public function getObjectCount()

View File

@ -26,4 +26,9 @@ class IcingaConfigRendered implements IcingaConfigRenderer
{
return $this->toConfigString();
}
public function toLegacyConfigString()
{
return $this->rendered;
}
}

View File

@ -5,5 +5,6 @@ namespace Icinga\Module\Director\IcingaConfig;
interface IcingaConfigRenderer
{
public function toConfigString();
public function toLegacyConfigString();
public function __toString();
}

View File

@ -43,7 +43,6 @@ class IcingaLegacyConfigHelper
{
$special = array(
'/\\\/',
'/"/',
'/\$/',
'/\t/',
'/\r/',
@ -54,7 +53,6 @@ class IcingaLegacyConfigHelper
$replace = array(
'\\\\\\',
'\\"',
'\\$',
'\\t',
'\\r',

View File

@ -393,4 +393,9 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
}
}
}
public function toLegacyConfigString()
{
return 'UNSUPPORTED';
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaCommand extends IcingaObject
{
@ -29,6 +30,8 @@ class IcingaCommand extends IcingaObject
protected $supportsArguments = true;
protected $supportedInLegacy = true;
protected $intervalProperties = array(
'timeout' => 'timeout',
);
@ -173,4 +176,25 @@ class IcingaCommand extends IcingaObject
{
self::$pluginDir = $pluginDir;
}
public function getLegacyObjectType()
{
// there is only one type of command in Icinga 1.x
return 'command';
}
protected function renderLegacyCommand()
{
$command = $this->command;
if (preg_match('~^(\$USER\d+\$/?)(.+)$~', $command)) {
// should be fine, since the user decided to use a macro
} elseif (! $this->isAbsolutePath($command)) {
$command = '$USER1$/'.$command;
}
return c1::renderKeyValue(
$this->getLegacyObjectType().'_line',
c1::renderString($command)
);
}
}

View File

@ -7,6 +7,7 @@ use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Data\PropertiesFilter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaHost extends IcingaObject
@ -82,6 +83,8 @@ class IcingaHost extends IcingaObject
protected $supportsFields = true;
protected $supportedInLegacy = true;
public static function enumProperties(
DbConnection $connection = null,
$prefix = '',
@ -334,6 +337,22 @@ class IcingaHost extends IcingaObject
return '';
}
protected function renderLegacyDisplay_Name()
{
return c1::renderKeyValue('display_name', $this->display_name);
}
protected function renderLegacyCustomExtensions()
{
$str = parent::renderLegacyCustomExtensions();
if (($alias = $this->vars()->get('alias')) !== null) {
$str .= c1::renderKeyValue('alias', $alias->getValue());
}
return $str;
}
public static function loadWithApiKey($key, Db $db)
{
$query = $db->getDbAdapter()

View File

@ -51,6 +51,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
/** @var bool Whether Sets of object can be defined */
protected $supportsSets = false;
/** @var bool If the object is rendered in legacy config */
protected $supportedInLegacy = false;
protected $rangeClass;
protected $type;
@ -1450,6 +1453,10 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $config;
}
public function isSupportedInLegacy()
{
return $this->supportedInLegacy;
}
public function renderToLegacyConfig(IcingaConfig $config)
{
@ -1457,40 +1464,39 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return;
}
if (! $this->isSupportedInLegacy()) {
$config->configFile(
'director/ignored-objects', '.cfg'
)->prepend(
sprintf(
"# Not supported for legacy config: %s object_name=%s\n",
get_class($this),
$this->getObjectName()
)
);
return;
}
$filename = $this->getRenderingFilename();
if ($config->isLegacy()) {
if (
$this->getResolvedProperty('zone_id')
&& array_key_exists('enable_active_checks', $this->defaultProperties)
&& $config->getConfigFormat() !== 'v1-masterless'
) {
$passive = clone($this);
$passive->enable_active_checks = false;
if ($this->getResolvedProperty('zone_id')) {
$a = clone($this);
$a->set('enable_active_checks', true);
$b = clone($this);
$a->set('enable_active_checks', false);
$config->configFile(
'director/master/' . $filename,
'.cfg'
)->addLegacyObject($a);
$config->configFile(
'director/' . $this->getRenderingZone($config) . '/' . $filename,
'.cfg'
)->addLegacyObject($b);
} else {
$config->configFile(
'director/' . $this->getRenderingZone($config) . '/' . $filename,
'.cfg'
)->addLegacyObject($this);
}
} else {
$config->configFile(
'director/' . $this->getRenderingZone($config) . '/' . $filename
)->addObject($this);
'director/master/' . $filename,
'.cfg'
)->addLegacyObject($passive);
}
$config->configFile(
'director/' . $this->getRenderingZone($config) . '/' . $filename,
'.cfg'
)->addLegacyObject($this);
}
public function renderToConfig(IcingaConfig $config)
@ -1605,6 +1611,20 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
);
}
/**
* Display Name only exists for host/service in Icinga 1
*
* Render it as alias for everything by default.
*
* Alias does not exist in Icinga 2 currently!
*
* @return string
*/
protected function renderLegacyDisplay_Name()
{
return c1::renderKeyValue('alias', $this->display_name);
}
protected function renderLegacyTimeout()
{
return '';
@ -1845,6 +1865,18 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
/**
* @return string
*/
protected function renderLegacyCustomVars()
{
if ($this->supportsCustomVars()) {
return $this->vars()->toLegacyConfigString();
} else {
return '';
}
}
/**
* @return string
*/
@ -1857,6 +1889,18 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
/**
* @return string
*/
protected function renderLegacyGroups()
{
if ($this->supportsGroups()) {
return $this->groups()->toLegacyConfigString();
} else {
return '';
}
}
/**
* @return string
*/
@ -1882,6 +1926,17 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
/**
* @return string
*/
protected function renderLegacyRanges()
{
if ($this->supportsRanges()) {
return $this->ranges()->toLegacyConfigString();
} else {
return '';
}
}
/**
* @return string
@ -1920,6 +1975,19 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
);
}
protected function renderLegacyCheck_command($value)
{
$args = array();
foreach($this->vars() as $k => $v) {
if (substr($k, 0, 3) == 'ARG') {
$args[] = $v->getValue();
}
}
array_unshift($args, $value);
return c1::renderKeyValue('check_command', join('!', $args));
}
/**
* We do not render zone properties, objects are stored to zone dirs
*
@ -1939,6 +2007,24 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return '';
}
protected function renderLegacyCustomExtensions()
{
$str = '';
// force rendering of check_command when ARG1 is set
if ($this->supportsCustomVars() && array_key_exists('check_command_id', $this->defaultProperties)) {
if (
$this->vars()->get('ARG1') !== null
&& $this->get('check_command') === null
) {
$command = $this->getResolvedRelated('check_command');
$str .= $this->renderLegacyCheck_command($command->getObjectName());
}
}
return $str;
}
protected function renderObjectHeader()
{
return sprintf(
@ -1949,9 +2035,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
);
}
public function getLegacyObjectType()
{
return strtolower($this->getType());
}
protected function renderLegacyObjectHeader()
{
$type = strtolower($this->getType());
$type = $this->getLegacyObjectType();
if ($this->isTemplate()) {
$name = c1::renderKeyValue(
@ -1995,21 +2086,23 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this->renderLegacyObjectHeader(),
$this->renderLegacyImports(),
$this->renderLegacyProperties(),
//$this->renderRanges(),
$this->renderLegacyRanges(),
//$this->renderArguments(),
//$this->renderRelatedSets(),
//$this->renderGroups(),
$this->renderLegacyGroups(),
//$this->renderMultiRelations(),
//$this->renderCustomExtensions(),
//$this->renderCustomVars(),
$this->renderLegacyCustomExtensions(),
$this->renderLegacyCustomVars(),
$this->renderLegacySuffix()
));
$str = $this->alignLegacyProperties($str);
if ($this->isDisabled()) {
return "/* --- This object has been disabled ---\n"
. $str . "*/\n";
return
"# --- This object has been disabled ---\n"
. preg_replace('~^~m', '# ', trim($str))
. "\n";
} else {
return $str;
}
@ -2023,11 +2116,13 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
foreach ($lines as &$line) {
if (preg_match('/^\s{4}([^\t]+)\t+(.+)$/', $line, $m)) {
if ($len - strlen($m[1]) < 0) {
var_dump($m);
exit;
$fill = ' ';
}
else {
$fill = str_repeat(' ', $len - strlen($m[1]));
}
$line = ' ' . $m[1] . str_repeat(' ', $len - strlen($m[1])) . $m[2];
$line = ' ' . $m[1] . $fill . $m[2];
}
}

View File

@ -11,6 +11,8 @@ abstract class IcingaObjectGroup extends IcingaObject
{
protected $supportsImports = true;
protected $supportedInLegacy = true;
protected $defaultProperties = array(
'id' => null,
'object_name' => null,

View File

@ -9,6 +9,7 @@ use Countable;
use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
{
@ -344,6 +345,18 @@ class IcingaObjectGroups implements Iterator, Countable, IcingaConfigRenderer
return c::renderKeyValue('groups', c::renderArray($groups));
}
public function toLegacyConfigString()
{
$groups = array_keys($this->groups);
if (empty($groups)) {
return '';
}
$type = $this->object->getLegacyObjectType();
return c1::renderKeyValue($type.'groups', c1::renderArray($groups));
}
public function __toString()
{
try {

View File

@ -8,6 +8,7 @@ use Iterator;
use Countable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRenderer
{
@ -414,4 +415,15 @@ class IcingaObjectMultiRelations implements Iterator, Countable, IcingaConfigRen
}
}
}
public function toLegacyConfigString()
{
$relations = array_keys($this->relations);
if (empty($relations)) {
return '';
}
return c1::renderKeyValue($this->propertyName, c1::renderArray($relations));
}
}

View File

@ -6,6 +6,7 @@ use Icinga\Data\Filter\Filter;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaService extends IcingaObject
{
@ -85,6 +86,8 @@ class IcingaService extends IcingaObject
protected $supportsSets = true;
protected $supportedInLegacy = true;
protected $keyName = array('host_id', 'service_set_id', 'object_name');
protected $prioritizedProperties = array('host_id');
@ -280,10 +283,15 @@ class IcingaService extends IcingaObject
public function renderUse_var_overrides()
{
// @codingStandardsIgnoreEnd
return '';
}
protected function renderLegacyDisplay_Name()
{
// @codingStandardsIgnoreEnd
return c1::renderKeyValue('display_name', $this->display_name);
}
public function hasCheckCommand()
{
return $this->getResolvedProperty('check_command_id') !== null;

View File

@ -28,6 +28,8 @@ class IcingaServiceSet extends IcingaObject
protected $supportsApplyRules = true;
protected $supportedInLegacy = true;
protected $relations = array(
'host' => 'IcingaHost',
);

View File

@ -22,6 +22,8 @@ class IcingaTimePeriod extends IcingaObject
protected $supportsRanges = true;
protected $supportedInLegacy = true;
protected $relations = array(
'zone' => 'IcingaZone',
);

View File

@ -7,6 +7,7 @@ use Iterator;
use Countable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRenderer
{
@ -286,4 +287,25 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
}
}
}
public function toLegacyConfigString()
{
if (empty($this->ranges) && $this->object->object_type === 'template') {
return '';
}
$out = '';
foreach ($this->ranges as $range) {
$out .= c1::renderKeyValue(
$range->range_key,
$range->range_value
);
}
if ($out !== '') {
$out = "\n".$out;
}
return $out;
}
}

View File

@ -8,6 +8,7 @@ use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Core\CoreApi;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Monitoring;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\Web\Form\FormLoader;
@ -31,6 +32,8 @@ abstract class ActionController extends Controller
/** @var Monitoring */
private $monitoring;
protected $icingaConfig;
public function init()
{
if ($this->getRequest()->isApiRequest()) {
@ -305,4 +308,12 @@ abstract class ActionController extends Controller
return $this->monitoring;
}
protected function IcingaConfig() {
if ($this->icingaConfig === null) {
$this->icingaConfig = new IcingaConfig($this->db);
}
return $this->icingaConfig;
}
}