blob: 7446d4b487180e6284e6bc5d0c7908d5986630c4 [file] [log] [blame]
Brad Bishop1bb8be52016-06-08 22:03:59 -04001# Common code for systemd based services.
2#
3# Prior to inheriting this class, recipes can define services like this:
4#
5# SYSTEMD_SERVICE_${PN} = "foo.service bar.socket baz@.service"
6#
7# and these files will be added to the main package if they exist.
8#
9# Alternatively this class can just be inherited and
Brad Bishop8b875602016-07-11 00:42:58 -040010# ${PN}.service will be added to the main package.
Brad Bishop7aeda7b2016-07-11 13:05:26 -040011#
12# Other variables:
13# INHIBIT_SYSTEMD_RESTART_POLICY_${unit}
14# Inhibit the warning that is displayed if a service unit without a
15# restart policy is detected.
Brad Bishopb7e2a882016-07-14 19:34:06 -040016#
Brad Bishop8dcce252016-08-17 15:58:26 -040017# SYSTEMD_SUBSTITUTIONS = "var:val:file"
18# A specification for making python style {format} string
19# substitutions where:
20# var: the format string to search for
21# val: the value to replace with
22# file: the file in which to make the substitution
Brad Bishop039c66b2016-07-14 19:50:19 -040023#
Brad Bishopbcd1b652016-08-15 22:35:58 -040024# SYSTEMD_USER_${PN}.service = "foo"
25# SYSTEMD_USER_${unit}.service = "foo"
26# The user for the unit/package.
Brad Bishop0d703a02016-08-08 09:29:20 -040027#
28# SYSTEMD_ENVIRONMENT_FILE_${PN} = "foo"
29# One or more environment files to be installed.
Brad Bishopd4bdab22016-08-17 14:46:41 -040030#
31# SYSTEMD_LINK_${PN} = "tgt:name"
32# A specification for installing arbitrary links in
33# the ${systemd_system_unitdir} namespace, where:
34# tgt: the link target
35# name: the link name, relative to ${systemd_system_unitdir}
Brad Bishoped916e72016-08-18 23:59:44 -040036#
37# SYSTEMD_OVERRIDE_${PN} = "src:dest"
38# A specification for installing unit overrides where:
39# src: the override file template
40# dest: the override install location, relative to ${systemd_system_unitdir}
41#
42# Typically SYSTEMD_SUBSTITUTIONS is used to deploy a range
43# of overrides from a single template file. To simply install
44# a single override use "foo.conf:my-service.d/foo.conf"
Brad Bishopb7e2a882016-07-14 19:34:06 -040045
Brad Bishopbfef6ff2016-07-07 15:56:02 -040046
47inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000048inherit systemd
Brad Bishop039c66b2016-07-14 19:50:19 -040049inherit useradd
Brad Bishop93fb5352015-09-09 03:59:20 +000050
Brad Bishopbfef6ff2016-07-07 15:56:02 -040051_INSTALL_SD_UNITS=""
Brad Bishopb7e2a882016-07-14 19:34:06 -040052SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
Brad Bishop0d703a02016-08-08 09:29:20 -040053envfiledir ?= "${sysconfdir}/default"
Brad Bishopbfef6ff2016-07-07 15:56:02 -040054
Brad Bishop039c66b2016-07-14 19:50:19 -040055# Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure.
56# If there are users to be added, we'll add them in our post-parse.
57# If not...there don't seem to be any ill effects...
58USERADD_PACKAGES ?= " "
59USERADD_PARAM_${PN} ?= ";"
60
Brad Bishop1bb8be52016-06-08 22:03:59 -040061
Brad Bishopb1ebc602016-08-16 08:18:07 -040062def SystemdUnit(unit):
63 class Unit(object):
64 def __init__(self, unit):
65 self.unit = unit
Brad Bishop7aeda7b2016-07-11 13:05:26 -040066
Brad Bishopb1ebc602016-08-16 08:18:07 -040067 def __getattr__(self, item):
68 if item is 'name':
69 return self.unit
70 if item is 'is_activated':
71 return self.unit.startswith('dbus-')
72 if item is 'is_template':
73 return '@.' in self.unit
74 if item is 'is_instance':
75 return '@' in self.unit and not self.is_template
76 if item in ['is_service', 'is_target']:
77 return self.unit.split('.')[-1] == item
78 if item is 'base':
79 cls = self.unit.split('.')[-1]
80 base = self.unit.replace('dbus-', '')
81 base = base.replace('.%s' % cls, '')
82 if self.is_instance:
83 base = base.rstrip('@%s' % self.instance)
84 if self.is_template:
85 base = base.rstrip('@')
86 return base
87 if item is 'instance' and self.is_instance:
88 inst = self.unit.rsplit('@')[-1]
89 return inst.rsplit('.')[0]
90 if item is 'template' and self.is_instance:
91 cls = self.unit.split('.')[-1]
92 return '%s@.%s' % (self.base, cls)
93 if item is 'template' and self.is_template:
94 return '.'.join(self.base.split('@')[:-1])
Brad Bishop7aeda7b2016-07-11 13:05:26 -040095
Brad Bishopb1ebc602016-08-16 08:18:07 -040096 raise AttributeError(item)
97 return Unit(unit)
Brad Bishop7aeda7b2016-07-11 13:05:26 -040098
99
100def systemd_parse_unit(d, path):
101 import ConfigParser
102 parser = ConfigParser.SafeConfigParser()
103 parser.optionxform = str
104 parser.read('%s' % path)
105 return parser
106
107
Brad Bishop1bb8be52016-06-08 22:03:59 -0400108python() {
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400109 def check_sd_unit(d, unit):
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400110 searchpaths = d.getVar('FILESPATH', True)
Brad Bishopb1ebc602016-08-16 08:18:07 -0400111 path = bb.utils.which(searchpaths, '%s' % unit.name)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400112 if not os.path.isfile(path):
Brad Bishopb1ebc602016-08-16 08:18:07 -0400113 bb.fatal('Did not find unit file "%s"' % unit.name)
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400114
115 parser = systemd_parse_unit(d, path)
116 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
Brad Bishopb1ebc602016-08-16 08:18:07 -0400117 if unit.is_service and \
118 not unit.is_template and \
119 unit.name not in inhibit and \
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400120 not parser.has_option('Service', 'Restart'):
121 bb.warn('Systemd unit \'%s\' does not '
Brad Bishopb1ebc602016-08-16 08:18:07 -0400122 'have a restart policy defined.' % unit.name)
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400123
124
Brad Bishopaab8d362016-08-17 15:42:31 -0400125 def add_default_subs(d, file):
Brad Bishopb7e2a882016-07-14 19:34:06 -0400126 for x in [
127 'base_bindir',
128 'bindir',
129 'sbindir',
Brad Bishop0d703a02016-08-08 09:29:20 -0400130 'envfiledir',
Xo Wange72ff982017-01-18 17:55:50 -0800131 'sysconfdir',
Brad Bishopb7e2a882016-07-14 19:34:06 -0400132 'SYSTEMD_DEFAULT_TARGET' ]:
Brad Bishop8dcce252016-08-17 15:58:26 -0400133 set_append(d, 'SYSTEMD_SUBSTITUTIONS',
134 '%s:%s:%s' % (x, d.getVar(x, True), file))
Brad Bishopb7e2a882016-07-14 19:34:06 -0400135
Brad Bishop039c66b2016-07-14 19:50:19 -0400136
Brad Bishopaab8d362016-08-17 15:42:31 -0400137 def add_sd_unit(d, unit, pkg):
Brad Bishopb1ebc602016-08-16 08:18:07 -0400138 name = unit.name
Brad Bishopaab8d362016-08-17 15:42:31 -0400139 unit_dir = d.getVar('systemd_system_unitdir', True)
140 set_append(d, 'SRC_URI', 'file://%s' % name)
141 set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name))
142 set_append(d, '_INSTALL_SD_UNITS', name)
143 add_default_subs(d, name)
144
145
146 def add_sd_user(d, file, pkg):
Brad Bishop039c66b2016-07-14 19:50:19 -0400147 opts = [
148 '--system',
149 '--home',
150 '/',
151 '--no-create-home',
152 '--shell /sbin/nologin',
153 '--user-group']
154
Brad Bishopaab8d362016-08-17 15:42:31 -0400155 var = 'SYSTEMD_USER_%s' % file
Brad Bishopbcd1b652016-08-15 22:35:58 -0400156 user = listvar_to_list(d, var)
157 if len(user) is 0:
158 var = 'SYSTEMD_USER_%s' % pkg
159 user = listvar_to_list(d, var)
160 if len(user) is not 0:
161 if len(user) is not 1:
162 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
163
164 user = user[0]
Brad Bishop8dcce252016-08-17 15:58:26 -0400165 set_append(d, 'SYSTEMD_SUBSTITUTIONS',
166 'USER:%s:%s' % (user, file))
Brad Bishopbcd1b652016-08-15 22:35:58 -0400167 if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
168 set_append(
169 d,
170 'USERADD_PARAM_%s' % pkg,
171 '%s' % (' '.join(opts + [user])),
172 ';')
Brad Bishop039c66b2016-07-14 19:50:19 -0400173 if pkg not in d.getVar('USERADD_PACKAGES', True):
174 set_append(d, 'USERADD_PACKAGES', pkg)
175
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400176
Brad Bishop0d703a02016-08-08 09:29:20 -0400177 def add_env_file(d, name, pkg):
178 set_append(d, 'SRC_URI', 'file://%s' % name)
179 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
180 % (d.getVar('envfiledir', True), name))
181 set_append(d, '_INSTALL_ENV_FILES', name)
182
183
Brad Bishopd4bdab22016-08-17 14:46:41 -0400184 def install_link(d, spec, pkg):
185 tgt, dest = spec.split(':')
186
187 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
188 % (d.getVar('systemd_system_unitdir', True), dest))
189 set_append(d, '_INSTALL_LINKS', spec)
190
191
Brad Bishoped916e72016-08-18 23:59:44 -0400192 def add_override(d, spec, pkg):
193 tmpl, dest = spec.split(':')
194 set_append(d, '_INSTALL_OVERRIDES', '%s' % spec)
195 unit_dir = d.getVar('systemd_system_unitdir', True)
196 set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, dest))
197 add_default_subs(d, '%s' % dest)
198 add_sd_user(d, '%s' % dest, pkg)
199
200
Brad Bishop8b875602016-07-11 00:42:58 -0400201 pn = d.getVar('PN', True)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400202 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
203 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400204
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400205 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
Brad Bishopb1ebc602016-08-16 08:18:07 -0400206 svc = listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg)
207 svc = [SystemdUnit(x) for x in svc]
208 tmpl = [x.template for x in svc if x.is_instance]
209 tmpl = list(set(tmpl))
210 tmpl = [SystemdUnit(x) for x in tmpl]
211 svc = [x for x in svc if not x.is_instance]
212
213 for unit in tmpl + svc:
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400214 check_sd_unit(d, unit)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400215 add_sd_unit(d, unit, pkg)
Brad Bishopaab8d362016-08-17 15:42:31 -0400216 add_sd_user(d, unit.name, pkg)
Brad Bishop0d703a02016-08-08 09:29:20 -0400217 for name in listvar_to_list(d, 'SYSTEMD_ENVIRONMENT_FILE_%s' % pkg):
218 add_env_file(d, name, pkg)
Brad Bishopd4bdab22016-08-17 14:46:41 -0400219 for spec in listvar_to_list(d, 'SYSTEMD_LINK_%s' % pkg):
220 install_link(d, spec, pkg)
Brad Bishoped916e72016-08-18 23:59:44 -0400221 for spec in listvar_to_list(d, 'SYSTEMD_OVERRIDE_%s' % pkg):
222 add_override(d, spec, pkg)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400223}
Brad Bishop93fb5352015-09-09 03:59:20 +0000224
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400225
Brad Bishopb7e2a882016-07-14 19:34:06 -0400226python systemd_do_postinst() {
Brad Bishopaab8d362016-08-17 15:42:31 -0400227 def make_subs(d):
Brad Bishop8dcce252016-08-17 15:58:26 -0400228 all_subs = {}
229 for spec in listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS'):
230 spec, file = spec.rsplit(':', 1)
231 all_subs.setdefault(file, []).append(spec)
232
Saqib Khan8e75c832017-03-07 07:15:29 -0600233 for f, v in all_subs.items():
Brad Bishop8dcce252016-08-17 15:58:26 -0400234 subs = dict([ x.split(':') for x in v])
Brad Bishopaab8d362016-08-17 15:42:31 -0400235 if not subs:
236 continue
Brad Bishopb7e2a882016-07-14 19:34:06 -0400237
Brad Bishopaab8d362016-08-17 15:42:31 -0400238 path = d.getVar('D', True)
239 path += d.getVar('systemd_system_unitdir', True)
240 path += '/%s' % f
241 with open(path, 'r') as fd:
242 content = fd.read()
243 with open(path, 'w+') as fd:
244 try:
245 fd.write(content.format(**subs))
246 except KeyError as e:
247 bb.fatal('No substitution found for %s in '
248 'file \'%s\'' % (e, f))
249
250
Brad Bishop0d703a02016-08-08 09:29:20 -0400251 def install_envs(d):
252 install_dir = d.getVar('D', True)
253 install_dir += d.getVar('envfiledir', True)
254 searchpaths = d.getVar('FILESPATH', True)
255
256 for f in listvar_to_list(d, '_INSTALL_ENV_FILES'):
257 src = bb.utils.which(searchpaths, f)
258 if not os.path.isfile(src):
259 bb.fatal('Did not find SYSTEMD_ENVIRONMENT_FILE:'
260 '\'%s\'' % src)
261
262 dest = os.path.join(install_dir, f)
263 parent = os.path.dirname(dest)
264 if not os.path.exists(parent):
265 os.makedirs(parent)
266
267 with open(src, 'r') as fd:
268 content = fd.read()
269 with open(dest, 'w+') as fd:
270 fd.write(content)
271
272
Brad Bishopd4bdab22016-08-17 14:46:41 -0400273 def install_links(d):
274 install_dir = d.getVar('D', True)
275 install_dir += d.getVar('systemd_system_unitdir', True)
276
277 for spec in listvar_to_list(d, '_INSTALL_LINKS'):
278 tgt, dest = spec.split(':')
279 dest = os.path.join(install_dir, dest)
280 parent = os.path.dirname(dest)
281 if not os.path.exists(parent):
282 os.makedirs(parent)
283 os.symlink(tgt, dest)
284
285
Brad Bishoped916e72016-08-18 23:59:44 -0400286 def install_overrides(d):
287 install_dir = d.getVar('D', True)
288 install_dir += d.getVar('systemd_system_unitdir', True)
289 searchpaths = d.getVar('FILESPATH', True)
290
291 for spec in listvar_to_list(d, '_INSTALL_OVERRIDES'):
292 tmpl, dest = spec.split(':')
293 source = bb.utils.which(searchpaths, tmpl)
294 if not os.path.isfile(source):
295 bb.fatal('Did not find SYSTEMD_OVERRIDE '
296 'template: \'%s\'' % source)
297
298 dest = os.path.join(install_dir, dest)
299 parent = os.path.dirname(dest)
300 if not os.path.exists(parent):
301 os.makedirs(parent)
302
303 with open(source, 'r') as fd:
304 content = fd.read()
305 with open('%s' % dest, 'w+') as fd:
306 fd.write(content)
307
308
Brad Bishopd4bdab22016-08-17 14:46:41 -0400309 install_links(d)
Brad Bishop0d703a02016-08-08 09:29:20 -0400310 install_envs(d)
Brad Bishoped916e72016-08-18 23:59:44 -0400311 install_overrides(d)
Brad Bishopaab8d362016-08-17 15:42:31 -0400312 make_subs(d)
Brad Bishopb7e2a882016-07-14 19:34:06 -0400313}
314
315
Brad Bishop93fb5352015-09-09 03:59:20 +0000316do_install_append() {
Brad Bishop1bb8be52016-06-08 22:03:59 -0400317 # install systemd service/socket/template files
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400318 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishop1bb8be52016-06-08 22:03:59 -0400319 install -d ${D}${systemd_system_unitdir}
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400320 for s in ${_INSTALL_SD_UNITS}; do
321 install -m 0644 ${WORKDIR}/$s \
322 ${D}${systemd_system_unitdir}/$s
Brad Bishop1bb8be52016-06-08 22:03:59 -0400323 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
324 -e 's,@BINDIR@,${bindir},g' \
325 -e 's,@SBINDIR@,${sbindir},g' \
326 ${D}${systemd_system_unitdir}/$s
327 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000328}
Brad Bishopb7e2a882016-07-14 19:34:06 -0400329
330
331do_install[postfuncs] += "systemd_do_postinst"