2022-03-08 22:21:41 +00:00
|
|
|
#include "serialise.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
#include "path-with-outputs.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "build-result.hh"
|
|
|
|
#include "worker-protocol.hh"
|
2022-03-08 21:53:26 +00:00
|
|
|
#include "worker-protocol-impl.hh"
|
2022-03-08 22:21:41 +00:00
|
|
|
#include "archive.hh"
|
|
|
|
#include "derivations.hh"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-05-18 02:04:59 +00:00
|
|
|
namespace nix {
|
2022-03-08 22:21:41 +00:00
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
std::string WorkerProto::Serialise<std::string>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
return readString(from);
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<std::string>::write(const Store & store, Sink & out, const std::string & str)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
StorePath WorkerProto::Serialise<StorePath>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
return store.parseStorePath(readString(from));
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<StorePath>::write(const Store & store, Sink & out, const StorePath & storePath)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << store.printStorePath(storePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
std::optional<TrustedFlag> WorkerProto::Serialise<std::optional<TrustedFlag>>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
auto temp = readNum<uint8_t>(from);
|
|
|
|
switch (temp) {
|
|
|
|
case 0:
|
|
|
|
return std::nullopt;
|
|
|
|
case 1:
|
|
|
|
return { Trusted };
|
|
|
|
case 2:
|
|
|
|
return { NotTrusted };
|
|
|
|
default:
|
|
|
|
throw Error("Invalid trusted status from remote");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<std::optional<TrustedFlag>>::write(const Store & store, Sink & out, const std::optional<TrustedFlag> & optTrusted)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
if (!optTrusted)
|
|
|
|
out << (uint8_t)0;
|
|
|
|
else {
|
|
|
|
switch (*optTrusted) {
|
|
|
|
case Trusted:
|
|
|
|
out << (uint8_t)1;
|
|
|
|
break;
|
|
|
|
case NotTrusted:
|
|
|
|
out << (uint8_t)2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
ContentAddress WorkerProto::Serialise<ContentAddress>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
return ContentAddress::parse(readString(from));
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<ContentAddress>::write(const Store & store, Sink & out, const ContentAddress & ca)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << renderContentAddress(ca);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
DerivedPath WorkerProto::Serialise<DerivedPath>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
auto s = readString(from);
|
|
|
|
return DerivedPath::parseLegacy(store, s);
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<DerivedPath>::write(const Store & store, Sink & out, const DerivedPath & req)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << req.to_string_legacy(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
Realisation WorkerProto::Serialise<Realisation>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
std::string rawInput = readString(from);
|
|
|
|
return Realisation::fromJSON(
|
|
|
|
nlohmann::json::parse(rawInput),
|
|
|
|
"remote-protocol"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<Realisation>::write(const Store & store, Sink & out, const Realisation & realisation)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << realisation.toJSON().dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
DrvOutput WorkerProto::Serialise<DrvOutput>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
return DrvOutput::parse(readString(from));
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<DrvOutput>::write(const Store & store, Sink & out, const DrvOutput & drvOutput)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << drvOutput.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
KeyedBuildResult WorkerProto::Serialise<KeyedBuildResult>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
2023-05-26 15:07:25 +00:00
|
|
|
auto path = WorkerProto::Serialise<DerivedPath>::read(store, from);
|
|
|
|
auto br = WorkerProto::Serialise<BuildResult>::read(store, from);
|
2022-03-08 22:21:41 +00:00
|
|
|
return KeyedBuildResult {
|
|
|
|
std::move(br),
|
|
|
|
/* .path = */ std::move(path),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<KeyedBuildResult>::write(const Store & store, Sink & to, const KeyedBuildResult & res)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
2023-05-26 15:07:25 +00:00
|
|
|
WorkerProto::write(store, to, res.path);
|
|
|
|
WorkerProto::write(store, to, static_cast<const BuildResult &>(res));
|
2022-03-08 22:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
BuildResult WorkerProto::Serialise<BuildResult>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
BuildResult res;
|
|
|
|
res.status = (BuildResult::Status) readInt(from);
|
|
|
|
from
|
|
|
|
>> res.errorMsg
|
|
|
|
>> res.timesBuilt
|
|
|
|
>> res.isNonDeterministic
|
|
|
|
>> res.startTime
|
|
|
|
>> res.stopTime;
|
2023-05-26 15:07:25 +00:00
|
|
|
auto builtOutputs = WorkerProto::Serialise<DrvOutputs>::read(store, from);
|
2022-03-08 22:21:41 +00:00
|
|
|
for (auto && [output, realisation] : builtOutputs)
|
|
|
|
res.builtOutputs.insert_or_assign(
|
|
|
|
std::move(output.outputName),
|
|
|
|
std::move(realisation));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<BuildResult>::write(const Store & store, Sink & to, const BuildResult & res)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
to
|
|
|
|
<< res.status
|
|
|
|
<< res.errorMsg
|
|
|
|
<< res.timesBuilt
|
|
|
|
<< res.isNonDeterministic
|
|
|
|
<< res.startTime
|
|
|
|
<< res.stopTime;
|
|
|
|
DrvOutputs builtOutputs;
|
|
|
|
for (auto & [output, realisation] : res.builtOutputs)
|
|
|
|
builtOutputs.insert_or_assign(realisation.id, realisation);
|
2023-05-26 15:07:25 +00:00
|
|
|
WorkerProto::write(store, to, builtOutputs);
|
2022-03-08 22:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
std::optional<StorePath> WorkerProto::Serialise<std::optional<StorePath>>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
auto s = readString(from);
|
|
|
|
return s == "" ? std::optional<StorePath> {} : store.parseStorePath(s);
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<std::optional<StorePath>>::write(const Store & store, Sink & out, const std::optional<StorePath> & storePathOpt)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << (storePathOpt ? store.printStorePath(*storePathOpt) : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
std::optional<ContentAddress> WorkerProto::Serialise<std::optional<ContentAddress>>::read(const Store & store, Source & from)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
return ContentAddress::parseOpt(readString(from));
|
|
|
|
}
|
|
|
|
|
2023-05-26 15:07:25 +00:00
|
|
|
void WorkerProto::Serialise<std::optional<ContentAddress>>::write(const Store & store, Sink & out, const std::optional<ContentAddress> & caOpt)
|
2022-03-08 22:21:41 +00:00
|
|
|
{
|
|
|
|
out << (caOpt ? renderContentAddress(*caOpt) : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|