Commit 4b30d249789538c4d18911482d4873c911acd235

Authored by Thiago Rocha
2 parents 05f61cfd 61d7682c
Exists in master

Merge branch 'master' of http://github.com/lightbase/cacic-agente

cacic-daemon/cacicD/cacicD.pro
... ... @@ -19,26 +19,29 @@ win32 {
19 19 LIBS += -L/usr/lib -lcryptopp
20 20 }
21 21 TEMPLATE = app
22   -#TEMPLATE = lib
23 22  
24 23 SOURCES += main.cpp \
25 24 cacicd.cpp \
26 25 cacictimer.cpp \
  26 + cacicthread.cpp \
27 27 ../../src/cacic_comm.cpp \
28 28 ../../src/ccacic.cpp \
29 29 ../../src/wmi.cpp \
30 30 ../../src/cacic_computer.cpp \
31 31 ../../src/operatingsystem.cpp \
32   - ../../src/QLogger.cpp
  32 + ../../src/QLogger.cpp \
  33 +
33 34  
34 35 HEADERS += cacicd.h \
35 36 cacictimer.h \
  37 + cacicthread.h \
36 38 ../../src/ccacic.h \
37 39 ../../src/wmi.h \
38 40 ../../src/cacic_computer.h \
39 41 ../../src/operatingsystem.h \
40 42 ../../src/cacic_comm.h \
41   - ../../src/QLogger.h
  43 + ../../src/QLogger.h \
  44 +
42 45  
43 46 include(../../src/qtservice/src/qtservice.pri)
44 47  
... ...
cacic-daemon/cacicD/cacicthread.cpp 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +#include "cacicthread.h"
  2 +
  3 +CacicThread::CacicThread(QString applicationDirPath)
  4 +{
  5 + this->applicationDirPath = applicationDirPath;
  6 + iniciarInstancias();
  7 +}
  8 +
  9 +void CacicThread::run(){
  10 + iniciarModulo();
  11 +}
  12 +
  13 +void CacicThread::setModuloDirPath(const QString &value)
  14 +{
  15 + moduloDirPath = value;
  16 +}
  17 +
  18 +void CacicThread::iniciarModulo()
  19 +{
  20 + registraInicioColeta();
  21 + QDir::setCurrent(this->applicationDirPath);
  22 + QProcess proc;
  23 + proc.setWorkingDirectory(this->applicationDirPath);
  24 + proc.execute(this->moduloDirPath);
  25 + if((proc.atEnd()) && (proc.exitStatus() == QProcess::NormalExit)){
  26 + registraFimColeta("SUCESSO");
  27 + }else{
  28 + if((!proc.atEnd()) || (proc.exitStatus() == QProcess::CrashExit)){
  29 + registraFimColeta("ERRO");
  30 + proc.kill();
  31 + }
  32 + }
  33 + cMutex->unlock();
  34 + QLogger::QLog_Info("Cacic Daemon (Thread)", QString("Semáforo aberto com sucesso."));
  35 +}
  36 +
  37 +void CacicThread::setCMutex(QMutex *value)
  38 +{
  39 + cMutex = value;
  40 +}
  41 +
  42 +void CacicThread::registraInicioColeta()
  43 +{
  44 + QLogger::QLog_Info("Cacic Daemon (Thread)","Coleta iniciada em: " + QDateTime::currentDateTime().toLocalTime().toString());
  45 +}
  46 +
  47 +void CacicThread::registraFimColeta(QString msg)
  48 +{
  49 + QLogger::QLog_Info("Cacic Daemon (Thread)","Coleta finalizada com " + msg + " em: " + QDateTime::currentDateTime().toLocalTime().toString());
  50 +}
  51 +
  52 +void CacicThread::iniciarInstancias(){
  53 + logManager = QLogger::QLoggerManager::getInstance();
  54 + logManager->addDestination(this->applicationDirPath + "/cacicLog.txt","Cacic Daemon (Thread)",QLogger::InfoLevel);
  55 + logManager->addDestination(this->applicationDirPath + "/cacicLog.txt","Cacic Daemon (Thread)",QLogger::ErrorLevel);
  56 +}
