Skip to content

Commit b03d69c

Browse files
Merge pull request #414 from balena-os/run-from-app-uuid-and-release-commit
Add option to specify target OS by App UUID and release commit
2 parents d626e15 + 7564334 commit b03d69c

File tree

1 file changed

+90
-6
lines changed

1 file changed

+90
-6
lines changed

upgrade-2.x.sh

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,17 @@ function post_update_fixes() {
10911091

10921092
###
10931093
# Script start
1094+
#
1095+
# There are two patterns of required input arguments to invoke this script:
1096+
# --hostos-version, --balenaos-registry
1097+
# Used by balenaProxy push to device. Queries for target image URI.
1098+
#
1099+
# --app-uuid, --release-commit, --target-image-uri (optional)
1100+
# Used by Supervisor pull from balenaCloud. Queries for target version, and
1101+
# target-image-uri as needed, and parses registry from the target URI.
1102+
#
1103+
# Note: --target-image-uri value may not be stable long-term, so we cannot use
1104+
# it alone as the source of target version.
10941105
###
10951106

10961107
# If no arguments passed, just display the help
@@ -1164,6 +1175,13 @@ while [[ $# -gt 0 ]]; do
11641175
help
11651176
exit 0
11661177
;;
1178+
--app-uuid)
1179+
if [ -z "$2" ]; then
1180+
log ERROR "\"$1\" argument needs a value."
1181+
fi
1182+
app_uuid=$2
1183+
shift
1184+
;;
11671185
--force-slug)
11681186
if [ -z "$2" ]; then
11691187
log ERROR "\"$1\" argument needs a value."
@@ -1185,6 +1203,13 @@ while [[ $# -gt 0 ]]; do
11851203
esac
11861204
shift
11871205
;;
1206+
--release-commit)
1207+
if [ -z "$2" ]; then
1208+
log ERROR "\"$1\" argument needs a value."
1209+
fi
1210+
release_commit=$2
1211+
shift
1212+
;;
11881213
--resinos-registry | --balenaos-registry)
11891214
if [ -z "$2" ]; then
11901215
log ERROR "\"$1\" argument needs a value."
@@ -1216,6 +1241,13 @@ while [[ $# -gt 0 ]]; do
12161241
--stop-all)
12171242
STOP_ALL="yes"
12181243
;;
1244+
--target-image-uri)
1245+
if [ -z "$2" ]; then
1246+
log ERROR "\"$1\" argument needs a value."
1247+
fi
1248+
target_image=$2
1249+
shift
1250+
;;
12191251
--assume-supported)
12201252
log WARN "The --assume-supported flag is deprecated, and has no effect."
12211253
;;
@@ -1231,13 +1263,22 @@ _prepare_locking
12311263
# Try to get lock, and exit if cannot, meaning another instance is running already
12321264
exlock_now || exit 9
12331265

1234-
if [ -z "$target_version" ]; then
1235-
log ERROR "--hostos-version is required."
1266+
# Enforce required arguments. See comment above at 'Script start'.
1267+
if [ -z "$target_version" ] && [ -z "$app_uuid" ]; then
1268+
log ERROR "Either --hostos-version or --app-uuid is required."
12361269
fi
1237-
1238-
if [ -z "${REGISTRY_ENDPOINT}" ]; then
1270+
if [ -n "$target_version" ] && [ -n "$app_uuid" ]; then
1271+
log ERROR "Only one of --hostos-version and --app-uuid may be specified."
1272+
fi
1273+
if [ -n "$target_version" ] && [ -z "${REGISTRY_ENDPOINT}" ]; then
12391274
log ERROR "--balenaos-registry is required."
12401275
fi
1276+
if [ -n "$app_uuid" ] && [ -z "$release_commit" ]; then
1277+
log ERROR "--release-commit is required."
1278+
fi
1279+
if [ -z "$app_uuid" ] && [ -n "$target_image" ]; then
1280+
log ERROR "--target-image-uri is useful only with --app-uuid."
1281+
fi
12411282

