Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | # |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | # |
| 7 | # Simple utility script for cleaning up old builds on the Dev Server. Should be |
| 8 | # run from the root of the archive directory. |
| 9 | |
| 10 | declare -r NUM_BUILDS_KEPT=10 |
| 11 | declare -r NUM_IMAGES_KEPT=3 |
| 12 | declare -r IMAGE_NAME="chromiumos_test_image.bin" |
| 13 | declare -r BUILD_PATTERN="[0-9]*.[0-9]*.[0-9]*.[0-9]*" |
| 14 | declare -r NEW_BUILD_PATTERN="R[0-9]*-[0-9]*.[0-9]*.[0-9]*" |
| 15 | declare -r DEV_BUILD_PATTERN="[a-zA-Z]*-${BUILD_PATTERN}" |
| 16 | |
| 17 | function cleanup_dir() { |
| 18 | # First argument is the parent directory to look for builds under. |
| 19 | local dirs=($(ls -d -t $1 2>/dev/null)) |
| 20 | # Second argument is the directory/build pattern to match against. |
| 21 | local latest=$2 |
| 22 | latest=${latest:=2} |
| 23 | |
| 24 | for ((i=${latest}; i<${#dirs[@]}; i++)); do |
| 25 | # delete those old ones. |
| 26 | echo delete ${dirs[i]} |
| 27 | rm -rf ${dirs[i]} || echo "Failed to remove ${dirs[i]}" |
| 28 | done |
| 29 | } |
| 30 | |
| 31 | |
| 32 | for d in *; do |
| 33 | if [ -d $d ]; then |
| 34 | echo truncate ${d} |
| 35 | # Cleanup stale image dirs. |
| 36 | cleanup_dir "${d}/${BUILD_PATTERN}" ${NUM_BUILDS_KEPT} |
| 37 | cleanup_dir "${d}/${DEV_BUILD_PATTERN}" ${NUM_BUILDS_KEPT} |
| 38 | cleanup_dir "${d}/${NEW_BUILD_PATTERN}" ${NUM_BUILDS_KEPT} |
| 39 | |
| 40 | # Cleanup stale image files. Dev builds don't keep images. |
| 41 | cleanup_dir "${d}/${BUILD_PATTERN}/${IMAGE_NAME}" ${NUM_IMAGES_KEPT} |
| 42 | cleanup_dir "${d}/${NEW_BUILD_PATTERN}/${IMAGE_NAME}" ${NUM_IMAGES_KEPT} |
| 43 | fi |
| 44 | done |