Skip to content

Commit

Permalink
feat: using libcurl to download
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Sep 10, 2024
1 parent 8c5fe88 commit ff8c324
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 155 deletions.
3 changes: 3 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(OPENSSL_USE_STATIC_LIBS TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
# set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/build-deps/_install)
# This is the critical line for installing another package

Expand Down Expand Up @@ -106,6 +107,7 @@ find_package(CLI11 CONFIG REQUIRED)
find_package(unofficial-minizip CONFIG REQUIRED)
find_package(LibArchive REQUIRED)
find_package(tabulate CONFIG REQUIRED)
find_package(CURL REQUIRED)

# Build using CMAKE-JS
if(DEFINED CMAKE_JS_INC)
Expand Down Expand Up @@ -150,6 +152,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE CLI11::CLI11)
target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::minizip::minizip)
target_link_libraries(${PROJECT_NAME} PRIVATE LibArchive::LibArchive)
target_link_libraries(${PROJECT_NAME} PRIVATE tabulate::tabulate)
target_link_libraries(${PROJECT_NAME} PRIVATE CURL::libcurl)

# Build using CMAKE-JS
if(DEFINED CMAKE_JS_INC)
Expand Down
35 changes: 12 additions & 23 deletions engine/commands/cortex_upd_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// clang-format off
#include "utils/cortex_utils.h"
// clang-format on
#include "cortex_upd_cmd.h"
#include "httplib.h"
#include "nlohmann/json.hpp"
Expand Down Expand Up @@ -84,24 +81,18 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
for (auto& asset : assets) {
auto asset_name = asset["name"].get<std::string>();
if (asset_name == matched_variant) {
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>();
auto file_name = asset["name"].get<std::string>();
CTL_INF("URL: " << full_url);

auto download_task = DownloadTask{.id = "cortex",
.type = DownloadType::Cortex,
.error = std::nullopt,
.items = {DownloadItem{
.id = "cortex",
.host = host,
.fileName = fileName,
.type = DownloadType::Cortex,
.path = path,
}}};
auto download_task{DownloadTask{.id = "cortex",
.type = DownloadType::Cortex,
.items = {DownloadItem{
.id = "cortex",
.fileName = file_name,
.type = DownloadType::Cortex,
.path = full_url,
}}}};

DownloadService download_service;
download_service.AddDownloadTask(
Expand Down Expand Up @@ -157,16 +148,14 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
// Download file
std::string version = v.empty() ? "latest" : std::move(v);
std::ostringstream release_path;
release_path << "cortex/" << version << "/" << system_info.os << "-"
<< system_info.arch << "/" << kNightlyFileName;
release_path << kNightlyHost << "/cortex/" << version << "/" << system_info.os
<< "-" << system_info.arch << "/" << kNightlyFileName;
CTL_INF("Engine release path: " << kNightlyHost << release_path.str());

auto download_task = DownloadTask{.id = "cortex",
.type = DownloadType::Cortex,
.error = std::nullopt,
.items = {DownloadItem{
.id = "cortex",
.host = kNightlyHost,
.fileName = kNightlyFileName,
.type = DownloadType::Cortex,
.path = release_path.str(),
Expand Down Expand Up @@ -195,4 +184,4 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
return ReplaceBinaryInflight(src, dst);
}

} // namespace commands
} // namespace commands
32 changes: 12 additions & 20 deletions engine/commands/engine_init_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,18 @@ bool EngineInitCmd::Exec() const {
for (auto& asset : assets) {
auto assetName = asset["name"].get<std::string>();
if (assetName == matched_variant) {
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>();
auto file_name = asset["name"].get<std::string>();
CTL_INF("URL: " << full_url);

auto downloadTask = DownloadTask{.id = engineName_,
.type = DownloadType::Engine,
.error = std::nullopt,
.items = {DownloadItem{
.id = engineName_,
.host = host,
.fileName = fileName,
.type = DownloadType::Engine,
.path = path,
}}};
auto downloadTask{DownloadTask{.id = engineName_,
.type = DownloadType::Engine,
.items = {DownloadItem{
.id = engineName_,
.fileName = file_name,
.type = DownloadType::Engine,
.path = full_url,
}}}};

DownloadService download_service;
download_service.AddDownloadTask(downloadTask, [this](
Expand Down Expand Up @@ -202,25 +196,23 @@ bool EngineInitCmd::Exec() const {
}

std::ostringstream cuda_toolkit_path;
cuda_toolkit_path << "dist/cuda-dependencies/"
cuda_toolkit_path << jan_host << "/" << "dist/cuda-dependencies/"
<< cuda_driver_version << "/" << system_info.os
<< "/" << cuda_toolkit_file_name;

LOG_DEBUG << "Cuda toolkit download url: " << jan_host
<< cuda_toolkit_path.str();

auto downloadCudaToolkitTask = DownloadTask{
auto downloadCudaToolkitTask{DownloadTask{
.id = download_id,
.type = DownloadType::CudaToolkit,
.error = std::nullopt,
.items = {DownloadItem{
.id = download_id,
.host = jan_host,
.fileName = cuda_toolkit_file_name,
.type = DownloadType::CudaToolkit,
.path = cuda_toolkit_path.str(),
}},
};
}};

download_service.AddDownloadTask(
downloadCudaToolkitTask,
Expand Down
4 changes: 2 additions & 2 deletions engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void CommandLineParser::EngineInstall(CLI::App* parent,
std::string& version) {
auto install_engine_cmd = parent->add_subcommand(engine_name, "");

install_engine_cmd->callback([&] {
install_engine_cmd->callback([=] {
commands::EngineInitCmd eic(engine_name, version);
eic.Exec();
});
Expand All @@ -229,7 +229,7 @@ void CommandLineParser::EngineUninstall(CLI::App* parent,
const std::string& engine_name) {
auto uninstall_engine_cmd = parent->add_subcommand(engine_name, "");

uninstall_engine_cmd->callback([&] {
uninstall_engine_cmd->callback([=] {
commands::EngineUninstallCmd cmd(engine_name);
cmd.Exec();
});
Expand Down
24 changes: 9 additions & 15 deletions engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,18 @@ void Engines::InstallEngine(
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>();
auto name = 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,
}}};
auto downloadTask{DownloadTask{.id = engine,
.type = DownloadType::Engine,
.items = {DownloadItem{
.id = engine,
.fileName = name,
.type = DownloadType::Engine,
.path = full_url,
}}}};

DownloadService().AddAsyncDownloadTask(
downloadTask,
Expand Down
10 changes: 10 additions & 0 deletions engine/exceptions/failed_curl_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdexcept>

class FailedCurlException : public std::runtime_error {

public:
FailedCurlException(const std::string& message)
: std::runtime_error(message) {}
};
10 changes: 10 additions & 0 deletions engine/exceptions/failed_init_curl_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdexcept>

class FailedInitCurlException : public std::runtime_error {
constexpr static auto kErrorMessage = "Failed to init CURL";

public:
FailedInitCurlException() : std::runtime_error(kErrorMessage) {}
};
10 changes: 10 additions & 0 deletions engine/exceptions/failed_open_file_exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdexcept>

class FailedOpenFileException : public std::runtime_error {

public:
FailedOpenFileException(const std::string& message)
: std::runtime_error(message) {}
};
Loading

0 comments on commit ff8c324

Please sign in to comment.