-
Notifications
You must be signed in to change notification settings - Fork 0
/
hup-device.sh
executable file
·194 lines (167 loc) · 4.64 KB
/
hup-device.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
help () {
cat << EOF
Client script to trigger HUP via action server API
hup-device [options]
Options:
-h, --help
Display this help and exit.
-a, --auth API_TOKEN
balena API token for authentication.
-d, --debug
Run HUP in debug mode on device.
-e, --endpoint ACTIONS_ENDPOINT
Actions host endpoint; defaults to "actions.balena-devices.com/v2".
-f, --follow
Follow HUP progress to completion.
--follow-host
API host to follow progress; defaults to "api.balena-cloud.com".
-u, --uuid uuid
UUID for device to HUP; must use long UUID.
-v, --version os_version
Target os_version for HUP.
EOF
}
# Parse arguments
while [ "$#" -gt "0" ]; do
key=$1
case $key in
-h|--help)
help
exit 0
;;
-a|--auth)
API_TOKEN=$2
shift
;;
-d|--debug)
debug=1
;;
-e|--endpoint)
ACTIONS_ENDPOINT=$2
shift
;;
-f|--follow)
follow=1
;;
--follow-host)
follow_host=$2
shift
;;
-v|--version)
os_version=$2
shift
;;
-u|--uuid)
uuid=$2
shift
;;
*)
echo "[WARN] $0 : Argument '$1' unknown. Ignoring."
;;
esac
shift
done
# Fail if missing arguments
if [ -z "${ACTIONS_ENDPOINT}" ]; then
ACTIONS_ENDPOINT="actions.balena-devices.com/v2"
fi
if [ -z "${API_TOKEN}" ]; then
echo "[ERROR] hup-device-v2 : API token not provided"
exit 1
fi
if [ -z "${follow_host}" ]; then
follow_host="api.balena-cloud.com"
fi
if [ -z "${os_version}" ]; then
echo "[ERROR] hup-device-v2 : target OS version not provided"
exit 1
fi
if [ -z "${uuid}" ]; then
echo "[ERROR] hup-device-v2 : UUID not provided"
exit 1
fi
# Execute HUP request
outfile=$(mktemp)
errfile=$(mktemp)
if [ -n "${debug}" ]; then
debug_param=", \"debug\": true"
else
debug_param=""
fi
status_code=$(\
curl -s -X POST "https://${ACTIONS_ENDPOINT}/${uuid}/resinhup" \
--show-error \
-w "%{http_code}" \
-L \
-o "${outfile}" \
--retry 3 \
--header "Authorization: Bearer ${API_TOKEN}" \
--header "Content-Type: application/json" \
--data "{ \"parameters\": { \"target_version\": \"${os_version}\" ${debug_param} } }" \
2> "${errfile}"
)
# Print status code and output text
res=1
if [ -n "${status_code}" ]; then
if [ "${status_code:0:1}" == "2" ]; then
echo "[INFO] code: ${status_code}"
res=0
else
echo "[WARN] code: ${status_code}"
fi
else
echo "[WARN] no code"
fi
if [ -s "${errfile}" ]; then
echo "[WARN] stderr: $(cat "${errfile}")"
fi
rm -f "${errfile}"
if [ -s "${outfile}" ]; then
echo "[INFO] response: $(cat "${outfile}")"
rm -f "${outfile}"
fi
# Only concerned with following HUP progress below.
if [ -z "${follow}" ] || [ "${res}" != 0 ]; then
exit $res
fi
# Print device status, provisioning_progress, and provisioning_state updates as
# they change.
progress=""
last_status=""
last_progress=""
last_prov_state=""
printf "\n%8s %12s %3s %30s\n" " Time " " Status " "Pct" " Detail "
echo "-------- ------------ --- ------------------------------"
# ensure we cleanup
outfile=$(mktemp)
trap 'rm -f ${outfile};exit' ERR INT TERM
while [ 0 ]
do
status_code=$(curl "https://${follow_host}/v6/device(uuid='${uuid}')" \
-w "%{http_code}" \
-o "${outfile}" \
--no-progress-meter \
--header "Authorization: Bearer ${API_TOKEN}" \
--header "Content-Type: application/json")
if [ "${status_code:0:1}" != "2" ]; then
echo "[ERROR] code: ${status_code}"
else
status=$(cat "${outfile}" | jq -r '.d[0] | .status')
progress=$(cat "${outfile}" | jq -r '.d[0] | .provisioning_progress')
prov_state=$(cat "${outfile}" | jq -r '.d[0] | .provisioning_state')
# to keep output clean
if [ "${progress}" = "null" ]; then
progress=""
fi
# Only print if changed
if [ "${status}" != "${last_status}" ] || [ "${progress}" != "${last_progress}" ] || [ "${prov_state}" != "${last_prov_state}" ]; then
tstamp=$(date +"%H:%M:%S")
printf "%8s %-12.12s %3.3s %-30.30s\n" "${tstamp}" "${status}" "${progress}" "${prov_state}"
last_status="${status}"
last_progress="${progress}"
last_prov_state="${prov_state}"
fi
fi
sleep 3
done