From 6d8d500a5f001dc0c84e0f2890b16f56409a62bb Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 26 Feb 2016 08:13:36 +0100 Subject: Drop python2 support in tox.ini as well --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 68a25f8..346069a 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py27, py33, py34 +envlist = py34, py35 [testenv] commands = python setup.py nosetests -- cgit v1.2.3 From 73afbae396ca636140d7732eef46994aebbf25bf Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Thu, 7 Apr 2016 21:18:21 +0200 Subject: systemd: Shorten error message if proess is from a user session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No need to print the whole process tree which so far looked like WARNING: No systemd unit found for '/bin/bash': Can't parse service name from ● session-8762.scope - Session 8762 of user root Loaded: loaded Drop-In: /run/systemd/system/session-8762.scope.d └─50-After-systemd-logind\x2eservice.conf, 50-After-systemd-user-sessions\x2eservice.conf, 50-Description.conf, 50-SendSIGHUP.conf, 50-Slice.conf Active: active (running) since Thu 2016-04-07 20:53:52 CEST; 19min ago CGroup: /user.slice/user-0.slice/session-8762.scope ├─21150 sshd: root@pts/0 ├─21155 -bash ├─23956 /usr/bin/python /usr/bin/whatmaps --debug libc6 └─23962 systemctl status 21155 --- tests/test_systemd.py | 20 ++++++++++++++++++++ whatmaps/systemd.py | 3 +++ 2 files changed, 23 insertions(+) diff --git a/tests/test_systemd.py b/tests/test_systemd.py index f5af3c9..0559ccc 100644 --- a/tests/test_systemd.py +++ b/tests/test_systemd.py @@ -60,3 +60,23 @@ class TestSystemd(unittest.TestCase): result = Systemd().process_to_unit(p) self.assertIsNone(result) + def test_process_user_session(self): + p = Process(952) + output = """● session-8762.scope - Session 8762 of user root + Loaded: loaded + Drop-In: /run/systemd/system/session-8762.scope.d + └─50-After-systemd-logind\x2eservice.conf, 50-After-systemd-user-sessions\x2eservice.conf, 50-Description.conf, 50-SendSIGHUP.conf, 50-Slice.conf + Active: active (running) since Thu 2016-04-07 20:53:52 CEST; 19min ago + CGroup: /user.slice/user-0.slice/session-8762.scope + ├─21150 sshd: root@pts/0 + ├─21155 -bash + ├─23956 /usr/bin/python /usr/bin/whatmaps --debug libc6 + └─23962 systemctl status 21155 + """.encode('utf-8') + with patch('os.path.exists', return_value=True): + with patch('subprocess.Popen') as mock: + PopenMock = mock.return_value + PopenMock.communicate.return_value = [output] + PopenMock.returncode = 0 + with self.assertRaisesRegexp(ValueError, "Can't parse service name from session-8762.scope - Session 8762 of user root"): + Systemd().process_to_unit(p) diff --git a/whatmaps/systemd.py b/whatmaps/systemd.py index 9bc03b1..d4f45fe 100644 --- a/whatmaps/systemd.py +++ b/whatmaps/systemd.py @@ -43,5 +43,8 @@ class Systemd(object): return parts[0] elif parts[1].endswith('.service'): return parts[1] + elif parts[1].startswith('session-') and parts[1].endswith('.scope'): + msg = output.decode('utf-8').split('\n')[0][2:] + raise ValueError("Can't parse service name from %s" % msg) else: raise ValueError("Can't parse service name from: (%s %s)" % (parts[0], parts[1])) -- cgit v1.2.3 From 0bcd51ef848059359c4a9a37d2ecf3b8efc8bf81 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 18 Apr 2016 08:30:05 +0200 Subject: Don't bail out if we can't find a package in apt's cache Rather warn about it and continue so we get restarts for all other packages. --- whatmaps/command.py | 4 +++- whatmaps/debiandistro.py | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/whatmaps/command.py b/whatmaps/command.py index 0f8e433..263f964 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -174,7 +174,9 @@ def main(argv): return 1 if not pkgs: return 0 - pkgs = distro.filter_security_updates(pkgs) + pkgs, notfound = distro.filter_security_updates(pkgs) + if notfound: + logging.warning("Pkgs %s not found in apt cache" % ", ".join(notfound)) logging.debug("Security Upgrades: %s" % pkgs) else: parser.print_help() diff --git a/whatmaps/debiandistro.py b/whatmaps/debiandistro.py index 32f3774..1a1dbd4 100644 --- a/whatmaps/debiandistro.py +++ b/whatmaps/debiandistro.py @@ -165,9 +165,14 @@ class DebianDistro(Distro): security_update_origins = klass._security_update_origins() security_updates = {} + notfound = [] for pkg in list(pkgs.values()): - cache_pkg = cache[pkg.name] + try: + cache_pkg = cache[pkg.name] + except KeyError: + notfound.append(pkg) + continue for cache_version in cache_pkg.version_list: if pkg.version == cache_version.ver_str: for pfile, _ in cache_version.file_list: @@ -176,4 +181,4 @@ class DebianDistro(Distro): pfile.archive == origin[1]: security_updates[pkg] = pkg break - return security_updates + return (security_updates, notfound) -- cgit v1.2.3 From 6470d6c6e9d1ec48ed586cc60e295460b4c78176 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 10:01:52 +0200 Subject: Add missing spaces in warnings --- whatmaps/command.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/whatmaps/command.py b/whatmaps/command.py index 263f964..f59b214 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -117,11 +117,11 @@ def find_systemd_units(procmap, distro): try: unit = Systemd.process_to_unit(proc) except ValueError as e: - logging.warning("No systemd unit found for '%s': %s" + logging.warning("No systemd unit found for '%s': %s " "- restart manually" % (proc.exe, e)) continue if not unit: - logging.warning("No systemd unit found for '%s'" + logging.warning("No systemd unit found for '%s' " "- restart manually" % proc.exe) else: units.add(unit) -- cgit v1.2.3 From 26c214f301bde8153df90b13109e4cae7cdd95b6 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:37:24 +0200 Subject: Setup flake8 --- setup.cfg | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/setup.cfg b/setup.cfg index ee9ac0e..aca3f68 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,6 @@ [nosetests] cover-package=whatmaps + +[flake8] +# E501: ignore line length +ignore=E501 -- cgit v1.2.3 From 4592eb9c8269aaa2459fd620726209c9586d861c Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:37:30 +0200 Subject: whatmaps/command.py: flake8 clean --- whatmaps/command.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/whatmaps/command.py b/whatmaps/command.py index f59b214..913268c 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -17,7 +17,6 @@ # - import errno import glob import os @@ -30,15 +29,16 @@ from . distro import Distro from . pkg import PkgError from . systemd import Systemd + def check_maps(procs, shared_objects): restart_procs = {} for proc in procs: for so in shared_objects: if proc.maps(so): if proc.exe in restart_procs: - restart_procs[proc.exe] += [ proc ] + restart_procs[proc.exe] += [proc] else: - restart_procs[proc.exe] = [ proc ] + restart_procs[proc.exe] = [proc] break return restart_procs @@ -78,7 +78,7 @@ def find_pkgs(procs, distro): if pkg.name in pkgs: pkgs[pkg.name].procs.append(proc) else: - pkg.procs = [ proc ] + pkg.procs = [proc] pkgs[pkg.name] = pkg if pkgs: @@ -125,7 +125,7 @@ def find_systemd_units(procmap, distro): "- restart manually" % proc.exe) else: units.add(unit) - units -= set([ service + '.service' for service in distro.service_blacklist ]) + units -= set([service + '.service' for service in distro.service_blacklist]) return units @@ -165,7 +165,7 @@ def main(argv): logging.debug("Detected distribution: '%s'", distro.id) if args: - pkgs = [ distro.pkg(arg) for arg in args ] + pkgs = [distro.pkg(arg) for arg in args] elif options.apt and distro.has_apt(): try: pkgs = distro.read_apt_pipeline() @@ -242,6 +242,7 @@ def main(argv): return 0 + def run(): return(main(sys.argv)) -- cgit v1.2.3 From 70b6489772f865e0bb782b1f3307b19c689267bd Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:40:36 +0200 Subject: whatmaps/distro.py: flake8 clean --- whatmaps/distro.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/whatmaps/distro.py b/whatmaps/distro.py index f0f08eb..ebeab32 100644 --- a/whatmaps/distro.py +++ b/whatmaps/distro.py @@ -25,6 +25,7 @@ try: except ImportError: lsb_release = None + class Distro(object): """ A distribution @@ -75,8 +76,8 @@ class Distro(object): List of services that package pkg needs restarted that aren't part of pkg itself """ - return [ s for s in klass._pkg_services.get(pkg.name, []) - if klass.is_service_installed(s) ] + return [s for s in klass._pkg_services.get(pkg.name, []) + if klass.is_service_installed(s)] @classmethod def pkg_service_blacklist(klass, pkg): @@ -95,8 +96,9 @@ class Distro(object): def detect(): return detect() -import whatmaps.debiandistro -import whatmaps.redhatdistro +import whatmaps.debiandistro # noqa: E402 +import whatmaps.redhatdistro # noqa: E402 + def detect(): """ @@ -110,11 +112,11 @@ def detect(): else: try: lsb_cmd = subprocess.Popen(['lsb_release', '--id', '-s'], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) output = lsb_cmd.communicate()[0] if not lsb_cmd.returncode: - id = output.strip() + id = output.strip() except OSError: # id is None in this case pass -- cgit v1.2.3 From 91100e0a2927a58dbd6fa8723fbe6741bc06cf23 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:42:37 +0200 Subject: whatmaps/pkg.py: flake8 clean --- whatmaps/pkg.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/whatmaps/pkg.py b/whatmaps/pkg.py index 4290d0b..7aafd5a 100644 --- a/whatmaps/pkg.py +++ b/whatmaps/pkg.py @@ -19,6 +19,7 @@ import re import string import subprocess + class PkgError(Exception): pass @@ -55,8 +56,8 @@ class Pkg(object): if self._contents: return self._contents else: - cmd = [ string.Template(arg).substitute(arg, pkg_name=self.name) - for arg in self._list_contents ] + cmd = [string.Template(arg).substitute(arg, pkg_name=self.name) + for arg in self._list_contents] list_contents = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) -- cgit v1.2.3 From eb3bc11820c454e3565687a04e2e5f7e212bb759 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:43:41 +0200 Subject: whatmaps/redhatdistro.py: flake8 clean --- whatmaps/redhatdistro.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/whatmaps/redhatdistro.py b/whatmaps/redhatdistro.py index 0427c30..4b17f3c 100644 --- a/whatmaps/redhatdistro.py +++ b/whatmaps/redhatdistro.py @@ -21,10 +21,11 @@ import subprocess from . distro import Distro from . rpmpkg import RpmPkg + class RedHatDistro(Distro): "RPM based distribution""" _pkg_re = re.compile(r'(?P[\w\-\+]+)-(?P[\w\.]+)' - '-(?P[\w\.]+)\.(?P.+)') + '-(?P[\w\.]+)\.(?P.+)') @classmethod def pkg(klass, name): @@ -33,8 +34,8 @@ class RedHatDistro(Distro): @classmethod def pkg_by_file(klass, path): find_file = subprocess.Popen(['rpm', '-qf', path], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) output = find_file.communicate()[0] if find_file.returncode: return None @@ -52,4 +53,3 @@ class RedHatDistro(Distro): class FedoraDistro(RedHatDistro): id = 'Fedora' - -- cgit v1.2.3 From 42ef43a811fc2931801f6d72faa0c0accf15beef Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:44:42 +0200 Subject: whatmaps/rpmpkg.py: flake8 clean --- whatmaps/rpmpkg.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/whatmaps/rpmpkg.py b/whatmaps/rpmpkg.py index 595edd2..493dd5a 100644 --- a/whatmaps/rpmpkg.py +++ b/whatmaps/rpmpkg.py @@ -19,17 +19,18 @@ import re from . pkg import Pkg + class RpmPkg(Pkg): type = 'RPM' _init_script_re = re.compile('/etc/rc.d/init.d/[\w\-\.]') - _list_contents = [ 'rpm', '-ql', '$pkg_name' ] + _list_contents = ['rpm', '-ql', '$pkg_name'] def __init__(self, name): Pkg.__init__(self, name) @property def services(self): - if self._services != None: + if self._services is not None: return self._services self._services = [] -- cgit v1.2.3 From ea62f77365332727a48cfc7a9fb1f787420de5c7 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:45:34 +0200 Subject: whatmaps/debianpkg.py: flake8 clean --- whatmaps/debianpkg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/whatmaps/debianpkg.py b/whatmaps/debianpkg.py index 8a3349d..0192513 100644 --- a/whatmaps/debianpkg.py +++ b/whatmaps/debianpkg.py @@ -19,17 +19,18 @@ import re from . pkg import Pkg + class DebianPkg(Pkg): type = 'Debian' _init_script_re = re.compile('/etc/init.d/[\w\-\.]') - _list_contents = ['dpkg-query', '-L', '${pkg_name}' ] + _list_contents = ['dpkg-query', '-L', '${pkg_name}'] def __init__(self, name): Pkg.__init__(self, name) @property def services(self): - if self._services != None: + if self._services is not None: return self._services self._services = [] @@ -39,4 +40,3 @@ class DebianPkg(Pkg): if self._init_script_re.match(line): self._services.append(os.path.basename(line.strip())) return self._services - -- cgit v1.2.3 From f54fd46a0dd6734be0fa4c1ac5de7a3af41a21a9 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:53:23 +0200 Subject: whatmaps/debiandistro.py: flake8 clean --- whatmaps/debiandistro.py | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/whatmaps/debiandistro.py b/whatmaps/debiandistro.py index 1a1dbd4..bb3f5cb 100644 --- a/whatmaps/debiandistro.py +++ b/whatmaps/debiandistro.py @@ -36,26 +36,28 @@ from . debianpkg import DebianPkg from . pkg import PkgError from . systemd import Systemd + class DebianDistro(Distro): "Debian (dpkg) based distribution" id = 'Debian' - _pkg_services = { 'apache2-mpm-worker': [ 'apache2' ], - 'apache2-mpm-prefork': [ 'apache2' ], - 'apache2.2-bin': [ 'apache2' ], - 'apache2-bin': [ 'apache2' ], - 'dovecot-imapd': [ 'dovecot' ], - 'dovecot-pop3d': [ 'dovecot' ], - 'exim4-daemon-light': [ 'exim4' ], - 'exim4-daemon-heavy': [ 'exim4' ], - 'libvirt-daemon': [ 'libvirtd' ], - 'openjdk-6-jre-headless': ['jenkins', 'tomcat7'], - 'openjdk-7-jre-headless': ['jenkins', 'tomcat7'], - 'qemu-system-x86_64': [ 'libvirt-guests' ], - } + _pkg_services = { + 'apache2-mpm-worker': ['apache2'], + 'apache2-mpm-prefork': ['apache2'], + 'apache2.2-bin': ['apache2'], + 'apache2-bin': ['apache2'], + 'dovecot-imapd': ['dovecot'], + 'dovecot-pop3d': ['dovecot'], + 'exim4-daemon-light': ['exim4'], + 'exim4-daemon-heavy': ['exim4'], + 'libvirt-daemon': ['libvirtd'], + 'openjdk-6-jre-headless': ['jenkins', 'tomcat7'], + 'openjdk-7-jre-headless': ['jenkins', 'tomcat7'], + 'qemu-system-x86_64': ['libvirt-guests'], + } # Per package blacklist - _pkg_service_blacklist = { 'libvirt-bin': [ 'libvirt-guests' ] } + _pkg_service_blacklist = {'libvirt-bin': ['libvirt-guests']} # Per distro blacklist service_blacklist = set(['kvm', 'qemu-kvm', 'qemu-system-x86']) @@ -67,8 +69,8 @@ class DebianDistro(Distro): @classmethod def pkg_by_file(klass, path): find_file = subprocess.Popen(['dpkg-query', '-S', path], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) output = find_file.communicate()[0] if find_file.returncode: return None @@ -124,12 +126,11 @@ class DebianDistro(Distro): (pkgname, oldversion, compare, newversion, filename) = line.split() if filename == '**CONFIGURE**': - if oldversion != '-': # Updates only + if oldversion != '-': # Updates only pkgs[pkgname] = DebianPkg(pkgname) pkgs[pkgname].version = newversion return pkgs - @classmethod def _security_update_origins(klass): "Determine security update origins from apt configuration" @@ -138,9 +139,10 @@ class DebianDistro(Distro): raise PkgError("lsb_release not found, can't determine security updates") codename = lsb_release.get_distro_information()['CODENAME'] + def _subst(line): - mapping = {'distro_codename' : codename, - 'distro_id' : klass.id, } + mapping = {'distro_codename': codename, + 'distro_id': klass.id, } return string.Template(line).substitute(mapping) origins = [] @@ -151,7 +153,6 @@ class DebianDistro(Distro): logging.debug("Security Update Origins: %s", origins) return origins - @classmethod def filter_security_updates(klass, pkgs): """Filter on security updates""" @@ -160,7 +161,7 @@ class DebianDistro(Distro): raise PkgError("apt_pkg not installed, can't determine security updates") apt_pkg.init() - acquire = apt_pkg.Acquire() + apt_pkg.Acquire() cache = apt_pkg.Cache() security_update_origins = klass._security_update_origins() -- cgit v1.2.3 From 2495852ae597b81d18177a716667106454ff2dbb Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:53:58 +0200 Subject: whatmaps/process.py: flake8 clean --- whatmaps/process.py | 1 + 1 file changed, 1 insertion(+) diff --git a/whatmaps/process.py b/whatmaps/process.py index a39da89..78165a7 100644 --- a/whatmaps/process.py +++ b/whatmaps/process.py @@ -20,6 +20,7 @@ import logging import os import re + class Process(object): """A process - Linux only so far, needs /proc mounted""" deleted_re = re.compile(r"(?P.*) \(deleted\)$") -- cgit v1.2.3 From b8c698a18e6b3d28b3ec9470e9ec86594bd755da Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:55:02 +0200 Subject: tests/test_process.py: flake8 clean --- tests/test_process.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_process.py b/tests/test_process.py index 91f5431..e9cc1e0 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -15,7 +15,6 @@ """Test L{whatmaps.process} config""" import os -import sys import unittest import random @@ -23,6 +22,7 @@ from whatmaps.process import Process from . import context + class TestWhatmapsProcess(unittest.TestCase): def setUp(self): self.tmpdir = context.new_tmpdir(__name__) @@ -33,7 +33,7 @@ class TestWhatmapsProcess(unittest.TestCase): self.exe = os.path.join(self.piddir, 'exe') self.cmdline = os.path.join(self.piddir, 'cmdline') self.maps = os.path.join(self.piddir, 'maps') - self._write_cmdline('doesnotmatter') # Write at least an empty cmdline + self._write_cmdline('doesnotmatter') # Write at least an empty cmdline self._write_exe_symlink('acommand') self._write_maps([['f32b43221000-7f32b4522000', '---p', @@ -47,7 +47,7 @@ class TestWhatmapsProcess(unittest.TestCase): 'fe:02', '1704011', '/lib/x86_64-linux-gnu/libselinux.so.1'], - ]) + ]) def _write_exe_symlink(self, name): exe = os.path.join(str(self.tmpdir), name) -- cgit v1.2.3 From 5297e8191f4ed83f69f73d696f3d372e973a0115 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:55:31 +0200 Subject: tests/test_redhatdistro.py: flake8 clean --- tests/test_redhatdistro.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_redhatdistro.py b/tests/test_redhatdistro.py index 438adf8..ab39426 100644 --- a/tests/test_redhatdistro.py +++ b/tests/test_redhatdistro.py @@ -20,6 +20,7 @@ from mock import patch from whatmaps.redhatdistro import RedHatDistro from whatmaps.rpmpkg import RpmPkg + class TestRedHatDistro(unittest.TestCase): def test_vars(self): """Check RedHat distro vars""" -- cgit v1.2.3 From 4d3b85c54bef1ca6bf50dd5e9d7fb42177a48951 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:56:01 +0200 Subject: tests/test_debianpkg.py: flake8 clean --- tests/test_debianpkg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_debianpkg.py b/tests/test_debianpkg.py index 72ce8fb..6c130ed 100644 --- a/tests/test_debianpkg.py +++ b/tests/test_debianpkg.py @@ -20,10 +20,10 @@ from mock import patch from whatmaps.debianpkg import DebianPkg + class TestDebianPkg(unittest.TestCase): def test_services(self): with patch('whatmaps.pkg.Pkg._get_contents') as mock: mock.return_value = ['/etc/init.d/aservice', '/usr/bin/afile'] p = DebianPkg('doesnotmatter') self.assertEqual(p.services, ['aservice']) - -- cgit v1.2.3 From aef088dea5c1a93806927e2b1d7a4a7d87127c3a Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:56:51 +0200 Subject: tests/test_systemd.py: flake8 clean --- tests/test_systemd.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_systemd.py b/tests/test_systemd.py index 0559ccc..7615497 100644 --- a/tests/test_systemd.py +++ b/tests/test_systemd.py @@ -19,12 +19,13 @@ import unittest from mock import patch from whatmaps.systemd import Systemd -from whatmaps.process import Process + class Process(object): def __init__(self, pid): self.pid = pid + class TestSystemd(unittest.TestCase): def test_is_init(self): """Check if we create a systemd object if systemd is the init system in use""" -- cgit v1.2.3 From ac345eb4059c5e59a0b4782ee6b8c20a2486331a Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:59:08 +0200 Subject: tests/test_distro.py: flake8 clean --- tests/test_distro.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_distro.py b/tests/test_distro.py index 409c1c6..f061eab 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -19,16 +19,17 @@ import unittest from mock import patch try: - import lsb_release - have_lsb_release=True + import lsb_release # noqa: F401 + have_lsb_release = True except ImportError: - have_lsb_release=False - + have_lsb_release = False from whatmaps.distro import Distro, detect + class Pkg(object): name = 'doesnotmatter' + class TestDistro(unittest.TestCase): def test_abstract(self): """Check abstract method signatures""" @@ -45,7 +46,6 @@ class TestDistro(unittest.TestCase): self.assertEqual(Distro.pkg_service_blacklist(Pkg), []) self.assertFalse(Distro.has_apt()) - @unittest.skipUnless(have_lsb_release, "lsb_release not installed") def test_detect_via_lsb_release_module(self): "Detect distro via lsb_release" -- cgit v1.2.3 From fa101afec1d8fae56d6f1d9489ea1672ceefdcea Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 11:59:39 +0200 Subject: tests/context.py: flake8 clean --- tests/context.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/context.py b/tests/context.py index 5878771..8900a30 100644 --- a/tests/context.py +++ b/tests/context.py @@ -29,19 +29,22 @@ projectdir = os.path.dirname(os.path.dirname(os.path.abspath(whatmaps.__file__)) _chdir_backup = None _tmpdirs = [] + def chdir(dir): global _chdir_backup if not _chdir_backup: _chdir_backup = os.path.abspath(os.curdir) os.chdir(str(dir)) + def new_tmpdir(name): global _tmpdirs - prefix='whatmaps_%s_' % name + prefix = 'whatmaps_%s_' % name tmpdir = TmpDir(prefix) _tmpdirs.append(tmpdir) return tmpdir + def teardown(): if _chdir_backup: os.chdir(_chdir_backup) @@ -49,6 +52,7 @@ def teardown(): tmpdir.rmdir() del _tmpdirs[:] + class TmpDir(object): def __init__(self, suffix='', prefix='tmp'): -- cgit v1.2.3 From 9a83475381144df8b896766667da69c527819386 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:00:40 +0200 Subject: tests/test_pkg.py: flake8 clean --- tests/test_pkg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_pkg.py b/tests/test_pkg.py index b7707b8..2f2ef56 100644 --- a/tests/test_pkg.py +++ b/tests/test_pkg.py @@ -15,12 +15,13 @@ """Test L{whatmaps.process} config""" import unittest -from mock import patch +from mock import patch from whatmaps.pkg import Pkg, PkgError from . import context + class TestPkg(unittest.TestCase): def setUp(self): self.tmpdir = context.new_tmpdir(__name__) -- cgit v1.2.3 From 4383678f7f25cc49c284d140a73f6ac03b951c3a Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:02:20 +0200 Subject: tests/test_debiandistro.py: flake8 clean --- tests/test_debiandistro.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/test_debiandistro.py b/tests/test_debiandistro.py index 617005c..b976d65 100644 --- a/tests/test_debiandistro.py +++ b/tests/test_debiandistro.py @@ -18,20 +18,21 @@ import unittest from mock import patch try: - import apt_pkg - have_apt_pkg=True + import apt_pkg # noqa: F401 + have_apt_pkg = True except ImportError: - have_apt_pkg=False + have_apt_pkg = False try: - import lsb_release - have_lsb_release=True + import lsb_release # noqa: F401 + have_lsb_release = True except ImportError: - have_lsb_release=False + have_lsb_release = False from whatmaps.debiandistro import DebianDistro from whatmaps.debianpkg import DebianPkg + class TestDebianDistro(unittest.TestCase): def test_vars(self): """Check Debian distro vars""" @@ -100,7 +101,7 @@ class TestDebianDistro(unittest.TestCase): def test_filter_security_updates(self, apt_pkg_acquire, apt_pkg_init): pkgs = {'pkg1': DebianPkg('pkg1'), 'pkg2': DebianPkg('pkg2'), - } + } with patch('apt_pkg.Cache'): DebianDistro.filter_security_updates(pkgs) apt_pkg_init.assert_called_once_with() -- cgit v1.2.3 From e34b48e2376ff19a598f48cdc42c5053f2e77193 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:05:20 +0200 Subject: setup.py: flake8 clean --- setup.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/setup.py b/setup.py index be05caa..6cdf17b 100644 --- a/setup.py +++ b/setup.py @@ -15,30 +15,32 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os from setuptools import setup data_files = [] try: import lsb_release - if (lsb_release.get_distro_information()['ID'] in [ 'Debian' ] or - os.path.exists('/etc/debian_version')): - data_files = [('../etc/apt/apt.conf.d/', - ['apt/50whatmaps_apt']), - ('../etc/apt/apt.conf.d/', - ['apt/20services']), - ] + if (lsb_release.get_distro_information()['ID'] in ['Debian'] or + os.path.exists('/etc/debian_version')): + data_files = [('../etc/apt/apt.conf.d/', + ['apt/50whatmaps_apt']), + ('../etc/apt/apt.conf.d/', + ['apt/20services']), + ] except ImportError: pass -setup(name = "whatmaps", - author = 'Guido Günther', - author_email = 'agx@sigxcpu.org', - data_files = data_files, - packages = ['whatmaps'], - entry_points = { - 'console_scripts': [ 'whatmaps = whatmaps.command:run' ], + +setup(name="whatmaps", + author='Guido Günther', + author_email='agx@sigxcpu.org', + data_files=data_files, + packages=['whatmaps'], + entry_points={ + 'console_scripts': ['whatmaps = whatmaps.command:run'], }, -) + ) # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: -- cgit v1.2.3 From 47ffad9d15dc5cf15eb4b892a4e07e6e6e99438e Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:05:54 +0200 Subject: tests/test_rpmpkg.py: flake8 clean --- tests/test_rpmpkg.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_rpmpkg.py b/tests/test_rpmpkg.py index 064f059..0eb2175 100644 --- a/tests/test_rpmpkg.py +++ b/tests/test_rpmpkg.py @@ -20,6 +20,7 @@ from mock import patch from whatmaps.rpmpkg import RpmPkg + class TestRpmPkg(unittest.TestCase): def test_services(self): with patch('whatmaps.pkg.Pkg._get_contents') as mock: -- cgit v1.2.3 From 92cace0d2a4d90f1e1cb1ac19c69a262b8cd8ed3 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:08:11 +0200 Subject: Add .travis.yml --- .travis.yml | 7 +++++++ requirements.txt | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 .travis.yml create mode 100644 requirements.txt diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..838cd99 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +--- +language: python +python: + - "3.4" + - "3.5" +install: "pip install -r requirements.txt" +script: flake8 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4da1296 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +coverage>=2.85 +flake8>=3 +mock +nose>=0.11.1 +nosexcover>=1.0.7 -- cgit v1.2.3 From 9cb7517443b8dc25596a3d3b1fb75f05380c1f19 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:12:22 +0200 Subject: Don't abort if we can't parse a single package so far we would abort the whole operation, skip the unreadable packages instead and return with non-zero. --- whatmaps/command.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/whatmaps/command.py b/whatmaps/command.py index 913268c..8d0be31 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -132,6 +132,7 @@ def find_systemd_units(procmap, distro): def main(argv): shared_objects = [] services = None + ret = 0 parser = OptionParser(usage='%prog [options] pkg1 [pkg2 pkg3 pkg4]') parser.add_option("--debug", action="store_true", dest="debug", @@ -187,8 +188,8 @@ def main(argv): try: shared_objects += pkg.shared_objects except PkgError: - logging.error("Cannot parse contents of %s" % pkg.name) - return 1 + logging.error("Cannot parse contents of %s - skipping it" % pkg.name) + ret = 1 logging.debug("Found shared objects:") for so in shared_objects: logging.debug(" %s", so) @@ -240,7 +241,7 @@ def main(argv): for s in services: print(s) - return 0 + return ret def run(): -- cgit v1.2.3 From 04ec8384e2b7332c5f7d047ed28b91c9d15c7327 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 13:03:38 +0200 Subject: command: Use a distro instance instead of class so we can invoke non-static and non-class methods --- whatmaps/command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whatmaps/command.py b/whatmaps/command.py index 8d0be31..7d17191 100755 --- a/whatmaps/command.py +++ b/whatmaps/command.py @@ -158,7 +158,7 @@ def main(argv): logging.basicConfig(level=level, format='%(levelname)s: %(message)s') - distro = Distro.detect() + distro = Distro.detect()() if not distro: logging.error("Unsupported Distribution") return 1 -- cgit v1.2.3 From 39f306f1783b47cae11c80ba31c86be677eff226 Mon Sep 17 00:00:00 2001 From: Guido Günther 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.3 From 48651cda01095017ddc5e67a2491c02a44027ab5 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Sep 2016 12:57:07 +0200 Subject: Filter out user@.service since this aborts user sessions otherwise Closes: #833120 --- whatmaps/debiandistro.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/whatmaps/debiandistro.py b/whatmaps/debiandistro.py index bb3f5cb..4fcce54 100644 --- a/whatmaps/debiandistro.py +++ b/whatmaps/debiandistro.py @@ -62,6 +62,11 @@ class DebianDistro(Distro): # Per distro blacklist service_blacklist = set(['kvm', 'qemu-kvm', 'qemu-system-x86']) + # Per distro regex filter + service_blacklist_re = set([ + '^user@[0-9]+.service$', # Restarting systemd user service aborts the session + ]) + @classmethod def pkg(klass, name): return DebianPkg(name) -- cgit v1.2.3