~patviafore/ubuntu-on-ec2/ec2-publishing-scripts-hirsute

84 by Scott Moser
add publicize-build, generate-query-tree for publishing 'query' data
1
#!/bin/bash
393 by Scott Moser
generate-query-tree: update for current publish format
2
OWNER_ID="099720109477"
3
export LC_ALL=C
4
5
Usage() {
6
	cat <<EOF
7
Usage: ${0##*/} [outdir]
8
9
  outdir defaults to ./outd
10
11
  update a /query tree based data in ec2
12
  Note, this would be lossy for past images (dailies that have fallen off)
13
14
  this  makes an attempt to save old data. you could populate the
15
  output dir with:
16
      rsync cloud-images.ubuntu.com::uec-images/query/ outd -az --stats
17
18
EOF
19
}
20
21
error() { echo "$@" 1>&2; }
22
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
23
cleanup() {
24
	[ -z "${TMPD}" -o ! -d "${TMPD}" ] || rm -Rf "${TMPD}"
25
}
26
27
ec2data_sort() {
28
	# ec2 files are sorted by 
29
	#  serial (4), region (7), virtualization_type (11), 
30
	#  then store (5) and arch (6)
31
	local tab=$'\t'
32
	sort --uniq "--field-separator=${tab}" -k 4,4 -k 7,7 -k 11,11 -k 5,6
33
}
84 by Scott Moser
add publicize-build, generate-query-tree for publishing 'query' data
34
35
printdata() {
393 by Scott Moser
generate-query-tree: update for current publish format
36
	local bdir=${1} region=$2
37
	local loc=$region oifs=$IFS
38
	[ "$region" = "us-east-1" ] && loc="us" ||
39
		{ [ "$region" = "eu-west-1" ] && loc="eu"; }
40
41
	local bp="ubuntu[-/]images"
42
	local mdif="" manif iarch itype ami aki ari vt
43
	while IFS="|" read manif iarch itype ami aki ari vt; do
44
		mdir="${manif#${OWNER_ID}/}"
45
		case "$mdir" in
46
			ubuntu/*) mdir=${mdir#ubuntu/}; mdir="ubuntu/${mdir%%/*}";;
47
			*) mdir=${mdir%%/*};;
48
		esac
49
		mbname=${manif##*/}
50
		mbname=${mbname%.manifest.xml}
51
		case "${mdir}" in
537 by Ben Howard
Fix for query data excluding hvm instance store
52
			${bp}-testing-$loc|${bp}-testing|${bp}-testing-dev) rline=testing;;
393 by Scott Moser
generate-query-tree: update for current publish format
53
			${bp}-sandbox-$loc|${bp}-sandbox) continue;;
54
			${bp}-$loc|${bp}-milestone-$loc|$bp|$bp-milestone) rline=release;;
55
			*) fail "unknown manifest/name: $manif ($mdir)";
56
		esac
57
		[ "$iarch" = "x86_64" ] && arch=amd64 || arch=i386
58
		IFS="-"; set -- ${mbname}; IFS=${oifs}
59
		suite=${2}
60
61
		local label=""
62
		# spin (build name) is always the second to last token
63
		if [ $# -eq 7 ]; then
64
			serial=$7
65
 			spin=$6
66
			label=$4
67
		elif [ $# -eq 6 ]; then
68
			serial=$6
69
			spin=$5
70
			case "$3" in
71
				[0-9]*) label="release";;
72
				*) label=$3;;
73
			esac
74
		else
75
			fail "confused by name ${mbname}"
76
		fi
77
78
		error "label=$label spin=$spin mbname=$mbname"
79
80
		dir="${bdir}/${suite}/${spin}"
81
		print=( printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n"
82
			"$suite" "$spin" "$label" "$serial"
83
			"$itype" "$arch" "$region" "$ami" "$aki" "$ari" "$vt" )
84
		[ -d "${dir}" ] || mkdir -p "${dir}" || return 1
85
		if [ "${label}" = "daily" ]; then
86
			"${print[@]}" >> "${dir}/daily.txt"
87
		else
88
			"${print[@]}" >> "${dir}/released.txt"
89
		fi
