~josvaz/ubuntu-on-ec2/ec2-publishing-scripts-lp1602466-xen-fbfront-blacklist

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# vi: ts=4 noexpandtab

# the idea is to read a "config" file, similar to below, and call clean-region-obsolete for each line.
# the config would live in automated-ec2-builds.
# clean-region-obsolete nu
#  5 30 us-east-1 ^ubuntu-images-testing-us/ubuntu-karmic-daily-amd64
#  5 30 us-east-1 ^ubuntu-images-testing-us/ubuntu-karmic-daily-i386
#  5 30 eu-west-1 ^ubuntu-images-testing-eu/ubuntu-karmic-daily-amd64
#  5 30 eu-west-1 ^ubuntu-images-testing-eu/ubuntu-karmic-daily-i386
#  5 30 us-east-1 ^ubuntu-images-testing-us/ubuntu-lucid-daily-amd64
#  5 30 us-east-1 ^ubuntu-images-testing-us/ubuntu-lucid-daily-i386
#  5 30 eu-west-1 ^ubuntu-images-testing-us/ubuntu-lucid-daily-amd64
#  5 30 eu-west-1 ^ubuntu-images-testing-us/ubuntu-lucid-daily-i386

ALL_REGIONS=""
for x in $(ec2-list-all-regions); do
	ALL_REGIONS="${ALL_REGIONS},${x}"
done
ALL_REGIONS=${ALL_REGIONS#,}
PUBLISH_RESULTS_DIR=${PUBLISH_RESULTS_DIR:-$HOME/ec2-publish}
OUTPUT="${PUBLISH_RESULTS_DIR}/log-unpublish.txt"
PUBLISH_OUTPUT=""
PUBLISH_ERROR=""
TMPD=""

error() { echo "$@" 1>&2; }
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
fail() { 
	[ $# -eq 0 ] || error "$@"; 
	local x
	for x in "${PUBLISH_OUTPUT}" "${PUBLISH_ERROR}"; do
		[ -n "${x}" -a -f "${x}" ] || continue
		echo "===== ${x##*/} ====="
		cat "${x}"
	done
	exit 1; 
}

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

Usage() {
	cat <<EOF
Usage: ${0##*/} config
   unpublish (privatize and deregister) according to config

   options:
      -n | --dry-run      pass dry-run to clean-region-obsolete
      -h | --help         print this message
           --no-redirect  do not redirect output to results dir

   Calls clean-region-obsolete with inforation in 'config'.  Where
   config is like the following:

   # number-published keep-private-for-days region regex-for-manifest
   5 60 us-east-1 ^ubuntu-images-testing-us/ubuntu-karmic-daily-amd64

   The above example will keep 5 public builds, and keep builds private
   for 60 days before deletion.  For manifests that match the given
   regex

   if 'region' is "all", then loop over ${ALL_REGIONS}

   regex will have the following substituions done:
   - %{a} : arch (i386 or x86_64)
   - %{r} : region (${ALL_REGIONS})
   - %{l} : location (region, except for us-east-1:us, eu-west-1:eu)
EOF
}

run() {
	local hash="#" line="" nline="" keep_public="" remove_unpub=""
	local region="" regex="" regex_in=""

	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"}

	while read line; do
		nline=${line%%${hash}*}
		set -- ${nline}
		[ -n "$1" ] || continue
		[ $# -eq 4 ] || { error "bad line ${line}"; return 1; }
		keep_public=$1; remove_unpub=$2; region=$3; regex_in=$4;

		[ "${region}" = "all" ] && region=${ALL_REGIONS}
		OIFS=${IFS}; IFS=","; set -- ${region}; IFS=${OIFS}
		echo "#  ${line}"
		local x arch done_regex
		for region in "${@}"; do
			done_regex=( );
			xc2 ximages seed-cache $region ||
				fail "failed to get list of images in ${region}"
			case "${region}" in
				us-east-1) location="us";;
				eu-west-1) location="eu";;
				*) location="${region}";;
			esac
			for arch in i386 amd64; do
				regex=${regex_in}
				regex=${regex//%{r\}/${region}}
				regex=${regex//%{l\}/${location}}
				regex=${regex//%{a\}/${arch}}

				# only do each regex once, it may not have included
				# any of the tokens above
				for x in "${done_regex[@]}"; do
					[ "$x" = "${regex}" ] && continue 2
				done
				[ "$regex" = "${regex_in}" ] ||
					printf "## %s %s %s %s\n" "${keep_public}" \
						"${remove_unpub}" "${region}" "${regex}"

				clean-region-obsolete --verbose "${pt_args[@]}" \
					--keep-public "${keep_public}" \
					--remove-unpub-age "${remove_unpub}" \
						"${region}" "${regex}" ||
					{ error "failed: ${line}"; return 1; }

				done_regex[${#done_regex[@]}]="${regex}"
			done
		done
	done

	return 0
}

short_opts="hn"
long_opts="help,dry-run,no-redirect"

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

pt_args=( )
redirect=1;

while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		--) shift; break;;
		--no-redirect) redirect=0;;
		-n|--dry-run) pt_args[${#pt_args[@]}]=${cur};;
		-h|--help) Usage; exit 0;;
	esac
	shift;
done

[ $# -eq 1 ] || bad_Usage

config=${1}

[ -f "${config}" -a -r "${config}" ] || fail "${config}: not a readable file"

if [ ${redirect} -eq 1 ]; then
	resdir=${PUBLISH_RESULTS_DIR}
	{ [ -d "${resdir}" ] || mkdir -p "${resdir}"; } ||
		fail "failed to make ${resdir}"

	PUBLISH_OUTPUT="${resdir}/unpub.$$.out"
	PUBLISH_ERROR="${resdir}/unpub.$$.err"

	run < "${config}" > "${PUBLISH_OUTPUT}" 2> "${PUBLISH_ERROR}" || fail
else
	run < "${config}" || fail
fi