~ubuntu-branches/ubuntu/natty/euca2ools/natty-201106142030

« back to all changes in this revision

Viewing changes to bin/euca-delete-bundle

  • Committer: Scott Moser
  • Date: 2010-11-17 17:54:42 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: smoser@ubuntu.com-20101117175442-vbgznlpf184u1dpz
New upstream release
reset to 1.3.1 upstream, all delta is lost, but will be merged back in.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
2
3
 
3
4
# Software License Agreement (BSD License)
4
5
#
32
33
#
33
34
# Author: Neil Soman neil@eucalyptus.com
34
35
 
35
 
import getopt, sys, os
 
36
import getopt
 
37
import sys
 
38
import os
36
39
from xml.dom import minidom
37
 
from euca2ools import Euca2ool, FileValidationError, Util, ConnectionFailed
 
40
from euca2ools import Euca2ool, FileValidationError, Util, \
 
41
    ConnectionFailed
38
42
from boto.exception import S3ResponseError
39
43
from boto.s3 import Connection
40
44
from boto.s3.key import Key
41
45
 
42
 
usage_string = """
 
46
usage_string = \
 
47
    """
43
48
 
44
49
Delete a previously uploaded bundle.    
45
50
 
71
76
    Util().usage()
72
77
    sys.exit(status)
73
78
 
 
79
 
74
80
def version():
75
81
    print Util().version()
76
82
    sys.exit()
77
83
 
 
84
 
78
85
def ensure_bucket(connection, bucket):
79
86
    bucket_instance = None
80
87
    try:
81
 
        bucket_instance = connection.get_bucket(bucket)
 
88
        bucket_instance = connection.get_bucket(bucket)
82
89
    except S3ResponseError, s3error:
83
 
        print 'Unable to get bucket %s' % (bucket)
84
 
        sys.exit()
 
90
        print 'Unable to get bucket %s' % bucket
 
91
        sys.exit()
85
92
    return bucket_instance
86
 
 
 
93
 
 
94
 
87
95
def get_parts(manifest_filename):
88
96
    parts = []
89
 
    dom = minidom.parse(manifest_filename) 
 
97
    dom = minidom.parse(manifest_filename)
90
98
    manifest_elem = dom.getElementsByTagName('manifest')[0]
91
99
    parts_list = manifest_elem.getElementsByTagName('filename')
92
100
    for part_elem in parts_list:
93
 
        nodes = part_elem.childNodes
94
 
        for node in nodes:
 
101
        nodes = part_elem.childNodes
 
102
        for node in nodes:
95
103
            if node.nodeType == node.TEXT_NODE:
96
104
                parts.append(node.data)
97
105
    return parts
98
106
 
 
107
 
99
108
def get_relative_filename(filename):
100
109
    f_parts = filename.split('/')
101
110
    return f_parts[len(f_parts) - 1]
102
111
 
 
112
 
103
113
def get_manifests(bucket):
104
114
    manifests = []
105
115
    keys = bucket.get_all_keys()
106
116
    for k in keys:
107
 
        if k.name:
108
 
            if(k.name.find('manifest') >= 0):
109
 
                manifests.append(k.name)
 
117
        if k.name:
 
118
            if k.name.find('manifest') >= 0:
 
119
                manifests.append(k.name)
110
120
    return manifests
111
121
 
 
122
 
112
123
def download_manifests(bucket, manifests, directory):
113
 
    if (len(manifests) > 0):
114
 
        if not os.path.exists(directory):
115
 
            os.makedirs(directory)
 
124
    if len(manifests) > 0:
 
125
        if not os.path.exists(directory):
 
126
            os.makedirs(directory)
116
127
    for manifest in manifests:
117
 
        k = Key(bucket)
118
 
        k.key = manifest
119
 
        manifest_filename = os.path.join(directory, manifest)
120
 
        manifest_file = open(manifest_filename, "wb")
121
 
        try:
122
 
            k.get_contents_to_file(manifest_file)
 
128
        k = Key(bucket)
 
129
        k.key = manifest
 
130
        manifest_filename = os.path.join(directory, manifest)
 
131
        manifest_file = open(manifest_filename, 'wb')
 
132
        try:
 
133
            k.get_contents_to_file(manifest_file)
123
134
        except S3ResponseError, s3error:
124
 
            s3error_string = '%s' % (s3error)
125
 
            if (s3error_string.find('200') < 0):
