2024-02-03 22:31:40 +03:00
|
|
|
#!/bin/sh
|
2023-12-16 08:05:45 +03:00
|
|
|
# SPDX-License-Identifier: MIT
|
2024-02-01 20:54:46 +03:00
|
|
|
set -e
|
2023-12-16 08:05:45 +03:00
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
print() {
|
|
|
|
echo "$@" >&2
|
|
|
|
}
|
2023-12-16 08:05:45 +03:00
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
fail() {
|
|
|
|
print "$@"
|
2023-12-16 08:05:45 +03:00
|
|
|
exit 1
|
2024-02-01 20:54:46 +03:00
|
|
|
}
|
2023-12-16 08:05:45 +03:00
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
requireCommands() {
|
2024-02-18 13:10:49 +03:00
|
|
|
for cmd in "$@"; do
|
|
|
|
if ! command -v "$cmd" > /dev/null 2>&1; then
|
2024-02-01 20:54:46 +03:00
|
|
|
fail "Cannot find required command: $cmd"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-02-18 10:11:25 +03:00
|
|
|
requireCommands uname cut dd chmod rm realpath expr
|
2024-02-01 20:54:46 +03:00
|
|
|
|
2024-02-18 10:11:25 +03:00
|
|
|
# Finding alternative, but supported sha256sums
|
|
|
|
SHA256SUM=""
|
2024-04-12 20:27:47 +03:00
|
|
|
SHASUMFLAGS=""
|
|
|
|
PLATFORM="$(uname)"
|
|
|
|
ARCH="$(uname -m)"
|
2024-02-18 13:10:49 +03:00
|
|
|
if command -v sha256sum > /dev/null 2>&1; then
|
|
|
|
SHA256SUM="sha256sum"
|
2024-04-12 20:27:47 +03:00
|
|
|
SHASUMFLAGS="-c hashes --ignore-missing"
|
2024-02-18 10:11:25 +03:00
|
|
|
else
|
2024-04-12 20:27:47 +03:00
|
|
|
if command -v sha256 > /dev/null 2>&1; then
|
|
|
|
SHA256SUM="sha256"
|
|
|
|
SHASUMFLAGS="-C hashes runner-$PLATFORM-$ARCH"
|
|
|
|
fi
|
2024-02-18 13:10:49 +03:00
|
|
|
if command -v shasum > /dev/null 2>&1; then
|
2024-02-18 10:11:25 +03:00
|
|
|
SHASUMVER=$(shasum -v | cut -c 1)
|
2024-02-18 13:10:49 +03:00
|
|
|
if [ "$SHASUMVER" -ge 6 ]; then
|
|
|
|
SHA256SUM="shasum -a 256"
|
2024-04-12 20:27:47 +03:00
|
|
|
SHASUMFLAGS="-c hashes --ignore-missing"
|
2024-02-18 10:11:25 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-04-12 20:27:47 +03:00
|
|
|
if [ SHA256SUM = "" ]; then
|
|
|
|
fail "Could not find suitable sha256sum executable"
|
|
|
|
fi
|
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
if [ "$(realpath "$SHA256SUM" 2> /dev/null)" = "/bin/busybox" ]; then
|
2024-02-01 20:54:46 +03:00
|
|
|
fail "Busybox sha256sum detected, will not work. Refusing to continue"
|
2023-12-16 08:05:45 +03:00
|
|
|
fi
|
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
# Script could be used for any binary
|
|
|
|
NAME=${BIN:="{{NAME}}"}
|
|
|
|
EXTERNAL_ADDRESS=${EXTERNAL_ADDRESS:="{{EXTERNAL_ADDRESS}}"}
|
2023-12-16 08:05:45 +03:00
|
|
|
DOWNLOAD_COMMAND="curl"
|
|
|
|
OUTPUT_ARG="-o"
|
|
|
|
DIR="/tmp/binhost-$NAME-$(date +%s)"
|
|
|
|
FILE="$DIR/$NAME"
|
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
if ! command -v curl > /dev/null 2>&1; then
|
|
|
|
if ! command -v wget > /dev/null 2>&1; then
|
2024-02-01 20:54:46 +03:00
|
|
|
fail "No curl or wget found, install one and rerun the script"
|
2023-12-16 08:05:45 +03:00
|
|
|
fi
|
2024-02-18 13:10:49 +03:00
|
|
|
DOWNLOAD_COMMAND="wget"
|
|
|
|
OUTPUT_ARG="-O"
|
2023-12-16 08:05:45 +03:00
|
|
|
fi
|
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
PLATFORM_LIST="{{PLATFORM_LIST}}"
|
|
|
|
# Making script truly portable
|
|
|
|
if [ ! "{{NAME}}" = $NAME ]; then
|
|
|
|
print ":: Fetching platforms"
|
2024-02-18 13:10:49 +03:00
|
|
|
PLATFORM_LIST=$($DOWNLOAD_COMMAND $EXTERNAL_ADDRESS/$NAME/platforms $OUTPUT_ARG /dev/stdout)
|
2024-02-01 20:54:46 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
if ! expr "$PLATFORM_LIST" : "\(.*$(uname)-$(uname -m).*\)" > /dev/null; then
|
|
|
|
fail "Platform \"$(uname)-$(uname -m)\" is not supported"
|
|
|
|
fi
|
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
mkdir "$DIR"
|
|
|
|
cd "$DIR"
|
2024-02-01 20:54:46 +03:00
|
|
|
|
|
|
|
print ":: Downloading manifest"
|
|
|
|
$DOWNLOAD_COMMAND $EXTERNAL_ADDRESS/runner/manifest $OUTPUT_ARG manifest
|
2023-12-16 08:05:45 +03:00
|
|
|
|
2024-04-12 20:27:47 +03:00
|
|
|
MANIFEST_HASHSUM=$(cat manifest | $SHA256SUM)
|
2024-02-01 20:54:46 +03:00
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
if [ -n "$KEY" ]; then
|
|
|
|
if [ ! "$KEY" = "$(echo "$MANIFEST_HASHSUM" | cut -c 1-${#KEY})" ]; then
|
2024-02-01 20:54:46 +03:00
|
|
|
fail "Invalid manifest hashsum"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
print "Manifest KEY missing, skipping manifest check"
|
|
|
|
fi
|
2023-12-16 08:05:45 +03:00
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
print ":: Downloading signature"
|
2024-02-18 13:10:49 +03:00
|
|
|
$DOWNLOAD_COMMAND "$EXTERNAL_ADDRESS/bin/$NAME/$PLATFORM/$ARCH/sign" $OUTPUT_ARG signature
|
2024-02-01 20:54:46 +03:00
|
|
|
|
|
|
|
dd if=manifest of=public_key count=32 bs=1 2> /dev/null
|
|
|
|
dd if=manifest of=hashes skip=32 bs=1 2> /dev/null
|
|
|
|
|
|
|
|
print ":: Downloading runner"
|
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
$DOWNLOAD_COMMAND "$EXTERNAL_ADDRESS/runner/runner-$PLATFORM-$ARCH" $OUTPUT_ARG "runner-$PLATFORM-$ARCH"
|
2024-02-01 20:54:46 +03:00
|
|
|
|
2024-04-12 20:27:47 +03:00
|
|
|
if ! $SHA256SUM $SHASUMFLAGS >&2 ; then
|
2024-02-01 20:54:46 +03:00
|
|
|
fail "Incorrect hashsum of runner"
|
|
|
|
fi
|
|
|
|
|
|
|
|
chmod +x "runner-$PLATFORM-$ARCH"
|
|
|
|
|
|
|
|
print ":: Downloading binary"
|
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
$DOWNLOAD_COMMAND "$EXTERNAL_ADDRESS/bin/$NAME/$PLATFORM/$ARCH" $OUTPUT_ARG "$FILE"
|
2024-02-01 20:54:46 +03:00
|
|
|
|
2024-02-18 13:10:49 +03:00
|
|
|
if ! "./runner-$PLATFORM-$ARCH" "$FILE" >&2; then
|
2024-02-01 20:54:46 +03:00
|
|
|
exit 2
|
|
|
|
fi
|
2023-12-16 08:05:45 +03:00
|
|
|
|
|
|
|
chmod +x "$FILE"
|
|
|
|
|
2024-02-03 22:31:40 +03:00
|
|
|
$FILE $ARGS < /dev/tty
|
2023-12-16 08:05:45 +03:00
|
|
|
|
2024-02-01 20:54:46 +03:00
|
|
|
cd
|
|
|
|
|
|
|
|
rm -rf "$DIR"
|