2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-06-11 13:37:10 +02:00
|
|
|
*
|
2019-04-16 18:12:14 +02:00
|
|
|
* Copyright 2019, Dominic Hayes <ferenosdev@outlook.com>
|
2019-04-16 19:27:03 +02:00
|
|
|
* Copyright 2019, Gabriel Craciunescu <crazy@frugalware.org>
|
2015-04-10 14:43:39 +02:00
|
|
|
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
2018-06-15 11:59:11 +02:00
|
|
|
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
2014-06-11 13:37:10 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Settings.h"
|
2014-06-13 16:40:42 +02:00
|
|
|
|
2020-02-05 16:48:49 +01:00
|
|
|
#include "CalamaresConfig.h"
|
2019-04-29 12:27:31 +02:00
|
|
|
#include "utils/Dirs.h"
|
2014-06-12 18:47:50 +02:00
|
|
|
#include "utils/Logger.h"
|
2019-04-29 12:04:55 +02:00
|
|
|
#include "utils/Yaml.h"
|
2014-06-12 18:47:50 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2015-09-09 18:58:21 +02:00
|
|
|
#include <QPair>
|
2014-06-13 16:40:42 +02:00
|
|
|
|
2018-06-19 17:58:26 +02:00
|
|
|
static bool
|
|
|
|
hasValue( const YAML::Node& v )
|
|
|
|
{
|
2018-06-20 15:59:23 +02:00
|
|
|
return v.IsDefined() && !v.IsNull();
|
2018-06-19 17:58:26 +02:00
|
|
|
}
|
2014-06-13 16:40:42 +02:00
|
|
|
|
2019-04-01 12:26:42 +02:00
|
|
|
/** @brief Helper function to grab a QString out of the config, and to warn if not present. */
|
2018-06-11 14:33:47 +02:00
|
|
|
static QString
|
|
|
|
requireString( const YAML::Node& config, const char* key )
|
|
|
|
{
|
2018-06-19 17:58:26 +02:00
|
|
|
auto v = config[ key ];
|
2019-08-04 22:24:55 +02:00
|
|
|
if ( hasValue( v ) )
|
|
|
|
{
|
2018-06-19 17:58:26 +02:00
|
|
|
return QString::fromStdString( v.as< std::string >() );
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2018-06-11 14:33:47 +02:00
|
|
|
else
|
|
|
|
{
|
2019-04-15 14:15:17 +02:00
|
|
|
cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing.";
|
2018-06-11 14:33:47 +02:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:26:42 +02:00
|
|
|
/** @brief Helper function to grab a bool out of the config, and to warn if not present. */
|
2018-06-11 14:33:47 +02:00
|
|
|
static bool
|
|
|
|
requireBool( const YAML::Node& config, const char* key, bool d )
|
|
|
|
{
|
2018-06-19 17:58:26 +02:00
|
|
|
auto v = config[ key ];
|
2019-08-04 22:24:55 +02:00
|
|
|
if ( hasValue( v ) )
|
|
|
|
{
|
2018-06-19 17:58:26 +02:00
|
|
|
return v.as< bool >();
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2018-06-11 14:33:47 +02:00
|
|
|
else
|
|
|
|
{
|
2019-04-15 14:15:17 +02:00
|
|
|
cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing.";
|
2018-06-11 14:33:47 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:47:50 +02:00
|
|
|
namespace Calamares
|
|
|
|
{
|
|
|
|
|
2014-06-27 15:21:16 +02:00
|
|
|
Settings* Settings::s_instance = nullptr;
|
2014-06-12 18:47:50 +02:00
|
|
|
|
|
|
|
Settings*
|
|
|
|
Settings::instance()
|
|
|
|
{
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
2019-03-18 22:48:39 +01:00
|
|
|
static void
|
|
|
|
interpretModulesSearch( const bool debugMode, const QStringList& rawPaths, QStringList& output )
|
|
|
|
{
|
|
|
|
for ( const auto& path : rawPaths )
|
|
|
|
{
|
|
|
|
if ( path == "local" )
|
|
|
|
{
|
|
|
|
// If we're running in debug mode, we assume we might also be
|
|
|
|
// running from the build dir, so we add a maximum priority
|
|
|
|
// module search path in the build dir.
|
|
|
|
if ( debugMode )
|
|
|
|
{
|
2019-08-04 22:24:55 +02:00
|
|
|
QString buildDirModules
|
|
|
|
= QDir::current().absolutePath() + QDir::separator() + "src" + QDir::separator() + "modules";
|
2019-03-18 22:48:39 +01:00
|
|
|
if ( QDir( buildDirModules ).exists() )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-18 22:48:39 +01:00
|
|
|
output.append( buildDirModules );
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-18 22:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install path is set in CalamaresAddPlugin.cmake
|
2019-08-04 22:24:55 +02:00
|
|
|
output.append( CalamaresUtils::systemLibDir().absolutePath() + QDir::separator() + "calamares"
|
|
|
|
+ QDir::separator() + "modules" );
|
2019-03-18 22:48:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDir d( path );
|
|
|
|
if ( d.exists() && d.isReadable() )
|
|
|
|
{
|
|
|
|
output.append( d.absolutePath() );
|
|
|
|
}
|
|
|
|
else
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-04-15 14:15:17 +02:00
|
|
|
cDebug() << Logger::SubEntry << "module-search entry non-existent" << path;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-18 22:48:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 10:57:33 +01:00
|
|
|
static void
|
|
|
|
interpretInstances( const YAML::Node& node, Settings::InstanceDescriptionList& customInstances )
|
|
|
|
{
|
|
|
|
// Parse the custom instances section
|
|
|
|
if ( node )
|
|
|
|
{
|
|
|
|
QVariant instancesV = CalamaresUtils::yamlToVariant( node ).toList();
|
|
|
|
if ( instancesV.type() == QVariant::List )
|
|
|
|
{
|
|
|
|
const auto instances = instancesV.toList();
|
|
|
|
for ( const QVariant& instancesVListItem : instances )
|
|
|
|
{
|
|
|
|
if ( instancesVListItem.type() != QVariant::Map )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-19 10:57:33 +01:00
|
|
|
continue;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
|
|
|
QVariantMap instancesVListItemMap = instancesVListItem.toMap();
|
2019-03-19 10:57:33 +01:00
|
|
|
Settings::InstanceDescription instanceMap;
|
2019-08-04 22:24:55 +02:00
|
|
|
for ( auto it = instancesVListItemMap.constBegin(); it != instancesVListItemMap.constEnd(); ++it )
|
2019-03-19 10:57:33 +01:00
|
|
|
{
|
|
|
|
if ( it.value().type() != QVariant::String )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-19 10:57:33 +01:00
|
|
|
continue;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
instanceMap.insert( it.key(), it.value().toString() );
|
|
|
|
}
|
|
|
|
customInstances.append( instanceMap );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
interpretSequence( const YAML::Node& node, Settings::ModuleSequence& moduleSequence )
|
|
|
|
{
|
|
|
|
// Parse the modules sequence section
|
|
|
|
if ( node )
|
|
|
|
{
|
|
|
|
QVariant sequenceV = CalamaresUtils::yamlToVariant( node );
|
|
|
|
if ( !( sequenceV.type() == QVariant::List ) )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-19 10:57:33 +01:00
|
|
|
throw YAML::Exception( YAML::Mark(), "sequence key does not have a list-value" );
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
|
|
|
|
const auto sequence = sequenceV.toList();
|
|
|
|
for ( const QVariant& sequenceVListItem : sequence )
|
|
|
|
{
|
|
|
|
if ( sequenceVListItem.type() != QVariant::Map )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-19 10:57:33 +01:00
|
|
|
continue;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
QString thisActionS = sequenceVListItem.toMap().firstKey();
|
2019-09-14 14:18:56 +02:00
|
|
|
ModuleSystem::Action thisAction;
|
2019-03-19 10:57:33 +01:00
|
|
|
if ( thisActionS == "show" )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-09-14 14:18:56 +02:00
|
|
|
thisAction = ModuleSystem::Action::Show;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
else if ( thisActionS == "exec" )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-09-14 14:18:56 +02:00
|
|
|
thisAction = ModuleSystem::Action::Exec;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
else
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-19 10:57:33 +01:00
|
|
|
continue;
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
|
2019-08-04 22:24:55 +02:00
|
|
|
QStringList thisActionRoster = sequenceVListItem.toMap().value( thisActionS ).toStringList();
|
|
|
|
moduleSequence.append( qMakePair( thisAction, thisActionRoster ) );
|
2019-03-19 10:57:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2019-03-19 10:57:33 +01:00
|
|
|
throw YAML::Exception( YAML::Mark(), "sequence key is missing" );
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-03-19 10:57:33 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 16:48:49 +01:00
|
|
|
Settings::Settings( const QString& settingsFilePath, bool debugMode )
|
|
|
|
: QObject()
|
2014-07-17 17:44:16 +02:00
|
|
|
, m_debug( debugMode )
|
2016-02-26 13:19:47 +01:00
|
|
|
, m_doChroot( true )
|
2017-06-21 13:22:08 +02:00
|
|
|
, m_promptInstall( false )
|
2019-01-23 15:56:07 +01:00
|
|
|
, m_disableCancel( false )
|
2019-05-14 14:30:34 +02:00
|
|
|
, m_disableCancelDuringExec( false )
|
2014-06-11 13:37:10 +02:00
|
|
|
{
|
2014-08-06 17:23:40 +02:00
|
|
|
cDebug() << "Using Calamares settings file at" << settingsFilePath;
|
|
|
|
QFile file( settingsFilePath );
|
2014-06-13 16:40:42 +02:00
|
|
|
if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) )
|
2014-06-12 18:47:50 +02:00
|
|
|
{
|
|
|
|
QByteArray ba = file.readAll();
|
2014-06-13 16:40:42 +02:00
|
|
|
|
|
|
|
try
|
2014-06-12 18:47:50 +02:00
|
|
|
{
|
2014-06-13 16:40:42 +02:00
|
|
|
YAML::Node config = YAML::Load( ba.constData() );
|
|
|
|
Q_ASSERT( config.IsMap() );
|
2014-06-12 18:47:50 +02:00
|
|
|
|
2019-08-04 22:24:55 +02:00
|
|
|
interpretModulesSearch(
|
|
|
|
debugMode, CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths );
|
2019-03-19 10:57:33 +01:00
|
|
|
interpretInstances( config[ "instances" ], m_customModuleInstances );
|
|
|
|
interpretSequence( config[ "sequence" ], m_modulesSequence );
|
2015-09-09 18:58:21 +02:00
|
|
|
|
2018-06-11 14:33:47 +02:00
|
|
|
m_brandingComponentName = requireString( config, "branding" );
|
|
|
|
m_promptInstall = requireBool( config, "prompt-install", false );
|
2018-06-19 18:04:38 +02:00
|
|
|
m_doChroot = !requireBool( config, "dont-chroot", false );
|
2019-04-01 12:26:42 +02:00
|
|
|
m_isSetupMode = requireBool( config, "oem-setup", !m_doChroot );
|
2019-01-23 15:56:07 +01:00
|
|
|
m_disableCancel = requireBool( config, "disable-cancel", false );
|
2019-05-14 14:30:34 +02:00
|
|
|
m_disableCancelDuringExec = requireBool( config, "disable-cancel-during-exec", false );
|
2014-06-12 18:47:50 +02:00
|
|
|
}
|
2014-06-18 18:05:04 +02:00
|
|
|
catch ( YAML::Exception& e )
|
2014-06-12 18:47:50 +02:00
|
|
|
{
|
2018-02-26 20:07:06 +01:00
|
|
|
CalamaresUtils::explainYamlException( e, ba, file.fileName() );
|
2014-06-12 18:47:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-26 20:07:06 +01:00
|
|
|
cWarning() << "Cannot read settings file" << file.fileName();
|
2014-06-12 18:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QStringList
|
2014-07-11 14:40:15 +02:00
|
|
|
Settings::modulesSearchPaths() const
|
2014-06-12 18:47:50 +02:00
|
|
|
{
|
|
|
|
return m_modulesSearchPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-08 23:36:29 +02:00
|
|
|
Settings::InstanceDescriptionList
|
2015-09-09 18:58:21 +02:00
|
|
|
Settings::customModuleInstances() const
|
2014-06-12 18:47:50 +02:00
|
|
|
{
|
2015-09-09 18:58:21 +02:00
|
|
|
return m_customModuleInstances;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-26 14:41:16 +02:00
|
|
|
Settings::ModuleSequence
|
2015-09-09 18:58:21 +02:00
|
|
|
Settings::modulesSequence() const
|
|
|
|
{
|
|
|
|
return m_modulesSequence;
|
2014-06-12 18:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-10 18:13:02 +02:00
|
|
|
QString
|
|
|
|
Settings::brandingComponentName() const
|
|
|
|
{
|
|
|
|
return m_brandingComponentName;
|
|
|
|
}
|
|
|
|
|
2020-02-05 16:48:49 +01:00
|
|
|
static QStringList
|
|
|
|
settingsFileCandidates( bool assumeBuilddir )
|
|
|
|
{
|
|
|
|
static const char settings[] = "settings.conf";
|
|
|
|
|
|
|
|
QStringList settingsPaths;
|
|
|
|
if ( CalamaresUtils::isAppDataDirOverridden() )
|
|
|
|
{
|
|
|
|
settingsPaths << CalamaresUtils::appDataDir().absoluteFilePath( settings );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( assumeBuilddir )
|
|
|
|
{
|
|
|
|
settingsPaths << QDir::current().absoluteFilePath( settings );
|
|
|
|
}
|
|
|
|
if ( CalamaresUtils::haveExtraDirs() )
|
|
|
|
for ( auto s : CalamaresUtils::extraConfigDirs() )
|
|
|
|
{
|
|
|
|
settingsPaths << ( s + settings );
|
|
|
|
}
|
|
|
|
settingsPaths << CMAKE_INSTALL_FULL_SYSCONFDIR "/calamares/settings.conf"; // String concat
|
|
|
|
settingsPaths << CalamaresUtils::appDataDir().absoluteFilePath( settings );
|
|
|
|
}
|
|
|
|
|
|
|
|
return settingsPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings*
|
|
|
|
Settings::init( bool debugMode )
|
|
|
|
{
|
|
|
|
if ( s_instance )
|
|
|
|
{
|
|
|
|
cWarning() << "Calamares::Settings already created";
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList settingsFileCandidatesByPriority = settingsFileCandidates( debugMode );
|
|
|
|
|
|
|
|
QFileInfo settingsFile;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
foreach ( const QString& path, settingsFileCandidatesByPriority )
|
|
|
|
{
|
|
|
|
QFileInfo pathFi( path );
|
|
|
|
if ( pathFi.exists() && pathFi.isReadable() )
|
|
|
|
{
|
|
|
|
settingsFile = pathFi;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !found || !settingsFile.exists() || !settingsFile.isReadable() )
|
|
|
|
{
|
|
|
|
cError() << "Cowardly refusing to continue startup without settings."
|
|
|
|
<< Logger::DebugList( settingsFileCandidatesByPriority );
|
|
|
|
if ( CalamaresUtils::isAppDataDirOverridden() )
|
|
|
|
{
|
|
|
|
cError() << "FATAL: explicitly configured application data directory is missing settings.conf";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cError() << "FATAL: none of the expected configuration file paths exist.";
|
|
|
|
}
|
|
|
|
::exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* settings = new Calamares::Settings( settingsFile.absoluteFilePath(), debugMode ); // Creates singleton
|
|
|
|
if ( settings->modulesSequence().count() < 1 )
|
|
|
|
{
|
|
|
|
cError() << "FATAL: no sequence set.";
|
|
|
|
::exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings*
|
|
|
|
Settings::init( const QString& path )
|
|
|
|
{
|
|
|
|
if ( s_instance )
|
|
|
|
{
|
|
|
|
cWarning() << "Calamares::Settings already created";
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
return new Calamares::Settings( path, true );
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:24:55 +02:00
|
|
|
} // namespace Calamares
|