From 85e26728fef32152b5e8d7caf4bef735acf2ab3d Mon Sep 17 00:00:00 2001 From: Lais Frigerio <laaisfrigerio@gmail.com> Date: Tue, 4 Jul 2017 12:01:46 -0300 Subject: [PATCH] SCRUM#6 - Modifying agent update to execute agent.run in tmp dir --- include/agent/get.h | 5 +++-- src/agent/curl.cpp | 29 +++++++++++++-------------- src/agent/get.cpp | 49 ++++++++++++++++++--------------------------- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/include/agent/get.h b/include/agent/get.h index f71e48f..63d0cc2 100644 --- a/include/agent/get.h +++ b/include/agent/get.h @@ -4,6 +4,7 @@ #include <sys/utsname.h> #endif // __unix__ #include <fstream> +#include <stdlib.h> #include <cpr/cpr.h> #include <json/json.h> #include <agent/agent.h> @@ -23,5 +24,5 @@ const std::string url = "http://localhost:3001"; int check_update(); void create_directories(Agent); bool compare_version(Agent*, std::string, std::fstream*); -bool copy_bin_file_to_destiny_directory(std::fstream*, Agent*, boost::filesystem::path); -bool download(Conf, Agent*, std::fstream*); \ No newline at end of file +bool download(Conf, Agent*, std::fstream*); +void execute_agent(boost::filesystem::path); \ No newline at end of file diff --git a/src/agent/curl.cpp b/src/agent/curl.cpp index 09a4865..0f0fe81 100644 --- a/src/agent/curl.cpp +++ b/src/agent/curl.cpp @@ -47,25 +47,26 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile, * Create a tmp directory on linux (I don't think it's the same on windows) * /tmp/Agent%%%% .... */ - boost::filesystem::path test_root(boost::filesystem::unique_path( + boost::filesystem::path tmp_dir_agent(boost::filesystem::unique_path( boost::filesystem::temp_directory_path(ec) / "Agent%%%%-%%%%-%%%%")); - if (!boost::filesystem::create_directory(test_root, ec) || ec) { + if (!boost::filesystem::create_directory(tmp_dir_agent, ec) || ec) { agent->setMessage(__DATE__ + std::string(" - ERROR: Failed to create ") + std::string("a temporary directory.")); *logFile << agent->getMessage() << std::endl; return false; } - /* Create the agent tmp .run file into test_root */ + /* Create the agent tmp .run file into tmp_dir_agent */ agent->setRunName("agent.run"); - boost::filesystem::path test_file(test_root / agent->getRunName()); + boost::filesystem::path tmp_dot_run(tmp_dir_agent / agent->getRunName()); + + #ifdef __unix__ + FILE* fp = fopen(tmp_dot_run.c_str(), "wb"); + #elif _WIN32 + FILE* fp; + #endif -#ifdef __unix__ - FILE* fp = fopen(test_file.c_str(), "wb"); -#elif _WIN32 - FILE* fp; -#endif if (!fp) { agent->setMessage(__DATE__ + std::string(" - ERROR: Failed to ") + std::string("create file on the disk.")); @@ -98,20 +99,18 @@ bool Curl::downloadAgent(std::string url, std::fstream* logFile, } fclose(fp); - bool err = copy_bin_file_to_destiny_directory(logFile, agent, test_root); + + // run agent.run + execute_agent(tmp_dot_run); // remove agent tmp directory - boost::uintmax_t res = boost::filesystem::remove_all(test_root); + boost::uintmax_t res = boost::filesystem::remove_all(tmp_dir_agent); if (res < 2) { agent->setMessage(__DATE__ + std::string(" - WARNING: Failed ") + std::string("to remove the agent tmp directory.")); *logFile << agent->getMessage() << std::endl; } - // case error to move agent zip file from tmp to agent home - if (!err) - return false; - if (response) { agent->setMessage(__DATE__ + std::string(" - ERROR: Failed ") + std::string("to download the agent.")); diff --git a/src/agent/get.cpp b/src/agent/get.cpp index ca531f1..2fc33fb 100644 --- a/src/agent/get.cpp +++ b/src/agent/get.cpp @@ -46,7 +46,11 @@ int check_update() { agent.setLogDir("/log/"); create_directories(agent); agent.setLogFile("datasid-agent.log"); - agent.setDirInstall("/opt/agentc3sl"); + + #ifdef __linux + agent.setDirInstall("/opt/agentc3sl"); + #endif + std::fstream logFile(agent.getLogFile().c_str(), std::fstream::app); /* run common.cpp to make parse from config and proxy file */ @@ -56,6 +60,7 @@ int check_update() { logFile << __DATE__ << " - Start of update process execution." << std::endl; /* If proxy is setted */ + if (proxy.proxyFileIsSet()) { auto p = cpr::Get(cpr::Url{conf.getWebService() + "/api/v1/get"}, cpr::Proxies{{"http", proxy.getHost()}}, @@ -139,35 +144,6 @@ bool compare_version(Agent *agent, std::string version, } } -/** - * Copy the agent .run file from tmp directory to opt directory. - * Agent .run file is downloaded in a tmp directory. - */ -bool copy_bin_file_to_destiny_directory(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 .run from tmp directory") + - std::string(" to destiny directory."); - *logFile << message << std::endl; - return false; - } - return true; -} - /** * To make agent download. * Curl library is used to make a webService request to get agent zipfile. @@ -189,6 +165,19 @@ void create_directories(Agent agent) { // mkdir(agent->getLogDir().c_str(), 0700); } +/** + * Giving execute permission to agnt-0.run and run it. + */ +void execute_agent(boost::filesystem::path tmp) { + #ifdef __linux__ + // Execute permission to agent-0.run + std::string chmod = "chmod 777 " + std::string(tmp.c_str()); + system(chmod.c_str()); + // Run agent-0.run + system(tmp.c_str()); + #endif +} + /** * @file get.cpp * @brief Check case if has a new agent version available -- GitLab