summaryrefslogtreecommitdiff
path: root/openstackclient/image
diff options
context:
space:
mode:
authorDean Troyer <dtroyer@gmail.com>2014-10-13 22:40:11 -0500
committerDean Troyer <dtroyer@gmail.com>2014-10-14 15:37:23 -0500
commitca783f46595a7d90cea0ad8491b65aa5f9370a04 (patch)
treedb0f6a59e4dcec4f2cc3ccc438f8c059aa1a9b4d /openstackclient/image
parent36212c43d880d0eaeb3df271cccb314802bf3372 (diff)
downloadpython-openstackclient-ca783f46595a7d90cea0ad8491b65aa5f9370a04.tar.gz
Close files on image create
The file opened for --file was never closed. Close it if it is a file object. Change-Id: I7bd120a2413de42339771d01e8fd1894d38c3011
Diffstat (limited to 'openstackclient/image')
-rw-r--r--openstackclient/image/v1/image.py53
1 files changed, 30 insertions, 23 deletions
diff --git a/openstackclient/image/v1/image.py b/openstackclient/image/v1/image.py
index 465e9d7b..32dd388c 100644
--- a/openstackclient/image/v1/image.py
+++ b/openstackclient/image/v1/image.py
@@ -15,6 +15,7 @@
"""Image V1 Action Implementations"""
+import io
import logging
import os
import six
@@ -214,10 +215,9 @@ class CreateImage(show.ShowOne):
elif parsed_args.file:
# Send an open file handle to glanceclient so it will
# do a chunked transfer
- kwargs["data"] = open(parsed_args.file, "rb")
+ kwargs["data"] = io.open(parsed_args.file, "rb")
else:
# Read file from stdin
- kwargs["data"] = None
if sys.stdin.isatty() is not True:
if msvcrt:
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
@@ -225,29 +225,36 @@ class CreateImage(show.ShowOne):
# do a chunked transfer
kwargs["data"] = sys.stdin
+ # Wrap the call to catch exceptions in order to close files
try:
- image = utils.find_resource(
- image_client.images,
- parsed_args.name,
- )
-
- # Preserve previous properties if any are being set now
- if image.properties:
- if parsed_args.properties:
- image.properties.update(kwargs['properties'])
- kwargs['properties'] = image.properties
-
- except exceptions.CommandError:
- if not parsed_args.volume:
- # This is normal for a create or reserve (create w/o an image)
- # But skip for create from volume
- image = image_client.images.create(**kwargs)
- else:
- # Update an existing reservation
+ try:
+ image = utils.find_resource(
+ image_client.images,
+ parsed_args.name,
+ )
- # If an image is specified via --file, --location or
- # --copy-from let the API handle it
- image = image_client.images.update(image.id, **kwargs)
+ # Preserve previous properties if any are being set now
+ if image.properties:
+ if parsed_args.properties:
+ image.properties.update(kwargs['properties'])
+ kwargs['properties'] = image.properties
+
+ except exceptions.CommandError:
+ if not parsed_args.volume:
+ # This is normal for a create or reserve (create w/o
+ # an image), but skip for create from volume
+ image = image_client.images.create(**kwargs)
+ else:
+ # Update an existing reservation
+
+ # If an image is specified via --file, --location or
+ # --copy-from let the API handle it
+ image = image_client.images.update(image.id, **kwargs)
+ finally:
+ # Clean up open files - make sure data isn't a string
+ if ('data' in kwargs and hasattr(kwargs['data'], 'close') and
+ kwargs['data'] != sys.stdin):
+ kwargs['data'].close()
info = {}
info.update(image._info)