Move ModuleController from incubator

The Notification dependency is removed and uses exceptions now,
otherwise it's mostly the same like in the incubator

refs #4092
This commit is contained in:
Jannis Moßhammer 2013-06-20 14:06:02 +02:00 committed by Marius Hein
parent 81e27689bb
commit 04a23f0096
9 changed files with 221 additions and 20 deletions

View File

@ -0,0 +1,47 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
# namespace Icinga\Application\Controllers;
use Icinga\Web\ActionController;
use Icinga\Application\Icinga;
class ModulesController extends ActionController
{
protected $manager;
public function init()
{
$this->manager = Icinga::app()->moduleManager();
}
public function indexAction()
{
$this->view->modules = $this->manager->select()
->from('modules')
->order('name');
$this->render('overview');
}
public function overviewAction()
{
$this->indexAction();
}
public function enableAction()
{
$this->manager->enableModule($this->_getParam('name'));
$this->redirectNow('modules/overview?_render=body');
}
public function disableAction()
{
$this->manager->disableModule($this->_getParam('name'));
$this->redirectNow('modules/overview?_render=body');
}
}

View File

@ -0,0 +1,3 @@
<?= $this->layout()->moduleStart ?>
<?= $this->layout()->content ?>
<?= $this->layout()->moduleEnd ?>

View File

@ -0,0 +1,61 @@
<? if ($this->pageCount > 1): ?>
<?php
if (is_array($this->preserve)) {
$preserve = $this->preserve;
} else {
$preserve = array();
}
$fromto = t('%d to %d of %d');
$total = $this->totalItemCount;
?>
<div class="pagination pagination-mini" style="margin:0px">
<ul>
<!-- Previous page link -->
<? if (isset($this->previous)): ?>
<li><a href="<?= $this->url($preserve + array('page' => $this->previous)); ?>" title="<?=
sprintf($fromto,
($this->current - 2) * $this->itemCountPerPage + 1,
($this->current - 1) * $this->itemCountPerPage,
$this->totalItemCount)
?>">« <?= t('Back') ?></a></li>
<? else: ?>
<li class="disabled"><span>« <?= t('Back') ?></span></li>
<?php
endif;
?>
<!-- Numbered page links -->
<? foreach ($this->pagesInRange as $page): ?>
<?php
$start = ($page - 1) * $this->itemCountPerPage + 1;
$end = $page * $this->itemCountPerPage;
if ($end > $total) {
$end = $total;
}
$title = sprintf($fromto, $start, $end, $total);
$active_class = $page === $this->current ? ' class="active"' : '';
?>
<?php if ($page === '...'): ?>
<li class="disabled"><span>...</span></li>
<?php else: ?>
<li<?= $active_class ?>><a href="<?= $this->url(
$preserve + array('page' => $page)
); ?>" title="<?= $title ?>"><?= $page; ?></a></li>
<? endif ?>
<? endforeach ?>
<!-- Next page link -->
<? if (isset($this->next)): ?>
<li><a href="<?= $this->url($preserve + array('page' => $this->next)); ?>" title="<?=
sprintf($fromto,
($this->current) * $this->itemCountPerPage + 1,
($this->current + 1) * $this->itemCountPerPage,
$total)
?>"><?= t('Next') ?> »</a></li>
<? else: ?>
<li class="disabled"><span><?= t('Next') ?> »</span></li>
<? endif ?>
</ul>
</div>
<? endif ?>

View File

@ -0,0 +1,38 @@
<?php
$this->modules->limit(10);
$modules = $this->modules->paginate();
?>
<h3>Installed Modules</h3>
<?= $this->paginationControl($modules, null, null, array(
'preserve' => $this->preserve
)); ?>
<table >
<tbody>
<? foreach ($modules as $module): ?>
<tr>
<td>
<? if ($module->enabled): ?>
<i class="icon-ok-sign"></i> <?= $this->qlink(
$module->name,
'modules/disable',
array('name' => $module->name),
array('target'=>'body')
) ?>
<? else: ?>
<i class="icon-remove-sign"></i> <?= $this->qlink(
$module->name,
'modules/enable',
array('name' => $module->name),
array('target'=>'body')
) ?>
<? endif ?>
(<?=
$module->enabled
? ($module->loaded ? 'enabled' : 'failed')
: 'disabled'
?>)</td>
</tr>
<? endforeach ?>
</tbody>
</table>

View File

@ -0,0 +1,24 @@
<?php $container = $this->container('modules-container',array(
"class" => "expanded_absolute"
))->beginContent() ?>
<table class="table table-bordered" style="width:100%">
<thead>
<tr style="text-align:left">
<th width="70%">Module</th>
<th width="15%">Type</th>
<th width="15%">Active</th>
</tr>
</thead>
<tbody>
<?php foreach($this->modules as $module): ?>
<tr>
<td><?= $module["name"] ?></td>
<td><?= $module["type"] ?></td>
<td><?= $module["active"] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?= $container; ?>

