Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
moreamazingnick committed Nov 18, 2024
1 parent 69bd61d commit 535a5a4
Show file tree
Hide file tree
Showing 26 changed files with 1,424 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
31 changes: 31 additions & 0 deletions application/clicommands/CreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Icinga\Module\Lshw\Clicommands;

use Icinga\Cli\Command;
use Icinga\Module\Lshw\DirectorConfig;


class CreateCommand extends Command
{
/**
* USAGE:
*
* icingacli selenium create command
*/
public function commandAction()
{
$config = new DirectorConfig();
$config->sync();
if($config->commandExists($config->createWindowsCommand()) && $config->commandExists($config->createLinuxCommand()) && $config->commandExists($config->createLinuxCommandUnprivileged())){
echo "Commands in sync\n";
exit(0);
}else{
echo "Commands not sync\n";
exit(1);
}

}

}
156 changes: 156 additions & 0 deletions application/controllers/DirectorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Icinga\Module\Lshw\Controllers;

use Exception;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Lshw\DirectorConfig;
use Icinga\Web\Notification;
use Icinga\Web\Widget\Tab;
use Icinga\Web\Widget\Tabs;
use ipl\Html\Html;
use ipl\Web\Compat\CompatController;
use ipl\Web\Url;
use ipl\Web\Widget\ActionLink;
use ipl\Web\Widget\Link;

class DirectorController extends CompatController
{
/**
* @throws \Icinga\Security\SecurityException
*/
public function init()
{
$this->assertPermission('config/modules');
}


protected function runFailSafe($callable)
{
try {
if (is_array($callable)) {
return call_user_func($callable);
} elseif (is_string($callable)) {
return call_user_func([$this, $callable]);
} else {
return $callable();
}
} catch (Exception $e) {
$this->addContent(
Html::tag('p', ['class' => 'state-hint error'], sprintf(
$this->translate('ERROR: %s'),
$e->getMessage()
))
);
// $this->addContent(Html::tag('pre', null, $e->getTraceAsString()));

return false;
}
}



public function indexAction()
{
$this->assertPermission('director/admin');
$this->mergeTabs($this->Module()->getConfigTabs()->activate('config/director'));

if ($this->params->get('action') === 'sync') {
$this->runFailSafe('sync');
return;
}
$this->addControl(new ActionLink(
'Sync to Director',
Url::fromPath('lshw/director', ['action' => 'sync']),
'sync'
));
$this->runFailSafe(function () {
try {
$config = new DirectorConfig();
$this->addCommand($config->createLinuxCommand(), $config);
$this->addCommand($config->createLinuxCommandUnprivileged(), $config);
$this->addCommand($config->createWindowsCommand(), $config);
} catch (ConfigurationError $e) {
$this->addContent(
Html::tag('h1', ['class' => 'state-hint error'], $this->translate(
'Icinga Director has not been configured on this system: %s',
$e->getMessage()
))
);
}
});
}

protected function sync()
{
$config = new DirectorConfig();
if ($config->sync()) {
Notification::success('Commands have been updated in Icinga Director');
} else {
Notification::success('Nothing changed, commands are fine');
}
$this->redirectNow($this->getRequest()->getUrl()->without('action'));
}

/**
* @param IcingaCommand $command
* @param DirectorConfig $config
*/
protected function addCommand(IcingaCommand $command, DirectorConfig $config)
{
$name = $command->getObjectName();
$this->addContent(Html::tag('h1', null, $name));
if ($config->commandExists($command)) {
$link = new Link(
$name,
Url::fromPath('director/command', ['name' => $name]),
['data-base-target' => '_next']
);

if ($config->commandDiffers($command)) {
$this->addContent($this->createHint(
Html::sprintf(
'The CheckCommand %s exists but differs in your Icinga Director',
$link
),
'warning'
));
} else {
$this->addContent($this->createHint(
Html::sprintf(
'The CheckCommand definition for %s is fine',
$link
),
'ok'
));
}
} else {
$this->addContent($this->createHint(
'Command does not exist in your Icinga Director',
'warning'
));
}
$this->addContent(Html::tag('pre', null, (string) $command));
}

protected function createHint($msg, $state)
{
return Html::tag('p', ['class' => ['state-hint', $state]], $msg);
}

/**
* Merge tabs with other tabs contained in this tab panel
*
* @param Tabs $tabs
*
* @return void
*/
protected function mergeTabs(Tabs $tabs): void
{
/** @var Tab $tab */
foreach ($tabs->getTabs() as $tab) {
$this->tabs->add($tab->getName(), $tab);
}
}
}
10 changes: 10 additions & 0 deletions configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$this->providePermission('config/lshw', $this->translate('allow access to lshw configuration'));

$this->provideConfigTab('config/director', array(
'title' => $this->translate('Director Configuration'),
'label' => $this->translate('Director Configuration'),
'url' => 'director'
));

?>
Loading

0 comments on commit 535a5a4

Please sign in to comment.