This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
feat: add engine install cli #1034
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #include "engines.h" | ||
| #include "utils/archive_utils.h" | ||
| #include "utils/file_manager_utils.h" | ||
| #include "utils/system_info_utils.h" | ||
|
|
||
| void Engines::InitEngine(const HttpRequestPtr& req, | ||
| std::function<void(const HttpResponsePtr&)>&& callback, | ||
| const std::string& engine) const { | ||
| LOG_DEBUG << "InitEngine, Engine: " << engine; | ||
| if (engine.empty()) { | ||
| Json::Value res; | ||
| res["message"] = "Engine name is required"; | ||
| auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
| resp->setStatusCode(k409Conflict); | ||
| callback(resp); | ||
| LOG_WARN << "No engine field in path param"; | ||
| return; | ||
| } | ||
|
|
||
| auto system_info = system_info_utils::GetSystemInfo(); | ||
| if (system_info.arch == system_info_utils::kUnsupported || | ||
| system_info.os == system_info_utils::kUnsupported) { | ||
| Json::Value res; | ||
| res["message"] = "Unsupported OS or architecture"; | ||
| auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
| resp->setStatusCode(k409Conflict); | ||
| callback(resp); | ||
| LOG_ERROR << "Unsupported OS or architecture: " << system_info.os << ", " | ||
| << system_info.arch; | ||
| return; | ||
| } | ||
|
|
||
| auto version{"latest"}; | ||
| constexpr auto gitHubHost = "https://api.github.com"; | ||
|
|
||
| std::ostringstream engineReleasePath; | ||
| engineReleasePath << "/repos/janhq/" << engine << "/releases/" << version; | ||
|
|
||
| httplib::Client cli(gitHubHost); | ||
| using namespace nlohmann; | ||
| if (auto res = cli.Get(engineReleasePath.str())) { | ||
| if (res->status == httplib::StatusCode::OK_200) { | ||
| try { | ||
| auto jsonResponse = json::parse(res->body); | ||
| auto assets = jsonResponse["assets"]; | ||
|
|
||
| auto os_arch{system_info.os + "-" + system_info.arch}; | ||
| for (auto& asset : assets) { | ||
| auto assetName = asset["name"].get<std::string>(); | ||
| if (assetName.find(os_arch) != std::string::npos) { | ||
| std::string host{"https://github.com"}; | ||
|
|
||
| auto full_url = asset["browser_download_url"].get<std::string>(); | ||
| std::string path = full_url.substr(host.length()); | ||
|
|
||
| auto fileName = asset["name"].get<std::string>(); | ||
| LOG_INFO << "URL: " << full_url; | ||
|
|
||
| auto downloadTask = DownloadTask{.id = engine, | ||
| .type = DownloadType::Engine, | ||
| .error = std::nullopt, | ||
| .items = {DownloadItem{ | ||
| .id = engine, | ||
| .host = host, | ||
| .fileName = fileName, | ||
| .type = DownloadType::Engine, | ||
| .path = path, | ||
| }}}; | ||
|
|
||
| DownloadService().AddAsyncDownloadTask( | ||
| downloadTask, [](const std::string& absolute_path) { | ||
| // try to unzip the downloaded file | ||
| std::filesystem::path downloadedEnginePath{absolute_path}; | ||
| LOG_INFO << "Downloaded engine path: " | ||
| << downloadedEnginePath.string(); | ||
|
|
||
| archive_utils::ExtractArchive( | ||
| downloadedEnginePath.string(), | ||
| downloadedEnginePath.parent_path() | ||
| .parent_path() | ||
| .string()); | ||
|
|
||
| // remove the downloaded file | ||
| std::filesystem::remove(absolute_path); | ||
| LOG_INFO << "Finished!"; | ||
| }); | ||
|
|
||
| Json::Value res; | ||
| res["message"] = "Engine download started"; | ||
| res["result"] = "OK"; | ||
| auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
| resp->setStatusCode(k200OK); | ||
| callback(resp); | ||
| return; | ||
| } | ||
| } | ||
| Json::Value res; | ||
| res["message"] = "Engine not found"; | ||
| res["result"] = "Error"; | ||
| auto resp = cortex_utils::CreateCortexHttpJsonResponse(res); | ||
| resp->setStatusCode(k404NotFound); | ||
| callback(resp); | ||
| } catch (const json::parse_error& e) { | ||
| std::cerr << "JSON parse error: " << e.what() << std::endl; | ||
| } | ||
| } | ||
| } else { | ||
| auto err = res.error(); | ||
| LOG_ERROR << "HTTP error: " << httplib::to_string(err); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <drogon/HttpController.h> | ||
| #include <trantor/utils/Logger.h> | ||
| #include "services/download_service.h" | ||
| #include "utils/cortex_utils.h" | ||
| #include "utils/cortexso_parser.h" | ||
| #include "utils/http_util.h" | ||
|
|
||
| using namespace drogon; | ||
|
|
||
| class Engines : public drogon::HttpController<Engines> { | ||
| public: | ||
| METHOD_LIST_BEGIN | ||
| METHOD_ADD(Engines::InitEngine, "/{1}/init", Post); | ||
| METHOD_LIST_END | ||
|
|
||
| void InitEngine(const HttpRequestPtr& req, | ||
| std::function<void(const HttpResponsePtr&)>&& callback, | ||
| const std::string& engine) const; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include <array> | ||
| #include <cstdio> | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| #ifdef _WIN32 | ||
| #define POPEN _popen | ||
| #define PCLOSE _pclose | ||
| #else | ||
| #define POPEN popen | ||
| #define PCLOSE pclose | ||
| #endif | ||
|
|
||
| class CommandExecutor { | ||
| public: | ||
| CommandExecutor(const std::string& command) { | ||
| FILE* pipe = POPEN(command.c_str(), "r"); | ||
| if (!pipe) { | ||
| throw std::runtime_error("popen() failed!"); | ||
| } | ||
| m_pipe = std::unique_ptr<FILE, decltype(&PCLOSE)>(pipe, PCLOSE); | ||
| } | ||
|
|
||
| CommandExecutor(const CommandExecutor&) = delete; | ||
| CommandExecutor& operator=(const CommandExecutor&) = delete; | ||
| CommandExecutor(CommandExecutor&&) = default; | ||
| CommandExecutor& operator=(CommandExecutor&&) = default; | ||
| ~CommandExecutor() = default; | ||
|
|
||
| std::string execute() { | ||
| if (!m_pipe) { | ||
| throw std::runtime_error("Command not initialized!"); | ||
| } | ||
|
|
||
| std::array<char, 128> buffer; | ||
| std::string result; | ||
|
|
||
| while (fgets(buffer.data(), buffer.size(), m_pipe.get()) != nullptr) { | ||
| result += buffer.data(); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private: | ||
| std::unique_ptr<FILE, decltype(&PCLOSE)> m_pipe{nullptr, PCLOSE}; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.