~philroche/ubuntu-on-ec2/ec2-publishing-scripts-ena

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# vi: ts=4 noexpandtab

TMPD=""
VERBOSITY=0

# ubuntu-images-testing/ubuntu-maverick-daily-i386-server-20100830
# files are named like ubuntu-data/ebs/<name>.tar.gz
TAR_BUCKET="ubuntu-data/images-testing"

error() { echo "$@" 1>&2; }
errorp() { printf "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
failp() { [ $# -eq 0 ] || errorp "$@"; exit 1; }
debugp() {
	[ "${1}" -gt "${VERBOSITY}" ] && return
	shift;
	errorp "${@}"
}

Usage() {
	cat <<EOF
Usage: ${0##*/} [ options ]

   This utility just removes any unused tarballs.
   goes through through ${TAR_BUCKET}

   -n | --dry-run              only report what would be done
   -v | --verbose              be more verbose

   Example:
    ${0##*/} --dry-run
EOF
}

bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }

cleanup() {
	[ -n "${TMPD}" -a -d "${TMPD}" ] && rm -Rf "${TMPD}"
	return 0
}

short_opts="hnv"
long_opts="dry-run,help,verbose"

getopt_out=$(getopt --name "${0##*/}" \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

dry_run=0;
VERBOSITY=0

while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		--) shift; break;;
		-h|--help) Usage; exit 0;;
		-n|--dry-run) dry_run=1; VERBOSITY=$(($VERBOSITY+1));;
		-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
	esac
	shift;
done

[ $# -eq 0 ] || bad_Usage "unexpected arguments: $*"

TMPD=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX) ||
	fail "failed to make tempdir"
trap cleanup EXIT

export XC2_XIMAGES_CACHE_D=${XC2_XIMAGES_CACHE_D:-"$TMPD/ximgcache"}

ffmt="%-7s: %s\n"
# get a list of all our in each region
regions=$(ec2-list-all-regions)
debugp 1 "${ffmt}" "info" "getting list of images in $(echo $regions)"
for region in $regions; do
	xc2 ximages describe-images -o self --region $region > "$TMPD/images.$region" ||
		fail "failed to get images in $region"
done

# get a list of the bucket
debugp 1 "${ffmt}" "info" "getting bucket list for ${TAR_BUCKET}"
xc2 s3cmd ls s3://${TAR_BUCKET}/ > "${TMPD}/tarballs.txt" ||
	fail "failed to list images in ${TAR_BUCKET}"

count_keep=0
count_del=0
count_ignore=0
size_del=0
size_keep=0
size_ignore=0
while read ymd ms size s3path other; do
	tarname=${s3path##*/};
	case ${tarname} in
		*.img.tar.gz) imgname=${tarname%.img.tar.gz};;
		*.tar.gz) imgname=${tarname%.tar.gz};;
		*) debugp 1 "${ffmt}" "ignore" "${tarname}"
			count_ignore=$((${count_ignore}+1));
			size_ignore=$((${size_ignore}+${size}));
			continue;;
	esac
	grep -q "${imgname}" "${TMPD}/images."* && {
		debugp 1 "${ffmt}" "keep" "${imgname}"
		count_keep=$((${count_keep}+1))
		size_keep=$((${size_keep}+${size}))
		continue
	}
	debugp 1 "${ffmt}" "remove" "${imgname}"
	count_del=$((${count_del}+1))
	size_del=$((${size_del}+size));
	if [ ${dry_run} -eq 0 ]; then
    	xc2 s3cmd del "${s3path}" ||
			fail "failed to remove ${imgname} (${s3path})"
	fi
done < "${TMPD}/tarballs.txt"

m_del=$((size_del/1048576))M;
m_keep=$((size_keep/1048576))M;
m_ignore=$((size_ignore/1048576))M;

msg="${count_del} (${m_del}) removed, "
msg="${msg}${count_keep} (${m_keep}) kept, "
msg="${msg}${count_ignore} (${m_ignore}) ignored"
errorp "${ffmt}" "summary" "${msg}"