#!/bin/sh
# Exit 0 when the filesystem holding the Docker root is more than $1 percent full (the pressure
# prune may run), 1 to skip. Every failure skips: the prune must not run when the state is unknown.
threshold="$1"
case "$threshold" in
  '' | *[!0-9]*) echo "invalid threshold '$threshold'"; exit 1 ;;
esac
root=$(/usr/bin/docker info --format '{{.DockerRootDir}}') || { echo "docker info failed"; exit 1; }
use=$(df -P "$root" | awk 'NR == 2 { sub(/%$/, "", $5); print $5 }')
case "$use" in
  '' | *[!0-9]*) echo "cannot read the use of '$root'"; exit 1 ;;
esac
if [ "$use" -gt "$threshold" ]; then
  echo "$root is $use% full, above $threshold%: pruning"
  exit 0
fi
echo "$root is $use% full, not above $threshold%: skipping the prune"
exit 1
