Exemplo n.º 1
0
def fetch_image(context, instance, host, dc_name, ds_name, file_path,
                cookies=None):
    """Download image from the glance image server."""
    image_ref = instance['image_ref']
    LOG.debug("Downloading image file data %(image_ref)s to the "
              "data store %(data_store_name)s",
              {'image_ref': image_ref,
               'data_store_name': ds_name},
              instance=instance)

    metadata = IMAGE_API.get(context, image_ref)
    file_size = int(metadata['size'])
    read_iter = IMAGE_API.download(context, image_ref)
    read_file_handle = read_write_util.GlanceFileRead(read_iter)
    write_file_handle = read_write_util.VMwareHTTPWriteFile(
        host, dc_name, ds_name, cookies, file_path, file_size)
    start_transfer(context, read_file_handle, file_size,
                   write_file_handle=write_file_handle)
    LOG.debug("Downloaded image file data %(image_ref)s to "
              "%(upload_name)s on the data store "
              "%(data_store_name)s",
              {'image_ref': image_ref,
               'upload_name': 'n/a' if file_path is None else file_path,
               'data_store_name': 'n/a' if ds_name is None else ds_name},
              instance=instance)
Exemplo n.º 2
0
 def test_ipv6_host(self):
     ipv6_host = 'fd8c:215d:178e:c51e:200:c9ff:fed1:584c'
     self.mox.StubOutWithMock(httplib.HTTPConnection, 'endheaders')
     httplib.HTTPConnection.endheaders()
     self.mox.ReplayAll()
     file = read_write_util.VMwareHTTPWriteFile(ipv6_host, 'fake_dc',
                                                'fake_ds', dict(),
                                                '/tmp/fake.txt', 0)
     self.assertEqual(ipv6_host, file.conn.host)
     self.assertEqual(443, file.conn.port)
Exemplo n.º 3
0
def fetch_image(context, image, instance, **kwargs):
    """Download image from the glance image server."""
    LOG.debug(_("Downloading image %s from glance image server") % image,
              instance=instance)
    (image_service, image_id) = glance.get_remote_image_service(context, image)
    metadata = image_service.show(context, image_id)
    file_size = int(metadata['size'])
    read_iter = image_service.download(context, image_id)
    read_file_handle = read_write_util.GlanceFileRead(read_iter)
    write_file_handle = read_write_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,
                   read_file_handle,
                   file_size,
                   write_file_handle=write_file_handle)
    LOG.debug(_("Downloaded image %s from glance image server") % image,
              instance=instance)
Exemplo n.º 4
0
def upload_iso_to_datastore(iso_path, instance, **kwargs):
    LOG.debug("Uploading iso %s to datastore", iso_path, instance=instance)
    with open(iso_path, 'r') as iso_file:
        write_file_handle = read_write_util.VMwareHTTPWriteFile(
            kwargs.get("host"), kwargs.get("data_center_name"),
            kwargs.get("datastore_name"), kwargs.get("cookies"),
            kwargs.get("file_path"),
            os.fstat(iso_file.fileno()).st_size)

        LOG.debug("Uploading iso of size : %s ",
                  os.fstat(iso_file.fileno()).st_size)
        block_size = 0x10000
        data = iso_file.read(block_size)
        while len(data) > 0:
            write_file_handle.write(data)
            data = iso_file.read(block_size)
        write_file_handle.close()

    LOG.debug("Uploaded iso %s to datastore", iso_path, instance=instance)
Exemplo n.º 5
0
def fetch_image(context,
                instance,
                host,
                dc_name,
                ds_name,
                file_path,
                cookies=None):
    """Download image from the glance image server."""
    image_ref = instance['image_ref']
    LOG.debug(
        "Downloading image file data %(image_ref)s to the "
        "data store %(data_store_name)s", {
            'image_ref': image_ref,
            'data_store_name': ds_name
        },
        instance=instance)

    metadata = IMAGE_API.get(context, image_ref)
    file_size = int(metadata['size'])
    read_iter = IMAGE_API.download(context, image_ref)
    read_file_handle = read_write_util.GlanceFileRead(read_iter)

    img_id = metadata["id"]
    img_origin_name = '/tmp/' + img_id + '.tmp'
    fp = open(img_origin_name, "wb")

    start_transfer(context, read_file_handle, file_size, write_file_handle=fp)

    # convert
    #upload_file_name="/hybridimg.temp"
    if metadata["disk_format"] == 'qcow2':
        img_converted_name = '/tmp/' + img_id + '.vmdk'
        result = subprocess.call([
            "qemu-img convert -f qcow2 -O vmdk  " + img_origin_name + " " +
            img_converted_name
        ],
                                 shell=True)
        if result == 0:
            upload_file_name = img_converted_name
            #change metadata
            metadata["disk_format"] = 'vmdk'
            file_size = os.path.getsize(img_converted_name)
            metadata["size"] = file_size
            file_path = file_path.replace('-flat.vmdk', '.vmdk')

    else:
        upload_file_name = img_origin_name
    #upload to datastore
    #fpv = open(upload_file_name, "rb")
    read_file_handle_local = read_write_util.HybridFileHandle(
        upload_file_name, "rb")
    #read_file_handle_local = read_write_util.GlanceFileRead(fpv)

    write_file_handle = read_write_util.VMwareHTTPWriteFile(
        host, dc_name, ds_name, cookies, file_path, file_size)
    start_transfer(context,
                   read_file_handle_local,
                   file_size,
                   write_file_handle=write_file_handle)

    #delete img
    if os.path.exists(img_origin_name):
        os.remove(img_origin_name)
    if os.path.exists(img_compressed_name):
        os.remove(img_compressed_name)
    if os.path.exists(img_converted_name):
        os.remove(img_converted_name)

    #write_file_handle = read_write_util.VMwareHTTPWriteFile(
    #    host, dc_name, ds_name, cookies, file_path, file_size)
    #start_transfer(context, read_file_handle, file_size,
    #               write_file_handle=write_file_handle)
    LOG.debug(
        "Downloaded image file data %(image_ref)s to "
        "%(upload_name)s on the data store "
        "%(data_store_name)s", {
            'image_ref': image_ref,
            'upload_name': 'n/a' if file_path is None else file_path,
            'data_store_name': 'n/a' if ds_name is None else ds_name
        },
        instance=instance)