Compare commits

...

3 Commits

Author SHA1 Message Date
odain
b35ffb0fe9 add some error handling to have full stacktrace 2024-09-11 15:36:50 +02:00
odain
1e319f3943 add a test to reproduct the hub extension deploy issue 2024-09-11 15:36:24 +02:00
odain
cd41eda29d adapt hub code to use another env thant production 2024-09-11 15:35:17 +02:00
5 changed files with 165 additions and 31 deletions

View File

@@ -36,6 +36,7 @@ require_once(APPROOT.'core/dict.class.inc.php');
require_once(APPROOT.'setup/xmldataloader.class.inc.php');
require_once(__DIR__.'/hubruntimeenvironment.class.inc.php');
/**
* Overload of DBBackup to handle logging
*/
@@ -79,11 +80,11 @@ function DoBackup($sTargetFile)
// Make sure the target directory exists
$sBackupDir = dirname($sTargetFile);
SetupUtils::builddir($sBackupDir);
$oBackup = new DBBackupWithErrorReporting();
$oBackup->SetMySQLBinDir(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', ''));
$sSourceConfigFile = APPCONF.utils::GetCurrentEnvironment().'/'.ITOP_CONFIG_FILE;
$oMutex = new iTopMutex('backup.'.utils::GetCurrentEnvironment());
$oMutex->Lock();
try
@@ -101,7 +102,7 @@ function DoBackup($sTargetFile)
/**
* Outputs the status of the current ajax execution (as a JSON structure)
*
*
* @param string $sMessage
* @param bool $bSuccess
* @param number $iErrorCode
@@ -123,7 +124,7 @@ function ReportStatus($sMessage, $bSuccess, $iErrorCode = 0, $aMoreFields = arra
/**
* Helper to output the status of a successful execution
*
*
* @param string $sMessage
* @param array $aMoreFields
* Extra fields to pass to the caller, if needed
@@ -135,7 +136,7 @@ function ReportSuccess($sMessage, $aMoreFields = array())
/**
* Helper to output the status of a failed execution
*
*
* @param string $sMessage
* @param number $iErrorCode
* @param array $aMoreFields
@@ -156,21 +157,21 @@ try
SetupUtils::ExitMaintenanceMode(false); // Reset maintenance mode in case of problem
utils::PushArchiveMode(false);
ini_set('max_execution_time', max(3600, ini_get('max_execution_time'))); // Under Windows SQL/backup operations are part of the PHP timeout and require extra time
ini_set('display_errors', 1); // Make sure that fatal errors remain visible from the end-user
// Most of the ajax calls are done without the MetaModel being loaded
// Therefore, the language must be passed as an argument,
// and the dictionnaries be loaded here
$sLanguage = utils::ReadParam('language', '');
if ($sLanguage!='')
{
foreach (glob(APPROOT.'env-production/dictionaries/*.dict.php') as $sFilePath)
foreach (glob(APPROOT.'env-'. utils::GetCurrentEnvironment(). '/dictionaries/*.dict.php') as $sFilePath)
{
require_once ($sFilePath);
}
$aLanguages = Dict::GetLanguages();
if (array_key_exists($sLanguage, $aLanguages))
{
@@ -211,7 +212,7 @@ try
}
}
break;
case 'do_backup':
require_once (APPROOT.'/application/startup.inc.php');
require_once (APPROOT.'/application/loginwebpage.class.inc.php');
@@ -254,8 +255,9 @@ try
ReportError($e->getMessage(), $e->getCode());
}
break;
case 'compile':
SetupLog::Info('Deployment starts...');
$sAuthent = utils::ReadParam('authent', '', false, 'raw_data');
if (!file_exists(APPROOT.'data/hub/compile_authent') || $sAuthent !== file_get_contents(APPROOT.'data/hub/compile_authent'))
@@ -266,13 +268,13 @@ try
$aSelectedExtensionCodes = utils::ReadParam('extension_codes', array());
$aSelectedExtensionDirs = utils::ReadParam('extension_dirs', array());
$oRuntimeEnv = new HubRunTimeEnvironment('production', false); // use a temp environment: production-build
$oRuntimeEnv = new HubRunTimeEnvironment(utils::GetCurrentEnvironment(), false); // use a temp environment: production-build
$oRuntimeEnv->MoveSelectedExtensions(APPROOT.'/data/downloaded-extensions/', $aSelectedExtensionDirs);
$oConfig = new Config(APPCONF.'production/'.ITOP_CONFIG_FILE);
$oConfig = new Config(APPCONF.utils::GetCurrentEnvironment().'/'.ITOP_CONFIG_FILE);
if ($oConfig->Get('demo_mode')) throw new Exception('Sorry the installation of extensions is not allowed in demo mode');
$aSelectModules = $oRuntimeEnv->CompileFrom('production', false); // WARNING symlinks does not seem to be compatible with manual Commit
$aSelectModules = $oRuntimeEnv->CompileFrom(utils::GetCurrentEnvironment(), false); // WARNING symlinks does not seem to be compatible with manual Commit
$oRuntimeEnv->UpdateIncludes($oConfig);
@@ -291,11 +293,11 @@ try
SetupLog::Info('Compilation completed...');
ReportSuccess('Ok'); // No access to Dict::S here
break;
case 'move_to_production':
// Second step: update the schema and the data
// Everything happening below is based on env-production
$oRuntimeEnv = new RunTimeEnvironment('production', true);
$oRuntimeEnv = new RunTimeEnvironment(utils::GetCurrentEnvironment(), true);
try
{
@@ -307,7 +309,7 @@ try
}
unlink(APPROOT.'data/hub/compile_authent');
// Load the "production" config file to clone & update it
$oConfig = new Config(APPCONF.'production/'.ITOP_CONFIG_FILE);
$oConfig = new Config(APPCONF.utils::GetCurrentEnvironment().'/'.ITOP_CONFIG_FILE);
SetupUtils::EnterReadOnlyMode($oConfig);
$oRuntimeEnv->InitDataModel($oConfig, true /* model only */);
@@ -375,7 +377,7 @@ try
unlink(APPROOT.'data/hub/compile_authent');
}
// Note: at this point, the dictionnary is not necessarily loaded
SetupLog::Error(get_class($e).': '.Dict::S('iTopHub:ConfigurationSafelyReverted')."\n".$e->getMessage());
SetupLog::Error(get_class($e).': '.Dict::S('iTopHub:ConfigurationSafelyReverted'), null, [$e->getMessage(), $e->getTraceAsString()]);
SetupLog::Error('Debug trace: '.$e->getTraceAsString());
ReportError($e->getMessage(), $e->getCode());
}
@@ -384,17 +386,17 @@ try
SetupUtils::ExitReadOnlyMode();
}
break;
default:
ReportError("Invalid operation: '$sOperation'", -1);
}
}
catch (Exception $e)
{
SetupLog::Error(get_class($e).': '.Dict::S('iTopHub:ConfigurationSafelyReverted')."\n".$e->getMessage());
SetupLog::Error(get_class($e).': '.Dict::S('iTopHub:ConfigurationSafelyReverted'), null, [$e->getMessage(), $e->getTraceAsString()]);
SetupLog::Error('Debug trace: '.$e->getTraceAsString());
utils::PopArchiveMode();
ReportError($e->getMessage(), $e->getCode());
}

