blob: 22d79f91eb44717f017b75d373bc4080b8e059a5 [file] [log] [blame]
#!/usr/bin/env bash
set -eu
set -o pipefail
[[ ${DEBUG:-} != true ]] || set -x
dirname=$(dirname "$0")
function error() { echo "ERROR: $1" >&2 ; exit 1; }
function info() { echo "INFO: $1"; }
DISK_SPACE_FILES="$*"
[[ -n $DISK_SPACE_FILES ]] || error "no disk space file specified"
WORKSPACE=${WORKSPACE:-$PWD/workspace}
WORKDIR=${WORKSPACE}/workdir
mkdir -p $WORKSPACE
rm -rf $WORKSPACE/workdir
mkdir -p $WORKSPACE/workdir
cat $DISK_SPACE_FILES >$WORKDIR/disk_space_expected.txt
function check_df
{
local dir="${1?}"
local free_min="${2?}"
local size_min="${free_min/[KMGT%]/}"
local unit="${free_min#$size_min}"
[[ -n $unit ]] || unit="K"
local df_opt=""
local free=""
case "$unit" in
%) \
size=$(df $df_opt "$dir" | tail -n1 | awk '{print $(NF-1)}' | sed 's/\%//')
size=$((100 - $size))
;;
*) \
df_opt="-B$unit"
size=$(df $df_opt "$dir" | tail -n1 | awk '{print $(NF-2)}' | sed 's/'$unit'//')
;;
esac
[[ -n $size ]] || return 1
[[ $size -ge $size_min ]] || return 1
return 0
}
info "----------"
info "Available disk space for / /boot /tmp /home:"
df -h / /boot /tmp /home
rm -f $WORKSPACE/errors.log.txt
declare -i failed_num=0
while read -r line ; do
line=$(echo "$line" | sed -e 's/ *#.*//' -e 's/^ *//')
[[ -n $line ]] || continue
path=$(echo "$line" | cut -f1 -d,)
mins=$(echo "$line" | cut -f2 -d,)
[[ -n $path ]] || continue
[[ -e $path ]] || continue # Skip if path does not exist
info "Checking disk free space for $path: minimum: $mins"
path_status=0
for min in $mins; do
status=0
check_df "$path" "$min" || status=1
[[ $status = 0 ]] || path_status=1
[[ $status = 0 ]] || echo "$path: expected $min free" >> $WORKSPACE/errors.log.txt
done
[[ $path_status = 0 ]] || failed_num=$((failed_num + 1))
done < $WORKDIR/disk_space_expected.txt
if [[ $failed_num -gt 0 ]]; then
if [[ $failed_num -eq 1 ]]; then
paths="path"
does="does"
else
paths="paths"
does="do"
fi
echo "ERROR: $failed_num $paths $does not have enough disk space"
echo "Please cleanup the disks on the following paths:"
cat $WORKSPACE/errors.log.txt
exit 1
else
echo "SUCCESS: all disk free space ok"
fi