Skip to content

Commit

Permalink
Merge pull request #62 from synacker/feature/ssh_provider
Browse files Browse the repository at this point in the history
Feature/ssh provider
  • Loading branch information
synacker authored Dec 9, 2023
2 parents fdd2abf + 14fa610 commit 52bd3a1
Show file tree
Hide file tree
Showing 18 changed files with 499 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/Daggy/CConsoleDaggy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void CConsoleDaggy::stop()
{
if (need_hard_stop_) {
qWarning() << "HARD STOP";
delete daggy_core_;
qApp->exit();
} else {
daggy_core_->stop();
Expand Down
5 changes: 5 additions & 0 deletions src/DaggyCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ target_sources(${TARGET}
Errors.cpp
providers/IProvider.cpp
providers/CLocal.cpp
providers/CLocal.hpp
providers/IFabric.cpp
providers/CLocalFabric.cpp
providers/CSsh.cpp
providers/CSsh.hpp
providers/CSshFabric.cpp
providers/CSshFabric.hpp
aggregators/IAggregator.cpp
aggregators/CFile.cpp
aggregators/CConsole.cpp
Expand Down
9 changes: 8 additions & 1 deletion src/DaggyCore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ SOFTWARE.
#include "providers/CLocalFabric.hpp"
#include "providers/CLocal.hpp"

#include "providers/CSshFabric.hpp"
#include "providers/CSsh.hpp"

#ifdef SSH2_SUPPORT
#include "providers/CSsh2Fabric.hpp"
#include "providers/CSsh2.hpp"
Expand Down Expand Up @@ -281,13 +284,17 @@ try {
static thread_local providers::CLocalFabric local_fabric;
fabrics_map[providers::CLocal::provider_type] = &local_fabric;

static thread_local providers::CSshFabric ssh_fabric;
fabrics_map[providers::CSsh::provider_type] = &ssh_fabric;

#ifdef SSH2_SUPPORT
static thread_local providers::CSsh2Fabric ssh2_fabric;
fabrics_map[providers::CSsh2::provider_type] = &ssh2_fabric;
#endif
if (fabrics)
if (fabrics) {
for (size_t index = 0; index < size; index++)
fabrics_map[fabrics[index]->type()] = fabrics[index];
}

auto source = sources_.cbegin();
while(source != sources_.cend()) {
Expand Down
1 change: 1 addition & 0 deletions src/DaggyCore/Precompiled.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.
#pragma once

#include <QDir>
#include <QSaveFile>
#include <QString>
#include <QStandardPaths>
#include <QHostAddress>
Expand Down
37 changes: 37 additions & 0 deletions src/DaggyCore/Sources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ struct convert<QString>
}
};

template<>
struct convert<QStringList>
{
static bool decode(const Node& node, QStringList& rhs)
{
if (!node.IsSequence())
return false;

rhs.clear();
const_iterator it = node.begin();
while (it != node.end())
{
rhs.push_back(it->as<QString>());
++it;
}
return true;
}
};

template<class K, class V>
struct convert<QMap<K, V>>
{
Expand Down Expand Up @@ -252,6 +271,24 @@ bool daggy::sources::commands::Properties::operator==(const Properties& other) c
restart == other.restart;
}

QStringList daggy::sources::commands::Properties::getParameters() const
{
QStringList result;
for (const auto& option : parameters.keys())
{
result << option ;
const auto& value = parameters[option];
const auto& list = value.toStringList();
if (list.empty()) {
const auto& string_value = value.toString();
if (!string_value.isEmpty())
result << string_value;
} else
result << list;
}
return result;
}


bool daggy::sources::Properties::operator==(const Properties& other) const
{
Expand Down
2 changes: 2 additions & 0 deletions src/DaggyCore/Sources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ struct DAGGYCORE_EXPORT Properties {
bool restart = false;

bool operator==(const Properties& other) const;

QStringList getParameters() const;
};
}

Expand Down
64 changes: 32 additions & 32 deletions src/DaggyCore/providers/CLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::error_code daggy::providers::CLocal::stop() noexcept
case DaggyProviderStarting:
case DaggyProviderStarted:
case DaggyProviderFinishing:
terminate();
terminateAll();
break;

}
Expand Down Expand Up @@ -197,7 +197,7 @@ void daggy::providers::CLocal::onProcessFinished(int exit_code, QProcess::ExitSt
onProcessStop(process);
}

void daggy::providers::CLocal::terminate()
void daggy::providers::CLocal::terminateAll()
{
if (state() != DaggyProviderStarted)
return;
Expand All @@ -207,11 +207,9 @@ void daggy::providers::CLocal::terminate()
for (QProcess* process : processes()) {
switch (process->state()) {
case QProcess::Running:
#ifdef Q_OS_WIN
process->kill();
#else
process->terminate();
#endif
{
terminate(process);
}
break;
case QProcess::Starting:
process->close();
Expand All @@ -226,25 +224,17 @@ void daggy::providers::CLocal::terminate()

QProcess* daggy::providers::CLocal::startProcess(const sources::Command& command)
{
QProcess* process = new QProcess(this);
process->setObjectName(command.first);

connect(process, &QProcess::destroyed, this, &CLocal::onProcessDestroyed);
connect(process, &QProcess::started, this, &CLocal::onProcessStart);
connect(process, &QProcess::errorOccurred, this, &CLocal::onProcessError);
connect(process, &QProcess::readyReadStandardOutput, this, qOverload<>(&CLocal::onProcessReadyReadStandard));
connect(process, &QProcess::readyReadStandardError, this, qOverload<>(&CLocal::onProcessReadyReadError));
connect(process, &QProcess::finished, this, &CLocal::onProcessFinished);

const auto& properties = command.second;
return startProcess(command.first, properties.exec, properties.getParameters());
}

auto parameters = properties.exec.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
auto program = parameters.takeFirst();
emit commandStateChanged(process->objectName(),
DaggyCommandStarting,
process->exitCode());
process->start(program, parameters);
return process;
void daggy::providers::CLocal::terminate(QProcess* process)
{
#ifdef Q_OS_WIN
process->kill();
#else
process->terminate();
#endif
}

bool daggy::providers::CLocal::onProcessStop(QProcess* process)
Expand All @@ -258,6 +248,7 @@ bool daggy::providers::CLocal::onProcessStop(QProcess* process)
startProcess({command_id, properties});
return true;
}

return false;
}

Expand Down Expand Up @@ -293,15 +284,24 @@ void daggy::providers::CLocal::startCommands()
}
}

void daggy::providers::CLocal::startProcess(QProcess* process, const QString& command)
QProcess* daggy::providers::CLocal::startProcess(const QString& process_name, const QString& exec, const QStringList& arguments)
{
const auto& id = process->objectName();
auto parameters = command.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
auto program = parameters.takeFirst();
metaStream(id, DaggyStreamStandard, true);
metaStream(id, DaggyStreamError, true);
emit commandStateChanged(id,
QProcess* process = new QProcess(this);
process->setObjectName(process_name);

connect(process, &QProcess::destroyed, this, &CLocal::onProcessDestroyed);
connect(process, &QProcess::started, this, &CLocal::onProcessStart);
connect(process, &QProcess::errorOccurred, this, &CLocal::onProcessError);
connect(process, &QProcess::readyReadStandardOutput, this, qOverload<>(&CLocal::onProcessReadyReadStandard));
connect(process, &QProcess::readyReadStandardError, this, qOverload<>(&CLocal::onProcessReadyReadError));
connect(process, &QProcess::finished, this, &CLocal::onProcessFinished);

emit commandStateChanged(process->objectName(),
DaggyCommandStarting,
process->exitCode());
process->start(program, parameters, QIODevice::ReadOnly);
if (arguments.empty())
process->startCommand(exec);
else
process->start(exec, arguments);
return process;
}
10 changes: 6 additions & 4 deletions src/DaggyCore/providers/CLocal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ private slots:
void onProcessFinished(int exit_code, QProcess::ExitStatus);

private:
void terminate();

QProcess* startProcess(const daggy::sources::Command& command);
bool onProcessStop(QProcess* process);

QList<QProcess*> processes() const;
int activeProcessesCount() const;

protected:
virtual QProcess* startProcess(const daggy::sources::Command& command);
virtual void terminate(QProcess* process);

void startCommands();
void terminateAll();

void startProcess(QProcess* process, const QString& command);
QProcess* startProcess(const QString& process_name, const QString& exec, const QStringList& arguments);
};

}
Expand Down
Loading

0 comments on commit 52bd3a1

Please sign in to comment.