feat(worker): add the vbs remote-execution worker client
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 6s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 9s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 14s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 49s

This commit is contained in:
faumaray committed 2026-12-05 11:18:22 +03:00
1 parent 1ae72e4d07
commit 0674b7f9fe
10 files changed
+423 -1

No files matched your search

+131
View File
@@ -1449,6 +1449,117 @@ int execute_compdb_entries_command(const CompdbEntriesCmdOptions &opts) {
return 0;
}
struct WorkerCmdOptions {
std::optional<std::string> registry_arg;
std::optional<std::string> action_key_arg;
std::optional<std::string> record_arg;
std::string project_arg;
std::optional<std::string> build_arg;
std::string token_env{"VBS_REGISTRY_TOKEN"};
std::string lease_seconds_arg{"60"};
};
int execute_worker_command(const WorkerCmdOptions &opts) {
if (!opts.registry_arg || !opts.action_key_arg || !opts.record_arg) {
std::cerr << "vbs worker: --registry, --action-key, and --record are required\n";
return 2;
}
int lease_seconds = 60;
if (!Void::Str::to_int(opts.lease_seconds_arg, lease_seconds) || lease_seconds <= 0 ||
lease_seconds > 3600) {
std::cerr << "vbs worker: invalid --lease-seconds value '" << opts.lease_seconds_arg
<< "'\n";
return 2;
}
const std::string registry = *opts.registry_arg;
const std::string action_key = *opts.action_key_arg;
Package::RegistryAuth auth{Void::Configuration::get_env_or(opts.token_env, "")};
auto record_text = Void::FileSystem::read_file(*opts.record_arg);
if (!record_text) {
std::cerr << "vbs worker: " << record_text.error().message() << '\n';
return 1;
}
auto record_json = Void::Json::Strict::parse(*record_text);
const auto *record_object =
record_json ? std::get_if<Void::Json::Strict::Value::Object>(&record_json->value) : nullptr;
if (record_object == nullptr) {
std::cerr << "vbs worker: --record must contain a JSON object\n";
return 1;
}
const std::filesystem::path project_root{opts.project_arg};
const std::filesystem::path build_root =
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
auto state = State::StateStore::open(build_root / ".vbs");
if (!state) {
std::cerr << "vbs worker: " << state.error().message() << '\n';
return 1;
}
const std::string platform =
Toolchain::host_os_name() + "-" + Toolchain::host_architecture_name();
auto registration = Package::register_worker(registry, platform, auth);
if (!registration) {
std::cerr << "vbs worker: " << registration.error().message() << '\n';
return 1;
}
Void::Log::info("Worker", "Registered as {}", registration->worker_id);
auto leased =
Package::lease_action(registry, action_key, registration->worker_id, lease_seconds, auth);
if (!leased) {
std::cerr << "vbs worker: " << leased.error().message() << '\n';
return 1;
}
Void::Log::info("Worker", "Leased action {} until epoch {}", action_key, leased->expires_epoch);
State::RemoteCacheConfig remote;
remote.url = registry;
remote.bearer_token = auth.bearer_token;
state->configure_remote_cache(remote);
// Publish every referenced output blob, then the action record itself.
if (const auto outputs_it = record_object->find("outputs");
outputs_it != record_object->end()) {
if (const auto *outputs =
std::get_if<Void::Json::Strict::Value::Object>(&outputs_it->second.value)) {
for (const auto &[path, ref] : *outputs) {
const auto *ref_object = std::get_if<Void::Json::Strict::Value::Object>(&ref.value);
if (ref_object == nullptr) {
continue;
}
const auto sha_it = ref_object->find("sha256");
if (sha_it == ref_object->end()) {
continue;
}
const auto *sha = std::get_if<std::string>(&sha_it->second.value);
if (sha == nullptr) {
continue;
}
if (auto published = state->publish_remote_blob(*sha); !published) {
std::cerr << "vbs worker: publishing blob " << *sha << ": "
<< published.error().message() << '\n';
return 1;
}
}
}
}
if (auto published = state->publish_remote_action(action_key, *record_text); !published) {
std::cerr << "vbs worker: publishing action record: " << published.error().message()
<< '\n';
return 1;
}
if (auto submitted = Package::submit_action_result(registry, action_key, leased->lease_token,
*record_text, auth);
!submitted) {
std::cerr << "vbs worker: " << submitted.error().message() << '\n';
return 1;
}
std::cout << "worker " << registration->worker_id << " completed action " << action_key << '\n';
return 0;
}
struct MigrateCmdOptions {
std::string input_arg{"CMakeLists.txt"};
std::string output_arg{"build.cpp"};
@@ -2950,6 +3061,8 @@ int main(int argc, char **argv) {
VerifyCmdOptions verify_opts;
PresetsCmdOptions presets_opts;
MigrateCmdOptions migrate_opts;
WorkerCmdOptions worker_opts;
worker_opts.project_arg = cwd;
CompdbEntriesCmdOptions compdb_entries_opts;
publish_opts.project_arg = cwd;
BridgeCmdOptions bridge_opts;
@@ -3163,6 +3276,21 @@ int main(int argc, char **argv) {
verify_opts.trusted_key_files);
add_log_flags(verify_cmd, false); // `-v` is `--version` on verify.
auto &worker_cmd = root.subcommand(
"worker", "Run one remote-execution worker step (lease, publish, result upload)");
worker_cmd
.option("--registry", "-r", "Registry base URL (or VBS_REGISTRY_URL)",
worker_opts.registry_arg)
.option("--action-key", "", "Action key to lease and publish", worker_opts.action_key_arg)
.option("--record", "", "Action record JSON file to publish", worker_opts.record_arg)
.option_default("--project", "-C", "Project directory", worker_opts.project_arg, cwd)
.option("--build-dir", "-B", "Build directory", worker_opts.build_arg)
.option_default("--token-env", "", "Environment variable holding bearer token",
worker_opts.token_env, "VBS_REGISTRY_TOKEN")
.option_default("--lease-seconds", "", "Lease TTL in seconds (1..3600)",
worker_opts.lease_seconds_arg, "60");
add_log_flags(worker_cmd);
auto &migrate_cmd = root.subcommand(
"migrate", "Generate a build.cpp scaffold from a CMakeLists.txt (bounded subset)");
migrate_cmd
@@ -3666,6 +3794,9 @@ int main(int argc, char **argv) {
if (cmd == "migrate") {
return execute_migrate_command(migrate_opts);
}
if (cmd == "worker") {
return execute_worker_command(worker_opts);
}
if (cmd == "compdb-entries") {
return execute_compdb_entries_command(compdb_entries_opts);
}
+142
View File
@@ -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) {
+19
View File
@@ -1174,4 +1174,23 @@ std::size_t StateStore::recorded_action_count() const {
return db ? db->actions.size() : 0;
}
Void::Result::Result<std::string, Void::Result::Error>
StateStore::export_action_record_json(std::string_view action_key) const {
auto lock = Platform::FileLock::acquire(lock_file_, Platform::LockMode::Shared);
if (!lock) {
return std::unexpected{lock.error()};
}
auto db = load_db_locked();
if (!db) {
return std::unexpected{db.error()};
}
const auto it = db->actions.find(std::string{action_key});
if (it == db->actions.end()) {
return std::unexpected{
make_error(Void::System::SystemErrorCode::not_found,
std::format("no local action record for '{}'", action_key))};
}
return serialize_action(it->second);
}
} // namespace Void::Build::State