Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions engine/cli/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "commands/engine_get_cmd.h"
#include "commands/engine_install_cmd.h"
#include "commands/engine_list_cmd.h"
#include "commands/engine_load_cmd.h"
#include "commands/engine_uninstall_cmd.h"
#include "commands/engine_unload_cmd.h"
#include "commands/engine_update_cmd.h"
#include "commands/engine_use_cmd.h"
#include "commands/hardware_activate_cmd.h"
Expand Down Expand Up @@ -474,6 +476,41 @@ void CommandLineParser::SetupEngineCommands() {
EngineUse(engine_use_cmd, engine_name);
}

auto engine_load_cmd = engines_cmd->add_subcommand("load", "Load engine");
engine_load_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines load [engine_name]");
engine_load_cmd->callback([this, engine_load_cmd] {
if (std::exchange(executed_, true))
return;
if (engine_load_cmd->get_subcommands().empty()) {
CLI_LOG("[engine_name] is required\n");
CLI_LOG(engine_load_cmd->help());
}
});
engine_load_cmd->group(kSubcommands);
for (auto& engine : engine_service_.kSupportEngines) {
std::string engine_name{engine};
EngineLoad(engine_load_cmd, engine_name);
}

auto engine_unload_cmd =
engines_cmd->add_subcommand("unload", "Unload engine");
engine_unload_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines unload [engine_name]");
engine_unload_cmd->callback([this, engine_unload_cmd] {
if (std::exchange(executed_, true))
return;
if (engine_unload_cmd->get_subcommands().empty()) {
CLI_LOG("[engine_name] is required\n");
CLI_LOG(engine_unload_cmd->help());
}
});
engine_unload_cmd->group(kSubcommands);
for (auto& engine : engine_service_.kSupportEngines) {
std::string engine_name{engine};
EngineUnload(engine_unload_cmd, engine_name);
}

EngineGet(engines_cmd);
}

Expand Down Expand Up @@ -691,6 +728,44 @@ void CommandLineParser::EngineUpdate(CLI::App* parent,
});
}

void CommandLineParser::EngineUnload(CLI::App* parent,
const std::string& engine_name) {
auto sub_cmd = parent->add_subcommand(engine_name, "");
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines unload " +
engine_name);
sub_cmd->group(kEngineGroup);

sub_cmd->callback([this, engine_name] {
if (std::exchange(executed_, true))
return;
auto result = commands::EngineUnloadCmd().Exec(
cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort), engine_name);
if (result.has_error()) {
CTL_ERR(result.error());
}
});
}

void CommandLineParser::EngineLoad(CLI::App* parent,
const std::string& engine_name) {
auto sub_cmd = parent->add_subcommand(engine_name, "");
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines load " +
engine_name);
sub_cmd->group(kEngineGroup);

sub_cmd->callback([this, engine_name] {
if (std::exchange(executed_, true))
return;
auto result = commands::EngineLoadCmd().Exec(
cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort), engine_name);
if (result.has_error()) {
CTL_ERR(result.error());
}
});
}

