-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
508 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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,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 |
This file contains 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,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 |
This file contains 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 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 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
Oops, something went wrong.