Catch nodes flapping issue
[fuel.git] / ci / clean_cache.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2016 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 # Clean the build cache according to its expiry date. Invoke with the cache
12 # directory as the first argument.
13
14 if [ -z "$1" ]; then
15   echo "No cache directory specified, exiting..."
16   exit 1
17 else
18   CACHEDIR=$1
19   echo "Operating on cache $CACHEDIR"
20 fi
21
22 NOW=$(date '+%s')
23
24 cd $CACHEDIR
25 echo "Step 1, cleaning orphaned meta and blob files"
26 ls *.meta *.blob | sed 's/\..*//' | sort | uniq -u | xargs -n 1 -I {} sh -c "rm -vf {}.*"
27 echo "Step 2, cleaning expired files"
28 for cache in $(ls -1 *.meta | sed 's/\..*//')
29 do
30   blob=${cache}.blob
31   meta=${cache}.meta
32   expiry=$(grep Expires: $meta | sed 's/Expires: *//')
33   if [ $expiry -le $NOW ]; then
34      echo "$cache expired $(date -d "@$expiry"), removing..."
35      rm -f $blob $meta
36   fi
37 done
38