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
---*/
2022-09-27 22:11:35 +03:00
import { S3, S3Object } from "s3"
import { crypto, toHashString } from "deno/crypto/mod.ts";
import { readerFromStreamReader } from "deno/streams/reader_from_stream_reader.ts"
import { readAll } from "deno/streams/read_all.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 = toHashString(await crypto.subtle.digest("SHA-256", contents))
2022-09-27 22:11:35 +03:00
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`)
}
}