build: make the "rpm" rule work once again
[iwhd.git] / dc-register-image
blob0a839bdf9c7d7126e7ecba96dd8d9d256338c9e9
1 #!/bin/bash
3 # Copyright (C) 2010 Red Hat, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Uncomment for debugging.
19 #ECHO=echo
21 # $bucket is iwhd bucket (not S3 bucket, see $ami_bkt below).
22 bucket=$1; shift
23 # $object is iwhd's image name, which also serves as a basename in S3.
24 # So, if $object=foo.img, then we end uploading foo.img.manifest.xml and
25 # a bunch of foo.img.part.NN files.
26 # The foo.img itself is an uncompressed root filesystem, a device image.
27 # Finally, $object is in local directory $bucket/ (as fs back-end works).
28 object=$1; shift
29 # $api_key is "key ID" for S3.
30 api_key=$1; shift
31 # $api_secret is "secret" for S3.
32 api_secret=$1; shift
33 # $cert_file contains X.509 certificate for EC2 (cert-foo.pem).
34 cert_file=$1; shift
35 # $key_file contains private key for EC2 (pk-foo.pem).
36 key_file=$1; shift
37 # $api_uid is AWS account ID, but without dashes.
38 api_uid=$1; shift
39 # $ami_bkt is S3 bucket into which we upload foo.img.manifest.xml etc.
40 ami_bkt=$1; shift
41 # $kernel is an aki-xxxxxxxx ID (e.g. aki-99a0f1dc is pvgrub)
42 kernel=$1; shift
43 # $ramdisk is an ari-xxxxxxxx ID or "_default_" (most of the time for pvgrub)
44 ramdisk=$1; shift
46 # We do not set JAVA_HOME and EC2_HOME because they are not necessary
47 # if pre-packaged versions of ec2-api-tools and ec2-ami-tools are installed
48 # from RPMfusion and Amazon respectively. This also allows the user to
49 # set these locations in case an alternative set of tools is desired.
51 # However, we look for each missing tool in commonly overlooked locations.
53 which_it () {
54 cmd=$1
55 ret=$(/usr/bin/which $cmd 2>/dev/null)
56 if [ $? != 0 ]; then
57 if [ -x /usr/local/bin/$cmd ]; then
58 ret=/usr/local/bin/$cmd
59 elif [ -x ~/bin/$cmd ]; then
60 ret=~/bin/$cmd
63 if [ -z "$ret" ]; then
64 echo "ERROR missing_$cmd" >&2
65 exit 1
67 echo $ret
70 ec2_bundle_image=$(which_it ec2-bundle-image)
71 ec2_upload_bundle=$(which_it ec2-upload-bundle)
72 ec2_register=$(which_it ec2-register)
73 [ -z "$ec2_bundle_image" -o -z "$ec2_upload_bundle" -o -z "$ec2_register" ] && exit 1
75 # XXX Is this safe against running 2 requests simultaneously?
76 tmpdir=$(mktemp -d -p $PWD/$bucket) || exit 1
77 trap "rm -rf $tmpdir" EXIT
79 bundle_args="--batch --arch x86_64 -c $cert_file -k $key_file -u $api_uid"
80 if [ "$kernel" != "_default_" ]; then
81 bundle_args="$bundle_args --kernel $kernel"
83 if [ "$ramdisk" != "_default_" ]; then
84 bundle_args="$bundle_args --ramdisk $ramdisk"
86 $ECHO $ec2_bundle_image -i $bucket/$object -d $tmpdir $bundle_args
87 if [ $? != 0 ]; then
88 echo "ERROR bundling_failed" >&2
89 echo $ec2_bundle_image -i $bucket/$object -d $tmpdir $bundle_args >&2
90 exit 1
93 upload_args="--batch --retry -b $ami_bkt -a $api_key -s $api_secret"
94 $ECHO $ec2_upload_bundle -m $tmpdir/$object.manifest.xml $upload_args
95 if [ $? != 0 ]; then
96 echo "ERROR uploading_failed" >&2
97 echo $ec2_upload_bundle -m $tmpdir/$object.manifest.xml $upload_args >&2
98 exit 1
101 register_args="-C $cert_file -K $key_file"
102 $ECHO $ec2_register $register_args $ami_bkt/$object.manifest.xml -n $object
103 if [ $? != 0 ]; then
104 echo "ERROR registration_failed" >&2
105 echo $ec2_register $register_args $ami_bkt/$object.manifest.xml -n $object >&2
106 exit 1