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
This commit is contained in:
Johannes Erdfelt 2012-02-28 05:54:48 +00:00
parent f01b9b8dd2
commit f0d5df523b
10 changed files with 249 additions and 314 deletions

View File

@ -24,9 +24,7 @@ Nova authentication management
""" """
import os import os
import shutil
import string # pylint: disable=W0402 import string # pylint: disable=W0402
import tempfile
import uuid import uuid
import zipfile import zipfile
@ -767,45 +765,44 @@ class AuthManager(object):
pid = Project.safe_id(project) pid = Project.safe_id(project)
private_key, signed_cert = crypto.generate_x509_cert(user.id, pid) private_key, signed_cert = crypto.generate_x509_cert(user.id, pid)
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
zf = os.path.join(tmpdir, "temp.zip") zf = os.path.join(tmpdir, "temp.zip")
zippy = zipfile.ZipFile(zf, 'w') zippy = zipfile.ZipFile(zf, 'w')
if use_dmz and FLAGS.region_list: if use_dmz and FLAGS.region_list:
regions = {} regions = {}
for item in FLAGS.region_list: for item in FLAGS.region_list:
region, _sep, region_host = item.partition("=") region, _sep, region_host = item.partition("=")
regions[region] = region_host regions[region] = region_host
else: else:
regions = {'nova': FLAGS.ec2_host} regions = {'nova': FLAGS.ec2_host}
for region, host in regions.iteritems(): for region, host in regions.iteritems():
rc = self.__generate_rc(user, rc = self.__generate_rc(user,
pid, pid,
use_dmz, use_dmz,
host) host)
zippy.writestr(FLAGS.credential_rc_file % region, rc) zippy.writestr(FLAGS.credential_rc_file % region, rc)
zippy.writestr(FLAGS.credential_key_file, private_key) zippy.writestr(FLAGS.credential_key_file, private_key)
zippy.writestr(FLAGS.credential_cert_file, signed_cert) zippy.writestr(FLAGS.credential_cert_file, signed_cert)
(vpn_ip, vpn_port) = self.get_project_vpn_data(project) (vpn_ip, vpn_port) = self.get_project_vpn_data(project)
if vpn_ip: if vpn_ip:
configfile = open(FLAGS.vpn_client_template, "r") configfile = open(FLAGS.vpn_client_template, "r")
s = string.Template(configfile.read()) s = string.Template(configfile.read())
configfile.close() configfile.close()
config = s.substitute(keyfile=FLAGS.credential_key_file, config = s.substitute(keyfile=FLAGS.credential_key_file,
certfile=FLAGS.credential_cert_file, certfile=FLAGS.credential_cert_file,
ip=vpn_ip, ip=vpn_ip,
port=vpn_port) port=vpn_port)
zippy.writestr(FLAGS.credential_vpn_file, config) zippy.writestr(FLAGS.credential_vpn_file, config)
else: else:
LOG.warn(_("No vpn data for project %s"), pid) LOG.warn(_("No vpn data for project %s"), pid)
zippy.writestr(FLAGS.ca_file, crypto.fetch_ca(pid)) zippy.writestr(FLAGS.ca_file, crypto.fetch_ca(pid))
zippy.close() zippy.close()
with open(zf, 'rb') as f: with open(zf, 'rb') as f:
read_buffer = f.read() read_buffer = f.read()
shutil.rmtree(tmpdir)
return read_buffer return read_buffer
def get_environment_rc(self, user, project=None, use_dmz=True): def get_environment_rc(self, user, project=None, use_dmz=True):

View File

