~ubuntu-branches/debian/sid/freeipa/sid

« back to all changes in this revision

Viewing changes to ipaplatform/redhat/services.py

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2014-10-25 02:43:59 UTC
  • Revision ID: package-import@ubuntu.com-20141025024359-to6c607ln0lmzwik
Tags: upstream-4.0.4
ImportĀ upstreamĀ versionĀ 4.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Author: Alexander Bokovoy <abokovoy@redhat.com>
 
2
#         Tomas Babej <tbabej@redhat.com>
 
3
#
 
4
# Copyright (C) 2011-2014   Red Hat
 
5
# see file 'COPYING' for use and warranty information
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 3 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
 
 
21
"""
 
22
Contains Red Hat OS family-specific service class implementations.
 
23
"""
 
24
 
 
25
import os
 
26
import time
 
27
 
 
28
from ipaplatform.tasks import tasks
 
29
from ipaplatform.base import services as base_services
 
30
 
 
31
from ipapython import ipautil, dogtag
 
32
from ipapython.ipa_log_manager import root_logger
 
33
from ipalib import api
 
34
from ipaplatform.paths import paths
 
35
 
 
36
# Mappings from service names as FreeIPA code references to these services
 
37
# to their actual systemd service names
 
38
 
 
39
# For beginning just remap names to add .service
 
40
# As more services will migrate to systemd, unit names will deviate and
 
41
# mapping will be kept in this dictionary
 
42
redhat_system_units = dict((x, "%s.service" % x)
 
43
                           for x in base_services.wellknownservices)
 
44
 
 
45
redhat_system_units['rpcgssd'] = 'nfs-secure.service'
 
46
redhat_system_units['rpcidmapd'] = 'nfs-idmap.service'
 
47
 
 
48
# Rewrite dirsrv and pki-tomcatd services as they support instances via separate
 
49
# service generator. To make this working, one needs to have both foo@.servic
 
50
# and foo.target -- the latter is used when request should be coming for
 
51
# all instances (like stop). systemd, unfortunately, does not allow one
 
52
# to request action for all service instances at once if only foo@.service
 
53
# unit is available. To add more, if any of those services need to be
 
54
# started/stopped automagically, one needs to manually create symlinks in
 
55
# /etc/systemd/system/foo.target.wants/ (look into systemd.py's enable()
 
56
# code).
 
57
 
 
58
redhat_system_units['dirsrv'] = 'dirsrv@.service'
 
59
# Our directory server instance for PKI is dirsrv@PKI-IPA.service
 
60
redhat_system_units['pkids'] = 'dirsrv@PKI-IPA.service'
 
61
# Old style PKI instance
 
62
redhat_system_units['pki-cad'] = 'pki-cad@pki-ca.service'
 
63
redhat_system_units['pki_cad'] = redhat_system_units['pki-cad']
 
64
# Our PKI instance is pki-tomcatd@pki-tomcat.service
 
65
redhat_system_units['pki-tomcatd'] = 'pki-tomcatd@pki-tomcat.service'
 
66
redhat_system_units['pki_tomcatd'] = redhat_system_units['pki-tomcatd']
 
67
redhat_system_units['ipa-otpd'] = 'ipa-otpd.socket'
 
68
 
 
69
 
 
70
# Service classes that implement Red Hat OS family-specific behaviour
 
71
 
 
72
class RedHatService(base_services.SystemdService):
 
73
    system_units = redhat_system_units
 
74
 
 
75
    def __init__(self, service_name):
 
76
        systemd_name = service_name
 
77
        if service_name in self.system_units:
 
78
            systemd_name = self.system_units[service_name]
 
79
        else:
 
80
            if '.' not in service_name:
 
81
                # if service_name does not have a dot, it is not foo.service
 
82
                # and not a foo.target. Thus, not correct service name for
 
83
                # systemd, default to foo.service style then
 
84
                systemd_name = "%s.service" % (service_name)
 
85
        super(RedHatService, self).__init__(service_name, systemd_name)
 
86
 
 
87
 
 
88
class RedHatDirectoryService(RedHatService):
 
89
 
 
90
    def tune_nofile_platform(self, num=8192, fstore=None):
 
91
        """
 
92
        Increase the number of files descriptors available to directory server
 
93
        from the default 1024 to 8192. This will allow to support a greater
 
94
        number of clients out of the box.
 
95
 
 
96
        This is a part of the implementation that is systemd-specific.
 
97
 
 
98
        Returns False if the setting of the nofile limit needs to be skipped.
 
99
        """
 
100
 
 
101
        if os.path.exists(paths.SYSCONFIG_DIRSRV_SYSTEMD):
 
102
            # We need to enable LimitNOFILE=8192 in the dirsrv@.service
 
103
            # Since 389-ds-base-1.2.10-0.8.a7 the configuration of the
 
104
            # service parameters is performed via
 
105
            # /etc/sysconfig/dirsrv.systemd file which is imported by systemd
 
106
            # into dirsrv@.service unit
 
107
 
 
108
            replacevars = {'LimitNOFILE': str(num)}
 
109
            ipautil.inifile_replace_variables(paths.SYSCONFIG_DIRSRV_SYSTEMD,
 
110
                                              'service',
 
111
                                              replacevars=replacevars)
 
112
            tasks.restore_context(paths.SYSCONFIG_DIRSRV_SYSTEMD)
 
113
            ipautil.run(["/bin/systemctl", "--system", "daemon-reload"],
 
114
                        raiseonerr=False)
 
115
 
 
116
        return True
 
117
 
 
118
    def restart(self, instance_name="", capture_output=True, wait=True):
 
