-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
79 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,13 @@ | |
# shellcheck disable=SC2154,SC2034,SC1090 | ||
set -ae | ||
|
||
[[ $VERBOSE =~ on|On|Yes|yes|true|True ]] && set -x | ||
curl_opts="--retry 3 --fail" | ||
if [[ $VERBOSE =~ on|On|Yes|yes|true|True ]]; then | ||
set -x | ||
curl_opts="${curl_opts} --verbose" | ||
else | ||
curl_opts="${curl_opts} --silent" | ||
fi | ||
|
||
source /usr/sbin/functions | ||
|
||
|
@@ -20,6 +26,7 @@ function remove_update_lock() { | |
} | ||
|
||
function cleanup() { | ||
shutdown_dut | ||
remove_test_assets | ||
remove_update_lock | ||
|
||
|
@@ -28,14 +35,22 @@ function cleanup() { | |
} | ||
trap 'cleanup' EXIT | ||
|
||
function shutdown_dut() { | ||
local balena_device_uuid | ||
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)" | ||
|
||
if [[ -n $balena_device_uuid ]]; then | ||
with_backoff balena device "${balena_device_uuid}" | ||
balena device shutdown -f "${balena_device_uuid}" || true | ||
fi | ||
} | ||
|
||
function set_update_lock { | ||
if [[ -n "$BALENA_SUPERVISOR_ADDRESS" ]] && [[ -n "$BALENA_SUPERVISOR_API_KEY" ]]; then | ||
while [[ $(curl --silent --retry "${ATTEMPTS}" --fail \ | ||
"${BALENA_SUPERVISOR_ADDRESS}/v1/device?apikey=${BALENA_SUPERVISOR_API_KEY}" \ | ||
while [[ $(curl ${curl_opts} "${BALENA_SUPERVISOR_ADDRESS}/v1/device?apikey=${BALENA_SUPERVISOR_API_KEY}" \ | ||
-H "Content-Type: application/json" | jq -r '.update_pending') == 'true' ]]; do | ||
|
||
curl --silent --retry "${ATTEMPTS}" --fail \ | ||
"${BALENA_SUPERVISOR_ADDRESS}/v1/device?apikey=${BALENA_SUPERVISOR_API_KEY}" \ | ||
curl ${curl_opts} "${BALENA_SUPERVISOR_ADDRESS}/v1/device?apikey=${BALENA_SUPERVISOR_API_KEY}" \ | ||
-H "Content-Type: application/json" | jq -r | ||
|
||
sleep "$(( (RANDOM % 3) + 3 ))s" | ||
|
@@ -72,7 +87,7 @@ function update_ca_certificates() { | |
} | ||
|
||
function wait_for_api() { | ||
while ! curl --silent --fail "https://api.${DNS_TLD}/ping"; do | ||
while ! curl ${curl_opts} "https://api.${DNS_TLD}/ping"; do | ||
sleep "$(( (RANDOM % 5) + 5 ))s" | ||
done | ||
} | ||
|
@@ -139,20 +154,20 @@ function configure_virtual_device() { | |
} | ||
|
||
function check_device_status() { | ||
if [[ -e /balena/config.json ]]; then | ||
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)" | ||
|
||
if [[ -n $balena_device_uuid ]]; then | ||
is_online="$(balena devices --json --fleet "${TEST_FLEET}" \ | ||
| jq -r --arg uuid "${balena_device_uuid}" '.[] | select(.uuid==$uuid).is_online == true')" | ||
|
||
if [[ $is_online =~ true ]]; then | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
fi | ||
fi | ||
if [[ -e /balena/config.json ]]; then | ||
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)" | ||
|
||
if [[ -n $balena_device_uuid ]]; then | ||
is_online="$(balena devices --json --fleet "${TEST_FLEET}" \ | ||
| jq -r --arg uuid "${balena_device_uuid}" '.[] | select(.uuid==$uuid).is_online == true')" | ||
|
||
if [[ $is_online =~ true ]]; then | ||
return 0 | ||
else | ||
return 1 | ||
fi | ||
fi | ||
fi | ||
} | ||
|
||
function wait_for_device() { | ||
|
@@ -206,25 +221,30 @@ function get_release_id() { | |
} | ||
|
||
function supervisor_update_target_state() { | ||
local balena_device_uuid | ||
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)" | ||
|
||
if [[ -n $balena_device_uuid ]]; then | ||
while ! curl -X POST --silent --fail \ | ||
while ! curl ${curl_opts} "https://api.${DNS_TLD}/supervisor/v1/update" \ | ||
--header "Content-Type: application/json" \ | ||
--header "Authorization: Bearer $(cat <~/.balena/token)" \ | ||
--data "{\"uuid\": \"${balena_device_uuid}\", \"data\": {\"force\": true}}" \ | ||
"https://api.${DNS_TLD}/supervisor/v1/update"; do | ||
--data "{\"uuid\": \"${balena_device_uuid}\", \"data\": {\"force\": true}}"; do | ||
|
||
sleep "$(( (RANDOM % 5) + 5 ))s" | ||
done | ||
fi | ||
} | ||
|
||
function check_running_release() { | ||
local balena_device_uuid | ||
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)" | ||
|
||
if [[ -n $balena_device_uuid ]] && [[ -n $1 ]]; then | ||
while ! [[ $(balena device "${balena_device_uuid}" | grep -E ^COMMIT | awk '{print $2}') =~ ${should_be_running_release_id} ]]; do | ||
local should_be_running_release | ||
should_be_running_release="$(get_release_commit)" | ||
[[ -z $should_be_running_release ]] && false | ||
|
||
if [[ -n $balena_device_uuid ]]; then | ||
while ! [[ $(balena device "${balena_device_uuid}" | grep -E ^COMMIT | awk '{print $2}') =~ ${should_be_running_release} ]]; do | ||
running_release_id="$(balena device "${balena_device_uuid}" | grep -E ^COMMIT | awk '{print $2}')" | ||
printf 'please wait, device %s should be running %s, but is still running %s...\n' \ | ||
"${balena_device_uuid}" \ | ||
|
@@ -233,9 +253,6 @@ function check_running_release() { | |
|
||
sleep "$(( (RANDOM % 5) + 5 ))s" | ||
done | ||
|
||
with_backoff balena device "${balena_device_uuid}" | ||
with_backoff balena device shutdown -f "${balena_device_uuid}" | ||
fi | ||
} | ||
|
||
|
@@ -249,15 +266,19 @@ function get_os_version() { | |
} | ||
|
||
function upload_release_asset() { | ||
local release_id | ||
release_id=${1:-} | ||
|
||
curl --silent --retry ${{ env.RETRY }} --fail "https://api.$DNS_TLD/resin/release_asset" \ | ||
--header "Authorization: Bearer $(cat <~/.balena/token)" \ | ||
--form '[email protected]' \ | ||
--form "release=${release_id}" \ | ||
--form "asset_key=logo-$((RANDOM)).png" | jq -re .asset.href \ | ||
| xargs curl --retry ${{ env.RETRY }} -sfivo /tmp/logo-$((RANDOM)).png | ||
if [[ "$RELEASE_ASSETS_TEST" =~ true ]]; then | ||
local release_id | ||
release_id=${1:-1} | ||
release_asset="$(find / -type f -name '*.png' | head -n 1)" | ||
|
||
curl ${curl_opts} "https://api.${DNS_TLD}/resin/release_asset" \ | ||
--header "Authorization: Bearer $(cat <~/.balena/token)" \ | ||
--form "asset=@${release_asset}" \ | ||
--form "release=${release_id}" \ | ||
--form "asset_key=$((RANDOM))-$(basename ${release_asset})" \ | ||
| jq -re .asset.href \ | ||
| xargs curl ${curl_opts} -o "/tmp/$((RANDOM))-$(basename ${release_asset})" | ||
fi | ||
} | ||
|
||
# --- main | ||
|
@@ -272,7 +293,6 @@ else | |
TLD="${DNS_TLD}" | ||
fi | ||
|
||
ATTEMPTS=${ATTEMPTS:-3} | ||
BALENA_API_URL=${BALENA_API_URL:-https://api.balena-cloud.com} | ||
BALENARC_BALENA_URL="${DNS_TLD}" | ||
CERTS=${CERTS:-/certs} | ||
|
@@ -300,14 +320,9 @@ set_update_lock | |
download_os_image | ||
configure_virtual_device | ||
deploy_release | ||
upload_release_asset "$(get_release_id)" # upload an additional asset to a release | ||
remove_update_lock | ||
# .. end | ||
|
||
wait_for_device # spin here until test-device comes online | ||
|
||
# .. and ensure the device is running our release | ||
should_be_running_release_id="$(get_release_commit)" | ||
check_running_release "${should_be_running_release_id}" | ||
|
||
# finally see if we can upload an additional asset to a release | ||
upload_release_asset "$(get_release_id)" | ||
check_running_release # .. and ensure the device is running our release |