From f0d5df523b982ef1737dc0ee2e698b13041af64c Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Tue, 28 Feb 2012 05:54:48 +0000 Subject: Add utils.tempdir() context manager for easy temp dirs Fixes bug 883323 (and others) Users of tempfile.mkdtemp() need to make sure the directory is cleaned up when it's done being used. Unfortunately, not all of the code does so at all, or safely (by using a try/finally block). Change-Id: I270109d83efec4f8b3dd954021493f4d96c6ab79 --- nova/virt/libvirt/connection.py | 33 +++++++++++++++------------------ nova/virt/xenapi/vm_utils.py | 7 +------ 2 files changed, 16 insertions(+), 24 deletions(-) (limited to 'nova/virt') diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index c531c2cc8..9ee098507 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -46,7 +46,6 @@ import multiprocessing import os import shutil import sys -import tempfile import uuid from eventlet import greenthread @@ -622,23 +621,21 @@ class LibvirtConnection(driver.ComputeDriver): disk_path = source.get('file') # Export the snapshot to a raw image - temp_dir = tempfile.mkdtemp() - try: - out_path = os.path.join(temp_dir, snapshot_name) - libvirt_utils.extract_snapshot(disk_path, source_format, - snapshot_name, out_path, - image_format) - # Upload that image to the image service - with libvirt_utils.file_open(out_path) as image_file: - image_service.update(context, - image_href, - metadata, - image_file) - - finally: - # Clean up - shutil.rmtree(temp_dir) - snapshot_ptr.delete(0) + with utils.tempdir() as tmpdir: + try: + out_path = os.path.join(tmpdir, snapshot_name) + libvirt_utils.extract_snapshot(disk_path, source_format, + snapshot_name, out_path, + image_format) + # Upload that image to the image service + with libvirt_utils.file_open(out_path) as image_file: + image_service.update(context, + image_href, + metadata, + image_file) + + finally: + snapshot_ptr.delete(0) @exception.wrap_exception() def reboot(self, instance, network_info, reboot_type=None, xml=None): diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index 3c5264f97..83103d6f2 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -25,7 +25,6 @@ import json import os import pickle import re -import tempfile import time import urllib import urlparse @@ -1750,8 +1749,7 @@ def _mounted_processing(device, key, net, metadata): """Callback which runs with the image VDI attached""" # NB: Partition 1 hardcoded dev_path = utils.make_dev_path(device, partition=1) - tmpdir = tempfile.mkdtemp() - try: + with utils.tempdir() as tmpdir: # Mount only Linux filesystems, to avoid disturbing NTFS images err = _mount_filesystem(dev_path, tmpdir) if not err: @@ -1770,9 +1768,6 @@ def _mounted_processing(device, key, net, metadata): else: LOG.info(_('Failed to mount filesystem (expected for ' 'non-linux instances): %s') % err) - finally: - # remove temporary directory - os.rmdir(tmpdir) def _prepare_injectables(inst, networks_info): -- cgit