2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2017-11-09 11:19:24 +01:00
|
|
|
*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2017-11-09 11:19:24 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2017-11-09 11:19:24 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TrackingJobs.h"
|
|
|
|
|
2020-05-19 10:42:25 +02:00
|
|
|
#include "Config.h"
|
|
|
|
|
2020-06-17 15:24:21 +02:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
2019-08-26 15:45:10 +02:00
|
|
|
#include "network/Manager.h"
|
2017-11-22 14:04:37 +01:00
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
2017-11-09 11:19:24 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
2020-06-17 10:36:35 +02:00
|
|
|
#include <KMacroExpander>
|
|
|
|
|
2021-12-07 15:01:29 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2019-08-01 23:07:45 +02:00
|
|
|
#include <chrono>
|
|
|
|
|
2021-09-08 13:30:32 +02:00
|
|
|
|
|
|
|
// Namespace keeps all the actual jobs anonymous, the
|
|
|
|
// public API is the addJob() functions below the namespace.
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @brief Install-tracking job (gets a URL)
|
|
|
|
*
|
|
|
|
* The install-tracking job (there is only one kind) does a GET
|
|
|
|
* on a configured URL with some additional information about
|
|
|
|
* the machine (if configured into the URL).
|
|
|
|
*
|
|
|
|
* No persistent tracking is done.
|
|
|
|
*/
|
|
|
|
class TrackingInstallJob : public Calamares::Job
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TrackingInstallJob( const QString& url );
|
|
|
|
~TrackingInstallJob() override;
|
|
|
|
|
|
|
|
QString prettyName() const override;
|
|
|
|
QString prettyStatusMessage() const override;
|
|
|
|
Calamares::JobResult exec() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QString m_url;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Tracking machines, update-manager style
|
|
|
|
*
|
|
|
|
* The machine has a machine-id, and this is sed(1)'ed into the
|
|
|
|
* update-manager configuration, to report the machine-id back
|
|
|
|
* to distro servers.
|
|
|
|
*/
|
|
|
|
class TrackingMachineUpdateManagerJob : public Calamares::Job
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~TrackingMachineUpdateManagerJob() override;
|
|
|
|
|
|
|
|
QString prettyName() const override;
|
|
|
|
QString prettyStatusMessage() const override;
|
|
|
|
Calamares::JobResult exec() override;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Turn on KUserFeedback in target system
|
|
|
|
*
|
|
|
|
* This writes suitable files for turning on KUserFeedback for the
|
|
|
|
* normal user configured in Calamares. The feedback can be reconfigured
|
|
|
|
* by the user through Plasma's user-feedback dialog.
|
|
|
|
*/
|
|
|
|
class TrackingKUserFeedbackJob : public Calamares::Job
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TrackingKUserFeedbackJob( const QString& username, const QStringList& areas );
|
|
|
|
~TrackingKUserFeedbackJob() override;
|
|
|
|
|
|
|
|
QString prettyName() const override;
|
|
|
|
QString prettyStatusMessage() const override;
|
|
|
|
Calamares::JobResult exec() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString m_username;
|
|
|
|
QStringList m_areas;
|
|
|
|
};
|
|
|
|
|
2017-11-09 11:19:24 +01:00
|
|
|
TrackingInstallJob::TrackingInstallJob( const QString& url )
|
|
|
|
: m_url( url )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:14:06 +02:00
|
|
|
TrackingInstallJob::~TrackingInstallJob() {}
|
2017-11-09 11:45:25 +01:00
|
|
|
|
2019-08-01 23:05:42 +02:00
|
|
|
QString
|
|
|
|
TrackingInstallJob::prettyName() const
|
2017-11-09 11:19:24 +01:00
|
|
|
{
|
2021-12-07 15:01:29 +01:00
|
|
|
return QCoreApplication::translate( "TrackingInstallJob", "Installation feedback" );
|
2017-11-09 11:19:24 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:05:42 +02:00
|
|
|
QString
|
|
|
|
TrackingInstallJob::prettyStatusMessage() const
|
2017-11-09 11:19:24 +01:00
|
|
|
{
|
2021-12-07 15:01:29 +01:00
|
|
|
return QCoreApplication::translate( "TrackingInstallJob", "Sending installation feedback." );
|
2017-11-09 11:19:24 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:05:42 +02:00
|
|
|
Calamares::JobResult
|
|
|
|
TrackingInstallJob::exec()
|
2017-11-09 11:19:24 +01:00
|
|
|
{
|
2019-08-26 15:45:10 +02:00
|
|
|
using CalamaresUtils::Network::Manager;
|
|
|
|
using CalamaresUtils::Network::RequestOptions;
|
|
|
|
using CalamaresUtils::Network::RequestStatus;
|
|
|
|
|
|
|
|
auto result = Manager::instance().synchronousPing(
|
|
|
|
QUrl( m_url ),
|
|
|
|
RequestOptions( RequestOptions::FollowRedirect | RequestOptions::FakeUserAgent,
|
|
|
|
RequestOptions::milliseconds( 5000 ) ) );
|
|
|
|
if ( result.status == RequestStatus::Timeout )
|
2017-11-09 11:19:24 +01:00
|
|
|
{
|
2018-02-13 11:07:12 +01:00
|
|
|
cWarning() << "install-tracking request timed out.";
|
2021-12-07 15:01:29 +01:00
|
|
|
return Calamares::JobResult::error(
|
|
|
|
QCoreApplication::translate( "TrackingInstallJob", "Internal error in install-tracking." ),
|
|
|
|
QCoreApplication::translate( "TrackingInstallJob", "HTTP request timed out." ) );
|
2017-11-09 11:19:24 +01:00
|
|
|
}
|
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:37:56 +02:00
|
|
|
TrackingMachineUpdateManagerJob::~TrackingMachineUpdateManagerJob() {}
|
2020-05-19 10:42:25 +02:00
|
|
|
|
2019-08-01 23:05:42 +02:00
|
|
|
QString
|
2020-06-17 10:18:08 +02:00
|
|
|
TrackingMachineUpdateManagerJob::prettyName() const
|
2017-11-22 13:39:52 +01:00
|
|
|
{
|
2021-12-07 15:01:29 +01:00
|
|
|
return QCoreApplication::translate( "TrackingMachineUpdateManagerJob", "Machine feedback" );
|
2017-11-22 13:39:52 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:05:42 +02:00
|
|
|
QString
|
2020-06-17 10:18:08 +02:00
|
|
|
TrackingMachineUpdateManagerJob::prettyStatusMessage() const
|
2017-11-22 13:39:52 +01:00
|
|
|
{
|
2021-12-07 15:01:29 +01:00
|
|
|
return QCoreApplication::translate( "TrackingMachineUpdateManagerJob", "Configuring machine feedback." );
|
2017-11-22 13:39:52 +01:00
|
|
|
}
|
|
|
|
|
2019-08-01 23:05:42 +02:00
|
|
|
Calamares::JobResult
|
2020-06-17 10:18:08 +02:00
|
|
|
TrackingMachineUpdateManagerJob::exec()
|
2017-11-22 13:39:52 +01:00
|
|
|
{
|
2020-06-17 14:14:06 +02:00
|
|
|
static const auto script = QStringLiteral(
|
|
|
|
"sed -i '/^URI/s,${MACHINE_ID},'`cat /etc/machine-id`',' /etc/update-manager/meta-release || true" );
|
|
|
|
|
|
|
|
auto res = CalamaresUtils::System::instance()->runCommand( CalamaresUtils::System::RunLocation::RunInTarget,
|
|
|
|
QStringList { QStringLiteral( "/bin/sh" ) },
|
|
|
|
QString(), // Working dir
|
|
|
|
script, // standard input
|
|
|
|
std::chrono::seconds( 1 ) );
|
2020-06-17 11:30:12 +02:00
|
|
|
int r = res.first;
|
2017-11-22 14:04:37 +01:00
|
|
|
|
|
|
|
if ( r == 0 )
|
2019-08-02 09:19:15 +02:00
|
|
|
{
|
2017-11-22 14:04:37 +01:00
|
|
|
return Calamares::JobResult::ok();
|
2019-08-02 09:19:15 +02:00
|
|
|
}
|
2017-11-22 14:04:37 +01:00
|
|
|
else if ( r > 0 )
|
2019-08-02 09:19:15 +02:00
|
|
|
{
|
2019-08-01 23:05:42 +02:00
|
|
|
return Calamares::JobResult::error(
|
2021-12-07 15:01:29 +01:00
|
|
|
QCoreApplication::translate( "TrackingMachineUpdateManagerJob",
|
|
|
|
"Error in machine feedback configuration." ),
|
|
|
|
QCoreApplication::translate( "TrackingMachineUpdateManagerJob",
|
|
|
|
"Could not configure machine feedback correctly, script error %1." )
|
|
|
|
.arg( r ) );
|
2019-08-02 09:19:15 +02:00
|
|
|
}
|
2017-11-22 14:04:37 +01:00
|
|
|
else
|
2019-08-02 09:19:15 +02:00
|
|
|
{
|
2019-08-01 23:05:42 +02:00
|
|
|
return Calamares::JobResult::error(
|
2021-12-07 15:01:29 +01:00
|
|
|
QCoreApplication::translate( "TrackingMachineUpdateManagerJob",
|
|
|
|
"Error in machine feedback configuration." ),
|
|
|
|
QCoreApplication::translate( "TrackingMachineUpdateManagerJob",
|
|
|
|
"Could not configure machine feedback correctly, Calamares error %1." )
|
|
|
|
.arg( r ) );
|
2019-08-02 09:19:15 +02:00
|
|
|
}
|
2017-11-22 13:39:52 +01:00
|
|
|
}
|
2020-05-25 15:56:32 +02:00
|
|
|
|
2020-06-17 15:11:11 +02:00
|
|
|
TrackingKUserFeedbackJob::TrackingKUserFeedbackJob( const QString& username, const QStringList& areas )
|
|
|
|
: m_username( username )
|
|
|
|
, m_areas( areas )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:37:56 +02:00
|
|
|
TrackingKUserFeedbackJob::~TrackingKUserFeedbackJob() {}
|
|
|
|
|
2020-05-25 15:56:32 +02:00
|
|
|
QString
|
|
|
|
TrackingKUserFeedbackJob::prettyName() const
|
|
|
|
{
|
2021-12-07 15:01:29 +01:00
|
|
|
return QCoreApplication::translate( "TrackingKUserFeedbackJob", "KDE user feedback" );
|
2020-05-25 15:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
TrackingKUserFeedbackJob::prettyStatusMessage() const
|
|
|
|
{
|
2021-12-07 15:01:29 +01:00
|
|
|
return QCoreApplication::translate( "TrackingKUserFeedbackJob", "Configuring KDE user feedback." );
|
2020-05-25 15:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::JobResult
|
|
|
|
TrackingKUserFeedbackJob::exec()
|
|
|
|
{
|
|
|
|
// This is the contents of a config file to turn on some kind
|
|
|
|
// of KUserFeedback tracking; the level (16) is chosen for minimal
|
|
|
|
// but not zero tracking.
|
|
|
|
static const char config[] = R"x([Global]
|
|
|
|
FeedbackLevel=16
|
|
|
|
)x";
|
|
|
|
|
2020-06-17 15:11:11 +02:00
|
|
|
for ( const QString& area : m_areas )
|
2020-05-25 15:56:32 +02:00
|
|
|
{
|
2020-06-17 15:11:11 +02:00
|
|
|
QString path = QStringLiteral( "/home/%1/.config/%2" ).arg( m_username, area );
|
2020-05-25 15:56:32 +02:00
|
|
|
cDebug() << "Configuring KUserFeedback" << path;
|
|
|
|
|
|
|
|
int r = CalamaresUtils::System::instance()->createTargetFile( path, config );
|
|
|
|
if ( r > 0 )
|
|
|
|
{
|
|
|
|
return Calamares::JobResult::error(
|
2021-12-07 15:01:29 +01:00
|
|
|
QCoreApplication::translate( "TrackingKUserFeedbackJob", "Error in KDE user feedback configuration." ),
|
|
|
|
QCoreApplication::translate( "TrackingKUserFeedbackJob",
|
|
|
|
"Could not configure KDE user feedback correctly, script error %1." )
|
|
|
|
.arg( r ) );
|
2020-05-25 15:56:32 +02:00
|
|
|
}
|
|
|
|
else if ( r < 0 )
|
|
|
|
{
|
|
|
|
return Calamares::JobResult::error(
|
2021-12-07 15:01:29 +01:00
|
|
|
QCoreApplication::translate( "TrackingKUserFeedbackJob", "Error in KDE user feedback configuration." ),
|
|
|
|
QCoreApplication::translate( "TrackingKUserFeedbackJob",
|
|
|
|
"Could not configure KDE user feedback correctly, Calamares error %1." )
|
|
|
|
.arg( r ) );
|
2020-05-25 15:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Calamares::JobResult::ok();
|
|
|
|
}
|
2020-06-23 13:37:56 +02:00
|
|
|
|
2021-12-07 15:01:29 +01:00
|
|
|
} // namespace
|
2021-09-08 13:30:32 +02:00
|
|
|
|
2020-06-23 13:37:56 +02:00
|
|
|
void
|
|
|
|
addJob( Calamares::JobList& list, InstallTrackingConfig* config )
|
|
|
|
{
|
|
|
|
if ( config->isEnabled() )
|
|
|
|
{
|
|
|
|
const auto* s = CalamaresUtils::System::instance();
|
|
|
|
QHash< QString, QString > map { std::initializer_list< std::pair< QString, QString > > {
|
|
|
|
{ QStringLiteral( "CPU" ), s->getCpuDescription() },
|
|
|
|
{ QStringLiteral( "MEMORY" ), QString::number( s->getTotalMemoryB().first ) },
|
|
|
|
{ QStringLiteral( "DISK" ), QString::number( s->getTotalDiskB() ) } } };
|
|
|
|
QString installUrl = KMacroExpander::expandMacros( config->installTrackingUrl(), map );
|
|
|
|
|
|
|
|
cDebug() << Logger::SubEntry << "install-tracking URL" << installUrl;
|
|
|
|
|
|
|
|
list.append( Calamares::job_ptr( new TrackingInstallJob( installUrl ) ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
addJob( Calamares::JobList& list, MachineTrackingConfig* config )
|
|
|
|
{
|
|
|
|
if ( config->isEnabled() )
|
|
|
|
{
|
|
|
|
const auto style = config->machineTrackingStyle();
|
|
|
|
if ( style == "updatemanager" )
|
|
|
|
{
|
|
|
|
list.append( Calamares::job_ptr( new TrackingMachineUpdateManagerJob() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "Unsupported machine tracking style" << style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
addJob( Calamares::JobList& list, UserTrackingConfig* config )
|
|
|
|
{
|
|
|
|
if ( config->isEnabled() )
|
|
|
|
{
|
|
|
|
const auto* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
static const auto key = QStringLiteral( "username" );
|
|
|
|
QString username = ( gs && gs->contains( key ) ) ? gs->value( key ).toString() : QString();
|
|
|
|
|
|
|
|
if ( username.isEmpty() )
|
|
|
|
{
|
|
|
|
cWarning() << "No username is set in GlobalStorage, skipping user-tracking.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto style = config->userTrackingStyle();
|
|
|
|
if ( style == "kuserfeedback" )
|
|
|
|
{
|
|
|
|
list.append( Calamares::job_ptr( new TrackingKUserFeedbackJob( username, config->userTrackingAreas() ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "Unsupported user tracking style" << style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|