119
    # We need to explicitly enable instances to install proper symlinks as
 
120
    # dirsrv.target.wants/ dependencies. Standard systemd service class does it
 
121
    # on enable() method call. Unfortunately, ipa-server-install does not do
 
122
    # explicit dirsrv.enable() because the service startup is handled by ipactl.
 
123
    #
 
124
    # If we wouldn't do this, our instances will not be started as systemd would
 
125
    # not have any clue about instances (PKI-IPA and the domain we serve)
 
126
    # at all. Thus, hook into dirsrv.restart().
 
127
 
 
128
        if instance_name:
 
129
            elements = self.systemd_name.split("@")
 
130
 
 
131
            srv_etc = os.path.join(paths.ETC_SYSTEMD_SYSTEM_DIR,
 
132
                                   self.systemd_name)
 
133
            srv_tgt = os.path.join(paths.ETC_SYSTEMD_SYSTEM_DIR,
 
134
                                   self.SYSTEMD_SRV_TARGET % (elements[0]))
 
135
            srv_lnk = os.path.join(srv_tgt,
 
136
                                   self.service_instance(instance_name))
 
137
 
 
138
            if not os.path.exists(srv_etc):
 
139
                self.enable(instance_name)
 
140
            elif not os.path.samefile(srv_etc, srv_lnk):
 
141
                os.unlink(srv_lnk)
 
142
                os.symlink(srv_etc, srv_lnk)
 
143
 
 
144
        super(RedHatDirectoryService, self).restart(instance_name,
 
145
            capture_output=capture_output, wait=wait)
 
146
 
 
147
 
 
148
class RedHatIPAService(RedHatService):
 
149
    # Enforce restart of IPA services when we do enable it
 
150
    # This gets around the fact that after ipa-server-install systemd thinks
 
151
    # ipa.service is not yet started but all services were actually started
 
152
    # already.
 
153
    def enable(self, instance_name=""):
 
154
        super(RedHatIPAService, self).enable(instance_name)
 
155
        self.restart(instance_name)
 
156
 
 
157
 
 
158
class RedHatSSHService(RedHatService):
 
159
    def get_config_dir(self, instance_name=""):
 
160
        return '/etc/ssh'
 
161
 
 
162
 
 
163
class RedHatCAService(RedHatService):
 
164
    def wait_until_running(self):
 
165
        # We must not wait for the httpd proxy if httpd is not set up yet.
 
166
        # Unfortunately, knownservices.httpd.is_installed() can return
 
167
        # false positives, so check for existence of our configuration file.
 
168
        # TODO: Use a cleaner solution
 
169
        use_proxy = True
 
170
        if not (os.path.exists('/etc/httpd/conf.d/ipa.conf') and
 
171
                os.path.exists(paths.HTTPD_IPA_PKI_PROXY_CONF)):
 
172
            root_logger.debug(
 
173
                'The httpd proxy is not installed, wait on local port')
 
174
            use_proxy = False
 
175
        root_logger.debug('Waiting until the CA is running')
 
176
        timeout = float(api.env.startup_timeout)
 
177
        op_timeout = time.time() + timeout
 
178
        while time.time() < op_timeout:
 
179
            try:
 
180
                status = dogtag.ca_status(use_proxy=use_proxy)
 
181
            except Exception:
 
182
                status = 'check interrupted'
 
183
            root_logger.debug('The CA status is: %s' % status)
 
184
            if status == 'running':
 
185
                break
 
186
            root_logger.debug('Waiting for CA to start...')
 
187
            time.sleep(1)
 
188
        else:
 
189
            raise RuntimeError('CA did not start in %ss' % timeout)
 
190
 
 
191
    def start(self, instance_name="", capture_output=True, wait=True):
 
192
        super(RedHatCAService, self).start(
 
193
            instance_name, capture_output=capture_output, wait=wait)
 
194
        if wait:
 
195
            self.wait_until_running()
 
196
 
 
197
    def restart(self, instance_name="", capture_output=True, wait=True):
 
198
        super(RedHatCAService, self).restart(
 
199
            instance_name, capture_output=capture_output, wait=wait)
 
200
        if wait:
 
201
            self.wait_until_running()
 
202
 
 
203
 
 
204
# Function that constructs proper Red Hat OS family-specific server classes for
 
205
# services of specified name
 
206
 
 
207
def redhat_service_class_factory(name):
 
208
    if name == 'dirsrv':
 
209
        return RedHatDirectoryService(name)
 
210
    if name == 'ipa':
 
211
        return RedHatIPAService(name)
 
212
    if name == 'sshd':
 
213
        return RedHatSSHService(name)
 
214
    if name in ('pki-cad', 'pki_cad', 'pki-tomcatd', 'pki_tomcatd'):
 
215
        return RedHatCAService(name)
 
216
    return RedHatService(name)
 
217
 
 
218
 
 
219
# Magicdict containing RedHatService instances.
 
220
 
 
221
class RedHatServices(base_services.KnownServices):
 
222
    def service_class_factory(self, name):
 
223
        return redhat_service_class_factory(name)
 
224
 
 
225
    def __init__(self):
 
226
        services = dict()
 
227
        for s in base_services.wellknownservices:
 
228
            services[s] = self.service_class_factory(s)
 
229
        # Call base class constructor. This will lock services to read-only
 
230
        super(RedHatServices, self).__init__(services)
 
231
 
 
232
 
 
233
# Objects below are expected to be exported by platform module
 
234
 
 
235
from ipaplatform.base.services import timedate_services
 
236
service = redhat_service_class_factory
 
237
knownservices = RedHatServices()