Commit 24dcc1be authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch 'development' into 'master'

Versão 1.0.1

See merge request !112
parents 14f2641c 3de80dc0
Pipeline #20539 passed with stages
in 30 minutes and 40 seconds
......@@ -7,4 +7,4 @@ styleguide/*
Documentation/*
.vs/
bootstrap.log
.vscode
cache:
paths:
- build/Boost/src/Boost
stages:
- lint
- build
......@@ -29,7 +33,7 @@ compileUnix:
libudev-dev
- git submodule init
- git submodule update --init --recursive
- mkdir build
- mkdir -p build
- cd build
- cmake -DCURL_STATICLIB=ON ..
- make
......@@ -38,25 +42,54 @@ compileUnix:
- ./generate_agent.sh -idp=1234 -ws=www.C3Sl.ufpr.br -pch=mumm -pcp=6666 -pcu=ninguem -pcpasswd=123mudar -prj=simmc
- chmod +x agent-1234.run
compileWin:
compileWin32:
stage: build
artifacts:
paths:
- agent-1234.exe
- agent-32.exe
tags:
- windows
- inno
script:
- set PATH=%PATH%;C:\Program Files\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build
- call vcvarsall.bat x86
- echo "Estágio 'build'"
- dpkg --add-architecture i386
- apt-get update && apt-get install -y build-essential cmake git makeself
libudev-dev g++-mingw-w64-i686
- git submodule init
- git submodule update --init --recursive
- rd /s /q build
- call mkdir build
- mkdir -p build
- cd build
- cmake .. -G "NMake Makefiles"
- nmake
- cd ..
- iscc generate_agent.iss /Didp=1234 /Dws=www.C3Sl.ufpr.br /Dpch=mumm /Dpcp=6666 /Dpcu=ninguem /Dpcpasswd=123mudar /Dprj=simmc /Dversion=1.0.0
- cmake -D CMAKE_TOOLCHAIN_FILE=mingw_cross_toolchain_32-bit.cmake ..
- make
- su inno
- Xvfb :98 &
- export DISPLAY=:98
- cd /
- wine /home/inno/iscc ./builds/simmctic/agent/generate_agent.iss /Didp=1234 /Dws=www.C3Sl.ufpr.br /Dpch=mumm /Dpcp=6666 /Dpcu=ninguem /Dpcpasswd=123mudar /Dprj=simmc /Dversion=1.0.0
- mv ./builds/simmctic/agent/agent-1234.exe ./builds/simmctic/agent/agent-32.exe
compileWin64:
stage: build
artifacts:
paths:
- agent-64.exe
tags:
- inno
script:
- echo "Estágio 'build'"
- apt-get update && apt-get install -y build-essential cmake git makeself
libudev-dev g++-mingw-w64-x86-64
- git submodule init
- git submodule update --init --recursive
- mkdir -p build
- cd build
- cmake -D CMAKE_TOOLCHAIN_FILE=mingw_cross_toolchain_64-bit.cmake ..
- make
- su inno
- Xvfb :98 &
- export DISPLAY=:98
- cd /
- wine /home/inno/iscc ./builds/simmctic/agent/generate_agent.iss /Didp=1234 /Dws=www.C3Sl.ufpr.br /Dpch=mumm /Dpcp=6666 /Dpcu=ninguem /Dpcpasswd=123mudar /Dprj=simmc /Dversion=1.0.0
- mv ./builds/simmctic/agent/agent-1234.exe ./builds/simmctic/agent/agent-64.exe
testDebian:
stage: test
......@@ -109,21 +142,7 @@ testOpensuse:
dependencies:
- compileUnix
testWin7:
stage: test
tags:
- windows
script:
- if exist C:\agentC3SL rmdir C:\agentC3SL /s /q
- agent-1234.exe /SILENT /VERYSILENT
- if not exist C:\agentC3SL exit 1;
- copy C:\jq-win32.exe .\test
- cd .\test
- C:\agentC3SL\bin\agent-v0.0.exe --once --print > test.json
- testWin.bat
- C:\agentC3SL\uninstall_agent.exe /SILENT /VERYSILENT
- dir /b /a "C:\agentC3SL\*" | >nul findstr "^" && (echo Agent has been uninstalled) || (exit 1);
dependencies:
- compileWin
# Optamos por remover os testes do windows, pois não conseguimos criar o runner, visto que sua
# criação iriam ter um impacto negativo em todos os demais runners.
# Caso se deseje testar o programa pelo windows existe o script test/testWin.bat, que ira realizar os testes,
# no ambiente windows.
......@@ -6,21 +6,34 @@ project( simmc-agent )
set ( VERSION_MAJOR 0 )
set ( VERSION_MINOR 0 )
# determines the thread library of the system
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
# cpr requires c++11
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
#
if(WIN32)
add_definitions( -DBOOST_ALL_NO_LIB )
set(Boost_USE_STATIC_LIBS ON)
add_definitions(-DCURL_STATICLIB)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_WIN32_WINNT=0x0501)
add_definitions(-D_WIN32_WINNT=0x0501)
set(CMAKE_USE_WIN32_THREADS_INIT 1)
set(Threads_FOUND TRUE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -lwinpthread -static-libstdc++ -static-libgcc")
else()
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpthread " )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
# src : main, collect functions + jsoncpp library
file ( GLOB SOURCES src/agent/*.cpp )
# src : curl requests
# options for cpr
......@@ -37,7 +50,6 @@ option( RUN_CURL_TESTS "Set to ON to run cURL tests." OFF )
option( CURL_STATICLIB "Set to ON to generated curl statically." ON )
add_subdirectory ( lib/cpr )
include_directories ( ${CPR_INCLUDE_DIRS} )
include_directories ( ${CURL_INCLUDE_DIRS} )
# headers
......@@ -53,11 +65,14 @@ include ( "modules/boost.cmake" )
# compile
set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" )
add_executable ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES} )
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CPR_LIBRARIES} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} )
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} ${CMAKE_THREAD_LIBS_INIT})
add_dependencies ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} Boost )
add_dependencies ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} Boost)
if ( UNIX )
# libudev
target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} udev )
endif ()
......@@ -11,7 +11,7 @@ Using *Linux* as default
- c++ compiler
- cmake
- libudev
- make-self
- makeself
<a name="HeaderBuildingAndCompiling"></a>
......
......@@ -3,5 +3,9 @@
"project" : "%s",
"proxyConf" : "%s",
"version" : "%s",
"webService" : "%s"
}
\ No newline at end of file
"webService" : "%s",
"agent_send_inventory": "%s",
"agent_send_net_band" : "%s",
"agent_send_user_hist": "%s",
"agent_send_database" : "%s"
}
......@@ -88,7 +88,7 @@ Name: "{app}\tmp"
[Run]
; After install agent on windows machine, schedule a task to run every login
Filename: "schtasks.exe"; \
Parameters : "/Create /SC ONLOGON /TN Start_agent /TR ""'{app}\bin\agent-v0.0.exe'"""; \
Parameters : "/Create /SC ONLOGON /TN Start_agent /TR ""'{app}\bin\agent-v0.0.exe'"" /F"; \
Flags: runascurrentuser nowait;
; Rename uninstall .exe file
Filename: {cmd}; Parameters: "/C Move ""{app}\unins000.exe"" ""{app}\uninstall_agent.exe"""; Flags: RunHidden WaitUntilTerminated;
......@@ -159,7 +159,7 @@ procedure change_config_file();
var
key, value : string;
begin
key := '{#idp},conf/proxy-conf.json,{#ws},{#version},{#prj}';
key := '{#idp},{#prj},conf/proxy-conf.json,{#version},{#ws}';
value := '%d,%s,%s,%s,%s';
read_file(WizardDirValue() + '\conf\datasid-conf.json', key, value);
end;
......
......@@ -31,6 +31,11 @@ usr=""
passwd=""
destination="./"
project="simmc"
asi="true" #agent_send_inventory
asnb="true" #agent_send_net_band
asuh="true" #agent_send_user_hist
asd="false" #agent_send_database
dir=$(dirname $0)
cd $dir
......@@ -47,9 +52,13 @@ for i in $@; do
;;
-h|--help)
echo "Usage: ";echo "generate agent.run with the desired setup";echo "options:"
echo " -idp=N: set idpoint as N";echo" -ws=s: set webService as s "
echo " -idp=N: set idpoint as N";echo " -ws=s: set webService as s "
echo " -pch=s: set proxyConf host as s";echo " -pcp=s: set proxyConf port as s"
echo " -pcu=s: set proxyConf user as s";echo " -pcpssd=s: set proxyConf password as s"
echo " -ver=s: set version as s";echo " -prj=s: set project name"
echo " -asi=true|false: set agent_send_inventory as true or false";echo " -asnb=true|false: set agent_send_net_band as true or false"
echo " -asuh=true|false: set agent_send_user_hist as true or false";echo " -asd=true|falses: set agent_send_database as true or false"
echo " -dest=d:set the agent directory as d"
echo " -h or --help: show this message"
exit 1
;;
......@@ -81,6 +90,22 @@ for i in $@; do
project="${i#*=}"
shift
;;
-asi=*)
asi="${i#*=}"
shift
;;
-asnb=*)
asnb="${i#*=}"
shift
;;
-asuh=*)
asuh="${i#*=}"
shift
;;
-asd=*)
asd="${i#*=}"
shift
;;
*)
echo "wrong usage\ntry -h or --help for help"
exit 1
......@@ -94,15 +119,13 @@ cp bin/agent-v0.0 $idPoint/agentC3SL/
mkdir $idPoint/agentC3SL/conf
cd $idPoint/agentC3SL/conf
touch datasid-conf.json
touch proxy.json
touch proxy-conf.json
tmp=$(cat ../../../conf/proxy-conf.json)
printf "$tmp\n" $host $port $usr $passwd > proxy.json
printf "$tmp\n" "$host" "$port" "$usr" "$passwd" > proxy-conf.json
tmp=$(cat ../../../conf/datasid-conf.json)
printf "$tmp\n" $idPoint $project conf/proxy-conf.json $version $webService > datasid-conf.json
printf "$tmp\n" "$idPoint" "$project" "conf/proxy-conf.json" "$version" "$webService" "$asi" "$asnb" "$asuh" "$asd" > datasid-conf.json
cd ../../..
......
......@@ -10,6 +10,8 @@
#include <agent/get.h>
#include <json/json.h>
namespace fs = boost::filesystem;
/**
* @file connection.h
* @brief This file has Curl class and the purpose is to make a get or post request to web service and download the agent zip file
......@@ -26,11 +28,11 @@ class Connection{
long httpCode = 0; /*!< Code return from webSerice (200, 404, 502 and so on) */
public:
bool downloadAgent (std::string, Agent*, int);
bool getVersionByProxy(Agent*, std::string*, std::string);
int getInstallerNameByProxy(std::string, Agent*, std::string*);
void setCurlOption(std::string, Agent*, std::string*);
int sendJsonByProxy(Agent*, std::string, int);
bool downloadAgent (std::string, Agent*, bool);
bool getVersion(Agent*, std::string*, std::string, bool);
int getInstallerName(std::string, Agent*, std::string*, bool);
void setCurlOption(std::string, Agent*, std::string*, bool);
int sendJson(Agent*, std::string, int, bool);
/** Default callback function that is necessary to make a curl request.
* @brief get agent binary (agent.run or agent.exe)
......
......@@ -2,7 +2,6 @@
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cpr/cpr.h>
#include <json/json.h>
#include <agent/common.h>
#include <agent/connection.h>
......@@ -24,6 +23,6 @@ const std::string url = "http://localhost:3001";
int check_update(Agent*);
bool create_directories(std::string);
bool compare_version(Agent*, std::string);
bool download(Agent*, std::string, int);
bool download(Agent*, std::string, bool);
void execute_agent(Agent*, boost::filesystem::path);
bool get_agent_installer_name(Agent*, std::string*, int);
bool get_agent_installer_name(Agent*, std::string*, bool);
......@@ -16,7 +16,7 @@
#include <agent/get_user_count.h>
#include <json/json.h>
/**
/**
* @file inventory.h
* @brief Collect all info from hardware, like processor, memory, mac adress, disks, machine type, os distro and so on.
* - The data collected are saved in a json object:
......@@ -34,4 +34,4 @@
* - Mac address
*/
Json::Value get_inventory(Agent*);
\ No newline at end of file
Json::Value get_inventory(Agent*);
......@@ -17,7 +17,6 @@
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/filesystem/fstream.hpp>
#include <cpr/cpr.h>
namespace pt = boost::posix_time;
namespace po = boost::program_options;
......@@ -32,13 +31,13 @@ namespace fs = boost::filesystem;
std::string path_to_agent = "/opt/agentC3SL/"; // NOLINT(runtime/string)
//std::string path_to_agent_conf = // NOLINT(runtime/string)
//"/opt/agentC3SL/conf/"; // NOLINT(runtime/string)
std::string path_to_agent_conf = "/home/teste/Imagens/agent/conf/";
std::string path_to_agent_conf = "/opt/agentC3SL//conf/";
#else
// Temporary... Define agent path
std::string path_to_agent_tmp = "C:\\agentC3SL\\tmp\\";
std::string path_to_agent = "C:\\agentC3SL\\";
std::string path_to_agent_conf = // NOLINT(runtime/string)
"C:\\agentC3SL\\teste\\"; // NOLINT(runtime/string)
"C:\\agentC3SL\\conf\\"; // NOLINT(runtime/string)
#endif
bool agent_send_inventory = false;
......
......@@ -9,6 +9,7 @@
#include <agent/get_time.h>
#include <agent/helper.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <agent/agent.h>
/**
* @file network_bandwidth.h
......@@ -27,4 +28,4 @@
*/
std::string get_cutted_line(std::string, int);
Json::Value get_net_bandwidth();
Json::Value get_net_bandwidth(Agent*);
#pragma once
#include <iostream>
#include <cpr/cpr.h>
#include <json/json.h>
#include <agent/agent.h>
#include <agent/connection.h>
......
......@@ -20,17 +20,18 @@
# USA.
cd ..
mkdir tmp
cd tmp
temp_dir=$(mktemp -d)
if test -d /opt/agentC3SL; then
cp /opt/agentC3SL/conf/* . && rm ../agentC3SL/conf/* && mv ./* ../agentC3SL/conf/
cp /opt/agentC3SL/conf/* "$temp_dir" && rm conf/* && mv "$temp_dir"/* conf/
fi
cd ..
rm -rf tmp
rm -rf "$temp_dir"
rm -rf /opt/agentC3SL
cp -r agentC3SL /opt/
cd ..
mv agentC3SL /opt/
cd /opt/agentC3SL
if ! (crontab -l | grep -q agentC3SL) ;then
......
......@@ -6,7 +6,6 @@ SET ( CMAKE_C_COMPILER i686-w64-mingw32-gcc-posix )
SET ( CMAKE_CXX_COMPILER i686-w64-mingw32-g++-posix )
SET ( CMAKE_RC_COMPILER i686-w64-mingw32-windres )
SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWIN32" )
SET ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -lwinpthread" )
SET ( CROSS_STATIC "iphlpapi" )
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32 )
......
......@@ -6,9 +6,7 @@ SET ( CMAKE_C_COMPILER x86_64-w64-mingw32-gcc-posix )
SET ( CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++-posix )
SET ( CMAKE_RC_COMPILER x86_64-w64-mingw32-windres )
SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWIN32" )
SET ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -lwinpthread" )
SET ( CROSS_STATIC "iphlpapi" )
# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32 )
# adjust the default behaviour of the FIND_XXX() commands:
......
......@@ -28,8 +28,6 @@ if (NOT Boost)
PREFIX Boost
GIT_REPOSITORY ${BOOST_URL}
GIT_SUBMODULES libs/asio libs/date_time libs/config libs/core libs/detail libs/io libs/iterator libs/predef libs/preprocessor libs/smart_ptr libs/throw_exception libs/system libs/filesystem libs/integer tools/build tools/bcp libs/serialization libs/interprocess libs/tokenizer libs/algorithm libs/program_options libs/regex libs/static_assert libs/thread libs/utility libs/numeric libs/range libs/array libs/bind libs/concept_check libs/lexical_cast libs/math libs/functional libs/assert libs/type_traits libs/mpl libs/move libs/container libs/any libs/type_index libs/function
UPDATE_COMMAND ./bootstrap.sh --with-libraries=filesystem,system,regex,program_options --includedir=${CMAKE_BINARY_DIR}/include/
CONFIGURE_COMMAND ./b2 headers
......@@ -42,8 +40,6 @@ if (NOT Boost)
PREFIX Boost
GIT_REPOSITORY ${BOOST_URL}
GIT_SUBMODULES libs/asio libs/config libs/core libs/date_time libs/detail libs/io libs/iterator libs/predef libs/preprocessor libs/smart_ptr libs/throw_exception libs/system libs/filesystem libs/integer tools/build tools/bcp libs/serialization libs/interprocess libs/tokenizer libs/algorithm libs/program_options libs/regex libs/static_assert libs/thread libs/utility libs/numeric libs/range libs/array libs/bind libs/concept_check libs/lexical_cast libs/math libs/functional libs/assert libs/type_traits libs/mpl libs/move libs/container libs/any libs/type_index libs/function libs/chrono libs/ratio libs/intrusive libs/spirit libs/winapi libs/tuple libs/exception libs/optional
UPDATE_COMMAND ./bootstrap.sh --without-icu --with-libraries=filesystem,system,regex,program_options --includedir=${CMAKE_BINARY_DIR}/include/ && ${CMAKE_BINARY_DIR}/../configure_boost_crosscompile.sh ${CMAKE_CXX_COMPILER}
CONFIGURE_COMMAND ./b2 headers
......@@ -57,8 +53,6 @@ if (NOT Boost)
PREFIX Boost
GIT_REPOSITORY ${BOOST_URL}
GIT_SUBMODULES libs/asio libs/config libs/core libs/date_time libs/detail libs/io libs/iterator libs/predef libs/preprocessor libs/smart_ptr libs/throw_exception libs/system libs/filesystem libs/integer tools/build tools/bcp libs/serialization libs/interprocess libs/tokenizer libs/algorithm libs/program_options libs/regex libs/static_assert libs/thread libs/utility libs/numeric libs/range libs/array libs/bind libs/concept_check libs/lexical_cast libs/math libs/functional libs/assert libs/type_traits libs/mpl libs/move libs/container libs/any libs/type_index libs/function libs/chrono libs/ratio libs/intrusive libs/spirit libs/winapi libs/tuple libs/exception libs/optional
UPDATE_COMMAND ./bootstrap.bat --with-toolset=msvc-14.1 --with-libraries=filesystem,system,regex,program_options,chrono
CONFIGURE_COMMAND ./b2 headers
......
......@@ -31,7 +31,7 @@
* @param agent object agent
* @param option use proxy or not (option == 0 - use proxy)
*/
bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
bool Connection::downloadAgent(std::string url, Agent* agent, bool option) {
curl = curl_easy_init();
if (!curl) {
agent->logFile << __DATE__ + std::string(" - ERROR: Curl ") +
......@@ -44,10 +44,11 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
* Create a tmp directory on linux (I don't think it's the same on windows)
* /tmp/Agent%%%% ....
*/
boost::filesystem::path tmp_dir_agent(boost::filesystem::unique_path(
boost::filesystem::temp_directory_path(ec) / "Agent%%%%-%%%%-%%%%"));
if (!boost::filesystem::create_directory(tmp_dir_agent, ec) || ec) {
fs::path tmp_dir_agent(fs::unique_path(fs::temp_directory_path(ec)
/ "Agent%%%%-%%%%-%%%%"));
if (!fs::create_directory(tmp_dir_agent, ec) || ec) {
agent->logFile << __DATE__ + std::string(" - ERROR: Failed to create ")
+ std::string("a temporary directory.") << std::endl;
return false;
......@@ -60,12 +61,12 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
#endif
/* Create the agent tmp (.run) or (.exe) file into tmp_dir_agent */
boost::filesystem::path file_installer(tmp_dir_agent / agent->getInstallerName()); // NOLINT [whitespace/line_length]
fs::path file_installer(tmp_dir_agent / agent->getInstallerName()); // NOLINT [whitespace/line_length]
#ifdef __unix__
FILE* fp = fopen(file_installer.c_str(), "wb");
#elif _WIN32
FILE* fp = fopen((const char*)file_installer.c_str(), "wb");
FILE* fp = fopen((const char*)(file_installer.string()).c_str(), "wb");
#endif
if (!fp) {
......@@ -79,13 +80,15 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
&Connection::callBackDownload);
if (option == 0) {
if (option) {
std::string::size_type sz; // NOLINT [runtime/int]
long port; // NOLINT [runtime/int]
try {
port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
} catch (std::invalid_argument& ex) {
agent->logFile << __DATE__ + std::string(" - ERROR: ") +
std::string(ex.what()) << std::endl;
port = 0;
}
......@@ -99,7 +102,7 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
}
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
response = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
......@@ -117,6 +120,12 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
return false;
}
if (httpCode == 404) {
agent->logFile << __DATE__ + std::string(" - ERROR: Agent installer") +
std::string(" not found at web service.") << std::endl;
return false;
}
fclose(fp);
// run agent.run on linux
......@@ -147,28 +156,34 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
* @param version pointer variable thats receive version or filename from web service.
*/
void Connection::setCurlOption(std::string url, Agent* agent,
std::string* out) {
std::string* out, bool opt) {
std::string::size_type sz; // NOLINT [runtime/int]
long port; // NOLINT [runtime/int]
try {
port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
} catch (std::invalid_argument& ex) {
agent->logFile << __DATE__ + std::string(" - ERROR: ") +
std::string(ex.what()) << std::endl;
port = 0;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
if (opt) {
curl_easy_setopt(curl, CURLOPT_PROXY,
agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
agent->getProxy()->getUser().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
agent->getProxy()->getPassword().c_str());
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Connection::callback);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
response = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
......@@ -181,9 +196,10 @@ void Connection::setCurlOption(std::string url, Agent* agent,
* @param url references to webService
* @param agent object agent
* @param version pointer variable thats receive version from web service;
* @param opt is proxy request or not
*/
bool Connection::getVersionByProxy(Agent* agent, std::string *version,
std::string url) {
bool Connection::getVersion(Agent* agent, std::string *version,
std::string url, bool opt) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
......@@ -193,8 +209,8 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version,
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
this->setCurlOption(url, agent, version);
if (httpCode == 200)
this->setCurlOption(url, agent, version, opt);
if (this->httpCode == 200)
return true;
else
return false;
......@@ -207,12 +223,13 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version,
* @param url references to webService
* @param agent object agent
* @param version pointer variable thats receive file name from web service;
* @param opt is proxy request or not
*/
int Connection::getInstallerNameByProxy(std::string url, Agent* agent,
std::string* filename) {
int Connection::getInstallerName(std::string url, Agent* agent,
std::string* filename, bool opt) {
curl = curl_easy_init();
this->setCurlOption(url, agent, filename);
return httpCode;
this->setCurlOption(url, agent, filename, opt);
return this->httpCode;
}
/** Contact webservice server using proxy to send json (inventory, net_usage or user_history)
......@@ -221,15 +238,19 @@ int Connection::getInstallerNameByProxy(std::string url, Agent* agent,
*
* @param agent object agent
* @param json references to json
* @param opt opt == 0 (send inventory) | opt == 2 (send net bandwidth) | otherwise (send user history)
* @param send send == 0 (send inventory) | send == 2 (send net bandwidth) | otherwise (send user history)
* @param opt is proxy request or not
*/
int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
int Connection::sendJson(Agent* agent, std::string json,
int send, bool opt) {
std::string::size_type sz; // NOLINT [runtime/int]
long port; // NOLINT [runtime/int]
try {
port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
} catch (std::invalid_argument& ex) {
agent->logFile << __DATE__ + std::string(" - ERROR: ") +
std::string(ex.what()) << std::endl;
port = 0;
}
......@@ -243,9 +264,9 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
// opt == 1 {send inventory}
// opt = 2 {send net bandwidth}
// otherwise, {send user history}
if (opt == 1)
if (send == 1)
base_url = base_url + std::string("/api/agent/collect/inventory");
else if (opt == 2)
else if (send == 2)
base_url = base_url + std::string("/api/agent/collect/net_usage");
else
base_url = base_url + std::string("/api/agent/collect/user_history");
......@@ -254,12 +275,15 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, base_url.c_str());
curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
agent->getProxy()->getUser().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
agent->getProxy()->getPassword().c_str());
if (opt) {
curl_easy_setopt(curl, CURLOPT_PROXY,
agent->getProxy()->getHost().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
agent->getProxy()->getUser().c_str());
curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
agent->getProxy()->getPassword().c_str());
}
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
......
......@@ -35,56 +35,28 @@ int check_update(Agent* agent) {
Json::Reader reader;
Json::Value version_json;
Json::FastWriter fastWriter;
int option = 1;
/* Contact webservice server to get client version. */
auto p = cpr::Get(cpr::Url{agent->getConf()->getWebService() +
"/api/agent/version"},
cpr::Header{{"content-type", "application/json"}});
// Check error with connection
if (p.status_code != 200) {
option = 0;
// Case if not connected before, try it with proxy
std::string web_service = agent->getConf()->getWebService() +
bool option = false;
std::string web_service = agent->getConf()->getWebService() +
std::string("/api/agent/version");
if (!agent->getConnection()->getVersionByProxy(agent, &version,
web_service)) {
if (!agent->getConnection()->getVersion(agent, &version,
web_service, 0)) {
agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
"version from simmc web service without proxy." << std::endl;
option = true;
if (!agent->getConnection()->getVersion(agent, &version,
web_service, 1)) {
agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
"version from simmc web service." << std::endl;
"version from simmc web service with proxy." << std::endl;
}
} else {
version = p.text;
}
// p.text - response from api
reader.parse(version, version_json, false);
version = fastWriter.write(version_json["version"]);
boost::regex re("(\")|(\n)");
version = boost::regex_replace(version, re, "");
if (agent->getConf()->getVersion() == "0.0.0" && version != "") {
// open version.json file and update to current version
Json::Value config;
Json::Reader reader;
std::string path = agent->getPathConfigFile();
std::ifstream version_file(path.c_str(), std::ofstream::binary);
if (version_file.is_open()) {
reader.parse(version_file, config , false);
version_file.close();
config["version"] = version;
std::string idPoint = config["idPoint"].asString();
std::string proxyConf = config["proxyConf"].asString();