Browse Source

Ajout de l'espace de mise à jour du site

stany.ferer 1 năm trước cách đây
mục cha
commit
76fc01f832

+ 1 - 0
conf.inc.php

@@ -17,6 +17,7 @@ define("DIR_DATAS", DOCUMENT_ROOT . "datas/");
 define("DIR_PRINT", DOCUMENT_ROOT . "core/print/");
 define("DIR_BACKUP", DOCUMENT_ROOT . "backups/");
 define("DIR_TEMP", DOCUMENT_ROOT . "tmp/");
+define("DIR_MAJ", DOCUMENT_ROOT . "maj/");
 
 define("SFTP_LOCAL", DOCUMENT_ROOT . "datas/sftp/");
 

+ 12 - 0
core/class/backup.class.php

@@ -76,6 +76,7 @@ class backup
                     $tempFile["file"] = $file;
                     $tempFile["size"] = core::formatFileSize(filesize(DIR_BACKUP . $file), 2);
                     $tempFile["date"] = core::dateFromTimestamp(filemtime(DIR_BACKUP . $file));
+                    $tempFile["timestamp"] = filemtime(DIR_BACKUP . $file);
                     $tempArray[] = $tempFile;
                 }
             }
@@ -83,6 +84,16 @@ class backup
         return $tempArray;
     }
 
+    public static function last()
+    {
+        $return = NULL;
+        foreach (self::scanFolder() as $key => $value) {
+            $return[$value["timestamp"]] = $value;
+        }
+        krsort($return);
+        return array_values($return)[0]["file"];
+    }
+
     public static function delete(string $_nameBackup)
     {
         if(file_exists("/" . DIR_BACKUP . "/" . $_nameBackup)){
@@ -94,4 +105,5 @@ class backup
             return FALSE;
         }
     }
+
 }

+ 54 - 16
core/class/git.class.php

