From 847bb8666d4f08e076e789c8cd3d094e49ea03be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nasiadka?= Date: Thu, 18 Feb 2021 15:32:57 +0100 Subject: [PATCH] Add output printing to host-command-run Change-Id: I50f75f8f8ed200b1c70f834973463c2f7343ed1a --- ansible/host-command-run.yml | 9 +++++++++ kayobe/cli/commands.py | 15 ++++++++++++--- kayobe/tests/unit/cli/test_commands.py | 18 ++++++++++++------ .../cli-show-output-11a257c87502d5a3.yaml | 5 +++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 releasenotes/notes/cli-show-output-11a257c87502d5a3.yaml diff --git a/ansible/host-command-run.yml b/ansible/host-command-run.yml index 1eb6f9c3c..18a9dd880 100644 --- a/ansible/host-command-run.yml +++ b/ansible/host-command-run.yml @@ -5,3 +5,12 @@ tasks: - name: Run a command shell: "{{ host_command_to_run }}" + register: command_output + - name: Print stdout + debug: + msg: "{{ command_output.stdout }}" + when: show_output | bool + - name: Print stderr + debug: + msg: "{{ command_output.stderr }}" + when: show_output | bool diff --git a/kayobe/cli/commands.py b/kayobe/cli/commands.py index 94fb42eca..f4df461f4 100644 --- a/kayobe/cli/commands.py +++ b/kayobe/cli/commands.py @@ -494,12 +494,15 @@ class SeedHypervisorHostCommandRun(KayobeAnsibleMixin, VaultMixin, Command): group = parser.add_argument_group("Host Command Run") group.add_argument("--command", required=True, help="Command to run (required).") + group.add_argument("--show-output", action='store_true', + help="Show command output") return parser def take_action(self, parsed_args): self.app.LOG.debug("Run command on seed hypervisor host") extra_vars = { - "host_command_to_run": utils.escape_jinja(parsed_args.command)} + "host_command_to_run": utils.escape_jinja(parsed_args.command), + "show_output": parsed_args.show_output} playbooks = _build_playbook_list("host-command-run") self.run_kayobe_playbooks(parsed_args, playbooks, limit="seed-hypervisor", @@ -659,12 +662,15 @@ class SeedHostCommandRun(KayobeAnsibleMixin, VaultMixin, Command): group = parser.add_argument_group("Host Command Run") group.add_argument("--command", required=True, help="Command to run (required).") + group.add_argument("--show-output", action='store_true', + help="Show command output") return parser def take_action(self, parsed_args): self.app.LOG.debug("Run command on seed host") extra_vars = { - "host_command_to_run": utils.escape_jinja(parsed_args.command)} + "host_command_to_run": utils.escape_jinja(parsed_args.command), + "show_output": parsed_args.show_output} playbooks = _build_playbook_list("host-command-run") self.run_kayobe_playbooks(parsed_args, playbooks, limit="seed", extra_vars=extra_vars) @@ -1018,12 +1024,15 @@ class OvercloudHostCommandRun(KayobeAnsibleMixin, VaultMixin, Command): group = parser.add_argument_group("Host Command Run") group.add_argument("--command", required=True, help="Command to run (required).") + group.add_argument("--show-output", action='store_true', + help="Show command output") return parser def take_action(self, parsed_args): self.app.LOG.debug("Run command on overcloud host") extra_vars = { - "host_command_to_run": utils.escape_jinja(parsed_args.command)} + "host_command_to_run": utils.escape_jinja(parsed_args.command), + "show_output": parsed_args.show_output} playbooks = _build_playbook_list("host-command-run") self.run_kayobe_playbooks(parsed_args, playbooks, limit="overcloud", extra_vars=extra_vars) diff --git a/kayobe/tests/unit/cli/test_commands.py b/kayobe/tests/unit/cli/test_commands.py index 4339eb567..b8d9f7c63 100644 --- a/kayobe/tests/unit/cli/test_commands.py +++ b/kayobe/tests/unit/cli/test_commands.py @@ -342,7 +342,8 @@ class TestCase(unittest.TestCase): def test_seed_hypervisor_host_command_run(self, mock_run): command = commands.SeedHypervisorHostCommandRun(TestApp(), []) parser = command.get_parser("test") - parsed_args = parser.parse_args(["--command", "ls -a"]) + parsed_args = parser.parse_args(["--command", "ls -a", + "--show-output"]) result = command.run(parsed_args) self.assertEqual(0, result) @@ -356,7 +357,8 @@ class TestCase(unittest.TestCase): ], limit="seed-hypervisor", extra_vars={ - "host_command_to_run": utils.escape_jinja("ls -a")}, + "host_command_to_run": utils.escape_jinja("ls -a"), + "show_output": True} ), ] self.assertEqual(expected_calls, mock_run.call_args_list) @@ -551,7 +553,8 @@ class TestCase(unittest.TestCase): def test_seed_host_command_run(self, mock_run): command = commands.SeedHostCommandRun(TestApp(), []) parser = command.get_parser("test") - parsed_args = parser.parse_args(["--command", "ls -a"]) + parsed_args = parser.parse_args(["--command", "ls -a", + "--show-output"]) result = command.run(parsed_args) self.assertEqual(0, result) @@ -565,7 +568,8 @@ class TestCase(unittest.TestCase): ], limit="seed", extra_vars={ - "host_command_to_run": utils.escape_jinja("ls -a")}, + "host_command_to_run": utils.escape_jinja("ls -a"), + "show_output": True} ), ] self.assertEqual(expected_calls, mock_run.call_args_list) @@ -1057,7 +1061,8 @@ class TestCase(unittest.TestCase): def test_overcloud_host_command_run(self, mock_run): command = commands.OvercloudHostCommandRun(TestApp(), []) parser = command.get_parser("test") - parsed_args = parser.parse_args(["--command", "ls -a"]) + parsed_args = parser.parse_args(["--command", "ls -a", + "--show-output"]) result = command.run(parsed_args) self.assertEqual(0, result) @@ -1071,7 +1076,8 @@ class TestCase(unittest.TestCase): ], limit="overcloud", extra_vars={ - "host_command_to_run": utils.escape_jinja("ls -a")}, + "host_command_to_run": utils.escape_jinja("ls -a"), + "show_output": True} ), ] self.assertEqual(expected_calls, mock_run.call_args_list) diff --git a/releasenotes/notes/cli-show-output-11a257c87502d5a3.yaml b/releasenotes/notes/cli-show-output-11a257c87502d5a3.yaml new file mode 100644 index 000000000..a139d315b --- /dev/null +++ b/releasenotes/notes/cli-show-output-11a257c87502d5a3.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + The ``kayobe * host command run`` commands now support ``--show-output`` + which displays both standard output and standard error.