126
 
                print s3error_string
127
 
                print 'unable to download manifest %s' % (manifest)
128
 
                if os.path.exists(manifest_filename):
129
 
                    os.remove(manifest_filename)
130
 
                return False
131
 
        manifest_file.close()
 
135
            s3error_string = '%s' % s3error
 
136
            if s3error_string.find('200') < 0:
 
137
                print s3error_string
 
138
                print 'unable to download manifest %s' % manifest
 
139
                if os.path.exists(manifest_filename):
 
140
                    os.remove(manifest_filename)
 
141
                return False
 
142
        manifest_file.close()
132
143
    return True
133
144
 
 
145
 
134
146
def delete_parts(bucket, manifests, directory=None):
135
147
    for manifest in manifests:
136
 
        manifest_filename = os.path.join(directory, manifest)
137
 
        parts = get_parts(manifest_filename)
138
 
        for part in parts:
139
 
            k = Key(bucket)
140
 
            k.key = part 
141
 
            try:
142
 
                k.delete()
143
 
            except S3ResponseError, s3error:
144
 
                s3error_string = '%s' % (s3error)
145
 
                if (s3error_string.find('200') < 0):
146
 
                    print s3error_string
147
 
                    print 'unable to delete part %s' % (part)
148
 
                    sys.exit()
149
 
        
150
 
 
151
 
def delete_manifests(bucket, manifests, clear, euca, bucket_name):
 
148
        manifest_filename = os.path.join(directory, manifest)
 
149
        parts = get_parts(manifest_filename)
 
150
        for part in parts:
 
151
            k = Key(bucket)
 
152
            k.key = part
 
153
            try:
 
154
                k.delete()
 
155
            except S3ResponseError, s3error:
 
156
                s3error_string = '%s' % s3error
 
157
                if s3error_string.find('200') < 0:
 
158
                    print s3error_string
 
159
                    print 'unable to delete part %s' % part
 
160
                    sys.exit()
 
161
 
 
162
 
 
163
def delete_manifests(
 
164
    bucket,
 
165
    manifests,
 
166
    clear,
 
167
    euca,
 
168
    bucket_name,
 
169
    ):
152
170
    for manifest in manifests:
153
 
        k = Key(bucket)
154
 
        k.key = manifest
155
 
        try:
156
 
            k.delete()
157
 
        except Exception, s3error:
158
 
            s3error_string = '%s' % (s3error)
159
 
            if (s3error_string.find('200') < 0):
160
 
                print s3error_string
161
 
                print 'unable to delete manifest %s' % (manifest)
162
 
                try:
163
 
                    bucket = ensure_bucket(euca.make_connection(), bucket_name)
164
 
                except ConnectionFailed, e:
165
 
                    print e.message
166
 
                    sys.exit(1)
 
171
        k = Key(bucket)
 
172
        k.key = manifest
 
173
        try:
 
174
            k.delete()
 
175
        except Exception, s3error:
 
176
            s3error_string = '%s' % s3error
 
177
            if s3error_string.find('200') < 0:
 
178
                print s3error_string
 
179
                print 'unable to delete manifest %s' % manifest
 
180
                try:
 
181
                    bucket = ensure_bucket(euca.make_connection(),
 
182
                            bucket_name)
 
183
                except ConnectionFailed, e:
 
184
                    print e.message
 
185
                    sys.exit(1)
167
186
    if clear:
168
 
        try:
169
 
            bucket.delete()
170
 
        except Exception, s3error:
171
 
            s3error_string = '%s' % (s3error)
172
 
            if (s3error_string.find('200') < 0):
173
 
                print s3error_string
174
 
                print 'unable to delete bucket %s' % (bucket.name)
 
187
        try:
 
188
            bucket.delete()
 
189
        except Exception, s3error:
 
190
            s3error_string = '%s' % s3error
 
191
            if s3error_string.find('200') < 0:
 
192
                print s3error_string
 
193
                print 'unable to delete bucket %s' % bucket.name
 
194
 
175
195
 
176
196
def remove_manifests(manifests, directory):
177
197
    for manifest in manifests:
178
 
        manifest_filename = os.path.join(directory, manifest)
179
 
        if os.path.exists(manifest_filename):
180
 
            os.remove(manifest_filename)        
 
198
        manifest_filename = os.path.join(directory, manifest)
 
