pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0189-Extend-the-advice-printing-code-by-some-useful-abstr.patch

ac7d03
From 17f988f0524ff682a95fe6a4be55b49ea7a0a419 Mon Sep 17 00:00:00 2001
ac7d03
From: Martin Babinsky <mbabinsk@redhat.com>
ac7d03
Date: Mon, 5 Jun 2017 16:59:25 +0200
ac7d03
Subject: [PATCH] Extend the advice printing code by some useful abstractions
ac7d03
ac7d03
The advise printing code was augmented by methods that simplify
ac7d03
generating bash snippets that report errors or failed commands.
ac7d03
ac7d03
https://pagure.io/freeipa/issue/6982
ac7d03
ac7d03
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
ac7d03
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
ac7d03
---
ac7d03
 ipaserver/advise/base.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++--
ac7d03
 1 file changed, 61 insertions(+), 2 deletions(-)
ac7d03
ac7d03
diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
ac7d03
index 40dabd0426719c5a791fee5be81a998e1c45854b..ba412b872472580cd32baf2a326a14edb951cab1 100644
ac7d03
--- a/ipaserver/advise/base.py
ac7d03
+++ b/ipaserver/advise/base.py
ac7d03
@@ -94,8 +94,67 @@ class _AdviceOutput(object):
ac7d03
         if self.options.verbose:
ac7d03
             self.comment('DEBUG: ' + line)
ac7d03
 
ac7d03
-    def command(self, line):
ac7d03
-        self.content.append(line)
ac7d03
+    def command(self, line, indent_spaces=0):
ac7d03
+        self.content.append(
ac7d03
+            '{}{}'.format(self._format_indent(indent_spaces), line))
ac7d03
+
ac7d03
+    def _format_indent(self, num_spaces):
ac7d03
+        return ' ' * num_spaces
ac7d03
+
ac7d03
+    def echo_error(self, error_message, indent_spaces=0):
ac7d03
+        self.command(
ac7d03
+            self._format_error(error_message), indent_spaces=indent_spaces)
ac7d03
+
ac7d03
+    def _format_error(self, error_message):
ac7d03
+        return 'echo "{}" >&2'.format(error_message)
ac7d03
+
ac7d03
+    def exit_on_failed_command(self, command_to_run,
ac7d03
+                               error_message_lines, indent_spaces=0):
ac7d03
+        self.command(command_to_run, indent_spaces=indent_spaces)
ac7d03
+        self.exit_on_predicate(
ac7d03
+            '[ "$?" -ne "0" ]',
ac7d03
+            error_message_lines,
ac7d03
+            indent_spaces=indent_spaces)
ac7d03
+
ac7d03
+    def exit_on_nonroot_euid(self):
ac7d03
+        self.exit_on_predicate(
ac7d03
+            '[ "$(id -u)" -ne "0" ]',
ac7d03
+            ["This script has to be run as root user"]
ac7d03
+        )
ac7d03
+
ac7d03
+    def exit_on_predicate(self, predicate, error_message_lines,
ac7d03
+                          indent_spaces=0):
ac7d03
+        commands_to_run = [
ac7d03
+            self._format_error(error_message_line)
ac7d03
+            for error_message_line in error_message_lines]
ac7d03
+
ac7d03
+        commands_to_run.append('exit 1')
ac7d03
+        self.commands_on_predicate(
ac7d03
+            predicate,
ac7d03
+            commands_to_run,
ac7d03
+            indent_spaces=indent_spaces)
ac7d03
+
ac7d03
+    def commands_on_predicate(self, predicate, commands_to_run_when_true,
ac7d03
+                              commands_to_run_when_false=None,
ac7d03
+                              indent_spaces=0):
ac7d03
+        if_command = 'if {}'.format(predicate)
ac7d03
+        self.command(if_command, indent_spaces=indent_spaces)
ac7d03
+        self.command('then', indent_spaces=indent_spaces)
ac7d03
+
ac7d03
+        indented_block_spaces = indent_spaces + 2
ac7d03
+
ac7d03
+        for command_to_run_when_true in commands_to_run_when_true:
ac7d03
+            self.command(
ac7d03
+                command_to_run_when_true, indent_spaces=indented_block_spaces)
ac7d03
+
ac7d03
+        if commands_to_run_when_false is not None:
ac7d03
+            self.command("else", indent_spaces=indent_spaces)
ac7d03
+            for command_to_run_when_false in commands_to_run_when_false:
ac7d03
+                self.command(
ac7d03
+                    command_to_run_when_false,
ac7d03
+                    indent_spaces=indented_block_spaces)
ac7d03
+
ac7d03
+        self.command('fi', indent_spaces=indent_spaces)
ac7d03
 
ac7d03
 
ac7d03
 class Advice(Plugin):
ac7d03
-- 
ac7d03
2.9.4
ac7d03