Skip to content

Commit

Permalink
Move ModuleController from incubator
Browse files Browse the repository at this point in the history
The Notification dependency is removed and uses exceptions now,
otherwise it's mostly the same like in the incubator

refs #4092
  • Loading branch information
Jannis Moßhammer committed Jun 21, 2013
1 parent 076b8a9 commit fd48948
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 20 deletions.
47 changes: 47 additions & 0 deletions application/controllers/ModulesController.php
Original file line number Diff line number Diff line change
@@ -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');
}

}
3 changes: 3 additions & 0 deletions application/views/scripts/inline.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?= $this->layout()->moduleStart ?>
<?= $this->layout()->content ?>
<?= $this->layout()->moduleEnd ?>
61 changes: 61 additions & 0 deletions application/views/scripts/mixedPagination.phtml
Original file line number Diff line number Diff line change
@@ -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 ?>
38 changes: 38 additions & 0 deletions application/views/scripts/modules/overview.phtml
Original file line number Diff line number Diff line change
@@ -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>
24 changes: 24 additions & 0 deletions application/views/scripts/modules/show.phtml
Original file line number Diff line number Diff line change
@@ -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; ?>
3 changes: 2 additions & 1 deletion library/Icinga/Application/ApplicationBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Usage example for CLI:
* <code>
* use Icinga\Application\Cli;
* Cli::start();
* </code>
*
Expand Down Expand Up @@ -99,7 +100,7 @@ abstract protected function bootstrap();
public function moduleManager()
{
if ($this->moduleManager === null) {
$this->moduleManager = new ModuleManager($this);
$this->moduleManager = new ModuleManager($this, "/etc/icinga2/modules_enabled");
}
return $this->moduleManager;
}
Expand Down
54 changes: 39 additions & 15 deletions library/Icinga/Application/Modules/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
Expand All @@ -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(
Expand Down Expand Up @@ -99,13 +103,20 @@ public function enableModule($name)
$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;
}
Expand All @@ -116,16 +127,29 @@ public function disableModule($name)
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;
}
Expand Down
7 changes: 5 additions & 2 deletions public/js/icinga/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -100,6 +100,9 @@
};

this.getContainer = function(id) {
if(id == 'body') {
return $(document.body);
}
return $('div[container-id='+id+']');
};

Expand All @@ -111,4 +114,4 @@
return containerMgrInstance;

});
})();
})();
4 changes: 2 additions & 2 deletions public/js/icinga/util/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -89,7 +89,7 @@
if(destination) {
pending.push({
request: req,
DOM: getDOMForDestination(destination)
DOM: getDOMForDestination(destination)
});
req.destination = destination;
}
Expand Down

0 comments on commit fd48948

Please sign in to comment.