Commit 48bd8e5a7d5a3b08241c8e6eaa6fb662bfd22890
1 parent
fb84683c
Exists in
master
Modificações para adequar ao JSON de configuração definido.
Showing
4 changed files
with
161 additions
and
126 deletions
Show diff stats
cacic-daemon/cacicD/cacicd.cpp
... | ... | @@ -15,6 +15,7 @@ cacicD::cacicD(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, |
15 | 15 | logManager->addDestination(application()->applicationDirPath() + "/cacicLog.txt","Cacic Daemon",QLogger::ErrorLevel); |
16 | 16 | } catch (...){ |
17 | 17 | qCritical() << "Error desconhecido no construtor."; |
18 | + QLogger::QLog_Error("Cacic Daemon", QString("Erro desconhecido no construtor.")); | |
18 | 19 | } |
19 | 20 | } |
20 | 21 | |
... | ... | @@ -24,51 +25,49 @@ cacicD::~cacicD() |
24 | 25 | logManager->closeLogger(); |
25 | 26 | application()->exit(); |
26 | 27 | } catch (...){ |
27 | - qCritical() << "Error desconhecido no desconstrutor."; | |
28 | + QLogger::QLog_Error("Cacic Daemon", QString("Erro desconhecido no destrutor.")); | |
28 | 29 | } |
29 | 30 | } |
30 | 31 | |
31 | 32 | void cacicD::start() { |
32 | 33 | try{ |
33 | - qDebug() << "Inicio do daemon"; | |
34 | - qDebug() << "\nServiço iniciado em" << application()->applicationDirPath(); | |
35 | 34 | QLogger::QLog_Info("Cacic Daemon", QString("Servico iniciado em ").append(application()->applicationDirPath()).append(".")); |
36 | 35 | QJsonObject result = ccacic->getJsonFromFile(application()->applicationDirPath().append("/getConfig.json")); |
37 | 36 | if(!result.contains("error") && !result.isEmpty()){ |
38 | 37 | Ocacictimer->iniciarTimer(result["codestatus"].toInt()); |
39 | 38 | }else{ |
40 | - qDebug() << "getConfig.json não encontrado."; | |
39 | + QLogger::QLog_Error("Cacic Daemon", QString("getConfig.json não encontrado.")); | |
41 | 40 | } |
42 | 41 | //QLogger::QLog_Info("Cacic Daemon", "Serviço finalizado."); |
43 | 42 | }catch (...){ |
44 | - qCritical() << "Error desconhecido ao iniciar o serviço."; | |
43 | + QLogger::QLog_Error("Cacic Daemon", QString("Erro desconhecido ao iniciar o serviço.")); | |
45 | 44 | } |
46 | 45 | } |
47 | 46 | |
48 | 47 | void cacicD::pause() |
49 | 48 | { |
50 | 49 | try{ |
51 | - qDebug() << "Serviço pausado."; | |
50 | + QLogger::QLog_Info("Cacic Daemon", QString("Serviço pausado.")); | |
52 | 51 | } catch (...){ |
53 | - qCritical() << "Error desconhecido ao pausar o serviço."; | |
52 | + QLogger::QLog_Error("Cacic Daemon", QString("Erro desconhecido ao pausar o serviço.")); | |
54 | 53 | } |
55 | 54 | } |
56 | 55 | |
57 | 56 | void cacicD::resume() |
58 | 57 | { |
59 | 58 | try{ |
60 | - qDebug() << "Serviço resumido."; | |
59 | + QLogger::QLog_Info("Cacic Daemon", QString("Serviço resumido.")); | |
61 | 60 | } catch (...){ |
62 | - qCritical() << "Error desconhecido ao resumir o serviço."; | |
61 | + QLogger::QLog_Error("Cacic Daemon", QString("Erro desconhecido ao resumir o serviço.")); | |
63 | 62 | } |
64 | 63 | } |
65 | 64 | |
66 | 65 | void cacicD::stop() |
67 | 66 | { |
68 | 67 | try{ |
69 | - qDebug() << "Serviço parado."; | |
68 | + QLogger::QLog_Info("Cacic Daemon", QString("Serviço parado.")); | |
70 | 69 | } catch (...){ |
71 | - qCritical() << "Error desconhecido ao parar o serviço."; | |
70 | + QLogger::QLog_Error("Cacic Daemon", QString("Erro desconhecido ao parar o serviço.")); | |
72 | 71 | } |
73 | 72 | } |
74 | 73 | ... | ... |
cacic-daemon/cacicD/cacictimer.cpp
... | ... | @@ -25,117 +25,106 @@ void CacicTimer::mslot(){ |
25 | 25 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("mslot();")); |
26 | 26 | cMutex->lock(); |
27 | 27 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo fechado.")); |
28 | - //if(getTest()){ | |
29 | - // QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success.")); | |
30 | - // if(getConfig()){ | |
31 | - // QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success.")); | |
32 | - iniciarGercols(); | |
33 | - // } | |
34 | - // } | |
28 | + | |
29 | + if(getTest()){ | |
30 | + QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success.")); | |
31 | + | |
32 | + if(getConfig()){ | |
33 | + QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success.")); | |
34 | + | |
35 | + // Compara o novo arquivo de configuração com um antigo e se forem diferentes | |
36 | + // mantem o mais recente; caso iguais simplesmente apaga o novo. | |
37 | + QFile *fileOld; | |
38 | + QFile *fileNew; | |
39 | + | |
40 | + fileOld = new QFile(getApplicationDirPath().append("/getConfig.json")); | |
41 | + fileNew = new QFile(getApplicationDirPath().append("/getConfigNew.json")); | |
42 | + if( fileOld->exists() && fileNew->exists() ){ | |
43 | + if( Md5IsEqual(QVariant::fromValue(fileOld), QVariant::fromValue(fileNew)) ) { | |
44 | + fileNew->remove(); | |
45 | + } else { | |
46 | + // Renomeia getConfigNew.json para getConfig.json | |
47 | + fileOld->remove(); | |
48 | + fileNew->rename("getConfigNew.json","getConfig.json"); | |
49 | + } | |
50 | + jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json")); | |
51 | + } else if( fileOld->exists() ){ | |
52 | + jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json")); | |
53 | + } else { | |
54 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Arquivo de configuração não criado."); | |
55 | + } | |
56 | + delete fileOld; | |
57 | + delete fileNew; | |
58 | + | |
59 | + lerArquivoConfig(jsonConfig); | |
60 | + | |
61 | + int countExecNotFound = 0; | |
62 | + QMap<QString, QString>::const_iterator mapIterator = moduleMap.constBegin(); | |
63 | + while (mapIterator != moduleMap.constEnd()) { | |
64 | + QString nomeModulo = mapIterator.value(); | |
65 | + QString hashModulo = mapIterator.key(); | |
66 | + | |
67 | + // Calcula hash do binario atual | |
68 | +#if defined(Q_OS_WIN) | |
69 | + fileOld = new QFile(getApplicationDirPath().append("/").append(modulo).append(".exe")); | |
70 | +#else | |
71 | + fileOld = new QFile(getApplicationDirPath().append("/").append(nomeModulo)); | |
72 | +#endif | |
73 | + if(!fileOld->exists()) { | |
74 | + QLogger::QLog_Error("Cacic Daemon (Timer)", QString("Módulo ").append(nomeModulo).append(" não encontrado.")); | |
75 | + | |
76 | + countExecNotFound++; | |
77 | + if( countExecNotFound == moduleMap.size() ) { | |
78 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Não foi possível encontrar nenhum módulo executável!"); | |
79 | + return; | |
80 | + } | |
81 | + continue; | |
82 | + } | |
83 | + | |
84 | + QString oldMd5 = QString(QCryptographicHash::hash(fileOld->readAll(),QCryptographicHash::Md5).toHex()); | |
85 | + | |
86 | + if ( oldMd5 != hashModulo ) { | |
87 | + | |
88 | + fileOld->rename(getApplicationDirPath().append("/").append(nomeModulo), | |
89 | + getApplicationDirPath().append("/").append("nomeModulo").append("Old") ); | |
90 | + | |
91 | + // Download nova versão do executável | |
92 | + QList<QMap<QString,QString> >::const_iterator metodosIterator = metodosDownload.constBegin(); | |
93 | + bool downloadSucess = false; | |
94 | + while ( !downloadSucess && metodosIterator != metodosDownload.constEnd() ) { | |
95 | + | |
96 | + if( metodosIterator->value("tipo") == "ftp" || metodosIterator->value("tipo") == "" ) { | |
97 | + if ( OCacicComm->ftpDownload( metodosIterator->value("url"), metodosIterator->value("path") ) ) | |
98 | + downloadSucess = true; | |
99 | + | |
100 | + } else if ( metodosIterator->value("tipo") == "http" ) { | |
101 | + if( OCacicComm->httpDownload( metodosIterator->value("url"), metodosIterator->value("path") ) ) | |
102 | + downloadSucess = true; | |
103 | + } | |
104 | + metodosIterator++; | |
105 | + } | |
106 | + fileOld->remove(); | |
107 | + delete fileOld; | |
108 | + } | |
109 | + | |
110 | + mapIterator++; | |
111 | + } | |
112 | + }else{ | |
113 | + qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
114 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração."); | |
115 | + } | |
116 | + }else{ | |
117 | + qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
118 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na execução do getTest()."); | |
119 | + } | |
120 | + | |
35 | 121 | cMutex->unlock(); |
36 | 122 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo aberto.")); |
37 | 123 | } |
38 | 124 | |
39 | -// QLogger::QLog_Info("Cacic Timer", QString("semáforo aberto.")); | |
40 | -// qDebug() << "getConfig() success. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
41 | -// QLogger::QLog_Info("Cacic Daemon (Timer)", "getConfig() executado com sucesso."); | |
42 | - | |
43 | -// // Compara o novo arquivo de configuração com um antigo e se forem diferentes | |
44 | -// // mantem o mais recente; caso iguais simplesmente apaga o novo. | |
45 | -// QFile *fileOld; | |
46 | -// QFile *fileNew; | |
47 | - | |
48 | -// fileOld = new QFile(getApplicationDirPath().append("/getConfig.json")); | |
49 | -// fileNew = new QFile(getApplicationDirPath().append("/getConfigNew.json")); | |
50 | -// if( fileOld->exists() && fileNew->exists() ){ | |
51 | -// if( Md5IsEqual(QVariant::fromValue(fileOld), QVariant::fromValue(fileNew)) ) { | |
52 | -// fileNew->remove(); | |
53 | -// } else { | |
54 | -// // Renomeia getConfigNew.json para getConfig.json | |
55 | -// fileOld->remove(); | |
56 | -// fileNew->rename("getConfigNew.json","getConfig.json"); | |
57 | -// } | |
58 | -// jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json")); | |
59 | -// } else if( fileOld->exists() ){ | |
60 | -// jsonConfig = ccacic->getJsonFromFile(getApplicationDirPath().append("/getConfig.json")); | |
61 | -// } else { | |
62 | -// QLogger::QLog_Error("Cacic Daemon (Timer)", "Arquivo de configuração não criado."); | |
63 | -// } | |
64 | -// delete fileOld; | |
65 | -// delete fileNew; | |
66 | - | |
67 | -// /* lê json de configurações e armazena quais módulos executáveis. | |
68 | -// * E faz o mesmo tipo de comparação de hashs, com o fim de: | |
69 | -// * ou mantem o binário do módulo ou baixa um novo. | |
70 | -// */ | |
71 | - | |
72 | -// /* Aqui estou assumindo um formato do .json em que: | |
73 | -// * há a key modulos contem uma lista com o nome dos executaveis e os seus valores hash md5 | |
74 | -// * há a key metodo que explicita o método de download dos executaveis | |
75 | -// */ | |
76 | -// foreach( QJsonValue moduloValues, jsonConfig["modulos"].toArray() ) { | |
77 | -// QString moduloKey, moduloValue; | |
78 | - | |
79 | -// moduloKey = moduloValues.toObject()["md5"].toString(); | |
80 | -// moduloValue = moduloValues.toObject()["nome"].toString(); | |
81 | - | |
82 | -// moduleMap.insert(moduloKey, moduloValue); | |
83 | -// } | |
84 | -// metodoDownload = jsonConfig["metodo"].toString(); | |
85 | - | |
86 | -// int countExecNotFound = 0; | |
87 | -// QMap<QString, QString>::const_iterator mapIterator = moduleMap.constBegin(); | |
88 | -// while (mapIterator != moduleMap.constEnd()) { | |
89 | -// QString nomeModulo = mapIterator.value(); | |
90 | -// QString hashModulo = mapIterator.key(); | |
91 | - | |
92 | -// // Calcula hash do binario atual | |
93 | -//#if defined(Q_OS_WIN) | |
94 | -// fileOld = new QFile(getApplicationDirPath().append("/").append(modulo).append(".exe")); | |
95 | -//#else | |
96 | -// fileOld = new QFile(getApplicationDirPath().append("/").append(nomeModulo)); | |
97 | -//#endif | |
98 | -// if(!fileOld->exists()) { | |
99 | -// QLogger::QLog_Error("Cacic Daemon (Timer)", QString("Módulo ").append(nomeModulo).append(" não encontrado.")); | |
100 | - | |
101 | -// countExecNotFound++; | |
102 | -// if( countExecNotFound == moduleMap.size() ) { | |
103 | -// QLogger::QLog_Error("Cacic Daemon (Timer)", "Não foi possível encontrar nenhum módulo executável!"); | |
104 | -// return; | |
105 | -// } | |
106 | -// continue; | |
107 | -// } | |
108 | - | |
109 | -// QString oldMd5 = QString(QCryptographicHash::hash(fileOld->readAll(),QCryptographicHash::Md5).toHex()); | |
110 | - | |
111 | -// if ( oldMd5 != hashModulo ) { | |
112 | - | |
113 | -// fileOld->rename(getApplicationDirPath().append("/").append(nomeModulo), | |
114 | -// getApplicationDirPath().append("/").append("nomeModulo").append("Old") ); | |
115 | - | |
116 | -// // Download nova versão do executável | |
117 | -// if(metodoDownload == "ftp" || metodoDownload == "") { | |
118 | -// // OCacicComm->ftpDownload( QString("/ws/get/").append(nomeModulo) ); | |
119 | -// } | |
120 | - | |
121 | - | |
122 | -// fileOld->remove(); | |
123 | -// delete fileOld; | |
124 | -// } | |
125 | - | |
126 | -// mapIterator++; | |
127 | -// } | |
128 | -// }else{ | |
129 | -// qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
130 | -// QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração."); | |
131 | -// } | |
132 | -// }else{ | |
133 | -// qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
134 | -// QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na execução do getTest()."); | |
135 | -// } | |
136 | -// } | |
137 | -// } | |
138 | -//} | |
125 | +QString CacicTimer::getApplicationDirPath() { | |
126 | + return applicationDirPath; | |
127 | +} | |
139 | 128 | |
140 | 129 | bool CacicTimer::getTest(){ |
141 | 130 | try{ |
... | ... | @@ -181,7 +170,37 @@ bool CacicTimer::getConfig(){ |
181 | 170 | } |
182 | 171 | } |
183 | 172 | |
184 | -void CacicTimer::registraInicio() | |
173 | +void CacicTimer::lerArquivoConfig ( const QJsonObject& jsonConfig ) | |
174 | +{ | |
175 | + /* lê json de configurações e armazena quais módulos executáveis. | |
176 | + * E faz o mesmo tipo de comparação de hashs, com o fim de: | |
177 | + * ou mantem o binário do módulo ou baixa um novo. | |
178 | + * | |
179 | + * Aqui estou assumindo um formato do .json em que: | |
180 | + * há a key modulos contem uma lista com o nome dos executaveis e os seus valores hash md5 | |
181 | + * há a key metodo que explicita o método de download dos executaveis | |
182 | + */ | |
183 | + foreach( QJsonValue individualModule, jsonConfig["modulos"].toArray() ) { | |
184 | + QString moduloKey, moduloValue; | |
185 | + | |
186 | + moduloKey = individualModule.toObject()["hash"].toString(); | |
187 | + moduloValue = individualModule.toObject()["nome"].toString(); | |
188 | + | |
189 | + moduleMap.insert(moduloKey, moduloValue); | |
190 | + } | |
191 | + | |
192 | + foreach (QJsonValue individualMetodo, jsonConfig["metodoDownload"].toArray() ) { | |
193 | + QMap<QString, QString> newEntry; | |
194 | + | |
195 | + newEntry.insert(QString("tipo"), individualMetodo.toObject()["tipo"].toString() ); | |
196 | + newEntry.insert(QString("url"), individualMetodo.toObject()["url"].toString() ); | |
197 | + newEntry.insert(QString("path"), individualMetodo.toObject()["path"].toString() ); | |
198 | + | |
199 | + metodosDownload.append( newEntry ); | |
200 | + } | |
201 | +} | |
202 | + | |
203 | +void CacicTimer::registraInicioColeta() | |
185 | 204 | { |
186 | 205 | QLogger::QLog_Info("Cacic Daemon (Timer)","Coleta iniciada em: " + QDateTime::currentDateTime().toLocalTime().toString()); |
187 | 206 | } |
... | ... | @@ -199,17 +218,17 @@ void CacicTimer::setDirProgram(const QString &value) |
199 | 218 | |
200 | 219 | void CacicTimer::iniciarGercols() |
201 | 220 | { |
202 | - registraInicio(); | |
221 | + registraInicioColeta(); | |
203 | 222 | QDir::setCurrent(this->applicationDirPath); |
204 | 223 | QProcess proc; |
205 | 224 | proc.setWorkingDirectory(this->applicationDirPath); |
206 | 225 | proc.execute(getDirProgram()); |
207 | 226 | if((proc.atEnd()) && (proc.exitStatus() == QProcess::NormalExit)){ |
208 | - registraFim("SUCESSO"); | |
227 | + registraFimColeta("SUCESSO"); | |
209 | 228 | }else{ |
210 | 229 | proc.waitForFinished(120000); |
211 | 230 | if((!proc.atEnd()) || (proc.exitStatus() == QProcess::CrashExit)){ |
212 | - registraFim("ERRO"); | |
231 | + registraFimColeta("ERRO"); | |
213 | 232 | proc.kill(); |
214 | 233 | } |
215 | 234 | } |
... | ... | @@ -221,7 +240,7 @@ void CacicTimer::setApplicationDirPath(const QString &value) |
221 | 240 | } |
222 | 241 | |
223 | 242 | |
224 | -void CacicTimer::registraFim(QString msg) | |
243 | +void CacicTimer::registraFimColeta(QString msg) | |
225 | 244 | { |
226 | 245 | QLogger::QLog_Info("Cacic Daemon (Timer)","Coleta finalizada com " + msg + " em: " + QDateTime::currentDateTime().toLocalTime().toString()); |
227 | 246 | } | ... | ... |
cacic-daemon/cacicD/cacictimer.h
... | ... | @@ -24,16 +24,18 @@ public: |
24 | 24 | CACIC_Computer OCacic_Computer; |
25 | 25 | CCacic *ccacic; |
26 | 26 | QMutex *cMutex; |
27 | + | |
27 | 28 | void iniciarTimer(int x); |
28 | 29 | bool getTest(); |
29 | 30 | bool getConfig(); |
30 | 31 | bool Md5IsEqual(QVariant document01,QVariant document02); |
31 | 32 | void setApplicationDirPath(const QString &value); |
33 | + QString getApplicationDirPath(); | |
32 | 34 | void setDirProgram(const QString &value); |
33 | 35 | |
34 | 36 | private: |
35 | - void registraFim(QString msg); | |
36 | - void registraInicio(); | |
37 | + void registraFimColeta(QString msg); | |
38 | + void registraInicioColeta(); | |
37 | 39 | QLogger::QLoggerManager *logManager; |
38 | 40 | QString dirProgram; |
39 | 41 | //QProcess *myProcess; |
... | ... | @@ -41,9 +43,11 @@ private: |
41 | 43 | QString getDirProgram() const; |
42 | 44 | void iniciarGercols(); |
43 | 45 | void iniciarInstancias(); |
46 | + void lerArquivoConfig( const QJsonObject &jsonConfig); | |
44 | 47 | void definirDirGercols(QString appDirPath); |
45 | 48 | QJsonObject jsonConfig; |
46 | - QString metodoDownload; | |
49 | + | |
50 | + QList<QMap<QString,QString> > metodosDownload; | |
47 | 51 | QMap<QString, QString> moduleMap; // key = hash md5, value = nome do modulo |
48 | 52 | |
49 | 53 | private slots: | ... | ... |
src/cacic_comm.h
... | ... | @@ -161,6 +161,19 @@ public: |
161 | 161 | url.setScheme("ftp"); |
162 | 162 | url.setPath(path); |
163 | 163 | |
164 | + return false; | |
165 | + } | |
166 | + | |
167 | + bool ftpDownload(QString urlServer, QString path){ | |
168 | + QUrl url(urlServer); | |
169 | + url.setScheme("ftp"); | |
170 | + url.setPath(path); | |
171 | + | |
172 | + return false; | |
173 | + } | |
174 | + | |
175 | + bool httpDownload( QString urlServer, QString path ){ | |
176 | + return false; | |
164 | 177 | } |
165 | 178 | |
166 | 179 | QString getUrlSsl (){ | ... | ... |