Created
August 18, 2019 06:54
-
-
Save justaugustus/74bf0480a1686fb4949d2445a7fe11ce to your computer and use it in GitHub Desktop.
Testing SHAs for Kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
GITHUB_TOKEN="$( echo "$GITHUB_TOKEN" | tr -d '[:space:]' )" | |
[[ -n "$GITHUB_TOKEN" ]] && GITHUB_TOKEN_FLAG=("-u" "${GITHUB_TOKEN}:x-oauth-basic") | |
GHCURL="curl -s --fail --retry 10 ${GITHUB_TOKEN_FLAG[*]}" | |
GITHUB_API='https://api.github.com' | |
K8S_GITHUB_API_ROOT="${GITHUB_API}/repos" | |
K8S_GITHUB_API="$K8S_GITHUB_API_ROOT/justaugustus/kubernetes" | |
K8S_GITHUB_URL='https://github.com/justaugustus/kubernetes' | |
RELEASE_BRANCH="master" | |
RELEASE_VERSION_PRIME="v1.17.0-alpha.0" | |
CHANGELOG_FILE="changelog.md" | |
changelog_url="https://changelog" | |
# Pretty curses stuff for terminals | |
if [[ -t 1 ]]; then | |
# Set some video text attributes for use in error/warning msgs. | |
declare -A TPUT=([BOLD]=$(tput bold 2>/dev/null)) | |
TPUT+=( | |
[REVERSE]=$(tput rev 2>/dev/null) | |
[UNDERLINE]=$(tput smul 2>/dev/null) | |
[BLINK]=$(tput blink 2>/dev/null) | |
[GREEN]=${TPUT[BOLD]}$(tput setaf 2 2>/dev/null) | |
[RED]=${TPUT[BOLD]}$(tput setaf 1 2>/dev/null) | |
[YELLOW]=${TPUT[BOLD]}$(tput setaf 3 2>/dev/null) | |
[OFF]=$(tput sgr0 2>/dev/null) | |
[COLS]=$(tput cols 2>/dev/null) | |
) | |
# HR | |
HR="$(for ((i=1;i<=${TPUT[COLS]};i++)); do echo -en '\u2500'; done)" | |
# Save original TTY State | |
TTY_SAVED_STATE="$(stty -g)" | |
else | |
HR="$(for ((i=1;i<=80;i++)); do echo -en '='; done)" | |
fi | |
# Set some usable highlighted keywords for functions like logrun -s | |
YES="${TPUT[GREEN]}YES${TPUT[OFF]}" | |
OK="${TPUT[GREEN]}OK${TPUT[OFF]}" | |
DONE="${TPUT[GREEN]}DONE${TPUT[OFF]}" | |
PASSED="${TPUT[GREEN]}PASSED${TPUT[OFF]}" | |
FAILED="${TPUT[RED]}FAILED${TPUT[OFF]}" | |
FATAL="${TPUT[RED]}FATAL${TPUT[OFF]}" | |
NO="${TPUT[RED]}NO${TPUT[OFF]}" | |
WARNING="${TPUT[YELLOW]}WARNING${TPUT[OFF]}" | |
ATTENTION="${TPUT[YELLOW]}ATTENTION${TPUT[OFF]}" | |
MOCK="${TPUT[YELLOW]}MOCK${TPUT[OFF]}" | |
FOUND="${TPUT[GREEN]}FOUND${TPUT[OFF]}" | |
NOTFOUND="${TPUT[YELLOW]}NOT FOUND${TPUT[OFF]}" | |
shadir="fake/shas" | |
mkdir -p $shadir | |
rm -rf "${shadir:?}"/{*.{sha256,sha512,txt},SHA256SUMS,SHA512SUMS} | |
for num in {0..9}; do | |
newfile="file$num.txt" | |
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 > $shadir/$newfile | |
done | |
# Simple yes/no prompt | |
# | |
# @optparam default -n(default)/-y/-e (default to n, y or make (e)xplicit) | |
# @param message | |
common::askyorn () { | |
local yorn | |
local def=n | |
local msg="y/N" | |
case $1 in | |
-y) # yes default | |
def="y" msg="Y/n" | |
shift | |
;; | |
-e) # Explicit | |
def="" msg="y/n" | |
shift | |
;; | |
-n) shift | |
;; | |
esac | |
while [[ $yorn != [yYnN] ]]; do | |
logecho -n "$*? ($msg): " | |
read yorn | |
: ${yorn:=$def} | |
done | |
# Final test to set return code | |
[[ $yorn == [yY] ]] | |
} | |
common::sha () { | |
local file=$1 | |
local algo=${2:-256} | |
local output_type=${3:-hash} | |
local shasum_cmd=$(which shasum >/dev/null 2>&1 && LANG=C shasum -a$algo $file) | |
if [[ "$output_type" != "full" ]]; then | |
echo "$shasum_cmd" | awk '{print $1}' | |
else | |
echo "$shasum_cmd" | sed 's/ .*\// /' | |
fi | |
} | |
find $shadir -type f | while read path; do | |
common::sha $path 256 "full" > "$path.sha256" || return 1 | |
common::sha $path 512 "full" > "$path.sha512" || return 1 | |
done | |
for f in $shadir/*.sha256; do | |
cat "$f" >> "$shadir/SHA256SUMS" | |
done | |
for f in $shadir/*.sha512; do | |
cat "$f" >> "$shadir/SHA512SUMS" | |
done | |
update_github_release () { | |
local release_id | |
local id_suffix | |
local release_verb="Posting" | |
local prerelease="true" | |
local draft="true" | |
local staging_dir="$shadir" | |
local tarball="$staging_dir/kubernetes.tar.gz" | |
local sha256_hash=$(common::sha $tarball 256) | |
local sha512_hash=$(common::sha $tarball 512) | |
local sha256sums_file="$staging_dir/SHA256SUMS" | |
local sha512sums_file="$staging_dir/SHA512SUMS" | |
# Does the release exist yet? | |
release_id=$($GHCURL $K8S_GITHUB_API/releases/tags/$RELEASE_VERSION_PRIME |\ | |
jq -r '.id') | |
if [[ -n "$release_id" ]]; then | |
logecho "The $RELEASE_VERSION_PRIME is already published on github." | |
if ((FLAGS_yes)) || common::askyorn -e "Would you like to update it"; then | |
logecho "Setting post data id to $release_id to update existing release." | |
id_suffix="/$release_id" | |
release_verb="Updating" | |
else | |
logecho "Existing release (id #$release_id) left intact." | |
return 1 | |
fi | |
fi | |
# post release data | |
logecho "$release_verb the $RELEASE_VERSION_PRIME release on github..." | |
local changelog_url="$K8S_GITHUB_URL/blob/master/$CHANGELOG_FILE" | |
release_id=$($GHCURL $K8S_GITHUB_API/releases$id_suffix --data \ | |
'{ | |
"tag_name": "'$RELEASE_VERSION_PRIME'", | |
"target_commitish": "'$RELEASE_BRANCH'", | |
"name": "'$RELEASE_VERSION_PRIME'", | |
"body": "See [kubernetes-announce@](https://groups.google.com/forum/#!forum/kubernetes-announce) and ['$CHANGELOG_FILE']('$changelog_url'#'${RELEASE_VERSION_PRIME//\./}') for details.\n\nSHA256 for `kubernetes.tar.gz`: `'$sha256_hash'`\n\nSHA512 for `kubernetes.tar.gz`: `'$sha512_hash'`\n\nAdditional binary downloads are linked in the ['$CHANGELOG_FILE']('$changelog_url'#downloads-for-'${RELEASE_VERSION_PRIME//\./}').", | |
"draft": '$draft', | |
"prerelease": '$prerelease' | |
}' |jq -r '.id') | |
# verify it was created | |
if [[ -z "$release_id" ]]; then | |
logecho | |
logecho -r "$FAILED to create the $RELEASE_VERSION_PRIME release on github!" | |
return 1 | |
fi | |
# publish binary | |
logecho -n "Uploading binary to github: " | |
if $GHCURL -H "Content-Type:application/x-compressed" \ | |
--data-binary @$tarball \ | |
"${K8S_GITHUB_API/api\./uploads\.}/releases/$release_id/assets?name=${tarball##*/}"; then | |
logecho $OK | |
else | |
logecho $FAILED | |
fi | |
# Upload SHA sums to GitHub | |
logecho -n "Uploading SHA sums to github: " | |
for file in "$sha256sums_file" "$sha512sums_file"; do | |
if [[ ! -f $file ]]; then | |
logecho -n "Unable to find $file. Skipping..." | |
else | |
if $GHCURL -H "Content-Type:application/octet-stream" \ | |
--data-binary @$file \ | |
"${K8S_GITHUB_API/api\./uploads\.}/releases/$release_id/assets?name=${file##*/}"; then | |
logecho $OK | |
else | |
logecho $FAILED | |
fi | |
fi | |
done | |
} | |
############################################################################### | |
# Define logecho() function to display to both log and stdout. | |
# As this is widely used and to reduce clutter, we forgo the common:: prefix | |
# Options can be -n or -p or -np/-pn. | |
# @optparam -p Add $PROG: prefix to stdout | |
# @optparam -r Exclude log prefix (used to output status' like $OK $FAILED) | |
# @optparam -n no newline (just like echo -n) | |
# @param a string to echo to stdout | |
logecho () { | |
local log_prefix="$PROG::${FUNCNAME[1]:-"main"}(): " | |
local prefix | |
# Dynamically set fmtlen | |
local fmtlen=$((${TPUT[COLS]:-"90"})) | |
local n | |
local raw=0 | |
#local -a sed_pat=() | |
while [[ "$#" -gt 0 ]]; do | |
case "$1" in | |
-r) raw=1; shift ;; | |
-n) n="-n"; shift ;; | |
-p) prefix="$PROG: "; ((fmtlen+=${#prefix})); shift ;; | |
*) break ;; | |
esac | |
done | |
if ((raw)) || [[ -z "$*" ]]; then | |
# Clean log_prefix for blank lines | |
log_prefix="" | |
#else | |
# Increase fmtlen to account for control characters | |
#((fmtlen+=$(echo "$*" grep -o '[[:cntrl:]]' |wc -l))) | |
#sed_pat=(-e '2,${s}/^/ ... /g') | |
fi | |
# Allow widespread use of logecho without having to | |
# determine if $LOGFILE exists first. | |
[[ -f $LOGFILE ]] || LOGFILE="/dev/null" | |
( | |
# If -n is set, do not provide autoformatting or you lose the -n effect | |
# Use of -n should only be used on short status lines anyway. | |
if ((raw)) || [[ $n == "-n" ]]; then | |
echo -e $n "$log_prefix$*" | |
else | |
# Add FUNCNAME to line prefix, but strip it from visible output | |
# Useful for viewing log detail | |
echo -e "$*" | fmt -$fmtlen | sed -e "1s,^,$log_prefix,g" "${sed_pat[@]}" | |
fi | |
) | tee -a "$LOGFILE" |sed "s,^$log_prefix,$prefix,g" | |
} | |
update_github_release |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment