From 78df00f017f6baaf39ffc7a968f53dcb1ed8cff0 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Fri, 12 May 2017 09:37:06 +0200
Subject: [PATCH] CI Webgui: block impossible deployments (empty queue,
 inactive phase)

---
 config/lang/de.json                           |  5 +-
 config/lang/en.json                           |  1 +
 .../deployment/classes/project.class.php      | 83 +++++++++---------
 public_html/deployment/pages/act_deploy.php   | 84 ++++++++++---------
 4 files changed, 93 insertions(+), 80 deletions(-)

diff --git a/config/lang/de.json b/config/lang/de.json
index b0f51b14..8eb22a04 100644
--- a/config/lang/de.json
+++ b/config/lang/de.json
@@ -144,7 +144,7 @@
     "class-user-error-deny-no-role": "FEHLER: Sie haben nicht gen&uuml;gend Berechtigungen.",
     
     "page-accept-error-cannot-accept-phase": "Die Phase [%s] kann nicht akzeptiert werden.",
-    "page-accept-info": "Die Software wurde erfolgreich in der Phase <span class=\"%s\">%s</span> getestet und soll auf die n&auml;chstePhase <span class=\"%s\">%s</span> ausgerollt werden?",
+    "page-accept-info": "Die Software wurde erfolgreich in der Phase <span class=\"%s\">%s</span> getestet und soll auf die n&auml;chste Phase <span class=\"%s\">%s</span> ausgerollt werden?",
     "page-accept-warning-version-exists-in-next-queue": "In der Queue von Phase [%s] ist die Version von [%s] bereits vorhanden!",
     "page-accept-warning-version-exists-in-next-repo": "In der Phase [%s] ist die Version von [%s] bereits im Puppet-Repository vorhanden!",
     "page-accept-buttonlabel": "Die Phase [%s] akzeptieren und für Phase [%s] freigeben",
@@ -224,6 +224,7 @@
     "defaultbranch": "[default: Master oder Trunk]",
     "deploy": "Deploy",
     "deploy-hint": "Deploy der Queue von Phase [%s]",
+    "deploy-impossible": "Deploy der Queue von Phase [%s] ist nicht m&ouml;glich.",
     "deploy-settings": "Deployment - Einstellungen",
     "deploytimes": "Deployment Zeitpunkte",
     "deploytimes-immediately": "Ein Archiv in der Queue wird sofort ins Repo gestellt.",
@@ -273,7 +274,7 @@
     "replacement-fields-not-found": "Es wurden keine Platzhalter im Format <em>@replace[&quot;Name&quot;]</em> im Template gefunden.",
     "replacement-targetfile": "Ziel-Datei",
     "replacements": "Ersetzungen mit Template-Dateien",
-    "replacements-info": "Beim Build-Prozess gefundene Templates werden aufgelistet und darin erkannte Platzhalter erkannt. Die Felder sind ausschliesslich für die neue Infrastruktur relevant.",
+    "replacements-info": "Beim Build-Prozess gefundene Templates werden aufgelistet und darin vorhandene Platzhalter erkannt. Die Felder sind ausschliesslich für die neue Infrastruktur relevant.",
     "repositoryinfos": "Quell-Repository",
     "repository-access-browser": "Browserzugriff auf das Repo",
     "repository-auth": "Authentifizierung/ Dateiname zum SSH-Private-Key",
diff --git a/config/lang/en.json b/config/lang/en.json
index a2b3edcf..238db677 100644
--- a/config/lang/en.json
+++ b/config/lang/en.json
@@ -226,6 +226,7 @@
     "defaultbranch": "[default: master or trunk]",
     "deploy": "Deploy",
     "deploy-hint": "Deploy queue of phase [%s]",
+    "deploy-impossible": "Deploy queue of phase [%s] is not possible.",
     "deploy-settings": "Deployment - settings",
     "deploymethod": "Deployment method",
     "deploymethod-none": "None. Do not trigger any action.",
