Fixes glance add / update / image-create / image-update on Windows

Fixes Bug #1050345

The image upload hangs if the file contains a byte with value 0x1A (EOF), due to
the fact that the file or stdin streams are treated as text and not
binary streams. This fix sets the proper binary mode.

Change-Id: I3425cb9729a8da4d1b73fbfba06fd6f2c7e8833e
This commit is contained in:
Alessandro Pilotti
2012-09-13 14:02:20 +03:00
parent 8cee48b1dd
commit 91896ff518
+18 -10
View File
@@ -15,8 +15,14 @@
import argparse
import copy
import os
import sys
if os.name == 'nt':
import msvcrt
else:
msvcrt = None
from glanceclient.common import utils
import glanceclient.v1.images
@@ -70,6 +76,16 @@ def _image_show(image):
utils.print_dict(info)
def _set_data_field(fields, args):
if 'location' not in fields and 'copy_from' not in fields:
if args.file:
fields['data'] = open(args.file, 'rb')
else:
if msvcrt:
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
fields['data'] = sys.stdin
@utils.arg('id', metavar='<IMAGE_ID>', help='ID of image to describe.')
def do_image_show(gc, args):
"""Describe a specific image."""
@@ -151,11 +167,7 @@ def do_image_create(gc, args):
CREATE_PARAMS = glanceclient.v1.images.CREATE_PARAMS
fields = dict(filter(lambda x: x[0] in CREATE_PARAMS, fields.items()))
if 'location' not in fields and 'copy_from' not in fields:
if args.file:
fields['data'] = open(args.file, 'r')
else:
fields['data'] = sys.stdin
_set_data_field(fields, args)
image = gc.images.create(**fields)
_image_show(image)
@@ -222,11 +234,7 @@ def do_image_update(gc, args):
UPDATE_PARAMS = glanceclient.v1.images.UPDATE_PARAMS
fields = dict(filter(lambda x: x[0] in UPDATE_PARAMS, fields.items()))
if 'location' not in fields and 'copy_from' not in fields:
if args.file:
fields['data'] = open(args.file, 'r')
else:
fields['data'] = sys.stdin
_set_data_field(fields, args)
image = gc.images.update(image_id, purge_props=args.purge_props, **fields)
_image_show(image)