@@ -2,38 +2,76 @@
 
 class git
 {
-    public static function getCommitHash(){
-        return substr(trim(exec('git log --pretty="%H" -n1 HEAD')), 0, 10);
+    public static function checkError(string $_string = NULL){
+        $return = NULL;
+        if($_string != NULL){
+            if(strpos($_string, '(use "git pull" to update your local branch)') === FALSE){
+                $return = FALSE;
+            } else {
+                return TRUE;
+            }
+            return $return;
+        } else {
+            return TRUE;
+        }
     }
 
-    public static function getCommitDate(){
-        $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD')));
+    public static function getCommitHash($_target = NULL){
+        if($_target == NULL){
+            $target = "HEAD";
+        } else {
+            $target = "origin/".$_target;
+        }
+        return substr(trim(exec('git log --pretty="%H" -n1 ' . $target)), 0, 10);
+    }
+
+    public static function getCommitDate($_target = NULL){
+        if($_target == NULL){
+            $target = "HEAD";
+        } else {
+            $target = "origin/".$_target;
+        }
+        $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci ' . $target)));
         $commitDate->setTimezone(new \DateTimeZone('Europe/Paris'));
         return $commitDate->format('d/m/Y à H:i:s');
     }
 
-    public static function getCommitAuthor(){
-        return exec('git log --pretty="%cn" -n1 HEAD');
+    public static function getCommitAuthor($_target = NULL){
+        if($_target == NULL){
+            $target = "HEAD";
+        } else {
+            $target = "origin/".$_target;
+        }
+        return exec('git log --pretty="%cn" -n1 ' . $target);
     }
 
-    public static function getCommitName(){
-        return exec('git log --pretty="%s" -n1 HEAD');
+    public static function getCommitName($_target = NULL){
+        if($_target == NULL){
+            $target = "HEAD";
+        } else {
+            $target = "origin/".$_target;
+        }
+        return exec('git log --pretty="%s" -n1 ' . $target);
     }
 
-    public static function getBranchName(){
-        return exec('git rev-parse --abbrev-ref HEAD');
+    public static function getBranchName($_target = NULL){
+        if($_target == NULL){
+            return exec('git rev-parse --abbrev-ref HEAD');
+        } else {
+            return $_target;
+        }
     }
 
-    public static function printVersion(){
+    public static function printVersion($_target = NULL){
         echo sprintf(
             '<h6>%s <span class="badge bg-info">%s</span> <span class="badge bg-success">%s</span></h6>
             <code>%s</code>
             <div class="media-body pb-3 mb-0 small lh-125">Le %s</div>', 
-            self::getCommitAuthor(),         
-            self::getBranchName(), 
-            self::getCommitHash(),
-            self::getCommitName(),
-            self::getCommitDate()
+            self::getCommitAuthor($_target),         
+            self::getBranchName($_target), 
+            self::getCommitHash($_target),
+            self::getCommitName($_target),
+            self::getCommitDate($_target)
         );
     }
 }

+ 154 - 0
core/class/maj.class.php

@@ -0,0 +1,154 @@
+<?php
+
+class maj {
+
+    public static function query(string $query = NULL){
+        if($query != NULL){
+            db::query($query);
+            try {
+                db::execute();
+                maj::print("SUCCESS : " . $query);
+                return TRUE;
+            } catch (Exception $ex) {
+                maj::print("ERREUR TECHNIQUE : " . $query);
+                return FALSE;
+            }
+        } else {
+            maj::print("ERREUR TECHNIQUE : Query not found");
+            return FALSE;
+        }
+    }
+
+    public static function compareHash(){
+        if(git::getCommitHash() == git::getCommitHash(MAJ_TARGET)){
+            return TRUE;
+        } else {
+            return FALSE;
+        }
+    }
+    
+    public static function print(string $string){
+        echo "<div>" . $string . "</string>";
+    }
+
+    public static function printSeparateur(){
+        self::print("**********************");
+    }
+
+    public static function start(){
+
+        self::printSeparateur();
+        self::print(">> Démarrage de la mise à jour de " . DOMAIN_CMS);
+
+        historique::recRef("/parametres.html");
+        historique::add(array(
+            "idType" => historique::getIdRef("ACTION"),
+            "idUser" => session::getId(),
+            "idPage" => historique::getIdRef("/parametres.html"),
+            "log" => "Lancement de la mise à jour [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "]"
+        ));
+
+        // Activation du mode maintenance
+        self::printSeparateur();
+        self::print(">> Mode maintenance activé");
+        core::addFileMaintenance();
+
+        // Création d'un backUp
+        self::printSeparateur();
+        self::print(">> Création du backup");
+        backup::create();
+        file::cleanFilesByOrder(DIR_BACKUP, BACKUP_LIMIT);
+
+        include_once(DIR_MAJ . "sql/maj.php");
+        self::printSeparateur();
+        if(goSql() == TRUE){
+            self::print("Maj SQL réalisée");
+            self::printSeparateur();
+            $gitReturn = self::bash();
+        } else {
+            self::print("ERROR : Maj SQL");
+        }
+
+        if($gitReturn == FALSE){
+            self::printSeparateur();
+            self::print("ERROR : Repo GIT [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "]");
+            self::printSeparateur();
+            self::print(">> ROLLBACK : Restauration des données");
+            backup::restore(DIR_BACKUP . backup::last());
+
+            alert::recError("Mise à jour en erreur de [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "]");
+
+            historique::recRef("/parametres.html");
+            historique::add(array(
+                "idType" => historique::getIdRef("ERROR"),
+                "idUser" => session::getId(),
+                "idPage" => historique::getIdRef("/parametres.html"),
+                "log" => "Erreur lors de la mise à jour [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "]"
+            ));
+        } else {
+            self::printSeparateur();
+            self::print("Mise à jour du repo GIT [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "] effectué");
+        }
+
+        self::printSeparateur();
+        self::print(">> Réinitialisation des JSON");
+        json::create("salaries");
+        json::create("excel");
+        json::create("excel-proweb");
+        json::create("events");
+        json::create("users");
+        json::create("salaries-proweb");
+        json::create("lotterys");
+
+        self::printSeparateur();
+        self::print(">> Mode maintenance désactivé");
+        core::removeFileMaintenance();
+
+        self::printSeparateur();
+        self::print(">> Fin de la mise à jour de " . DOMAIN_CMS);
+        self::printSeparateur();
+
+        if($gitReturn == TRUE){
+            historique::recRef("/parametres.html");
+            historique::add(array(
+                "idType" => historique::getIdRef("ACTION"),
+                "idUser" => session::getId(),
+                "idPage" => historique::getIdRef("/parametres.html"),
+                "log" => "Mise à jour [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "] terminée"
+            ));
+
+            self::print("MAJ EN SUCCES [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "]");
+            self::printSeparateur();
+        } else {
+            self::print("MAJ EN ERREUR [" . MAJ_TARGET . " - " . git::getCommitHash(MAJ_TARGET) . "]");
+            self::printSeparateur();
+        }
+    }
+
+    public static function bash(){
+        $output = NULL;
+        $retval = NULL;
+        $bash = DIR_MAJ . MAJ . ".sh";
+        $error = 0;
+
+        if(is_file($bash)){
+            try {
+                exec("bash " .$bash, $output, $retval);
+                foreach ($output as $key => $value) {
+                    self::print($value);
+                    if(git::checkError($value) == TRUE){
+                        $error = 1;
+                    }
+                }
+            } catch (\Throwable $th) {
+                self::print("ERROR : " . $bash);
+                return FALSE;
+            }
+            return ($error == 0)? TRUE : FALSE;
+        } else {
+            self::print("ERROR : " . $bash . " not found");
+            return FALSE;
+        }
+    }
+
+}

+ 24 - 28
core/views/pages/cms.parametres-debug.php

@@ -1,43 +1,39 @@
 <table class="table table-bordered">
 <tbody>
     <tr>
-        <th scope="row">
+        <td scope="row">
+                <h4>Refresh des données</h4>
                 <div class="element-parametres">
                     <a href="/submit.php?from=parametres-json-refresh" onclick="return confirm('Etes-vous certain de vouloir réinitialiser les JSON ?')"><button type="button" class="btn btn-warning">Réinitialisation des JSON</button></a>
                 </div>
+        </td>
+        <?php if (session::accessUserByType(1) and session::getId() == 1 and (ENVIRONNEMENT == "DEV" or ENVIRONNEMENT == "PREPROD")) { ?>
+        <td>
+            <h4>Outils de débug</h4>
+            <div class="element-parametres">
+                <a href="/submit.php?from=parametres-reset-datas" onclick="return confirm('Etes-vous certain de vouloir supprimer les données ?')"><button type="button" class="btn btn-danger">Nettoyer les données</button></a>
+            </div>
+            <div class="element-parametres">
+                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalPHPInfo">Voir le PHP Infos</button>
+            </div>
 
-                <?php if (session::accessUserByType(1) and session::getId() == 1 and (ENVIRONNEMENT == "DEV" or ENVIRONNEMENT == "PREPROD")) { ?>
-
-                <div class="element-parametres">
-                    <a href="/submit.php?from=parametres-reset-datas" onclick="return confirm('Etes-vous certain de vouloir supprimer les données ?')"><button type="button" class="btn btn-danger">Nettoyer les données</button></a>
-                </div>
-                <div class="element-parametres">
-                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalPHPInfo">Voir le PHP Infos</button>
-                </div>
-
-                <div class="modal fade" id="modalPHPInfo" tabindex="-1" role="dialog" aria-hidden="true">
-                    <div class="modal-dialog" role="document">
-                        <div class="modal-content">
-                            <div class="modal-header">
-                                <h5 class="modal-title" id="exampleModalLabel">PHP Info</h5>
-                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
-                                    <span aria-hidden="true">&times;</span>
-                                </button>
-                            </div>
-                            <div class="modal-body">
-                                <iframe width="970" height="800" src="/parametres-debug-info.vue"></iframe>
-                            </div>
+            <div class="modal fade" id="modalPHPInfo" tabindex="-1" role="dialog" aria-hidden="true">
+                <div class="modal-dialog" role="document">
+                    <div class="modal-content">
+                        <div class="modal-header">
+                            <h5 class="modal-title" id="exampleModalLabel">PHP Info</h5>
+                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                                <span aria-hidden="true">&times;</span>
+                            </button>
+                        </div>
+                        <div class="modal-body">
+                            <iframe width="970" height="800" src="/parametres-debug-info.vue"></iframe>
                         </div>
                     </div>
                 </div>
-
-                <?php } ?>
-        </th>
-        <td>
-            <div class="element-parametres">
-                <?php git::printVersion() ?>
             </div>
         </td>
+        <?php } ?>
     </tr>
     </tbody>
 </table>

+ 86 - 0
core/views/pages/cms.parametres-maj.php

@@ -0,0 +1,86 @@
+<?php 
+    $majAJour = maj::compareHash();
+?>
+
+<div class="row">
+    <div class="col-sm-6 mb-3 mb-sm-0">
+        <div class="card">
+            <div class="card-body">
+                <h5 class="card-title">Version actuelle du site</h5>
+            </div>  
+            <ul class="list-group list-group-flush">
+                <li class="list-group-item">
+                    <div class="element-parametres">
+                        <?php git::printVersion() ?>
+                    </div>
+                </li>
+                <li class="list-group-item">
+                    <?php
+                        if($majAJour == TRUE){
+                            echo '<button type="button" class="btn btn-success btn-sm" disabled>Le site est à jour</button>';
+                        } else {
+                            echo '<button type="button" class="btn btn-warning btn-sm" disabled>Le site n\'est pas à jour</button>';
+                        }
+                    ?>
+                </li>
+            </ul>
+        </div>
+    </div>
+<?php if($majAJour == FALSE): ?>
+    <div class="col-sm-6">
+        <div class="card">
+            <div class="card-body">
+                <h5 class="card-title">Mise à jour disponible</h5>
+            </div>  
+            <ul class="list-group list-group-flush">
+                <li class="list-group-item">
+                    <div class="element-parametres">
+                        <?php git::printVersion(MAJ_TARGET) ?>
+                    </div>
+                </li>
+                <li class="list-group-item">
+                    <button type="submit" id="startMaj" class="btn btn-outline-danger btn-sm">Mettre à jour le site</button>
+                    <button type="button" id="progressMaj" class="btn btn-primary btn-sm" style="display:none;" disabled>Mise à jour en cours</button>
+                    <button type="button" id="endMaj" class="btn btn-secondary btn-sm" style="display:none;" disabled>Mise à jour terminée</button>
+                </li>
+            </ul>
+        </div>
+    </div>
+<?php endif; ?>
+</div>
+
+<div id="alertMajSuccess" class="alert alert-success" role="alert" style="margin:20px 0; display:none;">
+    La mise à jour s'est réalisée avec succès.
+</div>
+<div id="alertMajError" class="alert alert-danger" role="alert" style="margin:20px 0; display:none;">
+    La mise à jour a échoué et toutes les données ont été réinitialisées.
+</div>
+
+<div id="consoleContenaire" class="card text-bg-light" style="margin:20px 0; display:none;">
+    <div class="card-body">
+        <code id="consoleMaj"></code>
+    </div>
+</div>
+
+<script>
+    $( "#startMaj" ).on( "click", function() {
+        $("#consoleContenaire").show();
+        $("#startMaj").hide();
+        $("#progressMaj").show();
+        $.get('/console.php', function(data) {
+            $("#consoleMaj").append( data );
+        });
+    });
+
+    $("body").on('DOMSubtreeModified', "#consoleMaj", function() {
+        var divContent = $('#consoleMaj').text();
+        var divContentEtat = divContent.indexOf("MAJ EN SUCCES");    
+        if(divContentEtat !== -1){
+            $("#alertMajSuccess").show();
+        } else{
+            $("#alertMajError").show();
+        }
+        $("#progressMaj").hide();
+        $("#endMaj").show();
+    }); 
+</script>

+ 3 - 3
core/views/pages/cms.parametres-restore.php

@@ -16,9 +16,9 @@
             data-url="/json.php?jsonData=parametres-restore">
             <thead>
                 <tr>
-                    <th data-formatter="downloadFile" data-sortable="true" data-field="file" data-filter-control="input" data-align="left">Nom</th>
-                    <th data-sortable="true" data-field="size" data-filter-control="input" data-width="100">Taille</th>
-                    <th data-sortable="true" data-field="date" data-filter-control="input" data-width="170">Date de création</th>
+                    <th data-formatter="downloadFile" data-field="file" data-filter-control="input" data-align="left">Nom</th>
+                    <th data-field="size" data-filter-control="input" data-width="100">Taille</th>
+                    <th data-field="date" data-filter-control="input" data-width="170">Date de création</th>
                     <th data-formatter="restoreBackup" data-width="110"></th>
                     <th data-formatter="deleteBackup" data-width="10"></th>
                 </tr>

+ 18 - 1
core/views/pages/cms.parametres.php

@@ -30,9 +30,12 @@ echo core::filAriane(array(
         <li class="nav-item">
             <a class="nav-link" data-toggle="tab" id="backup-tab" role="tab" aria-selected="">Backup & restauration</a>
         </li> 
+        <li class="nav-item">
+            <a class="nav-link" data-toggle="tab" id="maj-tab" role="tab" aria-selected="">Mise à jour</a>
+        </li>
         <li class="nav-item">
             <a class="nav-link" data-toggle="tab" id="debug-tab" role="tab" aria-selected="">Debug & développement</a>
-        </li> 
+        </li>
     </ul>
 <br />
 
@@ -49,6 +52,9 @@ echo core::filAriane(array(
     <div id="tab-debug" style="display: none;">
         <?php get::page("parametres-debug") ?>
     </div>
+    <div id="tab-maj" style="display: none;">
+        <?php get::page("parametres-maj") ?>
+    </div>
 </div>
 
 <script>
@@ -58,24 +64,35 @@ echo core::filAriane(array(
             $("#tab-users").hide();
             $("#tab-backup").hide();
             $("#tab-debug").hide();
+            $("#tab-maj").hide();
         }); 
         $("#users-tab").click(function() {
             $("#tab-general").hide();
             $("#tab-users").show();
             $("#tab-backup").hide();
             $("#tab-debug").hide();
+            $("#tab-maj").hide();
         });  
         $("#backup-tab").click(function() {
             $("#tab-general").hide();
             $("#tab-users").hide();
             $("#tab-backup").show();
             $("#tab-debug").hide();
+            $("#tab-maj").hide();
         });
         $("#debug-tab").click(function() {
             $("#tab-general").hide();
             $("#tab-users").hide();
             $("#tab-backup").hide();
             $("#tab-debug").show();
+            $("#tab-maj").hide();
+        });
+        $("#maj-tab").click(function() {
+            $("#tab-general").hide();
+            $("#tab-users").hide();
+            $("#tab-backup").hide();
+            $("#tab-debug").hide();
+            $("#tab-maj").show();
         });
     })
 </script>

+ 1 - 4
core/views/pages/cms.user.php

@@ -162,9 +162,6 @@ if(core::ifGet("add")) {
         <input type="hidden" name="actif" value="<?php echo $user["actif"] ?>">
     <?php endif; ?>
 
-    <input class="btn btn-primary btn-lg" style="width: 100%" type="submit" value="<?php echo $submit ?>">
-
-    <br />
-    <br />
+    <input class="btn btn-primary btn-lg" style="width: 100%; margin-bottom:20px;" type="submit" value="<?php echo $submit ?>">
     
 </form>

+ 12 - 0
maj/dev.sh

@@ -0,0 +1,12 @@
+FOLDER=dev.cms.cse-invent.com
+BRANCH_ORIGIN=dev
+BRANCH=dev
+
+cd /var/www
+sudo chown -R debian:debian ${FOLDER}/
+cd ${FOLDER}
+git checkout ${BRANCH}
+git pull origin ${BRANCH_ORIGIN}
+git push
+cd ..
+sudo chown -R www-data:www-data ${FOLDER}/

+ 12 - 0
maj/preprod.sh

@@ -0,0 +1,12 @@
+FOLDER=pp.cms.cse-invent.com
+BRANCH_ORIGIN=dev
+BRANCH=preprod
+
+cd /var/www
+sudo chown -R debian:debian ${FOLDER}/
+cd ${FOLDER}
+git checkout ${BRANCH}
+git pull origin ${BRANCH_ORIGIN}
+git push
+cd ..
+sudo chown -R www-data:www-data ${FOLDER}/

+ 12 - 0
maj/prod.sh

@@ -0,0 +1,12 @@
+FOLDER=cms.cse-invent.com
+BRANCH_ORIGIN=preprod
+BRANCH=master
+
+cd /var/www
+sudo chown -R debian:debian ${FOLDER}/
+cd ${FOLDER}
+git checkout ${BRANCH}
+git pull origin ${BRANCH_ORIGIN}
+git push
+cd ..
+sudo chown -R www-data:www-data ${FOLDER}/

+ 6 - 0
maj/sql/maj.php

@@ -0,0 +1,6 @@
+<?php 
+
+function goSql(){
+    //maj::query("...");
+    return TRUE;
+}

+ 11 - 0
public-cms/console.php

@@ -0,0 +1,11 @@
+<?php
+
+session_start();
+
+require_once "../env.inc.php";
+require_once "../access.inc.php";
+require_once "../conf.inc.php";
+require_once DIR_PHP_LAYOUTS . "header.php";
+require_once DIR_PHP_LAYOUTS . "cms.session.php";
+
+maj::start();