Changeset View
Standalone View
kolabd/__init__.py
# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com) | # Copyright 2010-2016 Kolab Systems AG (http://www.kolabsys.com) | ||||
# | # | ||||
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> | # Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> | ||||
# | # | ||||
# This program is free software: you can redistribute it and/or modify | # This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU General Public License as published by | # it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | # the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | # (at your option) any later version. | ||||
Show All 25 Lines | |||||
from pykolab import utils | from pykolab import utils | ||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
from process import KolabdProcess as Process | from process import KolabdProcess as Process | ||||
log = pykolab.getLogger('pykolab.daemon') | log = pykolab.getLogger('pykolab.daemon') | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
class KolabDaemon(object): | class KolabDaemon(object): | ||||
def __init__(self): | def __init__(self): | ||||
""" | """ | ||||
The main Kolab Groupware daemon process. | The main Kolab Groupware daemon process. | ||||
""" | """ | ||||
daemon_group = conf.add_cli_parser_option_group(_("Daemon Options")) | daemon_group = conf.add_cli_parser_option_group(_("Daemon Options")) | ||||
daemon_group.add_option( | daemon_group.add_option( | ||||
"--fork", | "--fork", | ||||
dest = "fork_mode", | dest = "fork_mode", | ||||
Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
action = "store_true", | action = "store_true", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
default = False, | default = False, | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
help = _("Fork to the background.") | help = _("Fork to the background.") | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
) | ) | ||||
daemon_group.add_option( | daemon_group.add_option( | ||||
"-p", | "-p", | ||||
"--pid-file", | "--pid-file", | ||||
dest = "pidfile", | dest = "pidfile", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
action = "store", | action = "store", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
default = "/var/run/kolabd/kolabd.pid", | default = "/var/run/kolabd/kolabd.pid", | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
help = _("Path to the PID file to use.") | help = _("Path to the PID file to use.") | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
) | ) | ||||
daemon_group.add_option( | daemon_group.add_option( | ||||
"-u", | "-u", | ||||
"--user", | "--user", | ||||
dest = "process_username", | dest = "process_username", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
action = "store", | action = "store", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
default = "kolab", | default = "kolab", | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
help = _("Run as user USERNAME"), | help = _("Run as user USERNAME"), | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
metavar = "USERNAME" | metavar = "USERNAME" | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
) | ) | ||||
daemon_group.add_option( | daemon_group.add_option( | ||||
"-g", | "-g", | ||||
"--group", | "--group", | ||||
dest = "process_groupname", | dest = "process_groupname", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
action = "store", | action = "store", | ||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
default = "kolab", | default = "kolab", | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
help = _("Run as group GROUPNAME"), | help = _("Run as group GROUPNAME"), | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E221 multiple spaces before operator Lint: PEP8 E221: multiple spaces before operator | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
metavar = "GROUPNAME" | metavar = "GROUPNAME" | ||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
Lint: PEP8 E251 unexpected spaces around keyword / parameter equals Lint: PEP8 E251: unexpected spaces around keyword / parameter equals | |||||
) | ) | ||||
conf.finalize_conf() | conf.finalize_conf() | ||||
def run(self): | def run(self): | ||||
"""Run Forest, RUN!""" | """Run Forest, RUN!""" | ||||
exitcode = 0 | exitcode = 0 | ||||
Show All 29 Lines | def run(self): | ||||
conf.process_groupname | conf.process_groupname | ||||
) | ) | ||||
sys.exit(1) | sys.exit(1) | ||||
# Set real and effective group if not the same as current. | # Set real and effective group if not the same as current. | ||||
if not group_gid == rgid: | if not group_gid == rgid: | ||||
log.debug( | log.debug( | ||||
_("Switching real and effective group id to %d") % ( | _("Switching real and effective group id to %d") % ( | ||||
Lint: PEP8 E501 line too long (84 > 79 characters) Lint: PEP8 E501: line too long (84 > 79 characters) | |||||
group_gid | group_gid | ||||
), | ), | ||||
level=8 | level=8 | ||||
) | ) | ||||
os.setregid(group_gid, group_gid) | os.setregid(group_gid, group_gid) | ||||
if ruid == 0: | if ruid == 0: | ||||
Show All 11 Lines | def run(self): | ||||
except KeyError: | except KeyError: | ||||
print >> sys.stderr, _("User %s does not exist") % ( | print >> sys.stderr, _("User %s does not exist") % ( | ||||
conf.process_username | conf.process_username | ||||
) | ) | ||||
sys.exit(1) | sys.exit(1) | ||||
# Set real and effective user if not the same as current. | # Set real and effective user if not the same as current. | ||||
if not user_uid == ruid: | if not user_uid == ruid: | ||||
log.debug( | log.debug( | ||||
_("Switching real and effective user id to %d") % ( | _("Switching real and effective user id to %d") % ( | ||||
Lint: PEP8 E501 line too long (83 > 79 characters) Lint: PEP8 E501: line too long (83 > 79 characters) | |||||
user_uid | user_uid | ||||
), | ), | ||||
level=8 | level=8 | ||||
) | ) | ||||
os.setreuid(user_uid, user_uid) | os.setreuid(user_uid, user_uid) | ||||
except: | except: | ||||
Show All 40 Lines | def run(self): | ||||
except KeyboardInterrupt: | except KeyboardInterrupt: | ||||
exitcode = 1 | exitcode = 1 | ||||
log.info(_("Interrupted by user")) | log.info(_("Interrupted by user")) | ||||
except AttributeError, errmsg: | except AttributeError, errmsg: | ||||
exitcode = 1 | exitcode = 1 | ||||
traceback.print_exc() | traceback.print_exc() | ||||
print >> sys.stderr, _("Traceback occurred, please report a " + \ | print >> sys.stderr, _("Traceback occurred, please report a " + | ||||
"bug at https://issues.kolab.org") | "bug at https://issues.kolab.org") | ||||
except TypeError, errmsg: | except TypeError, errmsg: | ||||
exitcode = 1 | exitcode = 1 | ||||
traceback.print_exc() | traceback.print_exc() | ||||
log.error(_("Type Error: %s") % errmsg) | log.error(_("Type Error: %s") % errmsg) | ||||
except: | except: | ||||
exitcode = 2 | exitcode = 2 | ||||
traceback.print_exc() | traceback.print_exc() | ||||
print >> sys.stderr, _("Traceback occurred, please report a " + \ | print >> sys.stderr, _("Traceback occurred, please report a " + | ||||
"bug at https://issues.kolab.org") | "bug at https://issues.kolab.org") | ||||
sys.exit(exitcode) | sys.exit(exitcode) | ||||
def do_sync(self): | def do_sync(self): | ||||
domain_auth = {} | domain_auth = {} | ||||
pid = os.getpid() | pid = os.getpid() | ||||
Show All 27 Lines | def do_sync(self): | ||||
time.sleep(5) | time.sleep(5) | ||||
continue | continue | ||||
# domains now is a list of tuples in the format of | # domains now is a list of tuples in the format of | ||||
# ('primary',[secondaries]), we want the primary_domains | # ('primary',[secondaries]), we want the primary_domains | ||||
domain_base_dns = [] | domain_base_dns = [] | ||||
primary_domains = [] | primary_domains = [] | ||||
for primary_domain in list(set(domains.values())): | for primary_domain in list(set(domains.values())): | ||||
domain_base_dn = primary_auth.domain_naming_context(primary_domain) | domain_base_dn = primary_auth.domain_naming_context(primary_domain) | ||||
Lint: PEP8 E501 line too long (83 > 79 characters) Lint: PEP8 E501: line too long (83 > 79 characters) | |||||
log.debug(_("Domain Base DN for domain %r is %r") % (primary_domain, domain_base_dn), level=8) | log.debug(_("Domain Base DN for domain %r is %r") % (primary_domain, domain_base_dn), level=8) | ||||
Lint: PEP8 E501 line too long (110 > 79 characters) Lint: PEP8 E501: line too long (110 > 79 characters) | |||||
if not domain_base_dn == None: | if domain_base_dn is not None: | ||||
if not domain_base_dn in domain_base_dns: | if domain_base_dn not in domain_base_dns: | ||||
domain_base_dns.append(domain_base_dn) | domain_base_dns.append(domain_base_dn) | ||||
primary_domain = primary_auth.primary_domain_for_naming_context(domain_base_dn) | primary_domain = primary_auth.primary_domain_for_naming_context(domain_base_dn) | ||||
Lint: PEP8 E501 line too long (103 > 79 characters) Lint: PEP8 E501: line too long (103 > 79 characters) | |||||
primary_domains.append(primary_domain) | primary_domains.append(primary_domain) | ||||
log.debug(_("Naming contexts to synchronize: %r") % (primary_domains), level=8) | log.debug(_("Naming contexts to synchronize: %r") % (primary_domains), level=8) | ||||
Lint: PEP8 E501 line too long (91 > 79 characters) Lint: PEP8 E501: line too long (91 > 79 characters) | |||||
# Now we can check if any changes happened. | # Now we can check if any changes happened. | ||||
added_domains = [] | added_domains = [] | ||||
removed_domains = [] | removed_domains = [] | ||||
# Combine the domains from LDAP with the domain processes | # Combine the domains from LDAP with the domain processes | ||||
# accounted for locally. | # accounted for locally. | ||||
all_domains = list(set(primary_domains + domain_auth.keys())) | all_domains = list(set(primary_domains + domain_auth.keys())) | ||||
log.debug(_("All naming contexts: %r") % (all_domains), level=8) | log.debug(_("All naming contexts: %r") % (all_domains), level=8) | ||||
for domain in all_domains: | for domain in all_domains: | ||||
log.debug(_("Checking for domain %s") % (domain), level=8) | log.debug(_("Checking for domain %s") % (domain), level=8) | ||||
if domain in domain_auth.keys() and domain in primary_domains: | if domain in domain_auth.keys() and domain in primary_domains: | ||||
if not domain_auth[domain].is_alive(): | if not domain_auth[domain].is_alive(): | ||||
log.debug(_("Domain %s isn't alive anymore.") % (domain), level=8) | log.debug(_("Domain %s isn't alive anymore.") % (domain), level=8) | ||||
Lint: PEP8 E501 line too long (90 > 79 characters) Lint: PEP8 E501: line too long (90 > 79 characters) | |||||
domain_auth[domain].terminate() | domain_auth[domain].terminate() | ||||
added_domains.append(domain) | added_domains.append(domain) | ||||
else: | else: | ||||
log.debug(_("Domain %s already there and alive.") % (domain), level=8) | log.debug(_("Domain %s already there and alive.") % (domain), level=8) | ||||
Lint: PEP8 E501 line too long (94 > 79 characters) Lint: PEP8 E501: line too long (94 > 79 characters) | |||||
continue | continue | ||||
elif domain in domain_auth.keys(): | elif domain in domain_auth.keys(): | ||||
log.debug(_("Domain %s should not exist any longer.") % (domain), level=8) | log.debug(_("Domain %s should not exist any longer.") % (domain), level=8) | ||||
Lint: PEP8 E501 line too long (94 > 79 characters) Lint: PEP8 E501: line too long (94 > 79 characters) | |||||
removed_domains.append(domain) | removed_domains.append(domain) | ||||
else: | else: | ||||
log.debug(_("Domain %s does not have a process yet.") % (domain), level=8) | log.debug(_("Domain %s does not have a process yet.") % (domain), level=8) | ||||
Lint: PEP8 E501 line too long (94 > 79 characters) Lint: PEP8 E501: line too long (94 > 79 characters) | |||||
added_domains.append(domain) | added_domains.append(domain) | ||||
if len(removed_domains) == 0 and len(added_domains) == 0: | if len(removed_domains) == 0 and len(added_domains) == 0: | ||||
try: | try: | ||||
sleep_between_domain_operations_in_seconds = (float)(conf.get('kolab', 'domain_sync_interval')) | sleep_between_domain_operations_in_seconds = (float)(conf.get('kolab', 'domain_sync_interval')) | ||||
Lint: PEP8 E501 line too long (115 > 79 characters) Lint: PEP8 E501: line too long (115 > 79 characters) | |||||
time.sleep(sleep_between_domain_operations_in_seconds) | time.sleep(sleep_between_domain_operations_in_seconds) | ||||
except ValueError: | except ValueError: | ||||
time.sleep(600) | time.sleep(600) | ||||
log.debug( | log.debug( | ||||
_("added domains: %r, removed domains: %r") % ( | _("added domains: %r, removed domains: %r") % ( | ||||
added_domains, | added_domains, | ||||
removed_domains | removed_domains | ||||
Show All 34 Lines | class KolabDaemon(object): | ||||
def set_signal_handlers(self): | def set_signal_handlers(self): | ||||
import signal | import signal | ||||
signal.signal(signal.SIGHUP, self.reload_config) | signal.signal(signal.SIGHUP, self.reload_config) | ||||
signal.signal(signal.SIGTERM, self.remove_pid) | signal.signal(signal.SIGTERM, self.remove_pid) | ||||
def write_pid(self): | def write_pid(self): | ||||
pid = os.getpid() | pid = os.getpid() | ||||
fp = open(conf.pidfile,'w') | fp = open(conf.pidfile, 'w') | ||||
fp.write("%d\n" % (pid)) | fp.write("%d\n" % (pid)) | ||||
fp.close() | fp.close() |
multiple spaces before operator