2015-06-17 13:31:51 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-20 22:34:04 +02:00
|
|
|
namespace Icinga\Module\Director\Controllers;
|
|
|
|
|
2015-06-17 13:31:51 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
2015-06-23 14:37:23 +02:00
|
|
|
use Icinga\Module\Director\Util;
|
2015-06-30 11:27:32 +02:00
|
|
|
use Icinga\Module\Director\Web\Controller\ActionController;
|
2015-09-30 08:40:09 +02:00
|
|
|
use Icinga\Web\Notification;
|
2015-10-20 22:43:33 +02:00
|
|
|
use Icinga\Web\Url;
|
2016-02-28 16:52:37 +01:00
|
|
|
use Exception;
|
2015-06-17 13:31:51 +02:00
|
|
|
|
2015-10-20 22:34:04 +02:00
|
|
|
class ConfigController extends ActionController
|
2015-06-17 13:31:51 +02:00
|
|
|
{
|
2016-02-03 00:55:16 +01:00
|
|
|
protected $isApified = true;
|
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
public function deploymentsAction()
|
|
|
|
{
|
|
|
|
$this->setAutorefreshInterval(5);
|
|
|
|
try {
|
|
|
|
if ($this->getRequest()->getUrl()->shift('checkforchanges')) {
|
|
|
|
$this->fetchLogs();
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// No problem, Icinga might be reloading
|
|
|
|
}
|
|
|
|
$this->view->addLink = $this->view->qlink(
|
|
|
|
$this->translate('Render config'),
|
|
|
|
'director/config/store',
|
|
|
|
null,
|
|
|
|
array('class' => 'icon-wrench')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->overviewTabs()->activate('deploymentlog');
|
|
|
|
$this->view->title = $this->translate('Deployments');
|
|
|
|
$this->prepareTable('deploymentLog');
|
|
|
|
try {
|
|
|
|
// Move elsewhere
|
|
|
|
$this->view->table->setActiveStageName(
|
|
|
|
$this->api()->getActiveStageName()
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Don't care
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->render('objects/table', null, 'objects');
|
|
|
|
}
|
|
|
|
|
2015-09-30 08:40:09 +02:00
|
|
|
public function deployAction()
|
|
|
|
{
|
2016-02-19 12:53:47 +01:00
|
|
|
// TODO: require POST
|
2016-02-03 00:55:16 +01:00
|
|
|
$isApiRequest = $this->getRequest()->isApiRequest();
|
2015-09-30 08:40:09 +02:00
|
|
|
$checksum = $this->params->get('checksum');
|
2016-02-03 00:55:16 +01:00
|
|
|
if ($checksum) {
|
|
|
|
$config = IcingaConfig::load(Util::hex2binary($checksum), $this->db());
|
|
|
|
} else {
|
|
|
|
$config = IcingaConfig::generate($this->db());
|
|
|
|
$checksum = $config->getHexChecksum();
|
|
|
|
}
|
|
|
|
|
2015-09-30 08:40:09 +02:00
|
|
|
if ($this->api()->dumpConfig($config, $this->db())) {
|
2016-02-03 00:55:16 +01:00
|
|
|
if ($isApiRequest) {
|
|
|
|
return $this->sendJson((object) array('checksum' => $checksum));
|
|
|
|
} else {
|
2016-02-28 16:52:37 +01:00
|
|
|
$url = Url::fromPath('director/config/deployments?checkforchanges');
|
2016-02-03 00:55:16 +01:00
|
|
|
Notification::success(
|
|
|
|
$this->translate('Config has been submitted, validation is going on')
|
|
|
|
);
|
|
|
|
$this->redirectNow($url);
|
|
|
|
}
|
2015-09-30 08:40:09 +02:00
|
|
|
} else {
|
2016-02-03 00:55:16 +01:00
|
|
|
if ($isApiRequest) {
|
|
|
|
return $this->sendJsonError('Config deployment failed');
|
|
|
|
} else {
|
|
|
|
$url = Url::fromPath('director/config/show', array('checksum' => $checksum));
|
|
|
|
Notification::success(
|
|
|
|
$this->translate('Config deployment failed')
|
|
|
|
);
|
|
|
|
$this->redirectNow($url);
|
|
|
|
}
|
2015-09-30 08:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
public function activitiesAction()
|
|
|
|
{
|
|
|
|
$this->setAutorefreshInterval(10);
|
|
|
|
$this->overviewTabs()->activate('activitylog');
|
|
|
|
$this->view->title = $this->translate('Activity Log');
|
2016-03-16 22:45:29 +01:00
|
|
|
$lastDeployedId = $this->db()->getLastDeploymentActivityLogId();
|
|
|
|
$this->prepareTable('activityLog');
|
|
|
|
$this->view->table->setLastDeployedId($lastDeployedId);
|
|
|
|
$this->render('list/table', null, true);
|
2016-02-28 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function fetchLogs()
|
|
|
|
{
|
|
|
|
$api = $this->api();
|
|
|
|
$collected = false;
|
|
|
|
foreach ($this->db()->getUncollectedDeployments() as $deployment) {
|
|
|
|
$stage = $deployment->stage_name;
|
|
|
|
try {
|
|
|
|
$availableFiles = $api->listStageFiles($stage);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// This is not correct. We might miss logs as af an ongoing reload
|
|
|
|
$deployment->stage_collected = 'y';
|
|
|
|
$deployment->store();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('startup.log', $availableFiles)
|
|
|
|
&& in_array('status', $availableFiles)
|
|
|
|
) {
|
|
|
|
if ($api->getStagedFile($stage, 'status') === '0') {
|
|
|
|
$deployment->startup_succeeded = 'y';
|
|
|
|
} else {
|
|
|
|
$deployment->startup_succeeded = 'n';
|
|
|
|
}
|
|
|
|
$deployment->startup_log = $this->api()->getStagedFile($stage, 'startup.log');
|
|
|
|
}
|
|
|
|
$collected = true;
|
|
|
|
|
|
|
|
$deployment->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Not correct, we might clear logs we formerly skipped
|
|
|
|
if ($collected) {
|
|
|
|
// $api->wipeInactiveStages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 19:02:58 +01:00
|
|
|
// Show all files for a given config
|
|
|
|
public function filesAction()
|
|
|
|
{
|
2016-02-05 15:41:02 +01:00
|
|
|
$this->view->title = $this->translate('Generated config');
|
2015-12-15 19:02:58 +01:00
|
|
|
$tabs = $this->getTabs();
|
|
|
|
|
|
|
|
if ($deploymentId = $this->params->get('deployment_id')) {
|
|
|
|
$tabs->add('deployment', array(
|
|
|
|
'label' => $this->translate('Deployment'),
|
2015-12-16 16:19:58 +01:00
|
|
|
'url' => 'director/deployment',
|
2015-12-15 19:02:58 +01:00
|
|
|
'urlParams' => array(
|
|
|
|
'id' => $deploymentId
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$tabs->add('config', array(
|
|
|
|
'label' => $this->translate('Config'),
|
|
|
|
'url' => $this->getRequest()->getUrl(),
|
|
|
|
))->activate('config');
|
|
|
|
|
|
|
|
$checksum = $this->params->get('checksum');
|
|
|
|
|
|
|
|
$this->view->table = $this
|
|
|
|
->loadTable('GeneratedConfigFile')
|
|
|
|
->setConnection($this->db())
|
|
|
|
->setConfigChecksum($checksum);
|
|
|
|
|
2016-02-05 15:41:02 +01:00
|
|
|
$this->view->config = IcingaConfig::load(
|
|
|
|
Util::hex2binary($this->params->get('checksum')),
|
|
|
|
$this->db()
|
|
|
|
);
|
2015-12-15 19:02:58 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 10:54:38 +01:00
|
|
|
// Show a single file
|
|
|
|
public function fileAction()
|
|
|
|
{
|
|
|
|
$this->view->config = IcingaConfig::load(Util::hex2binary($this->params->get('config_checksum')), $this->db());
|
|
|
|
$filename = $this->view->filename = $this->params->get('file_path');
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Config file "%s"'),
|
|
|
|
$filename
|
|
|
|
);
|
|
|
|
$this->view->file = $this->view->config->getFile($filename);
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:31:51 +02:00
|
|
|
public function showAction()
|
|
|
|
{
|
2015-10-16 18:03:32 +02:00
|
|
|
$tabs = $this->getTabs();
|
|
|
|
|
|
|
|
if ($deploymentId = $this->params->get('deployment_id')) {
|
|
|
|
$tabs->add('deployment', array(
|
|
|
|
'label' => $this->translate('Deployment'),
|
|
|
|
'url' => 'director/deployment/show',
|
|
|
|
'urlParams' => array(
|
|
|
|
'id' => $deploymentId
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$tabs->add('config', array(
|
|
|
|
'label' => $this->translate('Config'),
|
|
|
|
'url' => $this->getRequest()->getUrl(),
|
|
|
|
))->activate('config');
|
|
|
|
|
2015-12-15 16:46:19 +01:00
|
|
|
$this->view->config = IcingaConfig::load(Util::hex2binary($this->params->get('checksum')), $this->db());
|
2015-06-17 13:31:51 +02:00
|
|
|
}
|
2015-06-17 18:57:51 +02:00
|
|
|
|
2015-12-15 16:46:19 +01:00
|
|
|
// TODO: Check if this can be removed
|
2015-06-17 18:57:51 +02:00
|
|
|
public function storeAction()
|
|
|
|
{
|
|
|
|
$config = IcingaConfig::generate($this->db());
|
2015-06-29 10:13:39 +02:00
|
|
|
$this->redirectNow(
|
2016-02-26 12:42:21 +01:00
|
|
|
Url::fromPath(
|
|
|
|
'director/config/show',
|
|
|
|
array('checksum' => $config->getHexChecksum())
|
|
|
|
)
|
2015-06-29 10:13:39 +02:00
|
|
|
);
|
2015-06-17 18:57:51 +02:00
|
|
|
}
|
2016-02-28 16:52:37 +01:00
|
|
|
|
|
|
|
protected function overviewTabs()
|
|
|
|
{
|
|
|
|
$this->view->tabs = $this->getTabs()->add(
|
|
|
|
'deploymentlog',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Deployments'),
|
|
|
|
'url' => 'director/config/deployments'
|
|
|
|
)
|
|
|
|
)->add(
|
|
|
|
'activitylog',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Activity Log'),
|
|
|
|
'url' => 'director/config/activities'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return $this->view->tabs;
|
|
|
|
}
|
2015-06-17 13:31:51 +02:00
|
|
|
}
|