From 5f8fb655c4410bdd57f5977374c4d9533735c663 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 10 Jan 2018 08:58:15 -0500 Subject: [PATCH] [shellprocess] Stub of a shell process module. This is basically dummyprocess, except with an expanded configuration interface so you can run 1 or more shell commands in the live or target system with a suitable configuration file and instance of shellprocess in settings.conf. It can replace downstream modules that implement their own process modules with a command, by an instance of shellprocess. --- src/modules/shellprocess/CMakeLists.txt | 30 ++++++++ src/modules/shellprocess/CommandList.cpp | 70 +++++++++++++++++ src/modules/shellprocess/CommandList.h | 39 ++++++++++ src/modules/shellprocess/ShellProcessJob.cpp | 80 ++++++++++++++++++++ src/modules/shellprocess/ShellProcessJob.h | 54 +++++++++++++ src/modules/shellprocess/Tests.cpp | 65 ++++++++++++++++ src/modules/shellprocess/Tests.h | 36 +++++++++ src/modules/shellprocess/module.desc | 5 ++ src/modules/shellprocess/shellprocess.conf | 19 +++++ 9 files changed, 398 insertions(+) create mode 100644 src/modules/shellprocess/CMakeLists.txt create mode 100644 src/modules/shellprocess/CommandList.cpp create mode 100644 src/modules/shellprocess/CommandList.h create mode 100644 src/modules/shellprocess/ShellProcessJob.cpp create mode 100644 src/modules/shellprocess/ShellProcessJob.h create mode 100644 src/modules/shellprocess/Tests.cpp create mode 100644 src/modules/shellprocess/Tests.h create mode 100644 src/modules/shellprocess/module.desc create mode 100644 src/modules/shellprocess/shellprocess.conf diff --git a/src/modules/shellprocess/CMakeLists.txt b/src/modules/shellprocess/CMakeLists.txt new file mode 100644 index 000000000..0ed69add5 --- /dev/null +++ b/src/modules/shellprocess/CMakeLists.txt @@ -0,0 +1,30 @@ +calamares_add_plugin( shellprocess + TYPE job + EXPORT_MACRO PLUGINDLLEXPORT_PRO + SOURCES + CommandList.cpp + ShellProcessJob.cpp + LINK_PRIVATE_LIBRARIES + calamares + SHARED_LIB +) + +find_package(ECM ${ECM_VERSION} NO_MODULE) +if( ECM_FOUND ) + find_package( Qt5 COMPONENTS Test REQUIRED ) + include( ECMAddTests ) + + ecm_add_test( + Tests.cpp + CommandList.cpp + TEST_NAME + shellprocesstest + LINK_LIBRARIES + ${CALAMARES_LIBRARIES} + calamaresui + ${YAMLCPP_LIBRARY} + Qt5::Core + Qt5::Test + ) + set_target_properties( shellprocesstest PROPERTIES AUTOMOC TRUE ) +endif() diff --git a/src/modules/shellprocess/CommandList.cpp b/src/modules/shellprocess/CommandList.cpp new file mode 100644 index 000000000..261ff9429 --- /dev/null +++ b/src/modules/shellprocess/CommandList.cpp @@ -0,0 +1,70 @@ +/* === This file is part of Calamares - === + * + * Copyright 2018, Adriaan de Groot + * + * 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 . + */ + +#include "CommandList.h" + +#include + +class CommandList::Private +{ +public: + Private( const QVariantList& l ); + ~Private(); +} ; + +CommandList::Private::Private(const QVariantList& l) +{ +} + +CommandList::Private::~Private() +{ +} + + + +CommandList::CommandList() + : m_d( nullptr ) +{ +} + +CommandList::CommandList::CommandList(const QVariant& v) + : CommandList() +{ + if ( ( v.type() == QVariant::List ) ) + { + const auto v_list = v.toList(); + if ( v_list.count() ) + { + m_d = new Private( v_list ); + } + } +} + +CommandList::~CommandList() +{ + delete m_d; + m_d = nullptr; // TODO: UniquePtr +} + +bool CommandList::isEmpty() const +{ + if ( !m_d ) + return true; + + return false; // FIXME: actually count things +} diff --git a/src/modules/shellprocess/CommandList.h b/src/modules/shellprocess/CommandList.h new file mode 100644 index 000000000..ed6b69b00 --- /dev/null +++ b/src/modules/shellprocess/CommandList.h @@ -0,0 +1,39 @@ +/* === This file is part of Calamares - === + * + * Copyright 2018, Adriaan de Groot + * + * 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 . + */ + +#ifndef COMMANDLIST_H +#define COMMANDLIST_H + +#include + +class CommandList +{ + class Private; + +public: + CommandList(); + CommandList(const QVariant& v); + ~CommandList(); + + bool isEmpty() const; + +private: + Private *m_d; +} ; + +#endif // COMMANDLIST_H diff --git a/src/modules/shellprocess/ShellProcessJob.cpp b/src/modules/shellprocess/ShellProcessJob.cpp new file mode 100644 index 000000000..57d7dcabe --- /dev/null +++ b/src/modules/shellprocess/ShellProcessJob.cpp @@ -0,0 +1,80 @@ +/* === This file is part of Calamares - === + * + * Copyright 2018, Adriaan de Groot + * + * 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 . + */ + +#include "ShellProcessJob.h" + +#include "CommandList.h" + +#include +#include +#include + +#include "CalamaresVersion.h" +#include "JobQueue.h" +#include "GlobalStorage.h" + +#include "utils/CalamaresUtils.h" +#include "utils/Logger.h" + +class CommandList; + +ShellProcessJob::ShellProcessJob( QObject* parent ) + : Calamares::CppJob( parent ) + , m_commands( nullptr ) + , m_dontChroot( false ) +{ +} + + +ShellProcessJob::~ShellProcessJob() +{ + delete m_commands; + m_commands = nullptr; // TODO: UniquePtr +} + + +QString +ShellProcessJob::prettyName() const +{ + return tr( "Shell Processes Job" ); +} + + +Calamares::JobResult +ShellProcessJob::exec() +{ + QThread::sleep( 3 ); + + return Calamares::JobResult::ok(); +} + + +void +ShellProcessJob::setConfigurationMap( const QVariantMap& configurationMap ) +{ + m_dontChroot = CalamaresUtils::getBool( configurationMap, "dontChroot", false ); + + if ( configurationMap.contains( "script" ) ) + { + m_commands = new CommandList( configurationMap.value( "script" ) ); + if ( m_commands->isEmpty() ) + cDebug() << "ShellProcessJob: \"script\" contains no commands."; + } +} + +CALAMARES_PLUGIN_FACTORY_DEFINITION( ShellProcessJobFactory, registerPlugin(); ) diff --git a/src/modules/shellprocess/ShellProcessJob.h b/src/modules/shellprocess/ShellProcessJob.h new file mode 100644 index 000000000..c11ec9565 --- /dev/null +++ b/src/modules/shellprocess/ShellProcessJob.h @@ -0,0 +1,54 @@ +/* === This file is part of Calamares - === + * + * Copyright 2018, Adriaan de Groot + * + * 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 . + */ + +#ifndef SHELLPROCESSJOB_H +#define SHELLPROCESSJOB_H + +#include +#include + +#include + +#include + +#include + +class CommandList; + +class PLUGINDLLEXPORT ShellProcessJob : public Calamares::CppJob +{ + Q_OBJECT + +public: + explicit ShellProcessJob( QObject* parent = nullptr ); + virtual ~ShellProcessJob() override; + + QString prettyName() const override; + + Calamares::JobResult exec() override; + + void setConfigurationMap( const QVariantMap& configurationMap ) override; + +private: + CommandList *m_commands; + bool m_dontChroot; +}; + +CALAMARES_PLUGIN_FACTORY_DECLARATION( ShellProcessJobFactory ) + +#endif // SHELLPROCESSJOB_H diff --git a/src/modules/shellprocess/Tests.cpp b/src/modules/shellprocess/Tests.cpp new file mode 100644 index 000000000..f30619c1f --- /dev/null +++ b/src/modules/shellprocess/Tests.cpp @@ -0,0 +1,65 @@ +/* === This file is part of Calamares - === + * + * Copyright 2017, Adriaan de Groot + * + * 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 . + */ + +#include "Tests.h" +#include "CommandList.h" + +#include "utils/YamlUtils.h" + +#include + +#include + +#include +#include + +QTEST_GUILESS_MAIN( ShellProcessTests ) + +ShellProcessTests::ShellProcessTests() +{ +} + +ShellProcessTests::~ShellProcessTests() +{ +} + +void +ShellProcessTests::initTestCase() +{ +} + +void +ShellProcessTests::testProcessList() +{ + YAML::Node doc; + + QStringList dirs { "src/modules/shellprocess", "." }; + for ( const auto& dir : dirs ) + { + QString filename = dir + "/shellprocess.conf"; + if ( QFileInfo::exists( filename ) ) + { + doc = YAML::LoadFile( filename.toStdString() ); + break; + } + } + + CommandList cl( + CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + QVERIFY( !cl.isEmpty() ); +} diff --git a/src/modules/shellprocess/Tests.h b/src/modules/shellprocess/Tests.h new file mode 100644 index 000000000..82f93afe6 --- /dev/null +++ b/src/modules/shellprocess/Tests.h @@ -0,0 +1,36 @@ +/* === This file is part of Calamares - === + * + * Copyright 2018, Adriaan de Groot + * + * 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 . + */ + +#ifndef TESTS_H +#define TESTS_H + +#include + +class ShellProcessTests : public QObject +{ + Q_OBJECT +public: + ShellProcessTests(); + ~ShellProcessTests() override; + +private Q_SLOTS: + void initTestCase(); + void testProcessList(); +}; + +#endif diff --git a/src/modules/shellprocess/module.desc b/src/modules/shellprocess/module.desc new file mode 100644 index 000000000..ade63fca3 --- /dev/null +++ b/src/modules/shellprocess/module.desc @@ -0,0 +1,5 @@ +--- +type: "job" +name: "shellprocess" +interface: "qtplugin" +load: "libcalamares_job_shellprocess.so" diff --git a/src/modules/shellprocess/shellprocess.conf b/src/modules/shellprocess/shellprocess.conf new file mode 100644 index 000000000..0825538e1 --- /dev/null +++ b/src/modules/shellprocess/shellprocess.conf @@ -0,0 +1,19 @@ +# Configuration for the shell process job. +# +# Executes a list of commands found under the key *script*. +# If the top-level key *dontChroot* is true, then the commands +# are executed in the context of the live system, otherwise +# in the context of the target system. In all of the commands, +# `@@ROOT@@` is replaced by the root mount point of the **target** +# system from the point of view of the command (for chrooted +# commands, that will be */*). +# +# If a command starts with "-" (a single minus sign), then the +# return value of the command following the - is ignored; otherwise, +# a failing command will abort the installation. This is much like +# make's use of - in a command. +--- +dontChroot: false +script: + - "-touch @@ROOT@@/tmp/thingy" + - "/usr/bin/false"