2017-12-20 15:12:27 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
2020-02-27 17:14:41 +01:00
|
|
|
* Copyright 2017-2020, Adriaan de Groot <groot@kde.org>
|
2017-12-20 15:12:27 +01: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 "ContextualProcessJob.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
2019-08-01 23:10:49 +02:00
|
|
|
#include <QProcess>
|
2017-12-20 15:12:27 +01:00
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#include "CalamaresVersion.h"
|
|
|
|
#include "GlobalStorage.h"
|
2019-08-01 23:10:49 +02:00
|
|
|
#include "JobQueue.h"
|
2017-12-20 15:12:27 +01:00
|
|
|
|
2018-01-15 14:57:34 +01:00
|
|
|
#include "utils/CommandList.h"
|
2017-12-20 15:12:27 +01:00
|
|
|
#include "utils/Logger.h"
|
2019-04-29 11:51:27 +02:00
|
|
|
#include "utils/Variant.h"
|
2017-12-20 15:12:27 +01:00
|
|
|
|
2019-08-01 23:10:49 +02:00
|
|
|
struct ValueCheck : public QPair< QString, CalamaresUtils::CommandList* >
|
2018-02-19 12:18:08 +01:00
|
|
|
{
|
|
|
|
ValueCheck( const QString& value, CalamaresUtils::CommandList* commands )
|
2019-08-01 23:10:49 +02:00
|
|
|
: QPair< QString, CalamaresUtils::CommandList* >( value, commands )
|
2018-02-19 12:18:08 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:24:12 +01:00
|
|
|
// ~ValueCheck()
|
|
|
|
//
|
|
|
|
// There is no destructor.
|
|
|
|
//
|
|
|
|
// We don't own the commandlist, the binding holding this valuecheck
|
|
|
|
// does, so don't delete. This is closely tied to (temporaries created
|
|
|
|
// by) pass-by-value in QList::append().
|
2018-02-19 12:18:08 +01:00
|
|
|
|
|
|
|
QString value() const { return first; }
|
|
|
|
CalamaresUtils::CommandList* commands() const { return second; }
|
2019-08-01 23:10:49 +02:00
|
|
|
};
|
2018-02-19 12:18:08 +01:00
|
|
|
|
2020-02-27 17:14:41 +01:00
|
|
|
class ContextualProcessBinding
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
public:
|
2018-02-19 12:18:08 +01:00
|
|
|
ContextualProcessBinding( const QString& varname )
|
2020-02-27 17:14:41 +01:00
|
|
|
: m_variable( varname )
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ContextualProcessBinding();
|
|
|
|
|
2020-02-27 17:14:41 +01:00
|
|
|
QString variable() const { return m_variable; }
|
|
|
|
int count() const { return m_checks.count(); }
|
|
|
|
|
2018-02-19 12:18:08 +01:00
|
|
|
/**
|
|
|
|
* @brief add commands to be executed when @p value is matched.
|
|
|
|
*
|
2018-02-19 12:58:42 +01:00
|
|
|
* Ownership of the CommandList passes to this binding.
|
2018-02-19 12:18:08 +01:00
|
|
|
*/
|
|
|
|
void append( const QString& value, CalamaresUtils::CommandList* commands )
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
m_checks.append( ValueCheck( value, commands ) );
|
2018-06-14 14:50:47 +02:00
|
|
|
if ( value == QString( "*" ) )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
m_wildcard = commands;
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-02-19 12:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult run( const QString& value ) const
|
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
for ( const auto& c : m_checks )
|
2018-02-19 12:18:08 +01:00
|
|
|
{
|
|
|
|
if ( value == c.value() )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2018-02-19 12:18:08 +01:00
|
|
|
return c.commands()->run();
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-02-19 12:18:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:14:41 +01:00
|
|
|
if ( m_wildcard )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
return m_wildcard->run();
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-02-19 12:18:08 +01:00
|
|
|
|
|
|
|
return Calamares::JobResult::ok();
|
2018-01-15 14:57:34 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:14:41 +01:00
|
|
|
/** @brief Tries to obtain this binding's value from GS
|
|
|
|
*
|
|
|
|
* Stores the value in @p value and returns true if a value
|
|
|
|
* was found (e.g. @p storage contains the variable this binding
|
|
|
|
* is for) and false otherwise.
|
|
|
|
*/
|
|
|
|
bool fetch( Calamares::GlobalStorage* storage, QString& value ) const
|
|
|
|
{
|
|
|
|
value.clear();
|
|
|
|
if ( !storage )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( m_variable.contains( '.' ) )
|
|
|
|
{
|
|
|
|
QStringList steps = m_variable.split( '.' );
|
|
|
|
return fetch( value, steps, 1, storage->value( steps.first() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = storage->value( m_variable ).toString();
|
|
|
|
return storage->contains( m_variable );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool fetch( QString& value, QStringList& selector, int index, const QVariant& v )
|
|
|
|
{
|
|
|
|
if ( !v.canConvert( QMetaType::QVariantMap ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const QVariantMap map = v.toMap();
|
|
|
|
const QString& key = selector.at( index );
|
|
|
|
if ( index == selector.length() )
|
|
|
|
{
|
|
|
|
value = map.value( key ).toString();
|
|
|
|
return map.contains( key );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return fetch( value, selector, index + 1, map.value( key ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString m_variable;
|
|
|
|
QList< ValueCheck > m_checks;
|
|
|
|
CalamaresUtils::CommandList* m_wildcard = nullptr;
|
2019-08-01 23:10:49 +02:00
|
|
|
};
|
2018-01-15 14:57:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
ContextualProcessBinding::~ContextualProcessBinding()
|
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
m_wildcard = nullptr;
|
|
|
|
for ( const auto& c : m_checks )
|
2018-02-19 12:58:42 +01:00
|
|
|
{
|
|
|
|
delete c.commands();
|
|
|
|
}
|
2018-01-15 14:57:34 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 15:12:27 +01:00
|
|
|
ContextualProcessJob::ContextualProcessJob( QObject* parent )
|
|
|
|
: Calamares::CppJob( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ContextualProcessJob::~ContextualProcessJob()
|
|
|
|
{
|
2018-01-15 14:57:34 +01:00
|
|
|
qDeleteAll( m_commands );
|
2017-12-20 15:12:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
ContextualProcessJob::prettyName() const
|
|
|
|
{
|
|
|
|
return tr( "Contextual Processes Job" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Calamares::JobResult
|
|
|
|
ContextualProcessJob::exec()
|
|
|
|
{
|
2018-01-15 14:57:34 +01:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2017-12-20 15:12:27 +01:00
|
|
|
|
2018-01-15 14:57:34 +01:00
|
|
|
for ( const ContextualProcessBinding* binding : m_commands )
|
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
QString value;
|
|
|
|
if ( binding->fetch( gs, value ) )
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
Calamares::JobResult r = binding->run( value );
|
2018-01-15 14:57:34 +01:00
|
|
|
if ( !r )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2018-01-15 14:57:34 +01:00
|
|
|
return r;
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-01-15 14:57:34 +01:00
|
|
|
}
|
2018-02-19 12:34:52 +01:00
|
|
|
else
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
cWarning() << "ContextualProcess checks for unknown variable" << binding->variable();
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-01-15 14:57:34 +01:00
|
|
|
}
|
2017-12-20 15:12:27 +01:00
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ContextualProcessJob::setConfigurationMap( const QVariantMap& configurationMap )
|
|
|
|
{
|
2018-01-29 22:08:12 +01:00
|
|
|
bool dontChroot = CalamaresUtils::getBool( configurationMap, "dontChroot", false );
|
2019-08-21 11:56:49 +02:00
|
|
|
qint64 timeout = CalamaresUtils::getInteger( configurationMap, "timeout", 10 );
|
2018-01-29 22:08:12 +01:00
|
|
|
if ( timeout < 1 )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2018-01-29 22:08:12 +01:00
|
|
|
timeout = 10;
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-01-15 14:57:34 +01:00
|
|
|
|
|
|
|
for ( QVariantMap::const_iterator iter = configurationMap.cbegin(); iter != configurationMap.cend(); ++iter )
|
|
|
|
{
|
|
|
|
QString variableName = iter.key();
|
2018-01-29 22:08:12 +01:00
|
|
|
if ( variableName.isEmpty() || ( variableName == "dontChroot" ) || ( variableName == "timeout" ) )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2018-01-15 14:57:34 +01:00
|
|
|
continue;
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2018-01-15 14:57:34 +01:00
|
|
|
|
|
|
|
if ( iter.value().type() != QVariant::Map )
|
|
|
|
{
|
2018-02-12 17:55:06 +01:00
|
|
|
cWarning() << moduleInstanceKey() << "bad configuration values for" << variableName;
|
2018-01-15 14:57:34 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-19 12:18:08 +01:00
|
|
|
auto binding = new ContextualProcessBinding( variableName );
|
|
|
|
m_commands.append( binding );
|
2018-01-15 14:57:34 +01:00
|
|
|
QVariantMap values = iter.value().toMap();
|
|
|
|
for ( QVariantMap::const_iterator valueiter = values.cbegin(); valueiter != values.cend(); ++valueiter )
|
|
|
|
{
|
|
|
|
QString valueString = valueiter.key();
|
|
|
|
if ( variableName.isEmpty() )
|
|
|
|
{
|
2019-08-01 23:10:49 +02:00
|
|
|
cWarning() << moduleInstanceKey() << "variable" << variableName << "unrecognized value"
|
|
|
|
<< valueiter.key();
|
2018-01-15 14:57:34 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-01 23:10:49 +02:00
|
|
|
CalamaresUtils::CommandList* commands
|
|
|
|
= new CalamaresUtils::CommandList( valueiter.value(), !dontChroot, std::chrono::seconds( timeout ) );
|
2018-01-15 14:57:34 +01:00
|
|
|
|
2018-02-19 12:18:08 +01:00
|
|
|
binding->append( valueString, commands );
|
2018-01-15 14:57:34 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-20 15:12:27 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 12:42:13 +01:00
|
|
|
int
|
|
|
|
ContextualProcessJob::count()
|
|
|
|
{
|
|
|
|
return m_commands.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-08-01 23:10:49 +02:00
|
|
|
ContextualProcessJob::count( const QString& variableName )
|
2018-02-19 12:42:13 +01:00
|
|
|
{
|
|
|
|
for ( const ContextualProcessBinding* binding : m_commands )
|
2020-02-27 17:14:41 +01:00
|
|
|
{
|
|
|
|
if ( binding->variable() == variableName )
|
2019-08-01 23:10:49 +02:00
|
|
|
{
|
2020-02-27 17:14:41 +01:00
|
|
|
return binding->count();
|
2019-08-01 23:10:49 +02:00
|
|
|
}
|
2020-02-27 17:14:41 +01:00
|
|
|
}
|
2018-02-19 12:42:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-01 23:10:49 +02:00
|
|
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( ContextualProcessJobFactory, registerPlugin< ContextualProcessJob >(); )
|