From 7d08636af4d1a76fafd194a1aea3ed156cdbecf0 Mon Sep 17 00:00:00 2001 From: Alexander Klimov Date: Tue, 11 Mar 2014 15:43:41 +0100 Subject: [PATCH] Handle missing config files more intelligent --- library/Icinga/Application/Config.php | 35 +++++++++---------- library/Icinga/Config/PreservingIniWriter.php | 6 +++- .../controllers/ConfigController.php | 15 +++----- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/library/Icinga/Application/Config.php b/library/Icinga/Application/Config.php index a7b5e0bfb..be0c43a60 100644 --- a/library/Icinga/Application/Config.php +++ b/library/Icinga/Application/Config.php @@ -29,6 +29,7 @@ namespace Icinga\Application; +use Zend_Config; use Zend_Config_Ini; use Icinga\Exception\NotReadableError; use Icinga\Exception\ProgrammingError; @@ -36,7 +37,7 @@ use Icinga\Exception\ProgrammingError; /** * Global registry of application and module configuration. */ -class Config extends Zend_Config_Ini +class Config extends Zend_Config { /** * Configuration directory where ALL (application and module) configuration is located @@ -66,6 +67,8 @@ class Config extends Zend_Config_Ini */ protected static $modules = array(); + private $instance; + /** * Load configuration from the config file $filename * @@ -75,19 +78,16 @@ class Config extends Zend_Config_Ini */ public function __construct($filename) { - $canonical = realpath($filename); - if ($canonical === false) { - throw new NotReadableError('Cannot read config file "' . $filename . '". Config file does not exist'); - } - if (!is_readable($canonical)) { + parent::__construct(array(), true); + $filepath = realpath($filename); + if ($filepath === false) { + $this->configFile = $filename; + } else if (is_readable($filepath)) { + $this->configFile = $filepath; + $this->merge(new Zend_Config_Ini($filepath)); + } else { throw new NotReadableError('Cannot read config file "' . $filename . '". Permission denied'); - }; - $this->configFile = $canonical; - $section = null; - $options = array( - 'allowModifications' => true - ); - parent::__construct($canonical, $section, $options); + } } /** @@ -124,12 +124,9 @@ class Config extends Zend_Config_Ini } $moduleConfigs = self::$modules[$modulename]; if (!isset($moduleConfigs[$configname]) || $fromDisk) { - $filename = self::$configDir . '/modules/' . $modulename . '/' . $configname . '.ini'; - if (file_exists($filename)) { - $moduleConfigs[$configname] = new Config(realpath($filename)); - } else { - $moduleConfigs[$configname] = null; - } + $moduleConfigs[$configname] = new Config( + self::$configDir . '/modules/' . $modulename . '/' . $configname . '.ini' + ); } return $moduleConfigs[$configname]; } diff --git a/library/Icinga/Config/PreservingIniWriter.php b/library/Icinga/Config/PreservingIniWriter.php index a33811cc4..d8dc8cf6d 100644 --- a/library/Icinga/Config/PreservingIniWriter.php +++ b/library/Icinga/Config/PreservingIniWriter.php @@ -71,7 +71,11 @@ class PreservingIniWriter extends Zend_Config_Writer_FileAbstract */ public function render() { - $oldconfig = new Zend_Config_Ini($this->_filename); + if (file_exists($this->_filename)) { + $oldconfig = new Zend_Config_Ini($this->_filename); + } else { + $oldconfig = new Zend_Config(array()); + } $newconfig = $this->_config; $editor = new IniEditor(file_get_contents($this->_filename), $this->options); $this->diffConfigs($oldconfig, $newconfig, $editor); diff --git a/modules/monitoring/application/controllers/ConfigController.php b/modules/monitoring/application/controllers/ConfigController.php index f70a637db..1afdf1b0c 100644 --- a/modules/monitoring/application/controllers/ConfigController.php +++ b/modules/monitoring/application/controllers/ConfigController.php @@ -261,18 +261,11 @@ class Monitoring_ConfigController extends BaseConfigController { */ private function writeConfiguration($config, $file) { - $configFile = IcingaConfig::module('monitoring', $file); - if ($configFile === null) { - throw new Exception('Configuration file is missing!'); - } else { - $configFile = $configFile->getConfigFile(); - } - $writer = new PreservingIniWriter(array( - 'filename' => $configFile, - 'config' => $config - )); - try { + $writer = new PreservingIniWriter(array( + 'filename' => IcingaConfig::module('monitoring', $file)->getConfigFile(), + 'config' => $config + )); $writer->write(); return true; } catch (Exception $exc) {