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
|
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Eucalyptus Systems, Inc.
# All rights reserved.
#
# Redistribution and use of this software in source and binary forms, with or
# without modification, are permitted provided that the following conditions
# are met:
#
# Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
#
# Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Neil Soman neil@eucalyptus.com
import getopt, sys, os
from xml.dom import minidom
from euca2ools import Euca2ool, FileValidationError, Util
from boto.exception import S3ResponseError
from boto.s3 import Connection
from boto.s3.key import Key
usage_string = """
Downloads a bundled image from a bucket.
euca-download-bundle -b, --bucket bucket [-m, --manifest manifest_path] [-d, --directory directory]
[-h, --help] [--version] [--debug]
REQUIRED PARAMETERS
-b, --bucket The name of the bucket to download to. Bucket will be created if it does not exist.
OPTIONAL PARAMETERS
-m, --manifest_path The path to the manifest file.
-d, --directory The name of the directory to download the bundled parts to.
"""
version_string = """ euca-download-bundle version: 1.0 (BSD)"""
def usage():
print usage_string
Util().usage()
def version():
print version_string
sys.exit()
def ensure_bucket(connection, bucket):
bucket_instance = None
try:
bucket_instance = connection.get_bucket(bucket)
except S3ResponseError, s3error:
print 'Unable to get bucket %s' % (bucket)
sys.exit()
return bucket_instance
def get_parts(manifest_filename):
parts = []
dom = minidom.parse(manifest_filename)
manifest_elem = dom.getElementsByTagName('manifest')[0]
parts_list = manifest_elem.getElementsByTagName('filename')
for part_elem in parts_list:
nodes = part_elem.childNodes
for node in nodes:
if node.nodeType == node.TEXT_NODE:
parts.append(node.data)
return parts
def get_relative_filename(filename):
f_parts = filename.split('/')
return f_parts[len(f_parts) - 1]
def get_manifests(bucket):
manifests = []
keys = bucket.get_all_keys()
for k in keys:
if k.name:
if(k.name.find('manifest') >= 0):
manifests.append(k.name)
return manifests
def download_manifests(bucket, manifests, directory):
if (len(manifests) > 0):
if not os.path.exists(directory):
os.makedirs(directory)
for manifest in manifests:
k = Key(bucket)
k.key = manifest
print "Downloading", manifest
manifest_file = open(os.path.join(directory, manifest), "wb")
k.get_contents_to_file(manifest_file)
manifest_file.close()
def download_parts(bucket, manifests, directory):
for manifest in manifests:
manifest_filename = os.path.join(directory, manifest)
parts = get_parts(manifest_filename)
for part in parts:
k = Key(bucket)
k.key = part
print "Downloading", part
part_file = open(os.path.join(directory, part), "wb")
k.get_contents_to_file(part_file)
part_file.close()
def main():
euca = None
try:
euca = Euca2ool('b:m:d:',
['bucket=', 'manifest=', 'directory='],
is_s3=True)
except Exception, e:
print e
usage()
bucket=None
manifest_path = None
directory = '/tmp'
for name, value in euca.opts:
if name in ('-h', '--help'):
usage()
elif name in ('-d', '--directory'):
directory = value
elif name in ('-b', '--bucket'):
bucket = value
elif name in ('-m', '--manifest'):
manifest_path = value
if bucket:
if manifest_path:
try:
euca.validate_file(manifest_path)
except FileValidationError:
print 'Invalid manifest', manifest_path
sys.exit(1)
conn = euca.make_connection()
bucket_instance = ensure_bucket(conn, bucket)
manifests = get_manifests(bucket_instance)
download_manifests(bucket_instance, manifests, directory)
download_parts(bucket_instance, manifests, directory)
else:
print 'bucket must be specified.'
usage()
if __name__ == "__main__":
main()
|