From 995591a9564b74b424f6ac519d458f23cddd3053 Mon Sep 17 00:00:00 2001
From: Enno Gotthold <egotthold@suse.de>
Date: Wed, 19 Jan 2022 15:05:01 +0100
Subject: [PATCH] Cosmetic changes to the installation modules

Those changes are small simplifications and Black formatting issues.

(cherry picked from commit 8ad30a1a95d0dde494bf41fead2c1fca2bdee4b8)
---
 cobbler/modules/installation/post_power.py    |  9 +++------
 cobbler/modules/installation/post_puppet.py   | 15 +++++++--------
 cobbler/modules/installation/post_report.py   | 18 +++++++++++-------
 .../installation/pre_clear_anamon_logs.py     | 19 ++++++++++---------
 4 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/cobbler/modules/installation/post_power.py b/cobbler/modules/installation/post_power.py
index b359d84d..ac9726ce 100644
--- a/cobbler/modules/installation/post_power.py
+++ b/cobbler/modules/installation/post_power.py
@@ -35,23 +35,20 @@ def run(api, args, logger):
 
     :param api: The api to resolve information with.
     :param args: This is an array containing two objects.
-                 0: String with the content "target" or "profile".
-                 1: The name of target or profile
+                 0: The str "system". All other content will result in an early exit of the trigger.
+                 1: The name of the target system.
     :param logger: Unused parameter for this hook.
     :return: ``0`` on success.
     """
-    # FIXME: make everything use the logger
-
     objtype = args[0]
     name = args[1]
-    # boot_ip = args[2] # ip or "?"
 
     if objtype == "system":
         target = api.find_system(name)
     else:
         return 0
 
-    if target and 'postreboot' in target.autoinstall_meta:
+    if target and "postreboot" in target.autoinstall_meta:
         # Run this in a thread so the system has a chance to finish and umount the filesystem
         current = reboot(api, target)
         current.start()
diff --git a/cobbler/modules/installation/post_puppet.py b/cobbler/modules/installation/post_puppet.py
index 1e4bf1b6..6604ad66 100644
--- a/cobbler/modules/installation/post_puppet.py
+++ b/cobbler/modules/installation/post_puppet.py
@@ -4,7 +4,7 @@ puppet master server is running on the same machine as the Cobbler
 server.
 
 Based on:
-http://www.ithiriel.com/content/2010/03/29/writing-install-triggers-cobbler
+https://www.ithiriel.com/content/2010/03/29/writing-install-triggers-cobbler
 """
 from builtins import str
 import re
@@ -25,14 +25,13 @@ def run(api, args, logger):
     The obligatory Cobbler modules hook.
 
     :param api: The api to resolve all information with.