@ -65,36 +65,39 @@ class CloudPipe(object):
def get_encoded_zip(self, project_id): def get_encoded_zip(self, project_id):
# Make a payload.zip # Make a payload.zip
tmpfolder = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
filename = "payload.zip" filename = "payload.zip"
zippath = os.path.join(tmpfolder, filename) zippath = os.path.join(tmpdir, filename)
z = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED) z = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED)
shellfile = open(FLAGS.boot_script_template, "r") shellfile = open(FLAGS.boot_script_template, "r")
s = string.Template(shellfile.read()) s = string.Template(shellfile.read())
shellfile.close() shellfile.close()
boot_script = s.substitute(cc_dmz=FLAGS.ec2_dmz_host, boot_script = s.substitute(cc_dmz=FLAGS.ec2_dmz_host,
cc_port=FLAGS.ec2_port, cc_port=FLAGS.ec2_port,
dmz_net=FLAGS.dmz_net, dmz_net=FLAGS.dmz_net,
dmz_mask=FLAGS.dmz_mask, dmz_mask=FLAGS.dmz_mask,
num_vpn=FLAGS.cnt_vpn_clients) num_vpn=FLAGS.cnt_vpn_clients)
# genvpn, sign csr # genvpn, sign csr
crypto.generate_vpn_files(project_id) crypto.generate_vpn_files(project_id)
z.writestr('autorun.sh', boot_script) z.writestr('autorun.sh', boot_script)
crl = os.path.join(crypto.ca_folder(project_id), 'crl.pem') crl = os.path.join(crypto.ca_folder(project_id), 'crl.pem')
z.write(crl, 'crl.pem') z.write(crl, 'crl.pem')
server_key = os.path.join(crypto.ca_folder(project_id), 'server.key') server_key = os.path.join(crypto.ca_folder(project_id),
z.write(server_key, 'server.key') 'server.key')
ca_crt = os.path.join(crypto.ca_path(project_id)) z.write(server_key, 'server.key')
z.write(ca_crt, 'ca.crt') ca_crt = os.path.join(crypto.ca_path(project_id))
server_crt = os.path.join(crypto.ca_folder(project_id), 'server.crt') z.write(ca_crt, 'ca.crt')
z.write(server_crt, 'server.crt') server_crt = os.path.join(crypto.ca_folder(project_id),
z.close() 'server.crt')
zippy = open(zippath, "r") z.write(server_crt, 'server.crt')
# NOTE(vish): run instances expects encoded userdata, it is decoded z.close()
# in the get_metadata_call. autorun.sh also decodes the zip file, zippy = open(zippath, "r")
# hence the double encoding. # NOTE(vish): run instances expects encoded userdata, it is decoded
encoded = zippy.read().encode("base64").encode("base64") # in the get_metadata_call. autorun.sh also decodes the zip file,
zippy.close() # hence the double encoding.
encoded = zippy.read().encode("base64").encode("base64")
zippy.close()
return encoded return encoded
def launch_vpn_instance(self, project_id, user_id): def launch_vpn_instance(self, project_id, user_id):

View File

@ -175,6 +175,8 @@ def handle_flagfiles_managed(args):
# Do stuff # Do stuff
# Any temporary fils have been removed # Any temporary fils have been removed
''' '''
# NOTE(johannes): Would be nice to use utils.tempdir(), but it
# causes an import loop
tempdir = tempfile.mkdtemp(prefix='nova-conf-') tempdir = tempfile.mkdtemp(prefix='nova-conf-')
try: try:
yield handle_flagfiles(args, tempdir=tempdir) yield handle_flagfiles(args, tempdir=tempdir)

View File

