Implement {app} placeholder and Icinga\Config::resolvePath()

refs #4642
This commit is contained in:
Jannis Moßhammer 2013-08-27 17:17:09 +02:00
parent 3af7e2591b
commit 0b515e39ac
8 changed files with 64 additions and 27 deletions

View File

@ -1,6 +1,6 @@
[global] [global]
environment = "development" environment = "development"
timezone = "Europe/Berlin" timezone = "Europe/Minsk"
indexModule = "monitoring" indexModule = "monitoring"
indexController = "dashboard" indexController = "dashboard"
; The moduleFolder directive is currently not used anywhere but configureable ; The moduleFolder directive is currently not used anywhere but configureable
@ -13,16 +13,16 @@ timeFormat = "g:i A"
[logging] [logging]
; General log ; General log
enable = "1" enable = 1
type = "stream" type = "stream"
verbose = "1" verbose = "1"
target = "/etc/icinga2-web/icinga2.log" target = "{app}/var/log/icinga.log"
; For development and debug purposes: Logs additional (non critical) events to a ; For development and debug purposes: Logs additional (non critical) events to a
; seperate log ; seperate log
debug.enable = "1" debug.enable = 1
debug.type = "stream" debug.type = "stream"
debug.target = "/etc/icinga2-web/icinga2.debug.log" debug.target = "{app}/var/log/icinga.debug.log"
; Use ini store to store preferences on local disk ; Use ini store to store preferences on local disk
[preferences] [preferences]
@ -33,4 +33,4 @@ type = "ini"
;type=db ;type=db
;resource=icingaweb-mysql ;resource=icingaweb-mysql
configPath = "/vagrant/config/preferences" configPath = "{app}/config/preferences"

View File

@ -28,7 +28,8 @@
namespace Icinga\Application; namespace Icinga\Application;
use Zend_Config_Ini; use \Icinga\Exception\ProgrammingError;
use \Zend_Config_Ini;
/** /**
* Global registry of application and module configuration. * Global registry of application and module configuration.
@ -143,8 +144,32 @@ class Config extends Zend_Config_Ini
} }
} }
/**
* Return the application wide config file
*
* @return string
*/
public function getConfigFile() public function getConfigFile()
{ {
return $this->configFile; return $this->configFile;
} }
/**
* Return the input path with resolved path variables
*
* Currently only %app% is considered a path variable and points to the application paths
*
* @param string $path The path to resolve
*
* @return string The resolved path
*/
public static function resolvePath($path)
{
try {
$appDir = realpath(Icinga::app()->getApplicationDir() . '/..');
} catch (ProgrammingError $appNotStarted) {
$appDir = realpath(__DIR__ . '/../../..');
}
return str_replace('{app}', $appDir, $path);
}
} }

View File

@ -190,7 +190,7 @@ final class Logger
try { try {
/** @var Zend_Log_Writer_Abstract $writer */ /** @var Zend_Log_Writer_Abstract $writer */
$writer = new $writerClass($target); $writer = new $writerClass(Config::resolvePath($target));
$writer->addFilter(new Zend_Log_Filter_Priority($priority)); $writer->addFilter(new Zend_Log_Filter_Priority($priority));
$this->logger->addWriter($writer); $this->logger->addWriter($writer);
$this->writers[] = $writer; $this->writers[] = $writer;

View File

@ -249,7 +249,8 @@ class Web extends ApplicationBootstrap
$preferences->attach($sessionStore); $preferences->attach($sessionStore);
if ($this->getConfig()->preferences !== null) { if ($this->getConfig()->preferences !== null) {
if (is_dir($this->getConfig()->preferences->configPath) === false) { $path = Config::resolvePath($this->getConfig()->preferences->configPath);
if (is_dir($path) === false) {
Logger::error( Logger::error(
'Path for preferences not found (IniStore, "%s"). Using default one: "%s"', 'Path for preferences not found (IniStore, "%s"). Using default one: "%s"',
$this->getConfig()->preferences->configPath, $this->getConfig()->preferences->configPath,

View File

@ -28,13 +28,16 @@
namespace Icinga\User\Preferences; namespace Icinga\User\Preferences;
use Icinga\Application\Logger;
use Icinga\Protocol\Ldap\Exception;
use \SplObserver; use \SplObserver;
use \SplSubject; use \SplSubject;
use Icinga\User; use \Icinga\User;
use Icinga\User\Preferences; use \Icinga\User\Preferences;
use Icinga\Exception\ConfigurationError; use \Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError; use \Icinga\Exception\ProgrammingError;
use \Zend_Config; use \Zend_Config;
use \Icinga\Application\Config as IcingaConfig;
use \Zend_Config_Writer_Ini; use \Zend_Config_Writer_Ini;
/** /**
@ -99,6 +102,7 @@ class IniStore implements LoadInterface, FlushObserverInterface
*/ */
public function setConfigPath($configPath) public function setConfigPath($configPath)
{ {
$configPath = IcingaConfig::resolvePath($configPath);
if (!is_dir($configPath)) { if (!is_dir($configPath)) {
throw new ConfigurationError('Config dir dos not exist: '. $configPath); throw new ConfigurationError('Config dir dos not exist: '. $configPath);
} }
@ -124,7 +128,7 @@ class IniStore implements LoadInterface, FlushObserverInterface
if (file_exists($this->preferencesFile) === false) { if (file_exists($this->preferencesFile) === false) {
$this->createDefaultIniFile(); $this->createDefaultIniFile();
} }
try {
$this->iniConfig = new Zend_Config( $this->iniConfig = new Zend_Config(
parse_ini_file($this->preferencesFile), parse_ini_file($this->preferencesFile),
true true
@ -136,6 +140,10 @@ class IniStore implements LoadInterface, FlushObserverInterface
'filename' => $this->preferencesFile 'filename' => $this->preferencesFile
) )
); );
} catch (Exception $e) {
Logger::error('Could not create IniStore backend: %s', $e->getMessage());
throw new \RuntimeException("Creating user preference backend failed");
}
} }
/** /**

View File

@ -29,6 +29,7 @@
namespace Icinga\Web\Form\Validator; namespace Icinga\Web\Form\Validator;
use \Zend_Validate_Abstract; use \Zend_Validate_Abstract;
use \Icinga\Application\Config as IcingaConfig;
/** /**
* Validator that interprets the value as a path and checks if it's writable * Validator that interprets the value as a path and checks if it's writable
@ -77,8 +78,9 @@ class WritablePathValidator extends Zend_Validate_Abstract
public function isValid($value, $context = null) public function isValid($value, $context = null)
{ {
$value = (string) $value; $value = (string) $value;
$this->_setValue($value);
$this->_setValue($value);
$value = IcingaConfig::resolvePath($value);
if ($this->requireExistence && !file_exists($value)) { if ($this->requireExistence && !file_exists($value)) {
$this->_error('DOES_NOT_EXIST'); $this->_error('DOES_NOT_EXIST');
return false; return false;

View File

@ -30,6 +30,7 @@ namespace Test\Icinga\Web\Form\Validator;
require_once('Zend/Validate/Abstract.php'); require_once('Zend/Validate/Abstract.php');
require_once(realpath('../../library/Icinga/Web/Form/Validator/WritablePathValidator.php')); require_once(realpath('../../library/Icinga/Web/Form/Validator/WritablePathValidator.php'));
require_once(realpath('../../library/Icinga/Application/Config.php'));
use \PHPUnit_Framework_TestCase; use \PHPUnit_Framework_TestCase;
use \Icinga\Web\Form\Validator\WritablePathValidator; use \Icinga\Web\Form\Validator\WritablePathValidator;