Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Oct 31, 2024
1 parent 923882d commit 76481c5
Show file tree
Hide file tree
Showing 16 changed files with 508 additions and 86 deletions.
92 changes: 71 additions & 21 deletions engine/cli/commands/engine_install_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "engine_install_cmd.h"
#include <future>
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
#include "utils/download_progress.h"
#include "utils/json_helper.h"
#include "utils/logging_utils.h"

namespace commands {
Expand Down Expand Up @@ -38,27 +38,77 @@ bool EngineInstallCmd::Exec(const std::string& engine,
[&dp, &engine] { return dp.Handle(engine); });
CLI_LOG("Validating download items, please wait..")

httplib::Client cli(host_ + ":" + std::to_string(port_));
Json::Value json_data;
json_data["version"] = version.empty() ? "latest" : version;
auto data_str = json_data.toStyledString();
cli.set_read_timeout(std::chrono::seconds(60));
auto res = cli.Post("/v1/engines/install/" + engine, httplib::Headers(),
data_str.data(), data_str.size(), "application/json");
auto versions_url = url_parser::Url{
.protocol = "http",
.host = host_ + ":" + std::to_string(port_),
.pathParams = {"v1", "engines", engine, "versions"},
};
auto versions_result = curl_utils::SimpleGetJson(versions_url.ToFullPath());
if (versions_result.has_error()) {
CTL_ERR(versions_result.error());
return false;
}
std::vector<std::string> version_selections;
for (const auto& release_version : versions_result.value()) {
version_selections.push_back(release_version["name"].asString());
}

if (res) {
if (res->status != httplib::StatusCode::OK_200) {
auto root = json_helper::ParseJsonString(res->body);
CLI_LOG(root["message"].asString());
dp.ForceStop();
return false;
} else {
CLI_LOG("Start downloading..");
}
} else {
auto err = res.error();
CTL_ERR("HTTP error: " << httplib::to_string(err));
dp.ForceStop();
auto selected_version =
cli_selection_utils::PrintSelection(version_selections);
if (selected_version == std::nullopt) {
CTL_ERR("Invalid version selection");
return false;
}
std::cout << "Selected version: " << selected_version.value() << std::endl;

auto variant_url = url_parser::Url{
.protocol = "http",
.host = host_ + ":" + std::to_string(port_),
.pathParams =
{
"v1",
"engines",
engine,
"versions",
selected_version.value(),
},
};
auto variant_result = curl_utils::SimpleGetJson(variant_url.ToFullPath());
if (variant_result.has_error()) {
CTL_ERR(variant_result.error());
return false;
}

std::vector<std::string> variant_selections;
for (const auto& variant : variant_result.value()) {
variant_selections.push_back(variant["name"].asString());
}
auto selected_variant =
cli_selection_utils::PrintSelection(variant_selections);
if (selected_variant == std::nullopt) {
CTL_ERR("Invalid variant selection");
return false;
}
std::cout << "Selected " << selected_variant.value() << " - "
<< selected_version.value() << std::endl;

auto install_url =
url_parser::Url{.protocol = "http",
.host = host_ + ":" + std::to_string(port_),
.pathParams =
{
"v1",
"engines",
engine,
},
.queries = {
{"version", selected_version.value()},
{"variant", selected_variant.value()},
}};

auto response = curl_utils::SimplePostJson(install_url.ToFullPath());
if (response.has_error()) {
CTL_ERR(response.error());
return false;
}

Expand Down
112 changes: 112 additions & 0 deletions engine/cli/commands/engine_update_cmd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "engine_update_cmd.h"
#include <future>
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
#include "utils/download_progress.h"
#include "utils/logging_utils.h"

namespace commands {
bool EngineUpdateCmd::Exec(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 false;
}
}
// TODO: implement this
DownloadProgress dp;
dp.Connect(host_, port_);
// engine can be small, so need to start ws first
auto dp_res = std::async(std::launch::deferred,
[&dp, &engine] { return dp.Handle(engine); });
CLI_LOG("Validating download items, please wait..")

auto versions_url = url_parser::Url{
.protocol = "http",
.host = host_ + ":" + std::to_string(port_),
.pathParams = {"v1", "engines", engine, "versions"},
};
auto versions_result = curl_utils::SimpleGetJson(versions_url.ToFullPath());
if (versions_result.has_error()) {
CTL_ERR(versions_result.error());
return false;
}
std::vector<std::string> version_selections;
for (const auto& release_version : versions_result.value()) {
version_selections.push_back(release_version["name"].asString());
}

auto selected_version =
cli_selection_utils::PrintSelection(version_selections);
if (selected_version == std::nullopt) {
CTL_ERR("Invalid version selection");
return false;
}
std::cout << "Selected version: " << selected_version.value() << std::endl;

auto variant_url = url_parser::Url{
.protocol = "http",
.host = host_ + ":" + std::to_string(port_),
.pathParams =
{
"v1",
"engines",
engine,
"versions",
selected_version.value(),
},
};
auto variant_result = curl_utils::SimpleGetJson(variant_url.ToFullPath());
if (variant_result.has_error()) {
CTL_ERR(variant_result.error());
return false;
}

std::vector<std::string> variant_selections;
for (const auto& variant : variant_result.value()) {
variant_selections.push_back(variant["name"].asString());
}
auto selected_variant =
cli_selection_utils::PrintSelection(variant_selections);
if (selected_variant == std::nullopt) {
CTL_ERR("Invalid variant selection");
return false;
}
std::cout << "Selected " << selected_variant.value() << " - "
<< selected_version.value() << std::endl;

auto install_url =
url_parser::Url{.protocol = "http",
.host = host_ + ":" + std::to_string(port_),
.pathParams =
{
"v1",
"engines",
engine,
},
.queries = {
{"version", selected_version.value()},
{"variant", selected_variant.value()},
}};

auto response = curl_utils::SimplePostJson(install_url.ToFullPath());
if (response.has_error()) {
CTL_ERR(response.error());
return false;
}

if (!dp_res.get())
return false;

bool check_cuda_download = !system_info_utils::GetCudaVersion().empty();
if (check_cuda_download) {
if (!dp.Handle("cuda"))
return false;
}

CLI_LOG("Engine " << engine << " downloaded successfully!")
return true;
}
}; // namespace commands
23 changes: 23 additions & 0 deletions engine/cli/commands/engine_update_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <string>
#include "services/engine_service.h"

