diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index c08f5caea1..fd6abee6a6 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -20,7 +20,6 @@ import getpass import io import logging import os -import sys from novaclient.v2 import servers from osc_lib.cli import parseractions @@ -189,12 +188,6 @@ def _prep_server_detail(compute_client, image_client, server): return info -def _show_progress(progress): - if progress: - sys.stdout.write('\rProgress: %s' % progress) - sys.stdout.flush() - - class AddFixedIP(command.Command): _description = _("Add fixed IP address to server") @@ -580,6 +573,12 @@ class CreateServer(command.ShowOne): return parser def take_action(self, parsed_args): + + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute volume_client = self.app.client_manager.volume image_client = self.app.client_manager.image @@ -814,11 +813,11 @@ class CreateServer(command.ShowOne): server.id, callback=_show_progress, ): - sys.stdout.write('\n') + self.app.stdout.write('\n') else: LOG.error(_('Error creating server: %s'), parsed_args.server_name) - sys.stdout.write(_('Error creating server\n')) + self.app.stdout.write(_('Error creating server\n')) raise SystemExit details = _prep_server_detail(compute_client, image_client, server) @@ -872,6 +871,12 @@ class DeleteServer(command.Command): return parser def take_action(self, parsed_args): + + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute for server in parsed_args.server: server_obj = utils.find_resource( @@ -883,11 +888,11 @@ class DeleteServer(command.Command): server_obj.id, callback=_show_progress, ): - sys.stdout.write('\n') + self.app.stdout.write('\n') else: LOG.error(_('Error deleting server: %s'), server_obj.id) - sys.stdout.write(_('Error deleting server\n')) + self.app.stdout.write(_('Error deleting server\n')) raise SystemExit @@ -1290,6 +1295,11 @@ class MigrateServer(command.Command): def take_action(self, parsed_args): + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute server = utils.find_resource( @@ -1315,11 +1325,11 @@ class MigrateServer(command.Command): server.id, callback=_show_progress, ): - sys.stdout.write(_('Complete\n')) + self.app.stdout.write(_('Complete\n')) else: LOG.error(_('Error migrating server: %s'), server.id) - sys.stdout.write(_('Error migrating server\n')) + self.app.stdout.write(_('Error migrating server\n')) raise SystemExit @@ -1380,6 +1390,12 @@ class RebootServer(command.Command): return parser def take_action(self, parsed_args): + + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute server = utils.find_resource( compute_client.servers, parsed_args.server) @@ -1391,11 +1407,11 @@ class RebootServer(command.Command): server.id, callback=_show_progress, ): - sys.stdout.write(_('Complete\n')) + self.app.stdout.write(_('Complete\n')) else: LOG.error(_('Error rebooting server: %s'), server.id) - sys.stdout.write(_('Error rebooting server\n')) + self.app.stdout.write(_('Error rebooting server\n')) raise SystemExit @@ -1428,6 +1444,12 @@ class RebuildServer(command.ShowOne): return parser def take_action(self, parsed_args): + + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute image_client = self.app.client_manager.image @@ -1445,11 +1467,11 @@ class RebuildServer(command.ShowOne): server.id, callback=_show_progress, ): - sys.stdout.write(_('Complete\n')) + self.app.stdout.write(_('Complete\n')) else: LOG.error(_('Error rebuilding server: %s'), server.id) - sys.stdout.write(_('Error rebuilding server\n')) + self.app.stdout.write(_('Error rebuilding server\n')) raise SystemExit details = _prep_server_detail(compute_client, image_client, server) @@ -1727,6 +1749,11 @@ the new server and restart the old one.""") def take_action(self, parsed_args): + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute server = utils.find_resource( compute_client.servers, @@ -1745,11 +1772,11 @@ the new server and restart the old one.""") success_status=['active', 'verify_resize'], callback=_show_progress, ): - sys.stdout.write(_('Complete\n')) + self.app.stdout.write(_('Complete\n')) else: LOG.error(_('Error resizing server: %s'), server.id) - sys.stdout.write(_('Error resizing server\n')) + self.app.stdout.write(_('Error resizing server\n')) raise SystemExit elif parsed_args.confirm: compute_client.servers.confirm_resize(server) @@ -1915,7 +1942,9 @@ class ShowServer(command.ShowOne): if parsed_args.diagnostics: (resp, data) = server.diagnostics() if not resp.status_code == 200: - sys.stderr.write(_("Error retrieving diagnostics data\n")) + self.app.stderr.write(_( + "Error retrieving diagnostics data\n" + )) return ({}, {}) else: data = _prep_server_detail(compute_client, diff --git a/openstackclient/compute/v2/server_backup.py b/openstackclient/compute/v2/server_backup.py index ddcf91010e..a79f5f7039 100644 --- a/openstackclient/compute/v2/server_backup.py +++ b/openstackclient/compute/v2/server_backup.py @@ -15,8 +15,6 @@ """Compute v2 Server action implementations""" -import sys - from osc_lib.command import command from osc_lib import exceptions from osc_lib import utils @@ -26,12 +24,6 @@ import six from openstackclient.i18n import _ -def _show_progress(progress): - if progress: - sys.stderr.write('\rProgress: %s' % progress) - sys.stderr.flush() - - class CreateServerBackup(command.ShowOne): _description = _("Create a server backup image") @@ -74,6 +66,12 @@ class CreateServerBackup(command.ShowOne): return parser def take_action(self, parsed_args): + + def _show_progress(progress): + if progress: + self.app.stderr.write('\rProgress: %s' % progress) + self.app.stderr.flush() + compute_client = self.app.client_manager.compute server = utils.find_resource( @@ -114,7 +112,7 @@ class CreateServerBackup(command.ShowOne): image.id, callback=_show_progress, ): - sys.stdout.write('\n') + self.app.stdout.write('\n') else: msg = _('Error creating server backup: %s') % parsed_args.name raise exceptions.CommandError(msg) diff --git a/openstackclient/compute/v2/server_image.py b/openstackclient/compute/v2/server_image.py index c66e06747f..3bc5d94aab 100644 --- a/openstackclient/compute/v2/server_image.py +++ b/openstackclient/compute/v2/server_image.py @@ -16,7 +16,6 @@ """Compute v2 Server action implementations""" import logging -import sys from osc_lib.command import command from osc_lib import exceptions @@ -30,12 +29,6 @@ from openstackclient.i18n import _ LOG = logging.getLogger(__name__) -def _show_progress(progress): - if progress: - sys.stdout.write('\rProgress: %s' % progress) - sys.stdout.flush() - - class CreateServerImage(command.ShowOne): _description = _("Create a new server disk image from an existing server") @@ -64,6 +57,12 @@ class CreateServerImage(command.ShowOne): return parser def take_action(self, parsed_args): + + def _show_progress(progress): + if progress: + self.app.stdout.write('\rProgress: %s' % progress) + self.app.stdout.flush() + compute_client = self.app.client_manager.compute server = utils.find_resource( @@ -92,7 +91,7 @@ class CreateServerImage(command.ShowOne): image_id, callback=_show_progress, ): - sys.stdout.write('\n') + self.app.stdout.write('\n') else: LOG.error(_('Error creating server image: %s'), parsed_args.server) diff --git a/openstackclient/compute/v2/usage.py b/openstackclient/compute/v2/usage.py index 3edcffe460..4320bf90b6 100644 --- a/openstackclient/compute/v2/usage.py +++ b/openstackclient/compute/v2/usage.py @@ -16,7 +16,6 @@ """Usage action implementations""" import datetime -import sys from osc_lib.command import command from osc_lib import utils @@ -96,7 +95,7 @@ class ListUsage(command.Lister): pass if parsed_args.formatter == 'table' and len(usage_list) > 0: - sys.stdout.write(_("Usage from %(start)s to %(end)s: \n") % { + self.app.stdout.write(_("Usage from %(start)s to %(end)s: \n") % { "start": start.strftime(dateformat), "end": end.strftime(dateformat), }) @@ -168,8 +167,9 @@ class ShowUsage(command.ShowOne): usage = compute_client.usage.get(project, start, end) if parsed_args.formatter == 'table': - sys.stdout.write(_("Usage from %(start)s to %(end)s on " - "project %(project)s: \n") % { + self.app.stdout.write(_( + "Usage from %(start)s to %(end)s on project %(project)s: \n" + ) % { "start": start.strftime(dateformat), "end": end.strftime(dateformat), "project": project, diff --git a/openstackclient/identity/v3/group.py b/openstackclient/identity/v3/group.py index 39c8547c4e..02eeadd63f 100644 --- a/openstackclient/identity/v3/group.py +++ b/openstackclient/identity/v3/group.py @@ -16,7 +16,6 @@ """Group action implementations""" import logging -import sys from keystoneauth1 import exceptions as ks_exc from osc_lib.command import command @@ -122,7 +121,7 @@ class CheckUserInGroup(command.Command): 'user': parsed_args.user, 'group': parsed_args.group, } - sys.stderr.write(msg) + self.app.stderr.write(msg) else: raise e else: @@ -130,7 +129,7 @@ class CheckUserInGroup(command.Command): 'user': parsed_args.user, 'group': parsed_args.group, } - sys.stdout.write(msg) + self.app.stdout.write(msg) class CreateGroup(command.ShowOne): diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 7e6a7aa178..ddcee4ade9 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -17,7 +17,6 @@ import argparse import logging -import sys from glanceclient.common import utils as gc_utils from osc_lib.cli import parseractions @@ -653,7 +652,7 @@ class SaveImage(command.Command): if data.wrapped is None: msg = _('Image %s has no data.') % image.id LOG.error(msg) - sys.stdout.write(msg + '\n') + self.app.stdout.write(msg + '\n') raise SystemExit gc_utils.save_image(data, parsed_args.file) diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index c4e125bab2..25a71db5cd 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -925,7 +925,7 @@ class TestServerCreate(TestServer): mock_wait_for_status.assert_called_once_with( self.servers_mock.get, self.new_server.id, - callback=server._show_progress, + callback=mock.ANY, ) kwargs = dict( @@ -975,7 +975,7 @@ class TestServerCreate(TestServer): mock_wait_for_status.assert_called_once_with( self.servers_mock.get, self.new_server.id, - callback=server._show_progress, + callback=mock.ANY, ) kwargs = dict( @@ -1494,7 +1494,7 @@ class TestServerDelete(TestServer): mock_wait_for_delete.assert_called_once_with( self.servers_mock, servers[0].id, - callback=server._show_progress + callback=mock.ANY, ) self.assertIsNone(result) @@ -1516,7 +1516,7 @@ class TestServerDelete(TestServer): mock_wait_for_delete.assert_called_once_with( self.servers_mock, servers[0].id, - callback=server._show_progress + callback=mock.ANY, ) @@ -2152,7 +2152,7 @@ class TestServerRebuild(TestServer): mock_wait_for_status.assert_called_once_with( self.servers_mock.get, self.server.id, - callback=server._show_progress, + callback=mock.ANY, # **kwargs ) @@ -2177,7 +2177,7 @@ class TestServerRebuild(TestServer): mock_wait_for_status.assert_called_once_with( self.servers_mock.get, self.server.id, - callback=server._show_progress + callback=mock.ANY, ) self.servers_mock.get.assert_called_with(self.server.id) @@ -2619,7 +2619,7 @@ class TestServerResize(TestServer): mock_wait_for_status.assert_called_once_with( self.servers_mock.get, self.server.id, - callback=server._show_progress, + callback=mock.ANY, **kwargs ) @@ -2659,7 +2659,7 @@ class TestServerResize(TestServer): mock_wait_for_status.assert_called_once_with( self.servers_mock.get, self.server.id, - callback=server._show_progress, + callback=mock.ANY, **kwargs )