summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/nova_utils.py
diff options
context:
space:
mode:
authorspisarski <s.pisarski@cablelabs.com>2017-07-27 10:27:14 -0600
committerspisarski <s.pisarski@cablelabs.com>2017-07-27 10:27:14 -0600
commitca276f452540f68c08cb3df9049e9e7876364dac (patch)
treece26c5fb721f10402669bf761c4e69317d3f3eca /snaps/openstack/utils/nova_utils.py
parentfdcd5600bade294ae2a2207ef01da92f22f64327 (diff)
Ensure library and tests close all necessary resources.
The SNAPS-OO library and tests had left open files, ssh, and scp connections. These have all now been wrapped with try/finally blocks. JIRA: SNAPS-152 Change-Id: I43e09978b5c075bd78ff3279c0799556b8758878 Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack/utils/nova_utils.py')
-rw-r--r--snaps/openstack/utils/nova_utils.py49
1 files changed, 33 insertions, 16 deletions
diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py
index ab434f1..b148bc5 100644
--- a/snaps/openstack/utils/nova_utils.py
+++ b/snaps/openstack/utils/nova_utils.py
@@ -232,12 +232,18 @@ def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
if not os.path.isdir(pub_dir):
os.mkdir(pub_dir)
- public_handle = open(pub_expand_file, 'wb')
- public_bytes = keys.public_key().public_bytes(
- serialization.Encoding.OpenSSH,
- serialization.PublicFormat.OpenSSH)
- public_handle.write(public_bytes)
- public_handle.close()
+
+ public_handle = None
+ try:
+ public_handle = open(pub_expand_file, 'wb')
+ public_bytes = keys.public_key().public_bytes(
+ serialization.Encoding.OpenSSH,
+ serialization.PublicFormat.OpenSSH)
+ public_handle.write(public_bytes)
+ finally:
+ if public_handle:
+ public_handle.close()
+
os.chmod(pub_expand_file, 0o400)
logger.info("Saved public key to - " + pub_expand_file)
if priv_file_path:
@@ -246,13 +252,19 @@ def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
priv_dir = os.path.dirname(priv_expand_file)
if not os.path.isdir(priv_dir):
os.mkdir(priv_dir)
- private_handle = open(priv_expand_file, 'wb')
- private_handle.write(
- keys.private_bytes(
- encoding=serialization.Encoding.PEM,
- format=serialization.PrivateFormat.TraditionalOpenSSL,
- encryption_algorithm=serialization.NoEncryption()))
- private_handle.close()
+
+ private_handle = None
+ try:
+ private_handle = open(priv_expand_file, 'wb')
+ private_handle.write(
+ keys.private_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PrivateFormat.TraditionalOpenSSL,
+ encryption_algorithm=serialization.NoEncryption()))
+ finally:
+ if private_handle:
+ private_handle.close()
+
os.chmod(priv_expand_file, 0o400)
logger.info("Saved private key to - " + priv_expand_file)
@@ -265,9 +277,14 @@ def upload_keypair_file(nova, name, file_path):
:param file_path: the path to the public key file
:return: the keypair object
"""
- with open(os.path.expanduser(file_path), 'rb') as fpubkey:
- logger.info('Saving keypair to - ' + file_path)
- return upload_keypair(nova, name, fpubkey.read())
+ fpubkey = None
+ try:
+ with open(os.path.expanduser(file_path), 'rb') as fpubkey:
+ logger.info('Saving keypair to - ' + file_path)
+ return upload_keypair(nova, name, fpubkey.read())
+ finally:
+ if fpubkey:
+ fpubkey.close()
def upload_keypair(nova, name, key):