diff --git a/src/plugins/remotelinux/CMakeLists.txt b/src/plugins/remotelinux/CMakeLists.txt index 31fbd6a672a..2c3807af868 100644 --- a/src/plugins/remotelinux/CMakeLists.txt +++ b/src/plugins/remotelinux/CMakeLists.txt @@ -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 diff --git a/src/plugins/remotelinux/publickeydeploymentdialog.cpp b/src/plugins/remotelinux/publickeydeploymentdialog.cpp index a5dc7aafdc6..30cf82f7b1d 100644 --- a/src/plugins/remotelinux/publickeydeploymentdialog.cpp +++ b/src/plugins/remotelinux/publickeydeploymentdialog.cpp @@ -25,11 +25,9 @@ #include "publickeydeploymentdialog.h" -#include "sshkeydeployer.h" - -#include #include #include +#include #include 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("%2") - .arg(textColor) - .arg(buttonText.replace("\n", "
"))); + .arg(textColor, buttonText.replace("\n", "
"))); 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 diff --git a/src/plugins/remotelinux/publickeydeploymentdialog.h b/src/plugins/remotelinux/publickeydeploymentdialog.h index e6955dd0204..2d533782fc5 100644 --- a/src/plugins/remotelinux/publickeydeploymentdialog.h +++ b/src/plugins/remotelinux/publickeydeploymentdialog.h @@ -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; }; diff --git a/src/plugins/remotelinux/remotelinux.qbs b/src/plugins/remotelinux/remotelinux.qbs index d04347a7325..3440fa9689e 100644 --- a/src/plugins/remotelinux/remotelinux.qbs +++ b/src/plugins/remotelinux/remotelinux.qbs @@ -84,8 +84,6 @@ Project { "remotelinuxx11forwardingaspect.h", "rsyncdeploystep.cpp", "rsyncdeploystep.h", - "sshkeydeployer.cpp", - "sshkeydeployer.h", "sshprocessinterface.h", "tarpackagecreationstep.cpp", "tarpackagecreationstep.h", diff --git a/src/plugins/remotelinux/sshkeydeployer.cpp b/src/plugins/remotelinux/sshkeydeployer.cpp deleted file mode 100644 index d5a81799d9d..00000000000 --- a/src/plugins/remotelinux/sshkeydeployer.cpp +++ /dev/null @@ -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 -#include - -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 diff --git a/src/plugins/remotelinux/sshkeydeployer.h b/src/plugins/remotelinux/sshkeydeployer.h deleted file mode 100644 index 984f7a664f5..00000000000 --- a/src/plugins/remotelinux/sshkeydeployer.h +++ /dev/null @@ -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 - -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