Skip to content

Commit

Permalink
PXE driver should rmtree directories it created
Browse files Browse the repository at this point in the history
Baremetal PXE driver was failing to delete the per-instance tftpboot and
image directories which it created when the instance was deleted. This
happened partly because of dangling files within the directory, and
partly because 'unlink' does not remove directories and the error was
squelched.

Now, when destroy()ing an instance, PXE driver will call shutil.rmtree()
on the per-instance directories which it created.

Fixes bug 1101048.

Change-Id: I682d0264288add488ea23e5d5200357b7be52dd9
  • Loading branch information
AevaOnline committed Jan 22, 2013
1 parent a4d608f commit e306bea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
13 changes: 8 additions & 5 deletions nova/tests/baremetal/test_pxe.py
Expand Up @@ -414,10 +414,11 @@ def test_cache_images(self):

def test_destroy_images(self):
self._create_node()
self.mox.StubOutWithMock(os, 'unlink')
self.mox.StubOutWithMock(bm_utils, 'unlink_without_raise')
self.mox.StubOutWithMock(bm_utils, 'rmtree_without_raise')

os.unlink(pxe.get_image_file_path(self.instance))
os.unlink(pxe.get_image_dir_path(self.instance))
bm_utils.unlink_without_raise(pxe.get_image_file_path(self.instance))
bm_utils.rmtree_without_raise(pxe.get_image_dir_path(self.instance))
self.mox.ReplayAll()

self.driver.destroy_images(self.context, self.node, self.instance)
Expand Down Expand Up @@ -482,6 +483,7 @@ def test_deactivate_bootloader(self):
pxe_path = pxe.get_pxe_config_file_path(self.instance)

self.mox.StubOutWithMock(bm_utils, 'unlink_without_raise')
self.mox.StubOutWithMock(bm_utils, 'rmtree_without_raise')
self.mox.StubOutWithMock(pxe, 'get_tftp_image_info')
self.mox.StubOutWithMock(self.driver, '_collect_mac_addresses')

Expand All @@ -493,7 +495,7 @@ def test_deactivate_bootloader(self):
AndReturn(macs)
for mac in macs:
bm_utils.unlink_without_raise(pxe.get_pxe_mac_path(mac))
bm_utils.unlink_without_raise(
bm_utils.rmtree_without_raise(
os.path.join(CONF.baremetal.tftp_root, 'fake-uuid'))
self.mox.ReplayAll()

Expand All @@ -516,6 +518,7 @@ def test_deactivate_bootloader_for_nonexistent_instance(self):
pxe_path = pxe.get_pxe_config_file_path(self.instance)

self.mox.StubOutWithMock(bm_utils, 'unlink_without_raise')
self.mox.StubOutWithMock(bm_utils, 'rmtree_without_raise')
self.mox.StubOutWithMock(pxe, 'get_tftp_image_info')
self.mox.StubOutWithMock(self.driver, '_collect_mac_addresses')

Expand All @@ -524,7 +527,7 @@ def test_deactivate_bootloader_for_nonexistent_instance(self):
bm_utils.unlink_without_raise(pxe_path)
self.driver._collect_mac_addresses(self.context, self.node).\
AndRaise(exception.DBError)
bm_utils.unlink_without_raise(
bm_utils.rmtree_without_raise(
os.path.join(CONF.baremetal.tftp_root, 'fake-uuid'))
self.mox.ReplayAll()

Expand Down
5 changes: 2 additions & 3 deletions nova/virt/baremetal/pxe.py
Expand Up @@ -21,7 +21,6 @@
"""

import os
import shutil

from nova.compute import instance_types
from nova import exception
Expand Down Expand Up @@ -344,7 +343,7 @@ def cache_images(self, context, node, instance,
def destroy_images(self, context, node, instance):
"""Delete instance's image file."""
bm_utils.unlink_without_raise(get_image_file_path(instance))
bm_utils.unlink_without_raise(get_image_dir_path(instance))
bm_utils.rmtree_without_raise(get_image_dir_path(instance))

def activate_bootloader(self, context, node, instance):
"""Configure PXE boot loader for an instance
Expand Down Expand Up @@ -419,7 +418,7 @@ def deactivate_bootloader(self, context, node, instance):
for mac in macs:
bm_utils.unlink_without_raise(get_pxe_mac_path(mac))

bm_utils.unlink_without_raise(
bm_utils.rmtree_without_raise(
os.path.join(CONF.baremetal.tftp_root, instance['uuid']))

def activate_node(self, context, node, instance):
Expand Down
9 changes: 9 additions & 0 deletions nova/virt/baremetal/utils.py
Expand Up @@ -16,6 +16,7 @@
# under the License.

import os
import shutil

from nova.openstack.common import log as logging
from nova.virt.disk import api as disk_api
Expand Down Expand Up @@ -47,6 +48,14 @@ def unlink_without_raise(path):
LOG.exception(_("Failed to unlink %s") % path)


def rmtree_without_raise(path):
try:
if os.path.isdir(path):
shutil.rmtree(path)
except OSError, e:
LOG.warn(_("Failed to remove dir %(path)s, error: %(e)s") % locals())


def write_to_file(path, contents):
with open(path, 'w') as f:
f.write(contents)
Expand Down

0 comments on commit e306bea

Please sign in to comment.