Don't use /cache/token anymore except as a fallback

This commit is contained in:
Eelco Dolstra 2024-01-10 20:48:48 +01:00
parent 9781bb8b6e
commit 0d9e0c088c
2 changed files with 53 additions and 61 deletions

View file

@ -54,6 +54,9 @@ pub enum Error {
#[error("Bad URL")]
BadURL(reqwest::Url),
#[error("FlakeHub did not return any cache for the calling user")]
NoKnownCaches,
}
impl IntoResponse for Error {

View file

@ -80,8 +80,8 @@ pub async fn init_cache(
.await?;
}
// Get the cache we're supposed to use.
let expected_cache_name = {
// Get the cache UUID for this project.
let cache_name = {
let github_repo = env::var("GITHUB_REPOSITORY")
.expect("GITHUB_REPOSITORY environment variable is not set");
@ -108,26 +108,28 @@ pub async fn init_cache(
let project_info = response.json::<ProjectInfo>().await?;
let expected_cache_name = format!(
let cache_name = format!(
"{}:{}",
project_info.organization_uuid_v7, project_info.project_uuid_v7,
);
tracing::info!("Want to use cache {:?}.", expected_cache_name);
tracing::info!("Using cache {:?}.", cache_name);
Some(expected_cache_name)
cache_name
} else {
tracing::error!(
"Failed to get project info from {}: {}",
url,
response.status()
);
None
}
};
// Get a token for creating and pushing to the FlakeHub binary cache.
let (known_caches, token) = {
// As a fallback (if the project doesn't exist yet on
// FlakeHub), get the list of known caches for this user
// and use the oldest one. FIXME: this might not be a good
// idea.
// FIXME: we don't actually need this token. We just need
// to query the caches the user has access to.
let url = flakehub_api_server.join("cache/token").unwrap();
let request = reqwest::Client::new()
@ -168,35 +170,22 @@ pub async fn init_cache(
.as_object()
.ok_or(Error::BadJWT)?;
(known_caches.to_owned(), token)
};
// Use the expected cache if we have access to it, otherwise use
// the oldest cache to which we have access.
let cache_name = {
if expected_cache_name
.as_ref()
.map_or(false, |x| known_caches.get(x).is_some())
{
expected_cache_name.unwrap().to_owned()
} else {
let mut keys: Vec<_> = known_caches.keys().collect();
keys.sort();
keys.first()
.expect("FlakeHub did not return any cache for the calling user.")
.to_string()
let cache_name = keys.first().ok_or(Error::NoKnownCaches)?.to_string();
tracing::info!("Falling back to existing cache {}.", cache_name);
cache_name
}
};
let cache = CacheSliceIdentifier::from_str(&cache_name)?;
tracing::info!("Using cache {}.", cache);
// Create the cache.
let api = ApiClient::from_server_config(ServerConfig {
endpoint: flakehub_cache_server.to_string(),
//token: netrc_entry.password.as_ref().cloned(),
token: Some(token.to_owned()),
token: netrc_entry.password.as_ref().cloned(),
})?;
let request = CreateCacheRequest {