File: euca-unbundle

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 (147 lines) | stat: -rwxr-xr-x 4,596 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
#!/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, DirValidationError, Util
import tarfile
import gzip
from xml.dom import minidom
from xml.dom.minidom import Document
from M2Crypto import BN, EVP, RSA, util, Rand, m2, X509
from binascii import hexlify, unhexlify

VERSION = "2007-10-10"
BUNDLER_NAME = "euca-tools"
BUNDLER_VERSION = "1.0"
AES = 'AES-128-CBC'

usage_string = """
Unbundles a previously bundled image.    

euca-unbundle-image -m, --manifest manifest_path [-k, --privatekey private_key_path] 
[-s, --source source_directory] [-d, --destination destination_directory]
[-h, --help] [--version] [--debug]

REQUIRED PARAMETERS

-m, --manifest			Path to the manifest file for the bundled image.

OPTIONAL PARAMETERS

-d, --destination		Directory to store the image to (defaults to the current directory). 

-s, --source			Source directory for the bundled image parts (defaults to manifest directory).

"""

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

IMAGE_IO_CHUNK = 8 * 1024
IMAGE_SPLIT_CHUNK = IMAGE_IO_CHUNK * 1024;

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

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

def main():
    try:
        euca = Euca2ool('m:k:d:s:',
                                   ['manifest=', 'privatekey=', 'destination=', 'source='])
    except Exception, e:
	print e
        usage()
  
    private_key_path=euca.get_environ('EC2_PRIVATE_KEY')
    manifest_path = None
    batch=False
    directory = '.' 
    src_directory = None
     
    for name, value in euca.opts:
        if name in ('-h', '--help'):
            usage()
        elif name in ('-k', '--privatekey'):
            private_key_path = value
	elif name in ('-m', '--manifest'):
	    manifest_path = value 
	elif name in ('-d', '--directory'):
	    directory = value
	elif name in ('-s', '--source'):
	    src_directory = value
	elif name == '--version':
	    version()

    if manifest_path and private_key_path:
	try:
	    euca.validate_file(manifest_path)
	except FileValidationError:
	    print 'Invalid manifest'
	    sys.exit(1)
	try:
	    euca.validate_file(private_key_path)
	except FileValidationError:
	    print 'Invalid private key'
	    sys.exit(1)

	if src_directory:
	    try:
	        euca.validate_dir(src_directory)
	    except DirValidationError:
	        print 'Invalid source directory'
	        sys.exit(1)

        parts, encrypted_key, encrypted_iv = euca.parse_manifest(manifest_path)
	if not src_directory:
	    src_directory = euca.get_file_path(manifest_path)
	image = euca.assemble_parts(src_directory, directory, manifest_path, parts)
	print "Decrypting image"
	decrypted_image = euca.decrypt_image(image, encrypted_key, encrypted_iv, private_key_path)
	os.remove(image)
	print "Uncompressing image"
	unencrypted_image = euca.untarzip_image(directory, decrypted_image)
	os.remove(decrypted_image)
    else:
	if not manifest_path:
 	    print 'manifest path must be specified.'
	if not private_key_path:
	    print 'private key must be specified.'
	usage()
if __name__ == "__main__":
    main()