From 6b9d92915e187de3fb14dad7055aea080e8e03f8 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 26 Aug 2019 17:47:04 +1000 Subject: [PATCH 01/13] Allow minimum disk before cleanup to be customized --- packer/linux/conf/bin/bk-check-disk-space.sh | 64 ++++++++++++++----- .../conf/bin/bk-install-elastic-stack.sh | 6 ++ .../conf/buildkite-agent/hooks/environment | 9 ++- .../linux/conf/docker/cron.hourly/docker-gc | 12 +++- .../docker/cron.hourly/docker-low-disk-gc | 18 ++++-- templates/aws-stack.yml | 12 ++++ 6 files changed, 94 insertions(+), 27 deletions(-) diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index 3c25f11ef..52a98129f 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -1,25 +1,57 @@ #!/bin/bash set -euo pipefail -DISK_MIN_AVAILABLE=${DISK_MIN_AVAILABLE:-5242880} # 5GB -DISK_MIN_INODES=${DISK_MIN_INODES:-250000} # docker needs lots - -DOCKER_DIR="/var/lib/docker/" - -disk_avail=$(df -k --output=avail "$DOCKER_DIR" | tail -n1) - -echo "Disk space free: $(df -k -h --output=avail "$DOCKER_DIR" | tail -n1 | sed -e 's/^[[:space:]]//')" - -if [[ $disk_avail -lt $DISK_MIN_AVAILABLE ]]; then - echo "Not enough disk space free, cutoff is ${DISK_MIN_AVAILABLE} 🚨" >&2 - exit 1 +# Usage: +# bk-check-disk-space.sh (min disk required) (min inodes required) +# min disk required can be either an amount of bytes, a pattern like 10G +# or 500M, or a percentage like 5% +# min inodes must be a number, default to 250,000 + +# Converts human-readable units like 1.43K and 120.3M to bytes +dehumanize() { + awk '/[0-9][bB]?$/ {printf "%u\n", $1*1024} + /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} + /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024)} + /[mM][bB]?$/ {printf "%u\n", $1*(1024)} + /[kK][bB]?$/ {printf "%u\n", $1*1}' <<< "$1" +} + +min_available=${1:-5G} +docker_dir="/var/lib/docker/" + +# First check the disk available + +disk_avail=$(df -k --output=avail "$docker_dir" | tail -n1) +disk_avail_human=$(df -k -h --output=avail "$docker_dir" | tail -n1 | tr -d '[:space:]') +disk_used_pct=$(df -k --output=pcent "$docker_dir" | tail -n1 | tr -d '[:space:]' | tr -d '%') +disk_free_pct=$((100-disk_used_pct)) + +printf "Disk space free: %s (%s%%)\\n" "$disk_avail_human" "$disk_free_pct" + +# Check if the min_available is a percentage +if [[ $min_available =~ \%$ ]] ; then + if [[ $(echo "${disk_free_pct}<${min_available}" | sed 's/%//g' | bc) -gt 0 ]] ; then + echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 + exit 1 + fi +else + if [[ $disk_avail -lt $(dehumanize "$min_available") ]]; then + echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 + exit 1 + fi fi -inodes_avail=$(df -k --output=iavail "$DOCKER_DIR" | tail -n1) +# Next check inodes, these can be exhausted by docker build operations + +inodes_min_available=${2:-250000} +inodes_avail=$(df -k --output=iavail "$docker_dir" | tail -n1 | tr -d '[:space:]') +inodes_avail_human=$(df -k -h --output=iavail "$docker_dir" | tail -n1 | tr -d '[:space:]') +inodes_used_pct=$(df -k --output=ipcent "$docker_dir" | tail -n1 | tr -d '[:space:]' | tr -d '%') +inodes_free_pct=$((100-inodes_used_pct)) -echo "Inodes free: $(df -k -h --output=iavail "$DOCKER_DIR" | tail -n1 | sed -e 's/^[[:space:]]//')" +printf "Inodes free: %s (%s%%)\\n" "$inodes_avail_human" "$inodes_free_pct" -if [[ $inodes_avail -lt $DISK_MIN_INODES ]]; then - echo "Not enough inodes free, cutoff is ${DISK_MIN_INODES} 🚨" >&2 +if [[ $inodes_avail -lt $inodes_min_available ]]; then + echo "Not enough inodes free, cutoff is ${inodes_min_available} 🚨" >&2 exit 1 fi diff --git a/packer/linux/conf/bin/bk-install-elastic-stack.sh b/packer/linux/conf/bin/bk-install-elastic-stack.sh index 4c55c0cbf..982f11151 100755 --- a/packer/linux/conf/bin/bk-install-elastic-stack.sh +++ b/packer/linux/conf/bin/bk-install-elastic-stack.sh @@ -62,6 +62,12 @@ export PLUGINS_ENABLED="${PLUGINS_ENABLED[*]-}" export BUILDKITE_ECR_POLICY=${BUILDKITE_ECR_POLICY:-none} EOF +# cron-env is sourced by crontab entries and low disk scripts +cat << EOF > /var/lib/buildkite-agent/cron-env +export DISK_MIN_AVAILABLE=$DISK_MIN_AVAILABLE +export DOCKER_PRUNE_UNTIL=$DOCKER_PRUNE_UNTIL +EOF + if [[ "${BUILDKITE_AGENT_RELEASE}" == "edge" ]] ; then echo "Downloading buildkite-agent edge..." curl -Lsf -o /usr/bin/buildkite-agent-edge \ diff --git a/packer/linux/conf/buildkite-agent/hooks/environment b/packer/linux/conf/buildkite-agent/hooks/environment index d5995e13f..48712e02e 100755 --- a/packer/linux/conf/buildkite-agent/hooks/environment +++ b/packer/linux/conf/buildkite-agent/hooks/environment @@ -7,6 +7,11 @@ source ~/cfn-env echo "~~~ :llama: Setting up elastic stack environment ($BUILDKITE_STACK_VERSION)" cat ~/cfn-env +if [[ -f ~/cron-env ]] ; then + # shellcheck source=/dev/null + source ~/cron-env +fi + echo "Checking docker" if ! docker ps ; then echo "^^^ +++" @@ -17,13 +22,13 @@ if ! docker ps ; then fi echo "Checking disk space" -if ! /usr/local/bin/bk-check-disk-space.sh ; then +if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-5G}" ; then echo "Cleaning up docker resources older than ${DOCKER_PRUNE_UNTIL:-4h}" docker image prune --all --force --filter "until=${DOCKER_PRUNE_UNTIL:-4h}" echo "Checking disk space again" - if ! /usr/local/bin/bk-check-disk-space.sh ; then + if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-5G}"; then echo "Disk health checks failed" >&2 exit 1 fi diff --git a/packer/linux/conf/docker/cron.hourly/docker-gc b/packer/linux/conf/docker/cron.hourly/docker-gc index 1ab07e68f..71f85ae0a 100755 --- a/packer/linux/conf/docker/cron.hourly/docker-gc +++ b/packer/linux/conf/docker/cron.hourly/docker-gc @@ -5,10 +5,16 @@ if [[ $EUID -eq 0 ]]; then exec >> /var/log/elastic-stack.log 2>&1 # Logs to elastic-stack.log fi -DOCKER_PRUNE_UNTIL=${DOCKER_PRUNE_UNTIL:-4h} +# Load config from file if it exists +if [[ -f /var/lib/buildkite-agent/cron-env ]] ; then + # shellcheck source=/dev/null + source /var/lib/buildkite-agent/cron-env +else + DOCKER_PRUNE_UNTIL=4h +fi ## ------------------------------------------ ## Prune stuff that doesn't affect cache hits -docker network prune --force --filter "until=${DOCKER_PRUNE_UNTIL}" -docker container prune --force --filter "until=${DOCKER_PRUNE_UNTIL}" +docker network prune --force --filter "until=${!DOCKER_PRUNE_UNTIL}" +docker container prune --force --filter "until=${!DOCKER_PRUNE_UNTIL}" diff --git a/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc b/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc index ff68b64dc..44d772974 100644 --- a/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc +++ b/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc @@ -5,8 +5,6 @@ if [[ $EUID -eq 0 ]]; then exec >> /var/log/elastic-stack.log 2>&1 # Logs to elastic-stack.log fi -DOCKER_PRUNE_UNTIL=${DOCKER_PRUNE_UNTIL:-1h} - mark_instance_unhealthy() { # cancel any running buildkite builds killall -QUIT buildkite-agent || true @@ -19,14 +17,22 @@ mark_instance_unhealthy() { trap mark_instance_unhealthy ERR +# Load config from file if it exists +if [[ -f /var/lib/buildkite-agent/cron-env ]] ; then + # shellcheck source=/dev/null + source /var/lib/buildkite-agent/cron-env +else + DISK_MIN_AVAILABLE=5G +fi + ## ----------------------------------------------------------------- ## Check disk, we only want to prune images/containers if we have to -if ! /usr/local/bin/bk-check-disk-space.sh ; then - echo "Cleaning up docker resources older than ${DOCKER_PRUNE_UNTIL}" - docker image prune --all --force --filter "until=${DOCKER_PRUNE_UNTIL}" +if ! /usr/local/bin/bk-check-disk-space.sh "$DISK_MIN_AVAILABLE" ; then + echo "Cleaning up docker resources older than 1h" + docker image prune --all --force --filter "until=1h" - if ! /usr/local/bin/bk-check-disk-space.sh ; then + if ! /usr/local/bin/bk-check-disk-space.sh "$DISK_MIN_AVAILABLE" ; then echo "Disk health checks failed" >&2 exit 1 fi diff --git a/templates/aws-stack.yml b/templates/aws-stack.yml index f6855d9fb..ed9d7c776 100644 --- a/templates/aws-stack.yml +++ b/templates/aws-stack.yml @@ -375,6 +375,16 @@ Parameters: - "false" Default: "false" + MinimumDiskAvailableBeforeCleanup: + Type: String + Description: Either a percentage (%) or absolute unit (B, MB, GB) of disk below which disk cleanup is run + Default: "2GB" + + DockerPruneUntil: + Type: String + Description: How far back to prune docker networks images and containers on hourly cleanup + Default: "4h" + Outputs: VpcId: Value: @@ -857,6 +867,8 @@ Resources: BUILDKITE_ECR_POLICY=${ECRAccessPolicy} \ BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB=${BuildkiteTerminateInstanceAfterJob} \ BUILDKITE_ADDITIONAL_SUDO_PERMISSIONS=${BuildkiteAdditionalSudoPermissions} \ + DISK_MIN_AVAILABLE="${MinimumDiskAvailableBeforeCleanup}" \ + DOCKER_PRUNE_UNTIL="${DockerPruneUntil} \ AWS_DEFAULT_REGION=${AWS::Region} \ SECRETS_PLUGIN_ENABLED=${EnableSecretsPlugin} \ ECR_PLUGIN_ENABLED=${EnableECRPlugin} \ From bd5fbab21f415059b77413a7a36cfb96fd3053f7 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Tue, 27 Aug 2019 21:57:44 +1000 Subject: [PATCH 02/13] Fix mis-matched quotes --- templates/aws-stack.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/aws-stack.yml b/templates/aws-stack.yml index ed9d7c776..12de96435 100644 --- a/templates/aws-stack.yml +++ b/templates/aws-stack.yml @@ -868,7 +868,7 @@ Resources: BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB=${BuildkiteTerminateInstanceAfterJob} \ BUILDKITE_ADDITIONAL_SUDO_PERMISSIONS=${BuildkiteAdditionalSudoPermissions} \ DISK_MIN_AVAILABLE="${MinimumDiskAvailableBeforeCleanup}" \ - DOCKER_PRUNE_UNTIL="${DockerPruneUntil} \ + DOCKER_PRUNE_UNTIL="${DockerPruneUntil}" \ AWS_DEFAULT_REGION=${AWS::Region} \ SECRETS_PLUGIN_ENABLED=${EnableSecretsPlugin} \ ECR_PLUGIN_ENABLED=${EnableECRPlugin} \ From 0ae78f347f0de326c8d1247749e3a8e6cb3e0e7c Mon Sep 17 00:00:00 2001 From: Arturo Pie Date: Wed, 2 Oct 2019 22:07:19 -0400 Subject: [PATCH 03/13] Fix and add tests for the dehumanize function --- packer/linux/conf/bin/bk-check-disk-space.sh | 9 +---- packer/linux/conf/bin/dehumanize-test.sh | 39 ++++++++++++++++++++ packer/linux/conf/bin/dehumanize.sh | 10 +++++ 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100755 packer/linux/conf/bin/dehumanize-test.sh create mode 100644 packer/linux/conf/bin/dehumanize.sh diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index 52a98129f..43d072324 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -7,14 +7,7 @@ set -euo pipefail # or 500M, or a percentage like 5% # min inodes must be a number, default to 250,000 -# Converts human-readable units like 1.43K and 120.3M to bytes -dehumanize() { - awk '/[0-9][bB]?$/ {printf "%u\n", $1*1024} - /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} - /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024)} - /[mM][bB]?$/ {printf "%u\n", $1*(1024)} - /[kK][bB]?$/ {printf "%u\n", $1*1}' <<< "$1" -} +. "$(dirname "$0")"/dehumanize.sh min_available=${1:-5G} docker_dir="/var/lib/docker/" diff --git a/packer/linux/conf/bin/dehumanize-test.sh b/packer/linux/conf/bin/dehumanize-test.sh new file mode 100755 index 000000000..1e3d4c393 --- /dev/null +++ b/packer/linux/conf/bin/dehumanize-test.sh @@ -0,0 +1,39 @@ +#!/bin/bash +set -o pipefail + +. "$(dirname "$0")"/dehumanize.sh + +test_without_unit(){ + assertEquals 45 $(dehumanize 45) +} + +test_bytes(){ + assertEquals 45 $(dehumanize 45b) + assertEquals 45 $(dehumanize 45B) +} + +test_kilobytes(){ + assertEquals 46080 $(dehumanize 45kb) + assertEquals 46080 $(dehumanize 45KB) +} + +test_megabytes(){ + assertEquals 47185920 $(dehumanize 45mb) + assertEquals 47185920 $(dehumanize 45MB) +} + +test_gigabytes(){ + assertEquals 48318382080 $(dehumanize 45gb) + assertEquals 48318382080 $(dehumanize 45GB) +} + +test_terabytes(){ + assertEquals 49478023249920 $(dehumanize 45tb) + assertEquals 49478023249920 $(dehumanize 45TB) +} + +test_using_decimals(){ + assertEquals 1610612736 $(dehumanize 1.5gb) +} + +. shunit2 diff --git a/packer/linux/conf/bin/dehumanize.sh b/packer/linux/conf/bin/dehumanize.sh new file mode 100644 index 000000000..ca90e8897 --- /dev/null +++ b/packer/linux/conf/bin/dehumanize.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +# Converts human-readable units like 1.43K and 120.3M to bytes +dehumanize() { + awk '/[0-9][bB]?$/ {printf "%u\n", $1*1} + /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024*1024)} + /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} + /[mM][bB]?$/ {printf "%u\n", $1*(1024*1024)} + /[kK][bB]?$/ {printf "%u\n", $1*1024}' <<< "$1" +} From dfa2eb3eb8830ccc86c0fd4b0a396a215f03d3f3 Mon Sep 17 00:00:00 2001 From: Arturo Pie Date: Sun, 6 Oct 2019 11:03:06 -0400 Subject: [PATCH 04/13] removes default disk min duplication. bk-check-disk-space will have the default --- packer/linux/conf/buildkite-agent/hooks/environment | 4 ++-- packer/linux/conf/docker/cron.hourly/docker-low-disk-gc | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packer/linux/conf/buildkite-agent/hooks/environment b/packer/linux/conf/buildkite-agent/hooks/environment index 48712e02e..b853dba19 100755 --- a/packer/linux/conf/buildkite-agent/hooks/environment +++ b/packer/linux/conf/buildkite-agent/hooks/environment @@ -22,13 +22,13 @@ if ! docker ps ; then fi echo "Checking disk space" -if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-5G}" ; then +if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-}" ; then echo "Cleaning up docker resources older than ${DOCKER_PRUNE_UNTIL:-4h}" docker image prune --all --force --filter "until=${DOCKER_PRUNE_UNTIL:-4h}" echo "Checking disk space again" - if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-5G}"; then + if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-}"; then echo "Disk health checks failed" >&2 exit 1 fi diff --git a/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc b/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc index 44d772974..c0697c9c3 100644 --- a/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc +++ b/packer/linux/conf/docker/cron.hourly/docker-low-disk-gc @@ -21,18 +21,16 @@ trap mark_instance_unhealthy ERR if [[ -f /var/lib/buildkite-agent/cron-env ]] ; then # shellcheck source=/dev/null source /var/lib/buildkite-agent/cron-env -else - DISK_MIN_AVAILABLE=5G fi ## ----------------------------------------------------------------- ## Check disk, we only want to prune images/containers if we have to -if ! /usr/local/bin/bk-check-disk-space.sh "$DISK_MIN_AVAILABLE" ; then +if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-}" ; then echo "Cleaning up docker resources older than 1h" docker image prune --all --force --filter "until=1h" - if ! /usr/local/bin/bk-check-disk-space.sh "$DISK_MIN_AVAILABLE" ; then + if ! /usr/local/bin/bk-check-disk-space.sh "${DISK_MIN_AVAILABLE:-}" ; then echo "Disk health checks failed" >&2 exit 1 fi From f2c081e35d7a67d57959fc6f20ac5a4e7bac6528 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 13:34:37 +1100 Subject: [PATCH 05/13] Base unit-tests environment off amazonlinux 2 --- docker-compose.unit-tests.yml | 4 ++-- unit-tests/Dockerfile | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 unit-tests/Dockerfile diff --git a/docker-compose.unit-tests.yml b/docker-compose.unit-tests.yml index 0c39813f7..5bcb0246f 100644 --- a/docker-compose.unit-tests.yml +++ b/docker-compose.unit-tests.yml @@ -2,8 +2,8 @@ version: '3' services: unit-tests: - image: lucor/bats + build: ./unit-tests volumes: - .:/src:ro working_dir: /src - command: bats /src/unit-tests/ \ No newline at end of file + command: bats /src/unit-tests/ diff --git a/unit-tests/Dockerfile b/unit-tests/Dockerfile new file mode 100644 index 000000000..306a96ed2 --- /dev/null +++ b/unit-tests/Dockerfile @@ -0,0 +1,10 @@ +FROM amazonlinux:2 + +RUN yum install -y git + +RUN git clone https://github.com/bats-core/bats-core.git \ + && cd bats-core \ + && git checkout v1.1.0 \ + && ./install.sh /usr/local + +CMD [ "bash", "/usr/local/bin/bats", "/src/unit-tests/*.bats"] From 956ca6099ef27cf1c3a794a5356c69aae5787ff3 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 13:34:56 +1100 Subject: [PATCH 06/13] Move ad-hoc tests into bats tests --- packer/linux/conf/bin/dehumanize-test.sh | 39 ---------- packer/linux/conf/bin/dehumanize.sh | 17 +++-- unit-tests/dehumanize.bats | 95 ++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 46 deletions(-) delete mode 100755 packer/linux/conf/bin/dehumanize-test.sh mode change 100644 => 100755 packer/linux/conf/bin/dehumanize.sh create mode 100644 unit-tests/dehumanize.bats diff --git a/packer/linux/conf/bin/dehumanize-test.sh b/packer/linux/conf/bin/dehumanize-test.sh deleted file mode 100755 index 1e3d4c393..000000000 --- a/packer/linux/conf/bin/dehumanize-test.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -set -o pipefail - -. "$(dirname "$0")"/dehumanize.sh - -test_without_unit(){ - assertEquals 45 $(dehumanize 45) -} - -test_bytes(){ - assertEquals 45 $(dehumanize 45b) - assertEquals 45 $(dehumanize 45B) -} - -test_kilobytes(){ - assertEquals 46080 $(dehumanize 45kb) - assertEquals 46080 $(dehumanize 45KB) -} - -test_megabytes(){ - assertEquals 47185920 $(dehumanize 45mb) - assertEquals 47185920 $(dehumanize 45MB) -} - -test_gigabytes(){ - assertEquals 48318382080 $(dehumanize 45gb) - assertEquals 48318382080 $(dehumanize 45GB) -} - -test_terabytes(){ - assertEquals 49478023249920 $(dehumanize 45tb) - assertEquals 49478023249920 $(dehumanize 45TB) -} - -test_using_decimals(){ - assertEquals 1610612736 $(dehumanize 1.5gb) -} - -. shunit2 diff --git a/packer/linux/conf/bin/dehumanize.sh b/packer/linux/conf/bin/dehumanize.sh old mode 100644 new mode 100755 index ca90e8897..4e68b5b01 --- a/packer/linux/conf/bin/dehumanize.sh +++ b/packer/linux/conf/bin/dehumanize.sh @@ -1,10 +1,13 @@ #!/usr/bin/env bash +if [[ ! "${1:-}" =~ ^[0-9] ]] ; then + printf "Invalid input, must start with a number: %q\n" "${1:-}" >&2 + exit 1 +fi + # Converts human-readable units like 1.43K and 120.3M to bytes -dehumanize() { - awk '/[0-9][bB]?$/ {printf "%u\n", $1*1} - /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024*1024)} - /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} - /[mM][bB]?$/ {printf "%u\n", $1*(1024*1024)} - /[kK][bB]?$/ {printf "%u\n", $1*1024}' <<< "$1" -} +/usr/bin/awk '/[0-9][bB]?$/ {printf "%u\n", $1*1} + /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024*1024)} + /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} + /[mM][bB]?$/ {printf "%u\n", $1*(1024*1024)} + /[kK][bB]?$/ {printf "%u\n", $1*1024}' <<< "$1" diff --git a/unit-tests/dehumanize.bats b/unit-tests/dehumanize.bats new file mode 100644 index 000000000..b50dfe2dd --- /dev/null +++ b/unit-tests/dehumanize.bats @@ -0,0 +1,95 @@ +#!/usr/bin/env bats + +DEHUMANIZE_SCRIPT="/src/packer/linux/conf/bin/dehumanize.sh" + +@test "dehumanize with invalid input" { + run "$DEHUMANIZE_SCRIPT" "llamas" + [ "$status" -eq 1 ] +} + +@test "dehumanize with no input" { + run "$DEHUMANIZE_SCRIPT" + [ "$status" -eq 1 ] +} + +@test "dehumanize without unit" { + run "$DEHUMANIZE_SCRIPT" "45" + [ "$status" -eq 0 ] + [ "$output" = "45" ] +} + +@test "dehumanize with bytes" { + run "$DEHUMANIZE_SCRIPT" "45b" + [ "$status" -eq 0 ] + [ "$output" = "45" ] + run "$DEHUMANIZE_SCRIPT" "45B" + [ "$status" -eq 0 ] + [ "$output" = "45" ] +} + +@test "dehumanize with kilobytes" { + run "$DEHUMANIZE_SCRIPT" "45kb" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] + run "$DEHUMANIZE_SCRIPT" "45KB" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] + run "$DEHUMANIZE_SCRIPT" "45Kb" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] + run "$DEHUMANIZE_SCRIPT" "45K" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] +} + +@test "dehumanize with megabytes" { + run "$DEHUMANIZE_SCRIPT" "45mb" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] + run "$DEHUMANIZE_SCRIPT" "45MB" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] + run "$DEHUMANIZE_SCRIPT" "45Mb" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] + run "$DEHUMANIZE_SCRIPT" "45M" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] +} + +@test "dehumanize with gigabytes" { + run "$DEHUMANIZE_SCRIPT" "45gb" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] + run "$DEHUMANIZE_SCRIPT" "45GB" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] + run "$DEHUMANIZE_SCRIPT" "45Gb" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] + run "$DEHUMANIZE_SCRIPT" "45G" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] +} + +@test "dehumanize with terabytes" { + run "$DEHUMANIZE_SCRIPT" "45tb" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] + run "$DEHUMANIZE_SCRIPT" "45TB" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] + run "$DEHUMANIZE_SCRIPT" "45Tb" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] + run "$DEHUMANIZE_SCRIPT" "45T" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] +} + +@test "dehumanize with decimals" { + run "$DEHUMANIZE_SCRIPT" "1.5gb" + [ "$status" -eq 0 ] + [ "$output" = "1610612736" ] + run "$DEHUMANIZE_SCRIPT" "45TB" +} From 238a584464d087bffcb38848563840d548369801 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 14:33:28 +1100 Subject: [PATCH 07/13] Call dehumanize script vs include --- packer/linux/conf/bin/bk-check-disk-space.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index 43d072324..c4d407888 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -7,7 +7,9 @@ set -euo pipefail # or 500M, or a percentage like 5% # min inodes must be a number, default to 250,000 -. "$(dirname "$0")"/dehumanize.sh +dehumanize() { + "$(dirname "$0")"/dehumanize.sh "$@" +} min_available=${1:-5G} docker_dir="/var/lib/docker/" From 3135d6841b84e51ec6929412fda0a7e57daac673 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 14:36:25 +1100 Subject: [PATCH 08/13] Convert tabs to spaces --- unit-tests/dehumanize.bats | 44 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/unit-tests/dehumanize.bats b/unit-tests/dehumanize.bats index b50dfe2dd..4c84cdc97 100644 --- a/unit-tests/dehumanize.bats +++ b/unit-tests/dehumanize.bats @@ -4,92 +4,92 @@ DEHUMANIZE_SCRIPT="/src/packer/linux/conf/bin/dehumanize.sh" @test "dehumanize with invalid input" { run "$DEHUMANIZE_SCRIPT" "llamas" - [ "$status" -eq 1 ] + [ "$status" -eq 1 ] } @test "dehumanize with no input" { run "$DEHUMANIZE_SCRIPT" - [ "$status" -eq 1 ] + [ "$status" -eq 1 ] } @test "dehumanize without unit" { run "$DEHUMANIZE_SCRIPT" "45" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "45" ] } @test "dehumanize with bytes" { run "$DEHUMANIZE_SCRIPT" "45b" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "45" ] run "$DEHUMANIZE_SCRIPT" "45B" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "45" ] } @test "dehumanize with kilobytes" { run "$DEHUMANIZE_SCRIPT" "45kb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "46080" ] run "$DEHUMANIZE_SCRIPT" "45KB" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "46080" ] run "$DEHUMANIZE_SCRIPT" "45Kb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "46080" ] run "$DEHUMANIZE_SCRIPT" "45K" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "46080" ] } @test "dehumanize with megabytes" { run "$DEHUMANIZE_SCRIPT" "45mb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "47185920" ] run "$DEHUMANIZE_SCRIPT" "45MB" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "47185920" ] run "$DEHUMANIZE_SCRIPT" "45Mb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "47185920" ] run "$DEHUMANIZE_SCRIPT" "45M" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "47185920" ] } @test "dehumanize with gigabytes" { run "$DEHUMANIZE_SCRIPT" "45gb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "48318382080" ] run "$DEHUMANIZE_SCRIPT" "45GB" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "48318382080" ] run "$DEHUMANIZE_SCRIPT" "45Gb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "48318382080" ] run "$DEHUMANIZE_SCRIPT" "45G" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "48318382080" ] } @test "dehumanize with terabytes" { run "$DEHUMANIZE_SCRIPT" "45tb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "49478023249920" ] run "$DEHUMANIZE_SCRIPT" "45TB" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "49478023249920" ] run "$DEHUMANIZE_SCRIPT" "45Tb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "49478023249920" ] run "$DEHUMANIZE_SCRIPT" "45T" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "49478023249920" ] } @test "dehumanize with decimals" { run "$DEHUMANIZE_SCRIPT" "1.5gb" - [ "$status" -eq 0 ] + [ "$status" -eq 0 ] [ "$output" = "1610612736" ] run "$DEHUMANIZE_SCRIPT" "45TB" } From 414cf86472388885e9a488325ff175fac7cad114 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 15:39:27 +1100 Subject: [PATCH 09/13] Dehumanize made me sad, parse-byte-units is better --- packer/linux/conf/bin/bk-check-disk-space.sh | 6 +- .../{dehumanize.sh => bk-parse-byte-units.sh} | 5 +- unit-tests/dehumanize.bats | 95 ------------------- unit-tests/parse-byte-units.bats | 95 +++++++++++++++++++ 4 files changed, 99 insertions(+), 102 deletions(-) rename packer/linux/conf/bin/{dehumanize.sh => bk-parse-byte-units.sh} (88%) delete mode 100644 unit-tests/dehumanize.bats create mode 100644 unit-tests/parse-byte-units.bats diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index c4d407888..e89957179 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -7,10 +7,6 @@ set -euo pipefail # or 500M, or a percentage like 5% # min inodes must be a number, default to 250,000 -dehumanize() { - "$(dirname "$0")"/dehumanize.sh "$@" -} - min_available=${1:-5G} docker_dir="/var/lib/docker/" @@ -30,7 +26,7 @@ if [[ $min_available =~ \%$ ]] ; then exit 1 fi else - if [[ $disk_avail -lt $(dehumanize "$min_available") ]]; then + if [[ $disk_avail -lt $(/usr/local/bin/bk-parse-byte-units.sh "$min_available") ]]; then echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 exit 1 fi diff --git a/packer/linux/conf/bin/dehumanize.sh b/packer/linux/conf/bin/bk-parse-byte-units.sh similarity index 88% rename from packer/linux/conf/bin/dehumanize.sh rename to packer/linux/conf/bin/bk-parse-byte-units.sh index 4e68b5b01..d68ba62f7 100755 --- a/packer/linux/conf/bin/dehumanize.sh +++ b/packer/linux/conf/bin/bk-parse-byte-units.sh @@ -1,12 +1,13 @@ #!/usr/bin/env bash +# Converts human-readable units like 1.43K and 120.3M to bytes if [[ ! "${1:-}" =~ ^[0-9] ]] ; then printf "Invalid input, must start with a number: %q\n" "${1:-}" >&2 exit 1 fi -# Converts human-readable units like 1.43K and 120.3M to bytes -/usr/bin/awk '/[0-9][bB]?$/ {printf "%u\n", $1*1} +/usr/bin/awk \ + '/[0-9][bB]?$/ {printf "%u\n", $1*1} /[tT][bB]?$/ {printf "%u\n", $1*(1024*1024*1024*1024)} /[gG][bB]?$/ {printf "%u\n", $1*(1024*1024*1024)} /[mM][bB]?$/ {printf "%u\n", $1*(1024*1024)} diff --git a/unit-tests/dehumanize.bats b/unit-tests/dehumanize.bats deleted file mode 100644 index 4c84cdc97..000000000 --- a/unit-tests/dehumanize.bats +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env bats - -DEHUMANIZE_SCRIPT="/src/packer/linux/conf/bin/dehumanize.sh" - -@test "dehumanize with invalid input" { - run "$DEHUMANIZE_SCRIPT" "llamas" - [ "$status" -eq 1 ] -} - -@test "dehumanize with no input" { - run "$DEHUMANIZE_SCRIPT" - [ "$status" -eq 1 ] -} - -@test "dehumanize without unit" { - run "$DEHUMANIZE_SCRIPT" "45" - [ "$status" -eq 0 ] - [ "$output" = "45" ] -} - -@test "dehumanize with bytes" { - run "$DEHUMANIZE_SCRIPT" "45b" - [ "$status" -eq 0 ] - [ "$output" = "45" ] - run "$DEHUMANIZE_SCRIPT" "45B" - [ "$status" -eq 0 ] - [ "$output" = "45" ] -} - -@test "dehumanize with kilobytes" { - run "$DEHUMANIZE_SCRIPT" "45kb" - [ "$status" -eq 0 ] - [ "$output" = "46080" ] - run "$DEHUMANIZE_SCRIPT" "45KB" - [ "$status" -eq 0 ] - [ "$output" = "46080" ] - run "$DEHUMANIZE_SCRIPT" "45Kb" - [ "$status" -eq 0 ] - [ "$output" = "46080" ] - run "$DEHUMANIZE_SCRIPT" "45K" - [ "$status" -eq 0 ] - [ "$output" = "46080" ] -} - -@test "dehumanize with megabytes" { - run "$DEHUMANIZE_SCRIPT" "45mb" - [ "$status" -eq 0 ] - [ "$output" = "47185920" ] - run "$DEHUMANIZE_SCRIPT" "45MB" - [ "$status" -eq 0 ] - [ "$output" = "47185920" ] - run "$DEHUMANIZE_SCRIPT" "45Mb" - [ "$status" -eq 0 ] - [ "$output" = "47185920" ] - run "$DEHUMANIZE_SCRIPT" "45M" - [ "$status" -eq 0 ] - [ "$output" = "47185920" ] -} - -@test "dehumanize with gigabytes" { - run "$DEHUMANIZE_SCRIPT" "45gb" - [ "$status" -eq 0 ] - [ "$output" = "48318382080" ] - run "$DEHUMANIZE_SCRIPT" "45GB" - [ "$status" -eq 0 ] - [ "$output" = "48318382080" ] - run "$DEHUMANIZE_SCRIPT" "45Gb" - [ "$status" -eq 0 ] - [ "$output" = "48318382080" ] - run "$DEHUMANIZE_SCRIPT" "45G" - [ "$status" -eq 0 ] - [ "$output" = "48318382080" ] -} - -@test "dehumanize with terabytes" { - run "$DEHUMANIZE_SCRIPT" "45tb" - [ "$status" -eq 0 ] - [ "$output" = "49478023249920" ] - run "$DEHUMANIZE_SCRIPT" "45TB" - [ "$status" -eq 0 ] - [ "$output" = "49478023249920" ] - run "$DEHUMANIZE_SCRIPT" "45Tb" - [ "$status" -eq 0 ] - [ "$output" = "49478023249920" ] - run "$DEHUMANIZE_SCRIPT" "45T" - [ "$status" -eq 0 ] - [ "$output" = "49478023249920" ] -} - -@test "dehumanize with decimals" { - run "$DEHUMANIZE_SCRIPT" "1.5gb" - [ "$status" -eq 0 ] - [ "$output" = "1610612736" ] - run "$DEHUMANIZE_SCRIPT" "45TB" -} diff --git a/unit-tests/parse-byte-units.bats b/unit-tests/parse-byte-units.bats new file mode 100644 index 000000000..550bc1cb9 --- /dev/null +++ b/unit-tests/parse-byte-units.bats @@ -0,0 +1,95 @@ +#!/usr/bin/env bats + +PARSE_BYTE_UNITS_SCRIPT="/src/packer/linux/conf/bin/bk-parse-byte-units.sh" + +@test "parse with invalid input" { + run "$PARSE_BYTE_UNITS_SCRIPT" "llamas" + [ "$status" -eq 1 ] +} + +@test "parse with no input" { + run "$PARSE_BYTE_UNITS_SCRIPT" + [ "$status" -eq 1 ] +} + +@test "parse without unit" { + run "$PARSE_BYTE_UNITS_SCRIPT" "45" + [ "$status" -eq 0 ] + [ "$output" = "45" ] +} + +@test "parse with bytes" { + run "$PARSE_BYTE_UNITS_SCRIPT" "45b" + [ "$status" -eq 0 ] + [ "$output" = "45" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45B" + [ "$status" -eq 0 ] + [ "$output" = "45" ] +} + +@test "parse with kilobytes" { + run "$PARSE_BYTE_UNITS_SCRIPT" "45kb" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45KB" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45Kb" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45K" + [ "$status" -eq 0 ] + [ "$output" = "46080" ] +} + +@test "parse with megabytes" { + run "$PARSE_BYTE_UNITS_SCRIPT" "45mb" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45MB" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45Mb" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45M" + [ "$status" -eq 0 ] + [ "$output" = "47185920" ] +} + +@test "parse with gigabytes" { + run "$PARSE_BYTE_UNITS_SCRIPT" "45gb" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45GB" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45Gb" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45G" + [ "$status" -eq 0 ] + [ "$output" = "48318382080" ] +} + +@test "parse with terabytes" { + run "$PARSE_BYTE_UNITS_SCRIPT" "45tb" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45TB" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45Tb" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45T" + [ "$status" -eq 0 ] + [ "$output" = "49478023249920" ] +} + +@test "parse with decimals" { + run "$PARSE_BYTE_UNITS_SCRIPT" "1.5gb" + [ "$status" -eq 0 ] + [ "$output" = "1610612736" ] + run "$PARSE_BYTE_UNITS_SCRIPT" "45TB" +} From 5c159504e111d8ccb8faf37b9d9c74a908596819 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 15:39:50 +1100 Subject: [PATCH 10/13] Clean up permissions tests to remove warnings --- ...ix-buildkite-agent-builds-permissions.bats | 45 ------------------- 1 file changed, 45 deletions(-) diff --git a/unit-tests/fix-buildkite-agent-builds-permissions.bats b/unit-tests/fix-buildkite-agent-builds-permissions.bats index 4a4af13e9..cd450472b 100644 --- a/unit-tests/fix-buildkite-agent-builds-permissions.bats +++ b/unit-tests/fix-buildkite-agent-builds-permissions.bats @@ -5,29 +5,14 @@ FIX_PERMISSIONS_SCRIPT="/src/packer/linux/conf/buildkite-agent/scripts/fix-build @test "Slashes in the agent arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "/" "abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the agent arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc/" "abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the agent arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "/abc" "abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the agent arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc/def" "abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the agent arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc/def/ghi" "abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the agent arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "/abc/" "abc" "abc" [ "$status" -eq 1 ] } @@ -35,29 +20,14 @@ FIX_PERMISSIONS_SCRIPT="/src/packer/linux/conf/buildkite-agent/scripts/fix-build @test "Slashes in the org arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "/" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the org arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc/" "abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the org arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "/abc" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the org arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc/def" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the org arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc/def/ghi" "abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the org arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "/abc/" "abc" [ "$status" -eq 1 ] } @@ -65,29 +35,14 @@ FIX_PERMISSIONS_SCRIPT="/src/packer/linux/conf/buildkite-agent/scripts/fix-build @test "Slashes in the pipeline arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc" "/" [ "$status" -eq 1 ] -} - -@test "Slashes in the pipeline arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc" "abc/" [ "$status" -eq 1 ] -} - -@test "Slashes in the pipeline arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc" "/abc" [ "$status" -eq 1 ] -} - -@test "Slashes in the pipeline arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc" "abc/def" [ "$status" -eq 1 ] -} - -@test "Slashes in the pipeline arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc" "abc/def/ghi" [ "$status" -eq 1 ] -} - -@test "Slashes in the pipeline arg cause an exit 1" { run "$FIX_PERMISSIONS_SCRIPT" "abc" "abc" "/abc/" [ "$status" -eq 1 ] } From a2764a883fbcd94a27f69b9881253511489de15a Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 16:53:48 +1100 Subject: [PATCH 11/13] Clearer error messages on free disk space --- packer/linux/conf/bin/bk-check-disk-space.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index e89957179..8abd45bba 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -22,12 +22,13 @@ printf "Disk space free: %s (%s%%)\\n" "$disk_avail_human" "$disk_free_pct" # Check if the min_available is a percentage if [[ $min_available =~ \%$ ]] ; then if [[ $(echo "${disk_free_pct}<${min_available}" | sed 's/%//g' | bc) -gt 0 ]] ; then - echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 + echo "Not enough disk space free, cutoff percentage is ${min_available} 🚨" >&2 exit 1 fi else - if [[ $disk_avail -lt $(/usr/local/bin/bk-parse-byte-units.sh "$min_available") ]]; then - echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 + min_available_bytes="$(/usr/local/bin/bk-parse-byte-units.sh "$min_available")" + if [[ $disk_avail -lt $min_available_bytes ]]; then + echo "Not enough disk space free, cutoff is ${min_available} ($min_available_bytes bytes) 🚨" >&2 exit 1 fi fi From 3b952560e4d4681ed130fd3aa01aee0e1e7f8e7c Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 17:13:15 +1100 Subject: [PATCH 12/13] Add debugging to disk usage output --- packer/linux/conf/bin/bk-check-disk-space.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index 8abd45bba..a6ceff330 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -28,7 +28,7 @@ if [[ $min_available =~ \%$ ]] ; then else min_available_bytes="$(/usr/local/bin/bk-parse-byte-units.sh "$min_available")" if [[ $disk_avail -lt $min_available_bytes ]]; then - echo "Not enough disk space free, cutoff is ${min_available} ($min_available_bytes bytes) 🚨" >&2 + echo "Not enough disk space free, cutoff is ${min_available} ($disk_avail < $min_available_bytes) 🚨" >&2 exit 1 fi fi From 99c619eff73946b7ffb631a7e31f6d0e8ba3f933 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Mon, 7 Oct 2019 17:30:57 +1100 Subject: [PATCH 13/13] Disk available needs to be in bytes --- packer/linux/conf/bin/bk-check-disk-space.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packer/linux/conf/bin/bk-check-disk-space.sh b/packer/linux/conf/bin/bk-check-disk-space.sh index a6ceff330..df88c8a40 100755 --- a/packer/linux/conf/bin/bk-check-disk-space.sh +++ b/packer/linux/conf/bin/bk-check-disk-space.sh @@ -26,9 +26,10 @@ if [[ $min_available =~ \%$ ]] ; then exit 1 fi else + disk_avail_bytes="$((disk_avail*1024))" min_available_bytes="$(/usr/local/bin/bk-parse-byte-units.sh "$min_available")" - if [[ $disk_avail -lt $min_available_bytes ]]; then - echo "Not enough disk space free, cutoff is ${min_available} ($disk_avail < $min_available_bytes) 🚨" >&2 + if [[ $disk_avail_bytes -lt $min_available_bytes ]]; then + echo "Not enough disk space free, cutoff is ${min_available} 🚨" >&2 exit 1 fi fi