-
Notifications
You must be signed in to change notification settings - Fork 9
/
check-updates.sh
executable file
·85 lines (77 loc) · 2.63 KB
/
check-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
#!/usr/bin/env bash
set -eo pipefail
hash jq 2>/dev/null || {
echo >&2 "This script uses jq. Download at https://stedolan.github.io/jq/"
exit 1
}
choose_device() {
echo "Getting devices..."
DEVICES=$(aws panorama list-devices)
DEVICE_NAMES=($((echo ${DEVICES} | jq -r '.Devices |=sort_by(.LastUpdatedTime) | [.Devices[].Name] | @sh') | tr -d \'\"))
DEVICE_IDS=($((echo ${DEVICES} | jq -r '.Devices |=sort_by(.LastUpdatedTime) | [.Devices[].DeviceId] | @sh') | tr -d \'\"))
if [ -x ${DEVICE_IDS} ]; then
echo "No devices found. Provision a device with the management console or"
echo "use the provision-device.sh script under util-scripts/ in this repository."
exit
fi
for (( c=0; c<${#DEVICE_NAMES[@]}; c++ ))
do
echo "${c}: ${DEVICE_IDS[${c}]} ${DEVICE_NAMES[${c}]}"
done
echo "Choose a device"
read D_INDEX
DEVICE_ID=${DEVICE_IDS[${D_INDEX}]}
}
version_is_newer() {
NEW_VERSION=$1
NEW_MAJ=$(echo $NEW_VERSION | cut -d "." -f 1)
NEW_MIN=$(echo $NEW_VERSION | cut -d "." -f 2)
NEW_PAT=$(echo $NEW_VERSION | cut -d "." -f 3)
OLD_VERSION=$2
if [ ${OLD_VERSION} = "NOT_AVAILABLE" ]; then
echo "TRUE"
exit
fi
OLD_MAJ=$(echo $OLD_VERSION | cut -d "." -f 1)
OLD_MIN=$(echo $OLD_VERSION | cut -d "." -f 2)
OLD_PAT=$(echo $OLD_VERSION | cut -d "." -f 3)
if [ ${NEW_MAJ} -gt ${OLD_MAJ} ]; then
echo "TRUE"
elif [ ${NEW_MIN} -gt ${OLD_MIN} ]; then
echo "TRUE"
elif [ ${NEW_PAT} -gt ${OLD_PAT} ]; then
echo "TRUE"
else
echo "FALSE"
fi
}
apply_update() {
DEVICE_ID=$1
NEW_VERSION=$2
CONFIG='{"OTAJobConfig": {"ImageVersion": "NEW_VERSION"}}'
CONFIG=${CONFIG/NEW_VERSION/$NEW_VERSION}
aws panorama create-job-for-devices --device-ids ${DEVICE_ID} --device-job-config "${CONFIG}" --job-type OTA
}
if [[ $# -eq 1 ]] ; then
DEVICE_ID=${1}
else
choose_device
fi
echo "Checking ${DEVICE_ID} for updates."
DEVICE=$(aws panorama describe-device --device-id ${DEVICE_ID})
OLD_VERSION=$(echo $DEVICE | jq -r .CurrentSoftware)
NEW_VERSION=$(echo $DEVICE | jq -r .LatestSoftware)
IS_NEWER=$(version_is_newer ${NEW_VERSION} ${OLD_VERSION})
if [ ${IS_NEWER} = "TRUE" ]; then
echo "A new version is available for device ${DEVICE_ID}: ${OLD_VERSION} -> ${NEW_VERSION}"
while true; do
read -p "Apply update? (y/n)" response
case $response in
[Yy]* ) apply_update ${DEVICE_ID} ${NEW_VERSION}; break;;
[Nn]* ) exit;;
* ) echo "Response must start with y or n.";;
esac
done
else
echo "Device is up-to-date."
fi