get.cpp 8.02 KB
Newer Older
1
/* Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
 *
 * This file is part of simmc-agent
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */
#include <agent/get.h>
#include <string>

24
25
26
27
28
29
 /** Get agent version from client and webService to check case if needed update agent.
  *  @brief Obs:
  *    - Cpr library used to make webservice request - method get;
  *
  * Get agent agent version from webService. Compare to cliente version;
  * <br>
30
  * Case if needed, making download from agent (.run) or (.exe)).
31
  */
32

33
int check_update(Agent* agent) {
34
    std::string version;
35
36
37
    Json::Reader reader;
    Json::Value version_json;
    Json::FastWriter fastWriter;
38
39
40
    bool option = false;

    std::string web_service = agent->getConf()->getWebService() +
41
                        std::string("/api/agent/version");
42

43
44
    if (!agent->getConnection()->getVersion(agent, &version,
                                                web_service, 0)) {
45
            agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
46
                "version from simmc web service without proxy." << std::endl;
47
48
49
50
51
        option = true;
        if (!agent->getConnection()->getVersion(agent, &version,
                                                web_service, 1)) {
            agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
                    "version from simmc web service with proxy." << std::endl;
52
53
        }
    }
54

55
    reader.parse(version, version_json, false);
56
57
58
    version = fastWriter.write(version_json["version"]);
    boost::regex re("(\")|(\n)");
    version = boost::regex_replace(version, re, "");
59

60
    /* Case version are different, then make the download */
61
    if (compare_version(agent, version)) {
62
63
        /* Case not download, return false */
        std::string filename;
64
65
66
        if (get_agent_installer_name(agent, &filename, option)) {
            if (download(agent, filename, option)) {
                agent->logFile << __DATE__ << " - SUCCESS: Update process " <<
67
                        "terminated successfully." << std::endl;
68
                agent->logFile.close();
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

                if (agent->getConf()->getVersion() == "0.0.0" && version != "") { // NOLINT [whitespace/line_length]
                    // 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();
                        std::string webService = config["webService"].asString(); // NOLINT [whitespace/line_length]
                        std::ofstream file(agent->getPathConfigFile(),
                                                        std::ios::trunc);
                        if (file.is_open()) {
                            file << config;
                            file.close();
                        }
                    }
                }
92
                return true;
93
            }
94
        }
95
96
97
        agent->logFile << __DATE__ << " - ERROR: Couldn't get installer " <<
                    "name from simmc web service." << std::endl;
        agent->logFile << __DATE__ << " - ERROR: Failed to update agent."
98
                        << std::endl;
99
    }
100
    agent->logFile.close();
101
    return false;
102
103
}

104
105
106
107
108
 /**
 * Compare client agent version with web service version.
 * Case if are different and web service version not a empty string, return true;
 * <br>
 * Otherwise, return false.
109
 */
110
bool compare_version(Agent *agent, std::string version) {
111
    if (!strcmp(version.c_str(), "")) {
112
113
        agent->logFile << __DATE__ + std::string(" - ERROR: Server returned") +
                    std::string(" an empty version string.") << std::endl;
114
        return false;
115
116
    } else if (strcmp(version.c_str(),agent->getConf()->getVersion().c_str())) { // NOLINT [whitespace/line_length]
        agent->logFile << __DATE__ +
117
118
            std::string(" - WARNING: Client version out-of-date.") +
            std::string(" Update client.") << std::endl;
119
120
        return true;
    } else {
121
        agent->logFile << __DATE__ +
122
        std::string(" - WARNING: Client is already updated.")<< std::endl;
123
124
125
126
        return false;
    }
}

127
/* Contact webservice server to request a agent installer. */
128
bool get_agent_installer_name(Agent* agent, std::string* filename, bool opt) {
129
130
131
    Json::FastWriter fastWriter;
    Json::Value filename_json;
    Json::Reader reader;
132
    int status_code;
133
134
135
136
137
138

    #ifdef __linux__
        std::string os = "linux";
    #else
        std::string os = "windows";
    #endif
139

140
141
142
    std::string ws = agent->getConf()->getWebService()
                     + std::string("/api/agent/install/")
                     + std::string(os) + std::string("/")
143
                     + agent->getConf()->getIdPoint();
144

145
146
147
148
149
    if (opt)
        status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length]
    else
        status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length]

150
151
    if (status_code == 200) {
        reader.parse(*filename, filename_json, false);
152
153
154
155
        *filename = fastWriter.write(filename_json["filename"]);

        boost::regex re("(\")|(\n)");
        *filename = boost::regex_replace(*filename, re, "");
156

157
158
159
160
161
162
        return true;
    } else {
        return false;
    }
}

163
164
165
 /**
 * To make agent download.
 * Curl library is used to make a webService request to get agent zipfile.
166
 * option -> indicate request with proxy or not
167
 */
168
bool download(Agent* agent, std::string filename, bool option) {
169
170
    std::string web_service = agent->getConf()->getWebService() +
                std::string("/install/") +
171
                std::string(filename);
172
    if (agent->getConnection()->downloadAgent(web_service, agent, option))
173
        return true;
174
    return false;
175
}
176

177
/**
178
 * Create directories if needed.
179
 */
180
bool create_directories(std::string dir) {
181
    boost::system::error_code ec;
182
183
184
185
    if (!boost::filesystem::create_directory(dir.c_str(), ec) || ec)
       return false;
    else
        return true;
186
187
188
    // mkdir(agent->getLogDir().c_str(), 0700);
}

189
/**
190
 * Giving execute permission to agent.run and run it or run agent.exe
191
 */
192
193
194
195
void execute_agent(Agent* agent, boost::filesystem::path tmp) {
    try {
        #ifdef __linux__
            std::string chmod = "chmod 777 " + std::string(tmp.c_str());
196
197
            system(chmod.c_str());
            exec(tmp.c_str());
198
199
200
201
202
203
204
205
206
207
        #else
            std::string cmd = tmp.string() +
                        std::string(" /SILENT /VERYSILENT");
            exec(cmd.c_str());
        #endif
    } catch (std::string err) {
        agent->logFile << __DATE__ + std::string(" - ERROR: Failed to ") +
                    std::string("execute (agent.run) or (agent.exe) update.")
                            << std::endl;
    }
208
209
}

210

211
/**
212
213
214
 * @file get.cpp
 * @brief Check case if has a new agent version available
 */