From 39f306f1783b47cae11c80ba31c86be677eff226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Fri, 23 Sep 2016 12:55:47 +0200 Subject: Allow to filter service by regular expressions --- tests/test_distro.py | 17 +++++++++++++++++ whatmaps/command.py | 12 ++++++++++++ whatmaps/distro.py | 16 ++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/tests/test_distro.py b/tests/test_distro.py index f061eab..21df2c0 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -54,3 +54,20 @@ class TestDistro(unittest.TestCase): with patch('os.path.exists', return_value=False): d = detect() self.assertEqual(d.id, 'Debian') + + def test_filter_services_empty(self): + d = Distro() + self.assertEqual(set(['foo', 'bar']), + d.filter_services(['foo', 'bar'])) + + def test_filter_services_one(self): + d = Distro() + d.service_blacklist_re = ['^f..'] + self.assertEqual(set(['bar']), + d.filter_services(['foo', 'bar'])) + + def test_filter_services_all(self): + d = Distro() + d.service_blacklist_re = ['^f..', '^b..'] + self.assertEqual(set(), + d.filter_services(['foo', 'bar'])) diff --git a/whatmaps/command.py b/whatmaps/command.py index 7d17191..e28a8b9 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -129,6 +129,16 @@ def find_systemd_units(procmap, distro): return units +def filter_services(distro, services): + filtered = distro.filter_services(services) + diff = services - filtered + if len(diff): + logging.warning("Filtered out blacklisted service%s %s - restart manually", + 's' if len(diff) > 1 else '', + ', '.join(diff)) + return filtered + + def main(argv): shared_objects = [] services = None @@ -229,6 +239,8 @@ def main(argv): else: return 0 + services = filter_services(distro, services) + if options.restart: if options.print_cmds and services: write_cmd_file(services, options.print_cmds, distro) diff --git a/whatmaps/distro.py b/whatmaps/distro.py index ebeab32..29d2dc2 100644 --- a/whatmaps/distro.py +++ b/whatmaps/distro.py @@ -17,6 +17,7 @@ import logging import os +import re import subprocess @@ -33,6 +34,8 @@ class Distro(object): @cvar id: distro id as returned by lsb-release @cvar service_blacklist: services that should never be restarted + @cvar service_blacklist_re: regex list of services that should + never be restartet @cvar _pkg_services: A C{dict} that maps packages to services. In case we find binaries that match a key in this hash restart the services listed in values. @@ -41,6 +44,8 @@ class Distro(object): """ id = None service_blacklist = set() + service_blacklist_re = set() + _pkg_services = {} _pkg_blacklist = {} _pkg_service_blacklist = {} @@ -87,6 +92,17 @@ class Distro(object): """ return klass._pkg_service_blacklist.get(pkg.name, []) + def filter_services(self, services): + """ + Filter out servies that match service_blacklist_re + """ + ret = [] + matchers = [re.compile(b) for b in self.service_blacklist_re] + for s in services: + if not any([m.match(s) for m in matchers]): + ret.append(s) + return set(ret) + @classmethod def has_apt(klass): """Does the distribution use apt""" -- cgit v1.2.1