Handle missing config files more intelligent

This commit is contained in:
Alexander Klimov 2014-03-11 15:43:41 +01:00
parent 55dc579aa0
commit 7d08636af4
3 changed files with 25 additions and 31 deletions

View File

@ -29,6 +29,7 @@
namespace Icinga\Application; namespace Icinga\Application;
use Zend_Config;
use Zend_Config_Ini; use Zend_Config_Ini;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
@ -36,7 +37,7 @@ use Icinga\Exception\ProgrammingError;
/** /**
* Global registry of application and module configuration. * 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 * Configuration directory where ALL (application and module) configuration is located
@ -66,6 +67,8 @@ class Config extends Zend_Config_Ini
*/ */
protected static $modules = array(); protected static $modules = array();
private $instance;
/** /**
* Load configuration from the config file $filename * Load configuration from the config file $filename
* *
@ -75,19 +78,16 @@ class Config extends Zend_Config_Ini
*/ */
public function __construct($filename) public function __construct($filename)
{ {
$canonical = realpath($filename); parent::__construct(array(), true);
if ($canonical === false) { $filepath = realpath($filename);
throw new NotReadableError('Cannot read config file "' . $filename . '". Config file does not exist'); if ($filepath === false) {
} $this->configFile = $filename;
if (!is_readable($canonical)) { } 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'); 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]; $moduleConfigs = self::$modules[$modulename];
if (!isset($moduleConfigs[$configname]) || $fromDisk) { if (!isset($moduleConfigs[$configname]) || $fromDisk) {
$filename = self::$configDir . '/modules/' . $modulename . '/' . $configname . '.ini'; $moduleConfigs[$configname] = new Config(
if (file_exists($filename)) { self::$configDir . '/modules/' . $modulename . '/' . $configname . '.ini'
$moduleConfigs[$configname] = new Config(realpath($filename)); );
} else {
$moduleConfigs[$configname] = null;
}
} }
return $moduleConfigs[$configname]; return $moduleConfigs[$configname];
} }

View File

@ -71,7 +71,11 @@ class PreservingIniWriter extends Zend_Config_Writer_FileAbstract
*/ */
public function render() 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; $newconfig = $this->_config;
$editor = new IniEditor(file_get_contents($this->_filename), $this->options); $editor = new IniEditor(file_get_contents($this->_filename), $this->options);
$this->diffConfigs($oldconfig, $newconfig, $editor); $this->diffConfigs($oldconfig, $newconfig, $editor);

View File

@ -261,18 +261,11 @@ class Monitoring_ConfigController extends BaseConfigController {
*/ */
private function writeConfiguration($config, $file) 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 { try {
$writer = new PreservingIniWriter(array(
'filename' => IcingaConfig::module('monitoring', $file)->getConfigFile(),
'config' => $config
));
$writer->write(); $writer->write();
return true; return true;
} catch (Exception $exc) { } catch (Exception $exc) {