PublicKeyDeploymentDialog: Don't use SshRemoteProcessRunner

Use QtcProcess with a path on device instead.
Get rid of SshKeyDeployer, inline its functionality inside
PublicKeyDeploymentDialog.

Change-Id: I01b9fecb06b459f93e8eefaf889ef0dea9f69f1d
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2022-05-03 23:12:48 +02:00
parent 2095dbdd17
commit aa4695e76f
6 changed files with 45 additions and 223 deletions

View File

@@ -39,7 +39,6 @@ add_qtc_plugin(RemoteLinux
remotelinuxsignaloperation.cpp remotelinuxsignaloperation.h
remotelinuxx11forwardingaspect.cpp remotelinuxx11forwardingaspect.h
rsyncdeploystep.cpp rsyncdeploystep.h
sshkeydeployer.cpp sshkeydeployer.h
sshprocessinterface.h
tarpackagecreationstep.cpp tarpackagecreationstep.h
uploadandinstalltarpackagestep.cpp uploadandinstalltarpackagestep.h

View File

@@ -25,11 +25,9 @@
#include "publickeydeploymentdialog.h"
#include "sshkeydeployer.h"
#include <coreplugin/icore.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <ssh/sshconnection.h>
#include <utils/qtcprocess.h>
#include <utils/theme/theme.h>
using namespace ProjectExplorer;
@@ -37,11 +35,12 @@ using namespace Utils;
namespace RemoteLinux {
namespace Internal {
class PublicKeyDeploymentDialogPrivate
{
public:
SshKeyDeployer keyDeployer;
bool done;
QtcProcess m_process;
bool m_done;
};
} // namespace Internal;
@@ -68,16 +67,38 @@ PublicKeyDeploymentDialog::PublicKeyDeploymentDialog(const IDevice::ConstPtr &de
setMinimumDuration(0);
setMaximum(1);
d->done = false;
d->m_done = false;
setLabelText(tr("Deploying..."));
setValue(0);
connect(this, &PublicKeyDeploymentDialog::canceled,
this, &PublicKeyDeploymentDialog::handleCanceled);
connect(&d->keyDeployer, &SshKeyDeployer::error,
this, &PublicKeyDeploymentDialog::handleDeploymentError);
connect(&d->keyDeployer, &SshKeyDeployer::finishedSuccessfully,
this, &PublicKeyDeploymentDialog::handleDeploymentSuccess);
d->keyDeployer.deployPublicKey(deviceConfig->sshParameters(), publicKeyFileName);
connect(this, &PublicKeyDeploymentDialog::canceled, this,
[this] { d->m_done ? accept() : reject(); });
connect(&d->m_process, &QtcProcess::done, this, [this] {
const bool succeeded = d->m_process.error() == QProcess::UnknownError;
QString finalMessage;
if (!succeeded) {
QString errorMessage = d->m_process.errorString();
if (errorMessage.isEmpty())
errorMessage = d->m_process.stdErr();
if (errorMessage.endsWith('\n'))
errorMessage.chop(1);
finalMessage = tr("Key deployment failed.");
if (!errorMessage.isEmpty())
finalMessage += '\n' + errorMessage;
}
handleDeploymentDone(succeeded, finalMessage);
});
FileReader reader;
if (!reader.fetch(publicKeyFileName)) {
handleDeploymentDone(false, tr("Public key error: %1").arg(reader.errorString()));
return;
}
const QString command = "test -d .ssh || mkdir -p ~/.ssh && chmod 0700 .ssh && echo '"
+ QString::fromLocal8Bit(reader.data())
+ "' >> .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys";
d->m_process.setCommand({deviceConfig->mapToGlobalPath("/bin/sh"), {"-c", command}});
d->m_process.start();
}
PublicKeyDeploymentDialog::~PublicKeyDeploymentDialog()
@@ -85,43 +106,20 @@ PublicKeyDeploymentDialog::~PublicKeyDeploymentDialog()
delete d;
}
void PublicKeyDeploymentDialog::handleDeploymentSuccess()
void PublicKeyDeploymentDialog::handleDeploymentDone(bool succeeded, const QString &errorMessage)
{
handleDeploymentFinished(QString());
setValue(1);
d->done = true;
}
void PublicKeyDeploymentDialog::handleDeploymentError(const QString &errorMsg)
{
handleDeploymentFinished(errorMsg);
}
void PublicKeyDeploymentDialog::handleDeploymentFinished(const QString &errorMsg)
{
QString buttonText;
QString textColor;
if (errorMsg.isEmpty()) {
buttonText = tr("Deployment finished successfully.");
textColor = Utils::creatorTheme()->color(Utils::Theme::TextColorNormal).name();
} else {
buttonText = errorMsg;
textColor = Utils::creatorTheme()->color(Utils::Theme::TextColorError).name();
}
QString buttonText = succeeded ? tr("Deployment finished successfully.") : errorMessage;
const QString textColor = creatorTheme()->color(
succeeded ? Theme::TextColorNormal : Theme::TextColorError).name();
setLabelText(QString::fromLatin1("<font color=\"%1\">%2</font>")
.arg(textColor)
.arg(buttonText.replace("\n", "<br/>")));
.arg(textColor, buttonText.replace("\n", "<br/>")));
setCancelButtonText(tr("Close"));
}
void PublicKeyDeploymentDialog::handleCanceled()
{
disconnect(&d->keyDeployer, nullptr, this, nullptr);
d->keyDeployer.stopDeployment();
if (d->done)
accept();
else
reject();
if (!succeeded)
return;
setValue(1);
d->m_done = true;
}
} // namespace RemoteLinux