12421283
progress 25 "Preparing OS update"
12431284

@@ -1250,6 +1291,46 @@ FETCHED_SLUG=$(CURL_CA_BUNDLE="${TMPCRT}" ${CURL} -H "Authorization: Bearer ${AP
12501291
SLUG=${FORCED_SLUG:-$FETCHED_SLUG}
12511292
HOST_OS_VERSION=${META_BALENA_VERSION:-${VERSION_ID}}
12521293

1294+
# Must query for target version and perhaps for target image, which includes registry
1295+
# endpoint, if started from app UUID.
1296+
if [ -n "$app_uuid" ]; then
1297+
# Retrieve version for the provided app UUID and release commit. Verify that
1298+
# the release succeeded and has not been invalidated. We expect at most a
1299+
# single release. Catch command failure for handling below.
1300+
_query_res=$(CURL_CA_BUNDLE="${TMPCRT}" ${CURL} \
1301+
-H "Content-Type: application/json" -H "Authorization: Bearer ${APIKEY}" \
1302+
"${API_ENDPOINT}/v7/release?\$select=raw_version&\$filter=commit%20eq%20%27${release_commit}%27%20and%20(belongs_to__application/any(bta:bta/is_host%20and%20bta/uuid%20eq%20%27${app_uuid}%27))%20and%20status%20eq%20'success'%20and%20is_invalidated%20eq%20false" || echo "fail")
1303+
1304+
# Verify the result includes a json row.
1305+
_has_row=$(echo "${_query_res}" | jq -r ".d[]" || echo "")
1306+
if [ -z "$_has_row" ]; then
1307+
if [ "${_query_res}" = "fail" ]; then
1308+
log ERROR "Target release query from app UUID failed"
1309+
else
1310+
log ERROR "Target release query from app UUID not found or not valid"
1311+
fi
1312+
fi
1313+
target_version=$(echo "${_query_res}" | jq -r ".d[] | .raw_version")
1314+
1315+
# We do not expect registry endpoint is defined separately, so must query
1316+
# for optional target image here if not defined already -- so we can parse
1317+
# for the endpoint below.
1318+
if [ -z "${target_image}" ]; then
1319+
target_image=$(get_image_location "${target_version}")
1320+
if [ -z "${target_image}" ]; then
1321+
log ERROR "Zero or multiple matching target hostapp releases found, update attempt has failed..."
1322+
fi
1323+
fi
1324+
1325+
# Not expecting a backslash in domain name, so:
1326+
# shellcheck disable=SC2162
1327+
read -d "/" REGISTRY_ENDPOINT <<<"$target_image"
1328+
if [ -z "${REGISTRY_ENDPOINT}" ] || [ "${REGISTRY_ENDPOINT}" = "${target_image}" ]; then
1329+
log ERROR "Target image URI expected '/': ${target_image}"
1330+
fi
1331+
log "Registry endpoint from target release: ${REGISTRY_ENDPOINT}"
1332+
fi
1333+
12531334
if version_gt "${target_version}" "${minimum_hostapp_target_version}" || [ "${target_version}" == "${minimum_hostapp_target_version}" ]; then
12541335
log "Target version supports hostapps, no device type support check required."
12551336
else
@@ -1300,9 +1381,12 @@ if [ "${SLUG}" = "raspberrypi4-64" ] && \
13001381
fi
13011382
fi
13021383

1303-
target_image=$(get_image_location "${target_version}")
1384+
# Already retrieved if script inputs from App UUID and release commit.
13041385
if [ -z "${target_image}" ]; then
1305-
log ERROR "Zero or multiple matching target hostapp releases found, update attempt has failed..."
1386+
target_image=$(get_image_location "${target_version}")
1387+
if [ -z "${target_image}" ]; then
1388+
log ERROR "Zero or multiple matching target hostapp releases found, update attempt has failed..."
1389+
fi
13061390
fi
13071391

13081392
# starting with the balena engine, we can use native deltas

0 commit comments

Comments
 (0)