|
1 | 1 | #include "cortex_upd_cmd.h" |
2 | | -#include "httplib.h" |
| 2 | +#include "cli/commands/server_start_cmd.h" |
3 | 3 | #include "server_stop_cmd.h" |
4 | 4 | #include "utils/archive_utils.h" |
| 5 | +#include "utils/curl_utils.h" |
5 | 6 | #include "utils/file_manager_utils.h" |
6 | | -#include "utils/json_helper.h" |
7 | 7 | #include "utils/logging_utils.h" |
8 | 8 | #include "utils/scope_exit.h" |
9 | 9 | #include "utils/system_info_utils.h" |
@@ -56,10 +56,12 @@ std::string GetNightlyInstallerName(const std::string& v, |
56 | 56 | // C:\Users\vansa\AppData\Local\Temp\cortex\cortex-windows-amd64-network-installer.exe |
57 | 57 | std::string GetInstallCmd(const std::string& exe_path) { |
58 | 58 | #if defined(__APPLE__) && defined(__MACH__) |
59 | | - return "sudo touch /var/tmp/cortex_installer_skip_postinstall_check && sudo installer " |
| 59 | + return "sudo touch /var/tmp/cortex_installer_skip_postinstall_check && sudo " |
| 60 | + "installer " |
60 | 61 | "-pkg " + |
61 | 62 | exe_path + |
62 | | - " -target / && sudo rm /var/tmp/cortex_installer_skip_postinstall_check"; |
| 63 | + " -target / && sudo rm " |
| 64 | + "/var/tmp/cortex_installer_skip_postinstall_check"; |
63 | 65 | #elif defined(__linux__) |
64 | 66 | return "echo -e \"n\\n\" | sudo SKIP_POSTINSTALL=true apt install -y " |
65 | 67 | "--allow-downgrades " + |
@@ -137,64 +139,56 @@ std::optional<std::string> CheckNewUpdate( |
137 | 139 | auto release_path = GetReleasePath(); |
138 | 140 | CTL_INF("Engine release path: " << host_name << release_path); |
139 | 141 |
|
140 | | - httplib::Client cli(host_name); |
141 | | - if (timeout.has_value()) { |
142 | | - cli.set_connection_timeout(*timeout); |
143 | | - cli.set_read_timeout(*timeout); |
| 142 | + auto url_obj = url_parser::Url{ |
| 143 | + .protocol = "https", .host = host_name, .pathParams = {}, // todo |
| 144 | + }; |
| 145 | + auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath()); |
| 146 | + if (res.has_error()) { |
| 147 | + CTL_INF("HTTP error: " << res.error()); |
| 148 | + return std::nullopt; |
144 | 149 | } |
145 | | - if (auto res = cli.Get(release_path)) { |
146 | | - if (res->status == httplib::StatusCode::OK_200) { |
147 | | - try { |
148 | | - auto get_latest = [](const Json::Value& data) -> std::string { |
149 | | - if (data.empty()) { |
150 | | - return ""; |
151 | | - } |
152 | 150 |
|
153 | | - if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { |
154 | | - for (const auto& d : data) { |
155 | | - if (auto tag = d["tag_name"].asString(); |
156 | | - tag.find(kBetaComp) != std::string::npos) { |
157 | | - return tag; |
158 | | - } |
159 | | - } |
160 | | - return data[0]["tag_name"].asString(); |
161 | | - } else { |
162 | | - return data["tag_name"].asString(); |
| 151 | + try { |
| 152 | + auto get_latest = [](const Json::Value& data) -> std::string { |
| 153 | + if (data.empty()) { |
| 154 | + return ""; |
| 155 | + } |
| 156 | + |
| 157 | + if (CORTEX_VARIANT == file_manager_utils::kBetaVariant) { |
| 158 | + for (const auto& d : data) { |
| 159 | + if (auto tag = d["tag_name"].asString(); |
| 160 | + tag.find(kBetaComp) != std::string::npos) { |
| 161 | + return tag; |
163 | 162 | } |
164 | | - return ""; |
165 | | - }; |
166 | | - |
167 | | - auto json_res = json_helper::ParseJsonString(res->body); |
168 | | - std::string latest_version = get_latest(json_res); |
169 | | - if (latest_version.empty()) { |
170 | | - CTL_WRN("Release not found!"); |
171 | | - return std::nullopt; |
172 | | - } |
173 | | - std::string current_version = CORTEX_CPP_VERSION; |
174 | | - CTL_INF("Got the latest release, update to the config file: " |
175 | | - << latest_version) |
176 | | - config.latestRelease = latest_version; |
177 | | - auto result = config_yaml_utils::DumpYamlConfig( |
178 | | - config, file_manager_utils::GetConfigurationPath().string()); |
179 | | - if (result.has_error()) { |
180 | | - CTL_ERR("Error update " |
181 | | - << file_manager_utils::GetConfigurationPath().string() |
182 | | - << result.error()); |
183 | 163 | } |
184 | | - if (current_version != latest_version) { |
185 | | - return latest_version; |
186 | | - } |
187 | | - } catch (const std::exception& e) { |
188 | | - CTL_INF("JSON parse error: " << e.what()); |
189 | | - return std::nullopt; |
| 164 | + return data[0]["tag_name"].asString(); |
| 165 | + } else { |
| 166 | + return data["tag_name"].asString(); |
190 | 167 | } |
191 | | - } else { |
192 | | - CTL_INF("HTTP error: " << res->status); |
| 168 | + return ""; |
| 169 | + }; |
| 170 | + |
| 171 | + std::string latest_version = get_latest(res.value()); |
| 172 | + if (latest_version.empty()) { |
| 173 | + CTL_WRN("Release not found!"); |
193 | 174 | return std::nullopt; |
194 | 175 | } |
195 | | - } else { |
196 | | - auto err = res.error(); |
197 | | - CTL_INF("HTTP error: " << httplib::to_string(err)); |
| 176 | + std::string current_version = CORTEX_CPP_VERSION; |
| 177 | + CTL_INF( |
| 178 | + "Got the latest release, update to the config file: " << latest_version) |
| 179 | + config.latestRelease = latest_version; |
| 180 | + auto result = config_yaml_utils::DumpYamlConfig( |
| 181 | + config, file_manager_utils::GetConfigurationPath().string()); |
| 182 | + if (result.has_error()) { |
| 183 | + CTL_ERR("Error update " |
| 184 | + << file_manager_utils::GetConfigurationPath().string() |
| 185 | + << result.error()); |
| 186 | + } |
| 187 | + if (current_version != latest_version) { |
| 188 | + return latest_version; |
| 189 | + } |
| 190 | + } catch (const std::exception& e) { |
| 191 | + CTL_INF("JSON parse error: " << e.what()); |
198 | 192 | return std::nullopt; |
199 | 193 | } |
200 | 194 | return std::nullopt; |
@@ -281,9 +275,9 @@ void CortexUpdCmd::Exec(const std::string& v, bool force) { |
281 | 275 |
|
282 | 276 | { |
283 | 277 | auto config = file_manager_utils::GetCortexConfig(); |
284 | | - httplib::Client cli(config.apiServerHost + ":" + config.apiServerPort); |
285 | | - auto res = cli.Get("/healthz"); |
286 | | - if (res) { |
| 278 | + auto server_running = commands::IsServerAlive( |
| 279 | + config.apiServerHost, std::stoi(config.apiServerPort)); |
| 280 | + if (server_running) { |
287 | 281 | CLI_LOG("Server is running. Stopping server before updating!"); |
288 | 282 | commands::ServerStopCmd ssc(config.apiServerHost, |
289 | 283 | std::stoi(config.apiServerPort)); |
@@ -323,33 +317,30 @@ bool CortexUpdCmd::GetStable(const std::string& v) { |
323 | 317 | auto release_path = GetReleasePath(); |
324 | 318 | CTL_INF("Engine release path: " << github_host << release_path); |
325 | 319 |
|
326 | | - httplib::Client cli(github_host); |
327 | | - if (auto res = cli.Get(release_path)) { |
328 | | - if (res->status == httplib::StatusCode::OK_200) { |
329 | | - try { |
330 | | - auto json_data = json_helper::ParseJsonString(res->body); |
331 | | - if (json_data.empty()) { |
332 | | - CLI_LOG("Version not found: " << v); |
333 | | - return false; |
334 | | - } |
| 320 | + auto url_obj = url_parser::Url{ |
| 321 | + .protocol = "https", |
| 322 | + .host = github_host, |
| 323 | + .pathParams = {}, |
| 324 | + }; |
| 325 | + auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath()); |
| 326 | + if (res.has_error()) { |
| 327 | + CLI_LOG_ERROR("HTTP error: " << res.error()); |
| 328 | + return false; |
| 329 | + } |
335 | 330 |
|
336 | | - if (downloaded_exe_path = HandleGithubRelease( |
337 | | - json_data["assets"], |
338 | | - {system_info->os + "-" + system_info->arch}); |
339 | | - !downloaded_exe_path) { |
340 | | - return false; |
341 | | - } |
342 | | - } catch (const std::exception& e) { |
343 | | - CLI_LOG_ERROR("JSON parse error: " << e.what()); |
344 | | - return false; |
345 | | - } |
346 | | - } else { |
347 | | - CLI_LOG_ERROR("HTTP error: " << res->status); |
| 331 | + try { |
| 332 | + if (res.value().empty()) { |
| 333 | + CLI_LOG("Version not found: " << v); |
348 | 334 | return false; |
349 | 335 | } |
350 | | - } else { |
351 | | - auto err = res.error(); |
352 | | - CLI_LOG_ERROR("HTTP error: " << httplib::to_string(err)); |
| 336 | + |
| 337 | + if (downloaded_exe_path = HandleGithubRelease( |
| 338 | + res.value()["assets"], {system_info->os + "-" + system_info->arch}); |
| 339 | + !downloaded_exe_path) { |
| 340 | + return false; |
| 341 | + } |
| 342 | + } catch (const std::exception& e) { |
| 343 | + CLI_LOG_ERROR("JSON parse error: " << e.what()); |
353 | 344 | return false; |
354 | 345 | } |
355 | 346 |
|
@@ -379,45 +370,41 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { |
379 | 370 | auto release_path = GetReleasePath(); |
380 | 371 | CTL_INF("Engine release path: " << github_host << release_path); |
381 | 372 |
|
382 | | - httplib::Client cli(github_host); |
383 | | - if (auto res = cli.Get(release_path)) { |
384 | | - if (res->status == httplib::StatusCode::OK_200) { |
385 | | - try { |
386 | | - auto json_res = json_helper::ParseJsonString(res->body); |
387 | | - |
388 | | - Json::Value json_data; |
389 | | - for (const auto& jr : json_res) { |
390 | | - // Get the latest beta or match version |
391 | | - if (auto tag = jr["tag_name"].asString(); |
392 | | - (v.empty() && tag.find(kBetaComp) != std::string::npos) || |
393 | | - (tag == v)) { |
394 | | - json_data = jr; |
395 | | - break; |
396 | | - } |
397 | | - } |
| 373 | + auto url_obj = url_parser::Url{ |
| 374 | + .protocol = "https", .host = github_host, .pathParams = {}, // TODO: namh |
| 375 | + }; |
398 | 376 |
|
399 | | - if (json_data.empty()) { |
400 | | - CLI_LOG("Version not found: " << v); |
401 | | - return false; |
402 | | - } |
| 377 | + // todo:: host is contains protocol |
| 378 | + auto res = curl_utils::SimpleGetJson(url_obj.ToFullPath()); |
| 379 | + if (res.has_error()) { |
| 380 | + CLI_LOG_ERROR("HTTP error: " << res.error()); |
| 381 | + return false; |
| 382 | + } |
403 | 383 |
|
404 | | - if (downloaded_exe_path = HandleGithubRelease( |
405 | | - json_data["assets"], |
406 | | - {system_info->os + "-" + system_info->arch}); |
407 | | - !downloaded_exe_path) { |
408 | | - return false; |
409 | | - } |
410 | | - } catch (const std::exception& e) { |
411 | | - CLI_LOG_ERROR("JSON parse error: " << e.what()); |
412 | | - return false; |
| 384 | + try { |
| 385 | + Json::Value json_data; |
| 386 | + for (const auto& jr : res.value()) { |
| 387 | + // Get the latest beta or match version |
| 388 | + if (auto tag = jr["tag_name"].asString(); |
| 389 | + (v.empty() && tag.find(kBetaComp) != std::string::npos) || |
| 390 | + (tag == v)) { |
| 391 | + json_data = jr; |
| 392 | + break; |
413 | 393 | } |
414 | | - } else { |
415 | | - CLI_LOG_ERROR("HTTP error: " << res->status); |
| 394 | + } |
| 395 | + |
| 396 | + if (json_data.empty()) { |
| 397 | + CLI_LOG("Version not found: " << v); |
416 | 398 | return false; |
417 | 399 | } |
418 | | - } else { |
419 | | - auto err = res.error(); |
420 | | - CLI_LOG_ERROR("HTTP error: " << httplib::to_string(err)); |
| 400 | + |
| 401 | + if (downloaded_exe_path = HandleGithubRelease( |
| 402 | + json_data["assets"], {system_info->os + "-" + system_info->arch}); |
| 403 | + !downloaded_exe_path) { |
| 404 | + return false; |
| 405 | + } |
| 406 | + } catch (const std::exception& e) { |
| 407 | + CLI_LOG_ERROR("JSON parse error: " << e.what()); |
421 | 408 | return false; |
422 | 409 | } |
423 | 410 |
|
@@ -473,13 +460,15 @@ std::optional<std::string> CortexUpdCmd::HandleGithubRelease( |
473 | 460 | CLI_LOG_ERROR("Failed to create directories: " << e.what()); |
474 | 461 | return std::nullopt; |
475 | 462 | } |
476 | | - auto download_task{DownloadTask{.id = "cortex", |
477 | | - .type = DownloadType::Cortex, |
478 | | - .items = {DownloadItem{ |
479 | | - .id = "cortex", |
480 | | - .downloadUrl = download_url, |
481 | | - .localPath = local_path, |
482 | | - }}}}; |
| 463 | + auto download_task{DownloadTask{ |
| 464 | + .id = "cortex", |
| 465 | + .type = DownloadType::Cortex, |
| 466 | + .items = {DownloadItem{ |
| 467 | + .id = "cortex", |
| 468 | + .downloadUrl = download_url, |
| 469 | + .localPath = local_path, |
| 470 | + }}, |
| 471 | + }}; |
483 | 472 |
|
484 | 473 | auto result = download_service_->AddDownloadTask( |
485 | 474 | download_task, [](const DownloadTask& finishedTask) { |
|
0 commit comments