View File

@@ -1,6 +1,6 @@
<?php
class HubRunTimeEnvironment extends RunTimeEnvironment
{
{
/**
* Constructor
* @param string $sEnvironment
@@ -9,7 +9,7 @@ class HubRunTimeEnvironment extends RunTimeEnvironment
public function __construct($sEnvironment = 'production', $bAutoCommit = true)
{
parent::__construct($sEnvironment, $bAutoCommit);
if ($sEnvironment != $this->sTargetEnv)
{
if (is_dir(APPROOT.'/env-'.$this->sTargetEnv))
@@ -23,7 +23,7 @@ class HubRunTimeEnvironment extends RunTimeEnvironment
SetupUtils::copydir(APPROOT.'/data/'.$sEnvironment.'-modules', APPROOT.'/data/'.$this->sTargetEnv.'-modules');
}
}
/**
* Update the includes for the target environment
* @param Config $oConfig
@@ -32,7 +32,7 @@ class HubRunTimeEnvironment extends RunTimeEnvironment
{
$oConfig->UpdateIncludes('env-'.$this->sTargetEnv); // TargetEnv != FinalEnv
}
/**
* Move an extension (path to folder of this extension) to the target environment
* @param string $sExtensionDirectory The folder of the extension
@@ -45,7 +45,7 @@ class HubRunTimeEnvironment extends RunTimeEnvironment
if (!mkdir(APPROOT.'/data/'.$this->sTargetEnv.'-modules')) throw new Exception("ERROR: failed to create directory:'".(APPROOT.'/data/'.$this->sTargetEnv.'-modules')."'");
}
$sDestinationPath = APPROOT.'/data/'.$this->sTargetEnv.'-modules/';
// Make sure that the destination directory of the extension does not already exist
if (is_dir($sDestinationPath.basename($sExtensionDirectory)))
{
@@ -54,7 +54,7 @@ class HubRunTimeEnvironment extends RunTimeEnvironment
}
if (!rename($sExtensionDirectory, $sDestinationPath.basename($sExtensionDirectory))) throw new Exception("ERROR: failed move directory:'$sExtensionDirectory' to '".$sDestinationPath.basename($sExtensionDirectory)."'");
}
/**
* Move the selected extensions located in the given directory in data/<target-env>-modules
* @param string $sDownloadedExtensionsDir The directory to scan

View File

@@ -20,7 +20,7 @@ function DisplayStatus(WebPage $oPage)
if (is_dir($sPath)) {
$aExtraDirs[] = $sPath; // Also read the extra downloaded-modules directory
}
$oExtensionsMap = new iTopExtensionsMap('production', true, $aExtraDirs);
$oExtensionsMap = new iTopExtensionsMap(utils::GetCurrentEnvironment(), true, $aExtraDirs);
$oExtensionsMap->LoadChoicesFromDatabase(MetaModel::GetConfig());
foreach ($oExtensionsMap->GetAllExtensions() as $oExtension) {
@@ -156,7 +156,7 @@ function DoInstall(WebPage $oPage)
if (is_dir($sPath)) {
$aExtraDirs[] = $sPath; // Also read the extra downloaded-modules directory
}
$oExtensionsMap = new iTopExtensionsMap('production', true, $aExtraDirs);
$oExtensionsMap = new iTopExtensionsMap(utils::GetCurrentEnvironment(), true, $aExtraDirs);
$oExtensionsMap->LoadChoicesFromDatabase(MetaModel::GetConfig());
foreach ($oExtensionsMap->GetAllExtensions() as $oExtension) {

View File

@@ -27,7 +27,11 @@ require_once(APPROOT.'core/metamodel.class.php');
IssueLog::Trace('----- Request: '.utils::GetRequestUri(), LogChannels::WEB_REQUEST);
utils::InitTimeZone();
set_error_handler(function ($errno, $errstr) {
$e = new \Exception("");
\SetupLog::Error("Catching", null, [$e->getMessage(), $e->getTraceAsString()]);
echo $e->getMessage() . '<BR>' .$e->getTraceAsString();
}, E_WARNING);
/**
* @param string $sPagePath full path (if symlink, it will be resolved)

View File

@@ -0,0 +1,128 @@
<?php
namespace Combodo\iTop\Test\UnitTest\Hub;
use Combodo\iTop\Application\Helper\Session;
use Combodo\iTop\Application\UI\Base\iUIBlockFactory;
use Combodo\iTop\Service\InterfaceDiscovery\InterfaceDiscovery;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
class HubSetupTest extends ItopDataTestCase {
const USE_TRANSACTION = false;
public function setUp() : void {
parent::setUp();
$this->RequireOnceItopFile("application/utils.inc.php");
$this->RequireOnceItopFile("core/log.class.inc.php");
$this->RequireOnceItopFile("setup/runtimeenv.class.inc.php");
$this->RequireOnceItopFile("setup/backup.class.inc.php");
$this->RequireOnceItopFile("core/mutex.class.inc.php");
$this->RequireOnceItopFile("core/dict.class.inc.php");
$this->RequireOnceItopFile("setup/xmldataloader.class.inc.php");
$this->RequireOnceItopFile("datamodels/2.x/itop-hub-connector/hubruntimeenvironment.class.inc.php");
}
public function testSetupViaHub() {
$sEnvironment = 'test';
$_REQUEST['switch_env']=$sEnvironment;
Session::Set('itop_env', $sEnvironment);
require_once (APPROOT.'/application/startup.inc.php');
require_once (APPROOT.'/application/loginwebpage.class.inc.php');
/*$aSelectedExtensionCodes = ['molkobain-datacenter-view'];
$aSelectedExtensionDirs = $aSelectedExtensionCodes;
ini_set('display_errors', 1);
$oRuntimeEnv = new \HubRunTimeEnvironment($sEnvironment, false); // use a temp environment: production-build
$oRuntimeEnv->MoveSelectedExtensions(APPROOT.'/data/downloaded-extensions/', $aSelectedExtensionDirs);
$oConfig = new \Config(APPCONF."$sEnvironment/".ITOP_CONFIG_FILE);
$oRuntimeEnv->CompileFrom("production", false); // WARNING symlinks does not seem to be compatible with manual Commit
$oRuntimeEnv->UpdateIncludes($oConfig);*/
//$oRuntimeEnv->InitDataModel($oConfig, true /* model only */);
// Safety check: check the inter dependencies, will throw an exception in case of inconsistency
/*$oRuntimeEnv->AnalyzeInstallation($oConfig, $oRuntimeEnv->GetBuildDir(), true);
$oRuntimeEnv->CheckMetaModel(); // Will throw an exception if a problem is detected
// Everything seems Ok so far, commit in env-production!
$oRuntimeEnv->WriteConfigFileSafe($oConfig);
$oRuntimeEnv->Commit();*/
/*$sPath = APPROOT.'data/downloaded-extensions/';
$aExtraDirs = array();
if (is_dir($sPath)) {
$aExtraDirs[] = $sPath; // Also read the extra downloaded-modules directory
}
$oExtensionsMap = new \iTopExtensionsMap($sEnvironment, true, $aExtraDirs);*/
//InterfaceDiscovery::GetInstance()->FindItopClasses(iUIBlockFactory::class);
$sPassword = "abCDEF12345@";
/** @var User oUser */
$this->oUser = $this->CreateContactlessUser('login' . uniqid(),
ItopDataTestCase::$aURP_Profiles['Administrator'],
$sPassword
);
$sLogin = $this->oUser->Get('login');
$aPostFields = [
'auth_user' => $sLogin,
'auth_pwd' => $sPassword,
];
//$oConfig = new \Config();
$sConfigPath = APPCONF . "$sEnvironment/config-itop.php"; //$oConfig->GetLoadedFile();
@chmod($sConfigPath, 0770);
//$oConfig->WriteToFile($sConfigPath);
//@chmod($sConfigPath, 0440);
$sOutput = $this->CallItopUrl("/pages/exec.php?exec_module=itop-hub-connector&exec_page=ajax.php&switch_env=$sEnvironment&exec_env=$sEnvironment&maintenance=1",
[
'auth_user' => $sLogin,
'auth_pwd' => $sPassword,
'operation' => "compile",
'extension_codes[]' => "molkobain-datacenter-view",
'extension_dirs[]' => "molkobain-datacenter-view",
'authent' => '14b5da9d092f84044187421419a0347e7317bc8cd2b486fdda631be06b959269',
]);
/*$sOutput = $this->CallItopUrl("/pages/exec.php?exec_module=itop-hub-connector&exec_page=land.php&switch_env=$sEnvironment&exec_env=$sEnvironment&operation=install",
$aPostFields);*/
//var_dump($sOutput);
$aRes = json_decode($sOutput, true);
$this->assertNotNull($aRes, "output should be a json without any warning:" . PHP_EOL . $sOutput);
}
protected function CallItopUrl($sUri, ?array $aPostFields = null, $bXDebugEnabled = false)
{
$ch = curl_init();
if ($bXDebugEnabled) {
curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=phpstorm');
}
$sUrl = \MetaModel::GetConfig()->Get('app_root_url')."/$sUri";
var_dump($sUrl);
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPostFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$sOutput = curl_exec($ch);
//echo "$sUrl error code:".curl_error($ch);
curl_close($ch);
return $sOutput;
}
}