2017-12-20 15:12:27 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
2018-01-15 14:57:34 +01:00
|
|
|
* Copyright 2017-2018, 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 <QProcess>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#include "CalamaresVersion.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
|
2018-01-15 14:57:34 +01:00
|
|
|
#include "utils/CalamaresUtils.h"
|
|
|
|
#include "utils/CommandList.h"
|
2017-12-20 15:12:27 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
2018-02-19 12:18:08 +01:00
|
|
|
/**
|
|
|
|
* Passing a CommandList to ValueCheck gives ownership of the CommandList to
|
|
|
|
* the ValueCheck, which will delete the CommandList when needed.
|
|
|
|
*/
|
|
|
|
struct ValueCheck : public QPair<QString, CalamaresUtils::CommandList*>
|
|
|
|
{
|
|
|
|
ValueCheck( const QString& value, CalamaresUtils::CommandList* commands )
|
|
|
|
: QPair<QString, CalamaresUtils::CommandList*>(value, commands)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ValueCheck()
|
|
|
|
{
|
|
|
|
delete second;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString value() const { return first; }
|
|
|
|
CalamaresUtils::CommandList* commands() const { return second; }
|
|
|
|
} ;
|
|
|
|
|
2018-01-15 14:57:34 +01:00
|
|
|
struct ContextualProcessBinding
|
|
|
|
{
|
2018-02-19 12:18:08 +01:00
|
|
|
ContextualProcessBinding( const QString& varname )
|
|
|
|
: variable( varname )
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ContextualProcessBinding();
|
|
|
|
|
2018-02-19 12:18:08 +01:00
|
|
|
/**
|
|
|
|
* @brief add commands to be executed when @p value is matched.
|
|
|
|
*
|
|
|
|
* Ownership of the CommandList passes to the ValueCheck held
|
|
|
|
* by this binding.
|
|
|
|
*/
|
|
|
|
void append( const QString& value, CalamaresUtils::CommandList* commands )
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
2018-02-19 12:18:08 +01:00
|
|
|
checks.append( ValueCheck( value, commands ) );
|
|
|
|
if ( value == '*' )
|
|
|
|
wildcard = commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult run( const QString& value ) const
|
|
|
|
{
|
|
|
|
for ( const auto& c : checks )
|
|
|
|
{
|
|
|
|
if ( value == c.value() )
|
|
|
|
return c.commands()->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( wildcard )
|
|
|
|
return wildcard->run();
|
|
|
|
|
|
|
|
return Calamares::JobResult::ok();
|
2018-01-15 14:57:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString variable;
|
2018-02-19 12:18:08 +01:00
|
|
|
QList<ValueCheck> checks;
|
|
|
|
CalamaresUtils::CommandList* wildcard{ nullptr };
|
2018-01-15 14:57:34 +01:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
ContextualProcessBinding::~ContextualProcessBinding()
|
|
|
|
{
|
2018-02-19 12:18:08 +01:00
|
|
|
wildcard = nullptr;
|
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 )
|
|
|
|
{
|
2018-02-19 12:18:08 +01:00
|
|
|
if ( gs->contains( binding->variable ) )
|
2018-01-15 14:57:34 +01:00
|
|
|
{
|
2018-02-19 12:18:08 +01:00
|
|
|
Calamares::JobResult r = binding->run( gs->value( binding->variable ).toString() );
|
2018-01-15 14:57:34 +01:00
|
|
|
if ( !r )
|
|
|
|
return r;
|
|
|
|
}
|
2018-02-19 12:34:52 +01:00
|
|
|
else
|
|
|
|
cWarning() << "ContextualProcess checks for unknown variable" << binding->variable;
|
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 );
|
|
|
|
int timeout = CalamaresUtils::getInteger( configurationMap, "timeout", 10 );
|
|
|
|
if ( timeout < 1 )
|
|
|
|
timeout = 10;
|
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" ) )
|
2018-01-15 14:57:34 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
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() )
|
|
|
|
{
|
2018-02-12 17:55:06 +01:00
|
|
|
cWarning() << moduleInstanceKey() << "variable" << variableName << "unrecognized value" << valueiter.key();
|
2018-01-15 14:57:34 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-29 22:08:12 +01:00
|
|
|
CalamaresUtils::CommandList* commands = new CalamaresUtils::CommandList( valueiter.value(), !dontChroot, 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
|
|
|
}
|
|
|
|
|
|
|
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( ContextualProcessJobFactory, registerPlugin<ContextualProcessJob>(); )
|