... ...
cacic-daemon/cacicD/cacicthread.h 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +#ifndef CACICTHREAD_H
  2 +#define CACICTHREAD_H
  3 +#include <QtCore>
  4 +#include <QMutex>
  5 +#include "QLogger.h"
  6 +
  7 +class CacicThread : public QThread
  8 +{
  9 +public:
  10 + CacicThread(QString applicationDirPath);
  11 + void run();
  12 + void setModuloDirPath(const QString &value);
  13 + void setCMutex(QMutex *value);
  14 +
  15 +private:
  16 + QString moduloDirPath;
  17 + QString applicationDirPath;
  18 + QLogger::QLoggerManager *logManager;
  19 + void iniciarInstancias();
  20 + void registraInicioColeta();
  21 + void registraFimColeta(QString msg);
  22 + void iniciarModulo();
  23 + QMutex *cMutex;
  24 +};
  25 +
  26 +#endif // CACICTHREAD_H
... ...
cacic-daemon/cacicD/cacictimer.cpp
... ... @@ -38,36 +38,52 @@ void CacicTimer::mslot(){
38 38 }catch (...){
39 39 QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Não foi possivel verificar a periodicidade no getConfig.json"));
40 40 }
41   - cMutex->lock();
42   - QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo fechado."));
  41 +
  42 + verificarEIniciarQMutex();
  43 +
