summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils
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
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')
-rw-r--r--snaps/openstack/utils/glance_utils.py38
-rw-r--r--snaps/openstack/utils/nova_utils.py49
2 files changed, 56 insertions, 31 deletions
diff --git a/snaps/openstack/utils/glance_utils.py b/snaps/openstack/utils/glance_utils.py
index 49bfe95..ad9c5e5 100644
--- a/snaps/openstack/utils/glance_utils.py
+++ b/snaps/openstack/utils/glance_utils.py
@@ -124,22 +124,30 @@ def __create_image_v1(glance, image_settings):
'name': image_settings.name, 'disk_format': image_settings.format,
'container_format': 'bare', 'is_public': image_settings.public}
- if image_settings.extra_properties:
- kwargs['properties'] = image_settings.extra_properties
-
- if image_settings.url:
- kwargs['location'] = image_settings.url
- elif image_settings.image_file:
- image_file = open(image_settings.image_file, 'rb')
- kwargs['data'] = image_file
- else:
- logger.warn('Unable to create image with name - %s. No file or URL',
- image_settings.name)
- return None
+ image_file = None
- created_image = glance.images.create(**kwargs)
- return Image(name=image_settings.name, image_id=created_image.id,
- size=created_image.size, properties=created_image.properties)
+ try:
+ if image_settings.extra_properties:
+ kwargs['properties'] = image_settings.extra_properties
+
+ if image_settings.url:
+ kwargs['location'] = image_settings.url
+ elif image_settings.image_file:
+ image_file = open(image_settings.image_file, 'rb')
+ kwargs['data'] = image_file
+ else:
+ logger.warn(
+ 'Unable to create image with name - %s. No file or URL',
+ image_settings.name)
+ return None
+
+ created_image = glance.images.create(**kwargs)
+ return Image(name=image_settings.name, image_id=created_image.id,
+ size=created_image.size,
+ properties=created_image.properties)
+ finally:
+ if image_file:
+ image_file.close()
def __create_image_v2(glance, image_settings):
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):