- `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.
pull/1874/head
sebres 2017-08-23 12:55:36 +02:00
parent 1d5fbb95ae
commit e3b061e94b
3 changed files with 63 additions and 12 deletions

View File

@ -101,6 +101,22 @@ class SetupTest(unittest.TestCase):
"Seems to be running with python distribution %s" "Seems to be running with python distribution %s"
" -- install can be tested only with system distribution %s" % (str(tuple(sys.version_info)), sysVer)) " -- 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): def testSetupInstallRoot(self):
if not self.setup: if not self.setup:
return # if verbose skip didn't work out return # if verbose skip didn't work out
@ -108,8 +124,8 @@ class SetupTest(unittest.TestCase):
# suppress stdout (and stderr) if not heavydebug # suppress stdout (and stderr) if not heavydebug
supdbgout = ' >/dev/null' if unittest.F2B.log_level >= logging.DEBUG else '' # HEAVYDEBUG supdbgout = ' >/dev/null' if unittest.F2B.log_level >= logging.DEBUG else '' # HEAVYDEBUG
try: try:
os.system("%s %s install --disable-2to3 --dry-run --root=%s%s" self.assertEqual(os.system("%s %s install --disable-2to3 --root=%s%s"
% (sys.executable, self.setup, tmp, supdbgout)) % (sys.executable, self.setup, tmp, supdbgout)), 0)
def strippath(l): def strippath(l):
return [x[len(tmp)+1:] for x in l] return [x[len(tmp)+1:] for x in l]

View File

@ -7,11 +7,11 @@ PartOf=iptables.service firewalld.service
[Service] [Service]
Type=simple Type=simple
ExecStartPre=/bin/mkdir -p /var/run/fail2ban 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 # 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 # ExecStart=@BINDIR@/fail2ban-server -xf --logtarget=stdout start
ExecStop=/usr/bin/fail2ban-client stop ExecStop=@BINDIR@/fail2ban-client stop
ExecReload=/usr/bin/fail2ban-client reload ExecReload=@BINDIR@/fail2ban-client reload
PIDFile=/var/run/fail2ban/fail2ban.pid PIDFile=/var/run/fail2ban/fail2ban.pid
Restart=on-failure Restart=on-failure
RestartPreventExitStatus=0 255 RestartPreventExitStatus=0 255

View File

@ -50,6 +50,7 @@ except ImportError:
import os import os
from os.path import isfile, join, isdir, realpath from os.path import isfile, join, isdir, realpath
import re
import sys import sys
import warnings import warnings
from glob import glob from glob import glob
@ -57,11 +58,25 @@ from glob import glob
from fail2ban.setup import updatePyExec 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): # Wrapper to install python binding (to current python version):
class install_scripts_f2b(install_scripts): class install_scripts_f2b(install_scripts):
def get_outputs(self): def get_outputs(self):
outputs = install_scripts.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 fn = None
for fn in outputs: for fn in outputs:
if os.path.basename(fn) == 'fail2ban-server': if os.path.basename(fn) == 'fail2ban-server':
@ -71,6 +86,27 @@ class install_scripts_f2b(install_scripts):
updatePyExec(bindir) updatePyExec(bindir)
return outputs 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: # Wrapper to specify fail2ban own options:
class install_command_f2b(install): class install_command_f2b(install):
user_options = install.user_options + [ 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) # Update fail2ban-python env to current python version (where f2b-modules located/installed)
rootdir = os.path.realpath(os.path.dirname( updatePyExec(os.path.join(source_dir, 'bin'))
# __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'))
if setuptools and "test" in sys.argv: if setuptools and "test" in sys.argv:
import logging import logging
@ -270,5 +302,8 @@ if isdir("/usr/lib/fail2ban"):
if sys.argv[1] == "install": if sys.argv[1] == "install":
print("") print("")
print("Please do not forget to update your configuration files.") 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("") print("")