Implement register and run php scripts for modules

refs #5597
This commit is contained in:
Marius Hein 2014-02-11 13:40:29 +01:00
parent 5a913881a6
commit 7b55f3a2bd
4 changed files with 71 additions and 59 deletions

View File

@ -244,18 +244,22 @@ class Manager
) )
); );
} }
clearstatcache(true); clearstatcache(true);
$target = $this->installedBaseDirs[$name]; $target = $this->installedBaseDirs[$name];
$link = $this->enableDir . '/' . $name; $link = $this->enableDir . '/' . $name;
if (!is_writable($this->enableDir)) { if (!is_writable($this->enableDir)) {
throw new SystemPermissionException( throw new SystemPermissionException(
'Can not enable module "' . $name . '". ' 'Can not enable module "' . $name . '". '
. 'Insufficient system permissions for enabling modules.' . 'Insufficient system permissions for enabling modules.'
); );
} }
if (file_exists($link) && is_link($link)) { if (file_exists($link) && is_link($link)) {
return $this; return $this;
} }
if (!@symlink($target, $link)) { if (!@symlink($target, $link)) {
$error = error_get_last(); $error = error_get_last();
if (strstr($error["message"], "File exists") === false) { if (strstr($error["message"], "File exists") === false) {
@ -266,7 +270,12 @@ class Manager
); );
} }
} }
$this->enabledDirs[$name] = $link; $this->enabledDirs[$name] = $link;
$this->loadModule($name);
$this->getModule($name)->launchRegisterScript();
return $this; return $this;
} }

View File

@ -101,6 +101,13 @@ class Module
*/ */
private $runScript; private $runScript;
/**
* Module initialization script
*
* @var string
*/
private $registerScript;
/** /**
* Module configuration script * Module configuration script
* *
@ -145,17 +152,18 @@ class Module
*/ */
public function __construct(ApplicationBootstrap $app, $name, $basedir) public function __construct(ApplicationBootstrap $app, $name, $basedir)
{ {
$this->app = $app; $this->app = $app;
$this->name = $name; $this->name = $name;
$this->basedir = $basedir; $this->basedir = $basedir;
$this->cssdir = $basedir. '/public/css'; $this->cssdir = $basedir . '/public/css';
$this->libdir = $basedir. '/library'; $this->libdir = $basedir . '/library';
$this->configdir = $basedir. '/config'; $this->configdir = $basedir . '/config';
$this->localedir = $basedir. '/application/locale'; $this->localedir = $basedir . '/application/locale';
$this->formdir = $basedir. '/application/forms'; $this->formdir = $basedir . '/application/forms';
$this->controllerdir = $basedir. '/application/controllers'; $this->controllerdir = $basedir . '/application/controllers';
$this->runScript = $basedir. '/register.php'; $this->runScript = $basedir . '/run.php';
$this->configScript = $basedir. '/configuration.php'; $this->registerScript = $basedir . '/register.php';
$this->configScript = $basedir . '/configuration.php';
} }
/** /**
@ -503,6 +511,16 @@ class Module
return $this; return $this;
} }
/**
* Run module registration script
*
* @return self
*/
public function launchRegisterScript()
{
return $this->includeScript($this->registerScript);
}
/** /**
* Run module bootstrap script * Run module bootstrap script
* *
@ -510,13 +528,26 @@ class Module
*/ */
protected function launchRunScript() protected function launchRunScript()
{ {
if (file_exists($this->runScript) return $this->includeScript($this->runScript);
&& is_readable($this->runScript)) { }
include($this->runScript);
/**
* Include a php script if it is readable
*
* @param string $file File to include
*
* @return self
*/
protected function includeScript($file)
{
if (is_readable($file) === true) {
include($file);
} }
return $this; return $this;
} }
/** /**
* Run module config script * Run module config script
*/ */

View File

@ -1,48 +1,10 @@
<?php <?php
/*
* register.php
*
* This file runs only on installation
*/
use Icinga\Authentication\Manager as AuthManager; use Icinga\Application\Logger;
use Icinga\Application\Icinga;
use Icinga\Module\Monitoring\DataView\StatusSummary as StatusSummaryView;
use Icinga\Web\Topbar;
if (Icinga::app()->isCli()) { Logger::debug('Monitor/register');
return;
}
$request = Icinga::app()->getFrontController()->getRequest();
if (AuthManager::getInstance()->isAuthenticated()) {
$hostSummary = StatusSummaryView::fromRequest(
$request,
array(
'hosts_up',
'hosts_unreachable_handled',
'hosts_unreachable_unhandled',
'hosts_down_handled',
'hosts_down_unhandled',
'hosts_pending'
)
)->getQuery()->fetchRow();
$serviceSummary = StatusSummaryView::fromRequest(
$request,
array(
'services_ok',
'services_critical_handled',
'services_critical_unhandled',
'services_warning_handled',
'services_warning_unhandled',
'services_unknown_handled',
'services_unknown_unhandled',
'services_pending'
)
)->getQuery()->fetchRow();
Topbar::addPartial(
'topbar.phtml',
'monitoring',
array('hostSummary' => $hostSummary, 'serviceSummary' => $serviceSummary)
);
}
?>

View File

@ -0,0 +1,10 @@
<?php
/*
* run.php
*
* This file runs every request to register runtime functionality
*
*/
use Icinga\Application\Logger;
Logger::debug('Monitor/run');