Downloading a VM image 

We can download the image stored in the Glance database to your local system where the application program is running. This can be done by creating an image object by locating the image in the Glance database and then using the connection object to refer to the image class and invoking the download_image() function by passing it the image object.

The following function demonstrates this. It accepts the connection object as an input parameter and then invokes the conn.image.find_image() function to create an image object. This object represents the image to be downloaded. Next, it opens a local file for writing and then invokes the download_image() function by passing the image object to it. This function returns a response object, which contains the entire image file. The response object is then written to the local file:

def download_image(conn):
# Find the image that you want to download
image = conn.image.find_image("cirros-0.3.5-x86_64-disk")

# Open a file to write to in the local system
with open("local_image.qcow2", "w") as local_file:
response = conn.image.download_image(image)

# Write to the local file.
local_file.write(response)

If the image size is very large, it may not be a wise thing to represent the entire VM image as an object in memory. We can choose to download the VM image as streamed data, rather than downloading the entire file all at once. This can be done by simply passing the stream=True parameter to the download_image() function. We can then read the response object in chunks of a few bytes at a time and then write them to the output file. 

The following program demonstrates this:

def download_image_stream(conn):
# Find the image you would like to download.
image = conn.image.find_image("cirros-0.3.5-x86_64-disk")

with open("local_image.qcow2", "wb") as local_file:
response = conn.image.download_image(image, stream=True)

# Read 2048 bytes per iteration until we read the entire file
for chunk in response.iter_content(chunk_size=2048):
local_file.write(chunk)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.100.20