浏览代码

Added check-sha256 script

George Jones 2 年之前
父节点
当前提交
2d346a8f34
共有 1 个文件被更改,包括 71 次插入0 次删除
  1. 71 0
      bin/check-sha256-and-download.sh

+ 71 - 0
bin/check-sha256-and-download.sh

@@ -0,0 +1,71 @@
+#! /bin/bash
+# Download a file and it's sha256 checksum and validate
+#
+# Usage: check-sha256-and-download.sh FILE_URL CHECKSUM_URL
+#
+# Output:
+#   - The name of the validated file on stdout (to allow use in further scripting)
+#
+# Errors:
+#   - Errors, warnings on stderr
+#   - non-zero exit status on errors.
+
+# This is bash.  Be safe out there.
+set -e; set -u 
+
+# Helper functions
+PROG=`basename "$0" | tr -d '\n'`
+function info()  { echo `date +%c` ${PROG}\: info: "$@" 1>&2; }
+function warn()  { echo `date +%c` ${PROG}\: warning: "$@" 1>&2; }
+function error() { echo `date +%c` ${PROG}\: error: "$@" 1>&2; }
+function debug() { [[ -v DEBUG ]] && echo `date +%c` ${PROG}\: debug: "$@" 1>&2 || true ; }
+function die()   { echo `date +%c` ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
+
+USAGE="$PROG FILE_URL CHECKSUM_URL"
+
+if [[ "$#" -lt 1 ]]; then
+    warn "Missing FILE_URL"
+    die "Uaage: $USAGE"
+else
+    FILE_URL="$1"                   # "https://download.edge.mysocket.io/linux_arm64/mysocketctl"
+    FILE="`basename $1`"
+
+fi
+
+if [[ "$#" -lt 2 ]]; then
+    warn "Missing CHECKSUM_URL"
+    die "Uaage: $USAGE"
+
+else
+    CHECKSUM_URL="$2"               # "https://download.edge.mysocket.io/linux_arm64/sha256-checksum.txt"
+    CHECKSUM_FILE="`basename $2`"
+fi
+
+CURL="curl -s -S"
+SUM="sha256sum --quiet -c"
+
+# Always get latest checksum
+${CURL} "${CHECKSUM_URL}" > "${CHECKSUM_FILE}" \
+     || die "Failure to download mysocket sha checksum"
+
+# Check existing file against checksum or download
+if [ -f "${FILE}" ]; then
+    info "Allready have file."
+    # file already exists, check it, downlaod if failed
+    echo "`cat ${CHECKSUM_FILE}` ${FILE}" | ${SUM} -c \
+        || info "Checksum failed.   Downloading." \
+            && ${CURL} "${FILE_URL}" > "${FILE}" \
+                    || die "Failure to download"
+else
+    info "File not found.  Downloading."
+    ${CURL} "${FILE_URL}" > "${FILE}" \
+                    || die "Failure to download"
+fi
+
+# Final check of checksum
+echo "`cat ${CHECKSUM_FILE}` ${FILE}" | ${SUM} \
+    || die "Checksum failed.   Downloading." \
+
+info "$FILE validated"           
+echo $FILE
+