199
        if os.path.exists(manifest_filename):
 
200
            os.remove(manifest_filename)
 
201
 
181
202
 
182
203
def main():
183
204
    euca = None
184
205
    try:
185
 
        euca = Euca2ool('b:m:p:',
186
 
                                  ['bucket=', 'manifest=', 'prefix=', 'clear'],
187
 
                                  is_s3=True)
 
206
        euca = Euca2ool('b:m:p:', ['bucket=', 'manifest=', 'prefix=',
 
207
                        'clear'], is_s3=True)
188
208
    except Exception, e:
189
 
        print e
 
209
        print e
190
210
        usage()
191
 
 
192
 
    bucket=None
 
211
 
 
212
    bucket = None
193
213
    manifest_path = None
194
 
    directory = '/tmp' 
 
214
    directory = '/tmp'
195
215
    clear = False
196
216
    prefix = None
197
217
 
198
 
    for name, value in euca.opts:
 
218
    for (name, value) in euca.opts:
199
219
        if name in ('-h', '--help'):
200
220
            usage(0)
201
221
        elif name in ('-b', '--bucket'):
202
222
            bucket = value
203
223
        elif name in ('-m', '--manifest'):
204
224
            manifest_path = value
205
 
        elif name == '--clear':
206
 
            clear = True
 
225
        elif name == '--clear':
 
226
            clear = True
207
227
        elif name in ('-p', '--prefix'):
208
228
            prefix = value
209
229
 
210
230
    if bucket:
211
 
        try:
212
 
            if manifest_path:
213
 
                euca.validate_file(manifest_path)
214
 
        except FileValidationError:
215
 
            print 'Invalid manifest' 
216
 
            sys.exit(1)
217
 
        try:
218
 
            conn = euca.make_connection()
219
 
        except ConnectionFailed, e:
220
 
            print e.message
221
 
            sys.exit(1)
 
231
        try:
 
232
            if manifest_path:
 
233
                euca.validate_file(manifest_path)
 
234
        except FileValidationError:
 
235
            print 'Invalid manifest'
 
236
            sys.exit(1)
 
237
        try:
 
238
            conn = euca.make_connection()
 
239
        except ConnectionFailed, e:
 
240
            print e.message
 
241
            sys.exit(1)
222
242
        bucket_instance = ensure_bucket(conn, bucket)
223
 
        manifests = None
224
 
        delete_local_manifests = True
225
 
        if not manifest_path:
226
 
            if not prefix:
227
 
                manifests = get_manifests(bucket_instance)
228
 
            else:
229
 
                manifests = ["%s.manifest.xml" % prefix]
230
 
        else:
231
 
            manifests = ["%s" % euca.get_relative_filename(manifest_path)] 
232
 
            directory = "%s" % euca.get_file_path(manifest_path)
233
 
            delete_local_manifests = False                  
234
 
        return_code = download_manifests(bucket_instance, manifests, directory)
235
 
        if return_code:
236
 
            delete_parts(bucket_instance, manifests, directory)
237
 
        delete_manifests(bucket_instance, manifests, clear, euca, bucket)
238
 
        if delete_local_manifests:
239
 
            remove_manifests(manifests, directory)
240
 
            
 
243
        manifests = None
 
244
        delete_local_manifests = True
 
245
        if not manifest_path:
 
246
            if not prefix:
 
247
                manifests = get_manifests(bucket_instance)
 
248
            else:
 
249
                manifests = ['%s.manifest.xml' % prefix]
 
250
        else:
 
251
            manifests = ['%s'
 
252
                         % euca.get_relative_filename(manifest_path)]
 
253
            directory = '%s' % euca.get_file_path(manifest_path)
 
254
            delete_local_manifests = False
 
255
        return_code = download_manifests(bucket_instance, manifests,
 
256
                directory)
 
257
        if return_code:
 
258
            delete_parts(bucket_instance, manifests, directory)
 
259
        delete_manifests(bucket_instance, manifests, clear, euca,
 
260
                         bucket)
 
261
        if delete_local_manifests:
 
262
            remove_manifests(manifests, directory)
241
263
    else:
242
 
        print 'bucket must be specified.'       
 
264
 
 
265
        print 'bucket must be specified.'
243
266
        usage()
244
267
 
245
 
if __name__ == "__main__":
 
268
 
 
269
if __name__ == '__main__':
246
270
    main()
247
 
 
 
271