feat(worker): add the vbs remote-execution worker client
This commit is contained in:
1 parent
1ae72e4d07
commit
0674b7f9fe
10 files changed
+423
-1
No files matched your search
@@ -797,6 +797,148 @@ fetch_tokens(std::string_view registry_url, const RegistryAuth &auth) {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::string json_quote_of(std::string_view text) {
|
||||
std::string out{"\""};
|
||||
for (const char c : text) {
|
||||
if (c == '"' || c == '\\') {
|
||||
out.push_back('\\');
|
||||
out.push_back(c);
|
||||
} else if (c == '\n') {
|
||||
out += "\\n";
|
||||
} else {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
out.push_back('"');
|
||||
return out;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string, std::less<>> bearer_headers(const RegistryAuth &auth) {
|
||||
std::map<std::string, std::string, std::less<>> headers;
|
||||
headers["Content-Type"] = "application/json";
|
||||
if (!auth.bearer_token.empty()) {
|
||||
headers["Authorization"] = "Bearer " + auth.bearer_token;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<WorkerRegistration, Void::Result::Error>
|
||||
register_worker(std::string_view registry_url, std::string_view platform,
|
||||
const RegistryAuth &auth) {
|
||||
auto parsed_url = parse_registry_url(registry_url);
|
||||
if (!parsed_url) {
|
||||
return std::unexpected{parsed_url.error()};
|
||||
}
|
||||
const std::string body = std::format("{{\"platform\":{}}}", json_quote_of(platform));
|
||||
auto reply =
|
||||
send_http_request(*parsed_url, "POST", "/api/v1/workers", bearer_headers(auth), body);
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status != 201) {
|
||||
return std::unexpected{
|
||||
make_error(reply->status == 401 || reply->status == 403
|
||||
? Void::System::SystemErrorCode::permission_denied
|
||||
: Void::System::SystemErrorCode::io_error,
|
||||
std::format("worker registration failed with HTTP {}", reply->status))};
|
||||
}
|
||||
auto json = Void::Json::Strict::parse(reply->body);
|
||||
const auto *object = json ? std::get_if<JsonVal::Object>(&json->value) : nullptr;
|
||||
if (!object) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"invalid worker registration response")};
|
||||
}
|
||||
WorkerRegistration registration;
|
||||
registration.worker_id = get_str(*object, "worker_id");
|
||||
registration.registered_epoch = get_i64(*object, "registered_epoch", 0);
|
||||
if (registration.worker_id.empty()) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"worker registration response has no worker_id")};
|
||||
}
|
||||
return registration;
|
||||
}
|
||||
|
||||
Void::Result::Result<LeasedAction, Void::Result::Error>
|
||||
lease_action(std::string_view registry_url, std::string_view action_key, std::string_view worker_id,
|
||||
std::int64_t lease_seconds, const RegistryAuth &auth) {
|
||||
auto parsed_url = parse_registry_url(registry_url);
|
||||
if (!parsed_url) {
|
||||
return std::unexpected{parsed_url.error()};
|
||||
}
|
||||
const std::string body =
|
||||
std::format("{{\"action_key\":{},\"worker_id\":{},\"lease_seconds\":{}}}",
|
||||
json_quote_of(action_key), json_quote_of(worker_id), lease_seconds);
|
||||
auto reply =
|
||||
send_http_request(*parsed_url, "POST", "/api/v1/actions/lease", bearer_headers(auth), body);
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status != 200) {
|
||||
return std::unexpected{make_error(
|
||||
reply->status == 404 ? Void::System::SystemErrorCode::not_found
|
||||
: reply->status == 409 ? Void::System::SystemErrorCode::busy
|
||||
: reply->status == 401 || reply->status == 403
|
||||
? Void::System::SystemErrorCode::permission_denied
|
||||
: Void::System::SystemErrorCode::io_error,
|
||||
std::format("action lease failed with HTTP {}: {}", reply->status, reply->body))};
|
||||
}
|
||||
auto json = Void::Json::Strict::parse(reply->body);
|
||||
const auto *object = json ? std::get_if<JsonVal::Object>(&json->value) : nullptr;
|
||||
if (!object) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error, "invalid action lease response")};
|
||||
}
|
||||
LeasedAction leased;
|
||||
leased.action_key = get_str(*object, "action_key");
|
||||
leased.worker_id = get_str(*object, "worker_id");
|
||||
leased.lease_token = get_str(*object, "lease_token");
|
||||
leased.expires_epoch = get_i64(*object, "expires_epoch", 0);
|
||||
if (const auto it = object->find("record"); it != object->end()) {
|
||||
auto serialized = Void::Json::Strict::serialize(it->second);
|
||||
if (!serialized) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"cannot serialize leased record")};
|
||||
}
|
||||
leased.record_json = *serialized;
|
||||
}
|
||||
if (leased.lease_token.empty()) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"action lease response has no lease_token")};
|
||||
}
|
||||
return leased;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> submit_action_result(std::string_view registry_url,
|
||||
std::string_view action_key,
|
||||
std::string_view lease_token,
|
||||
std::string_view record_json,
|
||||
const RegistryAuth &auth) {
|
||||
auto parsed_url = parse_registry_url(registry_url);
|
||||
if (!parsed_url) {
|
||||
return std::unexpected{parsed_url.error()};
|
||||
}
|
||||
const std::string path =
|
||||
"/api/v1/actions/" + Void::Encoding::Url::encode(action_key) + "/result";
|
||||
const std::string body = std::format("{{\"lease_token\":{},\"record\":{}}}",
|
||||
json_quote_of(lease_token), record_json);
|
||||
auto reply = send_http_request(*parsed_url, "POST", path, bearer_headers(auth), body);
|
||||
if (!reply) {
|
||||
return std::unexpected{reply.error()};
|
||||
}
|
||||
if (reply->status != 200) {
|
||||
return std::unexpected{
|
||||
make_error(reply->status == 409 ? Void::System::SystemErrorCode::busy
|
||||
: reply->status == 403 ? Void::System::SystemErrorCode::permission_denied
|
||||
: reply->status == 400 ? Void::System::SystemErrorCode::invalid_argument
|
||||
: Void::System::SystemErrorCode::io_error,
|
||||
std::format("action result upload failed with HTTP {}: {}", reply->status,
|
||||
reply->body))};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<bool, Void::Result::Error>
|
||||
set_release_yanked(std::string_view registry_url, std::string_view package_name,
|
||||
std::string_view version, bool yanked, const RegistryAuth &auth) {
|
||||
|
||||
Reference in new issue
Block a user