pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone
Blob Blame History Raw
From 17f988f0524ff682a95fe6a4be55b49ea7a0a419 Mon Sep 17 00:00:00 2001
From: Martin Babinsky <mbabinsk@redhat.com>
Date: Mon, 5 Jun 2017 16:59:25 +0200
Subject: [PATCH] Extend the advice printing code by some useful abstractions

The advise printing code was augmented by methods that simplify
generating bash snippets that report errors or failed commands.

https://pagure.io/freeipa/issue/6982

Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
---
 ipaserver/advise/base.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 2 deletions(-)

diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
index 40dabd0426719c5a791fee5be81a998e1c45854b..ba412b872472580cd32baf2a326a14edb951cab1 100644
--- a/ipaserver/advise/base.py
+++ b/ipaserver/advise/base.py
@@ -94,8 +94,67 @@ class _AdviceOutput(object):
         if self.options.verbose:
             self.comment('DEBUG: ' + line)
 
-    def command(self, line):
-        self.content.append(line)
+    def command(self, line, indent_spaces=0):
+        self.content.append(
+            '{}{}'.format(self._format_indent(indent_spaces), line))
+
+    def _format_indent(self, num_spaces):
+        return ' ' * num_spaces
+
+    def echo_error(self, error_message, indent_spaces=0):
+        self.command(
+            self._format_error(error_message), indent_spaces=indent_spaces)
+
+    def _format_error(self, error_message):
+        return 'echo "{}" >&2'.format(error_message)
+
+    def exit_on_failed_command(self, command_to_run,
+                               error_message_lines, indent_spaces=0):
+        self.command(command_to_run, indent_spaces=indent_spaces)
+        self.exit_on_predicate(
+            '[ "$?" -ne "0" ]',
+            error_message_lines,
+            indent_spaces=indent_spaces)
+
+    def exit_on_nonroot_euid(self):
+        self.exit_on_predicate(
+            '[ "$(id -u)" -ne "0" ]',
+            ["This script has to be run as root user"]
+        )
+
+    def exit_on_predicate(self, predicate, error_message_lines,
+                          indent_spaces=0):
+        commands_to_run = [
+            self._format_error(error_message_line)
+            for error_message_line in error_message_lines]
+
+        commands_to_run.append('exit 1')
+        self.commands_on_predicate(
+            predicate,
+            commands_to_run,
+            indent_spaces=indent_spaces)
+
+    def commands_on_predicate(self, predicate, commands_to_run_when_true,
+                              commands_to_run_when_false=None,
+                              indent_spaces=0):
+        if_command = 'if {}'.format(predicate)
+        self.command(if_command, indent_spaces=indent_spaces)
+        self.command('then', indent_spaces=indent_spaces)
+
+        indented_block_spaces = indent_spaces + 2
+
+        for command_to_run_when_true in commands_to_run_when_true:
+            self.command(
+                command_to_run_when_true, indent_spaces=indented_block_spaces)
+
+        if commands_to_run_when_false is not None:
+            self.command("else", indent_spaces=indent_spaces)
+            for command_to_run_when_false in commands_to_run_when_false:
+                self.command(
+                    command_to_run_when_false,
+                    indent_spaces=indented_block_spaces)
+
+        self.command('fi', indent_spaces=indent_spaces)
 
 
 class Advice(Plugin):
-- 
2.9.4