From 5be800cfce8941fc11a56f43edd7d526688cc4b4 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 15 Jan 2020 15:48:54 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B02249=20-=20Supportability=20-=20Updater?= =?UTF-8?q?=20module=20(split=20ajax=20calls)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Controller/AjaxController.php | 38 ++++++++++- .../src/Service/CoreUpdater.php | 67 +++++++++++++++++-- .../view/UpdateCoreFiles.ready.js.twig | 4 +- setup/runtimeenv.class.inc.php | 8 +++ webservices/cron.php | 15 +++-- 5 files changed, 121 insertions(+), 11 deletions(-) diff --git a/datamodels/2.x/itop-core-update/src/Controller/AjaxController.php b/datamodels/2.x/itop-core-update/src/Controller/AjaxController.php index b840795c0..ae7fa2f15 100644 --- a/datamodels/2.x/itop-core-update/src/Controller/AjaxController.php +++ b/datamodels/2.x/itop-core-update/src/Controller/AjaxController.php @@ -155,6 +155,24 @@ class AjaxController extends Controller $this->DisplayJSONPage($aParams, $iResponseCode); } + public function OperationCheckCompile() + { + $aParams = array(); + try + { + CoreUpdater::CheckCompile(); + $iResponseCode = 200; + } + catch (Exception $e) + { + IssueLog::Error("Compile: ".$e->getMessage()); + $aParams['sError'] = $e->getMessage(); + $iResponseCode = 500; + } + + $this->DisplayJSONPage($aParams, $iResponseCode); + } + public function OperationCompile() { $aParams = array(); @@ -163,7 +181,25 @@ class AjaxController extends Controller CoreUpdater::Compile(); $iResponseCode = 200; } - catch (Exception $e) + catch (Exception $e) + { + IssueLog::Error("Compile: ".$e->getMessage()); + $aParams['sError'] = $e->getMessage(); + $iResponseCode = 500; + } + + $this->DisplayJSONPage($aParams, $iResponseCode); + } + + public function OperationUpdateDatabase() + { + $aParams = array(); + try + { + CoreUpdater::UpdateUpdateDatabase(); + $iResponseCode = 200; + } + catch (Exception $e) { IssueLog::Error("Compile: ".$e->getMessage()); $aParams['sError'] = $e->getMessage(); diff --git a/datamodels/2.x/itop-core-update/src/Service/CoreUpdater.php b/datamodels/2.x/itop-core-update/src/Service/CoreUpdater.php index 6195f05d5..31648367e 100644 --- a/datamodels/2.x/itop-core-update/src/Service/CoreUpdater.php +++ b/datamodels/2.x/itop-core-update/src/Service/CoreUpdater.php @@ -92,6 +92,39 @@ final class CoreUpdater } } + /** + * @throws \Exception + */ + public static function CheckCompile() + { + try + { + // Compile code + SetupLog::Info('itop-core-update: Start checking compilation'); + + $sFinalEnv = 'production'; + $oRuntimeEnv = new RunTimeEnvironmentCoreUpdater($sFinalEnv, false); + $oRuntimeEnv->CheckDirectories($sFinalEnv); + $oRuntimeEnv->CompileFrom('production'); + + $oRuntimeEnv->Rollback(); + + SetupLog::Info('itop-core-update: Checking compilation done'); + } + catch (Exception $e) + { + SetupLog::error($e->getMessage()); + try + { + SetupUtils::ExitReadOnlyMode(); + } catch (Exception $e1) + { + IssueLog::Error("ExitMaintenance: ".$e1->getMessage()); + } + throw $e; + } + } + /** * @throws \Exception */ @@ -103,9 +136,37 @@ final class CoreUpdater SetupLog::Info('itop-core-update: Start compilation'); $sFinalEnv = 'production'; - $oRuntimeEnv = new RunTimeEnvironmentCoreUpdater($sFinalEnv, false); + $oRuntimeEnv = new RunTimeEnvironmentCoreUpdater($sFinalEnv, true); $oRuntimeEnv->CheckDirectories($sFinalEnv); $oRuntimeEnv->CompileFrom('production'); + + SetupLog::Info('itop-core-update: Compilation done'); + } + catch (Exception $e) + { + SetupLog::error($e->getMessage()); + try + { + SetupUtils::ExitReadOnlyMode(); + } catch (Exception $e1) + { + IssueLog::Error("ExitMaintenance: ".$e1->getMessage()); + } + throw $e; + } + } + + /** + * @throws \Exception + */ + public static function UpdateDatabase() + { + try + { + SetupLog::Info('itop-core-update: Start Update database'); + + $sFinalEnv = 'production'; + $oRuntimeEnv = new RunTimeEnvironmentCoreUpdater($sFinalEnv, true); $oConfig = $oRuntimeEnv->MakeConfigFile($sFinalEnv.' (built on '.date('Y-m-d').')'); $oConfig->Set('access_mode', ACCESS_FULL); $oRuntimeEnv->WriteConfigFileSafe($oConfig); @@ -156,9 +217,7 @@ final class CoreUpdater $oRuntimeEnv->RecordInstallation($oConfig, $sDataModelVersion, $aSelectedModules, $aSelectedExtensionCodes, 'Done by the iTop Core Updater'); - $oRuntimeEnv->Commit(); - - SetupLog::Info('itop-core-update: Compilation done'); + SetupLog::Info('itop-core-update: Update database done'); } catch (Exception $e) { diff --git a/datamodels/2.x/itop-core-update/view/UpdateCoreFiles.ready.js.twig b/datamodels/2.x/itop-core-update/view/UpdateCoreFiles.ready.js.twig index 41e008d3c..42b278ce4 100644 --- a/datamodels/2.x/itop-core-update/view/UpdateCoreFiles.ready.js.twig +++ b/datamodels/2.x/itop-core-update/view/UpdateCoreFiles.ready.js.twig @@ -57,7 +57,7 @@ function GetAjaxRequest(sOperation) return oAjaxRequest; } -{% set aSteps = ['EnterMaintenance', 'Backup', 'FilesArchive', 'CopyFiles', 'Compile', 'ExitMaintenance', 'UpdateDone'] %} +{% set aSteps = ['EnterMaintenance', 'Backup', 'FilesArchive', 'CopyFiles', 'CheckCompile', 'Compile', 'UpdateDatabase', 'ExitMaintenance', 'UpdateDone'] %} aStepsName = []; @@ -75,7 +75,7 @@ var sFilesArchiveStep; sFilesArchiveStep = "FilesArchive"; {% endif %} -var aStepsAjaxOperation = ["EnterMaintenance", sBackupStep, sFilesArchiveStep, "CopyFiles", "Compile", "ExitMaintenance", null]; +var aStepsAjaxOperation = ["EnterMaintenance", sBackupStep, sFilesArchiveStep, "CopyFiles", "CheckCompile", "Compile", "UpdateDatabase", "ExitMaintenance", null]; var iNextStep = 0; function ExecNextStep() { diff --git a/setup/runtimeenv.class.inc.php b/setup/runtimeenv.class.inc.php index c6089b03a..b6018ec65 100644 --- a/setup/runtimeenv.class.inc.php +++ b/setup/runtimeenv.class.inc.php @@ -1065,6 +1065,14 @@ class RunTimeEnvironment } } } + + public function Rollback() + { + if ($this->sFinalEnv != $this->sTargetEnv) + { + SetupUtils::tidydir($this->sTargetEnv); + } + } /** * Call the given handler method for all selected modules having an installation handler diff --git a/webservices/cron.php b/webservices/cron.php index e7544878d..ca023f193 100644 --- a/webservices/cron.php +++ b/webservices/cron.php @@ -19,15 +19,22 @@ if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__)); require_once(__DIR__.'/../approot.inc.php'); + +const EXIT_CODE_ERROR = -1; +const EXIT_CODE_FATAL = -2; +// early exit +if (file_exists(READONLY_MODE_FILE)) +{ + echo "iTop is read-only. Exiting...\n"; + exit(EXIT_CODE_ERROR); +} + require_once(APPROOT.'/application/application.inc.php'); require_once(APPROOT.'/application/nicewebpage.class.inc.php'); require_once(APPROOT.'/application/webpage.class.inc.php'); require_once(APPROOT.'/application/clipage.class.inc.php'); require_once(APPROOT.'/core/background.inc.php'); -const EXIT_CODE_ERROR = -1; -const EXIT_CODE_FATAL = -2; - $sConfigFile = APPCONF.ITOP_DEFAULT_ENV.'/'.ITOP_CONFIG_FILE; if (!file_exists($sConfigFile)) { @@ -222,7 +229,7 @@ function CronExec($oP, $aProcesses, $bVerbose) if (file_exists(MAINTENANCE_MODE_FILE) || file_exists(READONLY_MODE_FILE)) { $oP->p("Maintenance detected, exiting"); - return; + exit(EXIT_CODE_ERROR); } $oTasks = new DBObjectSet($oSearch);