RsyncDeployStep: Use FileTransfer for rsync

Instead of constructing custom ssh command.
Create the mkdir process on stack.

Change-Id: I3944ec4e2979b820a40971a8836e36084a44902a
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Jarek Kobus
2022-05-12 17:32:41 +02:00
parent 80ae9c357d
commit 515845b815

View File

@@ -26,6 +26,7 @@
#include "rsyncdeploystep.h" #include "rsyncdeploystep.h"
#include "abstractremotelinuxdeployservice.h" #include "abstractremotelinuxdeployservice.h"
#include "filetransfer.h"
#include "remotelinux_constants.h" #include "remotelinux_constants.h"
#include <projectexplorer/deploymentdata.h> #include <projectexplorer/deploymentdata.h>
@@ -35,6 +36,7 @@
#include <ssh/sshconnection.h> #include <ssh/sshconnection.h>
#include <ssh/sshsettings.h> #include <ssh/sshsettings.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/processinterface.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
using namespace ProjectExplorer; using namespace ProjectExplorer;
@@ -49,9 +51,34 @@ class RsyncDeployService : public AbstractRemoteLinuxDeployService
Q_OBJECT Q_OBJECT
public: public:
RsyncDeployService(QObject *parent = nullptr) : AbstractRemoteLinuxDeployService(parent) RsyncDeployService(QObject *parent = nullptr) : AbstractRemoteLinuxDeployService(parent)
{ SshConnectionParameters::setupSshEnvironment(&m_rsync); } {
connect(&m_mkdir, &QtcProcess::done, this, [this] {
if (m_mkdir.result() != ProcessResult::FinishedWithSuccess) {
emit errorMessage(tr("Failed to create remote directories: %1").arg(m_mkdir.stdErr()));
setFinished();
return;
}
deployFiles();
});
connect(&m_fileTransfer, &FileTransfer::progress,
this, &AbstractRemoteLinuxDeployService::stdOutData);
connect(&m_fileTransfer, &FileTransfer::done, this, [this](const ProcessResultData &result) {
auto notifyError = [this](const QString &message) {
emit errorMessage(message);
setFinished();
};
if (result.m_error == QProcess::FailedToStart)
notifyError(tr("rsync failed to start: %1").arg(result.m_errorString));
else if (result.m_exitStatus == QProcess::CrashExit)
notifyError(tr("rsync crashed."));
else if (result.m_exitCode != 0)
notifyError(tr("rsync failed with exit code %1.").arg(result.m_exitCode));
else
setFinished();
});
}
void setDeployableFiles(const QList<DeployableFile> &files) { m_deployableFiles = files; } void setDeployableFiles(const QList<DeployableFile> &files);
void setIgnoreMissingFiles(bool ignore) { m_ignoreMissingFiles = ignore; } void setIgnoreMissingFiles(bool ignore) { m_ignoreMissingFiles = ignore; }
void setFlags(const QString &flags) { m_flags = flags; } void setFlags(const QString &flags) { m_flags = flags; }
@@ -61,23 +88,28 @@ private:
void doDeploy() override; void doDeploy() override;
void stopDeployment() override { setFinished(); }; void stopDeployment() override { setFinished(); };
void filterDeployableFiles() const; void filterFiles() const;
void createRemoteDirectories(); void createRemoteDirectories();
void deployFiles(); void deployFiles();
void deployNextFile();
void setFinished(); void setFinished();
mutable QList<DeployableFile> m_deployableFiles; mutable FilesToTransfer m_files;
bool m_ignoreMissingFiles = false; bool m_ignoreMissingFiles = false;
QString m_flags; QString m_flags;
QtcProcess m_rsync; QtcProcess m_mkdir;
std::unique_ptr<QtcProcess> m_mkdir; FileTransfer m_fileTransfer;
}; };
void RsyncDeployService::setDeployableFiles(const QList<DeployableFile> &files)
{
for (const DeployableFile &f : files)
m_files.append({f.localFilePath(), deviceConfiguration()->filePath(f.remoteFilePath())});
}
bool RsyncDeployService::isDeploymentNecessary() const bool RsyncDeployService::isDeploymentNecessary() const
{ {
filterDeployableFiles(); filterFiles();
return !m_deployableFiles.empty(); return !m_files.empty();
} }
void RsyncDeployService::doDeploy() void RsyncDeployService::doDeploy()
@@ -85,100 +117,46 @@ void RsyncDeployService::doDeploy()
createRemoteDirectories(); createRemoteDirectories();
} }
void RsyncDeployService::filterDeployableFiles() const void RsyncDeployService::filterFiles() const
{ {
if (m_ignoreMissingFiles) { if (!m_ignoreMissingFiles)
Utils::erase(m_deployableFiles, [](const DeployableFile &f) { return;
return !f.localFilePath().exists();
}); Utils::erase(m_files, [](const FileToTransfer &file) { return !file.m_source.exists(); });
}
} }
void RsyncDeployService::createRemoteDirectories() void RsyncDeployService::createRemoteDirectories()
{ {
QStringList remoteDirs; QStringList remoteDirs;
for (const DeployableFile &f : qAsConst(m_deployableFiles)) for (const FileToTransfer &file : qAsConst(m_files))
remoteDirs << f.remoteDirectory(); remoteDirs << file.m_target.parentDir().path();
remoteDirs.sort(); remoteDirs.sort();
remoteDirs.removeDuplicates(); remoteDirs.removeDuplicates();
m_mkdir.reset(new QtcProcess); m_mkdir.setCommand({deviceConfiguration()->filePath("mkdir"),
m_mkdir->setCommand({deviceConfiguration()->filePath("mkdir"),
{"-p", ProcessArgs::createUnixArgs(remoteDirs).toString()}}); {"-p", ProcessArgs::createUnixArgs(remoteDirs).toString()}});
connect(m_mkdir.get(), &QtcProcess::done, this, [this] { m_mkdir.start();
if (m_mkdir->result() != ProcessResult::FinishedWithSuccess) {
emit errorMessage(tr("Failed to create remote directories: %1")
.arg(QString::fromUtf8(m_mkdir->readAllStandardError())));
setFinished();
return;
}
deployFiles();
m_mkdir.release()->deleteLater();
});
m_mkdir->start();
} }
void RsyncDeployService::deployFiles() void RsyncDeployService::deployFiles()
{ {
connect(&m_rsync, &QtcProcess::readyReadStandardOutput, this, [this] { m_fileTransfer.setDevice(deviceConfiguration());
emit progressMessage(QString::fromLocal8Bit(m_rsync.readAllStandardOutput())); m_fileTransfer.setTransferMethod(FileTransferMethod::Rsync);
}); m_fileTransfer.setRsyncFlags(m_flags);
connect(&m_rsync, &QtcProcess::readyReadStandardError, this, [this] { m_fileTransfer.setFilesToTransfer(m_files);
emit warningMessage(QString::fromLocal8Bit(m_rsync.readAllStandardError())); m_fileTransfer.start();
});
connect(&m_rsync, &QtcProcess::done, this, [this] {
auto notifyError = [this](const QString &message) {
emit errorMessage(message);
setFinished();
};
if (m_rsync.error() == QProcess::FailedToStart)
notifyError(tr("rsync failed to start: %1").arg(m_rsync.errorString()));
else if (m_rsync.exitStatus() == QProcess::CrashExit)
notifyError(tr("rsync crashed."));
else if (m_rsync.exitCode() != 0)
notifyError(tr("rsync failed with exit code %1.").arg(m_rsync.exitCode()));
else
deployNextFile();
});
deployNextFile();
}
void RsyncDeployService::deployNextFile()
{
if (m_deployableFiles.empty()) {
setFinished();
return;
}
const DeployableFile file = m_deployableFiles.takeFirst();
const RsyncCommandLine cmdLine = RsyncDeployStep::rsyncCommand(*connection(), m_flags);
QString localFilePath = file.localFilePath().toString();
// On Windows, rsync is either from msys or cygwin. Neither work with the other's ssh.exe.
if (HostOsInfo::isWindowsHost()) {
localFilePath = '/' + localFilePath.at(0) + localFilePath.mid(2);
if (anyOf(cmdLine.options, [](const QString &opt) {
return opt.contains("cygwin", Qt::CaseInsensitive); })) {
localFilePath.prepend("/cygdrive");
}
}
// TODO: consider adding "--progress" option for reporting the progress.
const QStringList args = QStringList(cmdLine.options)
<< (localFilePath + (file.localFilePath().isDir() ? "/" : QString()))
<< (cmdLine.remoteHostSpec + ':' + file.remoteFilePath());
m_rsync.setCommand(CommandLine("rsync", args));
m_rsync.start(); // TODO: Get rsync location from settings?
} }
void RsyncDeployService::setFinished() void RsyncDeployService::setFinished()
{ {
if (m_mkdir) m_mkdir.close();
m_mkdir.release()->deleteLater(); m_fileTransfer.stop();
m_rsync.close();
handleDeploymentDone(); handleDeploymentDone();
} }
} // namespace Internal } // namespace Internal
static const char s_defaultFlags[] = "-av";
RsyncDeployStep::RsyncDeployStep(BuildStepList *bsl, Utils::Id id) RsyncDeployStep::RsyncDeployStep(BuildStepList *bsl, Utils::Id id)
: AbstractRemoteLinuxDeployStep(bsl, id) : AbstractRemoteLinuxDeployStep(bsl, id)
{ {
@@ -188,7 +166,7 @@ RsyncDeployStep::RsyncDeployStep(BuildStepList *bsl, Utils::Id id)
flags->setDisplayStyle(StringAspect::LineEditDisplay); flags->setDisplayStyle(StringAspect::LineEditDisplay);
flags->setSettingsKey("RemoteLinux.RsyncDeployStep.Flags"); flags->setSettingsKey("RemoteLinux.RsyncDeployStep.Flags");
flags->setLabelText(tr("Flags:")); flags->setLabelText(tr("Flags:"));
flags->setValue(defaultFlags()); flags->setValue(s_defaultFlags);
auto ignoreMissingFiles = addAspect<BoolAspect>(); auto ignoreMissingFiles = addAspect<BoolAspect>();
ignoreMissingFiles->setSettingsKey("RemoteLinux.RsyncDeployStep.IgnoreMissingFiles"); ignoreMissingFiles->setSettingsKey("RemoteLinux.RsyncDeployStep.IgnoreMissingFiles");