1
# Author: Alexander Bokovoy <abokovoy@redhat.com>
2
# Tomas Babej <tbabej@redhat.com>
4
# Copyright (C) 2011-2014 Red Hat
5
# see file 'COPYING' for use and warranty information
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.
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.
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/>.
22
Contains Red Hat OS family-specific service class implementations.
28
from ipaplatform.tasks import tasks
29
from ipaplatform.base import services as base_services
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
36
# Mappings from service names as FreeIPA code references to these services
37
# to their actual systemd service names
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)
45
redhat_system_units['rpcgssd'] = 'nfs-secure.service'
46
redhat_system_units['rpcidmapd'] = 'nfs-idmap.service'
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()
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'
70
# Service classes that implement Red Hat OS family-specific behaviour
72
class RedHatService(base_services.SystemdService):
73
system_units = redhat_system_units
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]
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)
88
class RedHatDirectoryService(RedHatService):
90
def tune_nofile_platform(self, num=8192, fstore=None):
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.
96
This is a part of the implementation that is systemd-specific.
98
Returns False if the setting of the nofile limit needs to be skipped.
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
108
replacevars = {'LimitNOFILE': str(num)}
109
ipautil.inifile_replace_variables(paths.SYSCONFIG_DIRSRV_SYSTEMD,
111
replacevars=replacevars)
112
tasks.restore_context(paths.SYSCONFIG_DIRSRV_SYSTEMD)
113
ipautil.run(["/bin/systemctl", "--system", "daemon-reload"],
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.
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().
129
elements = self.systemd_name.split("@")
131
srv_etc = os.path.join(paths.ETC_SYSTEMD_SYSTEM_DIR,
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))
138
if not os.path.exists(srv_etc):
139
self.enable(instance_name)
140
elif not os.path.samefile(srv_etc, srv_lnk):
142
os.symlink(srv_etc, srv_lnk)
144
super(RedHatDirectoryService, self).restart(instance_name,
145
capture_output=capture_output, wait=wait)
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
153
def enable(self, instance_name=""):
154
super(RedHatIPAService, self).enable(instance_name)
155
self.restart(instance_name)
158
class RedHatSSHService(RedHatService):
159
def get_config_dir(self, instance_name=""):
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
170
if not (os.path.exists('/etc/httpd/conf.d/ipa.conf') and
171
os.path.exists(paths.HTTPD_IPA_PKI_PROXY_CONF)):
173
'The httpd proxy is not installed, wait on local port')
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:
180
status = dogtag.ca_status(use_proxy=use_proxy)
182
status = 'check interrupted'
183
root_logger.debug('The CA status is: %s' % status)
184
if status == 'running':
186
root_logger.debug('Waiting for CA to start...')
189
raise RuntimeError('CA did not start in %ss' % timeout)
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)
195
self.wait_until_running()
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)
201
self.wait_until_running()
204
# Function that constructs proper Red Hat OS family-specific server classes for
205
# services of specified name
207
def redhat_service_class_factory(name):
209
return RedHatDirectoryService(name)
211
return RedHatIPAService(name)
213
return RedHatSSHService(name)
214
if name in ('pki-cad', 'pki_cad', 'pki-tomcatd', 'pki_tomcatd'):
215
return RedHatCAService(name)
216
return RedHatService(name)
219
# Magicdict containing RedHatService instances.
221
class RedHatServices(base_services.KnownServices):
222
def service_class_factory(self, name):
223
return redhat_service_class_factory(name)
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)
233
# Objects below are expected to be exported by platform module
235
from ipaplatform.base.services import timedate_services
236
service = redhat_service_class_factory
237
knownservices = RedHatServices()