checkin for saving current state
This commit is contained in:
44
common/include/command.h
Normal file
44
common/include/command.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef COMMAND_H_INCLUDED
|
||||
#define COMMAND_H_INCLUDED
|
||||
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QProcess>
|
||||
#include <QScopedPointer>
|
||||
|
||||
class Command : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
QString m_command;
|
||||
QString m_commandResult;
|
||||
|
||||
int m_waitForStartTimeout;
|
||||
int m_waitForFinishTimeout;
|
||||
int m_exitCode;
|
||||
QString m_workingDirectory;
|
||||
|
||||
QScopedPointer<QProcess> m_p;
|
||||
|
||||
QStringList m_args;
|
||||
|
||||
public:
|
||||
Command(QString command,
|
||||
QStringList args,
|
||||
QString workingDirectory,
|
||||
int start_timeout = 100000,
|
||||
int finish_timeout = 100000);
|
||||
|
||||
QString getCommandResult(bool reset = false);
|
||||
QString command() const { return m_command; }
|
||||
|
||||
bool exec();
|
||||
int exitCode() const { return m_exitCode; }
|
||||
|
||||
private slots:
|
||||
virtual void readyReadStandardOutput();
|
||||
virtual void readyReadStandardError();
|
||||
};
|
||||
|
||||
#endif // COMMAND_H_INCLUDED
|
16
common/include/utils.h
Normal file
16
common/include/utils.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef UTILS_INTERNAL_H_INCLUDED
|
||||
#define UTILS_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace internal {
|
||||
int read1stLineOfFile(QString fileName);
|
||||
QString customerRepoRoot();
|
||||
QString customerRepoDir();
|
||||
QString customerRepoDirName();
|
||||
QString repositoryUrl();
|
||||
QString branchName();
|
||||
bool customerRepoExists();
|
||||
}
|
||||
|
||||
#endif // UTILS_INTERNAL_H_INCLUDED
|
34
common/include/utils_internal.h
Normal file
34
common/include/utils_internal.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef UTILS_INTERNAL_H_INCLUDED
|
||||
#define UTILS_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace internal {
|
||||
|
||||
static constexpr const char *UPDATE_NOT_NECESSARY{"not necessary"};
|
||||
static constexpr const char *UPDATE_NOT_REQUESTED{"not requested"};
|
||||
static constexpr const char *UPDATE_REQUESTED{"requested"};
|
||||
static constexpr const char *NO_CUSTOMER_REPOSITORY{"no customer repository"};
|
||||
static constexpr const char *ISMAS_CONNECTED{"connected"};
|
||||
static constexpr const char *ISMAS_DISCONNECTED{"disconnected"};
|
||||
static constexpr const char *ISMAS_DISCONNECTING{"disconnecting"};
|
||||
static constexpr const char *ISMAS_NOT_CONNECTED{"not connected"};
|
||||
static constexpr const char *ISMAS_CONNECTION_IN_PROGRESS{"connecting"};
|
||||
static constexpr const char *GIT_CUSTOMER_REPO_CHECKOUT_ERROR{"checkout error"};
|
||||
static constexpr const char *GIT_CUSTOMER_REPO_PULL_ERROR{"pull error"};
|
||||
static constexpr const char *GIT_CUSTOMER_REPO_UP_TO_DATE{"up to date"};
|
||||
static constexpr const char *EXEC_OPKG_COMMANDS_SUCCESS{"success"};
|
||||
static constexpr const char *UPDATE_DC_JSON_FILES_SUCCESS{"success"};
|
||||
static constexpr const char *SYNC_CUSTOMER_REPO_FILES_SUCCESS{"success"};
|
||||
static constexpr const char *UPDATE_DC_FIRMARE_SUCCESS{"success"};
|
||||
|
||||
int read1stLineOfFile(QString fileName);
|
||||
QString customerRepoRoot();
|
||||
QString customerRepoDir();
|
||||
QString customerRepoDirName();
|
||||
QString repositoryUrl();
|
||||
QString branchName();
|
||||
bool customerRepoExists();
|
||||
}
|
||||
|
||||
#endif // UTILS_INTERNAL_H_INCLUDED
|
121
common/src/command.cpp
Normal file
121
common/src/command.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#include "command.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QRegularExpression>
|
||||
#include <QDateTime>
|
||||
|
||||
Command::Command(QString command, QStringList args, QString workingDirectory,
|
||||
int start_timeout, int finish_timeout)
|
||||
: m_command(command.trimmed())
|
||||
, m_commandResult("")
|
||||
, m_waitForStartTimeout(start_timeout)
|
||||
, m_waitForFinishTimeout(finish_timeout)
|
||||
, m_exitCode(-1)
|
||||
, m_workingDirectory(workingDirectory)
|
||||
, m_args(args) {
|
||||
m_p.reset(new QProcess(this));
|
||||
if (m_p) {
|
||||
m_p->setWorkingDirectory(workingDirectory);
|
||||
m_p->setProcessChannelMode(QProcess::MergedChannels);
|
||||
|
||||
connect(m_p.get(), SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()));
|
||||
connect(m_p.get(), SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()));
|
||||
}
|
||||
}
|
||||
|
||||
void Command::readyReadStandardOutput() {
|
||||
QProcess *p = (QProcess *)sender();
|
||||
if (p) {
|
||||
qCritical().noquote() << p->readAllStandardOutput();
|
||||
}
|
||||
}
|
||||
|
||||
void Command::readyReadStandardError() {
|
||||
QProcess *p = (QProcess *)sender();
|
||||
if (p) {
|
||||
qCritical().noquote() << p->readAllStandardError();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QString Command::getCommandResult(bool reset) {
|
||||
if (reset == false) {
|
||||
return m_commandResult;
|
||||
}
|
||||
|
||||
QString commandResult = m_commandResult;
|
||||
m_commandResult.clear();
|
||||
|
||||
return commandResult;
|
||||
}
|
||||
|
||||
bool Command::exec() {
|
||||
|
||||
if (!m_args.isEmpty()) {
|
||||
m_p->start(m_command, m_args);
|
||||
} else {
|
||||
m_p->start(m_command);
|
||||
}
|
||||
|
||||
qint64 const start = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
|
||||
bool started = false;
|
||||
if ((started = m_p->waitForStarted(m_waitForStartTimeout)) == true) {
|
||||
// qCritical() << "PROCESS" << m_command << "STARTED IN" << m_p->workingDirectory();
|
||||
if (m_p->state() == QProcess::ProcessState::Running) {
|
||||
// qDebug() << "PROCESS" << m_command << "RUNNING IN" << p->workingDirectory();
|
||||
// wait forever for git/opkg-commands to finish
|
||||
int wait = m_waitForFinishTimeout;
|
||||
if (m_command.trimmed().startsWith("git", Qt::CaseInsensitive) ||
|
||||
m_command.trimmed().startsWith("opkg", Qt::CaseInsensitive)) {
|
||||
wait = -1;
|
||||
}
|
||||
bool const no_timeout = m_p->waitForFinished(wait);
|
||||
if (no_timeout) {
|
||||
// qDebug() << "PROCESS" << m_command << "FINISHED IN" << p->workingDirectory();
|
||||
if (m_p->exitStatus() == QProcess::NormalExit) {
|
||||
if ((m_exitCode = m_p->exitCode()) == 0) {
|
||||
qCritical().noquote() << m_p->readAllStandardOutput();
|
||||
//qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
//qDebug() << "EXECUTED" << m_command
|
||||
// << QString("(runtime %1ms)").arg(end-start)
|
||||
// << "with code" << m_exitCode
|
||||
// << "IN" << m_p->workingDirectory();
|
||||
return true;
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "EXECUTED" << m_command
|
||||
<< QString("(runtime %1ms)").arg(end-start)
|
||||
<< "with code" << m_exitCode
|
||||
<< "IN" << m_p->workingDirectory();
|
||||
}
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "PROCESS" << m_command << "CRASHED with code"
|
||||
<< m_p->exitCode()
|
||||
<< QString("(after %1ms)").arg(end-start)
|
||||
<< "IN" << m_p->workingDirectory();
|
||||
}
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "PROCESS" << m_command
|
||||
<< "DID NOT FINISH WITH" << wait
|
||||
<< "MS IN" << m_p->workingDirectory()
|
||||
<< QString("(runtime %1ms)").arg(end-start);
|
||||
}
|
||||
} else {
|
||||
qCritical() << "WRONG PROCESS STATE" << m_p->state()
|
||||
<< "IN" << m_p->workingDirectory();
|
||||
}
|
||||
} else {
|
||||
qint64 const end = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
||||
qCritical() << "PROCESS" << m_command << "TIMEOUT AT START"
|
||||
<< QString("(runtime %1ms)").arg(end-start)
|
||||
<< "IN" << m_p->workingDirectory() << m_waitForStartTimeout;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
55
common/src/utils.cpp
Normal file
55
common/src/utils.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QTextStream>
|
||||
|
||||
namespace internal {
|
||||
|
||||
int read1stLineOfFile(QString fileName) {
|
||||
QFile f(fileName);
|
||||
if (f.exists()) {
|
||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QTextStream in(&f);
|
||||
in.setCodec("UTF-8");
|
||||
while(!in.atEnd()) {
|
||||
return in.readLine().toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString customerRepoRoot() {
|
||||
return "/opt/app/tools/atbupdate/";
|
||||
}
|
||||
|
||||
QString customerRepoDirName() {
|
||||
int const customerNr = read1stLineOfFile("/mnt/system_data/cust_nr");
|
||||
return (customerNr != -1) ? QString("customer_%1").arg(customerNr) : "";
|
||||
}
|
||||
|
||||
QString customerRepoDir() {
|
||||
QString const &n = customerRepoDirName();
|
||||
QString const &r = customerRepoRoot();
|
||||
return !n.isEmpty() ? QDir::cleanPath(r + QDir::separator() + n) : "";
|
||||
}
|
||||
|
||||
bool customerRepoExists() {
|
||||
QString const repoDir{customerRepoDir()};
|
||||
return !repoDir.isEmpty() ? QDir(repoDir).exists() : false;
|
||||
}
|
||||
|
||||
QString repositoryUrl() {
|
||||
return "gitea@ptu-config.atb-comm.de:ATB/";
|
||||
}
|
||||
|
||||
QString branchName() {
|
||||
int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
|
||||
if (zoneNr != -1) {
|
||||
return QString("zg1/zone%1").arg(zoneNr);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
} // namespace internal
|
55
common/src/utils_internal.cpp
Normal file
55
common/src/utils_internal.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QTextStream>
|
||||
|
||||
namespace internal {
|
||||
|
||||
int read1stLineOfFile(QString fileName) {
|
||||
QFile f(fileName);
|
||||
if (f.exists()) {
|
||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QTextStream in(&f);
|
||||
in.setCodec("UTF-8");
|
||||
while(!in.atEnd()) {
|
||||
return in.readLine().toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString customerRepoRoot() {
|
||||
return "/opt/app/tools/atbupdate/";
|
||||
}
|
||||
|
||||
QString customerRepoDirName() {
|
||||
int const customerNr = read1stLineOfFile("/mnt/system_data/cust_nr");
|
||||
return (customerNr != -1) ? QString("customer_%1").arg(customerNr) : "";
|
||||
}
|
||||
|
||||
QString customerRepoDir() {
|
||||
QString const &n = customerRepoDirName();
|
||||
QString const &r = customerRepoRoot();
|
||||
return !n.isEmpty() ? QDir::cleanPath(r + QDir::separator() + n) : "";
|
||||
}
|
||||
|
||||
bool customerRepoExists() {
|
||||
QString const repoDir{customerRepoDir()};
|
||||
return !repoDir.isEmpty() ? QDir(repoDir).exists() : false;
|
||||
}
|
||||
|
||||
QString repositoryUrl() {
|
||||
return "gitea@ptu-config.atb-comm.de:ATB/";
|
||||
}
|
||||
|
||||
QString branchName() {
|
||||
int const zoneNr = read1stLineOfFile("/mnt/system_data/zone_nr");
|
||||
if (zoneNr != -1) {
|
||||
return QString("zg1/zone%1").arg(zoneNr);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
} // namespace internal
|
Reference in New Issue
Block a user