90
		[ $? -eq 0 ] || return 1
91
	done
84 by Scott Moser
add publicize-build, generate-query-tree for publishing 'query' data
92
}
393 by Scott Moser
generate-query-tree: update for current publish format
93
94
TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
95
trap cleanup EXIT
96
export XC2_XIMAGES_CACHE_D=${XC2_XIMAGES_CACHE_D:-"$TEMP_D/ximgcache"}
97
98
[ $# -eq 0 ] && { Usage 1>&2; exit 1; }
99
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
100
101
outd="$1"
102
[ -d "$outd" ] || mkdir -p "$outd"
103
104
regions=( $(ec2-list-all-regions) ) || fail "failed to get list of regions"
105
106
# save off the originals
107
for f in ${outd}/*/*/*; do
108
	[ -f "$f" ] || continue
109
	case "$f" in
110
		*/released.txt|*/daily.txt) :;;
111
		*) continue;;
112
	esac
113
	cp "$f" "$f.orig" || fail "failed to save $f to $f.orig"
114
done
115
116
name_match="^(${OWNER_ID}/|^)ubuntu[/-]images"
117
for r in ${regions[@]}; do
118
	f="${TEMP_D}/images.$r.txt"
119
	error "updating image cache for $r"
120
	xc2 ximages describe-images --region "${r}" --all  > "${f}" ||
121
		fail "failed to describe-images in region $r"
122
	ufile="$TEMP_D/ubuntu.images.$r.txt"
123
	awk '-F\t' '$1 == "IMAGE" && $3 ~ ubum && $6 == "public" {
124
		print($3,$8,$13,$2,$10,$11,$14); }' \
125
		"ubum=$name_match" OFS="|" "$f" > "$ufile" ||
126
		fail "failed to get ubuntu images from $ufile"
127
		printdata "${outd}" "$r" < "$ufile" ||
128
		fail "failed to print ubuntu images in $r"
129
done
130
131
# now update the .current files from the released.txt
132
for f in ${outd}/*/*/*; do
133
	case "$f" in
134
		*/released.txt|*/daily.txt) :;;
135
		*) continue;;
136
	esac
137
138
	# append to the file any original data.
139
	if [ -f "$f.orig" ]; then
140
		 cat "$f.orig" >> "$f" && rm "$f.orig" ||
141
			fail "failed to update $f with orig data"
142
	fi
143
144
	# sort results
145
	ec2data_sort < "$f" > "$f.new"
146
	mv "${f}.new" "${f}"
147
	error "wrote to $f"
148
	cur="${f%.txt}.current.txt"
149
150
	# get the bottom entry, and populate current with stuff like it
151
	tail -n 1 "$f" > "$TEMP_D/latest" &&
152
		read suite bname label serial other < "$TEMP_D/latest" ||
153
		fail "failed to read final entry in $f"
154
155
	cond='$1 == suite && $2 == bname && $3 == label && $4 == serial'
156
	awk '-F\t' "$cond { print \$0 }" \
157
		"suite=$suite" "bname=$bname" "label=$label" "serial=$serial" \
158
		"$f" > "$cur" ||
159
		fail "failed to update current $cur"
160
	error "wrote to $cur"
161
done
162
163
# this goes through each of the suite/buildname/daily.current.txt 
164
# files and then writes the first 4 fields of each of those files into
165
# the current.txt file at the top level.
87 by Scott Moser
separate daily from released (remove 'all'), add 'latest' file
166
for r in daily released; do
393 by Scott Moser
generate-query-tree: update for current publish format
167
	tail -qn 1 */*/"$r.current.txt" > "$TEMP_D/$r.current" &&
168
		awk '-F\t' '{print $1,$2,$3,$4}' 'OFS=\t' \
169
			"$TEMP_D/$r.current" > "$outd/$r.latest.txt" ||
170
		fail "failed to write to $outd/$r.latest.txt"
171
	error "updated $r.latest.txt"
87 by Scott Moser
separate daily from released (remove 'all'), add 'latest' file
172
done
393 by Scott Moser
generate-query-tree: update for current publish format
173
174
# vi: ts=4 noexpandtab