virt: introduce model for describing local image metadata

It is not sufficient to just pass a filename into the
libguestfs APIs. There needs to be various pieces of
metadata passed in order to support network based images.
This introduces a simple data model to describe images
when passing info from virt drivers to the VFS APIs.

These classes are only intended to contain identifying
attributes for the various storage backends used as
images. They are not intended to hold any functional
logic, as that will remain in the libvirt driver private
image backend classes. This separates generic description
of storage backends, from driver specific implementation
details.

Related-Bug: #1257674
Change-Id: I5f86d91aa2bcef2385d5d16022a9bd4ea7b0485e
This commit is contained in:
Daniel P. Berrange 2014-10-29 17:08:21 +00:00
parent 4a02d9415f
commit 79af02046d
5 changed files with 228 additions and 0 deletions

View File

@ -1888,3 +1888,7 @@ class EnumFieldInvalid(Invalid):
class EnumFieldUnset(Invalid):
msg_fmt = _('%(fieldname)s missing field type')
class InvalidImageFormat(Invalid):
msg_fmt = _("Invalid image format '%(format)s'")

View File

View File

@ -0,0 +1,95 @@
#
# Copyright (C) 2014 Red Hat, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from nova import exception
from nova import test
from nova.virt.image import model as imgmodel
class ImageTest(test.NoDBTestCase):
def test_local_file_image(self):
img = imgmodel.LocalFileImage(
"/var/lib/libvirt/images/demo.qcow2",
imgmodel.FORMAT_QCOW2)
self.assertIsInstance(img, imgmodel.Image)
self.assertEqual("/var/lib/libvirt/images/demo.qcow2", img.path)
self.assertEqual(imgmodel.FORMAT_QCOW2, img.format)
def test_local_file_bad_format(self):
self.assertRaises(exception.InvalidImageFormat,
imgmodel.LocalFileImage,
"/var/lib/libvirt/images/demo.qcow2",
"jpeg")
def test_local_block_image(self):
img = imgmodel.LocalBlockImage(
"/dev/volgroup/demovol")
self.assertIsInstance(img, imgmodel.Image)
self.assertEqual("/dev/volgroup/demovol", img.path)
self.assertEqual(imgmodel.FORMAT_RAW, img.format)
def test_rbd_image(self):
img = imgmodel.RBDImage(
"demo",
"openstack",
"cthulu",
"braanes",
["rbd.example.org"])
self.assertIsInstance(img, imgmodel.Image)
self.assertEqual("demo", img.name)
self.assertEqual("openstack", img.pool)
self.assertEqual("cthulu", img.user)
self.assertEqual("braanes", img.password)
self.assertEqual(["rbd.example.org"], img.servers)
self.assertEqual(imgmodel.FORMAT_RAW, img.format)
def test_equality(self):
img1 = imgmodel.LocalFileImage(
"/var/lib/libvirt/images/demo.qcow2",
imgmodel.FORMAT_QCOW2)
img2 = imgmodel.LocalFileImage(
"/var/lib/libvirt/images/demo.qcow2",
imgmodel.FORMAT_QCOW2)
img3 = imgmodel.LocalFileImage(
"/var/lib/libvirt/images/demo.qcow2",
imgmodel.FORMAT_RAW)
img4 = imgmodel.LocalImage(
"/dev/mapper/vol",
imgmodel.FORMAT_RAW)
img5 = imgmodel.LocalBlockImage(
"/dev/mapper/vol")
self.assertEqual(img1, img1)
self.assertEqual(img1, img2)
self.assertEqual(img1.__hash__(), img2.__hash__())
self.assertNotEqual(img1, img3)
self.assertNotEqual(img4, img5)
def test_stringify(self):
img = imgmodel.RBDImage(
"demo",
"openstack",
"cthulu",
"braanes",
["rbd.example.org"])
msg = str(img)
self.assertTrue(msg.find("braanes") == -1)
self.assertTrue(msg.find("***") != -1)

View File

129
nova/virt/image/model.py Normal file
View File

@ -0,0 +1,129 @@
#
# Copyright (C) 2014 Red Hat, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from oslo_utils import strutils
from nova import exception
FORMAT_RAW = "raw"
FORMAT_QCOW2 = "qcow2"
ALL_FORMATS = [
FORMAT_RAW,
FORMAT_QCOW2,
]
class Image(object):
"""Base class for all image types.
All image types have a format, though for many of
them only a subset of formats will commonly be
used. For example, block devices are almost
always going to be FORMAT_RAW. Though it is in
fact possible from a technical POV to store a
qcow2 data inside a block device, Nova does not
(at this time) make use of such possibilities.
"""
def __init__(self, format):
"""Create a new abstract iamge
:param format: one of the format constants
"""
super(Image, self).__init__()
self.format = format
if format not in ALL_FORMATS:
raise exception.InvalidImageFormat(format=format)
def __repr__(self):
msg = "<" + self.__class__.__name__ + ":" + str(self.__dict__) + ">"
return strutils.mask_password(msg)
def __eq__(self, other):
return ((self.__class__ == other.__class__) and
(self.__dict__ == other.__dict__))
def __hash__(self):
return hash(str(self.__dict__))
class LocalImage(Image):
"""Class for images that are paths within the
local filesystem
"""
def __init__(self, path, format):
"""Create a new local image object
:param path: qualified filename of the image
:param format: one of the format constants
"""
super(LocalImage, self).__init__(format)
self.path = path
class LocalFileImage(LocalImage):
"""Class for images that are files on a locally
accessible filesystem
"""
def __init__(self, path, format):
"""Create a new local file object
:param path: qualified filename of the image
:param format: one of the format constants
"""
super(LocalFileImage, self).__init__(path, format)
class LocalBlockImage(LocalImage):
"""Class for images that are block devices on
the local host
"""
def __init__(self, path):
"""Create a new local file object
:param path: qualified filename of the image
"""
super(LocalBlockImage, self).__init__(path, FORMAT_RAW)
class RBDImage(Image):
"""Class for images that are volumes on a remote
RBD server
"""
def __init__(self, name, pool, user, password, servers):
"""Create a new RBD image object
:param name: name of the image relative to the pool
:param pool: name of the pool holding the image
:param user: username to authenticate as
:param password: credentials for authenticating with
:param servers: list of hostnames for the server
"""
super(RBDImage, self).__init__(FORMAT_RAW)
self.name = name
self.pool = pool
self.user = user
self.password = password
self.servers = servers