#!/usr/bin/env nu # Garbage-collect golden runner snapshots, retaining the N newest. # Requires HCLOUD_TOKEN in environment. def main [ --keep: int = 4 # How many snapshots to keep (newest first) ] { let result = (do { ^hcloud image list --type snapshot --selector app=buildkit-runner-golden --output json } | complete) if $result.exit_code != 0 { error make { msg: $"hcloud image list failed: ($result.stderr)" } } let snapshots = $result.stdout | from json | sort-by created | reverse let count = $snapshots | length if $count <= $keep { print $"($count) snapshots present, threshold ($keep) — nothing to delete" return } let to_delete = $snapshots | skip $keep print $"Deleting ($to_delete | length) snapshots \(keeping newest ($keep)\)" for snap in $to_delete { print $" delete id=($snap.id) created=($snap.created) description=($snap.description)" let del = (do { ^hcloud image delete ($snap.id | into string) } | complete) if $del.exit_code != 0 { error make { msg: $"Failed to delete snapshot ($snap.id): ($del.stderr)" } } } }