From 91896ff51861e8d90bdf0f7c54cab0f2b3e3c277 Mon Sep 17 00:00:00 2001 From: Alessandro Pilotti Date: Thu, 13 Sep 2012 14:02:20 +0300 Subject: [PATCH] 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 --- glanceclient/v1/shell.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py index 3646f9fc..839f4807 100644 --- a/glanceclient/v1/shell.py +++ b/glanceclient/v1/shell.py @@ -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='', 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)