Merge "libvirt: reworks configdrive creation"
This commit is contained in:
@@ -5685,6 +5685,34 @@ class LibvirtConnTestCase(test.TestCase,
|
||||
conn._create_ephemeral('/dev/something', 20, 'myVol', 'linux',
|
||||
is_block_dev=True)
|
||||
|
||||
def test_create_configdrive(self):
|
||||
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
|
||||
instance = db.instance_create(self.context, self.test_instance)
|
||||
network_info = _fake_network_info(self.stubs, 1)
|
||||
files = {'/etc/foo.conf': 'raw'}
|
||||
target = '/whateverfile/path'
|
||||
|
||||
self.mox.StubOutWithMock(instance_metadata.InstanceMetadata,
|
||||
'__init__')
|
||||
self.mox.StubOutWithMock(configdrive, 'ConfigDriveBuilder')
|
||||
self.mox.StubOutWithMock(configdrive.ConfigDriveBuilder, 'make_drive')
|
||||
|
||||
instance_metadata.InstanceMetadata.__init__(instance,
|
||||
content=files,
|
||||
extra_md={'admin_pass': 'fake'},
|
||||
network_info=network_info)
|
||||
cdb = self.mox.CreateMockAnything()
|
||||
m = configdrive.ConfigDriveBuilder(instance_md=mox.IgnoreArg())
|
||||
m.AndReturn(cdb)
|
||||
# __enter__ and __exit__ are required by "with"
|
||||
cdb.__enter__().AndReturn(cdb)
|
||||
cdb.make_drive(target)
|
||||
cdb.__exit__(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()
|
||||
).AndReturn(None)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
conn._create_configdrive(target, instance, 'fake', files, network_info)
|
||||
|
||||
def test_create_swap_default(self):
|
||||
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
|
||||
self.mox.StubOutWithMock(utils, 'execute')
|
||||
@@ -10765,8 +10793,7 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
def test_rescue_config_drive_rbd(self):
|
||||
CONF.set_override('images_type', 'rbd', 'libvirt')
|
||||
instance = self._create_instance()
|
||||
uuid = instance.uuid
|
||||
configdrive_path = uuid + '/disk.config.rescue'
|
||||
rescue_password = 'fake_password'
|
||||
|
||||
dummyxml = ("<domain type='kvm'><name>instance-0000000a</name>"
|
||||
"<devices>"
|
||||
@@ -10791,10 +10818,6 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
self.mox.StubOutWithMock(libvirt_utils, 'write_to_file')
|
||||
self.mox.StubOutWithMock(imagebackend.Backend, 'image')
|
||||
self.mox.StubOutWithMock(imagebackend.Image, 'cache')
|
||||
self.mox.StubOutWithMock(instance_metadata.InstanceMetadata,
|
||||
'__init__')
|
||||
self.mox.StubOutWithMock(configdrive, 'ConfigDriveBuilder')
|
||||
self.mox.StubOutWithMock(configdrive.ConfigDriveBuilder, 'make_drive')
|
||||
self.mox.StubOutWithMock(self.libvirtconnection, '_get_guest_xml')
|
||||
self.mox.StubOutWithMock(self.libvirtconnection, '_destroy')
|
||||
self.mox.StubOutWithMock(self.libvirtconnection, '_create_domain')
|
||||
@@ -10826,24 +10849,14 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
project_id=mox.IgnoreArg(),
|
||||
size=None, user_id=mox.IgnoreArg())
|
||||
|
||||
instance_metadata.InstanceMetadata.__init__(mox.IgnoreArg(),
|
||||
content=mox.IgnoreArg(),
|
||||
extra_md=mox.IgnoreArg(),
|
||||
network_info=mox.IgnoreArg())
|
||||
cdb = self.mox.CreateMockAnything()
|
||||
m = configdrive.ConfigDriveBuilder(instance_md=mox.IgnoreArg())
|
||||
m.AndReturn(cdb)
|
||||
# __enter__ and __exit__ are required by "with"
|
||||
cdb.__enter__().AndReturn(cdb)
|
||||
cdb.make_drive(mox.Regex(configdrive_path))
|
||||
cdb.__exit__(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()
|
||||
).AndReturn(None)
|
||||
|
||||
imagebackend.Backend.image(instance, 'disk.config.rescue', None
|
||||
).AndReturn(fake_imagebackend.Rbd())
|
||||
imagebackend.Image.cache(fetch_func=mox.IgnoreArg(),
|
||||
context=mox.IgnoreArg(),
|
||||
filename='disk.config.rescue')
|
||||
filename='disk.config.rescue',
|
||||
instance=instance,
|
||||
admin_pass='fake_password',
|
||||
files=None,
|
||||
network_info=network_info)
|
||||
|
||||
image_meta = {'id': 'fake', 'name': 'fake'}
|
||||
self.libvirtconnection._get_guest_xml(mox.IgnoreArg(), instance,
|
||||
@@ -10856,16 +10869,13 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
rescue_password = 'fake_password'
|
||||
|
||||
self.libvirtconnection.rescue(self.context, instance, network_info,
|
||||
image_meta, rescue_password)
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_rescue_config_drive(self):
|
||||
instance = self._create_instance()
|
||||
uuid = instance.uuid
|
||||
configdrive_path = uuid + '/disk.config.rescue'
|
||||
rescue_password = 'fake_password'
|
||||
dummyxml = ("<domain type='kvm'><name>instance-0000000a</name>"
|
||||
"<devices>"
|
||||
"<disk type='file'><driver name='qemu' type='raw'/>"
|
||||
@@ -10882,10 +10892,6 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
self.mox.StubOutWithMock(libvirt_utils, 'write_to_file')
|
||||
self.mox.StubOutWithMock(imagebackend.Backend, 'image')
|
||||
self.mox.StubOutWithMock(imagebackend.Image, 'cache')
|
||||
self.mox.StubOutWithMock(instance_metadata.InstanceMetadata,
|
||||
'__init__')
|
||||
self.mox.StubOutWithMock(configdrive, 'ConfigDriveBuilder')
|
||||
self.mox.StubOutWithMock(configdrive.ConfigDriveBuilder, 'make_drive')
|
||||
self.mox.StubOutWithMock(self.libvirtconnection, '_get_guest_xml')
|
||||
self.mox.StubOutWithMock(self.libvirtconnection, '_destroy')
|
||||
self.mox.StubOutWithMock(self.libvirtconnection, '_create_domain')
|
||||
@@ -10917,24 +10923,14 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
project_id=mox.IgnoreArg(),
|
||||
size=None, user_id=mox.IgnoreArg())
|
||||
|
||||
instance_metadata.InstanceMetadata.__init__(mox.IgnoreArg(),
|
||||
content=mox.IgnoreArg(),
|
||||
extra_md=mox.IgnoreArg(),
|
||||
network_info=mox.IgnoreArg())
|
||||
cdb = self.mox.CreateMockAnything()
|
||||
m = configdrive.ConfigDriveBuilder(instance_md=mox.IgnoreArg())
|
||||
m.AndReturn(cdb)
|
||||
# __enter__ and __exit__ are required by "with"
|
||||
cdb.__enter__().AndReturn(cdb)
|
||||
cdb.make_drive(mox.Regex(configdrive_path))
|
||||
cdb.__exit__(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()
|
||||
).AndReturn(None)
|
||||
|
||||
imagebackend.Backend.image(instance, 'disk.config.rescue', 'raw'
|
||||
).AndReturn(fake_imagebackend.Raw())
|
||||
imagebackend.Image.cache(fetch_func=mox.IgnoreArg(),
|
||||
context=mox.IgnoreArg(),
|
||||
filename='disk.config.rescue')
|
||||
filename='disk.config.rescue',
|
||||
instance=instance,
|
||||
admin_pass='fake_password',
|
||||
files=None,
|
||||
network_info=network_info)
|
||||
|
||||
image_meta = {'id': 'fake', 'name': 'fake'}
|
||||
self.libvirtconnection._get_guest_xml(mox.IgnoreArg(), instance,
|
||||
@@ -10947,8 +10943,6 @@ class LibvirtDriverTestCase(test.TestCase):
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
rescue_password = 'fake_password'
|
||||
|
||||
self.libvirtconnection.rescue(self.context, instance, network_info,
|
||||
image_meta, rescue_password)
|
||||
self.mox.VerifyAll()
|
||||
|
||||
@@ -2717,6 +2717,30 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
libvirt_utils.create_image('raw', target, '%dM' % swap_mb)
|
||||
utils.mkfs('swap', target)
|
||||
|
||||
@staticmethod
|
||||
def _create_configdrive(target, instance, admin_pass, files,
|
||||
network_info, max_size=None):
|
||||
extra_md = {}
|
||||
if admin_pass:
|
||||
extra_md['admin_pass'] = admin_pass
|
||||
|
||||
inst_md = instance_metadata.InstanceMetadata(instance,
|
||||
content=files, extra_md=extra_md,
|
||||
network_info=network_info)
|
||||
|
||||
with configdrive.ConfigDriveBuilder(
|
||||
instance_md=inst_md) as cdb:
|
||||
LOG.info(_LI('Creating config drive at %(path)s'),
|
||||
{'path': target}, instance=instance)
|
||||
|
||||
try:
|
||||
cdb.make_drive(target)
|
||||
except processutils.ProcessExecutionError as e:
|
||||
with excutils.save_and_reraise_exception():
|
||||
LOG.error(_LE('Creating config drive failed '
|
||||
'with error: %s'),
|
||||
e, instance=instance)
|
||||
|
||||
@staticmethod
|
||||
def _get_console_log_path(instance):
|
||||
return os.path.join(libvirt_utils.get_instance_path(instance),
|
||||
@@ -2956,36 +2980,15 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
# Config drive
|
||||
if configdrive.required_by(instance):
|
||||
LOG.info(_LI('Using config drive'), instance=instance)
|
||||
extra_md = {}
|
||||
if admin_pass:
|
||||
extra_md['admin_pass'] = admin_pass
|
||||
|
||||
inst_md = instance_metadata.InstanceMetadata(instance,
|
||||
content=files, extra_md=extra_md, network_info=network_info)
|
||||
with configdrive.ConfigDriveBuilder(instance_md=inst_md) as cdb:
|
||||
configdrive_path = self._get_disk_config_path(instance, suffix)
|
||||
LOG.info(_LI('Creating config drive at %(path)s'),
|
||||
{'path': configdrive_path}, instance=instance)
|
||||
|
||||
try:
|
||||
cdb.make_drive(configdrive_path)
|
||||
except processutils.ProcessExecutionError as e:
|
||||
with excutils.save_and_reraise_exception():
|
||||
LOG.error(_LE('Creating config drive failed '
|
||||
'with error: %s'),
|
||||
e, instance=instance)
|
||||
|
||||
def dummy_fetch_func(target, *args, **kwargs):
|
||||
# NOTE(sileht): this is never called because the
|
||||
# the target have already been created by
|
||||
# cdb.make_drive call
|
||||
pass
|
||||
|
||||
image_type = self._get_configdrive_image_type()
|
||||
backend = image('disk.config', image_type)
|
||||
backend.cache(fetch_func=dummy_fetch_func,
|
||||
context=context,
|
||||
filename='disk.config' + suffix)
|
||||
image_type = self._get_configdrive_image_type()
|
||||
backend = image('disk.config', image_type)
|
||||
backend.cache(fetch_func=self._create_configdrive,
|
||||
filename='disk.config' + suffix,
|
||||
instance=instance,
|
||||
admin_pass=admin_pass,
|
||||
files=files,
|
||||
network_info=network_info)
|
||||
|
||||
# File injection only if needed
|
||||
elif inject_files and CONF.libvirt.inject_partition != -2:
|
||||
|
||||
Reference in New Issue
Block a user