Пример #1
0
def fetch_qcow2_image(context, timeout_secs, image_service, image_id,
                      **kwargs):
    """Download flat image from the glance image server."""
    LOG.debug("Downloading image: %s from glance image server as a flat vmdk"
              " file." % image_id)
    read_iter = image_service.download(context, image_id)
    read_handle = rw_util.GlanceFileRead(read_iter)
    file_size = int(kwargs.get('image_size'))

    tmp_file_name = "/tmp/" + str(uuid.uuid4()) + ".qcow2"
    fp = open(tmp_file_name, "wb")

    start_transfer(context,
                   timeout_secs,
                   read_handle,
                   file_size,
                   write_file_handle=fp)
    if read_handle:
        read_handle.close()
    if fp:
        fp.close()

    converted_file_name = "/tmp/" + str(uuid.uuid4()) + ".vmdk"
    convert_command = "qemu-img convert -f qcow2 -O vmdk  " + tmp_file_name + " " + converted_file_name
    result = subprocess.call([convert_command], shell=True)
    if result == 0:
        file_size = os.path.getsize(converted_file_name)
        read_file_handle_local = rw_util.HybridFileHandle(
            converted_file_name, "rb")
        write_handle = rw_util.VMwareHTTPWriteFile(
            kwargs.get('host'), kwargs.get('data_center_name'),
            kwargs.get('datastore_name'), kwargs.get('cookies'),
            kwargs.get('file_path'), file_size)
        start_transfer(context,
                       timeout_secs,
                       read_file_handle_local,
                       file_size,
                       write_file_handle=write_handle)

        if read_file_handle_local:
            read_file_handle_local.close()
        if write_handle:
            write_handle.close()

        if os.path.exists(tmp_file_name):
            os.remove(tmp_file_name)
        if os.path.exists(converted_file_name):
            os.remove(converted_file_name)
    LOG.info(_("Downloaded image: %s from glance image server.") % image_id)
Пример #2
0
def fetch_flat_image(context, timeout_secs, image_service, image_id, **kwargs):
    """Download flat image from the glance image server."""
    LOG.debug("Downloading image: %s from glance image server as a flat vmdk"
              " file." % image_id)
    file_size = int(kwargs.get('image_size'))
    read_iter = image_service.download(context, image_id)
    read_handle = rw_util.GlanceFileRead(read_iter)
    write_handle = rw_util.VMwareHTTPWriteFile(kwargs.get('host'),
                                               kwargs.get('data_center_name'),
                                               kwargs.get('datastore_name'),
                                               kwargs.get('cookies'),
                                               kwargs.get('file_path'),
                                               file_size)
    start_transfer(context, timeout_secs, read_handle, file_size,
                   write_file_handle=write_handle)
    LOG.info(_("Downloaded image: %s from glance image server.") % image_id)