namespace commands {

class EngineUpdateCmd {
public:
explicit EngineUpdateCmd(std::shared_ptr<DownloadService> download_service,
const std::string& host, int port)
: engine_service_{EngineService(download_service)},
host_(host),
port_(port) {};

bool Exec(const std::string& engine);

private:
EngineService engine_service_;
std::string host_;
int port_;
};
} // namespace commands
10 changes: 5 additions & 5 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ void Engines::ListEngine(
void Engines::UninstallEngine(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine, const std::string& version,
const std::string& variant) {
const std::string& engine, const std::optional<std::string> version,
const std::optional<std::string> variant) {

auto result =
engine_service_->UninstallEngineVariant(engine, variant, version);
engine_service_->UninstallEngineVariant(engine, version, variant);

Json::Value ret;
if (result.has_error()) {
Expand Down Expand Up @@ -128,7 +128,7 @@ void Engines::GetEngineVariants(
callback(resp);
}

void Engines::InstallEngineVariant(
void Engines::InstallEngine(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine, const std::optional<std::string> version,
Expand All @@ -152,7 +152,7 @@ void Engines::InstallEngineVariant(
}
}

void Engines::GetEnginesInstalledVariants(
void Engines::GetInstalledEngineVariants(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine) const {
Expand Down
56 changes: 34 additions & 22 deletions engine/controllers/engines.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,36 @@ class Engines : public drogon::HttpController<Engines, false> {
public:
METHOD_LIST_BEGIN

METHOD_ADD(Engines::InstallEngineVariant, "/{1}?version={2}&variant={3}",
Post);
METHOD_ADD(Engines::UninstallEngine, "/{1}/{2}/{3}", Delete);
METHOD_ADD(Engines::ListEngine, "", Get);

METHOD_ADD(Engines::GetEngineVersions, "/{1}/versions", Get);
METHOD_ADD(Engines::GetEngineVariants, "/{1}/versions/{2}", Get);
METHOD_ADD(Engines::InstallEngineVariant, "/{1}/versions/{2}/{3}", Post);
METHOD_ADD(Engines::GetEnginesInstalledVariants, "/{1}", Get);

// METHOD_ADD(Engines::GetLatestEngineVersion, "/{1}/update", Get);
METHOD_ADD(Engines::UpdateEngine, "/{1}/update", Post);
METHOD_ADD(Engines::SetDefaultEngineVariant, "/{1}/default/{2}/{3}", Post);
METHOD_ADD(Engines::GetInstalledEngineVariants, "/{1}", Get);
METHOD_ADD(Engines::InstallEngine, "/{1}?version={2}&variant={3}", Post);
METHOD_ADD(Engines::UninstallEngine, "/{1}?version={2}&variant={3}", Delete);
METHOD_ADD(Engines::SetDefaultEngineVariant,
"/{1}/default?version={2}&variant={3}", Post);
METHOD_ADD(Engines::GetDefaultEngineVariant, "/{1}/default", Get);

METHOD_ADD(Engines::LoadEngine, "/{1}/load", Post);
METHOD_ADD(Engines::UnloadEngine, "/{1}/load", Delete);
METHOD_ADD(Engines::UpdateEngine, "/{1}/update", Post);
METHOD_ADD(Engines::ListEngine, "", Get);
METHOD_ADD(Engines::GetEngineVersions, "/{1}/versions", Get);
METHOD_ADD(Engines::GetEngineVariants, "/{1}/versions/{2}", Get);

ADD_METHOD_TO(Engines::UninstallEngine, "/v1/engines/{1}/{2}/{3}", Delete);
ADD_METHOD_TO(Engines::GetInstalledEngineVariants, "/v1/engines/{1}", Get);
ADD_METHOD_TO(Engines::InstallEngine,
"/v1/engines/{1}?version={2}&variant={3}", Post);
ADD_METHOD_TO(Engines::UninstallEngine,
"/v1/engines/{1}?version={2}&variant={3}", Delete);
ADD_METHOD_TO(Engines::SetDefaultEngineVariant,
"/v1/engines/{1}/default?version={2}&variant={3}", Post);
ADD_METHOD_TO(Engines::GetDefaultEngineVariant, "/v1/engines/{1}/default",
Get);

ADD_METHOD_TO(Engines::LoadEngine, "/v1/engines/{1}/load", Post);
ADD_METHOD_TO(Engines::UnloadEngine, "/v1/engines/{1}/load", Post);
ADD_METHOD_TO(Engines::UpdateEngine, "/v1/engines/{1}/update", Post);
ADD_METHOD_TO(Engines::GetEngineVersions, "/v1/engines/{1}/versions", Get);
ADD_METHOD_TO(Engines::GetEngineVariants, "/v1/engines/{1}/versions/{2}",
Get);

METHOD_LIST_END

Expand All @@ -42,8 +53,9 @@ class Engines : public drogon::HttpController<Engines, false> {

void UninstallEngine(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine, const std::string& version,
const std::string& variant);
const std::string& engine,
const std::optional<std::string> version,
const std::optional<std::string> variant);

void GetEngineVersions(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
Expand All @@ -54,13 +66,13 @@ class Engines : public drogon::HttpController<Engines, false> {
const std::string& engine,
const std::string& version) const;

void InstallEngineVariant(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine, const std::optional<std::string> version,
const std::optional<std::string> variant_name);
void InstallEngine(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine,
const std::optional<std::string> version,
const std::optional<std::string> variant_name);

void GetEnginesInstalledVariants(
void GetInstalledEngineVariants(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback,
const std::string& engine) const;
Expand Down
14 changes: 13 additions & 1 deletion engine/e2e-test/test_api_engine_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,17 @@ def setup_and_teardown(self):
stop_server()

def test_engines_install_llamacpp_should_be_successful(self):
response = requests.post("http://localhost:3928/engines/install/llama-cpp")
response = requests.post("http://localhost:3928/v1/engines/llama-cpp")
assert response.status_code == 200

def test_engines_install_llamacpp_specific_version_and_variant(self):
response = requests.post(
"http://localhost:3928/v1/engines/llama-cpp?version=v0.1.35-27.10.24&variant=linux-amd64-avx-cuda-11-7"
)
assert response.status_code == 200

def test_engines_install_llamacpp_specific_version_and_null_variant(self):
response = requests.post(
"http://localhost:3928/v1/engines/llama-cpp?version=v0.1.35-27.10.24"
)
assert response.status_code == 200
Loading

0 comments on commit 76481c5

Please sign in to comment.