pantry/scripts/fixup-checksums.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-09-08 23:40:35 +03:00
#!/usr/bin/env -S tea -E
/*---
args:
- deno
- run
- --allow-net
2022-09-13 19:46:10 +03:00
- --allow-env=AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_S3_BUCKET
2022-09-08 23:40:35 +03:00
- --import-map={{ srcroot }}/import-map.json
---*/
2022-09-27 22:11:35 +03:00
import { S3, S3Object } from "s3"
2022-09-20 14:53:40 +03:00
import { Sha256 } from "deno/hash/sha256.ts"
import { readerFromStreamReader, readAll } from "deno/streams/conversion.ts"
import Path from "path"
2022-09-08 23:40:35 +03:00
const s3 = new S3({
accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID")!,
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
region: "us-east-1",
});
2022-09-13 16:18:47 +03:00
const bucket = s3.getBucket(Deno.env.get("AWS_S3_BUCKET")!);
2022-09-08 23:40:35 +03:00
for await (const pkg of bucket.listAllObjects({ batchSize: 200 })) {
2022-09-27 22:11:35 +03:00
const keys = get_keys(pkg)
if (!keys) continue
2022-09-08 23:40:35 +03:00
2022-09-27 22:11:35 +03:00
console.log({ checking: keys.checksum });
2022-09-08 23:40:35 +03:00
2022-09-27 22:11:35 +03:00
if (!await bucket.headObject(keys.checksum.string)) {
console.log({ missing: keys.checksum })
2022-09-08 23:40:35 +03:00
2022-09-27 22:11:35 +03:00
const reader = (await bucket.getObject(keys.bottle.string))!.body.getReader()
const contents = await readAll(readerFromStreamReader(reader))
const sha256sum = new Sha256().update(contents).toString()
const body = new TextEncoder().encode(`${sha256sum} ${keys.bottle.basename()}`)
await bucket.putObject(keys.checksum.string, body)
2022-09-08 23:40:35 +03:00
2022-09-27 22:11:35 +03:00
console.log({ uploaded: keys.checksum })
2022-09-08 23:40:35 +03:00
}
}
2022-09-27 22:11:35 +03:00
function get_keys(pkg: S3Object): { bottle: Path, checksum: Path } | undefined {
if (!pkg.key) return
if (!/\.tar\.[gx]z$/.test(pkg.key)) return
return {
bottle: new Path(pkg.key),
checksum: new Path(`${pkg.key}.sha256sum`)
}
}