def do_image_upload(gc, args):
    """Upload data for a specific image."""
    image_data = utils.get_data_file(args)
    if args.progress:
        filesize = utils.get_file_size(image_data)
        image_data = progressbar.VerboseFileWrapper(image_data, filesize)
    gc.images.upload(args.id, image_data, args.size)
def do_image_update(gc, args):
    """Update a specific image."""
    # Filter out None values
    fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))

    image_arg = fields.pop('image')
    image = utils.find_resource(gc.images, image_arg)

    if 'is_protected' in fields:
        fields['protected'] = fields.pop('is_protected')

    raw_properties = fields.pop('property')
    fields['properties'] = {}
    for datum in raw_properties:
        key, value = datum.split('=', 1)
        fields['properties'][key] = value

    # Filter out values we can't use
    UPDATE_PARAMS = glanceclient.v1.images.UPDATE_PARAMS
    fields = dict(filter(lambda x: x[0] in UPDATE_PARAMS, fields.items()))

    if image.status == 'queued':
        _set_data_field(fields, args)

        if args.progress:
            filesize = utils.get_file_size(fields['data'])
            fields['data'] = progressbar.VerboseFileWrapper(
                fields['data'], filesize)

    image = gc.images.update(image, purge_props=args.purge_props, **fields)
    _image_show(image, args.human_readable)
Exemple #3
0
def do_image_create(gc, args):
    """Create a new image."""
    # Filter out None values
    fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))

    fields['is_public'] = fields.get('is_public')

    if 'is_protected' in fields:
        fields['protected'] = fields.pop('is_protected')

    raw_properties = fields.pop('property')
    fields['properties'] = {}
    for datum in raw_properties:
        key, value = datum.split('=', 1)
        fields['properties'][key] = value

    # Filter out values we can't use
    CREATE_PARAMS = glanceclient.v1.images.CREATE_PARAMS
    fields = dict(filter(lambda x: x[0] in CREATE_PARAMS, fields.items()))

    _set_data_field(fields, args)

    # Only show progress bar for local image files
    if fields.get('data') and args.progress:
        filesize = utils.get_file_size(fields['data'])
        if filesize is not None:
            # NOTE(kragniz): do not show a progress bar if the size of the
            # input is unknown (most likely a piped input)
            fields['data'] = progressbar.VerboseFileWrapper(
                fields['data'], filesize)

    image = gc.images.create(**fields)
    _image_show(image, args.human_readable)
def do_image_create(gc, args):
    """Create a new image."""
    # Filter out None values
    fields = dict(filter(lambda x: x[1] is not None, vars(args).items()))

    fields['is_public'] = fields.get('is_public') or fields.pop('public')

    if 'is_protected' in fields:
        fields['protected'] = fields.pop('is_protected')

    raw_properties = fields.pop('property')
    fields['properties'] = {}
    for datum in raw_properties:
        key, value = datum.split('=', 1)
        fields['properties'][key] = value

    # Filter out values we can't use
    CREATE_PARAMS = glanceclient.v1.images.CREATE_PARAMS
    fields = dict(filter(lambda x: x[0] in CREATE_PARAMS, fields.items()))

    _set_data_field(fields, args)

    if args.progress:
        filesize = utils.get_file_size(fields['data'])
        fields['data'] = progressbar.VerboseFileWrapper(
            fields['data'], filesize)

    image = gc.images.create(**fields)
    _image_show(image, args.human_readable)
def do_image_upload(gc, args):
    """Upload data for a specific image."""
    image_data = utils.get_data_file(args)
    if args.progress:
        filesize = utils.get_file_size(image_data)
        if filesize is not None:
            # NOTE(kragniz): do not show a progress bar if the size of the
            # input is unknown (most likely a piped input)
            image_data = progressbar.VerboseFileWrapper(image_data, filesize)
    gc.images.upload(args.id, image_data, args.size)
 def test_iter_file_display_progress_bar(self):
     size = 98304
     file_obj = six.StringIO('X' * size)
     saved_stdout = sys.stdout
     try:
         sys.stdout = output = test_utils.FakeTTYStdout()
         file_obj = progressbar.VerboseFileWrapper(file_obj, size)
         chunksize = 1024
         chunk = file_obj.read(chunksize)
         while chunk:
             chunk = file_obj.read(chunksize)
         self.assertEqual('[%s>] 100%%\n' % ('=' * 29), output.getvalue())
     finally:
         sys.stdout = saved_stdout
 def test_iter_file_no_tty(self):
     size = 98304
     file_obj = six.StringIO('X' * size)
     saved_stdout = sys.stdout
     try:
         sys.stdout = output = test_utils.FakeNoTTYStdout()
         file_obj = progressbar.VerboseFileWrapper(file_obj, size)
         chunksize = 1024
         chunk = file_obj.read(chunksize)
         while chunk:
             chunk = file_obj.read(chunksize)
         # If stdout is not a tty progress bar should do nothing.
         self.assertEqual('', output.getvalue())
     finally:
         sys.stdout = saved_stdout
Exemple #8
0
def upload_image_from_url(glance, img, image_url):

    resp = requests.get(image_url, stream=True)
    if resp.status_code != requests.codes.ok:  # pylint: disable=no-member
        raise Exception("Received HTTP %d" % resp.status_code)

    f_in = resp.raw
    if 'Content-Length' in resp.headers:
        f_in = progressbar.VerboseFileWrapper(f_in,
                                              resp.headers['Content-Length'])

    if image_url.endswith('.gz'):
        f_in = gzip.GzipFile(fileobj=f_in, mode='rb')
    elif image_url.endswith('.xz'):
        f_in = lzma.open(f_in)
    elif image_url.endswith('.bz2'):
        f_in = bz2.open(f_in)

    glance.images.upload(img, f_in)