diff --git a/engine/commands/model_get_cmd.cc b/engine/commands/model_get_cmd.cc index cc6639d33..acbce742c 100644 --- a/engine/commands/model_get_cmd.cc +++ b/engine/commands/model_get_cmd.cc @@ -6,6 +6,7 @@ #include "config/yaml_config.h" #include "trantor/utils/Logger.h" #include "utils/cortex_utils.h" +#include "utils/file_manager_utils.h" #include "utils/logging_utils.h" namespace commands { @@ -14,15 +15,15 @@ ModelGetCmd::ModelGetCmd(std::string model_handle) : model_handle_(std::move(model_handle)) {} void ModelGetCmd::Exec() { - if (std::filesystem::exists(cortex_utils::models_folder) && - std::filesystem::is_directory(cortex_utils::models_folder)) { + auto models_path = file_manager_utils::GetModelsContainerPath(); + if (std::filesystem::exists(models_path) && + std::filesystem::is_directory(models_path)) { CmdInfo ci(model_handle_); std::string model_file = ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch; bool found_model = false; // Iterate through directory - for (const auto& entry : - std::filesystem::directory_iterator(cortex_utils::models_folder)) { + for (const auto& entry : std::filesystem::directory_iterator(models_path)) { if (entry.is_regular_file() && entry.path().stem() == model_file && entry.path().extension() == ".yaml") { @@ -137,7 +138,7 @@ void ModelGetCmd::Exec() { break; } catch (const std::exception& e) { CTL_ERR("Error reading yaml file '" << entry.path().string() - << "': " << e.what()); + << "': " << e.what()); } } } diff --git a/engine/commands/model_list_cmd.cc b/engine/commands/model_list_cmd.cc index c2a04f06f..70d1e79ea 100644 --- a/engine/commands/model_list_cmd.cc +++ b/engine/commands/model_list_cmd.cc @@ -8,20 +8,22 @@ #include #include "config/yaml_config.h" #include "trantor/utils/Logger.h" +#include "utils/file_manager_utils.h" #include "utils/logging_utils.h" + namespace commands { void ModelListCmd::Exec() { - if (std::filesystem::exists(cortex_utils::models_folder) && - std::filesystem::is_directory(cortex_utils::models_folder)) { + auto models_path = file_manager_utils::GetModelsContainerPath(); + if (std::filesystem::exists(models_path) && + std::filesystem::is_directory(models_path)) { tabulate::Table table; table.add_row({"(Index)", "ID", "engine", "version"}); table.format().font_color(tabulate::Color::green); int count = 0; // Iterate through directory - for (const auto& entry : - std::filesystem::directory_iterator(cortex_utils::models_folder)) { + for (const auto& entry : std::filesystem::directory_iterator(models_path)) { if (entry.is_regular_file() && entry.path().extension() == ".yaml") { try { count += 1; @@ -32,7 +34,7 @@ void ModelListCmd::Exec() { model_config.engine, model_config.version}); } catch (const std::exception& e) { CTL_ERR("Error reading yaml file '" << entry.path().string() - << "': " << e.what()); + << "': " << e.what()); } } } diff --git a/engine/commands/run_cmd.cc b/engine/commands/run_cmd.cc index 5070c3937..c8bde1b14 100644 --- a/engine/commands/run_cmd.cc +++ b/engine/commands/run_cmd.cc @@ -7,6 +7,7 @@ #include "model_start_cmd.h" #include "trantor/utils/Logger.h" #include "utils/cortex_utils.h" +#include "utils/file_manager_utils.h" namespace commands { @@ -42,8 +43,9 @@ void RunCmd::Exec() { // Start model config::YamlHandler yaml_handler; - yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() + "/models/" + - model_file + ".yaml"); + yaml_handler.ModelConfigFromFile( + file_manager_utils::GetModelsContainerPath().string() + "/" + model_file + + ".yaml"); { ModelStartCmd msc(host_, port_, yaml_handler.GetModelConfig()); if (!msc.Exec()) { @@ -59,14 +61,11 @@ void RunCmd::Exec() { } bool RunCmd::IsModelExisted(const std::string& model_id) { - if (std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" + - cortex_utils::models_folder) && - std::filesystem::is_directory(cortex_utils::GetCurrentPath() + "/" + - cortex_utils::models_folder)) { + auto models_path = file_manager_utils::GetModelsContainerPath(); + if (std::filesystem::exists(models_path) && + std::filesystem::is_directory(models_path)) { // Iterate through directory - for (const auto& entry : std::filesystem::directory_iterator( - cortex_utils::GetCurrentPath() + "/" + - cortex_utils::models_folder)) { + for (const auto& entry : std::filesystem::directory_iterator(models_path)) { if (entry.is_regular_file() && entry.path().extension() == ".yaml") { try { config::YamlHandler handler; @@ -85,10 +84,9 @@ bool RunCmd::IsModelExisted(const std::string& model_id) { } bool RunCmd::IsEngineExisted(const std::string& e) { - if (std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" + - "engines") && - std::filesystem::exists(cortex_utils::GetCurrentPath() + "/" + - "engines/" + e)) { + auto engines_path = file_manager_utils::GetEnginesContainerPath(); + if (std::filesystem::exists(engines_path) && + std::filesystem::exists(engines_path.string() + "/" + e)) { return true; } return false; diff --git a/engine/controllers/command_line_parser.cc b/engine/controllers/command_line_parser.cc index 8c8c706a3..b8ad72f58 100644 --- a/engine/controllers/command_line_parser.cc +++ b/engine/controllers/command_line_parser.cc @@ -17,6 +17,7 @@ #include "httplib.h" #include "services/engine_service.h" #include "utils/cortex_utils.h" +#include "utils/file_manager_utils.h" #include "utils/logging_utils.h" CommandLineParser::CommandLineParser() @@ -38,8 +39,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) { std::string model_file = ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch; config::YamlHandler yaml_handler; - yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() + - "/models/" + model_file + ".yaml"); + yaml_handler.ModelConfigFromFile( + file_manager_utils::GetModelsContainerPath().string() + "/" + + model_file + ".yaml"); commands::ModelStartCmd msc("127.0.0.1", 3928, yaml_handler.GetModelConfig()); msc.Exec(); @@ -53,8 +55,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) { std::string model_file = ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch; config::YamlHandler yaml_handler; - yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() + - "/models/" + model_file + ".yaml"); + yaml_handler.ModelConfigFromFile( + file_manager_utils::GetModelsContainerPath().string() + "/" + + model_file + ".yaml"); commands::ModelStopCmd smc("127.0.0.1", 3928, yaml_handler.GetModelConfig()); smc.Exec(); @@ -107,8 +110,9 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) { std::string model_file = ci.branch == "main" ? ci.model_name : ci.model_name + "-" + ci.branch; config::YamlHandler yaml_handler; - yaml_handler.ModelConfigFromFile(cortex_utils::GetCurrentPath() + - "/models/" + model_file + ".yaml"); + yaml_handler.ModelConfigFromFile( + file_manager_utils::GetModelsContainerPath().string() + "/" + + model_file + ".yaml"); commands::ChatCmd cc("127.0.0.1", 3928, yaml_handler.GetModelConfig()); cc.Exec(msg); }); diff --git a/engine/controllers/models.cc b/engine/controllers/models.cc index 52a8bff28..e4b6f4adc 100644 --- a/engine/controllers/models.cc +++ b/engine/controllers/models.cc @@ -2,6 +2,7 @@ #include "config/yaml_config.h" #include "trantor/utils/Logger.h" #include "utils/cortex_utils.h" +#include "utils/file_manager_utils.h" #include "utils/model_callback_utils.h" void Models::PullModel( @@ -49,11 +50,11 @@ void Models::ListModel( Json::Value ret; ret["object"] = "list"; Json::Value data(Json::arrayValue); - if (std::filesystem::exists(cortex_utils::models_folder) && - std::filesystem::is_directory(cortex_utils::models_folder)) { + auto models_path = file_manager_utils::GetModelsContainerPath(); + if (std::filesystem::exists(models_path) && + std::filesystem::is_directory(models_path)) { // Iterate through directory - for (const auto& entry : - std::filesystem::directory_iterator(cortex_utils::models_folder)) { + for (const auto& entry : std::filesystem::directory_iterator(models_path)) { if (entry.is_regular_file() && entry.path().extension() == ".yaml") { try { config::YamlHandler handler; diff --git a/engine/controllers/server.cc b/engine/controllers/server.cc index 0c5963d7a..42a7595be 100644 --- a/engine/controllers/server.cc +++ b/engine/controllers/server.cc @@ -8,6 +8,7 @@ #include "utils/cortex_utils.h" #include "utils/cpuid/cpu_info.h" #include "utils/logging_utils.h" +#include "utils/file_manager_utils.h" using namespace inferences; using json = nlohmann::json; @@ -290,8 +291,9 @@ void server::LoadModel(const HttpRequestPtr& req, std::string abs_path = (getenv("ENGINE_PATH") ? getenv("ENGINE_PATH") - : cortex_utils::GetCurrentPath()) + + : file_manager_utils::GetCortexDataPath().string()) + get_engine_path(engine_type); + std::cout << abs_path << std::endl; engines_[engine_type].dl = std::make_unique(abs_path, "engine"); diff --git a/engine/main.cc b/engine/main.cc index 79e8b656e..5c83d1103 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -4,7 +4,6 @@ #include "controllers/command_line_parser.h" #include "cortex-common/cortexpythoni.h" #include "utils/archive_utils.h" -#include "utils/config_yaml_utils.h" #include "utils/cortex_utils.h" #include "utils/dylib.h" #include "utils/file_manager_utils.h" @@ -26,7 +25,7 @@ #endif void RunServer() { - auto config = config_yaml_utils::GetCortexConfig(); + auto config = file_manager_utils::GetCortexConfig(); LOG_INFO << "Host: " << config.host << " Port: " << config.port << "\n"; // Create logs/ folder and setup log to file @@ -127,7 +126,7 @@ void ForkProcess() { } int main(int argc, char* argv[]) { - { config_yaml_utils::CreateConfigFileIfNotExist(); } + { file_manager_utils::CreateConfigFileIfNotExist(); } // Check if this process is for python execution if (argc > 1) { diff --git a/engine/utils/config_yaml_utils.h b/engine/utils/config_yaml_utils.h index 75abfca0b..4330f3527 100644 --- a/engine/utils/config_yaml_utils.h +++ b/engine/utils/config_yaml_utils.h @@ -1,8 +1,8 @@ +#pragma once #include #include #include #include -#include "utils/file_manager_utils.h" #include "utils/logging_utils.h" #include "yaml-cpp/yaml.h" @@ -39,24 +39,6 @@ inline void DumpYamlConfig(const CortexConfig& config, } } -inline void CreateConfigFileIfNotExist() { - auto config_path = file_manager_utils::GetConfigurationPath(); - if (std::filesystem::exists(config_path)) { - // already exists - return; - } - CLI_LOG("Config file not found. Creating one at " + config_path.string()); - auto defaultDataFolderPath = - file_manager_utils::GetHomeDirectoryPath() / kCortexFolderName; - auto config = CortexConfig{ - .dataFolderPath = defaultDataFolderPath.string(), - .host = kDefaultHost, - .port = kDefaultPort, - }; - std::cout << "config: " << config.dataFolderPath << "\n"; - DumpYamlConfig(config, config_path.string()); -} - inline CortexConfig FromYaml(const std::string& path, const std::string& variant) { std::filesystem::path config_file_path{path}; @@ -78,9 +60,5 @@ inline CortexConfig FromYaml(const std::string& path, } } -inline CortexConfig GetCortexConfig() { - auto config_path = file_manager_utils::GetConfigurationPath(); - std::string variant = ""; // TODO: empty for now - return FromYaml(config_path.string(), variant); -} + } // namespace config_yaml_utils diff --git a/engine/utils/file_manager_utils.h b/engine/utils/file_manager_utils.h index fd2723078..fcf8a959a 100644 --- a/engine/utils/file_manager_utils.h +++ b/engine/utils/file_manager_utils.h @@ -4,6 +4,7 @@ #include #include "logging_utils.h" #include "services/download_service.h" +#include "utils/config_yaml_utils.h" #if defined(__APPLE__) && defined(__MACH__) #include @@ -84,23 +85,54 @@ inline std::filesystem::path GetConfigurationPath() { return configuration_path; } -inline std::filesystem::path GetCortexPath() { +inline void CreateConfigFileIfNotExist() { + auto config_path = file_manager_utils::GetConfigurationPath(); + if (std::filesystem::exists(config_path)) { + // already exists + return; + } + CLI_LOG("Config file not found. Creating one at " + config_path.string()); + auto defaultDataFolderPath = + file_manager_utils::GetHomeDirectoryPath() / config_yaml_utils::kCortexFolderName; + auto config = config_yaml_utils::CortexConfig{ + .dataFolderPath = defaultDataFolderPath.string(), + .host = config_yaml_utils::kDefaultHost, + .port = config_yaml_utils::kDefaultPort, + }; + std::cout << "config: " << config.dataFolderPath << "\n"; + DumpYamlConfig(config, config_path.string()); +} + +inline config_yaml_utils::CortexConfig GetCortexConfig() { + auto config_path = GetConfigurationPath(); + std::string variant = ""; // TODO: empty for now + return config_yaml_utils::FromYaml(config_path.string(), variant); +} + +inline std::filesystem::path GetCortexDataPath() { // TODO: We will need to support user to move the data folder to other place. // TODO: get the variant of cortex. As discussed, we will have: prod, beta, nightly - // currently we will store cortex data at ~/.cortex + // currently we will store cortex data at ~/cortexcpp + auto config = GetCortexConfig(); + std::filesystem::path data_folder_path; + if (!config.dataFolderPath.empty()) { + data_folder_path = + std::filesystem::path(config.dataFolderPath); + } else { + auto home_path = GetHomeDirectoryPath(); + data_folder_path = home_path / config_yaml_utils::kCortexFolderName; + } - auto home_path = GetHomeDirectoryPath(); - auto cortex_path = home_path / ".cortex"; - if (!std::filesystem::exists(cortex_path)) { + if (!std::filesystem::exists(data_folder_path)) { CTL_INF("Cortex home folder not found. Create one: " + - cortex_path.string()); - std::filesystem::create_directory(cortex_path); + data_folder_path.string()); + std::filesystem::create_directory(data_folder_path); } - return cortex_path; + return data_folder_path; } inline std::filesystem::path GetModelsContainerPath() { - auto cortex_path = GetCortexPath(); + auto cortex_path = GetCortexDataPath(); auto models_container_path = cortex_path / "models"; if (!std::filesystem::exists(models_container_path)) { @@ -113,7 +145,7 @@ inline std::filesystem::path GetModelsContainerPath() { } inline std::filesystem::path GetEnginesContainerPath() { - auto cortex_path = GetCortexPath(); + auto cortex_path = GetCortexDataPath(); auto engines_container_path = cortex_path / "engines"; if (!std::filesystem::exists(engines_container_path)) {