Massive refactor of module loading, configuration, startup, management.

This commit is contained in:
Teo Mrnjavac 2015-09-09 19:01:56 +02:00
parent c74c67805e
commit fb44fb97b6
12 changed files with 374 additions and 282 deletions

View file

@ -18,8 +18,12 @@
#include "ModuleManager.h"
#include "ExecutionViewStep.h"
#include "Module.h"
#include "utils/Logger.h"
#include "utils/YamlUtils.h"
#include "Settings.h"
#include "ViewManager.h"
#include <yaml-cpp/yaml.h>
@ -46,19 +50,14 @@ ModuleManager::instance()
ModuleManager::ModuleManager( const QStringList& paths, QObject* parent )
: QObject( parent )
, m_paths( paths )
, m_lastPhaseLoaded( Phase_NULL )
{
Q_ASSERT( !s_instance );
s_instance = this;
}
ModuleManager::~ModuleManager()
{
foreach ( Module* m, m_availableModules )
{
delete m;
}
}
{}
void
@ -68,70 +67,6 @@ ModuleManager::init()
}
QStringList
ModuleManager::availableModules()
{
return m_availableModules.keys();
}
Module*
ModuleManager::module( const QString& name )
{
return m_availableModules.value( name );
}
Phase
ModuleManager::currentPhase()
{
return m_lastPhaseLoaded;
}
void
ModuleManager::loadModules( Phase phase )
{
//FIXME: When we depend on Qt 5.4 this ugly hack should be replaced with
// QTimer::singleShot.
QTimer* timer = new QTimer();
timer->setSingleShot( true );
connect( timer, &QTimer::timeout,
this, [ this, timer, phase ]()
{
foreach ( const QString& moduleName, Settings::instance()->modules( phase ) )
{
if ( !m_availableModules.contains( moduleName ) ||
!m_availableModules.value( moduleName ) )
{
cDebug() << "Module" << moduleName << "not found in module search paths."
<< "\nCalamares will now quit.";
qApp->exit( 1 );
return;
}
Module* thisModule = m_availableModules.value( moduleName );
if ( thisModule && thisModule->isLoaded() )
{
cDebug() << "Module" << moduleName << "already loaded.";
continue;
}
doLoad( moduleName );
}
emit modulesLoaded( phase );
m_lastPhaseLoaded = phase;
// Loading sequence:
// 1) deps are already fine. check if we have all the modules needed by the roster
// 2) ask ModuleManager to load them from the list provided by Settings
// 3) MM must load them asyncly but one at a time, by calling Module::loadSelf
// 4) Module must have subclasses that reimplement loadSelf for various module types
timer->deleteLater();
});
timer->start( 0 );
}
void
ModuleManager::doInit()
{
@ -154,25 +89,47 @@ ModuleManager::doInit()
bool success = currentDir.cd( subdir );
if ( success )
{
QFileInfo metadataFileInfo( currentDir.absoluteFilePath( MODULE_CONFIG_FILENAME ) );
if ( ! ( metadataFileInfo.exists() && metadataFileInfo.isReadable() ) )
QFileInfo descriptorFileInfo( currentDir.absoluteFilePath( MODULE_CONFIG_FILENAME ) );
if ( ! ( descriptorFileInfo.exists() && descriptorFileInfo.isReadable() ) )
{
cDebug() << Q_FUNC_INFO << "unreadable file: "
<< metadataFileInfo.absoluteFilePath();
<< descriptorFileInfo.absoluteFilePath();
continue;
}
Module* moduleInfo = Module::fromDescriptorFile( metadataFileInfo.absoluteFilePath() );
QFile descriptorFile( descriptorFileInfo.absoluteFilePath() );
QVariant moduleDescriptor;
if ( descriptorFile.exists() && descriptorFile.open( QFile::ReadOnly | QFile::Text ) )
{
QByteArray ba = descriptorFile.readAll();
try
{
YAML::Node doc = YAML::Load( ba.constData() );
if ( moduleInfo &&
( moduleInfo->name() == currentDir.dirName() ) &&
( !m_availableModules.contains( moduleInfo->name() ) ) )
{
m_availableModules.insert( moduleInfo->name(), moduleInfo );
moduleDescriptor = CalamaresUtils::yamlToVariant( doc );
}
catch ( YAML::Exception& e )
{
cDebug() << "WARNING: YAML parser error " << e.what();
continue;
}
}
else
if ( moduleDescriptor.isValid() &&
!moduleDescriptor.isNull() &&
moduleDescriptor.type() == QVariant::Map )
{
delete moduleInfo;
QVariantMap moduleDescriptorMap = moduleDescriptor.toMap();
if ( moduleDescriptorMap.value( "name" ) == currentDir.dirName() &&
!m_availableDescriptorsByModuleName.contains( moduleDescriptorMap.value( "name" ).toString() ) )
{
m_availableDescriptorsByModuleName.insert( moduleDescriptorMap.value( "name" ).toString(),
moduleDescriptorMap );
m_moduleDirectoriesByModuleName.insert( moduleDescriptorMap.value( "name" ).toString(),
descriptorFileInfo.absoluteDir().absolutePath() );
}
}
}
else
@ -194,19 +151,169 @@ ModuleManager::doInit()
}
void
ModuleManager::doLoad( const QString& moduleName )
QStringList
ModuleManager::loadedInstanceKeys()
{
Module* thisModule = m_availableModules.value( moduleName );
if ( !thisModule )
{
cDebug() << "Module" << moduleName << "loading IMPOSSIBLE, module does not exist";
return;
}
return m_loadedModulesByInstanceKey.keys();
}
thisModule->loadSelf();
if ( !thisModule->isLoaded() )
cDebug() << "Module" << moduleName << "loading FAILED";
QVariantMap
ModuleManager::moduleDescriptor( const QString& name )
{
return m_availableDescriptorsByModuleName.value( name );
}
Module*
ModuleManager::moduleInstance( const QString& instanceKey )
{
return m_loadedModulesByInstanceKey.value( instanceKey );
}
void
ModuleManager::loadModules()
{
QTimer::singleShot( 0, this, [ this ]()
{
QList< QMap< QString, QString > > customInstances =
Settings::instance()->customModuleInstances();
for ( const QPair< ModuleAction, QStringList >& modulePhase :
Settings::instance()->modulesSequence() )
{
ModuleAction currentAction = modulePhase.first;
foreach ( const QString& moduleEntry,
modulePhase.second )
{
QStringList moduleEntrySplit = moduleEntry.split( '@' );
QString moduleName;
QString instanceId;
QString configFileName;
if ( moduleEntrySplit.length() < 1 ||
moduleEntrySplit.length() > 2 )
{
cDebug() << "Wrong module entry format for module" << moduleEntry << "."
<< "\nCalamares will now quit.";
qApp->exit( 1 );
return;
}
moduleName = moduleEntrySplit.first();
instanceId = moduleEntrySplit.last();
configFileName = QString( "%1.conf" ).arg( moduleName );
if ( !m_availableDescriptorsByModuleName.contains( moduleName ) ||
m_availableDescriptorsByModuleName.value( moduleName ).isEmpty() )
{
cDebug() << "Module" << moduleName << "not found in module search paths."
<< "\nCalamares will now quit.";
qApp->exit( 1 );
return;
}
auto findCustomInstance =
[ customInstances ]( const QString& moduleName,
const QString& instanceId ) -> int
{
for ( int i = 0; i < customInstances.count(); ++i )
{
auto thisInstance = customInstances[ i ];
if ( thisInstance.value( "module" ) == moduleName &&
thisInstance.value( "id" ) == instanceId )
return i;
}
return -1;
};
if ( moduleName != instanceId ) //means this is a custom instance
{
if ( findCustomInstance( moduleName, instanceId ) > -1 )
{
configFileName = customInstances[ findCustomInstance( moduleName, instanceId ) ].value( "config" );
}
else //ought to be a custom instance, but cannot find instance entry
{
cDebug() << "Custom instance" << moduleEntry << "not found in custom instances section."
<< "\nCalamares will now quit.";
qApp->exit( 1 );
return;
}
}
// So now we can assume that the module entry is at least valid,
// that we have a descriptor on hand (and therefore that the
// module exists), and that the instance is either default or
// defined in the custom instances section.
// We still don't know whether the config file for the entry
// exists and is valid, but that's the only thing that could fail
// from this point on. -- Teo 8/2015
QString instanceKey = QString( "%1@%2" )
.arg( moduleName )
.arg( instanceId );
Module* thisModule =
m_loadedModulesByInstanceKey.value( instanceKey, nullptr );
if ( thisModule && !thisModule->isLoaded() )
{
cDebug() << "Module" << instanceKey << "exists but not loaded."
<< "\nCalamares will now quit.";
qApp->exit( 1 );
return;
}
if ( thisModule && thisModule->isLoaded() )
{
cDebug() << "Module" << instanceKey << "already loaded.";
}
else
{
thisModule =
Module::fromDescriptor( m_availableDescriptorsByModuleName.value( moduleName ),
instanceId,
configFileName,
m_moduleDirectoriesByModuleName.value( moduleName ) );
if ( !thisModule )
{
cDebug() << "Module" << instanceKey << "cannot be created from descriptor.";
Q_ASSERT( thisModule );
continue;
}
// If it's a ViewModule, it also appends the ViewStep to the ViewManager.
thisModule->loadSelf();
m_loadedModulesByInstanceKey.insert( instanceKey, thisModule );
Q_ASSERT( thisModule->isLoaded() );
if ( !thisModule->isLoaded() )
{
cDebug() << "Module" << moduleName << "loading FAILED";
continue;
}
}
// At this point we most certainly have a pointer to a loaded module in
// thisModule. We now need to enqueue jobs info into an EVS.
if ( currentAction == Calamares::Exec )
{
ExecutionViewStep* evs =
qobject_cast< ExecutionViewStep* >(
Calamares::ViewManager::instance()->viewSteps().last() );
if ( !evs ) // If the last step is not an EVS, we must create it.
{
evs = new ExecutionViewStep( ViewManager::instance() );
ViewManager::instance()->addViewStep( evs );
}
else
{
cDebug() << "LAST VS IS EVS!";
}
evs->appendJobModuleInstanceKey( instanceKey );
}
}
}
emit modulesLoaded();
} );
}
@ -218,15 +325,16 @@ ModuleManager::checkDependencies()
bool somethingWasRemovedBecauseOfUnmetDependencies = false;
forever
{
for ( auto it = m_availableModules.begin();
it != m_availableModules.end(); ++it )
for ( auto it = m_availableDescriptorsByModuleName.begin();
it != m_availableDescriptorsByModuleName.end(); ++it )
{
foreach ( QString depName, (*it)->requiredModules() )
foreach ( const QString& depName,
(*it).value( "requiredModules" ).toStringList() )
{
if ( !m_availableModules.contains( depName ) )
if ( !m_availableDescriptorsByModuleName.contains( depName ) )
{
somethingWasRemovedBecauseOfUnmetDependencies = true;
m_availableModules.erase( it );
m_availableDescriptorsByModuleName.erase( it );
break;
}
}