HostController: get rid of legacy view-based code

This commit is contained in:
Thomas Gelf 2017-07-03 21:46:04 +02:00
parent 319f14f94b
commit f0059a38fc
3 changed files with 95 additions and 124 deletions

View File

@ -8,13 +8,13 @@ use Icinga\Module\Director\Db\AppliedServiceSetLoader;
use Icinga\Module\Director\Exception\NestingError; use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\IcingaConfig\AgentWizard; use Icinga\Module\Director\IcingaConfig\AgentWizard;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet; use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Restriction\HostgroupRestriction; use Icinga\Module\Director\Restriction\HostgroupRestriction;
use Icinga\Module\Director\Util; use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Controller\ObjectController; use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Web\Url; use Icinga\Web\Url;
use ipl\Html\Html;
use ipl\Html\Link; use ipl\Html\Link;
class HostController extends ObjectController class HostController extends ObjectController
@ -95,23 +95,20 @@ class HostController extends ObjectController
$db = $this->db(); $db = $this->db();
$host = $this->object; $host = $this->object;
$this->view->addLink = $this->view->qlink( $this->tabs()->activate('services');
$this->addTitle($this->translate('Services: %s'), $host->object_name);
$this->actions()->add(Link::create(
$this->translate('Add service'), $this->translate('Add service'),
'director/service/add', 'director/service/add',
array('host' => $host->object_name), ['host' => $host->object_name],
array('class' => 'icon-plus') ['class' => 'icon-plus']
) . ' ' . $this->view->qlink( ))->add(Link::create(
$this->translate('Add service set'), $this->translate('Add service set'),
'director/serviceset/add', 'director/serviceset/add',
array('host' => $host->object_name), ['host' => $host->object_name],
array('class' => 'icon-plus') ['class' => 'icon-plus']
); ));
$this->getTabs()->activate('services');
$this->view->title = sprintf(
$this->translate('Services: %s'),
$host->object_name
);
$resolver = $this->object->templateResolver(); $resolver = $this->object->templateResolver();
@ -185,7 +182,9 @@ class HostController extends ObjectController
$tables[$title] = $table; $tables[$title] = $table;
} }
$this->view->tables = $tables; foreach ($tables as $table) {
$this->content()->add($table);
}
} }
protected function addHostServiceSetTables(IcingaHost $host, & $tables, IcingaHost $affectedHost = null) protected function addHostServiceSetTables(IcingaHost $host, & $tables, IcingaHost $affectedHost = null)
@ -240,17 +239,18 @@ class HostController extends ObjectController
'vars' => $host->getOverriddenServiceVars($serviceName), 'vars' => $host->getOverriddenServiceVars($serviceName),
), $db); ), $db);
$this->view->title = sprintf( $this->addTitle(
$this->translate('Applied service: %s'), $this->translate('Applied service: %s'),
$serviceName $serviceName
); );
$this->view->form = $this->loadForm('IcingaService') $this->content()->add(
->setDb($db) $this->loadForm('IcingaService')
->setHost($host) ->setDb($db)
->setApplyGenerated($parent) ->setHost($host)
->setObject($service) ->setApplyGenerated($parent)
; ->setObject($service)
);
$this->commonForServices(); $this->commonForServices();
} }
@ -282,20 +282,17 @@ class HostController extends ObjectController
'vars' => $host->getOverriddenServiceVars($serviceName), 'vars' => $host->getOverriddenServiceVars($serviceName),
), $db); ), $db);
$this->view->title = sprintf( $this->addTitle($this->translate('Inherited service: %s'), $serviceName);
$this->translate('Inherited service: %s'), $form = $this->loadForm('IcingaService')
$serviceName
);
$this->view->form = $this->loadForm('IcingaService')
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setInheritedFrom($from->object_name) ->setInheritedFrom($from->object_name)
->setObject($service); ->setObject($service);
$form->handleRequest();
// TODO: figure out whether this has any effect $this->content()->add($form);
// $this->view->form->setResolvedImports();
$this->commonForServices(); $this->commonForServices();
// TODO: figure out whether this has any effect
// $form->setResolvedImports();
} }
public function removesetAction() public function removesetAction()
@ -309,8 +306,10 @@ class HostController extends ObjectController
array('si' => 'icinga_service_set_inheritance'), array('si' => 'icinga_service_set_inheritance'),
'si.service_set_id = ss.id', 'si.service_set_id = ss.id',
array() array()
)->where('si.parent_service_set_id = ?', $this->params->get('setId')) )->where(
->where('ss.host_id = ?', $this->object->id); 'si.parent_service_set_id = ?',
$this->params->get('setId')
)->where('ss.host_id = ?', $this->object->id);
IcingaServiceSet::loadWithAutoIncId($db->fetchOne($query), $this->db())->delete(); IcingaServiceSet::loadWithAutoIncId($db->fetchOne($query), $this->db())->delete();
$this->redirectNow( $this->redirectNow(
@ -344,37 +343,35 @@ class HostController extends ObjectController
), $db); ), $db);
// $set->copyVarsToService($service); // $set->copyVarsToService($service);
$this->view->title = sprintf( $this->addTitle(
$this->translate('%s on %s (from set: %s)'), $this->translate('%s on %s (from set: %s)'),
$serviceName, $serviceName,
$host->getObjectName(), $host->getObjectName(),
$set->getObjectName() $set->getObjectName()
); );
$this->getTabs()->activate('services'); $form = $this->loadForm('IcingaService')
$this->view->form = $this->loadForm('IcingaService')
->setDb($db) ->setDb($db)
->setHost($host) ->setHost($host)
->setServiceSet($set) ->setServiceSet($set)
->setObject($service); ->setObject($service);
// $this->view->form->setResolvedImports(); $form->handleRequest();
$this->view->form->handleRequest(); $this->getTabs()->activate('services');
$this->content()->add($form);
// $form->setResolvedImports();
$this->commonForServices(); $this->commonForServices();
} }
protected function commonForServices() protected function commonForServices()
{ {
$host = $this->object; $host = $this->object;
$this->view->actionLinks = $this->view->qlink( $this->actions()->add(Link::create(
$this->translate('back'), $this->translate('back'),
'director/host/services', 'director/host/services',
array('name' => $host->object_name), ['name' => $host->object_name],
array('class' => 'icon-left-big') ['class' => 'icon-left-big']
); ));
$this->getTabs()->activate('services'); $this->getTabs()->activate('services');
$this->view->form->handleRequest();
$this->setViewScript('object/form');
} }
public function agentAction() public function agentAction()
@ -402,35 +399,71 @@ class HostController extends ObjectController
exit; exit;
} }
$c = $this->content();
$docBaseUrl = 'https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/distributed-monitoring';
$sectionSetup = 'distributed-monitoring-setup-satellite-client';
$sectionTopDown = 'distributed-monitoring-top-down';
$c->add(Html::p()->addf(
'Please check the %s for more related information.'
. ' The Director-assisted setup corresponds to configuring a %s environment.',
Html::a(
['href' => $docBaseUrl . '#' . $sectionSetup],
$this->translate('Icinga 2 Client documentation')
),
Html::a(
['href' => $docBaseUrl . '#' . $sectionTopDown],
$this->translate('Top Down')
)
));
$this->gracefullyActivateTab('agent'); $this->gracefullyActivateTab('agent');
$this->view->title = 'Agent deployment instructions'; $this->addTitle('Agent deployment instructions');
// TODO: Fail when no ticket $certname = $this->object->object_name;
$this->view->certname = $this->object->object_name;
try { try {
$this->view->ticket = Util::getIcingaTicket( $ticket = Util::getIcingaTicket($certname, $this->api()->getTicketSalt());
$this->view->certname, $wizard = new AgentWizard($this->object);
$this->api()->getTicketSalt()
);
$wizard = $this->view->wizard = new AgentWizard($this->object);
$wizard->setTicketSalt($this->api()->getTicketSalt()); $wizard->setTicketSalt($this->api()->getTicketSalt());
$this->view->windows = $wizard->renderWindowsInstaller();
$this->view->linux = $wizard->renderLinuxInstaller();
} catch (Exception $e) { } catch (Exception $e) {
$this->view->ticket = 'ERROR'; $c->add(Html::p(['class' => 'error'], sprintf(
$this->view->error = sprintf(
$this->translate( $this->translate(
'A ticket for this agent could not have been requested from' 'A ticket for this agent could not have been requested from'
. ' your deployment endpoint: %s' . ' your deployment endpoint: %s'
), ),
$e->getMessage() $e->getMessage()
); )));
return;
} }
$this->view->master = $this->db()->getDeploymentEndpointName(); // TODO: move to CSS
$this->view->masterzone = $this->db()->getMasterZoneName(); $codeStyle = ['style' => 'background: black; color: white; height: 14em; overflow: scroll;'];
$this->view->globalzone = $this->db()->getDefaultGlobalZoneName(); $c->add([
Html::h2($this->translate('For manual configuration')),
Html::p($this->translate('Ticket'), ': ', Html::code($ticket)),
Html::h2($this->translate('Windows Kickstart Script')),
Link::create(
$this->translate('Download'),
$this->url()->with('download', 'windows-kickstart'),
null,
['class' => 'icon-download', 'target' => '_blank']
),
Html::pre($codeStyle, $wizard->renderWindowsInstaller()),
Html::p($this->translate(
'This requires the Icinga Agent to be installed. It generates and signs'
. ' it\'s certificate and it also generates a minimal icinga2.conf to get'
. ' your agent connected to it\'s parents'
)),
Html::h2($this->translate('Linux commandline')),
Link::create(
$this->translate('Download'),
$this->url()->with('download', 'linux'),
null,
['class' => 'icon-download', 'target' => '_blank']
),
Html::p($this->translate('Just download and run this script on your Linux Client Machine:')),
Html::pre($codeStyle, $wizard->renderLinuxInstaller())
]);
} }
protected function handleApiRequest() protected function handleApiRequest()

