~josvaz/ubuntu-on-ec2/ec2-publishing-scripts-lp1613470-udev-cdrom

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

TMPD=""

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

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

   Remove 'manifest' and associated part files from s3 bucket 'bucket'

   -n | --dry-run         perform a dry run
   -s | --safe            check ec2 for registered image before deleting
   -v | --verbose         increase verbosity

   *** Note *** 
   You likely do not want this tool, at least not without '--safe'
   Please use remove-image, or remove-build which are safer
EOF
}

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

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

short_opts="nsv"
long_opts="dry-run,message-label:,safe,unpub-path:,verbose"
getopt_out=$(getopt --name "${0##*/}" \
	--options "${short_opts}" --long "${long_opts}" -- "$@") &&
	eval set -- "${getopt_out}" ||
	bad_Usage

VERBOSITY=0
dry_run=0
safe=0
unpub=""
label=""

while [ $# -ne 0 ]; do
	cur=${1}; next=${2};
	case "$cur" in
		--) shift; break;;
		-n|--dry-run) dry_run=1;;
		   --unpub-path) unpub=${next}; shift;;
		   --message-label) label="${next}"; shift;;
		-s|--safe) safe=1;;
		-v|--verbose)
			VERBOSITY=$((${VERBOSITY}+1));;
	esac
	shift;
done

manif=${1}

[ $# -gt 1 ] && bad_Usage "to many arguments given"
[ $# -lt 1 ] && bad_Usage "must provide bucket/manifest"

manif=${manif#s3://}
bucket=${manif%%/*}

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

tmpmf="${TMPD}/${manif##*/}"
xc2 s3cmd get "s3://${manif}" - > "${tmpmf}" ||
	fail "failed to get ${manif} from s3"

if [ "${safe}" -ne 0 ]; then
	regions=$(xc2 bucket2regions "${bucket}" ) ||
		fail "failed to get regions for ${bucket}"
	list=""
	for region in ${regions}; do
		imdata="${TMPD}/images-${region}"
		xc2 ximages describe-images --all --region "$region" > "$imdata" ||
			fail "failed to query ${region} for images"
		id=$(awk '$3 == manif { print $2 }' "manif=${manif}" "${imdata}" ) &&
			[ -z "${id}" ] || list="${list:+${list} }${region}:${id}"
	done
	if [ -n "${list}" ]; then
		error "${manif} is registered in ec2"
		for x in ${list}; do
			errorp "\t%s\n" "${x}"
		done
		fail
	fi
fi

[ -z "${unpub}" ] && unpub="${manif}.unpublished"

xc2 s3cmd get "s3://${unpub}" - > "${TMPD}/unpublished" 2>/dev/null || unpub=""

if [ "${dry_run}" -eq 1 ]; then
	error "delete ${label:+${label} }${manif}${unpub:+, ${unpub}}"
else
	out=$(xc2 delete-bundle --yes --retry --bucket "${bucket}" \
		  --manifest "${tmpmf}" 2>&1) ||
		failp "%s\n %s\n" "${out}" "failed to delete ${manif}"
	{ [ -z "${unpub}" ] || xc2 s3cmd del "s3://${unpub}" ; } ||
		fail "failed to remove ${unpub} from s3"
	echo "deleted ${label:+${label} }${manif}"
fi

exit 0