2015-06-17 13:31:51 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-20 22:34:04 +02:00
|
|
|
namespace Icinga\Module\Director\Controllers;
|
|
|
|
|
2016-05-02 10:42:53 +02:00
|
|
|
use Icinga\Module\Director\ConfigDiff;
|
2017-07-13 15:23:28 +02:00
|
|
|
use Icinga\Module\Director\Forms\SettingsForm;
|
2015-06-17 13:31:51 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
2016-09-05 21:16:35 +02:00
|
|
|
use Icinga\Module\Director\Settings;
|
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-11-03 16:06:18 +01:00
|
|
|
protected function checkDirectorPermissions()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
public function deploymentsAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/deploy');
|
2016-02-28 16:52:37 +01:00
|
|
|
try {
|
2016-10-30 11:52:14 +01:00
|
|
|
if ($this->db()->hasUncollectedDeployments()) {
|
2016-11-09 20:37:33 +01:00
|
|
|
$this->setAutorefreshInterval(5);
|
2016-03-20 18:50:18 +01:00
|
|
|
$this->api()->collectLogFiles($this->db());
|
2016-11-09 20:37:33 +01:00
|
|
|
} else {
|
|
|
|
$this->setAutorefreshInterval(10);
|
2016-02-28 16:52:37 +01:00
|
|
|
}
|
|
|
|
} 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-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/deploy');
|
|
|
|
|
2016-02-19 12:53:47 +01:00
|
|
|
// TODO: require POST
|
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();
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:56:22 +01:00
|
|
|
try {
|
|
|
|
$this->api()->wipeInactiveStages($this->db());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->deploymentFailed($checksum, $e->getMessage());
|
|
|
|
}
|
2016-03-20 18:50:18 +01:00
|
|
|
|
2015-09-30 08:40:09 +02:00
|
|
|
if ($this->api()->dumpConfig($config, $this->db())) {
|
2016-12-14 14:56:22 +01:00
|
|
|
$this->deploymentSucceeded($checksum);
|
2015-09-30 08:40:09 +02:00
|
|
|
} else {
|
2016-12-14 14:56:22 +01:00
|
|
|
$this->deploymentFailed($checksum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deploymentSucceeded($checksum)
|
|
|
|
{
|
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
2017-07-06 21:32:06 +02:00
|
|
|
$this->sendJson($this->getResponse(), (object) array('checksum' => $checksum));
|
|
|
|
return;
|
2016-12-14 14:56:22 +01:00
|
|
|
} else {
|
|
|
|
$url = Url::fromPath('director/config/deployments');
|
|
|
|
Notification::success(
|
|
|
|
$this->translate('Config has been submitted, validation is going on')
|
|
|
|
);
|
|
|
|
$this->redirectNow($url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function deploymentFailed($checksum, $error = null)
|
|
|
|
{
|
|
|
|
$extra = $error ? ': ' . $error: '';
|
|
|
|
|
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
2017-07-06 21:32:06 +02:00
|
|
|
$this->sendJsonError($this->getResponse(), 'Config deployment failed' . $extra);
|
|
|
|
return;
|
2016-12-14 14:56:22 +01:00
|
|
|
} else {
|
|
|
|
$url = Url::fromPath('director/config/files', array('checksum' => $checksum));
|
|
|
|
Notification::error(
|
|
|
|
$this->translate('Config deployment failed') . $extra
|
|
|
|
);
|
|
|
|
$this->redirectNow($url);
|
2015-09-30 08:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
public function activitiesAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/audit');
|
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
$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);
|
2016-12-28 12:39:13 +01:00
|
|
|
$this->view->addLink = $this->view->qlink(
|
|
|
|
$this->translate('My changes'),
|
|
|
|
$this->getRequest()->getUrl()
|
|
|
|
->with('author', $this->Auth()->getUser()->getUsername())
|
|
|
|
->without('page'),
|
|
|
|
null,
|
|
|
|
array('class' => 'icon-user', 'data-base-target' => '_self')
|
|
|
|
);
|
2016-11-03 16:06:18 +01:00
|
|
|
if ($this->hasPermission('director/deploy')) {
|
2016-12-28 12:39:13 +01:00
|
|
|
$this->view->addLink .= $this
|
2016-11-03 16:06:18 +01:00
|
|
|
->loadForm('DeployConfig')
|
|
|
|
->setDb($this->db())
|
|
|
|
->setApi($this->api())
|
|
|
|
->handleRequest();
|
|
|
|
}
|
2016-11-07 22:04:51 +01:00
|
|
|
$this->provideFilterEditorForTable($this->view->table);
|
2016-10-30 11:52:14 +01:00
|
|
|
|
|
|
|
$this->setViewScript('list/table');
|
2016-02-28 16:52:37 +01:00
|
|
|
}
|
|
|
|
|
2016-09-05 21:16:35 +02:00
|
|
|
public function settingsAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/admin');
|
|
|
|
|
2017-07-13 15:23:28 +02:00
|
|
|
$this->addSingleTab($this->translate('Settings'))
|
|
|
|
->addTitle($this->translate('Global Director Settings'));
|
|
|
|
$this->content()->add(
|
|
|
|
SettingsForm::load()
|
2016-09-05 21:16:35 +02:00
|
|
|
->setSettings(new Settings($this->db()))
|
2017-07-13 15:23:28 +02:00
|
|
|
->handleRequest()
|
|
|
|
);
|
2016-09-05 21:16:35 +02:00
|
|
|
}
|
|
|
|
|
2015-12-15 19:02:58 +01:00
|
|
|
// Show all files for a given config
|
|
|
|
public function filesAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/showconfig');
|
|
|
|
|
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();
|
|
|
|
|
2016-10-30 11:52:14 +01:00
|
|
|
if ($deploymentId = $this->view->deploymentId = $this->params->get('deployment_id')) {
|
2015-12-15 19:02:58 +01:00
|
|
|
$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');
|
|
|
|
|
2016-10-30 11:52:14 +01:00
|
|
|
$this->view->deployForm = $this->loadForm('DeployConfig')
|
|
|
|
->setAttrib('class', 'inline')
|
|
|
|
->setDb($this->db())
|
|
|
|
->setApi($this->api())
|
|
|
|
->setChecksum($checksum)
|
|
|
|
->setDeploymentId($deploymentId)
|
|
|
|
->handleRequest();
|
|
|
|
|
2015-12-15 19:02:58 +01:00
|
|
|
$this->view->table = $this
|
|
|
|
->loadTable('GeneratedConfigFile')
|
2016-11-09 20:37:33 +01:00
|
|
|
->setActiveFilename($this->params->get('active_file'))
|
2015-12-15 19:02:58 +01:00
|
|
|
->setConnection($this->db())
|
|
|
|
->setConfigChecksum($checksum);
|
|
|
|
|
2016-03-23 01:31:33 +01:00
|
|
|
if ($deploymentId) {
|
|
|
|
$this->view->table->setDeploymentId($deploymentId);
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/showconfig');
|
2016-11-09 20:37:33 +01:00
|
|
|
$filename = $this->view->filename = $this->params->get('file_path');
|
2016-07-28 09:11:09 +02:00
|
|
|
$fileOnly = $this->params->get('fileOnly');
|
|
|
|
$this->view->highlight = $this->params->get('highlight');
|
|
|
|
$this->view->highlightSeverity = $this->params->get('highlightSeverity');
|
2016-03-23 01:31:33 +01:00
|
|
|
$tabs = $this->configTabs()->add('file', array(
|
|
|
|
'label' => $this->translate('Rendered file'),
|
|
|
|
'url' => $this->getRequest()->getUrl(),
|
|
|
|
))->activate('file');
|
|
|
|
|
2016-11-08 22:16:56 +01:00
|
|
|
$params = $this->getConfigTabParams();
|
|
|
|
if ('deployment' === $this->params->get('backTo')) {
|
|
|
|
$this->view->addLink = $this->view->qlink(
|
|
|
|
$this->translate('back'),
|
|
|
|
'director/deployment',
|
|
|
|
array('id' => $params['deployment_id']),
|
|
|
|
array('class' => 'icon-left-big')
|
|
|
|
);
|
2016-07-28 09:11:09 +02:00
|
|
|
} else {
|
2016-11-09 20:37:33 +01:00
|
|
|
$params['active_file'] = $filename;
|
2016-07-28 09:11:09 +02:00
|
|
|
$this->view->addLink = $this->view->qlink(
|
|
|
|
$this->translate('back'),
|
|
|
|
'director/config/files',
|
2016-11-08 22:16:56 +01:00
|
|
|
$params,
|
2016-07-28 09:11:09 +02:00
|
|
|
array('class' => 'icon-left-big')
|
|
|
|
);
|
|
|
|
}
|
2016-03-23 01:31:33 +01:00
|
|
|
|
2015-12-17 10:54:38 +01:00
|
|
|
$this->view->config = IcingaConfig::load(Util::hex2binary($this->params->get('config_checksum')), $this->db());
|
|
|
|
$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()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/showconfig');
|
|
|
|
|
2016-03-23 01:31:33 +01:00
|
|
|
$this->configTabs()->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(
|
2016-10-30 11:52:14 +01:00
|
|
|
'director/config/files',
|
2016-02-26 12:42:21 +01:00
|
|
|
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
|
|
|
|
2016-05-02 10:42:53 +02:00
|
|
|
public function diffAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/showconfig');
|
|
|
|
|
2016-05-02 10:42:53 +02:00
|
|
|
$db = $this->db();
|
|
|
|
$this->view->title = $this->translate('Config diff');
|
|
|
|
|
|
|
|
$tabs = $this->getTabs()->add('diff', array(
|
|
|
|
'label' => $this->translate('Config diff'),
|
|
|
|
'url' => $this->getRequest()->getUrl()
|
|
|
|
))->activate('diff');
|
|
|
|
|
|
|
|
$leftSum = $this->view->leftSum = $this->params->get('left');
|
|
|
|
$rightSum = $this->view->rightSum = $this->params->get('right');
|
|
|
|
$left = IcingaConfig::load(Util::hex2binary($leftSum), $db);
|
|
|
|
|
2016-10-27 22:40:57 +02:00
|
|
|
$configs = $db->enumDeployedConfigs();
|
|
|
|
foreach (array($leftSum, $rightSum) as $sum) {
|
|
|
|
if (! array_key_exists($sum, $configs)) {
|
|
|
|
$configs[$sum] = substr($sum, 0, 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->configs = $configs;
|
2016-05-02 10:42:53 +02:00
|
|
|
if ($rightSum === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$right = IcingaConfig::load(Util::hex2binary($rightSum), $db);
|
|
|
|
$this->view->table = $this
|
|
|
|
->loadTable('ConfigFileDiff')
|
|
|
|
->setConnection($this->db())
|
|
|
|
->setLeftChecksum($leftSum)
|
|
|
|
->setRightChecksum($rightSum);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filediffAction()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->assertPermission('director/showconfig');
|
|
|
|
|
2016-05-02 10:42:53 +02:00
|
|
|
$db = $this->db();
|
|
|
|
$leftSum = $this->params->get('left');
|
|
|
|
$rightSum = $this->params->get('right');
|
|
|
|
$filename = $this->view->filename = $this->params->get('file_path');
|
|
|
|
|
|
|
|
$left = IcingaConfig::load(Util::hex2binary($leftSum), $db);
|
|
|
|
$right = IcingaConfig::load(Util::hex2binary($rightSum), $db);
|
|
|
|
|
|
|
|
$leftFile = $left->getFile($filename);
|
|
|
|
$rightFile = $right->getFile($filename);
|
|
|
|
|
|
|
|
$d = ConfigDiff::create($leftFile, $rightFile);
|
|
|
|
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Config file "%s"'),
|
|
|
|
$filename
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->view->output = $d->renderHtml();
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
protected function overviewTabs()
|
|
|
|
{
|
2016-11-03 16:06:18 +01:00
|
|
|
$this->view->tabs = $tabs = $this->getTabs();
|
|
|
|
|
|
|
|
if ($this->hasPermission('director/audit')) {
|
|
|
|
$tabs->add(
|
|
|
|
'activitylog',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Activity Log'),
|
|
|
|
'url' => 'director/config/activities'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasPermission('director/deploy')) {
|
|
|
|
$tabs->add(
|
|
|
|
'deploymentlog',
|
|
|
|
array(
|
|
|
|
'label' => $this->translate('Deployments'),
|
|
|
|
'url' => 'director/config/deployments'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-07-13 15:23:28 +02:00
|
|
|
|
2016-02-28 16:52:37 +01:00
|
|
|
return $this->view->tabs;
|
|
|
|
}
|
2016-03-23 01:31:33 +01:00
|
|
|
|
|
|
|
protected function configTabs()
|
|
|
|
{
|
|
|
|
$tabs = $this->getTabs();
|
|
|
|
|
2016-11-03 16:06:18 +01:00
|
|
|
if ($this->hasPermission('director/deploy') && $deploymentId = $this->params->get('deployment_id')) {
|
2016-03-23 01:31:33 +01:00
|
|
|
$tabs->add('deployment', array(
|
|
|
|
'label' => $this->translate('Deployment'),
|
2016-11-09 20:37:33 +01:00
|
|
|
'url' => 'director/deployment',
|
2016-03-23 01:31:33 +01:00
|
|
|
'urlParams' => array(
|
|
|
|
'id' => $deploymentId
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-11-03 16:06:18 +01:00
|
|
|
if ($this->hasPermission('director/showconfig')) {
|
|
|
|
$tabs->add('config', array(
|
|
|
|
'label' => $this->translate('Config'),
|
|
|
|
'url' => 'director/config/files',
|
|
|
|
'urlParams' => $this->getConfigTabParams()
|
|
|
|
));
|
|
|
|
}
|
2016-03-23 01:31:33 +01:00
|
|
|
|
|
|
|
return $tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getConfigTabParams()
|
|
|
|
{
|
|
|
|
$params = array(
|
|
|
|
'checksum' => $this->params->get(
|
|
|
|
'config_checksum',
|
|
|
|
$this->params->get('checksum')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($deploymentId = $this->params->get('deployment_id')) {
|
|
|
|
$params['deployment_id'] = $deploymentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
2015-06-17 13:31:51 +02:00
|
|
|
}
|