...
 
Commits (6)
......@@ -21,6 +21,9 @@ build:
paths:
- docs/_build/html
expire_in: 1 week
only:
changes:
- docs/**/*
deploy-master:
stage: deploy
......@@ -35,3 +38,6 @@ deploy-master:
- apk update
- apk add openssh-client rsync bash
- cd docs && ./deploy.sh
only:
changes:
- docs/**/*
#!/bin/bash
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.
......@@ -54,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) ;&
......@@ -70,7 +90,7 @@ ghostAction() {
esac
local url="${server}/api/v2/disks/$diskImage/actions"
debug "curling cosmos: $url"
if [ -z "$vps" ]
if [[ -z "$vps" ]]
then
local data="{\"type\": \"$action\"}"
else
......@@ -80,6 +100,13 @@ ghostAction() {
# Record the complete body from the HTTP response in the log.
debug "output: "
debug $(jq '.' <<<"$response")
errorOccurred=$(jq '."error"' <<<"$response")
if [[ "$errorOccurred" = "true" ]]
then
errorMessage=$(jq '."message"' <<<"$response")
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.
diskSlot=$(jq '."disk_slot"' <<<"$response")
}
......@@ -96,11 +123,29 @@ domount() {
# which disk to detach; also
# 2. even if we could detach directly after unmount, there might be an error
# during detach, which we need to be able to recover from.
ghostAction "detach" "$apiToken" "$diskID"
if ! ghostAction "detach" "$apiToken" "$diskID"
then
exitWithFailure "$errorMessage"
fi
# Now tell Cosmos2 to attach the disk to the right VPS.
ghostAction "attach" "$apiToken" "$diskID" "$vpsID"
if ! ghostAction "attach" "$apiToken" "$diskID" "$vpsID"
then
exitWithFailure "$errorMessage"
fi
debug "disk_slot: $diskSlot"
# Do a sanity check on the disk slot number.
if ! [[ "$diskSlot" =~ ^[0-9]+$ ]]
then
errorMessage="disk_slot is not a non-negative integer: $diskSlot"
exitWithFailure "$errorMessage"
fi
if [[ $diskSlot -eq 0 ]]
then
errorMessage="disk_slot is 0; we refuse to mount /dev/xvda1 as that is usually the OS disk!"
exitWithFailure "$errorMessage"
fi
# Convert numeric disk slot index to device letter, so
# 0 becomes a, 1 becomes b, etc.
diskLetter=$(printf \\$(printf '%03o' $((97 + $diskSlot))))
......@@ -109,7 +154,7 @@ domount() {
# Use the `blkid` program to scan the device for an existing filesystem.
existingFileSystem=$(blkid "$device")
debug "blkid: $existingFileSystem"
if [ -z "$existingFileSystem" ]
if [[ -z "$existingFileSystem" ]]
then
debug "No existing filesystem; creating one."
mkfs.xfs "$device" >/dev/null 2>&1
......@@ -143,7 +188,8 @@ unmount() {
# This is the command Kubernetes wants us to perform.
op=$1
if [ "$op" = "init" ]; then
if [[ "$op" = "init" ]]
then
debug "init $@"
output "{\"status\":\"Success\",\"capabilities\":{\"attach\":false}}"
exit 0
......