summaryrefslogtreecommitdiff
path: root/openstackclient/image
diff options
context:
space:
mode:
authorStephen Finucane <sfinucan@redhat.com>2022-10-19 18:07:41 +0100
committerStephen Finucane <sfinucan@redhat.com>2022-11-09 16:51:54 +0000
commit1fb8d1f48b256a2bad78e7d5633ea53c6537907c (patch)
tree17331af0f0209f156d91ea1ccf14c1bf8b2df5b3 /openstackclient/image
parent3d9a9df935af1f76a33d008fe76975475c77a268 (diff)
downloadpython-openstackclient-1fb8d1f48b256a2bad78e7d5633ea53c6537907c.tar.gz
image: Add 'image stage' command
This is the equivalent of the 'image-stage' glanceclient command. Change-Id: I10b01ef145740a2f7ffe5a8c7ce0296df0ece0bd Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
Diffstat (limited to 'openstackclient/image')
-rw-r--r--openstackclient/image/v2/image.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py
index 53cfaded..039f1d2d 100644
--- a/openstackclient/image/v2/image.py
+++ b/openstackclient/image/v2/image.py
@@ -1484,3 +1484,80 @@ class UnsetImage(command.Command):
"Failed to unset %(propret)s of %(proptotal)s" " properties."
) % {'propret': propret, 'proptotal': proptotal}
raise exceptions.CommandError(msg)
+
+
+class StageImage(command.Command):
+ _description = _(
+ "Upload data for a specific image to staging.\n"
+ "This requires support for the interoperable image import process, "
+ "which was first introduced in Image API version 2.6 "
+ "(Glance 16.0.0 (Queens))"
+ )
+
+ def get_parser(self, prog_name):
+ parser = super().get_parser(prog_name)
+
+ parser.add_argument(
+ '--file',
+ metavar='<file>',
+ dest='filename',
+ help=_(
+ 'Local file that contains disk image to be uploaded. '
+ 'Alternatively, images can be passed via stdin.'
+ ),
+ )
+ # NOTE(stephenfin): glanceclient had a --size argument but it didn't do
+ # anything so we have chosen not to port this
+ parser.add_argument(
+ '--progress',
+ action='store_true',
+ default=False,
+ help=_(
+ 'Show upload progress bar '
+ '(ignored if passing data via stdin)'
+ ),
+ )
+ parser.add_argument(
+ 'image',
+ metavar='<image>',
+ help=_('Image to upload data for (name or ID)'),
+ )
+
+ return parser
+
+ def take_action(self, parsed_args):
+ image_client = self.app.client_manager.image
+
+ image = image_client.find_image(
+ parsed_args.image,
+ ignore_missing=False,
+ )
+ # open the file first to ensure any failures are handled before the
+ # image is created. Get the file name (if it is file, and not stdin)
+ # for easier further handling.
+ if parsed_args.filename:
+ try:
+ fp = open(parsed_args.filename, 'rb')
+ except FileNotFoundError:
+ raise exceptions.CommandError(
+ '%r is not a valid file' % parsed_args.filename,
+ )
+ else:
+ fp = get_data_from_stdin()
+
+ kwargs = {}
+
+ if parsed_args.progress and parsed_args.filename:
+ # NOTE(stephenfin): we only show a progress bar if the user
+ # requested it *and* we're reading from a file (not stdin)
+ filesize = os.path.getsize(parsed_args.filename)
+ if filesize is not None:
+ kwargs['data'] = progressbar.VerboseFileWrapper(fp, filesize)
+ else:
+ kwargs['data'] = fp
+ elif parsed_args.filename:
+ kwargs['filename'] = parsed_args.filename
+ elif fp:
+ kwargs['data'] = fp
+
+ image_client.stage_image(image, **kwargs)