diff --git a/public_html/deployment/classes/project.class.php b/public_html/deployment/classes/project.class.php
index 27c71de7..5e4827db 100644
--- a/public_html/deployment/classes/project.class.php
+++ b/public_html/deployment/classes/project.class.php
@@ -775,56 +775,56 @@ class project extends base {
             $this->_aData["phases"] = array();
 
         if (!array_key_exists($sPhase, $this->_aData["phases"])) {
+            if ($this->isActivePhase($sPhase)) {
 
-            $this->_aData["phases"][$sPhase] = array();
-            $aTmp = array();
-
-            // a blocked package is waiting for deployment timeslot?
-            $sKey = "onhold";
-            $sJsonfile = $this->_getInfofile($sPhase, $sKey);
-            $aTmp[$sKey] = array();
-            if (file_exists($sJsonfile)) {
-                $aJson = json_decode(file_get_contents($sJsonfile), true);
-                if (array_key_exists("version", $aJson)) {
-                    $aTmp[$sKey] = $aJson;
-                    $aTmp[$sKey]["infofile"] = $sJsonfile;
-                    $aTmp[$sKey]["ok"] = 1;
-                } else {
-                    $aTmp[$sKey]["error"] = sprintf(t("class-project-error-metafile-has-no-version"), $sJsonfile, print_r($aJson, true));
-                }
-            } else {
-                $aTmp[$sKey]["info"] = t("class-project-info-no-package-in-queue");
-                $aTmp[$sKey]["ok"] = 1;
-            }
+                $this->_aData["phases"][$sPhase] = array();
+                $aTmp = array();
 
-            // package for puppet
-            $sKey = "ready2install";
-            $sJsonfile = $this->_getInfofile($sPhase, $sKey);
-            $aTmp[$sKey] = array();
-            if (file_exists($sJsonfile)) {
-                $sPkgfile = $this->_getPackagefile($sPhase, $sKey);
-                if (file_exists($sPkgfile)) {
+                // a blocked package is waiting for deployment timeslot?
+                $sKey = "onhold";
+                $sJsonfile = $this->_getInfofile($sPhase, $sKey);
+                $aTmp[$sKey] = array();
+                if (file_exists($sJsonfile)) {
                     $aJson = json_decode(file_get_contents($sJsonfile), true);
-                    if (is_array($aJson) && array_key_exists("version", $aJson)) {
+                    if (array_key_exists("version", $aJson)) {
                         $aTmp[$sKey] = $aJson;
                         $aTmp[$sKey]["infofile"] = $sJsonfile;
-                        $aTmp[$sKey]["packagefile"] = $sPkgfile;
                         $aTmp[$sKey]["ok"] = 1;
                     } else {
                         $aTmp[$sKey]["error"] = sprintf(t("class-project-error-metafile-has-no-version"), $sJsonfile, print_r($aJson, true));
                     }
                 } else {
-                    $aTmp[$sKey]["error"] = sprintf(t("class-project-error-getPhaseInfos-package-not-found"), $sPkgfile);
+                    $aTmp[$sKey]["info"] = t("class-project-info-no-package-in-queue");
+                    $aTmp[$sKey]["ok"] = 1;
+                }
+
+                // package for puppet
+                $sKey = "ready2install";
+                $sJsonfile = $this->_getInfofile($sPhase, $sKey);
+                $aTmp[$sKey] = array();
+                if (file_exists($sJsonfile)) {
+                    $sPkgfile = $this->_getPackagefile($sPhase, $sKey);
+                    if (file_exists($sPkgfile)) {
+                        $aJson = json_decode(file_get_contents($sJsonfile), true);
+                        if (is_array($aJson) && array_key_exists("version", $aJson)) {
+                            $aTmp[$sKey] = $aJson;
+                            $aTmp[$sKey]["infofile"] = $sJsonfile;
+                            $aTmp[$sKey]["packagefile"] = $sPkgfile;
+                            $aTmp[$sKey]["ok"] = 1;
+                        } else {
+                            $aTmp[$sKey]["error"] = sprintf(t("class-project-error-metafile-has-no-version"), $sJsonfile, print_r($aJson, true));
+                        }
+                    } else {
+                        $aTmp[$sKey]["error"] = sprintf(t("class-project-error-getPhaseInfos-package-not-found"), $sPkgfile);
+                    }
+                } else {
+                    $aTmp[$sKey]["error"] = sprintf(t("class-project-error-metafile-does-not-exist"), $sJsonfile);
                 }
-            } else {
-                $aTmp[$sKey]["error"] = sprintf(t("class-project-error-metafile-does-not-exist"), $sJsonfile);
-            }
 
-            // published data
-            $sKey = "deployed";
-            $sJsonfile = $this->_getInfofile($sPhase, $sKey);
-            $aTmp[$sKey] = array();
-            if ($this->isActivePhase($sPhase)) {
+                // published data
+                $sKey = "deployed";
+                $sJsonfile = $this->_getInfofile($sPhase, $sKey);
+                $aTmp[$sKey] = array();
 
                 // use version cache
                 require_once(__DIR__. '/../../valuestore/classes/valuestore.class.php');
@@ -863,7 +863,9 @@ class project extends base {
                  * 
                  */
             } else {
-                $aTmp[$sKey]["warning"] = t("class-project-warning-phase-not-active");
+                $aTmp['onhold']["warning"] = sprintf(t("class-project-warning-phase-not-active"), $sPhase);
+                $aTmp['ready2install']["warning"] = sprintf(t("class-project-warning-phase-not-active"), $sPhase);
+                $aTmp['deployed']["warning"] = sprintf(t("class-project-warning-phase-not-active"), $sPhase);
             }
 
             $this->_aData["phases"][$sPhase] = $aTmp;
@@ -2620,7 +2622,8 @@ class project extends base {
                 } else if (array_key_exists("warning", $aData)) {
                     $sReturn.= '<div class="warning"><i class="glyphicon glyphicon-info-sign"></i> ' . t('warning') . ':<br>' . $aData["warning"] . '</div>';
                 } else {
-                    $sReturn.= t('empty');
+                    return false;
+                    // $sReturn.= t('empty');
                 }
             } // if
         // } // for
diff --git a/public_html/deployment/pages/act_deploy.php b/public_html/deployment/pages/act_deploy.php
index 31911a4e..c4c1c80c 100644
--- a/public_html/deployment/pages/act_deploy.php
+++ b/public_html/deployment/pages/act_deploy.php
@@ -33,50 +33,58 @@ if (array_key_exists("confirm", $aParams)) {
         $sPhase = $aParams["par3"];
 
         $aPhaseData = $oPrj->getPhaseInfos($sPhase);
+        if (!array_key_exists('version', $aPhaseData['onhold'])){
+            $sOut.=$oPrj->getBox("error", 
+                    sprintf(t("deploy-impossible"), $sPhase).'<br>'
+                    . (array_key_exists('info', $aPhaseData['onhold']) ? $aPhaseData['onhold']['info'].'<br>' : '')
+                    . (array_key_exists('warning', $aPhaseData['onhold']) ? $aPhaseData['onhold']['warning'].'<br>' : '')
+                    . (array_key_exists('error', $aPhaseData['onhold']) ? $aPhaseData['onhold']['error'].'<br>' : '')
+                    );
+        } else {
+            $sOut.='
+                   <p>
+                        ' . t("onhold") . ':<br>
+                        ' . $oPrj->renderPhaseDetail($sPhase, "onhold", false) . '
+                   </p>
+            ';
 
-        $sOut.='
-               <p>
-                    ' . t("onhold") . ':<br>
-                    ' . $oPrj->renderPhaseDetail($sPhase, "onhold", false) . '
-               </p>
-        ';
-
-        // Eingabe Kommentare zum Deployment
-        $i = 0;
-        $aForms = array(
-            'deploy' => array(
-                'meta' => array(
-                    'method' => 'POST',
-                    'action' => '?',
-                ),
-                'validate' => array(),
-                'form' => array(
-                    'input' . $i++ => array(
-                        'type' => 'hidden',
-                        'name' => 'confirm',
-                        'value' => '1',
+            // Eingabe Kommentare zum Deployment
+            $i = 0;
+            $aForms = array(
+                'deploy' => array(
+                    'meta' => array(
+                        'method' => 'POST',
+                        'action' => '?',
                     ),
-                    'input' . $i++ => array(
-                        'type' => 'checkbox',
-                        'name' => 'aIgnore',
-                        'label' => '',
-                        'validate' => 'isastring',
-                        'options' => array(
-                            'bIgnoreDeploytimes' => array(
-                                'label' => t("page-deploy-info-ignore-deploytime"),
-                                'checked' => false,
+                    'validate' => array(),
+                    'form' => array(
+                        'input' . $i++ => array(
+                            'type' => 'hidden',
+                            'name' => 'confirm',
+                            'value' => '1',
+                        ),
+                        'input' . $i++ => array(
+                            'type' => 'checkbox',
+                            'name' => 'aIgnore',
+                            'label' => t("deploy-settings"),
+                            'validate' => 'isastring',
+                            'options' => array(
+                                'bIgnoreDeploytimes' => array(
+                                    'label' => t("page-deploy-info-ignore-deploytime"),
+                                    'checked' => false,
+                                ),
                             ),
                         ),
-                    ),
-                    'button' . $i++ => array(
-                        'type' => 'submit',
-                        'value' => t("deploy"),
+                        'button' . $i++ => array(
+                            'type' => 'submit',
+                            'value' => t("deploy"),
+                        ),
                     ),
                 ),
-            ),
-        );
-        $oForm = new formgen($aForms);
-        $sOut .= $oForm->renderHtml("deploy");
+            );
+            $oForm = new formgen($aForms);
+            $sOut .= $oForm->renderHtml("deploy");
+        }
     } else {
         $sOut.=$oPrj->getBox("error", t("error-no-phase"));
     }
-- 
GitLab