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;
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];
}

View File

@ -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);

View File

@ -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) {