-    :param args: This is an array with two items. The first may be ``system`` or ``profile`` and the second is the name
-                 of this system or profile.
+    :param args: This is an array with two items. The first must be ``system``, if the value is different we do an
+                 early and the second is the name of this system or profile.
     :param logger: The logger to audit all actions with.
     :return: ``0`` or nothing.
     """
     objtype = args[0]
     name = args[1]
-    # ip = args[2]          # ip or "?"
 
     if objtype != "system":
         return 0
@@ -48,12 +47,12 @@ def run(api, args, logger):
     system = api.find_system(name)
     system = utils.blender(api, False, system)
     hostname = system["hostname"]
-    if not re.match(r'[\w-]+\..+', hostname):
-        search_domains = system['name_servers_search']
+    if not re.match(r"[\w-]+\..+", hostname):
+        search_domains = system["name_servers_search"]
         if search_domains:
-            hostname += '.' + search_domains[0]
+            hostname += "." + search_domains[0]
     puppetca_path = settings.puppetca_path
-    cmd = [puppetca_path, 'cert', 'sign', hostname]
+    cmd = [puppetca_path, "cert", "sign", hostname]
 
     rc = 0
 
diff --git a/cobbler/modules/installation/post_report.py b/cobbler/modules/installation/post_report.py
index 2a137a93..615a9f66 100644
--- a/cobbler/modules/installation/post_report.py
+++ b/cobbler/modules/installation/post_report.py
@@ -30,7 +30,7 @@ def run(api, args, logger):
 
     :param api: The api to resolve information with.
     :param args: This is an array with three elements.
-                 0: "target" or "profile"
+                 0: "system" or "profile"
                  1: name of target or profile
                  2: ip or "?"
     :param logger: In this module not used.
@@ -50,8 +50,10 @@ def run(api, args, logger):
 
     if objtype == "system":
         target = api.find_system(name)
-    else:
+    elif objtype == "profile":
         target = api.find_profile(name)
+    else:
+        return 1
 
     # collapse the object down to a rendered datastructure
     target = utils.blender(api, False, target)
@@ -75,14 +77,14 @@ def run(api, args, logger):
 
     subject = settings.build_reporting_subject
     if subject == "":
-        subject = '[Cobbler] install complete '
+        subject = "[Cobbler] install complete "
 
     to_addr = ",".join(to_addr)
     metadata = {
         "from_addr": from_addr,
         "to_addr": to_addr,
         "subject": subject,
-        "boot_ip": boot_ip
+        "boot_ip": boot_ip,
     }
     metadata.update(target)
 
@@ -90,18 +92,20 @@ def run(api, args, logger):
     input_data = input_template.read()
     input_template.close()
 
-    message = templar.Templar(api._config).render(input_data, metadata, None)
+    message = templar.Templar(api._config).render(
+            input_data, metadata, None
+        )
 
     sendmail = True
     for prefix in settings.build_reporting_ignorelist:
-        if prefix != '' and name.lower().startswith(prefix):
+        if prefix != "" and name.lower().startswith(prefix):
             sendmail = False
 
     if sendmail:
         # Send the mail
         # FIXME: on error, return non-zero
         server_handle = smtplib.SMTP(smtp_server)
-        server_handle.sendmail(from_addr, to_addr.split(','), message)
+        server_handle.sendmail(from_addr, to_addr.split(","), message)
         server_handle.quit()
 
     return 0
diff --git a/cobbler/modules/installation/pre_clear_anamon_logs.py b/cobbler/modules/installation/pre_clear_anamon_logs.py
index 189c5241..793ade64 100644
--- a/cobbler/modules/installation/pre_clear_anamon_logs.py
+++ b/cobbler/modules/installation/pre_clear_anamon_logs.py
@@ -21,11 +21,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 from builtins import str
 import glob
+import logging
 import os
 
+from cobbler import utils
 from cobbler.cexceptions import CX
 
 
+PATH_PREFIX = "/var/log/cobbler/anamon/"
+
+logger = logging.getLogger()
+
+
 def register():
     """
         This pure python trigger acts as if it were a legacy shell-trigger, but is much faster. The return of this method
@@ -47,9 +54,6 @@ def run(api, args, logger):
     :param logger: This parameter is unused currently.
     :return: "0" on success.
     """
-
-    # FIXME: use the logger
-
     if len(args) < 3:
         raise CX("invalid invocation")
 
@@ -62,15 +66,12 @@ def run(api, args, logger):
     def unlink_files(globex):
         for f in glob.glob(globex):
             if os.path.isfile(f):
-                try:
-                    os.unlink(f)
-                except OSError:
-                    pass
+                utils.rmfile(f)
 
     if str(anamon_enabled) in ["true", "1", "y", "yes"]:
-        dirname = "/var/log/cobbler/anamon/%s" % name
+        dirname = os.path.join(PATH_PREFIX, name)
         if os.path.isdir(dirname):
             unlink_files(os.path.join(dirname, "*"))
 
-    # TODO - log somewhere that we cleared a systems anamon logs
+    logger.info('Cleared Anamon logs for "%s".', name)
     return 0
-- 
2.35.1

