Skip to content

Commit

Permalink
upload_package: Improve error handling
Browse files Browse the repository at this point in the history
The get_package_id function was called with "if" which disabled set -e
and thus continued after resty returned an error. The resty function
itself doesn't want to be called with set -e and thus we have to wrap
the get_package_id call to use set -e while we can explictly react on
resty errors which disables set -e. So one time we should not use
"if"/"||" and one time we should. While at it, remove the tempfile
creation which resulted in many entries that weren't cleaned up and
which were confusing because resty also creates some and one doesn't
know which is which when debugging.
  • Loading branch information
pothos committed Apr 16, 2024
1 parent 641beed commit 7895075
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions upload_package
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ COREOS_APP_ID="e96281a6-d1af-4bde-9a0a-97b76e56dc57"
-H "Accept: application/json" -H "Content-Type: application/json"

function get_package_id() {
local tmpfile="$(mktemp)"
local output
local package_id
local r

GET /apps/"${COREOS_APP_ID}"/packages >& ${tmpfile}
if jq -e 'has("packages")' ${tmpfile} > /dev/null; then
package_id=$(cat ${tmpfile} | jq '.packages' | jq '.[] | select(.version=="'${VERSION}'" and .arch=='${ARCH_ID}').id')
# Use || here to disable "set -e" as resty does not like it
output=$(GET /apps/"${COREOS_APP_ID}"/packages 2>&1) || { r="$?"; echo "${output}" ; return "$r"; }
if jq -e 'has("packages")' <(echo "${output}") > /dev/null; then
package_id=$(echo "${output}" | jq '.packages' | jq '.[] | select(.version=="'${VERSION}'" and .arch=='${ARCH_ID}').id')
else
package_id=$(cat ${tmpfile} | jq '.[] | select(.version=="'${VERSION}'" and .arch=='${ARCH_ID}').id')
package_id=$(echo "${output}" | jq '.[] | select(.version=="'${VERSION}'" and .arch=='${ARCH_ID}').id')
fi
rm -f ${tmpfile}

echo ${package_id}
}
Expand Down Expand Up @@ -88,9 +89,17 @@ else
exit 1
fi

if ! PACKAGE_ID=$(get_package_id); then
set +e
PACKAGE_ID=$(
set -e
get_package_id
)
r="$?"
set -e
if ! [ "$r" -eq 0 ]; then
echo "Failed to get metadata from Nebraska."
echo "Please make sure that you have configured a valid GITHUB_TOKEN."
echo "Error: ${PACKAGE_ID}"
exit 1
fi

Expand All @@ -106,6 +115,7 @@ for EXTRA_FILE in "${EXTRA_FILES[@]}"; do
done
EMBED_EXTRA_JOINED=$(IFS=, ; echo "${EMBED_EXTRA[*]}")
if [ -z "${PACKAGE_ID}" ]; then
# Using 'if POST' here disables set -e as resty expects
if ! PACKAGE_JSON=$(POST /apps/"${COREOS_APP_ID}"/packages " \
{
\"application_id\": \"${COREOS_APP_ID}\",
Expand All @@ -127,6 +137,7 @@ if [ -z "${PACKAGE_ID}" ]; then
" 2>&1); then
echo "Failed to update metadata on Nebraska."
echo "Please make sure that you have configured a valid GITHUB_TOKEN."
echo "Error: ${PACKAGE_JSON}"
exit 1
fi
PACKAGE_ID=$(echo "${PACKAGE_JSON}" | jq .id)
Expand Down

0 comments on commit 7895075

Please sign in to comment.