[icinga-checkins] icinga.org: icingaweb2-module-director/master: config/file: link to generated objects

git at icinga.org git at icinga.org
Thu Jul 28 10:28:46 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: c46f9d52e6828c564592c3f2cfeea5c13449735f
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=c46f9d52e6828c564592c3f2cfeea5c13449735f

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Jul 28 07:11:09 2016 +0000

config/file: link to generated objects

---

 application/controllers/ConfigController.php |   22 ++++++++---
 application/views/scripts/config/file.phtml  |   54 +++++++++++++++++++++++++-
 public/css/module.less                       |   27 +++++++++++++
 3 files changed, 95 insertions(+), 8 deletions(-)

diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php
index 9499f20..428eb58 100644
--- a/application/controllers/ConfigController.php
+++ b/application/controllers/ConfigController.php
@@ -135,17 +135,27 @@ class ConfigController extends ActionController
     // Show a single file
     public function fileAction()
     {
+        $fileOnly = $this->params->get('fileOnly');
+        $this->view->highlight = $this->params->get('highlight');
+        $this->view->highlightSeverity = $this->params->get('highlightSeverity');
         $tabs = $this->configTabs()->add('file', array(
             'label'     => $this->translate('Rendered file'),
             'url'       => $this->getRequest()->getUrl(),
         ))->activate('file');
 
-        $this->view->addLink = $this->view->qlink(
-            $this->translate('back'),
-            'director/config/files',
-            $this->getConfigTabParams(),
-            array('class' => 'icon-left-big')
-        );
+        if ($fileOnly) {
+            $tabs->remove('config');
+            if ($tabs->has('deployment')) {
+                $tabs->remove('deployment');
+            }
+        } else {
+            $this->view->addLink = $this->view->qlink(
+                $this->translate('back'),
+                'director/config/files',
+                $this->getConfigTabParams(),
+                array('class' => 'icon-left-big')
+            );
+        }
 
         $this->view->config = IcingaConfig::load(Util::hex2binary($this->params->get('config_checksum')), $this->db());
         $filename = $this->view->filename = $this->params->get('file_path');
diff --git a/application/views/scripts/config/file.phtml b/application/views/scripts/config/file.phtml
index 586ba87..8d21530 100644
--- a/application/views/scripts/config/file.phtml
+++ b/application/views/scripts/config/file.phtml
@@ -5,9 +5,59 @@
 <?= $this->addLink ?>
 </span>
 </div>
+<?php
+
+if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
+    $decode = function($str) { return htmlspecialchars_decode($str, ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5); };
+} else {
+    $decode = function($str) { return htmlspecialchars_decode($str, ENT_COMPAT | ENT_IGNORE); };
+}
+
+$view = $this;
+$linkObject = function ($match) use ($view, $decode) {
+
+    if ($match[2] === 'Service') {
+        return $match[0];
+    }
+
+    $name = $decode($match[3]);
+    return sprintf(
+        '%s %s "%s" {',
+        $match[1],
+        $match[2],
+        $view->qlink(
+            $name,
+            'director/' . $match[2],
+            array('name' => $name),
+            array('data-base-target' => '_next')
+        )
+    );
+};
+
+function linkObjects($config, $linkCallback) {
+    $pattern = '/^(object|template)\s([A-Z][A-Za-z]*?)\s"(.+?)"\s{/m';
+
+    return preg_replace_callback(
+        $pattern,
+        $linkCallback,
+        $config
+    );
+}
+
+function highlight($what, $line, $severity) {
+    $lines = explode("\n", $what);
+    $lines[$line - 1] = '<span class="highlight ' . $severity . '">' . $lines[$line - 1] . '</span>';
+    return implode("\n", $lines);
+}
+
+?>
 
 <div class="content">
-<pre>
-<?= $this->escape($file->getContent()) ?>
+<pre class="generated-config">
+<?php if ($this->highlight): ?>
+<?= highlight(linkObjects($this->escape($file->getContent()), $linkObject), $highlight, $highlightSeverity) ?>
+<?php else: ?>
+<?= linkObjects($this->escape($file->getContent()), $linkObject) ?>
+<?php endif ?>
 </pre>
 </div>
diff --git a/public/css/module.less b/public/css/module.less
index 2a5b6ff..056d42e 100644
--- a/public/css/module.less
+++ b/public/css/module.less
@@ -65,6 +65,10 @@ pre.logfile {
   overflow: auto;
   white-space: pre;
 
+  a {
+    color: #fff;
+  }
+
   .loglevel, .application {
     font-weight: bold;
   }
@@ -95,6 +99,29 @@ pre.logfile {
   }
 }
 
+pre.generated-config {
+
+  .highlight {
+    padding-left: 0.5em;
+
+    a {
+      color: inherit;
+    }
+
+    &.critical {
+      border-left: 0.5em solid @color-critical;
+    }
+
+    &.warning {
+      border-left: 0.5em solid @color-warning;
+    }
+
+    &.information {
+      border-left: 0.5em solid @color-ok;
+    }
+  }
+}
+
 table.avp th {
     font-size: inherit;
 }



More information about the icinga-checkins mailing list