umorpha-boxes/bin/vultr-iso-id

41 lines
959 B
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
set -euE -o pipefail
main() {
local url
url=$1
{
list_resp=$(bin/vultr-api iso)
id=$(jq -r ".isos[]|select(.filename==\"${url##*/}\").id" <<<"$list_resp")
local status
if [[ -n "$id" ]]; then
status=$(jq -r ".isos[]|select(.id==\"${id}\").status" <<<"$list_resp")
else
local n
n=$(jq '.isos|length' <<<"$list_resp")
if [[ "$n" -ge 2 ]]; then
id=$(jq -r '.isos[0].id' <<<"$list_resp")
bin/vultr-api "iso/${id}" -XDELETE
fi
local post_resp
post_resp=$(bin/vultr-api iso -XPOST --data "{\"url\":\"${url}\"}")
id=$(jq -r .iso.id <<<"$post_resp")
status=$(jq -r .iso.status <<<"$post_resp")
fi
while [[ "$status" != complete ]]; do
echo "iso status=${status}..."
sleep 1
status=$(bin/vultr-api "iso/${id}" | jq -r .iso.status)
done
echo "iso status=${status}"
} >&2
echo "$id"
}
main "$@"