remove s3 cache items that are already on cache.nixos.org

This commit is contained in:
Charlotte 🦝 Delenk 2022-11-18 15:21:01 +01:00
parent 40136d283f
commit 1f8558b9c0
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122
4 changed files with 41 additions and 29 deletions

View file

@ -65,11 +65,10 @@ jobs:
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nixcache:8KKuGz95Pk4UJ5W/Ni+pN+v+LDTkMMFV4yrGmAYgkDg= hydra.nixos.org-1:CNHJZBh9K4tP3EKF6FkkgeVYsS3ohTl+oS0Qa8bezVs= trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nixcache:8KKuGz95Pk4UJ5W/Ni+pN+v+LDTkMMFV4yrGmAYgkDg= hydra.nixos.org-1:CNHJZBh9K4tP3EKF6FkkgeVYsS3ohTl+oS0Qa8bezVs=
experimental-features = nix-command flakes ca-derivations experimental-features = nix-command flakes ca-derivations
post-build-hook = ${{ github.workspace }}/scripts/post-build-hook
substituters = https://cache.chir.rs/ substituters = https://cache.chir.rs/
- name: Download patched nix - name: Download patched nix
run: nix build github:DarkKirb/nix-packages#nix-s3-dedup run: nix build github:DarkKirb/nix-packages#nix-s3-dedup
- name: Setup post-build-hook
run: echo "post-build-hook = ${{ github.workspace }}/scripts/post-build-hook" | sudo tee -a /etc/nix/nix.conf
- name: Set up secrets - name: Set up secrets
run: | run: |
echo "$NIX_CACHE_KEY" > ~/cache.key echo "$NIX_CACHE_KEY" > ~/cache.key

View file

@ -17,11 +17,10 @@ jobs:
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nixcache:8KKuGz95Pk4UJ5W/Ni+pN+v+LDTkMMFV4yrGmAYgkDg= hydra.nixos.org-1:CNHJZBh9K4tP3EKF6FkkgeVYsS3ohTl+oS0Qa8bezVs= trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= nixcache:8KKuGz95Pk4UJ5W/Ni+pN+v+LDTkMMFV4yrGmAYgkDg= hydra.nixos.org-1:CNHJZBh9K4tP3EKF6FkkgeVYsS3ohTl+oS0Qa8bezVs=
experimental-features = nix-command flakes ca-derivations experimental-features = nix-command flakes ca-derivations
post-build-hook = ${{ github.workspace }}/scripts/post-build-hook
substituters = https://cache.chir.rs/ substituters = https://cache.chir.rs/
- name: Download patched nix - name: Download patched nix
run: nix build github:DarkKirb/nix-packages#nix-s3-dedup run: nix build github:DarkKirb/nix-packages#nix-s3-dedup
- name: Setup post-build-hook
run: echo "post-build-hook = ${{ github.workspace }}/scripts/post-build-hook" | sudo tee -a /etc/nix/nix.conf
- name: Set up secrets - name: Set up secrets
run: | run: |
echo "$NIX_CACHE_KEY" > ~/cache.key echo "$NIX_CACHE_KEY" > ~/cache.key

View file

@ -1,11 +1,12 @@
{ {
python3, python3,
boto3, boto3,
aiohttp,
stdenvNoCC, stdenvNoCC,
lib, lib,
}: let }: let
clean-s3-cache-env = python3.buildEnv.override { clean-s3-cache-env = python3.buildEnv.override {
extraLibs = [boto3]; extraLibs = [boto3 aiohttp];
}; };
in in
stdenvNoCC.mkDerivation { stdenvNoCC.mkDerivation {

View file

@ -10,6 +10,7 @@ import json
import boto3 import boto3
from botocore.response import StreamingBody from botocore.response import StreamingBody
import aiohttp
ENDPOINT_URL: str = "https://s3.us-west-000.backblazeb2.com" ENDPOINT_URL: str = "https://s3.us-west-000.backblazeb2.com"
BUCKET_NAME: str = "cache-chir-rs" BUCKET_NAME: str = "cache-chir-rs"
@ -143,33 +144,45 @@ def get_store_hashes() -> set[str]:
return hashes return hashes
async def is_in_nixos_cache(client: aiohttp.ClientSession, narinfo: str) -> bool:
async with client.get(f"https://cache.nixos.org/{narinfo}"):
if response.status == 200:
return True
return False
async def main() -> None: async def main() -> None:
nars_to_delete = set() nars_to_delete = set()
nars_to_keep = set() nars_to_keep = set()
async for obj_key in list_cache_objects(): async with aiohttp.ClientSession() as client:
if obj_key.endswith(".narinfo"): async for obj_key in list_cache_objects():
# check if we have the hash locally if obj_key.endswith(".narinfo"):
narinfo = await get_object(obj_key) # check if we have the hash locally
narinfo = NarInfo(narinfo) narinfo = await get_object(obj_key)
if not await narinfo.exists_locally(): narinfo = NarInfo(narinfo)
print(f"Found unused NAR for {narinfo.store_path}") # check if cache.nixos.org has the narinfo
await delete_object(obj_key) if await is_in_nixos_cache(client, obj_key):
nars_to_delete.add(narinfo.url) print(f"Found duplicated NAR for {narinfo.store_path}")
else: await delete_object(obj_key)
nars_to_keep.add(narinfo.url) nars_to_delete.add(narinfo.url)
if obj_key.startswith("realisations/"): elif not await narinfo.exists_locally():
realisation = await get_object(obj_key) print(f"Found unused NAR for {narinfo.store_path}")
realisation = json.loads(realisation) await delete_object(obj_key)
if not isinstance(realisation, dict): nars_to_delete.add(narinfo.url)
continue else:
if "outPath" not in realisation: nars_to_keep.add(narinfo.url)
continue if obj_key.startswith("realisations/"):
if not await exists_locally("/nix/store/" + realisation = await get_object(obj_key)
realisation["outPath"]): realisation = json.loads(realisation)
print(f"Found unused realisation for {realisation['outPath']}") if not isinstance(realisation, dict):
await delete_object(obj_key) continue
if obj_key.startswith("nar/"): if "outPath" not in realisation:
nars_to_delete.add(obj_key) continue
if not await exists_locally("/nix/store/" +
realisation["outPath"]):
print(f"Found unused realisation for {realisation['outPath']}")
await delete_object(obj_key)
if obj_key.startswith("nar/"):
nars_to_delete.add(obj_key)
for nar in nars_to_delete: for nar in nars_to_delete:
if nar in nars_to_keep: if nar in nars_to_keep:
continue continue