~philroche/ubuntu-on-ec2/ec2-publishing-scripts-kernel-panic-revert

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
# vi: ts=4 noexpandtab

REGIONS=( )
TMPD=""
OWNER_ID="099720109477"
error() { echo "$@" 1>&2; }
errorp() { printf "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
failp() { [ $# -eq 0 ] || errorp "$@"; exit 1; }
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
cleanup() {
	[ -n "${TMPD}" -a -d "${TMPD}" ] && rm -Rf "${TMPD}"
	return 0
}

prepare_for_sort() {
	# example basenames:
	# ubuntu-karmic-i386-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-kernel.img
	# ubuntu-karmic-i386-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-ramdisk.20091020.img
	# ubuntu-karmic-x86_64-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-ramdisk.20091020.1.img
	# ubuntu-karmic-x86_64-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-kernel.img 
	
	# read 2 inputs id manifest
	# write 4 outputs:
	#   string up to "-kernel" or "-ramdisk"
    #   string after , or '0' if empty
	#   id, manifest
	# this should be sortable on key1, key2

	local bname id man tmp ser base suf=".img.manifest.xml"
	while read id man; do
		bname=${man##*/}
		case "${bname}" in
			*-kernel${suf}|*-ramdisk${suf}) ser=0; base=${bname%-*${suf}};;
			*-kernel*${suf})
				tmp=${bname%${suf}}
				base=${tmp%-kernel*}
				ser=${tmp##*-kernel.};;
			*-ramdisk.*.img.manifest.xml)
				tmp=${bname%${suf}}
				base=${tmp%-ramdisk*}
				ser=${tmp##*-ramdisk.};;
            *) ser=0; base=${bname};;
		esac
		printf "%s\t%s\t%s\t%s\t\n" \
			"${base}" "${ser}" "${id}" "${man}"
	done
}

krd_sort() {
	local x
	prepare_for_sort | LANG=C sort --reverse |
		awk -F '\t' '{ printf("%s\t%s\t\n",$3,$4); }'
	for x in "${PIPESTATUS[@]}"; do [ "$x" = "0" ] || return 1; done
	return 0
}

checkstatus() {
	local x="" i=0
	for x in "$@"; do
		[ "$x" = "0" ] || i=$(($i+1))
	done
	return $i
}

get_regions() {
	local regs=""
	[ "${#REGIONS[@]}" -eq 0 ] || return 0
	REGIONS=( $(ec2-list-all-regions) ) || return 1
}

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

   find kernels and ramdisks on ec2 for suite release-type
   output format is the same as 'publish-image' format, and should
   be sorted within each region in "newest first" as:
     kernel-1
     matching-ramdisk-b
     matching-ramdisk-a
     kernel-2
     matching-ramdisk-b
     matching-ramdisk-a

   if arch given, only do so for 'arch' (default: ${ARCHES[*]})

   options:
      -l | --latest           only print the "latest"
      -m | --match m          restrict kernels to those matching 'm'
      -r | --regions r        limit search to 'r' (comma or space delimited)
                              default: all (per describe-regions)
EOF
}

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

latest=0
match="";
regions=( )
arches=( i386 amd64 )

while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		--) shift; break;;
		-l|--latest) latest=1;;
		-m|--match) match=${next}; shift;;
		-r|--regions) regions=( ${next//,/ } ); shift;;
	esac
	shift;
done

[ $# -lt 1 ] && bad_Usage "must provide suite"
suite=${1}
shift;

[ $# -eq 0 ] || arches=( "${@}" );

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

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

if [ ${#regions[@]} -eq 0 ]; then
	get_regions || fail "failed to get region list"
	regions=( "${REGIONS[@]}" )
fi

for region in "${regions[@]}"; do
	imglist="${TMPD}/images.${region}.txt"
	xc2 ximages describe-images --region "${region}" --all > "${imglist}" ||
		fail "failed to get images on ${region}"

	for arch in "${arches[@]}"; do
		action='printf("%s\t%s\n",$2,$3);'
		criteria='$9 == type && $8 == xarch && $3 ~ regex'

		klist="${TMPD}/kernels-${arch}.txt"
		rlist="${TMPD}/ramdisks-${arch}.txt"
		sklist="${klist}.sort"
		srlist="${rlist}.sort"

		[ "${arch}" = "x86_64" ] && arch=amd64;
		xarch=${arch}; [ "${xarch}" = "amd64" ] && xarch=x86_64;

		# account for region replacement in match input
		if [ -n "$match" ]; then
			# replace '%R' in match with region
			rmatch="${match//%R/$region}"
			kmatch="^${OWNER_ID}/${rmatch}$"
		else
			kmatch="^${OWNER_ID}/"
		fi
		awk '-F\t' "${criteria} { ${action} }" \
			"regex=${kmatch}" "type=kernel" "xarch=${xarch}" \
			"${imglist}" > "${klist}" ||
			fail "failed to search image results for ${kmatch}"

		[ -s "${klist}" ] || { 
			errorp "no matching kernels in %s for suite %s arch %s\n\t%s\n" \
				"${region}" "${suite}" "${arch}" "regex:${kmatch}";
				continue; 
			}

		krd_sort < "${klist}" > "${sklist}" || fail "failed to sort list"

		while read k_ami k_manif; do

			rmatch=${k_manif%-kernel*} # find ramdisks that match this kernel

			awk '-F\t' "${criteria} { ${action} }" \
				"regex=${rmatch}" "type=ramdisk" "xarch=${xarch}" \
				"${imglist}" > "${rlist}" ||
				fail "failed to search image results for ${rmatch}"

			krd_sort < "${rlist}" > "${srlist}" ||
				fail "failed to sort ramdisk list"

			printf "%s\t%s\t%s\t%s\t%s\n" "${region}" "${k_ami}" \
				"${arch}" kernel "${k_manif}"

			# if 'latest', then we only print 1 ramdisk, and 1 kernel
			if [ $latest -eq 1 ]; then
				read ami manif < "${srlist}"
				[ -z "${ami}" ] ||
					printf "%s\t%s\t%s\t%s\t%s\n" "${region}" "${ami}" \
						"${arch}" "ramdisk" "${manif}"
				break;
			else
				# loop over ramdisk results
				while read ami manif; do
					[ -z "${ami}" ] ||
						printf "%s\t%s\t%s\t%s\t%s\n" "${region}" "${ami}" \
							"${arch}" "ramdisk" "${manif}"
				done < "${srlist}"
			fi

		done < "${sklist}"
	done
done

exit 0