void CommandLineParser::EngineUse(CLI::App* parent,
const std::string& engine_name) {
auto engine_use_cmd = parent->add_subcommand(engine_name, "");
Expand Down
8 changes: 5 additions & 3 deletions engine/cli/command_line_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
#include <memory>
#include <unordered_map>
#include "CLI/CLI.hpp"
#include "commands/hardware_list_cmd.h"
#include "services/engine_service.h"
#include "services/model_service.h"
#include "utils/config_yaml_utils.h"
#include "commands/hardware_list_cmd.h"
#include "common/hardware_config.h"

class CommandLineParser {
public:
Expand Down Expand Up @@ -40,6 +39,10 @@ class CommandLineParser {

void EngineUse(CLI::App* parent, const std::string& engine_name);

void EngineLoad(CLI::App* parent, const std::string& engine_name);

void EngineUnload(CLI::App* parent, const std::string& engine_name);

void ModelUpdate(CLI::App* parent);

CLI::App app_;
Expand All @@ -66,7 +69,6 @@ class CommandLineParser {

bool show_menu = false;


int port;
config_yaml_utils::CortexConfig config;
std::unordered_map<std::string, std::string> model_update_options;
Expand Down
2 changes: 1 addition & 1 deletion engine/cli/commands/config_upd_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void commands::ConfigUpdCmd::Exec(
}

auto update_cnf_result =
curl_utils::SimplePatch(url.ToFullPath(), json.toStyledString());
curl_utils::SimplePatchJson(url.ToFullPath(), json.toStyledString());
if (update_cnf_result.has_error()) {
CLI_LOG_ERROR(update_cnf_result.error());
return;
Expand Down
36 changes: 36 additions & 0 deletions engine/cli/commands/engine_load_cmd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "engine_load_cmd.h"
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
#include "utils/curl_utils.h"
#include "utils/logging_utils.h"
#include "utils/url_parser.h"

namespace commands {
cpp::result<void, std::string> EngineLoadCmd::Exec(const std::string& host,
int port,
const std::string& engine) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return cpp::fail("Failed to start server");
}
}

auto load_engine_url = url_parser::Url{
.protocol = "http",
.host = host + ":" + std::to_string(port),
.pathParams = {"v1", "engines", engine, "load"},
};
auto load_engine_result =
curl_utils::SimplePostJson(load_engine_url.ToFullPath());
if (load_engine_result.has_error()) {
CTL_ERR(load_engine_result.error());
return cpp::fail("Failed to load engine: " + load_engine_result.error());
}

CLI_LOG("Engine loaded successfully");
return {};
}
}; // namespace commands
13 changes: 13 additions & 0 deletions engine/cli/commands/engine_load_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>
#include "utils/result.hpp"

namespace commands {

class EngineLoadCmd {
public:
cpp::result<void, std::string> Exec(const std::string& host, int port,
const std::string& engine);
};
} // namespace commands
2 changes: 1 addition & 1 deletion engine/cli/commands/engine_uninstall_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void EngineUninstallCmd::Exec(const std::string& host, int port,
.host = host + ":" + std::to_string(port),
.pathParams = {"v1", "engines", engine}};

auto result = curl_utils::SimpleDelete(url.ToFullPath());
auto result = curl_utils::SimpleDeleteJson(url.ToFullPath());
if (result.has_error()) {
CTL_ERR(result.error());
return;
Expand Down
35 changes: 35 additions & 0 deletions engine/cli/commands/engine_unload_cmd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "engine_unload_cmd.h"
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
#include "utils/curl_utils.h"
#include "utils/logging_utils.h"
#include "utils/url_parser.h"

namespace commands {
cpp::result<void, std::string> EngineUnloadCmd::Exec(
const std::string& host, int port, const std::string& engine) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return cpp::fail("Failed to start server");
}
}

auto load_engine_url = url_parser::Url{
.protocol = "http",
.host = host + ":" + std::to_string(port),
.pathParams = {"v1", "engines", engine, "load"},
};
auto load_engine_result =
curl_utils::SimpleDeleteJson(load_engine_url.ToFullPath());
if (load_engine_result.has_error()) {
CTL_ERR(load_engine_result.error());
return cpp::fail("Failed to unload engine: " + load_engine_result.error());
}

CLI_LOG("Engine unloaded successfully");
return {};
}
}; // namespace commands
13 changes: 13 additions & 0 deletions engine/cli/commands/engine_unload_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>
#include "utils/result.hpp"

namespace commands {

class EngineUnloadCmd {
public:
cpp::result<void, std::string> Exec(const std::string& host, int port,
const std::string& engine);
};
} // namespace commands
10 changes: 5 additions & 5 deletions engine/common/engine_servicei.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include <json/value.h>
#include <string>
#include <vector>
#include "json/json.h"
#include "utils/result.hpp"

// TODO: namh think of the other name
Expand Down Expand Up @@ -37,12 +38,12 @@ struct EngineVariantResponse {
class EngineServiceI {
public:
virtual ~EngineServiceI() {}

virtual cpp::result<DefaultEngineVariant, std::string>
SetDefaultEngineVariant(const std::string& engine, const std::string& version,
const std::string& variant) = 0;

virtual cpp::result<DefaultEngineVariant, std::string>
virtual cpp::result<DefaultEngineVariant, std::string>
GetDefaultEngineVariant(const std::string& engine) = 0;

virtual cpp::result<std::vector<EngineVariantResponse>, std::string>
Expand All @@ -53,5 +54,4 @@ virtual cpp::result<DefaultEngineVariant, std::string>

virtual cpp::result<void, std::string> UnloadEngine(
const std::string& engine_name) = 0;

};
};
6 changes: 4 additions & 2 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ void Engines::SetDefaultEngineVariant(
resp->setStatusCode(k400BadRequest);
callback(resp);
} else {
auto resp =
cortex_utils::CreateCortexHttpJsonResponse(result.value().ToJson());
Json::Value res;
res["message"] = "Engine " + result.value().variant + " " +
result.value().version + " set as default";
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
resp->setStatusCode(k200OK);
callback(resp);
}
Expand Down
Loading
Loading