[icinga-checkins] icinga.org: icingaweb2-module-director/feature/test-runner-12905: DeployConfigForm: provide link-like form for...

git at icinga.org git at icinga.org
Wed Nov 2 19:31:04 CET 2016


Module: icingaweb2-module-director
Branch: feature/test-runner-12905
Commit: f41d45344863caa49770b5c4d4330cbc7095ad86
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=f41d45344863caa49770b5c4d4330cbc7095ad86

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sun Oct 30 10:52:14 2016 +0000

DeployConfigForm: provide link-like form for...

...deployments

fixes #11854

---

 application/controllers/ConfigController.php     |   27 +++--
 application/forms/DeployConfigForm.php           |  129 ++++++++++++++++++++++
 application/views/scripts/config/files.phtml     |    7 +-
 application/views/scripts/deployment/index.phtml |    2 +-
 library/Director/Web/Form/QuickForm.php          |    4 +
 5 files changed, 155 insertions(+), 14 deletions(-)

diff --git a/application/controllers/ConfigController.php b/application/controllers/ConfigController.php
index 19a2d00..2f3c017 100644
--- a/application/controllers/ConfigController.php
+++ b/application/controllers/ConfigController.php
@@ -19,9 +19,7 @@ class ConfigController extends ActionController
     {
         $this->setAutorefreshInterval(5);
         try {
-            if ($this->getRequest()->getUrl()->shift('checkforchanges')
-                || $this->db()->hasUncollectedDeployments()
-            ) {
+            if ($this->db()->hasUncollectedDeployments()) {
                 $this->api()->collectLogFiles($this->db());
             }
         } catch (Exception $e) {
@@ -67,7 +65,7 @@ class ConfigController extends ActionController
             if ($isApiRequest) {
                 return $this->sendJson((object) array('checksum' => $checksum));
             } else {
-                $url = Url::fromPath('director/config/deployments?checkforchanges');
+                $url = Url::fromPath('director/config/deployments');
                 Notification::success(
                     $this->translate('Config has been submitted, validation is going on')
                 );
@@ -94,7 +92,13 @@ class ConfigController extends ActionController
         $lastDeployedId = $this->db()->getLastDeploymentActivityLogId();
         $this->prepareTable('activityLog');
         $this->view->table->setLastDeployedId($lastDeployedId);
-        $this->render('list/table', null, true);
+        $this->view->form = $this
+            ->loadForm('DeployConfig')
+            ->setDb($this->db())
+            ->setApi($this->api())
+            ->handleRequest();
+
+        $this->setViewScript('list/table');
     }
 
     public function settingsAction()
@@ -112,10 +116,11 @@ class ConfigController extends ActionController
     // Show all files for a given config
     public function filesAction()
     {
+        $this->setAutorefreshInterval(10);
         $this->view->title = $this->translate('Generated config');
         $tabs = $this->getTabs();
 
-        if ($deploymentId = $this->params->get('deployment_id')) {
+        if ($deploymentId = $this->view->deploymentId = $this->params->get('deployment_id')) {
             $tabs->add('deployment', array(
                 'label'     => $this->translate('Deployment'),
                 'url'       => 'director/deployment',
@@ -132,6 +137,14 @@ class ConfigController extends ActionController
 
         $checksum = $this->params->get('checksum');
 
+        $this->view->deployForm = $this->loadForm('DeployConfig')
+            ->setAttrib('class', 'inline')
+            ->setDb($this->db())
+            ->setApi($this->api())
+            ->setChecksum($checksum)
+            ->setDeploymentId($deploymentId)
+            ->handleRequest();
+
         $this->view->table = $this
             ->loadTable('GeneratedConfigFile')
             ->setConnection($this->db())
@@ -193,7 +206,7 @@ class ConfigController extends ActionController
         $config = IcingaConfig::generate($this->db());
         $this->redirectNow(
             Url::fromPath(
-                'director/config/show',
+                'director/config/files',
                 array('checksum' => $config->getHexChecksum())
             )
         );
diff --git a/application/forms/DeployConfigForm.php b/application/forms/DeployConfigForm.php
new file mode 100644
index 0000000..26623da
--- /dev/null
+++ b/application/forms/DeployConfigForm.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace Icinga\Module\Director\Forms;
+
+use Icinga\Exception\IcingaException;
+use Icinga\Module\Director\Core\CoreApi;
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\IcingaConfig\IcingaConfig;
+// use Icinga\Module\Director\Objects\DirectorDeploymentLog;
+use Icinga\Module\Director\Util;
+use Icinga\Module\Director\Web\Form\QuickForm;
+
+class DeployConfigForm extends QuickForm
+{
+    /** @var CoreApi */
+    private $api;
+
+    /** @var Db */
+    private $db;
+
+    /** @var string */
+    private $checksum;
+
+    /** @var int */
+    private $deploymentId;
+
+    public function init()
+    {
+        $this->setAttrib('class', 'inline');
+    }
+
+    public function setup()
+    {
+        $activities = $this->db->countActivitiesSinceLastDeployedConfig();
+        if ($this->deploymentId) {
+            $label = $this->translate('Re-deploy now');
+        } elseif ($activities === 0) {
+            $label = $this->translate('There are no pending changes. Deploy anyways');
+        } else {
+            $label = sprintf(
+                $this->translate('Deploy %d pending changes', $activities),
+                $activities
+            );
+        }
+
+        if ($this->deploymentId) {
+            $deployIcon = 'reply-all';
+        } else {
+            $deployIcon = 'forward';
+        }
+
+        $this->addHtml(
+            $this->getView()->icon(
+                $deployIcon,
+                $label,
+                array('class' => 'link-color')
+            ) . '<nobr>'
+        );
+
+        $el = $this->createElement('submit', 'btn_deploy', array(
+            'label' => $label,
+            'escape' => false,
+            'decorators' => array('ViewHelper'),
+            'class' => 'link-button ' . $deployIcon,
+            ));
+
+        $this->addHtml('</nobr>');
+        $this->submitButtonName = $el->getName();
+        $this->setSubmitLabel($label);
+        $this->addElement($el);
+    }
+
+    public function onSuccess()
+    {
+        $db = $this->db;
+        $checksum = $this->checksum;
+        $msg = $this->translate('Config has been submitted, validation is going on');
+        $this->setSuccessMessage($msg);
+
+        $isApiRequest = $this->getRequest()->isApiRequest();
+        if ($this->checksum) {
+            $config = IcingaConfig::load(Util::hex2binary($this->checksum), $db);
+        } else {
+            $config = IcingaConfig::generate($db);
+            $checksum = $config->getHexChecksum();
+        }
+
+        $this->api->wipeInactiveStages($db);
+
+        if ($this->api->dumpConfig($config, $db)) {
+            if ($isApiRequest) {
+                die('Api not ready');
+                return $this->sendJson((object) array('checksum' => $checksum));
+            } else {
+                $this->setSuccessUrl('director/config/deployments');
+                $this->setSuccessMessage(
+                    $this->translate('Config has been submitted, validation is going on')
+                );
+            }
+            parent::onSuccess();
+        } else {
+            throw new IcingaException($this->translate('Config deployment failed'));
+        }
+    }
+
+    public function setChecksum($checksum)
+    {
+        $this->checksum = $checksum;
+        return $this;
+    }
+
+    public function setDeploymentId($id)
+    {
+        $this->deploymentId = $id;
+        return $this;
+    }
+
+    public function setApi(CoreApi $api)
+    {
+        $this->api = $api;
+        return $this;
+    }
+
+    public function setDb(Db $db)
+    {
+        $this->db = $db;
+        return $this;
+    }
+}
diff --git a/application/views/scripts/config/files.phtml b/application/views/scripts/config/files.phtml
index 7f0b22e..0e25612 100644
--- a/application/views/scripts/config/files.phtml
+++ b/application/views/scripts/config/files.phtml
@@ -14,12 +14,7 @@
 <table class="name-value-table">
  <tr>
   <th><?= $this->translate('Actions') ?></th>
-  <td data-base-target="_main"><?= $this->qlink(
-    $this->translate('Re-deploy now'),
-    'director/config/deploy',
-    array('checksum' => $this->config->getHexChecksum()),
-    array('class' => 'icon-reply-all')
-  ) ?><br /><?= $this->qlink(
+  <td data-base-target="_self"><?=  $this->deployForm ?><br /><?= $this->qlink(
     $this->translate('Last related activity'),
     'director/show/activitylog',
     array('checksum' => $this->config->getLastActivityHexChecksum()),
diff --git a/application/views/scripts/deployment/index.phtml b/application/views/scripts/deployment/index.phtml
index 7b814ff..ab5b1cf 100644
--- a/application/views/scripts/deployment/index.phtml
+++ b/application/views/scripts/deployment/index.phtml
@@ -49,7 +49,7 @@ function colorize($log, $logLink) {
 
     $log = preg_replace_callback(
         '~\>(critical|warning)(\<[^\n]+?\n?[^\n]+?in )'
-        . '(/.+?/api/packages/director/[^/]+/)([^:]+\.conf): (\d+)~s',
+        . '(/.+?/api/packages/director/[^/]+/)([^:]+\.conf): (\d+)~m',
         $logLink,
         $log
     );
diff --git a/library/Director/Web/Form/QuickForm.php b/library/Director/Web/Form/QuickForm.php
index 19b1079..dc4e2b8 100644
--- a/library/Director/Web/Form/QuickForm.php
+++ b/library/Director/Web/Form/QuickForm.php
@@ -100,6 +100,10 @@ abstract class QuickForm extends QuickBaseForm
             return;
         }
 
+        if ($this->submitButtonName && $el = $this->getElement($this->submitButtonName)) {
+            return;
+        }
+
         $el = $this->createElement('submit', $label)
             ->setLabel($label)
             ->setDecorators(array('ViewHelper'));



More information about the icinga-checkins mailing list