2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2018 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2018-12-14 15:41:59 +01:00
|
|
|
|
|
|
|
#include "rsyncdeploystep.h"
|
|
|
|
|
2022-07-01 16:42:37 +02:00
|
|
|
#include "abstractremotelinuxdeploystep.h"
|
2020-09-16 12:08:11 +02:00
|
|
|
#include "remotelinux_constants.h"
|
2022-07-15 12:20:23 +02:00
|
|
|
#include "remotelinuxtr.h"
|
2018-12-14 15:41:59 +01:00
|
|
|
|
2023-06-20 15:01:02 +02:00
|
|
|
#include <projectexplorer/buildsystem.h>
|
2018-12-14 15:41:59 +01:00
|
|
|
#include <projectexplorer/deploymentdata.h>
|
2022-05-20 17:53:20 +02:00
|
|
|
#include <projectexplorer/devicesupport/filetransfer.h>
|
2022-05-03 00:38:11 +02:00
|
|
|
#include <projectexplorer/devicesupport/idevice.h>
|
2022-10-13 14:14:51 +02:00
|
|
|
#include <projectexplorer/kitinformation.h>
|
2022-08-11 13:50:36 +02:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2018-12-14 15:41:59 +01:00
|
|
|
#include <projectexplorer/runconfigurationaspects.h>
|
|
|
|
#include <projectexplorer/target.h>
|
2022-07-15 12:20:23 +02:00
|
|
|
|
2018-12-14 15:41:59 +01:00
|
|
|
#include <utils/algorithm.h>
|
2023-06-20 15:01:02 +02:00
|
|
|
#include <utils/async.h>
|
2023-05-03 17:05:35 +02:00
|
|
|
#include <utils/process.h>
|
2022-05-12 17:32:41 +02:00
|
|
|
#include <utils/processinterface.h>
|
2018-12-14 15:41:59 +01:00
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
2023-05-10 19:54:52 +02:00
|
|
|
using namespace Tasking;
|
2018-12-14 15:41:59 +01:00
|
|
|
using namespace Utils;
|
|
|
|
|
2022-10-12 14:37:14 +03:00
|
|
|
namespace RemoteLinux {
|
2018-12-14 15:41:59 +01:00
|
|
|
|
2023-03-21 16:07:20 +01:00
|
|
|
// RsyncDeployStep
|
|
|
|
|
2023-03-29 08:09:49 +02:00
|
|
|
class RsyncDeployStep : public AbstractRemoteLinuxDeployStep
|
|
|
|
{
|
|
|
|
public:
|
2023-04-06 12:11:41 +02:00
|
|
|
RsyncDeployStep(BuildStepList *bsl, Id id);
|
2023-03-29 08:09:49 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool isDeploymentNecessary() const final;
|
2023-05-10 19:54:52 +02:00
|
|
|
Group deployRecipe() final;
|
2023-05-29 20:16:19 +02:00
|
|
|
GroupItem mkdirTask();
|
|
|
|
GroupItem transferTask();
|
2023-03-29 08:09:49 +02:00
|
|
|
|
2023-04-06 12:11:41 +02:00
|
|
|
mutable FilesToTransfer m_files;
|
2023-03-29 08:09:49 +02:00
|
|
|
bool m_ignoreMissingFiles = false;
|
|
|
|
QString m_flags;
|
|
|
|
};
|
|
|
|
|
2023-03-21 16:07:20 +01:00
|
|
|
RsyncDeployStep::RsyncDeployStep(BuildStepList *bsl, Id id)
|
|
|
|
: AbstractRemoteLinuxDeployStep(bsl, id)
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
2023-03-21 16:07:20 +01:00
|
|
|
auto flags = addAspect<StringAspect>();
|
|
|
|
flags->setDisplayStyle(StringAspect::LineEditDisplay);
|
|
|
|
flags->setSettingsKey("RemoteLinux.RsyncDeployStep.Flags");
|
|
|
|
flags->setLabelText(Tr::tr("Flags:"));
|
|
|
|
flags->setValue(FileTransferSetupData::defaultRsyncFlags());
|
|
|
|
|
|
|
|
auto ignoreMissingFiles = addAspect<BoolAspect>();
|
|
|
|
ignoreMissingFiles->setSettingsKey("RemoteLinux.RsyncDeployStep.IgnoreMissingFiles");
|
|
|
|
ignoreMissingFiles->setLabel(Tr::tr("Ignore missing files:"),
|
|
|
|
BoolAspect::LabelPlacement::InExtraLabel);
|
|
|
|
ignoreMissingFiles->setValue(false);
|
|
|
|
|
|
|
|
setInternalInitializer([this, ignoreMissingFiles, flags] {
|
|
|
|
if (BuildDeviceKitAspect::device(kit()) == DeviceKitAspect::device(kit())) {
|
|
|
|
// rsync transfer on the same device currently not implemented
|
|
|
|
// and typically not wanted.
|
|
|
|
return CheckResult::failure(
|
|
|
|
Tr::tr("rsync is only supported for transfers between different devices."));
|
|
|
|
}
|
2023-03-22 10:25:25 +01:00
|
|
|
m_ignoreMissingFiles = ignoreMissingFiles->value();
|
|
|
|
m_flags = flags->value();
|
2023-03-21 16:07:20 +01:00
|
|
|
return isDeploymentPossible();
|
|
|
|
});
|
|
|
|
|
|
|
|
setRunPreparer([this] {
|
2023-03-22 10:25:25 +01:00
|
|
|
const QList<DeployableFile> files = target()->deploymentData().allFiles();
|
|
|
|
m_files.clear();
|
|
|
|
for (const DeployableFile &f : files)
|
|
|
|
m_files.append({f.localFilePath(), deviceConfiguration()->filePath(f.remoteFilePath())});
|
2023-03-21 16:07:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsyncDeployStep::isDeploymentNecessary() const
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
2022-07-01 16:42:37 +02:00
|
|
|
if (m_ignoreMissingFiles)
|
|
|
|
Utils::erase(m_files, [](const FileToTransfer &file) { return !file.m_source.exists(); });
|
2022-05-12 17:32:41 +02:00
|
|
|
return !m_files.empty();
|
2018-12-14 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 20:16:19 +02:00
|
|
|
GroupItem RsyncDeployStep::mkdirTask()
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
2023-06-20 15:01:02 +02:00
|
|
|
using ResultType = expected_str<void>;
|
|
|
|
|
|
|
|
const auto onSetup = [files = m_files](Async<ResultType> &async) {
|
|
|
|
FilePaths remoteDirs;
|
|
|
|
for (const FileToTransfer &file : std::as_const(files))
|
|
|
|
remoteDirs << file.m_target.parentDir();
|
|
|
|
|
|
|
|
FilePath::sort(remoteDirs);
|
|
|
|
FilePath::removeDuplicates(remoteDirs);
|
|
|
|
|
|
|
|
async.setConcurrentCallData([remoteDirs](QPromise<ResultType> &promise) {
|
|
|
|
for (auto dir : remoteDirs) {
|
|
|
|
const expected_str<void> result = dir.ensureWritableDir();
|
|
|
|
promise.addResult(result);
|
|
|
|
if (!result)
|
|
|
|
promise.future().cancel();
|
|
|
|
}
|
2022-11-22 12:29:02 +01:00
|
|
|
});
|
|
|
|
};
|
2023-06-20 15:01:02 +02:00
|
|
|
|
|
|
|
const auto onError = [this](const Async<ResultType> &async) {
|
|
|
|
const int numResults = async.future().resultCount();
|
|
|
|
if (numResults == 0) {
|
|
|
|
addErrorMessage(
|
|
|
|
Tr::tr("Unknown error occurred while trying to create remote directories") + '\n');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numResults; ++i) {
|
|
|
|
const auto result = async.future().resultAt(i);
|
|
|
|
if (!result.has_value())
|
|
|
|
addErrorMessage(result.error());
|
2022-11-22 12:29:02 +01:00
|
|
|
}
|
|
|
|
};
|
2023-06-20 15:01:02 +02:00
|
|
|
|
|
|
|
return AsyncTask<ResultType>(onSetup, {}, onError);
|
2018-12-14 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 20:16:19 +02:00
|
|
|
GroupItem RsyncDeployStep::transferTask()
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
2022-11-24 20:55:54 +01:00
|
|
|
const auto setupHandler = [this](FileTransfer &transfer) {
|
2022-11-22 12:29:02 +01:00
|
|
|
transfer.setTransferMethod(FileTransferMethod::Rsync);
|
|
|
|
transfer.setRsyncFlags(m_flags);
|
|
|
|
transfer.setFilesToTransfer(m_files);
|
|
|
|
connect(&transfer, &FileTransfer::progress,
|
2023-03-22 09:34:34 +01:00
|
|
|
this, &AbstractRemoteLinuxDeployStep::handleStdOutData);
|
2022-11-22 12:29:02 +01:00
|
|
|
};
|
2022-11-24 20:55:54 +01:00
|
|
|
const auto errorHandler = [this](const FileTransfer &transfer) {
|
2022-11-22 12:29:02 +01:00
|
|
|
const ProcessResultData result = transfer.resultData();
|
2023-01-24 18:11:19 +01:00
|
|
|
if (result.m_error == QProcess::FailedToStart) {
|
2023-03-22 09:34:34 +01:00
|
|
|
addErrorMessage(Tr::tr("rsync failed to start: %1").arg(result.m_errorString));
|
2023-01-24 18:11:19 +01:00
|
|
|
} else if (result.m_exitStatus == QProcess::CrashExit) {
|
2023-03-22 09:34:34 +01:00
|
|
|
addErrorMessage(Tr::tr("rsync crashed."));
|
2023-01-24 18:11:19 +01:00
|
|
|
} else if (result.m_exitCode != 0) {
|
2023-03-22 09:34:34 +01:00
|
|
|
addErrorMessage(Tr::tr("rsync failed with exit code %1.").arg(result.m_exitCode)
|
|
|
|
+ "\n" + result.m_errorString);
|
2023-01-24 18:11:19 +01:00
|
|
|
}
|
2022-11-22 12:29:02 +01:00
|
|
|
};
|
2023-05-09 23:06:18 +02:00
|
|
|
return FileTransferTask(setupHandler, {}, errorHandler);
|
2018-12-14 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 16:07:20 +01:00
|
|
|
Group RsyncDeployStep::deployRecipe()
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
2022-11-24 21:45:00 +01:00
|
|
|
return Group { mkdirTask(), transferTask() };
|
2018-12-14 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 17:45:44 +01:00
|
|
|
// Factory
|
|
|
|
|
|
|
|
RsyncDeployStepFactory::RsyncDeployStepFactory()
|
|
|
|
{
|
|
|
|
registerStep<RsyncDeployStep>(Constants::RsyncDeployStepId);
|
|
|
|
setDisplayName(Tr::tr("Deploy files via rsync"));
|
|
|
|
}
|
|
|
|
|
2022-10-12 14:37:14 +03:00
|
|
|
} // RemoteLinux
|