From e3b061e94b54067525c5e7f2ac716d1c838c9f20 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 23 Aug 2017 12:55:36 +0200 Subject: [PATCH] - `files/fail2ban.service` renamed as template to `files/fail2ban.service.in`; - setup process generates `build/fail2ban.service` from `files/fail2ban.service.in` using distribution related bin-path; - bug-fixing by running setup with option `--dry-run` (note: specify option `--dry-run` before `install`, like `python setup.py --dry-run install`); - test cases extended to cover dry-run. --- fail2ban/tests/misctestcase.py | 20 +++++++- .../{fail2ban.service => fail2ban.service.in} | 8 ++-- setup.py | 47 ++++++++++++++++--- 3 files changed, 63 insertions(+), 12 deletions(-) rename files/{fail2ban.service => fail2ban.service.in} (70%) diff --git a/fail2ban/tests/misctestcase.py b/fail2ban/tests/misctestcase.py index 77e65d7c..a5e8fc17 100644 --- a/fail2ban/tests/misctestcase.py +++ b/fail2ban/tests/misctestcase.py @@ -101,6 +101,22 @@ class SetupTest(unittest.TestCase): "Seems to be running with python distribution %s" " -- install can be tested only with system distribution %s" % (str(tuple(sys.version_info)), sysVer)) + def testSetupInstallDryRun(self): + if not self.setup: + return # if verbose skip didn't work out + tmp = tempfile.mkdtemp() + # suppress stdout (and stderr) if not heavydebug + supdbgout = ' >/dev/null 2>&1' if unittest.F2B.log_level >= logging.DEBUG else '' # HEAVYDEBUG + try: + # try dry-run: + self.assertEqual(os.system("%s %s --dry-run install --disable-2to3 --root=%s%s" + % (sys.executable, self.setup , tmp, supdbgout)), 0) + # check nothing was created: + self.assertTrue(not os.listdir(tmp)) + finally: + # clean up + shutil.rmtree(tmp) + def testSetupInstallRoot(self): if not self.setup: return # if verbose skip didn't work out @@ -108,8 +124,8 @@ class SetupTest(unittest.TestCase): # suppress stdout (and stderr) if not heavydebug supdbgout = ' >/dev/null' if unittest.F2B.log_level >= logging.DEBUG else '' # HEAVYDEBUG try: - os.system("%s %s install --disable-2to3 --dry-run --root=%s%s" - % (sys.executable, self.setup, tmp, supdbgout)) + self.assertEqual(os.system("%s %s install --disable-2to3 --root=%s%s" + % (sys.executable, self.setup, tmp, supdbgout)), 0) def strippath(l): return [x[len(tmp)+1:] for x in l] diff --git a/files/fail2ban.service b/files/fail2ban.service.in similarity index 70% rename from files/fail2ban.service rename to files/fail2ban.service.in index 6eeba957..7114a938 100644 --- a/files/fail2ban.service +++ b/files/fail2ban.service.in @@ -7,11 +7,11 @@ PartOf=iptables.service firewalld.service [Service] Type=simple ExecStartPre=/bin/mkdir -p /var/run/fail2ban -ExecStart=/usr/bin/fail2ban-server -xf start +ExecStart=@BINDIR@/fail2ban-server -xf start # if should be logged in systemd journal, use following line or set logtarget to stdout in fail2ban.local -# ExecStart=/usr/bin/fail2ban-server -xf --logtarget=stdout start -ExecStop=/usr/bin/fail2ban-client stop -ExecReload=/usr/bin/fail2ban-client reload +# ExecStart=@BINDIR@/fail2ban-server -xf --logtarget=stdout start +ExecStop=@BINDIR@/fail2ban-client stop +ExecReload=@BINDIR@/fail2ban-client reload PIDFile=/var/run/fail2ban/fail2ban.pid Restart=on-failure RestartPreventExitStatus=0 255 diff --git a/setup.py b/setup.py index 5754db49..11748778 100755 --- a/setup.py +++ b/setup.py @@ -50,6 +50,7 @@ except ImportError: import os from os.path import isfile, join, isdir, realpath +import re import sys import warnings from glob import glob @@ -57,11 +58,25 @@ from glob import glob from fail2ban.setup import updatePyExec +source_dir = os.path.realpath(os.path.dirname( + # __file__ seems to be overwritten sometimes on some python versions (e.g. bug of 2.6 by running under cProfile, etc.): + sys.argv[0] if os.path.basename(sys.argv[0]) == 'setup.py' else __file__ +)) + # Wrapper to install python binding (to current python version): class install_scripts_f2b(install_scripts): def get_outputs(self): outputs = install_scripts.get_outputs(self) + # setup.py --dry-run install: + dry_run = not outputs + self.update_scripts(dry_run) + if dry_run: + #bindir = self.install_dir + bindir = self.build_dir + print('creating fail2ban-python binding -> %s (dry-run, real path can be different)' % (bindir,)) + print('Copying content of %s to %s' % (self.build_dir, self.install_dir)); + return outputs fn = None for fn in outputs: if os.path.basename(fn) == 'fail2ban-server': @@ -71,6 +86,27 @@ class install_scripts_f2b(install_scripts): updatePyExec(bindir) return outputs + def update_scripts(self, dry_run=False): + buildroot = os.path.dirname(self.build_dir) + print('Creating %s/fail2ban.service (from fail2ban.service.in): @BINDIR@ -> %s' % (buildroot, self.install_dir)) + with open(os.path.join(source_dir, 'files/fail2ban.service.in'), 'r') as fn: + lines = fn.readlines() + fn = None + if not dry_run: + fn = open(os.path.join(buildroot, 'fail2ban.service'), 'w') + try: + for ln in lines: + ln = re.sub(r'@BINDIR@', lambda v: self.install_dir, ln) + if dry_run: + sys.stdout.write(' | ' + ln) + continue + fn.write(ln) + finally: + if fn: fn.close() + if dry_run: + print(' `') + + # Wrapper to specify fail2ban own options: class install_command_f2b(install): user_options = install.user_options + [ @@ -94,11 +130,7 @@ class install_command_f2b(install): # Update fail2ban-python env to current python version (where f2b-modules located/installed) -rootdir = os.path.realpath(os.path.dirname( - # __file__ seems to be overwritten sometimes on some python versions (e.g. bug of 2.6 by running under cProfile, etc.): - sys.argv[0] if os.path.basename(sys.argv[0]) == 'setup.py' else __file__ -)) -updatePyExec(os.path.join(rootdir, 'bin')) +updatePyExec(os.path.join(source_dir, 'bin')) if setuptools and "test" in sys.argv: import logging @@ -270,5 +302,8 @@ if isdir("/usr/lib/fail2ban"): if sys.argv[1] == "install": print("") print("Please do not forget to update your configuration files.") - print("They are in /etc/fail2ban/.") + print("They are in \"/etc/fail2ban/\".") + print("") + print("You can also install systemd service-unit file from \"build/fail2ban.service\"") + print("resp. corresponding init script from \"files/*-initd\".") print("")