~philroche/ubuntu-on-ec2/ec2-publishing-scripts-cim-public

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

MS="milestone"
 LABELS=( sandbox daily   testing     beta beta[0-9] alpha alpha[0-9] rc  rc[0-9] release )
BUCKETS=( sandbox testing testing-dev $MS  $MS       $MS   $MS        $MS $MS     "" )

get_prefix() {
	local label=${1} itype=${2:-image}
	local i="" x=""
	case "${itype}" in
		kernels|vmlinuz|kernel|ramdisk|initrd) ftype="kernels";;
		image) ftype="images";;
		*) echo "invalid type ${itype}" 1>&2; return 1;;
	esac
	local path_prefix=${TEST_PUBLISH_PATH_PREFIX:-""}
	for((i=0;i<${#LABELS[@]};i++)); do
		case "${label}" in
			${LABELS[$i]})
				x=${BUCKETS[$i]};
				[ "$x" = "" ] || x=-${x}
				_RET="${path_prefix}${ftype}${x}"
				return 0
				;;
		esac
	done
	return 1
}

Usage() {
	cat <<EOF
Usage: ${0##*/} label image-type
   output the correct prefix (path within a bucket)
   for an image of type 'image-type' labeled 'label'.

   options:
     --show-labels:  dump valid labels to stdout and exit
     --show-mapping: dump table of label -> bucket for provided type
                     example: ${0##*/} --show-mapping kernel

   label is one of:
      ${LABELS[*]}
   image-type is one of:
      image, initrd, kernel, ramdisk, vmlinuz
EOF
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
	Usage; exit 0;
elif [ "$1" = "--show-labels" ]; then
	echo "${LABELS[*]}"; exit 0;
elif [ "$1" = "--show-mapping" ]; then
	shift;
	for x in "${LABELS[@]}"; do
		modx=${x}
		[ "${x#*[}" != "${x}" ] && modx=${x%%[*}1
		get_prefix "${modx}" ${1} || exit
		printf "%-12s %s\n" "${x}" "${_RET}"
	done
	exit 0
fi

[ $# -eq 0 ] && { Usage 1>&2; exit 1; }

get_prefix "${1}" ${2} && { echo "${_RET}"; exit 0; }
exit 1