View File

@@ -50,10 +50,7 @@ public:
~PublicKeyDeploymentDialog() override;
private:
void handleDeploymentFinished(const QString &errorMsg);
void handleDeploymentError(const QString &errorMsg);
void handleDeploymentSuccess();
void handleCanceled();
void handleDeploymentDone(bool succeeded, const QString &errorMessage);
Internal::PublicKeyDeploymentDialogPrivate * const d;
};

View File

@@ -84,8 +84,6 @@ Project {
"remotelinuxx11forwardingaspect.h",
"rsyncdeploystep.cpp",
"rsyncdeploystep.h",
"sshkeydeployer.cpp",
"sshkeydeployer.h",
"sshprocessinterface.h",
"tarpackagecreationstep.cpp",
"tarpackagecreationstep.h",

View File

@@ -1,108 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 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 "sshkeydeployer.h"
#include <ssh/sshremoteprocessrunner.h>
#include <utils/filepath.h>
using namespace QSsh;
using namespace Utils;
namespace RemoteLinux {
namespace Internal {
class SshKeyDeployerPrivate
{
public:
SshRemoteProcessRunner deployProcess;
};
} // namespace Internal
SshKeyDeployer::SshKeyDeployer(QObject *parent)
: QObject(parent), d(new Internal::SshKeyDeployerPrivate)
{
}
SshKeyDeployer::~SshKeyDeployer()
{
cleanup();
delete d;
}
void SshKeyDeployer::deployPublicKey(const SshConnectionParameters &sshParams,
const FilePath &keyFilePath)
{
cleanup();
FileReader reader;
if (!reader.fetch(keyFilePath)) {
emit error(tr("Public key error: %1").arg(reader.errorString()));
return;
}
connect(&d->deployProcess, &SshRemoteProcessRunner::connectionError,
this, &SshKeyDeployer::handleConnectionFailure);
connect(&d->deployProcess, &SshRemoteProcessRunner::finished,
this, &SshKeyDeployer::handleKeyUploadFinished);
const QString command = "test -d .ssh "
"|| mkdir -p ~/.ssh && chmod 0700 .ssh && echo '"
+ QString::fromLocal8Bit(reader.data())
+ "' >> .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys";
d->deployProcess.run(command, sshParams);
}
void SshKeyDeployer::handleConnectionFailure()
{
cleanup();
emit error(tr("Connection failed: %1").arg(d->deployProcess.lastConnectionErrorString()));
}
void SshKeyDeployer::handleKeyUploadFinished()
{
const int exitCode = d->deployProcess.exitCode();
const QString errorMsg = d->deployProcess.errorString();
cleanup();
if (errorMsg.isEmpty() && exitCode == 0) {
emit finishedSuccessfully();
} else {
emit error(tr("Key deployment failed: %1.").arg(errorMsg.isEmpty()
? QString::fromUtf8(d->deployProcess.readAllStandardError())
: errorMsg));
}
}
void SshKeyDeployer::stopDeployment()
{
cleanup();
}
void SshKeyDeployer::cleanup()
{
disconnect(&d->deployProcess, nullptr, this, nullptr);
}
} // namespace RemoteLinux

View File

@@ -1,62 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 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.
**
****************************************************************************/
#pragma once
#include "remotelinux_export.h"
#include <QObject>
namespace QSsh { class SshConnectionParameters; }
namespace Utils { class FilePath; }
namespace RemoteLinux {
namespace Internal { class SshKeyDeployerPrivate; }
class REMOTELINUX_EXPORT SshKeyDeployer : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(SshKeyDeployer)
public:
explicit SshKeyDeployer(QObject *parent = nullptr);
~SshKeyDeployer() override;
void deployPublicKey(const QSsh::SshConnectionParameters &sshParams,
const Utils::FilePath &keyFilePath);
void stopDeployment();
signals:
void error(const QString &errorMsg);
void finishedSuccessfully();
private:
void handleConnectionFailure();
void handleKeyUploadFinished();
void cleanup();
Internal::SshKeyDeployerPrivate * const d;
};
} // namespace RemoteLinux