From 3e46bb1e56e1fed35e357b5c64d002fb369e90d0 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 17:13:45 +0700 Subject: [PATCH 1/3] fix: nightly updater --- engine/commands/cortex_upd_cmd.cc | 5 +++-- engine/commands/cortex_upd_cmd.h | 11 ++++++----- engine/main.cc | 11 +++++++++++ engine/utils/file_manager_utils.h | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index a4343e55c..413a159f2 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -159,7 +159,7 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { std::ostringstream release_path; release_path << "cortex/" << version << "/" << system_info.os << "-" << system_info.arch << "/" << kNightlyFileName; - CTL_INF("Engine release path: " << kNightlyHost << release_path.str()); + CTL_INF("Engine release path: " << kNightlyHost << "/" << release_path.str()); auto download_task = DownloadTask{.id = "cortex", .type = DownloadType::Cortex, @@ -190,7 +190,8 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { // Replace binay file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = executable_path / "cortex" / GetCortexBinary(); + auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / + GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); } diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index 396c6cdaf..998b1b436 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -62,8 +62,10 @@ inline void CheckNewUpdate() { if (current_version != latest_version) { CLI_LOG("\nA new release of cortex is available: " << current_version << " -> " << latest_version); - CLI_LOG("To upgrade, run: cortex update"); - // CLI_LOG(json_res["html_url"].get()); + CLI_LOG("To upgrade, run: " << GetCortexBinary() << " update"); + if (CORTEX_VARIANT == file_manager_utils::kProdVariant) { + CLI_LOG(json_res["html_url"].get()); + } } } catch (const nlohmann::json::parse_error& e) { CTL_INF("JSON parse error: " << e.what()); @@ -83,7 +85,8 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, // Already has the newest return true; } - std::filesystem::path temp = std::filesystem::temp_directory_path() / "cortex_temp"; + std::filesystem::path temp = + file_manager_utils::GetExecutableFolderContainerPath() / "cortex_temp"; try { if (std::filesystem::exists(temp)) { @@ -97,8 +100,6 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, std::filesystem::perms::group_all | std::filesystem::perms::others_read | std::filesystem::perms::others_exec); - auto download_folder = src.parent_path(); - std::filesystem::remove_all(download_folder); } catch (const std::exception& e) { CTL_ERR("Something wrong happened: " << e.what()); if (std::filesystem::exists(temp)) { diff --git a/engine/main.cc b/engine/main.cc index 949f9e0d2..2c8065dae 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -133,6 +133,17 @@ void ForkProcess() { int main(int argc, char* argv[]) { { file_manager_utils::CreateConfigFileIfNotExist(); } + // Delete temporary file if it exists + auto temp = + file_manager_utils::GetExecutableFolderContainerPath() / "cortex_temp"; + if (std::filesystem::exists(temp)) { + try { + std::filesystem::remove(temp); + } catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + } + } + // Check if this process is for python execution if (argc > 1) { if (strcmp(argv[1], "--run_python_file") == 0) { diff --git a/engine/utils/file_manager_utils.h b/engine/utils/file_manager_utils.h index b4d7ab07a..1e5c522e0 100644 --- a/engine/utils/file_manager_utils.h +++ b/engine/utils/file_manager_utils.h @@ -230,7 +230,7 @@ inline std::filesystem::path GetContainerFolderPath( } else if (type == "CudaToolkit") { container_folder_path = current_path; } else if (type == "Cortex") { - container_folder_path = current_path / "cortex"; + container_folder_path = std::filesystem::temp_directory_path() / "cortex"; } else { container_folder_path = current_path / "misc"; } From d4c3f9c2d83726e2d67ab740ee5bcfbadac29c6b Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 12 Sep 2024 17:58:54 +0700 Subject: [PATCH 2/3] fix: correct temp --- engine/commands/cortex_upd_cmd.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index 998b1b436..b8aac9e7b 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -85,8 +85,7 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, // Already has the newest return true; } - std::filesystem::path temp = - file_manager_utils::GetExecutableFolderContainerPath() / "cortex_temp"; + std::filesystem::path temp = dst.parent_path() / "cortex_temp"; try { if (std::filesystem::exists(temp)) { From cd26a1d10903a7a44f0aa09b702b32623f51cddc Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Fri, 13 Sep 2024 06:48:43 +0700 Subject: [PATCH 3/3] fix: add e2e test --- engine/commands/cortex_upd_cmd.cc | 3 ++- engine/e2e-test/main.py | 1 + engine/e2e-test/test_cortex_update.py | 14 ++++++++++++++ engine/main.cc | 4 +++- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 engine/e2e-test/test_cortex_update.py diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 413a159f2..ca25f63d3 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -138,7 +138,8 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) { // Replace binary file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = executable_path / "cortex" / kCortexBinary / GetCortexBinary(); + auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / + GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); return ReplaceBinaryInflight(src, dst); } diff --git a/engine/e2e-test/main.py b/engine/e2e-test/main.py index d59466d29..cbb0cc01c 100644 --- a/engine/e2e-test/main.py +++ b/engine/e2e-test/main.py @@ -4,6 +4,7 @@ from test_cli_engine_install import TestCliEngineInstall from test_cli_engine_list import TestCliEngineList from test_cli_engine_uninstall import TestCliEngineUninstall +from test_cortex_update import TestCortexUpdate if __name__ == "__main__": pytest.main([__file__, "-v"]) diff --git a/engine/e2e-test/test_cortex_update.py b/engine/e2e-test/test_cortex_update.py new file mode 100644 index 000000000..fc1fe25cb --- /dev/null +++ b/engine/e2e-test/test_cortex_update.py @@ -0,0 +1,14 @@ +import platform + +import pytest +from test_runner import run + + +class TestCortexUpdate: + # We don't have stable release yet, so mark this test as skip for now + # Only able to test with beta and nightly + @pytest.mark.skip(reason="Stable release is not available yet") + def test_cortex_update(self): + exit_code, output, error = run("Update cortex", ["update"]) + assert exit_code == 0, f"Something wrong happened" + assert "Update cortex sucessfully" in output diff --git a/engine/main.cc b/engine/main.cc index 2c8065dae..97bf2698e 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -9,6 +9,7 @@ #include "utils/file_logger.h" #include "utils/file_manager_utils.h" #include "utils/logging_utils.h" +#include "commands/cortex_upd_cmd.h" #if defined(__APPLE__) && defined(__MACH__) #include // for dirname() @@ -91,8 +92,9 @@ void ForkProcess() { ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); + auto exe = commands::GetCortexBinary(); std::string cmds = - cortex_utils::GetCurrentPath() + "/cortex-cpp.exe --start-server"; + cortex_utils::GetCurrentPath() + "/" + exe + " --start-server"; // Create child process if (!CreateProcess( NULL, // No module name (use command line)