~smoser/ubuntu-on-ec2/ec2-publishing-scripts.query-dl-arm-data

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

S3_BASE="ubuntu-data/ec2-currency"
S3_BASE="smoser-test/ec2-currency"
S3_BASE_URL="http://ubuntu-data.s3.amazonaws.com/ec2-currency"
S3_BASE_URL="http://smoser-test.s3.amazonaws.com/ec2-currency"
RELEASE_TYPES=( server-testing server-release )

TMPD=""
VERBOSITY=0
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 ] release-type suite region image-id

   add 'image-id' to currency information at ${S3_BASE_URL}

   -f | --follow            if image-id is a machine image, also add
                            associated kernel and ramdisk
   -n | --dry-run           only report what would be done
   -c | --current           this id is 'current', update current

   release-type is one of:
      ${RELEASE_TYPES[*]}

   data is published to
     ${S3_BASE_URL}/<release-type>/all/img_type

   and (if '--current' is specified)
     ${S3_BASE_URL}/<release-type>/current/<suite>/<region>/<arch>/img_type

   where 'img_type' is of: ami,aki,ari

   Example:
   ${0##*/} server-release karmic us-east-1 ami-0532d16c
EOF
}

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

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

update_currency() {
	local oifs=${IFS};
	local rtype="" suite="" region="" id="" img_info="" x="" lfile=""
	local id_info="" manif="" acl="" itype="" kernel="" ramdisk="" arch=""
	local dry_run=0 follow=0 s3all="" s3cur="" current=0 followed=""
	local short_opts="cfhnx"
	local long_opts="cache:,current,dry-run,follow,following:,help"
	local getopt_out=$(getopt --name "${0##*/}" \
		--options "${short_opts}" --long "${long_opts}" -- "$@") &&
		eval set -- "${getopt_out}" ||
		{ bad_Usage; return 1; }

	local pt; pt=( );
	local put_args; put_args=( "--acl-public" "--mime-type" "text/plain" )

	while [ $# -ne 0 ]; do
		cur=${1}; next=${2};
		case "$cur" in
			--) shift; break;;
			-f|--follow) follow=1; pt[${#pt[@]}]=${cur};;
			   --following) followed=${next}; shift;;
			-h|--help) Usage; return 0;;
			-n|--dry-run) dry_run=1; pt[${#pt[@]}]=${cur};;
			-c|--current) current=1; pt[${#pt[@]}]=${cur};;
			   --cache)
				img_info=${next};
				pt[${#pt[@]}]="--cache=${next}";
				shift;;
		esac
		shift;
	done
	
	rtype=${1}
	suite=${2}
	region=${3}
	id=${4}

	[ $# -gt 4 ] && { bad_Usage "to many arguments given"; return 1; }
	[ $# -lt 4 ] && {
		bad_Usage "must provide release-type, suite, region, image-id";
		return 1;
	}

	[ -z "${img_info}" -o -f "${img_info}" ] ||
		{ error "${img_info}: not a file"; return 1; }

	if [ -z "${TMPD}" ]; then
		TMPD=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX) ||
			{ error "failed to make temp dir"; return 1; }
		trap cleanup EXIT
	fi

	if [ -z "${img_info}" ]; then
		img_info=${TMPD}/img_info.txt;
		xc2 describe-images --all --region "${region}" > "${img_info}" ||
			{ error "failed to query images with describe-images"; return 1; }
		pt[${#pt[@]}]="--cache=${img_info}"
	fi

	# get manifest, public/private, type, kernel,ramdisk
	id_info=$(awk '-F\t' '$2 == id { print $3,$6,$8,$9,$10,$11 }' \
	          "id=${id}" "OFS=|" "${img_info}" ) &&
		{ IFS="|"; set -- ${id_info}; IFS=${oifs}; } ||
		fail "failed to parse images info"

	manif=${1}; acl=${2}; arch=${3}; itype=${4}; kernel=${5}; ramdisk=${6};

	# if follow and 'machine' type then follow
	if [ "${itype}" = "machine" -a ${follow} -ne 0 ]; then
		for x in "${kernel}" "${ramdisk}"; do
			[ -n "${x}" ] || continue;
			update_currency "${pt[@]}" --following=${id} \
				"${rtype}" "${suite}" "${region}" "${x}" || return 1;
		done
	fi

	case "${itype}" in
		machine)   file="ami";;
		kernel)  file="aki";;
		ramdisk) file="ari";;
		*) error "bad type ${itype}"; return 1;;
	esac
	s3cur="${S3_BASE}/${rtype}/current/${suite}/${region}/${arch}"
	s3all="${S3_BASE}/${rtype}/all"

	if [ $current -eq 1 ]; then
		local old_curr=""
		mkdir -p "${TMPD}/current" && 
			lfile="${TMPD}/current/${file}" &&
			{ [ ! -e "${lfile}" ] || rm -f "${lfile}"; } ||
			fail "failed to make 'current' under tmpdir";
		# note below we ignore s3cmd error, assuming the file just
		# doesn't exist
		x=$(xc2 s3cmd get "s3://${s3cur}/${file}" "${lfile}" 2>&1) ||
			: > "${lfile}"
		read old_curr < "${lfile}"
		if [ "${id}" = "${old_cur}" ]; then
			echo "${id}: already currentin ${s3cur}/${file}";
		elif [ ${dry_run} -eq 0 ]; then
			echo "${id}" > "${lfile}" &&
				x=$(xc2 s3cmd put "${put_args[@]}" "${lfile}" \
				    "s3://${s3cur}/" 2>&1) || {
					errorp "%s\n\t%s\n" \
						"failed to mark ${id} as current in ${s3cur}" "$x"
					return 1;
				}
			echo "${id}: made current in ${s3cur}/${file}";
		else
			echo "${id}: make current in ${s3cur}/${file}";
		fi
	fi

	mkdir -p "${TMPD}/all" && 
		lfile="${TMPD}/all/${file}" &&
		{ [ ! -e "${lfile}" ] || rm -f "${lfile}"; } ||
		fail "failed to make 'all' under tmpdir";
	# note below we ignore s3cmd error, assuming the file just
	# doesn't exist
	x=$(xc2 s3cmd get "s3://${s3all}/${file}" "${lfile}" 2>&1) ||
		: > "${lfile}"

	x=$(awk '$4 = id { print $4 }' "id=${id}" "${lfile}" ) ||
		{ error "failed to read existing ${s3all}/${file}"; return 1; }
	
	if [ -n "${x}" ]; then
		echo "${id}: already present in ${s3all}/${file}";
	elif [ ${dry_run} -eq 0 ]; then
		printf "%s\t%s\t%s\t%s\n" \
			"${suite}" "${region}" "${arch}" "${id}" >> "${lfile}" &&
			x=$(xc2 s3cmd put "${put_args[@]}" "${lfile}" \
			    "s3://${s3all}/" 2>&1) || {
				errorp "%s\n\t%s\n" \
					"failed to add ${id} to ${s3all}/${file}" "$x"
				return 1;
			}
		echo "${id}: added to ${s3all}/${file}";
	else
		echo "${id}: add to ${s3all}/${file}";
	fi

	return 0
}

update_currency "${@}"