2024-07-18 06:32:27 +03:00
|
|
|
#include "http-binary-cache-store.hh"
|
2020-04-07 00:57:28 +03:00
|
|
|
#include "filetransfer.hh"
|
2016-03-04 18:23:42 +02:00
|
|
|
#include "globals.hh"
|
2016-04-20 15:12:38 +03:00
|
|
|
#include "nar-info-disk-cache.hh"
|
2020-09-21 19:40:11 +03:00
|
|
|
#include "callback.hh"
|
2016-03-03 19:03:34 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-05-30 14:33:05 +03:00
|
|
|
MakeError(UploadToHTTP, Error);
|
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
|
|
|
|
HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig(
|
|
|
|
std::string_view scheme,
|
|
|
|
std::string_view _cacheUri,
|
|
|
|
const Params & params)
|
|
|
|
: StoreConfig(params)
|
|
|
|
, BinaryCacheStoreConfig(params)
|
|
|
|
, cacheUri(
|
|
|
|
std::string { scheme }
|
|
|
|
+ "://"
|
|
|
|
+ (!_cacheUri.empty()
|
|
|
|
? _cacheUri
|
|
|
|
: throw UsageError("`%s` Store requires a non-empty authority in Store URL", scheme)))
|
2020-09-10 11:55:51 +03:00
|
|
|
{
|
2024-07-18 06:32:27 +03:00
|
|
|
while (!cacheUri.empty() && cacheUri.back() == '/')
|
|
|
|
cacheUri.pop_back();
|
|
|
|
}
|
2020-09-14 15:04:02 +03:00
|
|
|
|
2023-03-21 15:03:05 +02:00
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
std::string HttpBinaryCacheStoreConfig::doc()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
#include "http-binary-cache-store.md"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:55:51 +03:00
|
|
|
|
2020-12-20 17:33:12 +02:00
|
|
|
class HttpBinaryCacheStore : public virtual HttpBinaryCacheStoreConfig, public virtual BinaryCacheStore
|
2016-03-03 19:03:34 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2018-09-07 18:08:43 +03:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
bool enabled = true;
|
|
|
|
std::chrono::steady_clock::time_point disabledUntil;
|
|
|
|
};
|
|
|
|
|
|
|
|
Sync<State> _state;
|
|
|
|
|
2016-03-03 19:03:34 +02:00
|
|
|
public:
|
|
|
|
|
2016-05-04 21:15:41 +03:00
|
|
|
HttpBinaryCacheStore(
|
2024-01-25 17:31:52 +02:00
|
|
|
std::string_view scheme,
|
2024-07-18 06:32:27 +03:00
|
|
|
PathView cacheUri,
|
2020-09-08 15:50:23 +03:00
|
|
|
const Params & params)
|
2020-09-11 12:06:18 +03:00
|
|
|
: StoreConfig(params)
|
2020-12-20 17:33:12 +02:00
|
|
|
, BinaryCacheStoreConfig(params)
|
2024-07-18 06:32:27 +03:00
|
|
|
, HttpBinaryCacheStoreConfig(scheme, cacheUri, params)
|
2020-12-20 17:33:12 +02:00
|
|
|
, Store(params)
|
2020-09-11 12:06:18 +03:00
|
|
|
, BinaryCacheStore(params)
|
2016-03-03 19:03:34 +02:00
|
|
|
{
|
2016-04-20 15:12:38 +03:00
|
|
|
diskCache = getNarInfoDiskCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getUri() override
|
|
|
|
{
|
|
|
|
return cacheUri;
|
2016-03-03 19:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init() override
|
|
|
|
{
|
|
|
|
// FIXME: do this lazily?
|
2023-01-17 20:54:47 +02:00
|
|
|
if (auto cacheInfo = diskCache->upToDateCacheExists(cacheUri)) {
|
2021-09-22 15:15:35 +03:00
|
|
|
wantMassQuery.setDefault(cacheInfo->wantMassQuery);
|
|
|
|
priority.setDefault(cacheInfo->priority);
|
2019-12-17 18:17:53 +02:00
|
|
|
} else {
|
2016-05-30 14:33:05 +03:00
|
|
|
try {
|
|
|
|
BinaryCacheStore::init();
|
|
|
|
} catch (UploadToHTTP &) {
|
2018-01-31 16:12:27 +02:00
|
|
|
throw Error("'%s' does not appear to be a binary cache", cacheUri);
|
2016-05-30 14:33:05 +03:00
|
|
|
}
|
2019-12-17 18:17:53 +02:00
|
|
|
diskCache->createCache(cacheUri, storeDir, wantMassQuery, priority);
|
2016-04-20 15:12:38 +03:00
|
|
|
}
|
2016-03-03 19:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2018-09-07 18:08:43 +03:00
|
|
|
void maybeDisable()
|
|
|
|
{
|
|
|
|
auto state(_state.lock());
|
|
|
|
if (state->enabled && settings.tryFallback) {
|
|
|
|
int t = 60;
|
|
|
|
printError("disabling binary cache '%s' for %s seconds", getUri(), t);
|
|
|
|
state->enabled = false;
|
|
|
|
state->disabledUntil = std::chrono::steady_clock::now() + std::chrono::seconds(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkEnabled()
|
|
|
|
{
|
|
|
|
auto state(_state.lock());
|
|
|
|
if (state->enabled) return;
|
|
|
|
if (std::chrono::steady_clock::now() > state->disabledUntil) {
|
|
|
|
state->enabled = true;
|
|
|
|
debug("re-enabling binary cache '%s'", getUri());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw SubstituterDisabled("substituter '%s' is disabled", getUri());
|
|
|
|
}
|
|
|
|
|
2016-03-03 19:03:34 +02:00
|
|
|
bool fileExists(const std::string & path) override
|
|
|
|
{
|
2018-09-07 18:08:43 +03:00
|
|
|
checkEnabled();
|
|
|
|
|
2016-03-03 19:03:34 +02:00
|
|
|
try {
|
2020-09-01 17:41:42 +03:00
|
|
|
FileTransferRequest request(makeRequest(path));
|
2016-09-14 17:00:40 +03:00
|
|
|
request.head = true;
|
2020-04-07 00:43:43 +03:00
|
|
|
getFileTransfer()->download(request);
|
2016-03-03 19:03:34 +02:00
|
|
|
return true;
|
2020-04-07 00:43:43 +03:00
|
|
|
} catch (FileTransferError & e) {
|
2016-03-30 12:17:51 +03:00
|
|
|
/* S3 buckets return 403 if a file doesn't exist and the
|
|
|
|
bucket is unlistable, so treat 403 as 404. */
|
2020-04-07 00:43:43 +03:00
|
|
|
if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden)
|
2016-03-03 19:03:34 +02:00
|
|
|
return false;
|
2018-09-07 18:08:43 +03:00
|
|
|
maybeDisable();
|
2016-03-03 19:03:34 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 16:26:01 +02:00
|
|
|
void upsertFile(const std::string & path,
|
2020-07-13 21:07:19 +03:00
|
|
|
std::shared_ptr<std::basic_iostream<char>> istream,
|
2017-03-14 16:26:01 +02:00
|
|
|
const std::string & mimeType) override
|
2016-03-03 19:03:34 +02:00
|
|
|
{
|
2020-09-01 17:41:42 +03:00
|
|
|
auto req = makeRequest(path);
|
2022-01-17 23:20:05 +02:00
|
|
|
req.data = StreamToSourceAdapter(istream).drain();
|
2018-01-31 16:12:27 +02:00
|
|
|
req.mimeType = mimeType;
|
2018-01-26 21:12:30 +02:00
|
|
|
try {
|
2020-04-07 00:43:43 +03:00
|
|
|
getFileTransfer()->upload(req);
|
|
|
|
} catch (FileTransferError & e) {
|
2018-06-01 15:14:22 +03:00
|
|
|
throw UploadToHTTP("while uploading to HTTP binary cache at '%s': %s", cacheUri, e.msg());
|
2018-01-26 21:12:30 +02:00
|
|
|
}
|
2016-03-03 19:03:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 00:43:43 +03:00
|
|
|
FileTransferRequest makeRequest(const std::string & path)
|
2016-03-03 19:03:34 +02:00
|
|
|
{
|
2020-09-01 17:41:42 +03:00
|
|
|
return FileTransferRequest(
|
|
|
|
hasPrefix(path, "https://") || hasPrefix(path, "http://") || hasPrefix(path, "file://")
|
|
|
|
? path
|
|
|
|
: cacheUri + "/" + path);
|
|
|
|
|
2018-03-28 01:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void getFile(const std::string & path, Sink & sink) override
|
|
|
|
{
|
2018-09-07 18:08:43 +03:00
|
|
|
checkEnabled();
|
2018-03-28 01:01:47 +03:00
|
|
|
auto request(makeRequest(path));
|
|
|
|
try {
|
2020-04-07 00:43:43 +03:00
|
|
|
getFileTransfer()->download(std::move(request), sink);
|
|
|
|
} catch (FileTransferError & e) {
|
|
|
|
if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden)
|
2018-03-28 01:01:47 +03:00
|
|
|
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache '%s'", path, getUri());
|
2018-09-07 18:08:43 +03:00
|
|
|
maybeDisable();
|
2018-03-28 01:01:47 +03:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getFile(const std::string & path,
|
2022-01-17 23:20:05 +02:00
|
|
|
Callback<std::optional<std::string>> callback) noexcept override
|
2018-03-28 01:01:47 +03:00
|
|
|
{
|
2022-05-30 14:24:04 +03:00
|
|
|
try {
|
|
|
|
checkEnabled();
|
|
|
|
} catch (...) {
|
|
|
|
callback.rethrow();
|
|
|
|
return;
|
|
|
|
}
|
2018-09-07 18:08:43 +03:00
|
|
|
|
2019-07-10 20:46:15 +03:00
|
|
|
auto request(makeRequest(path));
|
|
|
|
|
2019-09-03 13:51:35 +03:00
|
|
|
auto callbackPtr = std::make_shared<decltype(callback)>(std::move(callback));
|
|
|
|
|
2020-04-07 00:43:43 +03:00
|
|
|
getFileTransfer()->enqueueFileTransfer(request,
|
|
|
|
{[callbackPtr, this](std::future<FileTransferResult> result) {
|
2019-07-10 20:46:15 +03:00
|
|
|
try {
|
2022-01-17 23:20:05 +02:00
|
|
|
(*callbackPtr)(std::move(result.get().data));
|
2020-04-07 00:43:43 +03:00
|
|
|
} catch (FileTransferError & e) {
|
|
|
|
if (e.error == FileTransfer::NotFound || e.error == FileTransfer::Forbidden)
|
2022-01-17 23:20:05 +02:00
|
|
|
return (*callbackPtr)({});
|
2019-07-10 20:46:15 +03:00
|
|
|
maybeDisable();
|
2019-09-03 13:51:35 +03:00
|
|
|
callbackPtr->rethrow();
|
2019-07-10 20:46:15 +03:00
|
|
|
} catch (...) {
|
2019-09-03 13:51:35 +03:00
|
|
|
callbackPtr->rethrow();
|
2019-07-10 20:46:15 +03:00
|
|
|
}
|
|
|
|
}});
|
2016-03-03 19:03:34 +02:00
|
|
|
}
|
|
|
|
|
2022-12-26 22:21:08 +02:00
|
|
|
/**
|
|
|
|
* This isn't actually necessary read only. We support "upsert" now, so we
|
|
|
|
* have a notion of authentication via HTTP POST/PUT.
|
|
|
|
*
|
|
|
|
* For now, we conservatively say we don't know.
|
|
|
|
*
|
|
|
|
* \todo try to expose our HTTP authentication status.
|
|
|
|
*/
|
|
|
|
std::optional<TrustedFlag> isTrustedClient() override
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2016-03-03 19:03:34 +02:00
|
|
|
};
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static RegisterStoreImplementation<HttpBinaryCacheStore, HttpBinaryCacheStoreConfig> regHttpBinaryCacheStore;
|
2016-03-03 19:03:34 +02:00
|
|
|
|
|
|
|
}
|