43 44 if(getTest()){
44 45 QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success."));
45 46 if(getConfig()){
46 47 QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success."));
47 48 //QStringList nomesModulos = verificarModulos();
48 49 //if ( !nomesModulos.empty() ) {
49   - // foreach( QString nome, nomesModulos ) {
  50 + // foreach( QString nome, nomesModulos ) {
50 51 definirDirModulo(getApplicationDirPath(), "gercols");
51   - iniciarModulo(getDirProgram());
52   -
53   - // if(nome == "gercols"){
54   - // // Envio do json gerado na coleta
55   - // bool ok;
56   - // QJsonObject jsonColeta = ccacic->getJsonFromFile("coleta.json");
57   - // OCacicComm->comm("/ws/neo/coleta", &ok, jsonColeta );
58   - // }
59   - // }
  52 + cacicthread->setCMutex(cMutex);
  53 + cacicthread->setModuloDirPath(getDirProgram());
  54 + cacicthread->start(QThread::NormalPriority);
  55 + //if(nome == "gercols"){
  56 + // Envio do json gerado na coleta
  57 + // bool ok;
  58 + // QJsonObject jsonColeta = ccacic->getJsonFromFile("coleta.json");
  59 + // OCacicComm->comm("/ws/neo/coleta", &ok, jsonColeta , false);
60 60 //}
  61 + // }
  62 + // }
61 63 }else{
62   - qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString();
63 64 QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração.");
64 65 }
65 66 }else{
66   - qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString();
67 67 QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na execução do getTest().");
68 68 }
69   - cMutex->unlock();
70   - QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo aberto."));
  69 +}
  70 +
  71 +void CacicTimer::verificarEIniciarQMutex(){
  72 + if(!cacicthread->isRunning()){
  73 + cMutex->lock();
  74 + QLogger::QLog_Info("Cacic Daemon (Timer)", "Semáforo fechado com sucesso.");
  75 + }else{
  76 + QLogger::QLog_Info("Cacic Daemon (Timer)", "Possivelmente o gercols travou e será finalizado.");
  77 + try{
  78 + cacicthread->terminate();
  79 + QLogger::QLog_Info("Cacic Daemon (Timer)", "Gercols finalizado com sucesso.");
  80 + }catch (...){
  81 + QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha ao finalizar gercols.");
  82 + return;
  83 + }
  84 + cMutex->lock();
  85 + QLogger::QLog_Info("Cacic Daemon (Timer)", "Semáforo fechado com sucesso.");
  86 + }
71 87 }
72 88  
73 89 QString CacicTimer::getApplicationDirPath() {
... ... @@ -167,11 +183,6 @@ void CacicTimer::lerArquivoConfig ( const QJsonObject&amp; jsonConfig )
167 183 }
168 184 }
169 185  
170   -void CacicTimer::registraInicioColeta()
171   -{
172   - QLogger::QLog_Info("Cacic Daemon (Timer)","Coleta iniciada em: " + QDateTime::currentDateTime().toLocalTime().toString());
173   -}
174   -
175 186 QString CacicTimer::getDirProgram() const
176 187 {
177 188 return dirProgram;
... ... @@ -183,44 +194,18 @@ void CacicTimer::setDirProgram(const QString &amp;value)
183 194 }
184 195  
185 196  
186   -void CacicTimer::iniciarModulo(QString modulo)
187   -{
188   - registraInicioColeta();
189   - QDir::setCurrent(this->applicationDirPath);
190   - QProcess proc;
191   - proc.setWorkingDirectory(this->applicationDirPath);
192   - proc.execute(modulo);
193   - if((proc.atEnd()) && (proc.exitStatus() == QProcess::NormalExit)){
194   - registraFimColeta("SUCESSO");
195   - }else{
196   - if((!proc.atEnd()) || (proc.exitStatus() == QProcess::CrashExit)){
197   - registraFimColeta("ERRO");
198   - proc.kill();
199   - }
200   - }
201   -}
202   -
203 197 void CacicTimer::setApplicationDirPath(const QString &value)
204 198 {
205 199 this->applicationDirPath = value;
206 200 }
207 201  
208 202  
209   -void CacicTimer::registraFimColeta(QString msg)
210   -{
211   - QLogger::QLog_Info("Cacic Daemon (Timer)","Coleta finalizada com " + msg + " em: " + QDateTime::currentDateTime().toLocalTime().toString());
212   -}
213   -
214 203 bool CacicTimer::Md5IsEqual(QVariant document01,QVariant document02){
215 204 QString getconfigMD5 = QString(QCryptographicHash::hash(
216 205 (document01.toByteArray()),QCryptographicHash::Md5).toHex());
217 206 QString getconfigMD52 = QString(QCryptographicHash::hash(
218 207 (document02.toByteArray()),QCryptographicHash::Md5).toHex());
219   - if(getconfigMD5 == getconfigMD52){
220   - return true;
221   - }else{
222   - return false;
223   - }
  208 + return getconfigMD5 == getconfigMD52;
224 209 }
225 210  
226 211 void CacicTimer::iniciarInstancias(){
... ... @@ -230,6 +215,7 @@ void CacicTimer::iniciarInstancias(){
230 215 ccacic = new CCacic();
231 216 timer = new QTimer(this);
232 217 cMutex = new QMutex(QMutex::Recursive);
  218 + cacicthread = new CacicThread(this->applicationDirPath);
233 219 OCacicComm = new CacicComm();
234 220 OCacicComm->setUrlSsl("https://teste.cacic.cc");
235 221 OCacicComm->setUrlGerente("teste.cacic.cc");
... ...
cacic-daemon/cacicD/cacictimer.h
... ... @@ -12,6 +12,7 @@
12 12 #include "cacic_comm.h"
13 13 #include "cacic_computer.h"
14 14 #include "QLogger.h"
  15 +#include "cacicthread.h"
15 16  
16 17 class CacicTimer : public QObject
17 18 {
... ... @@ -34,15 +35,14 @@ public:
34 35 void setPeriodicidadeExecucao(int value);
35 36  
36 37 private:
37   - void registraFimColeta(QString msg);
38   - void registraInicioColeta();
  38 + void verificarEIniciarQMutex();
39 39 QStringList verificarModulos();
40 40 void reiniciarTimer();
41 41 QLogger::QLoggerManager *logManager;
  42 + CacicThread *cacicthread;
42 43 QString dirProgram;
43 44 QString applicationDirPath;
44 45 QString getDirProgram() const;
45   - void iniciarModulo(QString modulo);
46 46 void iniciarInstancias();
47 47 void verificarPeriodicidadeJson();
48 48 void lerArquivoConfig( const QJsonObject &jsonConfig);
... ...
cacic-daemon/cacicD/main.cpp
1 1 #include <QCoreApplication>
2 2 #include "cacicd.h"
3   -#include "cacictimer.h"
4 3  
5 4 int main(int argc, char **argv)
6 5 {
... ...
install-cacic/installcacic.cpp
... ... @@ -4,7 +4,7 @@ InstallCacic::InstallCacic(QObject *parent) :
4 4 QObject(parent)
5 5 {
6 6 logManager = QLogger::QLoggerManager::getInstance();
7   - logManager->addDestination("./log.txt","Install",QLogger::DebugLevel);
  7 + logManager->addDestination("./install.log","Install",QLogger::DebugLevel);
8 8 }
9 9  
10 10 InstallCacic::~InstallCacic()
... ... @@ -28,31 +28,51 @@ void InstallCacic::run(QStringList argv, int argc) {
28 28 oCacicComm->setPassword(this->argumentos["password"]);
29 29 QJsonObject jsonLogin = oCacicComm->login(&ok);
30 30 if (ok){
  31 + QJsonObject jsonComm;
  32 + QLogger::QLog_Debug("Install", "Login: " + jsonLogin["reply"].toObject()["chavecript"].toString());
31 33 //conectado, grava a chave na classe;
32 34 oCacic.setChaveCrypt(jsonLogin["reply"].toObject()["chavecrip"].toString());
  35 + jsonComm["computador"] = oCacicComputer.toJsonObject();
  36 + QJsonObject configs = oCacicComm->comm("/ws/neo/config", &ok, jsonComm);
  37 + qDebug () << configs;
  38 + if (ok){
  39 + oCacicComm->setUrlGerente(configs["reply"].toObject()["applicationUrl"].toString());
33 40 #ifdef Q_OS_WIN
34   - oCacic.setCacicMainFolder("c:/cacic/");
  41 + oCacic.setCacicMainFolder(configs["reply"].toObject()["cacic_main_folder"].isString() ?
  42 + configs["reply"].toObject()["cacic_main_folder"].toString() :
  43 + "c:/cacic/");
35 44 #elif defined(Q_OS_LINUX)
36   - oCacic.setCacicMainFolder("/home/cacic");
  45 + oCacic.setCacicMainFolder(configs["reply"].toObject()["cacic_main_folder"].isString() ?
  46 + configs["reply"].toObject()["cacic_main_folder"].toString() :
  47 + "/usr/cacic");
37 48 #endif
38   - oCacic.createFolder(oCacic.getCacicMainFolder());
39   - //grava chave em registro;
40 49  
41   - QVariantMap registro;
42   - registro["key"] = oCacic.getChaveCrypt();
43   - registro["mainFolder"] = oCacic.getCacicMainFolder();
44   - oCacic.setValueToRegistry("Lightbase", "Cacic", registro);
45   - //starta o processo do cacic.
  50 + oCacic.createFolder(oCacic.getCacicMainFolder());
  51 + //grava chave em registro;
  52 + QVariantMap registro;
  53 + registro["key"] = oCacic.getChaveCrypt();
  54 + registro["mainFolder"] = oCacic.getCacicMainFolder();
  55 + oCacic.setValueToRegistry("Lightbase", "Cacic", registro);
  56 + //starta o processo do cacic.
  57 +
  58 + //TO DO: Fazer download do serviço
  59 + #ifdef Q_OS_WIN
  60 + oCacicComm->ftpDownload("agentes/cacic.exe");
  61 + QString exitStatus = oCacic.startProcess(oCacic.getCacicMainFolder() + "cacic.exe", false, &ok, QStringList("-install"));
  62 + #else
  63 + oCacicComm->ftpDownload("agentes/cacic");
  64 + QString exitStatus = oCacic.startProcess(oCacic.getCacicMainFolder() + "cacic", false, &ok, QStringList("-install"));
  65 + #endif
  66 + if (!ok)
  67 + std::cout << "Erro ao iniciar o processo: "
  68 + << exitStatus.toStdString() << "\n";
  69 + else {
  70 + std::cout << "Instalação realizada com sucesso.";
  71 + }
  72 + } else {
  73 + std::cout << "Falha ao pegar configurações: " << configs["error"].toString();
  74 + }
46 75  
47   - //TO DO: Fazer download do serviço
48   -#ifdef Q_OS_WIN
49   - QString exitStatus = oCacic.startProcess(oCacic.getCacicMainFolder() + "cacic.exe", false, &ok);
50   -#else
51   - QString exitStatus = oCacic.startProcess(oCacic.getCacicMainFolder() + "cacic", false, &ok);
52   -#endif
53   - if (!ok)
54   - std::cout << "Erro ao iniciar o processo: "
55   - << exitStatus.toStdString() << "\n";
56 76 } else
57 77 std::cout << "Nao foi possivel realizar o login.\n "
58 78 << jsonLogin["error"].toString().toStdString();
... ...