Browse Source

Refactor service restart to support systemd

Helmut Pozimski 10 months ago
parent
commit
23ebd703ba
3 changed files with 18 additions and 8 deletions
  1. 2 1
      amulib/apache.py
  2. 14 5
      amulib/helpers.py
  3. 2 2
      amulib/service.py

+ 2 - 1
amulib/apache.py

@@ -18,6 +18,7 @@ import OpenSSL
 
 from amulib import helpers
 from amulib.cert_path_provider import CertPathProvider
+from amulib.helpers import restart_service
 
 LOGGER = logging.getLogger("acme-updater")
 
@@ -126,7 +127,7 @@ def run(cert_path_provider: CertPathProvider, config=None,
                          "web server")
         else:
             try:
-                subprocess.check_call(["/etc/init.d/apache2", "restart"])
+                restart_service("apache2")
             except subprocess.CalledProcessError:
                 LOGGER.error("Apache restart failed!")
             else:

+ 14 - 5
amulib/helpers.py

@@ -1,4 +1,4 @@
-# SPDX-FileCopyrightText: 2016-2017 Helmut Pozimski <helmut@pozimski.eu>
+# SPDX-FileCopyrightText: 2016-2023 Helmut Pozimski <helmut@pozimski.eu>
 #
 # SPDX-License-Identifier: GPL-2.0-only
 
@@ -13,6 +13,7 @@ import datetime
 import os
 import shutil
 import hashlib
+import subprocess
 
 import OpenSSL
 import dns.tsigkeyring
@@ -299,7 +300,7 @@ def create_tlsa_records(domain, port, certificate, named_key_path,
                        dns_server=dns_server)
 
 
-def get_subject_alt_name(certificate):
+def get_subject_alt_name(certificate) -> list:
     """
     Extracts the subjectAltName entries from a X509 certficiate
 
@@ -309,11 +310,19 @@ def get_subject_alt_name(certificate):
     :return: list of hostnames
     :rtype: list
     """
-    list = []
+    alt_names = []
     for i in range(0, certificate.get_extension_count(), 1):
         if certificate.get_extension(i).get_short_name() == b"subjectAltName":
             extension_string = str(certificate.get_extension(i))
             for entry in extension_string.split(","):
-                list.append(entry.split(":")[1])
+                alt_names.append(entry.split(":")[1])
             break
-    return list
+    return alt_names
+
+
+def restart_service(service_name: str):
+    if os.path.exists("/run/systemd/system"):
+        subprocess.call(["/usr/bin/systemctl", "restart", service_name])
+    else:
+        subprocess.call(["/etc/init.d/%s" % service_name,
+                         "restart"])

+ 2 - 2
amulib/service.py

@@ -19,6 +19,7 @@ import OpenSSL
 
 from amulib import helpers
 from amulib.cert_path_provider import CertPathProvider
+from amulib.helpers import restart_service
 
 LOGGER = logging.getLogger("acme-updater")
 
@@ -111,5 +112,4 @@ def run(cert_path_provider: CertPathProvider, service_name, config,
                 LOGGER.info("Certificate for %s successfully "
                             "renewed, restarting service.",
                             service_name)
-                subprocess.call(["/etc/init.d/%s" % service_name,
-                                 "restart"])
+                restart_service(service_name)