File: euca-bundle-image

package info (click to toggle)
euca2ools 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 532 kB
  • ctags: 339
  • sloc: python: 4,145; makefile: 53
file content (230 lines) | stat: -rwxr-xr-x 7,629 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/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 euca2ools import Euca2ool, FileValidationError, Util

usage_string = """
Bundles an image for use with Eucalyptus or Amazon EC2.

euca-bundle-image -i, --image image_path -u, --user user [-c, --cert cert_path] 
[-k, --privatekey private_key_path] [-p, --prefix prefix] [--kernel kernel_id] 
[--ramdisk ramdisk_id] [-B, --block-device-mapping mapping] 
[-d, --destination destination_path] [--ec2cert ec2cert_path] 
[-r, --arch target_architecture] [--batch] [-h, --help] [--version] [--debug] 

REQUIRED PARAMETERS

-i, --image			Path to the image file to bundle.

-u, --user			User ID (12-digit) of the user who is bundling the image.

OPTIONAL PARAMETERS

-c, --cert			Path to the user's PEM encoded certificate.

-k, --privatekey		Path to the user's PEM encoded private key.

-p, --prefix			The prefix for the bundle image files. (default: image name).

--kernel			The kernel to be associated with the bundled image.

--ramdisk			The ramdisk to be associated with the bundled image.

-B, --block-device-mapping	Default block device mapping for the image (comma-separated list of key=value pairs).

-d, --destination		Directory to store the bundled image in (default: "/tmp"). Recommended. 

--ec2cert_path 			The path to the Cloud's X509 public key certificate.

-r, --arch			Target architecture for the image ('x86_64' or 'i386' default: 'x86_64').

--batch				Run in batch mode (compatibility only. has no effect).
"""

version_string = """    euca-bundle-image version: 1.0 (BSD)"""

def usage():
    print usage_string
    Util().usage()
    sys.exit()

def version():
    print version_string
    sys.exit()

def get_block_devs(mapping):
    mapping = []
    mapping_pairs = mapping.split(',')
    for m in mapping_pairs:
        m_parts = m.split('=')
        if(len(m_parts) > 1):
            mapping.append(m_parts[0])
            mapping.append(m_parts[1])
    return mapping

def add_product_codes(product_code_string, product_codes):
    if not product_codes:
	product_codes = []
    product_code_values = product_code_string.split(',')
   
    for p in product_code_values:
        product_codes.append(p)
    
    return product_codes


def main():
    euca = None
    try:
	euca = Euca2ool('i:c:k:u:B:d:br:p:',
                                   ['image=', 'cert=', 'privatekey=', 'user=', 'prefix=', 
                                    'kernel=', 'ramdisk=', 'block-device-mapping=', 'destination=', 'ec2cert=', 'arch=', 'productcodes=', 'batch'])
    except Exception, e:
	print e
        usage()
  
    image_path=None
    kernel=None
    user=None
    ramdisk=None
    cert_path=euca.get_environ('EC2_CERT')
    private_key_path=euca.get_environ('EC2_PRIVATE_KEY')
    prefix=None
    destination_path='/tmp'
    ec2cert_path=euca.get_environ('EUCALYPTUS_CERT')
    target_arch='x86_64'
    mapping = None
    product_codes = None
    product_code_string = None
    user_string = euca.get_environ("EC2_USER_ID")
    if user_string:
        try:
            user = int(user_string)
        except ValueError:
            print 'Invalid user', user_string
            sys.exit()	       
	user = user_string
 
    for name, value in euca.opts:
        if name in ('-h', '--help'):
            usage()
        elif name in ('-i', '--image'):
            image_path = value
        elif name in ('-c', '--cert'):
            cert_path = value
        elif name in ('-k', '--privatekey'):
            private_key_path = value
        elif name in ('-u', '--user'):
	    try:
		value = value.replace('-', '')
                user = int(value)
	    except ValueError:
		print 'Invalid user', value
		sys.exit()
	    user = value
        elif name == '--kernel':	
            kernel = value
        elif name == '--ramdisk':
            ramdisk = value
        elif name in ('-p', '--prefix'):
            prefix = value
        elif name in ('-d', '--destination'):
            destination_path = value
        elif name == '--ec2cert':
            ec2cert_path = value
        elif name in ('-r', '--arch'):
            target_arch = value
	    print target_arch
	    if target_arch != 'i386' and target_arch != 'x86_64':
		print 'target architecture must be i386 or x86_64'
		usage()
	elif name in ('-B', '--block-device-mapping'):
	    mapping = value
	elif name == '--productcodes':
	    product_code_string = value
	elif name == '--version':
	    version()

    if image_path and cert_path and private_key_path and user and ec2cert_path:
	try:
	    euca.validate_file(image_path)
	except FileValidationError:
	    print 'Invalid image'
	    sys.exit(1)
	try:
	    euca.validate_file(cert_path)
	except FileValidationError:
	    print 'Invalid cert'
	    sys.exit(1)
	try:
	    euca.validate_file(private_key_path)
	except FileValidationError:
	    print 'Invalid private key'
 	    sys.exit(1)
	try: 
	    euca.validate_file(ec2cert_path)
	except FileValidationError:
	    print 'Invalid ec2cert' 
	    sys.exit(1)

	image_size, sha_image_digest = euca.check_image(image_path, destination_path)
	if not prefix:
            prefix = euca.get_relative_filename(image_path)
	tgz_file = euca.tarzip_image(prefix, image_path, destination_path)
	encrypted_file, key, iv, bundled_size = euca.encrypt_image(tgz_file)
	os.remove(tgz_file)
	parts, parts_digest = euca.split_image(encrypted_file)
        if mapping:
            mapping = get_block_devs(mapping)
	if product_code_string:
	    product_codes = add_product_codes(product_code_string, product_codes)   
        euca.generate_manifest(destination_path, prefix, parts, parts_digest, image_path, key, iv, cert_path, ec2cert_path, private_key_path, target_arch, image_size, bundled_size, sha_image_digest, user, kernel, ramdisk, mapping, product_codes)
        os.remove(encrypted_file)
    else:
	if not image_path:
	    print 'image be specified.'
	if not cert_path:
	    print 'cert must be specified.'
	if not private_key_path:
	    print 'private key must be specified.'
	if not user:
	    print 'user must be specified.'
	if not ec2cert_path:
	    print 'ec2cert must be specified.'
	usage()
if __name__ == "__main__":
    main()