-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_new_release
executable file
·88 lines (74 loc) · 2.78 KB
/
check_new_release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
# @file check_new_release
# Update tap script if there is a new VKE CLI build
# @author Alister Lewis-Bowen <alister@lewis-bowen.org>
[[ -n $DEBUG ]] && set -x
set -eou pipefail
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
LAST_BUILD_FILE="$SCRIPT_PATH/.last_build_date"
CLI_URL=''
CMD_SHA256=''
VERBOSE='n'
help() {
echo "Update tap script if there is a new VKE CLI build"
echo
echo "Usage: chec_new_release [options]"
echo "Options:"
echo " -h, --help, help .... displays this help information"
echo " -v, --verbose ....... display informational messages"
return 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
-v | --verbose ) VERBOSE='y'; shift;;
help | -h | --help ) help; exit 0;;
* ) shift ;;
esac
done
# Platform dependencies ------------------------------------------------------
case "$OSTYPE" in
darwin*)
CMD_SHA256='shasum -a 256'
CLI_URL="$(curl -s https://api.vke.cloud.vmware.com/v1/cli | jq -r .latest.urls.mac)"
;;
linux*)
CMD_SHA256='sha256sum'
CLI_URL="$(curl -s https://api.vke.cloud.vmware.com/v1/cli | jq -r .latest.urls.linux64)"
;;
*) return 1;;
esac
# Helper functions -----------------------------------------------------------
get_last_modified() {
curl -s -I "$CLI_URL" | grep Last-Modified | sed 's/Last-Modified: //'
}
message() {
[[ "$VERBOSE" == 'y' ]] && echo "$@"
}
# Check if we have anything to compare against -------------------------------
if [[ ! -e "$LAST_BUILD_FILE" ]]; then
get_last_modified > "$LAST_BUILD_FILE"
message "Storing current build date: $(cat "$LAST_BUILD_FILE")"
exit 0
fi
# Check for build update before updating tap ---------------------------------
LAST_BUILD_DATE=$(cat "$LAST_BUILD_FILE")
TAP_SCRIPT="$SCRIPT_PATH/vke-cli.rb"
SHA256=$(grep sha256 "$TAP_SCRIPT" | cut -d'"' -f 2)
VERSION=$(grep version\ "$TAP_SCRIPT" | cut -d'"' -f 2)
if curl -s -I --header "If-Modified-Since: $LAST_BUILD_DATE" "$CLI_URL" | grep -q '304 Not Modified'; then
message "No build since $LAST_BUILD_DATE"
message " SHA256: $SHA256"
message " Version: $VERSION"
else
curl -s "$CLI_URL" -o /tmp/vke && chmod 755 /tmp/vke
SHA256_NEW=$($CMD_SHA256 /tmp/vke | cut -d' ' -f1)
BUILD=$(/tmp/vke --version | cut -d'(' -f2 | cut -d')' -f1 | cut -d':' -f2 | sed 's/ //')
VERSION_NEW="0.9.0.$BUILD"
rm -fR /tmp/vke
sed -i.tmp -e "s/$SHA256/$SHA256_NEW/g" -- "$TAP_SCRIPT" && rm "${TAP_SCRIPT}.tmp"
sed -i.tmp -e "s/$VERSION/$VERSION_NEW/g" -- "$TAP_SCRIPT" && rm "${TAP_SCRIPT}.tmp"
get_last_modified > "$LAST_BUILD_FILE"
message "New build $BUILD created $(cat "$LAST_BUILD_FILE")"
message " SHA256: $SHA256 → $SHA256_NEW"
message " Version: $VERSION → $VERSION_NEW"
fi