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

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