View File

@ -18,6 +18,7 @@ use Icinga\Exception\ConfigurationError;
* Usage example for CLI:
* <code>
* use Icinga\Application\Cli;
* Cli::start();
* </code>
*
@ -99,7 +100,7 @@ abstract class ApplicationBootstrap
public function moduleManager()
{
if ($this->moduleManager === null) {
$this->moduleManager = new ModuleManager($this);
$this->moduleManager = new ModuleManager($this, "/etc/icinga2/modules_enabled");
}
return $this->moduleManager;
}

View File

@ -6,6 +6,7 @@ use Icinga\Application\ApplicationBootstrap;
use Icinga\Data\ArrayDatasource;
use Icinga\Web\Notification;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\SystemPermissionException;
// TODO: show whether enabling/disabling modules is allowed by checking enableDir
// perms
@ -20,17 +21,20 @@ class Manager
protected $enableDir;
public function __construct(ApplicationBootstrap $app)
public function __construct(ApplicationBootstrap $app, $dir = null)
{
$this->app = $app;
$this->prepareEssentials();
if ($dir === null) {
$dir = $this->app->getConfig()->getConfigDir()
. '/enabledModules';
}
$this->prepareEssentials($dir);
$this->detectEnabledModules();
}
protected function prepareEssentials()
protected function prepareEssentials($moduleDir)
{
$this->enableDir = $this->app->getConfig()->getConfigDir()
. '/enabledModules';
$this->enableDir = $moduleDir;
if (! file_exists($this->enableDir) || ! is_dir($this->enableDir)) {
throw new ProgrammingError(
@ -99,13 +103,20 @@ class Manager
$target = $this->installedBaseDirs[$name];
$link = $this->enableDir . '/' . $name;
if (! is_writable($this->enableDir)) {
Notification::error("I do not have permissions to enable modules");
throw new SystemPermissionException(
"Insufficient system permissions for enabling modules",
"write",
$this->enableDir
);
}
if (file_exists($link) && is_link($link)) {
return $this;
}
if (@symlink($target, $link)) {
Notification::success("The module $name has been enabled");
} else {
Notification::error("Enabling module $name failed");
if (!@symlink($target, $link)) {
$error = error_get_last();
if (strstr($error["message"], "File exists") === false) {
throw new SystemPermissionException($error["message"], "symlink", $link);
}
}
return $this;
}
@ -116,16 +127,29 @@ class Manager
return $this;
}
if (! is_writable($this->enableDir)) {
Notification::error("I do not have permissions to disable modules");
throw new SystemPermissionException("Can't write the module directory", "write", $this->enableDir);
return $this;
}
$link = $this->enableDir . '/' . $name;
if (!file_exists($link)) {
throw new ConfigurationError("The module $name could not be found, can't disable it");
}
if (!is_link($link)) {
throw new ConfigurationError(
"The module $name can't be disabled as this would delete the whole module. ".
"It looks like you have installed this module manually and moved it to your module folder.".
"In order to dynamically enable and disable modules, you have to create a symlink to ".
"the enabled_modules folder"
);
}
if (file_exists($link) && is_link($link)) {
if (@unlink($link)) {
Notification::success("The module $name has been disabled");
} else {
Notification::error("Disabling module $name failed");
if (!@unlink($link)) {
$error = error_get_last();
throw new SystemPermissionException($error["message"], "unlink", $link);
}
} else {
}
return $this;
}

View File

@ -24,7 +24,7 @@
this.updateContainer = function(id,content,req) {
var target = id;
if (typeof id === "string") {
target = $('div[container-id='+id+']');
target = this.getContainer(id);
}
var ctrl = $('.container-controls',target);
target.html(content);
@ -100,6 +100,9 @@
};
this.getContainer = function(id) {
if(id == 'body') {
return $(document.body);
}
return $('div[container-id='+id+']');
};
@ -111,4 +114,4 @@
return containerMgrInstance;
});
})();
})();

View File

@ -10,7 +10,7 @@
};
var getDOMForDestination = function(destination) {
var target = destination;
if(typeof destination === "string") {
if (typeof destination === "string") {
target = containerMgr.getContainer(destination)[0];
} else if(typeof destination.context !== "undefined") {
target = destination[0];
@ -89,7 +89,7 @@
if(destination) {
pending.push({
request: req,
DOM: getDOMForDestination(destination)
DOM: getDOMForDestination(destination)
});
req.destination = destination;
}