- dev = disk.get_device()
- dev.mount()
- osclass = get_os_class(dev.distro, dev.ostype)
- image_os = osclass(dev.root, dev.g)
- metadata = image_os.get_metadata()
-
- if options.sysprep:
- image_os.sysprep()
-
- if options.cleanup:
- image_os.data_cleanup()
-
- dev.umount()
-
- size = options.shrink and dev.shrink() or dev.size()
- metadata['size'] = str(size // 2 ** 20)
- dd('if=%s' % dev.device,
- 'of=%s/%s.%s' % (options.outdir, options.name, 'diskdump'),
- 'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22))
-
- f = open('%s/%s.%s' % (options.outdir, options.name, 'meta'), 'w')
- for key in metadata.keys():
- f.write("%s=%s\n" % (key, metadata[key]))
- f.close()
+ snapshot = disk.snapshot()
+
+ image = disk.get_image(snapshot)
+
+ # If no customization is to be applied, the image should be mounted ro
+ ro = (not (options.sysprep or options.shrink) or options.print_sysprep)
+ image.mount(ro)
+ try:
+ for sysprep in options.disabled_syspreps:
+ image.os.disable_sysprep(image.os.get_sysprep_by_name(sysprep))
+
+ for sysprep in options.enabled_syspreps:
+ image.os.enable_sysprep(image.os.get_sysprep_by_name(sysprep))
+
+ if options.print_sysprep:
+ image.os.print_syspreps()
+ out.output()
+
+ if options.outfile is None and not options.upload:
+ return 0
+
+ if options.sysprep:
+ err_msg = "Unable to apply the system preparation tasks."
+ if not image.mounted:
+ raise FatalError("%s Couldn't mount the media." % err_msg)
+ elif image.mounted_ro:
+ raise FatalError("%s Couldn't mount the media read-write."
+ % err_msg)
+ image.os.do_sysprep()
+
+ metadata = image.os.meta
+ finally:
+ image.umount()
+
+ size = options.shrink and image.shrink() or image.size
+ metadata.update(image.meta)
+
+ # Add command line metadata to the collected ones...
+ metadata.update(options.metadata)
+
+ md5 = MD5(out)
+ checksum = md5.compute(image.device, size)
+
+ metastring = '\n'.join(
+ ['%s=%s' % (key, value) for (key, value) in metadata.items()])
+ metastring += '\n'
+
+ if options.outfile is not None:
+ image.dump(options.outfile)
+
+ out.output('Dumping metadata file ...', False)
+ with open('%s.%s' % (options.outfile, 'meta'), 'w') as f:
+ f.write(metastring)
+ out.success('done')
+
+ out.output('Dumping md5sum file ...', False)
+ with open('%s.%s' % (options.outfile, 'md5sum'), 'w') as f:
+ f.write('%s %s\n' % (checksum,
+ os.path.basename(options.outfile)))
+ out.success('done')
+
+ # Destroy the image instance. We only need the snapshot from now on
+ disk.destroy_image(image)
+
+ out.output()
+ try:
+ uploaded_obj = ""
+ if options.upload:
+ out.output("Uploading image to pithos:")
+ kamaki = Kamaki(account, out)
+ with open(snapshot, 'rb') as f:
+ uploaded_obj = kamaki.upload(
+ f, size, options.upload,
+ "(1/4) Calculating block hashes",
+ "(2/4) Uploading missing blocks")
+
+ out.output("(3/4) Uploading metadata file ...", False)
+ kamaki.upload(StringIO.StringIO(metastring),
+ size=len(metastring),
+ remote_path="%s.%s" % (options.upload, 'meta'))
+ out.success('done')
+ out.output("(4/4) Uploading md5sum file ...", False)
+ md5sumstr = '%s %s\n' % (checksum,
+ os.path.basename(options.upload))
+ kamaki.upload(StringIO.StringIO(md5sumstr),
+ size=len(md5sumstr),
+ remote_path="%s.%s" % (options.upload, 'md5sum'))
+ out.success('done')
+ out.output()
+
+ if options.register:
+ img_type = 'public' if options.public else 'private'
+ out.output('Registering %s image with ~okeanos ...' % img_type,
+ False)
+ kamaki.register(options.register, uploaded_obj, metadata,
+ options.public)
+ out.success('done')
+ out.output()
+ except ClientError as e:
+ raise FatalError("Pithos client: %d %s" % (e.status, e.message))
+