View File

@ -1,47 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?></h1>
</div>
<div class="content">
<?php
$cert = $this->escape($this->certname);
$master = $this->escape($this->master);
?>
<p>Please check the <a href="https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/distributed-monitoring#distributed-monitoring-setup-satellite-client">Icinga 2 Client documentation</a> for more related information. The Director-assisted setup corresponds to configuring a <a href="https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/distributed-monitoring#distributed-monitoring-top-down">Top Down</a> environment.</p>
<?php if ($this->error): ?>
<p class="error"><?= $this->escape($this->error) ?></p>
</div>
<?php return; endif ?>
<h2>For manual configuration</h2>
<p>Ticket : <code><?= $this->escape($ticket) ?></code></p>
<h2>Windows Kickstart Script</h2>
<?= $this->qlink(
'Download',
$this->url()->with('download', 'windows-kickstart'),
null,
array('class' => 'icon-download', 'target' => '_blank')
) ?>
<pre style="background: black; color: white; height: 14em; overflow: scroll;">
<?= $this->escape($this->windows) ?>
</pre>
<p><?= $this->translate(
'This requires the Icinga Agent to be installed. It generates and signs'
. ' it\'s certificate and it also generates a minimal icinga2.conf to get'
. ' your agent connected to it\'s parents'
) ?></p>
<h2>Linux commandline</h2>
<?= $this->qlink(
'Download',
$this->url()->with('download', 'linux'),
null,
array('class' => 'icon-download', 'target' => '_blank')
) ?>
<p>Just download and run this script on your Linux Client Machine:</p>
<pre style="background: black; color: white; height: 14em; overflow: scroll;">
<?= $this->escape($this->linux) ?>
</pre>
</div>

View File

@ -1,15 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?></h1>
<span class="action-links">
<?= $this->addLink ?>
<?= $this->render('object/deploymentLink.phtml') ?>
</span>
</div>
<div class="content">
<?= $this->form ?>
<?php foreach ($this->tables as $key => $table): ?>
<?= $table->render() ?>
<?php endforeach ?>
</div>