- Show only the "vsisible" parameters in the config file, or the ones already present in case of upgrade. Hide others for readability.

- Properly check DB connection in case of upgrade (setup wizard)
- Cleanup old remains of the V1.x setup program.

SVN:trunk[2537]
This commit is contained in:
Denis Flaven
2012-12-06 10:53:42 +00:00
parent 074551c506
commit 156993a517
7 changed files with 1 additions and 3049 deletions

View File

@@ -90,23 +90,6 @@ function FatalErrorCatcher($sOutput)
return $sOutput;
}
/**
* Helper function to create and administrator account for iTop
* @return boolean true on success, false otherwise
*/
function CreateAdminAccount(Config $oConfig, $sAdminUser, $sAdminPwd, $sLanguage)
{
SetupPage::log_info('CreateAdminAccount');
if (UserRights::CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage))
{
return true;
}
else
{
return false;
}
}
//Define some bogus, invalid HTML tags that no sane
//person would ever put in an actual document and tell
//PHP to delimit fatal error warnings with them.
@@ -169,270 +152,6 @@ try
$oPage->output();
break;
//////////////////////////////
//
case 'compile_data_model':
//
SetupPage::log_info("Compiling data model.");
require_once(APPROOT.'setup/modulediscovery.class.inc.php');
require_once(APPROOT.'setup/modelfactory.class.inc.php');
require_once(APPROOT.'setup/compiler.class.inc.php');
$sSelectedModules = Utils::ReadParam('selected_modules', '', false, 'raw_data');
$aSelectedModules = explode(',', $sSelectedModules);
$sWorkspace = Utils::ReadParam('workspace_dir', '', false, 'raw_data');
$sSourceDir = Utils::ReadParam('source_dir', '', false, 'raw_data');
$sTargetDir = Utils::ReadParam('target_dir', '', false, 'raw_data');
if (empty($sSourceDir) || empty($sTargetDir))
{
throw new Exception("missing parameter source_dir and/or target_dir");
}
$sSourcePath = APPROOT.$sSourceDir;
$sTargetPath = APPROOT.$sTargetDir;
if (!is_dir($sSourcePath))
{
throw new Exception("Failed to find the source directory '$sSourcePath', please check the rights of the web server");
}
if (!is_dir($sTargetPath) && !mkdir($sTargetPath))
{
throw new Exception("Failed to create directory '$sTargetPath', please check the rights of the web server");
}
// owner:rwx user/group:rx
chmod($sTargetPath, 0755);
$oFactory = new ModelFactory($sSourcePath);
$aModules = $oFactory->FindModules();
foreach($aModules as $foo => $oModule)
{
$sModule = $oModule->GetName();
if (in_array($sModule, $aSelectedModules))
{
$oFactory->LoadModule($oModule);
}
}
if (strlen($sWorkspace) > 0)
{
$oWorkspace = new MFWorkspace(APPROOT.$sWorkspace);
if (file_exists($oWorkspace->GetWorkspacePath()))
{
$oFactory->LoadModule($oWorkspace);
}
}
//$oFactory->Dump();
if ($oFactory->HasLoadErrors())
{
foreach($oFactory->GetLoadErrors() as $sModuleId => $aErrors)
{
SetupPage::log_error("Data model source file (xml) could not be loaded - found errors in module: $sModuleId");
foreach($aErrors as $oXmlError)
{
SetupPage::log_error("Load error: File: ".$oXmlError->file." Line:".$oXmlError->line." Message:".$oXmlError->message);
}
}
throw new Exception("The data model could not be compiled. Please check the setup error log");
}
else
{
$oMFCompiler = new MFCompiler($oFactory, $sSourcePath);
$oMFCompiler->Compile($sTargetPath);
SetupPage::log_info("Data model successfully compiled to '$sTargetPath'.");
}
break;
//////////////////////////////
//
case 'update_db_schema':
//
SetupPage::log_info("Update Database Schema.");
$oConfig = new Config();
$aParamValues = array(
'db_server' => utils::ReadParam('db_server', '', false, 'raw_data'),
'db_user' => utils::ReadParam('db_user', '', false, 'raw_data'),
'db_pwd' => utils::ReadParam('db_pwd', '', false, 'raw_data'),
'db_name' => utils::ReadParam('db_name', '', false, 'raw_data'),
'new_db_name' => utils::ReadParam('new_db_name', '', false, 'raw_data'),
'db_prefix' => utils::ReadParam('db_prefix', '', false, 'raw_data')
);
$sModuleDir = Utils::ReadParam('modules_dir', '');
$oConfig->UpdateFromParams($aParamValues, $sModuleDir);
$oProductionEnv = new RunTimeEnvironment();
$oProductionEnv->InitDataModel($oConfig, true); // load data model only
$sMode = Utils::ReadParam('mode', 'install');
if(!$oProductionEnv->CreateDatabaseStructure(MetaModel::GetConfig(), $sMode))
{
throw(new Exception("Failed to create/upgrade the database structure"));
}
SetupPage::log_info("Database Schema Successfully Updated.");
break;
//////////////////////////////
//
case 'after_db_create':
//
SetupPage::log_info('After Database Creation');
$oConfig = new Config();
$aParamValues = array(
'db_server' => utils::ReadParam('db_server', '', false, 'raw_data'),
'db_user' => utils::ReadParam('db_user', '', false, 'raw_data'),
'db_pwd' => utils::ReadParam('db_pwd', '', false, 'raw_data'),
'db_name' => utils::ReadParam('db_name', '', false, 'raw_data'),
'new_db_name' => utils::ReadParam('new_db_name', '', false, 'raw_data'),
'db_prefix' => utils::ReadParam('db_prefix', '', false, 'raw_data')
);
$sModuleDir = Utils::ReadParam('modules_dir', '');
$oConfig->UpdateFromParams($aParamValues, $sModuleDir);
$oProductionEnv = new RunTimeEnvironment();
$oProductionEnv->InitDataModel($oConfig, false); // load data model and connect to the database
$sMode = Utils::ReadParam('mode', 'install');
$sSelectedModules = Utils::ReadParam('selected_modules', '', false, 'raw_data');
$aSelectedModules = explode(',', $sSelectedModules);
// Perform here additional DB setup... profiles, etc...
//
$aAvailableModules = $oProductionEnv->AnalyzeInstallation(MetaModel::GetConfig(), $sModuleDir);
foreach($aAvailableModules as $sModuleId => $aModule)
{
if (($sModuleId != ROOT_MODULE) && in_array($sModuleId, $aSelectedModules) &&
isset($aAvailableModules[$sModuleId]['installer']) )
{
$sModuleInstallerClass = $aAvailableModules[$sModuleId]['installer'];
SetupPage::log_info("Calling Module Handler: $sModuleInstallerClass::AfterDatabaseCreation(oConfig, {$aModule['version_db']}, {$aModule['version_code']})");
// The validity of the sModuleInstallerClass has been established in BuildConfig()
$aCallSpec = array($sModuleInstallerClass, 'AfterDatabaseCreation');
call_user_func_array($aCallSpec, array(MetaModel::GetConfig(), $aModule['version_db'], $aModule['version_code']));
}
}
// Constant classes (e.g. User profiles)
//
foreach (MetaModel::GetClasses() as $sClass)
{
$aPredefinedObjects = call_user_func(array($sClass, 'GetPredefinedObjects'));
if ($aPredefinedObjects != null)
{
// Create/Delete/Update objects of this class,
// according to the given constant values
//
$aDBIds = array();
$oAll = new DBObjectSet(new DBObjectSearch($sClass));
while ($oObj = $oAll->Fetch())
{
if (array_key_exists($oObj->GetKey(), $aPredefinedObjects))
{
$aObjValues = $aPredefinedObjects[$oObj->GetKey()];
foreach ($aObjValues as $sAttCode => $value)
{
$oObj->Set($sAttCode, $value);
}
$oObj->DBUpdate();
$aDBIds[$oObj->GetKey()] = true;
}
else
{
$oObj->DBDelete();
}
}
foreach ($aPredefinedObjects as $iRefId => $aObjValues)
{
if (!array_key_exists($iRefId, $aDBIds))
{
$oNewObj = MetaModel::NewObject($sClass);
$oNewObj->SetKey($iRefId);
foreach ($aObjValues as $sAttCode => $value)
{
$oNewObj->Set($sAttCode, $value);
}
$oNewObj->DBInsert();
}
}
}
}
if (!$oProductionEnv->RecordInstallation($oConfig, $aSelectedModules, $sModuleDir))
{
throw(new Exception("Failed to record the installation information"));
}
if($sMode == 'install')
{
// Create the admin user only in case of installation
$sAdminUser = Utils::ReadParam('auth_user', '', false, 'raw_data');
$sAdminPwd = Utils::ReadParam('auth_pwd', '', false, 'raw_data');
$sLanguage = Utils::ReadParam('language', '');
if (!CreateAdminAccount(MetaModel::GetConfig(), $sAdminUser, $sAdminPwd, $sLanguage))
{
throw(new Exception("Failed to create the administrator account '$sAdminUser'"));
}
else
{
SetupPage::log_info("Administrator account '$sAdminUser' created.");
}
}
break;
//////////////////////////////
//
case 'load_data': // Load data files
//
$sFileName = Utils::ReadParam('file', '', false, 'raw_data');
$sSessionStatus = Utils::ReadParam('session_status', '');
$iPercent = (integer)Utils::ReadParam('percent', 0);
SetupPage::log_info("Loading file: $sFileName");
if (empty($sFileName) || !file_exists($sFileName))
{
throw(new Exception("File $sFileName does not exist"));
}
$oConfig = new Config();
$aParamValues = array(
'db_server' => utils::ReadParam('db_server', '', false, 'raw_data'),
'db_user' => utils::ReadParam('db_user', '', false, 'raw_data'),
'db_pwd' => utils::ReadParam('db_pwd', '', false, 'raw_data'),
'db_name' => utils::ReadParam('db_name', '', false, 'raw_data'),
'new_db_name' => utils::ReadParam('new_db_name', '', false, 'raw_data'),
'db_prefix' => utils::ReadParam('db_prefix', '', false, 'raw_data')
);
$sModuleDir = Utils::ReadParam('modules_dir', '');
$oConfig->UpdateFromParams($aParamValues, $sModuleDir);
$oProductionEnv = new RunTimeEnvironment();
$oProductionEnv->InitDataModel($oConfig, false); // load data model and connect to the database
$oDataLoader = new XMLDataLoader();
if ($sSessionStatus == 'start')
{
$oChange = MetaModel::NewObject("CMDBChange");
$oChange->Set("date", time());
$oChange->Set("userinfo", "Initialization");
$iChangeId = $oChange->DBInsert();
SetupPage::log_info("starting data load session");
$oDataLoader->StartSession($oChange);
}
$oDataLoader->LoadFile($sFileName);
$sResult = sprintf("loading of %s done. (Overall %d %% completed).", basename($sFileName), $iPercent);
SetupPage::log_info($sResult);
if ($sSessionStatus == 'end')
{
$oDataLoader->EndSession();
SetupPage::log_info("ending data load session");
}
break;
default:
throw(new Exception("Error unsupported operation '$sOperation'"));
}