From 8dbf141d52c84d0a7311e62d39d229fdc80876fd Mon Sep 17 00:00:00 2001 From: Simon Merrick Date: Thu, 19 Nov 2020 20:05:54 +1300 Subject: stop image downloads to memory + Fixes issue with large images hogging memory + stream image downloads + output to stdout if file not specified Change-Id: Ia01ff9b21a2dac5d0ccf2bd58a8640e88c5cbb36 Story: 2007672 Task: 39776 (cherry picked from commit 5bdcd590ecacbc0aa8db2cbafa0ab1a9f3c28ce0) --- openstackclient/image/v1/image.py | 6 +++++- openstackclient/image/v2/image.py | 6 +++++- openstackclient/tests/unit/image/v2/test_image.py | 3 ++- .../notes/fix-openstak-image-save-sdk-port-eb160e8ffc92e514.yaml | 7 +++++++ 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix-openstak-image-save-sdk-port-eb160e8ffc92e514.yaml diff --git a/openstackclient/image/v1/image.py b/openstackclient/image/v1/image.py index cf1d6817..64aa3fcd 100644 --- a/openstackclient/image/v1/image.py +++ b/openstackclient/image/v1/image.py @@ -478,7 +478,11 @@ class SaveImage(command.Command): image_client = self.app.client_manager.image image = image_client.find_image(parsed_args.image) - image_client.download_image(image.id, output=parsed_args.file) + output_file = parsed_args.file + if output_file is None: + output_file = getattr(sys.stdout, "buffer", sys.stdout) + + image_client.download_image(image.id, stream=True, output=output_file) class SetImage(command.Command): diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 029f57a3..2836e764 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -803,7 +803,11 @@ class SaveImage(command.Command): image_client = self.app.client_manager.image image = image_client.find_image(parsed_args.image) - image_client.download_image(image.id, output=parsed_args.file) + output_file = parsed_args.file + if output_file is None: + output_file = getattr(sys.stdout, "buffer", sys.stdout) + + image_client.download_image(image.id, stream=True, output=output_file) class SetImage(command.Command): diff --git a/openstackclient/tests/unit/image/v2/test_image.py b/openstackclient/tests/unit/image/v2/test_image.py index 310f6b76..a7f60398 100644 --- a/openstackclient/tests/unit/image/v2/test_image.py +++ b/openstackclient/tests/unit/image/v2/test_image.py @@ -1614,7 +1614,7 @@ class TestImageSave(TestImage): verifylist = [ ('file', '/path/to/file'), - ('image', self.image.id) + ('image', self.image.id), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -1622,6 +1622,7 @@ class TestImageSave(TestImage): self.client.download_image.assert_called_once_with( self.image.id, + stream=True, output='/path/to/file') diff --git a/releasenotes/notes/fix-openstak-image-save-sdk-port-eb160e8ffc92e514.yaml b/releasenotes/notes/fix-openstak-image-save-sdk-port-eb160e8ffc92e514.yaml new file mode 100644 index 00000000..857f3c50 --- /dev/null +++ b/releasenotes/notes/fix-openstak-image-save-sdk-port-eb160e8ffc92e514.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - Stream image download to avoid buffering data in memory which rapidly + exhausts memory resulting in OOM kill or system crash for all but the + smallest of images. Fixes https://storyboard.openstack.org/#!/story/2007672 + - Restore default behavior of 'openstack image save' to send data to stdout + Relates to https://storyboard.openstack.org/#!/story/2007672. \ No newline at end of file -- cgit v1.2.1