@ -27,9 +27,7 @@ from __future__ import absolute_import
import base64 import base64
import hashlib import hashlib
import os import os
import shutil
import string import string
import tempfile
import Crypto.Cipher.AES import Crypto.Cipher.AES
@ -127,36 +125,26 @@ def _generate_fingerprint(public_key_file):
def generate_fingerprint(public_key): def generate_fingerprint(public_key):
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
try:
pubfile = os.path.join(tmpdir, 'temp.pub')
with open(pubfile, 'w') as f:
f.write(public_key)
return _generate_fingerprint(pubfile)
except exception.ProcessExecutionError:
raise exception.InvalidKeypair()
finally:
try: try:
shutil.rmtree(tmpdir) pubfile = os.path.join(tmpdir, 'temp.pub')
except IOError, e: with open(pubfile, 'w') as f:
LOG.debug(_('Could not remove tmpdir: %s'), str(e)) f.write(public_key)
return _generate_fingerprint(pubfile)
except exception.ProcessExecutionError:
raise exception.InvalidKeypair()
def generate_key_pair(bits=1024): def generate_key_pair(bits=1024):
# what is the magic 65537? # what is the magic 65537?
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
keyfile = os.path.join(tmpdir, 'temp') keyfile = os.path.join(tmpdir, 'temp')
utils.execute('ssh-keygen', '-q', '-b', bits, '-N', '', utils.execute('ssh-keygen', '-q', '-b', bits, '-N', '',
'-t', 'rsa', '-f', keyfile) '-t', 'rsa', '-f', keyfile)
fingerprint = _generate_fingerprint('%s.pub' % (keyfile)) fingerprint = _generate_fingerprint('%s.pub' % (keyfile))
private_key = open(keyfile).read() private_key = open(keyfile).read()
public_key = open(keyfile + '.pub').read() public_key = open(keyfile + '.pub').read()
try:
shutil.rmtree(tmpdir)
except OSError, e:
LOG.debug(_('Could not remove tmpdir: %s'), str(e))
return (private_key, public_key, fingerprint) return (private_key, public_key, fingerprint)
@ -233,19 +221,15 @@ def _user_cert_subject(user_id, project_id):
def generate_x509_cert(user_id, project_id, bits=1024): def generate_x509_cert(user_id, project_id, bits=1024):
"""Generate and sign a cert for user in project.""" """Generate and sign a cert for user in project."""
subject = _user_cert_subject(user_id, project_id) subject = _user_cert_subject(user_id, project_id)
tmpdir = tempfile.mkdtemp()
keyfile = os.path.abspath(os.path.join(tmpdir, 'temp.key'))
csrfile = os.path.join(tmpdir, 'temp.csr')
utils.execute('openssl', 'genrsa', '-out', keyfile, str(bits))
utils.execute('openssl', 'req', '-new', '-key', keyfile, '-out', csrfile,
'-batch', '-subj', subject)
private_key = open(keyfile).read()
csr = open(csrfile).read()
try: with utils.tempdir() as tmpdir:
shutil.rmtree(tmpdir) keyfile = os.path.abspath(os.path.join(tmpdir, 'temp.key'))
except OSError, e: csrfile = os.path.join(tmpdir, 'temp.csr')
LOG.debug(_('Could not remove tmpdir: %s'), str(e)) utils.execute('openssl', 'genrsa', '-out', keyfile, str(bits))
utils.execute('openssl', 'req', '-new', '-key', keyfile, '-out',
csrfile, '-batch', '-subj', subject)
private_key = open(keyfile).read()
csr = open(csrfile).read()
(serial, signed_csr) = sign_csr(csr, project_id) (serial, signed_csr) = sign_csr(csr, project_id)
fname = os.path.join(ca_folder(project_id), 'newcerts/%s.pem' % serial) fname = os.path.join(ca_folder(project_id), 'newcerts/%s.pem' % serial)
@ -298,26 +282,30 @@ def sign_csr(csr_text, project_id=None):
def _sign_csr(csr_text, ca_folder): def _sign_csr(csr_text, ca_folder):
tmpfolder = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
inbound = os.path.join(tmpfolder, 'inbound.csr') inbound = os.path.join(tmpdir, 'inbound.csr')
outbound = os.path.join(tmpfolder, 'outbound.csr') outbound = os.path.join(tmpdir, 'outbound.csr')
csrfile = open(inbound, 'w')
csrfile.write(csr_text) with open(inbound, 'w') as csrfile:
csrfile.close() csrfile.write(csr_text)
LOG.debug(_('Flags path: %s'), ca_folder)
start = os.getcwd() LOG.debug(_('Flags path: %s'), ca_folder)
# Change working dir to CA start = os.getcwd()
if not os.path.exists(ca_folder):
os.makedirs(ca_folder) # Change working dir to CA
os.chdir(ca_folder) if not os.path.exists(ca_folder):
utils.execute('openssl', 'ca', '-batch', '-out', outbound, '-config', os.makedirs(ca_folder)
'./openssl.cnf', '-infiles', inbound)
out, _err = utils.execute('openssl', 'x509', '-in', outbound, os.chdir(ca_folder)
'-serial', '-noout') utils.execute('openssl', 'ca', '-batch', '-out', outbound, '-config',
serial = string.strip(out.rpartition('=')[2]) './openssl.cnf', '-infiles', inbound)
os.chdir(start) out, _err = utils.execute('openssl', 'x509', '-in', outbound,
with open(outbound, 'r') as crtfile: '-serial', '-noout')
return (serial, crtfile.read()) serial = string.strip(out.rpartition('=')[2])
os.chdir(start)
with open(outbound, 'r') as crtfile:
return (serial, crtfile.read())
def _build_cipher(key, iv): def _build_cipher(key, iv):

View File

@ -17,8 +17,6 @@ Tests for Crypto module.
""" """
import os import os
import shutil
import tempfile
import mox import mox
@ -50,9 +48,8 @@ class SymmetricKeyTestCase(test.TestCase):
class X509Test(test.TestCase): class X509Test(test.TestCase):
def test_can_generate_x509(self): def test_can_generate_x509(self):
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
self.flags(ca_path=tmpdir) self.flags(ca_path=tmpdir)
try:
crypto.ensure_ca_filesystem() crypto.ensure_ca_filesystem()
_key, cert_str = crypto.generate_x509_cert('fake', 'fake') _key, cert_str = crypto.generate_x509_cert('fake', 'fake')
@ -70,14 +67,10 @@ class X509Test(test.TestCase):
project_cert_file, '-verbose', signed_cert_file) project_cert_file, '-verbose', signed_cert_file)
self.assertFalse(err) self.assertFalse(err)
finally:
shutil.rmtree(tmpdir)
def test_encrypt_decrypt_x509(self): def test_encrypt_decrypt_x509(self):
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
self.flags(ca_path=tmpdir) self.flags(ca_path=tmpdir)
project_id = "fake" project_id = "fake"
try:
crypto.ensure_ca_filesystem() crypto.ensure_ca_filesystem()
cert = crypto.fetch_ca(project_id) cert = crypto.fetch_ca(project_id)
public_key = os.path.join(tmpdir, "public.pem") public_key = os.path.join(tmpdir, "public.pem")
@ -92,8 +85,6 @@ class X509Test(test.TestCase):
process_input=text) process_input=text)
dec = crypto.decrypt_text(project_id, enc) dec = crypto.decrypt_text(project_id, enc)
self.assertEqual(text, dec) self.assertEqual(text, dec)
finally:
shutil.rmtree(tmpdir)
class RevokeCertsTest(test.TestCase): class RevokeCertsTest(test.TestCase):

View File

@ -17,12 +17,11 @@
# under the License. # under the License.
import contextlib
import cStringIO import cStringIO
import hashlib import hashlib
import logging import logging
import os import os
import shutil
import tempfile
import time import time
from nova import test from nova import test
@ -58,9 +57,8 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertEquals(csum, None) self.assertEquals(csum, None)
def test_read_stored_checksum(self): def test_read_stored_checksum(self):
try: with utils.tempdir() as tmpdir:
dirname = tempfile.mkdtemp() fname = os.path.join(tmpdir, 'aaa')
fname = os.path.join(dirname, 'aaa')
csum_input = 'fdghkfhkgjjksfdgjksjkghsdf' csum_input = 'fdghkfhkgjjksfdgjksjkghsdf'
f = open('%s.sha1' % fname, 'w') f = open('%s.sha1' % fname, 'w')
@ -71,9 +69,6 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertEquals(csum_input, csum_output) self.assertEquals(csum_input, csum_output)
finally:
shutil.rmtree(dirname)
def test_list_base_images(self): def test_list_base_images(self):
listing = ['00000001', listing = ['00000001',
'ephemeral_0_20_None', 'ephemeral_0_20_None',
@ -281,13 +276,17 @@ class ImageCacheManagerTestCase(test.TestCase):
(base_file2, True, False), (base_file2, True, False),
(base_file3, False, True)]) (base_file3, False, True)])
@contextlib.contextmanager
def _intercept_log_messages(self): def _intercept_log_messages(self):
mylog = log.getLogger() try:
stream = cStringIO.StringIO() mylog = log.getLogger()
handler = logging.StreamHandler(stream) stream = cStringIO.StringIO()
handler.setFormatter(log.LegacyNovaFormatter()) handler = logging.StreamHandler(stream)
mylog.logger.addHandler(handler) handler.setFormatter(log.LegacyNovaFormatter())
return mylog, handler, stream mylog.logger.addHandler(handler)
yield stream
finally:
mylog.logger.removeHandler(handler)
def test_verify_checksum(self): def test_verify_checksum(self):
testdata = ('OpenStack Software delivers a massively scalable cloud ' testdata = ('OpenStack Software delivers a massively scalable cloud '
@ -295,74 +294,69 @@ class ImageCacheManagerTestCase(test.TestCase):
img = {'container_format': 'ami', 'id': '42'} img = {'container_format': 'ami', 'id': '42'}
self.flags(checksum_base_images=True) self.flags(checksum_base_images=True)
mylog, handler, stream = self._intercept_log_messages()
try: with self._intercept_log_messages() as stream:
dirname = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
fname = os.path.join(dirname, 'aaa') fname = os.path.join(tmpdir, 'aaa')
f = open(fname, 'w') f = open(fname, 'w')
f.write(testdata) f.write(testdata)
f.close() f.close()
# Checksum is valid # Checksum is valid
f = open('%s.sha1' % fname, 'w') f = open('%s.sha1' % fname, 'w')
csum = hashlib.sha1() csum = hashlib.sha1()
csum.update(testdata) csum.update(testdata)
f.write(csum.hexdigest()) f.write(csum.hexdigest())
f.close() f.close()
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
res = image_cache_manager._verify_checksum(img, fname) res = image_cache_manager._verify_checksum(img, fname)
self.assertTrue(res) self.assertTrue(res)
# Checksum is invalid # Checksum is invalid
f = open('%s.sha1' % fname, 'w') f = open('%s.sha1' % fname, 'w')
f.write('banana') f.write('banana')
f.close() f.close()
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
res = image_cache_manager._verify_checksum(img, fname) res = image_cache_manager._verify_checksum(img, fname)
self.assertFalse(res) self.assertFalse(res)
self.assertNotEqual(stream.getvalue().find('image verification ' log = stream.getvalue()
'failed'), -1) self.assertNotEqual(log.find('image verification failed'), -1)
# Checksum file missing # Checksum file missing
os.remove('%s.sha1' % fname) os.remove('%s.sha1' % fname)
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
res = image_cache_manager._verify_checksum(img, fname) res = image_cache_manager._verify_checksum(img, fname)
self.assertEquals(res, None) self.assertEquals(res, None)
# Checksum requests for a file with no checksum now have the # Checksum requests for a file with no checksum now have the
# side effect of creating the checksum # side effect of creating the checksum
self.assertTrue(os.path.exists('%s.sha1' % fname)) self.assertTrue(os.path.exists('%s.sha1' % fname))
finally: @contextlib.contextmanager
shutil.rmtree(dirname) def _make_base_file(self, checksum=True):
mylog.logger.removeHandler(handler)
def _make_base_file(checksum=True):
"""Make a base file for testing.""" """Make a base file for testing."""
dirname = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
fname = os.path.join(dirname, 'aaa') fname = os.path.join(tmpdir, 'aaa')
base_file = open(fname, 'w') base_file = open(fname, 'w')
base_file.write('data') base_file.write('data')
base_file.close() base_file.close()
base_file = open(fname, 'r') base_file = open(fname, 'r')
if checksum: if checksum:
checksum_file = open('%s.sha1' % fname, 'w') checksum_file = open('%s.sha1' % fname, 'w')
checksum_file.write(utils.hash_file(base_file)) checksum_file.write(utils.hash_file(base_file))
checksum_file.close() checksum_file.close()
base_file.close() base_file.close()
return dirname, fname yield fname
def test_remove_base_file(self): def test_remove_base_file(self):
dirname, fname = self._make_base_file() with self._make_base_file() as fname:
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager._remove_base_file(fname) image_cache_manager._remove_base_file(fname)
@ -377,12 +371,8 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertFalse(os.path.exists(fname)) self.assertFalse(os.path.exists(fname))
self.assertFalse(os.path.exists('%s.sha1' % fname)) self.assertFalse(os.path.exists('%s.sha1' % fname))
finally:
shutil.rmtree(dirname)
def test_remove_base_file_original(self): def test_remove_base_file_original(self):
dirname, fname = self._make_base_file() with self._make_base_file() as fname:
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.originals = [fname] image_cache_manager.originals = [fname]
image_cache_manager._remove_base_file(fname) image_cache_manager._remove_base_file(fname)
@ -405,51 +395,38 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertFalse(os.path.exists(fname)) self.assertFalse(os.path.exists(fname))
self.assertFalse(os.path.exists('%s.sha1' % fname)) self.assertFalse(os.path.exists('%s.sha1' % fname))
finally:
shutil.rmtree(dirname)
def test_remove_base_file_dne(self): def test_remove_base_file_dne(self):
# This test is solely to execute the "does not exist" code path. We # This test is solely to execute the "does not exist" code path. We
# don't expect the method being tested to do anything in this case. # don't expect the method being tested to do anything in this case.
dirname = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
try: fname = os.path.join(tmpdir, 'aaa')
fname = os.path.join(dirname, 'aaa')
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager._remove_base_file(fname) image_cache_manager._remove_base_file(fname)
finally:
shutil.rmtree(dirname)
def test_remove_base_file_oserror(self): def test_remove_base_file_oserror(self):
dirname = tempfile.mkdtemp() with self._intercept_log_messages() as stream:
fname = os.path.join(dirname, 'aaa') with utils.tempdir() as tmpdir:
mylog, handler, stream = self._intercept_log_messages() fname = os.path.join(tmpdir, 'aaa')
try: os.mkdir(fname)
os.mkdir(fname) os.utime(fname, (-1, time.time() - 3601))
os.utime(fname, (-1, time.time() - 3601))
# This will raise an OSError because of file permissions # This will raise an OSError because of file permissions
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager._remove_base_file(fname) image_cache_manager._remove_base_file(fname)
self.assertTrue(os.path.exists(fname)) self.assertTrue(os.path.exists(fname))
self.assertNotEqual(stream.getvalue().find('Failed to remove'), self.assertNotEqual(stream.getvalue().find('Failed to remove'),
-1) -1)
finally:
shutil.rmtree(dirname)
mylog.logger.removeHandler(handler)
def test_handle_base_image_unused(self): def test_handle_base_image_unused(self):
img = {'container_format': 'ami', img = {'container_format': 'ami',
'id': '123', 'id': '123',
'uuid': '1234-4567-2378'} 'uuid': '1234-4567-2378'}
dirname, fname = self._make_base_file() with self._make_base_file() as fname:
os.utime(fname, (-1, time.time() - 3601)) os.utime(fname, (-1, time.time() - 3601))
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.unexplained_images = [fname] image_cache_manager.unexplained_images = [fname]
image_cache_manager._handle_base_image(img, fname) image_cache_manager._handle_base_image(img, fname)
@ -459,18 +436,14 @@ class ImageCacheManagerTestCase(test.TestCase):
[fname]) [fname])
self.assertEquals(image_cache_manager.corrupt_base_files, []) self.assertEquals(image_cache_manager.corrupt_base_files, [])
finally:
shutil.rmtree(dirname)
def test_handle_base_image_used(self): def test_handle_base_image_used(self):
img = {'container_format': 'ami', img = {'container_format': 'ami',
'id': '123', 'id': '123',
'uuid': '1234-4567-2378'} 'uuid': '1234-4567-2378'}
dirname, fname = self._make_base_file() with self._make_base_file() as fname:
os.utime(fname, (-1, time.time() - 3601)) os.utime(fname, (-1, time.time() - 3601))
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.unexplained_images = [fname] image_cache_manager.unexplained_images = [fname]
image_cache_manager.used_images = {'123': (1, 0, ['banana-42'])} image_cache_manager.used_images = {'123': (1, 0, ['banana-42'])}
@ -480,18 +453,14 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertEquals(image_cache_manager.removable_base_files, []) self.assertEquals(image_cache_manager.removable_base_files, [])
self.assertEquals(image_cache_manager.corrupt_base_files, []) self.assertEquals(image_cache_manager.corrupt_base_files, [])
finally:
shutil.rmtree(dirname)
def test_handle_base_image_used_remotely(self): def test_handle_base_image_used_remotely(self):
img = {'container_format': 'ami', img = {'container_format': 'ami',
'id': '123', 'id': '123',
'uuid': '1234-4567-2378'} 'uuid': '1234-4567-2378'}
dirname, fname = self._make_base_file() with self._make_base_file() as fname:
os.utime(fname, (-1, time.time() - 3601)) os.utime(fname, (-1, time.time() - 3601))
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.used_images = {'123': (0, 1, ['banana-42'])} image_cache_manager.used_images = {'123': (0, 1, ['banana-42'])}
image_cache_manager._handle_base_image(img, None) image_cache_manager._handle_base_image(img, None)
@ -500,9 +469,6 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertEquals(image_cache_manager.removable_base_files, []) self.assertEquals(image_cache_manager.removable_base_files, [])
self.assertEquals(image_cache_manager.corrupt_base_files, []) self.assertEquals(image_cache_manager.corrupt_base_files, [])
finally:
shutil.rmtree(dirname)
def test_handle_base_image_absent(self): def test_handle_base_image_absent(self):
"""Ensure we warn for use of a missing base image.""" """Ensure we warn for use of a missing base image."""
@ -510,9 +476,7 @@ class ImageCacheManagerTestCase(test.TestCase):
'id': '123', 'id': '123',
'uuid': '1234-4567-2378'} 'uuid': '1234-4567-2378'}
mylog, handler, stream = self._intercept_log_messages() with self._intercept_log_messages() as stream:
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.used_images = {'123': (1, 0, ['banana-42'])} image_cache_manager.used_images = {'123': (1, 0, ['banana-42'])}
image_cache_manager._handle_base_image(img, None) image_cache_manager._handle_base_image(img, None)
@ -523,18 +487,14 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertNotEqual(stream.getvalue().find('an absent base file'), self.assertNotEqual(stream.getvalue().find('an absent base file'),
-1) -1)
finally:
mylog.logger.removeHandler(handler)
def test_handle_base_image_used_missing(self): def test_handle_base_image_used_missing(self):
img = {'container_format': 'ami', img = {'container_format': 'ami',
'id': '123', 'id': '123',
'uuid': '1234-4567-2378'} 'uuid': '1234-4567-2378'}
dirname = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
fname = os.path.join(dirname, 'aaa') fname = os.path.join(tmpdir, 'aaa')
try:
image_cache_manager = imagecache.ImageCacheManager() image_cache_manager = imagecache.ImageCacheManager()
image_cache_manager.unexplained_images = [fname] image_cache_manager.unexplained_images = [fname]
image_cache_manager.used_images = {'123': (1, 0, ['banana-42'])} image_cache_manager.used_images = {'123': (1, 0, ['banana-42'])}
@ -544,17 +504,12 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertEquals(image_cache_manager.removable_base_files, []) self.assertEquals(image_cache_manager.removable_base_files, [])
self.assertEquals(image_cache_manager.corrupt_base_files, []) self.assertEquals(image_cache_manager.corrupt_base_files, [])
finally:
shutil.rmtree(dirname)
def test_handle_base_image_checksum_fails(self): def test_handle_base_image_checksum_fails(self):
img = {'container_format': 'ami', img = {'container_format': 'ami',
'id': '123', 'id': '123',
'uuid': '1234-4567-2378'} 'uuid': '1234-4567-2378'}
dirname, fname = self._make_base_file() with self._make_base_file() as fname:
try:
f = open(fname, 'w') f = open(fname, 'w')
f.write('banana') f.write('banana')
f.close() f.close()
@ -569,9 +524,6 @@ class ImageCacheManagerTestCase(test.TestCase):
self.assertEquals(image_cache_manager.corrupt_base_files, self.assertEquals(image_cache_manager.corrupt_base_files,
[fname]) [fname])
finally:
shutil.rmtree(dirname)
def test_verify_base_images(self): def test_verify_base_images(self):
self.flags(instances_path='/instance_path') self.flags(instances_path='/instance_path')
self.flags(remove_unused_base_images=True) self.flags(remove_unused_base_images=True)

View File

@ -1050,26 +1050,25 @@ class LibvirtConnTestCase(test.TestCase):
def test_pre_block_migration_works_correctly(self): def test_pre_block_migration_works_correctly(self):
"""Confirms pre_block_migration works correctly.""" """Confirms pre_block_migration works correctly."""
# Replace instances_path since this testcase creates tmpfile # Replace instances_path since this testcase creates tmpfile
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
self.flags(instances_path=tmpdir) self.flags(instances_path=tmpdir)
# Test data # Test data
instance_ref = db.instance_create(self.context, self.test_instance) instance_ref = db.instance_create(self.context, self.test_instance)
dummyjson = ('[{"path": "%s/disk", "disk_size": "10737418240",' dummyjson = ('[{"path": "%s/disk", "disk_size": "10737418240",'
' "type": "raw", "backing_file": ""}]') ' "type": "raw", "backing_file": ""}]')
# Preparing mocks # Preparing mocks
# qemu-img should be mockd since test environment might not have # qemu-img should be mockd since test environment might not have
# large disk space. # large disk space.
self.mox.ReplayAll() self.mox.ReplayAll()
conn = connection.LibvirtConnection(False) conn = connection.LibvirtConnection(False)
conn.pre_block_migration(self.context, instance_ref, conn.pre_block_migration(self.context, instance_ref,
dummyjson % tmpdir) dummyjson % tmpdir)
self.assertTrue(os.path.exists('%s/%s/' % self.assertTrue(os.path.exists('%s/%s/' %
(tmpdir, instance_ref.name))) (tmpdir, instance_ref.name)))
shutil.rmtree(tmpdir)
db.instance_destroy(self.context, instance_ref['id']) db.instance_destroy(self.context, instance_ref['id'])
@test.skip_if(missing_libvirt(), "Test requires libvirt") @test.skip_if(missing_libvirt(), "Test requires libvirt")
@ -1926,13 +1925,10 @@ disk size: 4.4M''', ''))
libvirt_utils.mkfs('swap', '/my/swap/block/dev') libvirt_utils.mkfs('swap', '/my/swap/block/dev')
def test_ensure_tree(self): def test_ensure_tree(self):
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
try:
testdir = '%s/foo/bar/baz' % (tmpdir,) testdir = '%s/foo/bar/baz' % (tmpdir,)
libvirt_utils.ensure_tree(testdir) libvirt_utils.ensure_tree(testdir)
self.assertTrue(os.path.isdir(testdir)) self.assertTrue(os.path.isdir(testdir))
finally:
shutil.rmtree(tmpdir)
def test_write_to_file(self): def test_write_to_file(self):
dst_fd, dst_path = tempfile.mkstemp() dst_fd, dst_path = tempfile.mkstemp()

View File

@ -31,9 +31,11 @@ import pyclbr
import random import random
import re import re
import shlex import shlex
import shutil
import socket import socket
import struct import struct
import sys import sys
import tempfile
import time import time
import types import types
import uuid import uuid
@ -1543,3 +1545,15 @@ def temporary_chown(path, owner_uid=None):
finally: finally:
if orig_uid != owner_uid: if orig_uid != owner_uid:
execute('chown', orig_uid, path, run_as_root=True) execute('chown', orig_uid, path, run_as_root=True)
@contextlib.contextmanager
def tempdir(**kwargs):
tmpdir = tempfile.mkdtemp(**kwargs)
try:
yield tmpdir
finally:
try:
shutil.rmtree(tmpdir)
except OSError, e:
LOG.debug(_('Could not remove tmpdir: %s'), str(e))

View File

@ -46,7 +46,6 @@ import multiprocessing
import os import os
import shutil import shutil
import sys import sys
import tempfile
import uuid import uuid
from eventlet import greenthread from eventlet import greenthread
@ -622,23 +621,21 @@ class LibvirtConnection(driver.ComputeDriver):
disk_path = source.get('file') disk_path = source.get('file')
# Export the snapshot to a raw image # Export the snapshot to a raw image
temp_dir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
try: try:
out_path = os.path.join(temp_dir, snapshot_name) out_path = os.path.join(tmpdir, snapshot_name)
libvirt_utils.extract_snapshot(disk_path, source_format, libvirt_utils.extract_snapshot(disk_path, source_format,
snapshot_name, out_path, snapshot_name, out_path,
image_format) image_format)
# Upload that image to the image service # Upload that image to the image service
with libvirt_utils.file_open(out_path) as image_file: with libvirt_utils.file_open(out_path) as image_file:
image_service.update(context, image_service.update(context,
image_href, image_href,
metadata, metadata,
image_file) image_file)
finally: finally:
# Clean up snapshot_ptr.delete(0)
shutil.rmtree(temp_dir)
snapshot_ptr.delete(0)
@exception.wrap_exception() @exception.wrap_exception()
def reboot(self, instance, network_info, reboot_type=None, xml=None): def reboot(self, instance, network_info, reboot_type=None, xml=None):

View File

@ -25,7 +25,6 @@ import json
import os import os
import pickle import pickle
import re import re
import tempfile
import time import time
import urllib import urllib
import urlparse import urlparse
@ -1750,8 +1749,7 @@ def _mounted_processing(device, key, net, metadata):
"""Callback which runs with the image VDI attached""" """Callback which runs with the image VDI attached"""
# NB: Partition 1 hardcoded # NB: Partition 1 hardcoded
dev_path = utils.make_dev_path(device, partition=1) dev_path = utils.make_dev_path(device, partition=1)
tmpdir = tempfile.mkdtemp() with utils.tempdir() as tmpdir:
try:
# Mount only Linux filesystems, to avoid disturbing NTFS images # Mount only Linux filesystems, to avoid disturbing NTFS images
err = _mount_filesystem(dev_path, tmpdir) err = _mount_filesystem(dev_path, tmpdir)
if not err: if not err:
@ -1770,9 +1768,6 @@ def _mounted_processing(device, key, net, metadata):
else: else:
LOG.info(_('Failed to mount filesystem (expected for ' LOG.info(_('Failed to mount filesystem (expected for '
'non-linux instances): %s') % err) 'non-linux instances): %s') % err)
finally:
# remove temporary directory
os.rmdir(tmpdir)
def _prepare_injectables(inst, networks_info): def _prepare_injectables(inst, networks_info):