Commit 05478504 authored by Arie Peterson's avatar Arie Peterson 🐚
Browse files

Improve error output

parent 9b3404b6
......@@ -4,9 +4,22 @@ set -o errexit
DRIVER_LOG=${DRIVER_LOG:-"/tmp/flex-driver.log"}
# Write output to stdout.
# Write program result to stdout.
output() {
printf "$*" >&1
status=$1
message=$2
# Format the JSON output.
jq -n -c \
--arg status "$status" \
--arg message "$message" \
'{$status, $message}' \
>&1
}
exitWithFailure() {
debug "Fatal error: ${1-}"
output "Failure" "${1-}"
exit 1
}
# Write timestamped text to the log file.
......@@ -56,12 +69,17 @@ getParams() {
debug "disk id: $diskID"
}
# Call the Cosmos2 API to perform an action on a disk image.
ghostAction() {
debug "ghostAction $*"
# Action to perform: "attach" or "detach".
local action=$1
local apiToken=$2
# Numerical ID of disk image.
local diskImage=$3
# Numerical ID of VPS.
local vps=$4
# URL to the Cosmos2 instance to talk to.
local server="$cosmosUrl"
case $action in
attach) ;&
......@@ -86,7 +104,7 @@ ghostAction() {
if [ "$errorOccurred" = "true" ]
then
errorMessage=$(jq '."message"' <<<"$response")
debug "The Cosmos2 api call returned an error: $errorMessage"
errorMessage="The Cosmos2 api call returned an error: $errorMessage"
return 1
fi
# Store the `disk_slot` field of the output in a variable for later use.
......@@ -107,14 +125,12 @@ domount() {
# during detach, which we need to be able to recover from.
if ! ghostAction "detach" "$apiToken" "$diskID"
then
output "{\"status\":\"Failure\"}"
exit 1
exitWithFailure "$errorMessage"
fi
# Now tell Cosmos2 to attach the disk to the right VPS.
if ! ghostAction "attach" "$apiToken" "$diskID" "$vpsID"
then
output "{\"status\":\"Failure\"}"
exit 1
exitWithFailure "$errorMessage"
fi
debug "disk_slot: $diskSlot"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment