forked from AOEpeople/magento-deployscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup.sh
executable file
·63 lines (51 loc) · 1.66 KB
/
cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# Get absolute path to main directory
ABSPATH=$(cd "${0%/*}" 2>/dev/null; echo "${PWD}/${0##*/}")
SOURCE_DIR=`dirname "${ABSPATH}"`
function usage {
echo "Usage:"
echo " $0 -r <releasesDir> [-n <numberOfBuildsToKeep>]"
echo " -r Releases dir"
echo " -n Number of old builds to keep (current, latest and previous NOT included)"
echo ""
exit $1
}
NUMBEROFBUILDSTOKEEP=2
while getopts 'r:n:' OPTION ; do
case "${OPTION}" in
r) RELEASES="${OPTARG}";;
n) NUMBEROFBUILDSTOKEEP="${OPTARG}";;
\?) echo; usage 1;;
esac
done
if [ ! -d "${RELEASES}" ] ; then echo "Could not find releases dir ${RELEASES}"; usage 1; fi
if [ ! -L "${RELEASES}/current" ] ; then echo "No 'current' symlink found. This does not seem to be a valid release root dir"; usage 1; fi
cd ${RELEASES} || exit 1
SYMLINKS=()
SYMLINKS+=($(readlink -f "${RELEASES}/current"))
SYMLINKS+=($(readlink -f "${RELEASES}/latest"))
SYMLINKS+=($(readlink -f "${RELEASES}/previous"))
files=(`ls | grep build_ | sort -k2 -t_ -n -r | head -${NUMBEROFBUILDSTOKEEP}`)
for i in build_*; do
I_PATH=$(readlink -f "$i")
preserve=0;
# Check if a symlink is pointing to this file:
for a in ${SYMLINKS[@]}; do
if [ "${I_PATH}" == "$a" ]; then
preserve=1;
fi;
done;
# Check whether this file is in files array:
for a in ${files[@]}; do
if [ "$i" == "$a" ]; then
preserve=1;
fi;
done;
# If it wasn't, delete it (or in this case, print filename)
if [ $preserve == 0 ]; then
echo "Deleting old deployment $i"
rm -rf $i
else
echo "Skipping $i"
fi;
done