2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-10-21 23:28:19 +00:00
Files
xcat-core/xCAT-genesis-scripts/usr/bin/getdestiny
T
2026-10-13 22:16:49 -03:00

96 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
set -u
metadata_file=
metadata_temporary=
once=0
destination=${XCATDEST:-}
request_pid=
usage() {
printf '%s\n' 'Usage: getdestiny [ENDPOINT] [--metadata FILE] [--once]' >&2
exit 2
}
while (($# > 0)); do
case "$1" in
--metadata)
(($# >= 2)) || usage
metadata_file=$2
shift 2
;;
--once)
once=1
shift
;;
--)
shift
(($# <= 1)) || usage
if (($# == 1)); then
destination=$1
fi
break
;;
-*) usage ;;
*)
if [[ -z $destination ]]; then
destination=$1
fi
shift
;;
esac
done
[[ -n $destination ]] || usage
source "$(dirname -- "${BASH_SOURCE[0]}")/../lib/xcat/destiny.sh" || exit 1
temporary_dir=$(mktemp -d "${TMPDIR:-/tmp}/getdestiny.XXXXXX") || exit 1
request_file=$temporary_dir/request.xml
response_file=$temporary_dir/response.xml
trap cleanup EXIT
trap 'terminate 129' HUP
trap 'terminate 130' INT
trap 'terminate 143' TERM
printf '%s\n' \
'<xcatrequest>' \
'<command>getdestiny</command>' \
'<callback_port>300</callback_port>' \
'</xcatrequest>' >"$request_file"
request_destiny() {
local request_status=0
send_destiny_request || request_status=$?
if ((request_status != 0)) && [[ ! -s $response_file ]]; then
return 1
fi
! grep -q '<error>' "$response_file"
}
while ! request_destiny; do
if ((once)); then
exit 1
fi
for ((timer = 60; timer > 0; timer--)); do
printf 'Retrying in %s seconds \r' "$timer" >&2
sleep 1
done
printf '%*s\r' 48 '' >&2
done
destiny=$(extract_element destiny)
if [[ -z $destiny ]]; then
((once)) && exit 1
printf '\n'
exit 0
fi
if [[ -n $metadata_file ]]; then
write_metadata getdestiny
fi
printf '%s\n' "$destiny"