Compare commits
4 commits
9766f7b81b
...
3c5262367e
Author | SHA1 | Date | |
---|---|---|---|
Gabriel Simmer | 3c5262367e | ||
Gabriel Simmer | 03a1617e60 | ||
Gabriel Simmer | d603e52c69 | ||
Gabriel Simmer | 566a4a4a38 |
20
main.py
20
main.py
|
@ -1,20 +0,0 @@
|
||||||
import subprocess
|
|
||||||
import uuid
|
|
||||||
import os
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
URL = 'https://arch.dog'
|
|
||||||
|
|
||||||
def main():
|
|
||||||
filename = os.path.join('/tmp', str(uuid.uuid4()))
|
|
||||||
ARGS = ['-z', '-y2', f'-o{filename}', '--resolution=1920x1080']
|
|
||||||
print(f'rendering {URL} to {filename}')
|
|
||||||
subprocess.run(['/usr/bin/docker', 'run', '--rm', '-v/tmp:/tmp', 'git.gmem.ca/arch/servo:latest'] + ARGS + [URL])
|
|
||||||
with open(filename, 'rb') as file_to_check:
|
|
||||||
data = file_to_check.read()
|
|
||||||
md5_returned = hashlib.md5(data).hexdigest()
|
|
||||||
print(md5_returned)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
91
snapshot.sh
Executable file
91
snapshot.sh
Executable file
|
@ -0,0 +1,91 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
check_dependencies() {
|
||||||
|
local commands=("docker" "sha1sum" "curl" "mktemp" "date" "awk")
|
||||||
|
for cmd in "${commands[@]}"; do
|
||||||
|
if ! command -v "$cmd" &> /dev/null; then
|
||||||
|
echo "Error: $cmd is not available." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot() {
|
||||||
|
docker run --rm -v /tmp:/tmp git.gmem.ca/arch/servo:latest "$1" -z -y2 -o"$2" --resolution=1920x1080
|
||||||
|
}
|
||||||
|
|
||||||
|
hash() {
|
||||||
|
checksum=$(md5sum "$1" | awk '{print $1}')
|
||||||
|
echo "$checksum"
|
||||||
|
}
|
||||||
|
|
||||||
|
update_servo_image() {
|
||||||
|
docker pull git.gmem.ca/arch/servo:latest -q > /dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
post() {
|
||||||
|
curl -X POST -H "Authorization: ${1}" \
|
||||||
|
-F date="$(date -I)" \
|
||||||
|
-F hash="${2}" \
|
||||||
|
-F file="@${3}" \
|
||||||
|
"${4}" -s > /dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
check_dependencies
|
||||||
|
|
||||||
|
# Default value for dry-run flag
|
||||||
|
DRYRUN='false'
|
||||||
|
|
||||||
|
# Parse command-line options
|
||||||
|
while getopts "d-:" opt; do
|
||||||
|
case $opt in
|
||||||
|
d)
|
||||||
|
DRYRUN='true'
|
||||||
|
;;
|
||||||
|
-)
|
||||||
|
case "${OPTARG}" in
|
||||||
|
dry-run)
|
||||||
|
DRYRUN='true'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Invalid option --${OPTARG}"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
;;
|
||||||
|
\?)
|
||||||
|
echo "Invalid option: -$OPTARG" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Shift the parsed options
|
||||||
|
shift $((OPTIND-1))
|
||||||
|
|
||||||
|
# Ensure positional parameters are provided
|
||||||
|
if [ $# -lt 2 ]; then
|
||||||
|
echo "Usage: $0 [--dry-run] <url> <api endpoint>"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
url=$1
|
||||||
|
api=$2
|
||||||
|
|
||||||
|
tmpfile="$(mktemp -u)"
|
||||||
|
echo "Updating local git.gmem.ca/arch/servo:latest"
|
||||||
|
update_servo_image
|
||||||
|
echo "Snapshotting $1"
|
||||||
|
snapshot "$url" "$tmpfile"
|
||||||
|
checksum=$(hash "$tmpfile")
|
||||||
|
|
||||||
|
if [ $DRYRUN = 'false' ]; then
|
||||||
|
echo "Sending ${tmpfile} to API"
|
||||||
|
post "$SERVO_API_TOKEN" "$checksum" "$tmpfile" "$api"
|
||||||
|
else
|
||||||
|
echo "Dry run specified, not sending $tmpfile to API"
|
||||||
|
echo "File hash: $checksum"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Done!"
|
|
@ -107,7 +107,13 @@ export default {
|
||||||
hash,
|
hash,
|
||||||
file
|
file
|
||||||
} = Object.fromEntries(body)
|
} = Object.fromEntries(body)
|
||||||
|
|
||||||
|
// Don't bother uploading to R2 if the hashes match.
|
||||||
|
let latest = kv.get("LATEST");
|
||||||
|
if (latest != hash) {
|
||||||
await bucket.put(`${hash}.png`, file);
|
await bucket.put(`${hash}.png`, file);
|
||||||
|
}
|
||||||
|
|
||||||
await kv.put("LATEST", hash, {
|
await kv.put("LATEST", hash, {
|
||||||
metadata: { date: date }
|
metadata: { date: date }
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue