summaryrefslogtreecommitdiff
path: root/scripts/clean-artifact-cache
blob: 495f3fa9e2dccd2e1364179a8db7356a17baaf5d (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh

# Copyright (C) 2012  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Remove all chunk artifacts in the cache except the newest. Morph does
# not currently clean its caches at any point, so this script is necessary
# to avoid running out of disk space.

set -e

usage() {
	echo "Usage: clean-artifact-cache [--all] [CHUNK_NAME]"
	echo
	echo "WARNING: this script removes all but the chunks with the latest"
	echo "mtimes. This is usually what you want, but you should try the"
	echo "script on a small chunk first and trigger a rebuild to make sure"
	echo "that you are not removing artifacts that you still want."
}

if [ -z $1 ]; then
	usage
	exit 0
fi

CHUNK=
case $1 in
	--all)
		CHUNK=*
		;;
	-*)
		usage
		exit 0
		;;
	*)
		CHUNK=$1
esac


clean_chunk() {
	ARTIFACT_COUNT=$(ls *.chunk.$1 | wc -l)

	if [ $ARTIFACT_COUNT -lt 2 ]; then
		return
	fi

	echo "$1: $(expr $ARTIFACT_COUNT - 1) stale artifact(s)"

	SKIPPED_LATEST=
	for f in $(ls -1t *.chunk.$1); do
		if [ -z "$SKIPPED_LATEST" ]; then
			SKIPPED_LATEST=yes
		else
			rm $(echo $f | cut -c -64).build-log
			rm $(echo $f | cut -c -64).meta
			rm $(echo $f | cut -c -64).chunk.$1
		fi
	done
}

test "x$MORPH" = "x" && MORPH=morph

CACHE_DIR=$($MORPH --dump-config | grep cachedir | awk '{print $3}')
ARTIFACT_CACHE="${CACHE_DIR}/artifacts"

cd $ARTIFACT_CACHE
SIZE_BEFORE=$(du -sh . | cut -f 1)

if [ "$CHUNK" = "*" ]; then
	echo "Removing ALL out-of-date chunk artifacts in $ARTIFACT_CACHE"

	for chunk in $(ls *.chunk.* | cut -d '.' -f 3-); do
		clean_chunk $chunk
	done
else
	echo "Removing out of date artifacts for chunk $CHUNK in $ARTIFACT_CACHE"
	clean_chunk $CHUNK
fi

SIZE_AFTER=$(du -sh . | cut -f 1)

echo "Artifact cache size before: $SIZE_BEFORE   after: $SIZE_AFTER"