diff options
author | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2017-04-28 08:16:19 +0000 |
---|---|---|
committer | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2017-04-28 10:15:04 +0000 |
commit | 70a58c74e7ffec39d88dbed5f04955ba3211849c (patch) | |
tree | 5238cc757f3cd1151a832a31d0d6272f1ae9f5cf /tcwg-cleanup-stale-workspaces.sh | |
parent | aa6ef0d5b6d95e7730257ba9efcb0ed760ab7eae (diff) |
tcwg-cleanup-stale-workspaces: New script, move from .yaml file.
Change-Id: I64040596242119d4d9ecfd02c07a1892cba07b4e
Diffstat (limited to 'tcwg-cleanup-stale-workspaces.sh')
-rwxr-xr-x | tcwg-cleanup-stale-workspaces.sh | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tcwg-cleanup-stale-workspaces.sh b/tcwg-cleanup-stale-workspaces.sh new file mode 100755 index 00000000..f30c0908 --- /dev/null +++ b/tcwg-cleanup-stale-workspaces.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +set -e + +days=3 +dry_run=false +workspace_top=$HOME/workspace +maxdepth=1 + +while [ $# -gt 0 ]; do + case $1 in + --days) days="$2"; shift ;; + --dry_run) dry_run="$2"; shift ;; + --workspace_top) workspace_top="$2"; shift ;; + --maxdepth) maxdepth="$2"; shift ;; + *) echo "ERROR: Wrong option: $1"; exit 1 ;; + esac + shift +done + +set -x + +if [ "$days" -lt "0" ]; then + echo "ERROR: Refusing to delete workspaces that are $days days old" + exit 1 +fi + +echo "Date / size report before (`date`):" +ls -ld $workspace_top/* || true +df -h $workspace_top || true +du -hsc $workspace_top/* || true + +# Semantics of find's mtime "+N" stands for N+1 days old or older. +days_1=$(($days-1)) + +# Handle dirs with whitespaces by setting $IFS to newline. +SAVEIFS=$IFS +IFS=$'\n' +dirs=($(find $workspace_top/ -maxdepth $maxdepth -type d -mtime +$days_1)) +IFS=$SAVEIFS + +rm_dirs=() +for dir in "${dirs[@]}"; do + if [ x"$(find "$dir" -mtime -$days | wc -l)" = x"0" ]; then + rm_dirs=("${rm_dirs[@]}" "$dir") + fi +done + +if [ ${#rm_dirs[@]} != 0 ]; then + echo "Removing directories:" + ls -ld "${rm_dirs[@]}" + du -hs "${rm_dirs[@]}" + if ! $dry_run; then + rm -rf "${rm_dirs[@]}" + else + echo "DRY_RUN: NOT REMOVING DIRECTORIES" + fi + + echo "Date / size report after (`date`):" + ls -ld $workspace_top/* || true + df -h $workspace_top || true + du -hsc $workspace_top/* || true +fi |