Yelp/dumb-init

Shell script to download the latest dumb-init

sgleske-ias opened this issue · 0 comments

I was bored and wrote a cross platform bash script which can download the latest dumb-init. Just putting it here in case anybody finds it useful or wants to use it.

#!/bin/bash
# Created by Sam Gleske (GitHub @samrocketman)
# Sun Sep  9 00:44:23 PDT 2018
# Downloads the latest dumb-init binary and verifies the checksum.

DESTINATION="${DESTINATION:-dumb-init}"

function latest_tag() {
    curl -sfI https://github.com/Yelp/dumb-init/releases/latest |
    tr '\r' '\n' |
    awk '$1 == "Location:" { gsub(/^.*\//, "", $2);print $2 }'
}; declare -rf latest_tag

# get the checksum for the downloaded binary
function checksum() {
    curl -sL "https://github.com/Yelp/dumb-init/releases/download/${TAG}/sha256sums" |
    awk "\$0 ~ /${1}\$/ { print \$1 }"
}; declare -rf checksum

function sha256sum() {
    if type -P sha256sum > /dev/null; then
        command sha256sum "${@}"
    elif type -P shasum > /dev/null; then
        command shasum -a 256 "${@}"
    else
        echo "ERROR: could not find a sha256sum program."
        exit 1
    fi
}; declare -rf sha256sum

set -auxeEo pipefail

if [ ! -x "${DESTINATION}" ]; then
    TAG="$( latest_tag )"
    VERSION="$( grep -o '[.0-9]\+' <<< "${TAG}" )"
    BINARY="dumb-init_${VERSION}_amd64"
    curl -fLo "${DESTINATION}" "https://github.com/Yelp/dumb-init/releases/download/${TAG}/${BINARY}"
    sha256sum -c - <<< "$(checksum "${BINARY}")  ${DESTINATION}"
    chmod 755 "${DESTINATION}"
fi