Revision b1395967 image_creator/main.py

b/image_creator/main.py
43 43
import sys
44 44
import os
45 45
import optparse
46
import StringIO
46 47

  
47 48
dd = get_command('dd')
48 49

  
......
194 195
        size = options.shrink and dev.shrink() or dev.size()
195 196
        metadata['SIZE'] = str(size // 2 ** 20)
196 197

  
197
        #Calculating MD5sum
198
        output("Calculating md5sum...", False)
199 198
        checksum = md5(snapshot, size)
200
        success(checksum)
201
        output()
202 199

  
203
        if options.outfile is not None:
204
            f = open('%s.%s' % (options.outfile, 'meta'), 'w')
205
            try:
206
                for key in metadata.keys():
207
                    f.write("%s=%s\n" % (key, metadata[key]))
208
            finally:
209
                f.close()
200
        metastring = "\n".join(
201
            ['%s=%s' % (key, value) for (key, value) in metadata.items()])
210 202

  
203
        if options.outfile is not None:
211 204
            dev.dump(options.outfile)
212 205

  
206
            output('Dumping metadata file...', False)
207
            with open('%s.%s' % (options.outfile, 'meta'), 'w') as f:
208
                    f.write(metastring)
209
            success('done')
210

  
211
            output('Dumping md5sum file...', False)
212
            with open('%s.%s' % (options.outfile, 'md5sum'), 'w') as f:
213
                f.write('%s %s'% (checksum, os.path.basename(options.outfile)))
214
            success('done')
215

  
213 216
        # Destroy the device. We only need the snapshot from now on
214 217
        disk.destroy_device(dev)
215 218

  
219
        output()
220

  
221
        uploaded_obj = ""
216 222
        if options.upload:
217 223
            output("Uploading image to pithos:")
218 224
            kamaki = Kamaki(options.account, options.token)
219
            kamaki.upload(snapshot, size, options.upload)
225
            with open(snapshot) as f:
226
                uploaded_obj = kamaki.upload(f, size, options.upload,
227
                                "(1/4)  Calculating block hashes",
228
                                "(2/4)  Uploading missing blocks")
229

  
230
            output("(3/4)  Uploading metadata file...", False)
231
            kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
232
                                remote_path="%s.%s" % (options.upload, 'meta'))
233
            success('done')
234
            output("(4/4)  Uploading md5sum file...", False)
235
            md5sumstr = '%s %s' % (checksum, os.path.basename(options.upload))
236
            kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
237
                            remote_path="%s.%s" % (options.upload, 'md5sum'))
238
            success('done')
239
            output()
240

  
241
        if options.register:
242
            output('Registing image to ~okeanos...')
243
            kamaki.register(options.register, uploaded_obj, metadata)
244
            output('done')
245
            output()
220 246

  
221 247
    finally:
222 248
        output('cleaning up...')
223 249
        disk.cleanup()
224 250

  
251
    success("snf-image-creator exited without errors")
252

  
225 253
    return 0
226 254

  
227 255

  

Also available in: Unified diff