2018-12-14 15:41:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2018 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
**
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "rsyncdeploystep.h"
|
|
|
|
|
|
|
|
#include "abstractremotelinuxdeployservice.h"
|
|
|
|
|
|
|
|
#include <projectexplorer/deploymentdata.h>
|
|
|
|
#include <projectexplorer/runconfigurationaspects.h>
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
#include <ssh/sshconnection.h>
|
|
|
|
#include <ssh/sshremoteprocess.h>
|
|
|
|
#include <ssh/sshsettings.h>
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
#include <utils/qtcprocess.h>
|
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
using namespace QSsh;
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
namespace RemoteLinux {
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
class RsyncDeployService : public AbstractRemoteLinuxDeployService
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
RsyncDeployService(QObject *parent = nullptr) : AbstractRemoteLinuxDeployService(parent) {}
|
|
|
|
|
|
|
|
void setDeployableFiles(const QList<DeployableFile> &files) { m_deployableFiles = files; }
|
|
|
|
void setIgnoreMissingFiles(bool ignore) { m_ignoreMissingFiles = ignore; }
|
2019-04-23 15:57:54 +02:00
|
|
|
void setFlags(const QString &flags) { m_flags = flags; }
|
2018-12-14 15:41:59 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool isDeploymentNecessary() const override;
|
|
|
|
|
|
|
|
void doDeviceSetup() override { handleDeviceSetupDone(true); }
|
|
|
|
void stopDeviceSetup() override { handleDeviceSetupDone(false); };
|
|
|
|
|
|
|
|
void doDeploy() override;
|
|
|
|
void stopDeployment() override { setFinished(); };
|
|
|
|
|
|
|
|
void filterDeployableFiles() const;
|
|
|
|
void createRemoteDirectories();
|
|
|
|
void deployFiles();
|
|
|
|
void deployNextFile();
|
|
|
|
void setFinished();
|
|
|
|
|
|
|
|
mutable QList<DeployableFile> m_deployableFiles;
|
|
|
|
bool m_ignoreMissingFiles = false;
|
2019-04-23 15:57:54 +02:00
|
|
|
QString m_flags;
|
2018-12-19 17:28:59 +01:00
|
|
|
SshProcess m_rsync;
|
2018-12-14 15:41:59 +01:00
|
|
|
SshRemoteProcessPtr m_mkdir;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool RsyncDeployService::isDeploymentNecessary() const
|
|
|
|
{
|
|
|
|
filterDeployableFiles();
|
|
|
|
return !m_deployableFiles.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsyncDeployService::doDeploy()
|
|
|
|
{
|
|
|
|
createRemoteDirectories();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsyncDeployService::filterDeployableFiles() const
|
|
|
|
{
|
|
|
|
if (m_ignoreMissingFiles) {
|
|
|
|
erase(m_deployableFiles, [](const DeployableFile &f) {
|
|
|
|
return !f.localFilePath().exists();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsyncDeployService::createRemoteDirectories()
|
|
|
|
{
|
|
|
|
QStringList remoteDirs;
|
|
|
|
for (const DeployableFile &f : m_deployableFiles)
|
|
|
|
remoteDirs << f.remoteDirectory();
|
|
|
|
remoteDirs.sort();
|
|
|
|
remoteDirs.removeDuplicates();
|
|
|
|
m_mkdir = connection()->createRemoteProcess("mkdir -p " + QtcProcess::Arguments
|
2019-05-21 16:59:29 +02:00
|
|
|
::createUnixArgs(remoteDirs).toString());
|
2018-12-20 17:53:36 +01:00
|
|
|
connect(m_mkdir.get(), &SshRemoteProcess::done, this, [this](const QString &error) {
|
2018-12-14 15:41:59 +01:00
|
|
|
QString userError;
|
|
|
|
if (!error.isEmpty())
|
|
|
|
userError = error;
|
|
|
|
if (m_mkdir->exitCode() != 0)
|
|
|
|
userError = QString::fromUtf8(m_mkdir->readAllStandardError());
|
|
|
|
if (!userError.isEmpty()) {
|
|
|
|
emit errorMessage(tr("Failed to create remote directories: %1").arg(userError));
|
|
|
|
setFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deployFiles();
|
|
|
|
});
|
|
|
|
m_mkdir->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsyncDeployService::deployFiles()
|
|
|
|
{
|
2018-12-20 17:53:36 +01:00
|
|
|
connect(&m_rsync, &QProcess::readyReadStandardOutput, this, [this] {
|
2018-12-14 15:41:59 +01:00
|
|
|
emit progressMessage(QString::fromLocal8Bit(m_rsync.readAllStandardOutput()));
|
|
|
|
});
|
2018-12-20 17:53:36 +01:00
|
|
|
connect(&m_rsync, &QProcess::readyReadStandardError, this, [this] {
|
2018-12-14 15:41:59 +01:00
|
|
|
emit warningMessage(QString::fromLocal8Bit(m_rsync.readAllStandardError()));
|
|
|
|
});
|
2018-12-20 17:53:36 +01:00
|
|
|
connect(&m_rsync, &QProcess::errorOccurred, this, [this] {
|
2018-12-14 15:41:59 +01:00
|
|
|
if (m_rsync.error() == QProcess::FailedToStart) {
|
|
|
|
emit errorMessage(tr("rsync failed to start: %1").arg(m_rsync.errorString()));
|
|
|
|
setFinished();
|
|
|
|
}
|
|
|
|
});
|
2019-02-26 09:40:49 +01:00
|
|
|
connect(&m_rsync, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this] {
|
2018-12-14 15:41:59 +01:00
|
|
|
if (m_rsync.exitStatus() == QProcess::CrashExit) {
|
|
|
|
emit errorMessage(tr("rsync crashed."));
|
|
|
|
setFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_rsync.exitCode() != 0) {
|
|
|
|
emit errorMessage(tr("rsync failed with exit code %1.").arg(m_rsync.exitCode()));
|
|
|
|
setFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
deployNextFile();
|
|
|
|
});
|
|
|
|
deployNextFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsyncDeployService::deployNextFile()
|
|
|
|
{
|
|
|
|
if (m_deployableFiles.empty()) {
|
|
|
|
setFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const DeployableFile file = m_deployableFiles.takeFirst();
|
2019-04-23 15:57:54 +02:00
|
|
|
const RsyncCommandLine cmdLine = RsyncDeployStep::rsyncCommand(*connection(), m_flags);
|
2018-12-14 15:41:59 +01:00
|
|
|
const QStringList args = QStringList(cmdLine.options)
|
2019-12-02 17:55:22 +01:00
|
|
|
<< (file.localFilePath().toString() + (file.localFilePath().isDir() ? "/" : QString()))
|
2018-12-14 15:41:59 +01:00
|
|
|
<< (cmdLine.remoteHostSpec + ':' + file.remoteFilePath());
|
|
|
|
m_rsync.start("rsync", args); // TODO: Get rsync location from settings?
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsyncDeployService::setFinished()
|
|
|
|
{
|
|
|
|
if (m_mkdir) {
|
|
|
|
m_mkdir->disconnect();
|
|
|
|
m_mkdir->kill();
|
|
|
|
}
|
|
|
|
m_rsync.disconnect();
|
|
|
|
m_rsync.kill();
|
|
|
|
handleDeploymentDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
RsyncDeployStep::RsyncDeployStep(BuildStepList *bsl, Utils::Id id)
|
2019-12-20 17:05:30 +01:00
|
|
|
: AbstractRemoteLinuxDeployStep(bsl, id)
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
2019-06-13 17:03:56 +02:00
|
|
|
auto service = createDeployService<Internal::RsyncDeployService>();
|
|
|
|
|
2020-08-13 09:16:00 +02:00
|
|
|
auto flags = addAspect<StringAspect>();
|
|
|
|
flags->setDisplayStyle(StringAspect::LineEditDisplay);
|
2019-06-07 16:43:06 +02:00
|
|
|
flags->setSettingsKey("RemoteLinux.RsyncDeployStep.Flags");
|
|
|
|
flags->setLabelText(tr("Flags:"));
|
|
|
|
flags->setValue(defaultFlags());
|
|
|
|
|
2020-08-13 09:16:00 +02:00
|
|
|
auto ignoreMissingFiles = addAspect<BoolAspect>();
|
2019-06-07 16:43:06 +02:00
|
|
|
ignoreMissingFiles->setSettingsKey("RemoteLinux.RsyncDeployStep.IgnoreMissingFiles");
|
2019-11-26 17:11:01 +01:00
|
|
|
ignoreMissingFiles->setLabel(tr("Ignore missing files:"),
|
2020-08-13 09:16:00 +02:00
|
|
|
BoolAspect::LabelPlacement::InExtraLabel);
|
2019-06-07 16:43:06 +02:00
|
|
|
ignoreMissingFiles->setValue(false);
|
2018-12-14 15:41:59 +01:00
|
|
|
|
|
|
|
setDefaultDisplayName(displayName());
|
2019-06-07 16:43:06 +02:00
|
|
|
|
2019-06-13 17:03:56 +02:00
|
|
|
setInternalInitializer([service, flags, ignoreMissingFiles] {
|
|
|
|
service->setIgnoreMissingFiles(ignoreMissingFiles->value());
|
|
|
|
service->setFlags(flags->value());
|
|
|
|
return service->isDeploymentPossible();
|
2019-06-07 16:43:06 +02:00
|
|
|
});
|
2018-12-14 15:41:59 +01:00
|
|
|
|
2019-06-13 17:03:56 +02:00
|
|
|
setRunPreparer([this, service] {
|
|
|
|
service->setDeployableFiles(target()->deploymentData().allFiles());
|
|
|
|
});
|
2018-12-14 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:03:56 +02:00
|
|
|
RsyncDeployStep::~RsyncDeployStep() = default;
|
2019-04-12 14:49:59 +02:00
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
Utils::Id RsyncDeployStep::stepId()
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
|
|
|
return "RemoteLinux.RsyncDeployStep";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RsyncDeployStep::displayName()
|
|
|
|
{
|
|
|
|
return tr("Deploy files via rsync");
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:57:54 +02:00
|
|
|
QString RsyncDeployStep::defaultFlags()
|
|
|
|
{
|
|
|
|
return QString("-av");
|
|
|
|
}
|
|
|
|
|
|
|
|
RsyncCommandLine RsyncDeployStep::rsyncCommand(const SshConnection &sshConnection,
|
|
|
|
const QString &flags)
|
2018-12-14 15:41:59 +01:00
|
|
|
{
|
|
|
|
const QString sshCmdLine = QtcProcess::joinArgs(
|
|
|
|
QStringList{SshSettings::sshFilePath().toUserOutput()}
|
2019-10-01 11:07:06 +02:00
|
|
|
<< sshConnection.connectionOptions(SshSettings::sshFilePath()));
|
2018-12-14 15:41:59 +01:00
|
|
|
const SshConnectionParameters sshParams = sshConnection.connectionParameters();
|
2019-04-23 15:57:54 +02:00
|
|
|
return RsyncCommandLine(QStringList{"-e", sshCmdLine, flags},
|
2018-12-14 15:41:59 +01:00
|
|
|
sshParams.userName() + '@' + sshParams.host());
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace RemoteLinux
|
|
|
|
|
|
|
|
#include <rsyncdeploystep.moc>
|