'Migrate image_id files to destbucket'

@print_timing('Migrating    image')
def migrate_image(self, image_id, destbucket, migrate_manifest=False, kernel_id=None, ramdisk_id=None, region=None, cert=None, private_key=None):

    if migrate_manifest:
        utils.check_required(['ec2-migrate-manifest'])
        if (not cert):
            raise exception.BaseException('no    cert    specified')
        if (not private_key):
            raise exception.BaseException('no    private_key    specified')
        if (not kernel_id):
            raise exception.BaseException('no    kernel_id    specified')
        if (not ramdisk_id):
            raise exception.BaseException('no    ramdisk_id    specified')
    image = self.get_image(image_id)
    if (image.root_device_type == 'ebs'):
        raise exception.AWSError(('The    image    you    wish    to    migrate    is    EBS-based.    ' + 'This    method    only    works    for    instance-store    images'))
    files = self.get_image_files(image)
    if (not files):
        log.info(('No    files    found    for    image:    %s' % image_id))
        return
    log.info(('Migrating    image:    %s' % image_id))
    widgets = [files[0].name, progressbar.Percentage(), '    ', progressbar.Bar(marker=progressbar.RotatingMarker()), '    ', progressbar.ETA(), '    ', '    ']
    counter = 0
    num_files = len(files)
    pbar = progressbar.ProgressBar(widgets=widgets, maxval=num_files).start()
    for f in files:
        widgets[0] = ('%s:    (%s/%s)' % (f.name, (counter + 1), num_files))
        f.copy(destbucket, f.name)
        pbar.update(counter)
        counter += 1
    pbar.finish()
    if migrate_manifest:
        dbucket = self.s3.get_bucket(destbucket)
        manifest_key = dbucket.get_key(self.get_image_manifest(image))
        f = tempfile.NamedTemporaryFile()
        manifest_key.get_contents_to_file(f.file)
        f.file.close()
        cmd = (('ec2-migrate-manifest    -c    %s    -k    %s    -m    %s    --kernel    %s    ' + '--ramdisk    %s    --no-mapping    ') % (cert, private_key, f.name, kernel_id, ramdisk_id))
        register_cmd = ('ec2-register    %s/%s' % (destbucket, manifest_key.name))
        if region:
            cmd += ('--region    %s' % region)
            register_cmd += ('    --region    %s' % region)
        log.info('Migrating    manifest    file...')
        retval = os.system(cmd)
        if (retval != 0):
            raise exception.BaseException(('ec2-migrate-manifest    failed    with    status    %s' % retval))
        f.file = open(f.name, 'r')
        manifest_key.set_contents_from_file(f.file)
        manifest_key.add_email_grant('READ', 'za-team@amazon.com')
        f.close()
        os.unlink((f.name + '.bak'))
        log.info((('Manifest    migrated    successfully.    You    can    now    run:\n' + register_cmd) + '\nto    register    your    migrated    image.'))
