Skip to content
Snippets Groups Projects
Commit 28db1dda authored by Lais Frigerio's avatar Lais Frigerio
Browse files

Install agent in opt directory

parent e68da984
No related branches found
No related tags found
2 merge requests!51Merge development to master,!45Issue SCRUM#66 - Install agent in opt directory
Pipeline #
...@@ -16,4 +16,5 @@ const std::string url = "http://localhost:3001"; ...@@ -16,4 +16,5 @@ const std::string url = "http://localhost:3001";
int check_update(); int check_update();
bool compare_version(Json::Value*, Agent*, std::string, std::fstream*); bool compare_version(Json::Value*, Agent*, std::string, std::fstream*);
bool copy_zip_file_to_destiny_directory(Json::Value*, std::fstream*, Agent*, boost::filesystem::path);
bool download(Conf, Agent*, Json::Value*, std::fstream*); bool download(Conf, Agent*, Json::Value*, std::fstream*);
\ No newline at end of file
...@@ -39,5 +39,3 @@ class Agent{ ...@@ -39,5 +39,3 @@ class Agent{
void prefixISet(); void prefixISet();
}; };
bool copy_file_to_agent_home(Json::Value*, std::fstream*, Agent*, boost::filesystem::path);
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#endif #endif
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <curl/curl.h> #include <curl/curl.h>
#include <agent/get.h>
#include <json/json.h> #include <json/json.h>
class Curl{ class Curl{
......
...@@ -23,7 +23,7 @@ class Unzip{ ...@@ -23,7 +23,7 @@ class Unzip{
public: public:
void setError(int); void setError(int);
void setUF(); void setUF();
void setPathToUnzipAgent(std::string, int); void setPathToUnzipAgent(std::string);
void setPathToUnzipAgent(std::string, int, std::string); void setPathToUnzipAgent(std::string, int, std::string);
void setZipFileName(const char*); void setZipFileName(const char*);
......
...@@ -134,7 +134,6 @@ int check_update() { ...@@ -134,7 +134,6 @@ int check_update() {
bool compare_version(Json::Value* data, Agent *agent, std::string version, bool compare_version(Json::Value* data, Agent *agent, std::string version,
std::fstream* logFile) { std::fstream* logFile) {
return true;
if (!strcmp(version.c_str(), "")) { if (!strcmp(version.c_str(), "")) {
agent->setMessage(__DATE__ + agent->setMessage(__DATE__ +
std::string(" - ERROR: Server returned an empty version string.")); std::string(" - ERROR: Server returned an empty version string."));
...@@ -162,6 +161,38 @@ bool compare_version(Json::Value* data, Agent *agent, std::string version, ...@@ -162,6 +161,38 @@ bool compare_version(Json::Value* data, Agent *agent, std::string version,
} }
} }
/*---------------------------------------------------------------------
* Function: copy_file_to_agent_home
* Summary: Copy the agent zip file from tmp directory to opt directory.
*/
bool copy_zip_file_to_destiny_directory(Json::Value* data, std::fstream*
logFile, Agent* agent, boost::filesystem::path source) {
boost::filesystem::path destination = agent->getDirInstall();
boost::filesystem::create_directory(destination);
try {
for (boost::filesystem::directory_iterator file(source);
file != boost::filesystem::directory_iterator(); ++file) {
boost::filesystem::path current(file->path());
if (!boost::filesystem::is_directory(current)) {
// Found file: Copy
boost::filesystem::copy_file(current,
destination / current.filename());
}
}
} catch (const boost::filesystem::filesystem_error& e) {
std::string message = __DATE__ + std::string(" - ERROR: Failed to ")
+ std::string("copy the agent zipfile from tmp directory") +
std::string(" to destiny directory.");
*logFile << message << std::endl;
(*data)["error"]["failed_to_copy_agent_zip"] = message;
return false;
}
return true;
}
/*--------------------------------------------------------------------- /*---------------------------------------------------------------------
* Function: download * Function: download
* Summary: Make agent download. * Summary: Make agent download.
...@@ -170,7 +201,7 @@ bool compare_version(Json::Value* data, Agent *agent, std::string version, ...@@ -170,7 +201,7 @@ bool compare_version(Json::Value* data, Agent *agent, std::string version,
bool download(Conf conf, Agent* agent, Json::Value* data, bool download(Conf conf, Agent* agent, Json::Value* data,
std::fstream* logFile) { std::fstream* logFile) {
Curl curl; Curl curl;
if (curl.downloadAgent(data, "file:///home/lais/Documentos/xxx.zip", if (curl.downloadAgent(data, conf.getWebService(),
logFile, agent)) logFile, agent))
return true; return true;
return false; return false;
......
...@@ -103,36 +103,3 @@ void Agent::prefixISet() { ...@@ -103,36 +103,3 @@ void Agent::prefixISet() {
throw " ERROR: Prefix not set. "; throw " ERROR: Prefix not set. ";
} }
/*---------------------------------------------------------------------
* Function: copy_file_to_agent_home
* Summary: Copy the agent zip file from tmp directory to agent home.
*/
bool copy_file_to_agent_home(Json::Value* data, std::fstream* logFile,
Agent* agent, boost::filesystem::path source) {
const char* home = getenv("HOME");
boost::filesystem::path destination = agent->getDirInstall();
boost::filesystem::create_directory(destination);
try {
for (boost::filesystem::directory_iterator file(source);
file != boost::filesystem::directory_iterator(); ++file) {
boost::filesystem::path current(file->path());
if (!boost::filesystem::is_directory(current)) {
// Found file: Copy
boost::filesystem::copy_file(current,
destination / current.filename());
}
}
} catch (const boost::filesystem::filesystem_error& e) {
std::string message = __DATE__ + std::string(" - ERROR: Failed to ")
+ std::string("copy the agent zip file from tmp directory") +
std::string(" to agent home.");
*logFile << message << std::endl;
(*data)["error"]["failed_to_copy_agent_zip"] = message;
return false;
}
return true;
}
...@@ -65,8 +65,8 @@ bool Curl::downloadAgent(Json::Value* data, std::string url, ...@@ -65,8 +65,8 @@ bool Curl::downloadAgent(Json::Value* data, std::string url,
} }
/* Create the agent zip tmp file into test_root */ /* Create the agent zip tmp file into test_root */
boost::filesystem::path test_file(test_root / "7zip.zip"); agent->setZipName("agent.zip");
agent->setZipName("7zip.zip"); boost::filesystem::path test_file(test_root / agent->getZipName());
FILE* fp = fopen(test_file.c_str(), "wb"); FILE* fp = fopen(test_file.c_str(), "wb");
...@@ -112,8 +112,9 @@ bool Curl::downloadAgent(Json::Value* data, std::string url, ...@@ -112,8 +112,9 @@ bool Curl::downloadAgent(Json::Value* data, std::string url,
} }
fclose(fp); fclose(fp);
agent->setDirInstall("/opt/agent"); agent->setDirInstall("/opt/agentc3sl");
bool err = copy_file_to_agent_home(data, logFile, agent, test_root); bool err = copy_zip_file_to_destiny_directory(data, logFile, agent,
test_root);
// remove agent tmp directory // remove agent tmp directory
boost::uintmax_t res = boost::filesystem::remove_all(test_root); boost::uintmax_t res = boost::filesystem::remove_all(test_root);
...@@ -130,7 +131,8 @@ bool Curl::downloadAgent(Json::Value* data, std::string url, ...@@ -130,7 +131,8 @@ bool Curl::downloadAgent(Json::Value* data, std::string url,
if (!err) if (!err)
return false; return false;
unzip.setZipFileName((agent->getDirInstall()).c_str()); unzip.setZipFileName((agent->getDirInstall() + std::string("/")
+ agent->getZipName()).c_str());
if (!unzip.unzip(data, logFile, agent)) if (!unzip.unzip(data, logFile, agent))
return false; return false;
......
...@@ -33,13 +33,8 @@ void Unzip::setUF() { ...@@ -33,13 +33,8 @@ void Unzip::setUF() {
this->uf = unzOpen(this->zipFileName); this->uf = unzOpen(this->zipFileName);
} }
void Unzip::setPathToUnzipAgent(std::string p, int tot) { void Unzip::setPathToUnzipAgent(std::string p) {
int i = p.size(), count = 0; this->path = p;
while (i-->-1)
if (p[i] == '/')
if (++count == tot)
break;
this->path = p.substr(0, i);
} }
void Unzip::setPathToUnzipAgent(std::string p, int tot, std::string plus) { void Unzip::setPathToUnzipAgent(std::string p, int tot, std::string plus) {
...@@ -198,7 +193,7 @@ int Unzip::makeDir(const char* newdir, Json::Value* data, ...@@ -198,7 +193,7 @@ int Unzip::makeDir(const char* newdir, Json::Value* data,
*/ */
bool Unzip::unzip(Json::Value* data, std::fstream* logFile, Agent* agent) { bool Unzip::unzip(Json::Value* data, std::fstream* logFile, Agent* agent) {
if (!zipFileNameIsSet()) { if (!this->zipFileNameIsSet()) {
agent->setMessage(__DATE__ + agent->setMessage(__DATE__ +
std::string(" - ERROR: Zip File name not setted.")); std::string(" - ERROR: Zip File name not setted."));
*logFile << agent->getMessage() << std::endl; *logFile << agent->getMessage() << std::endl;
...@@ -215,6 +210,8 @@ bool Unzip::unzip(Json::Value* data, std::fstream* logFile, Agent* agent) { ...@@ -215,6 +210,8 @@ bool Unzip::unzip(Json::Value* data, std::fstream* logFile, Agent* agent) {
} }
if (this->unzipAllFiles(data, logFile, agent)) { if (this->unzipAllFiles(data, logFile, agent)) {
this->setZipFileName((agent->getDirInstall() + std::string("/")
+ agent->getZipName()).c_str());
boost::filesystem::remove_all(this->getZipFileName()); boost::filesystem::remove_all(this->getZipFileName());
agent->setMessage(__DATE__ + agent->setMessage(__DATE__ +
std::string(" - SUCCESS: Agent extracted successfully.")); std::string(" - SUCCESS: Agent extracted successfully."));
...@@ -276,7 +273,6 @@ int Unzip::unzipCurrentFile(Json::Value* data, std::fstream* logFile, ...@@ -276,7 +273,6 @@ int Unzip::unzipCurrentFile(Json::Value* data, std::fstream* logFile,
void* buf = NULL; void* buf = NULL;
int errclose = UNZ_OK; int errclose = UNZ_OK;
this->setError(unzGetCurrentFileInfo(this->uf, &file_info, filename_inzip, this->setError(unzGetCurrentFileInfo(this->uf, &file_info, filename_inzip,
sizeof(filename_inzip), NULL, 0, NULL, 0)); sizeof(filename_inzip), NULL, 0, NULL, 0));
...@@ -288,8 +284,8 @@ int Unzip::unzipCurrentFile(Json::Value* data, std::fstream* logFile, ...@@ -288,8 +284,8 @@ int Unzip::unzipCurrentFile(Json::Value* data, std::fstream* logFile,
return this->getError(); return this->getError();
} }
this->setPathToUnzipAgent(__FILE__, 6, "/aaagent/" + this->setPathToUnzipAgent(agent->getDirInstall() + std::string("/") +
std::string(filename_inzip)); std::string(filename_inzip));
p = filename_withoutpath = filename_inzip; p = filename_withoutpath = filename_inzip;
while (*p != 0) { while (*p != 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment