diff options
author | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2012-02-28 05:54:48 +0000 |
---|---|---|
committer | Johannes Erdfelt <johannes.erdfelt@rackspace.com> | 2012-02-28 05:56:58 +0000 |
commit | f0d5df523b982ef1737dc0ee2e698b13041af64c (patch) | |
tree | a2a8cd2c8ca465aa644fc4eb1a3162224b0d7124 /nova/cloudpipe | |
parent | f01b9b8dd25d763e652259a0f99264d93661b29f (diff) | |
download | nova-f0d5df523b982ef1737dc0ee2e698b13041af64c.tar.gz |
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
Diffstat (limited to 'nova/cloudpipe')
-rw-r--r-- | nova/cloudpipe/pipelib.py | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/nova/cloudpipe/pipelib.py b/nova/cloudpipe/pipelib.py index 4e5f7d4ba1..70c28d463e 100644 --- a/nova/cloudpipe/pipelib.py +++ b/nova/cloudpipe/pipelib.py @@ -65,36 +65,39 @@ class CloudPipe(object): def get_encoded_zip(self, project_id): # Make a payload.zip - tmpfolder = tempfile.mkdtemp() - filename = "payload.zip" - zippath = os.path.join(tmpfolder, filename) - z = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED) - shellfile = open(FLAGS.boot_script_template, "r") - s = string.Template(shellfile.read()) - shellfile.close() - boot_script = s.substitute(cc_dmz=FLAGS.ec2_dmz_host, - cc_port=FLAGS.ec2_port, - dmz_net=FLAGS.dmz_net, - dmz_mask=FLAGS.dmz_mask, - num_vpn=FLAGS.cnt_vpn_clients) - # genvpn, sign csr - crypto.generate_vpn_files(project_id) - z.writestr('autorun.sh', boot_script) - crl = os.path.join(crypto.ca_folder(project_id), 'crl.pem') - z.write(crl, 'crl.pem') - server_key = os.path.join(crypto.ca_folder(project_id), 'server.key') - z.write(server_key, 'server.key') - ca_crt = os.path.join(crypto.ca_path(project_id)) - z.write(ca_crt, 'ca.crt') - server_crt = os.path.join(crypto.ca_folder(project_id), 'server.crt') - z.write(server_crt, 'server.crt') - z.close() - zippy = open(zippath, "r") - # NOTE(vish): run instances expects encoded userdata, it is decoded - # in the get_metadata_call. autorun.sh also decodes the zip file, - # hence the double encoding. - encoded = zippy.read().encode("base64").encode("base64") - zippy.close() + with utils.tempdir() as tmpdir: + filename = "payload.zip" + zippath = os.path.join(tmpdir, filename) + z = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED) + shellfile = open(FLAGS.boot_script_template, "r") + s = string.Template(shellfile.read()) + shellfile.close() + boot_script = s.substitute(cc_dmz=FLAGS.ec2_dmz_host, + cc_port=FLAGS.ec2_port, + dmz_net=FLAGS.dmz_net, + dmz_mask=FLAGS.dmz_mask, + num_vpn=FLAGS.cnt_vpn_clients) + # genvpn, sign csr + crypto.generate_vpn_files(project_id) + z.writestr('autorun.sh', boot_script) + crl = os.path.join(crypto.ca_folder(project_id), 'crl.pem') + z.write(crl, 'crl.pem') + server_key = os.path.join(crypto.ca_folder(project_id), + 'server.key') + z.write(server_key, 'server.key') + ca_crt = os.path.join(crypto.ca_path(project_id)) + z.write(ca_crt, 'ca.crt') + server_crt = os.path.join(crypto.ca_folder(project_id), + 'server.crt') + z.write(server_crt, 'server.crt') + z.close() + zippy = open(zippath, "r") + # NOTE(vish): run instances expects encoded userdata, it is decoded + # in the get_metadata_call. autorun.sh also decodes the zip file, + # hence the double encoding. + encoded = zippy.read().encode("base64").encode("base64") + zippy.close() + return encoded def launch_vpn_instance(self, project_id, user_id): |