~philroche/ubuntu-on-ec2/ec2-publishing-scripts-querydata-arch

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
#!/bin/bash
# vi: ts=4 noexpandtab

export LANG=C

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

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

   clean obsolete public data from query directory
     -d | --info-dir  dir       publish info to dir
                                [default \${PUBLISH_BASE}/query]
          --dry-run             only report what would be done
EOF
}

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

short_opts="d:h"
long_opts="help,info-dir:,dry-run"
getopt_out=$(getopt --name "${0##*/}" \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

info_dir=""
[ -n "${PUBLISH_BASE}" ] && info_dir="${PUBLISH_BASE}/query"
dry_run=0

oargs=( "${0##*/}" "${@}" )
while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		-d|--info-dir) info_dir=${next}; shift;;
           --dry-run) dry_run=1;;
		-h|--help) Usage; exit 0;;
		--) shift; break;;
	esac
	shift;
done

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

[ "${info_dir}" = "/query" ] && bad_Usage "must set info dir or PUBLISH_BASE"
[ -n "${PUBLISH_BASE}" ] || fail "PUBLISH_BASE must be set"

[ -d "${info_dir}" ] || fail "${info_dir} is not a directory"
[ -d "${PUBLISH_BASE}" ] || fail "${PUBLISH_BASE} is not a directory"

# now remove entries in -dl*.txt who do not have existing files
# there is no reason to keep around data of obsolete urls
bad_missing=""
modified=( )
for ufile in "${info_dir}"/*/*/*-dl*.txt; do
	[ -f "${ufile}" ] || continue
	sargs=( )
	missing=""
	while read csuite cbname clabel cserial carch cpath other; do
		[ -f "${PUBLISH_BASE}/${cpath}" ] && continue

		# make sure images weren't moved to S3
        rpath=${cpath//server\//}
        ( curl -s -I http://cloud-images-archive.ubuntu.com/${rpath} | egrep "HTTP.*200 OK|HTTP.*302 Moved Temporarily" ) &&
			continue

		# otherwise images are local
		dstr="\|${csuite}\t${cbname}\t${clabel}\t${cserial}"
		dstr="${dstr}\t${carch}\t${cpath}\t|d"
		sargs[${#sargs[@]}]="-e"
		sargs[${#sargs[@]}]="${dstr}"

		missing="${missing:+${missing} }${cpath}"

		# non-daily builds should not disappear
		[ "${clabel}" = "daily" ] || {
			error "FAIL: non-daily build missing from ${ufile}: ${cpath}";
			bad_missing="${bad_missing:+${bad_missing} }${cpath}";
		}
		case "${ufile}" in
			*-dl.current.txt)
				error "FAIL: current build missing from ${ufile}: ${cpath}";
				bad_missing="${bad_missing:+${bad_missing} }${cpath}";
				;;
		esac
	done < "${ufile}"

	if [ -n "${missing}" -a -z "${bad_missing}" ]; then
		error "${ufile}: clean ${missing}"
		modified[${#modified[@]}]="${ufile#${info_dir}/}"
		if [ $dry_run -eq 0 ]; then
			sed -i "${ufile}" "${sargs[@]}" ||
				fail "sed failed updating ${ufile}:" "${sargs[@]}"
		fi
	fi
done

if [ -n "${bad_missing}" ]; then
	fail "unexpected missing files (released or current): ${bad_missing}";
fi

[ ${#modified[@]} -eq 0 ] && { error "nothing to do"; exit 0; }

[ $dry_run -eq 0 ] || exit 0

cd "${info_dir}" || fail "failed to cd ${info_dir}"
bzr add "${modified[@]}"
bzr commit -m "${oargs[*]}" "${modified[@]}" ||
		fail "failed to bzr commit in ${info_dir}"

exit 0