summaryrefslogtreecommitdiffstats
path: root/kolabd/__init__.py
blob: 47175e42461e794e7d050c2bb9e8b02817e1890f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# 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
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

"""
    The Kolab daemon.

    TODO: Write a pid file, check the pid file has a valid pid, and
    consider providing an option to specify the pid file path.
"""

from optparse import OptionParser
from ConfigParser import SafeConfigParser

import os
import shutil
import time
import traceback

import pykolab

from pykolab.auth import Auth
from pykolab.constants import *
from pykolab.translate import _

log = pykolab.getLogger('kolabd')
conf = pykolab.getConf()

class KolabDaemon(object):
    def __init__(self):
        """
            self.args == Arguments passed on the CLI
            self.cli_options == Parser results (again, CLI)
            self.parser == The actual Parser (from OptionParser)
            self.plugins == Our Kolab Plugins
        """

        daemon_group = conf.add_cli_parser_option_group(_("Daemon Options"))

        daemon_group.add_option(  "--fork",
                                dest    = "fork_mode",
                                action  = "store_true",
                                default = False,
                                help    = _("Fork to the background."))

        daemon_group.add_option( "-p", "--pid-file",
                                dest    = "pidfile",
                                action  = "store",
                                default = "/var/run/kolabd/kolabd.pid",
                                help    = _("Path to the PID file to use."))

        conf.finalize_conf()

        self.thread_count = 0

    def run(self):
        """Run Forest, RUN!"""

        exitcode = 0

        # TODO: Add a nosync option
        try:
            pid = 1
            if conf.fork_mode:
                self.thread_count += 1
                pid = os.fork()

            if pid == 0:
                log.remove_stdout_handler()
                self.write_pid()
                self.set_signal_handlers()
                self.do_sync()
            elif not conf.fork_mode:
                self.do_sync()

        except SystemExit, e:
            exitcode = e
        except KeyboardInterrupt:
            exitcode = 1
            log.info(_("Interrupted by user"))
        except AttributeError, e:
            exitcode = 1
            traceback.print_exc()
            print >> sys.stderr, _("Traceback occurred, please report a bug at http://bugzilla.kolabsys.com")
        except TypeError, e:
            exitcode = 1
            traceback.print_exc()
            log.error(_("Type Error: %s") % e)
        except:
            exitcode = 2
            traceback.print_exc()
            print >> sys.stderr, _("Traceback occurred, please report a bug at http://bugzilla.kolabsys.com")
        sys.exit(exitcode)

    def do_sync(self):
        domain_auth = {}

        pid = os.getpid()

        while 1:
            primary_auth = Auth()

            log.debug(_("Listing domains..."), level=5)

            start = time.time()
            domains = primary_auth.list_domains()
            if len(domains) == len(domain_auth.keys()):
                time.sleep(600)
            end = time.time()

            log.debug(_("Found %d domains in %d seconds") %(len(domains),(end-start)), level=8)

            for primary_domain,secondary_domains in domains:
                log.debug(_("Running for domain %s") %(primary_domain), level=5)

                if not pid == 0 and not domain_auth.has_key(primary_domain):
                    log.debug(_("Domain %s did not have a key yet") %(primary_domain), level=5)
                    domain_auth[primary_domain] = Auth()
                    pid = os.fork()
                    if pid == 0:
                        domain_auth[primary_domain].connect(primary_domain)
                        start_time = time.time()
                        domain_auth[primary_domain].synchronize(primary_domain, secondary_domains)
                        end_time = time.time()

                        log.info(_("Synchronizing users for %s took %d seconds")
                                %(primary_domain, (end_time-start_time))
                            )
                        domain_auth[primary_domain].synchronize(primary_domain, secondary_domains)

    def reload_config(self, *args, **kw):
        pass

    def remove_pid(self, *args, **kw):
        if os.access(conf.pidfile, os.R_OK):
            os.remove(conf.pidfile)
        raise SystemExit

    def set_signal_handlers(self):
        import signal
        signal.signal(signal.SIGHUP, self.reload_config)
        signal.signal(signal.SIGTERM, self.remove_pid)

    def write_pid(self):
        pid = os.getpid()
        fp = open(conf.pidfile,'w')
        fp.write("%d\n" %(pid))
        fp.close()