forked from perforce/p4prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_for_updates.sh
100 lines (77 loc) · 2.79 KB
/
check_for_updates.sh
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
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# check_for_updates.sh
#
# Checks github repo for script updates, and downloads them if available.
#
# Uses the github API and stores a local file with current status.
#
repo_path="scripts"
github_url="https://api.github.com/repos/perforce/p4prometheus/commits?per_page=1&path=$repo_path"
github_download_url="https://raw.githubusercontent.com/perforce/p4prometheus/master/scripts"
function msg () { echo -e "$*"; }
function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; }
function usage
{
declare style=${1:--h}
declare errorMessage=${2:-Unset}
if [[ "$errorMessage" != Unset ]]; then
echo -e "\\n\\nUsage Error:\\n\\n$errorMessage\\n\\n" >&2
fi
echo "USAGE for check_for_updates.sh:
check_for_updates.sh -c <config_file>
or
check_for_updates.sh -h
Checks github repo for script updates, and downloads them if available.
Uses the github API and stores a local file with current status.
Depends on 'curl' and 'jq' being in the path.
"
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
FILE_LIST="monitor_metrics.sh monitor_metrics.py monitor_wrapper.sh push_metrics.sh report_instance_data.sh check_for_updates.sh create_dashboard.py dashboard.yaml upload_grafana_dashboard.sh"
# Command Line Processing
declare -i shiftArgs=0
ConfigFile=".update_config"
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-h) usage -h && exit 0;;
# (-man) usage -man;;
(-c) ConfigFile=$2; shiftArgs=1;;
(-*) usage -h "Unknown command line option ($1)." && exit 1;;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ "$shiftArgs" -gt 0 ]]; do
[[ $# -eq 0 ]] && usage -h "Incorrect number of arguments."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
cd "$SCRIPT_DIR" || bail "Can't cd to $SCRIPT_DIR"
# Check for dependencies
curl=$(which curl)
[[ $? -eq 0 ]] || bail "Failed to find curl in path"
jq=$(which jq)
[[ $? -eq 0 ]] || bail "Failed to find jq in path"
last_github_sha=""
last_github_date=""
if [[ -e "$ConfigFile" ]]; then
last_github_sha=$(grep last_github_sha "$ConfigFile" | cut -d= -f2)
last_github_date=$(grep last_github_date "$ConfigFile" | cut -d= -f2)
fi
github_sha=$(curl "$github_url" | jq '.[] | .sha')
github_date=$(curl "$github_url" | jq '.[] | .commit.committer.date')
if [[ "$last_github_sha" != "$github_sha" ]]; then
msg "Updating scripts"
for fname in $FILE_LIST; do
[[ -f "$fname" ]] && mv "$fname" "$fname.bak"
msg "downloading $fname"
wget "$github_download_url/$fname"
chmod +x "$fname"
done
echo "last_github_sha=$github_sha" > "$ConfigFile"
echo "last_github_date=$github_date" >> "$ConfigFile"
msg "Scripts updated"
else
msg "Scripts are up-to-date - nothing to do"
fi