blob: 1881ec0ef1180f3bd4b8441b1530639abdb2d55c [file] [log] [blame]
Brad Bishope36358c2016-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 Bishop62d676a2016-07-11 00:42:58 -040010# ${PN}.service will be added to the main package.
Brad Bishop687146f2016-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 Bishop51528fe2016-07-14 19:34:06 -040016#
Brad Bishop58ab4a02016-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 Bishopf815b2e2016-07-14 19:50:19 -040023#
Brad Bishop58700d12016-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 Bishopafb2e1b2016-08-08 09:29:20 -040027#
28# SYSTEMD_ENVIRONMENT_FILE_${PN} = "foo"
29# One or more environment files to be installed.
Brad Bishop0161aff2016-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 Bishop51528fe2016-07-14 19:34:06 -040036
Brad Bishop9dc56712016-07-07 15:56:02 -040037
38inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000039inherit systemd
Brad Bishopf815b2e2016-07-14 19:50:19 -040040inherit useradd
Brad Bishop93fb5352015-09-09 03:59:20 +000041
Brad Bishop9dc56712016-07-07 15:56:02 -040042_INSTALL_SD_UNITS=""
Brad Bishop51528fe2016-07-14 19:34:06 -040043SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
Brad Bishopafb2e1b2016-08-08 09:29:20 -040044envfiledir ?= "${sysconfdir}/default"
Brad Bishop9dc56712016-07-07 15:56:02 -040045
Brad Bishopf815b2e2016-07-14 19:50:19 -040046# Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure.
47# If there are users to be added, we'll add them in our post-parse.
48# If not...there don't seem to be any ill effects...
49USERADD_PACKAGES ?= " "
50USERADD_PARAM_${PN} ?= ";"
51
Brad Bishope36358c2016-06-08 22:03:59 -040052
Brad Bishop18645292016-08-16 08:18:07 -040053def SystemdUnit(unit):
54 class Unit(object):
55 def __init__(self, unit):
56 self.unit = unit
Brad Bishop687146f2016-07-11 13:05:26 -040057
Brad Bishop18645292016-08-16 08:18:07 -040058 def __getattr__(self, item):
59 if item is 'name':
60 return self.unit
61 if item is 'is_activated':
62 return self.unit.startswith('dbus-')
63 if item is 'is_template':
64 return '@.' in self.unit
65 if item is 'is_instance':
66 return '@' in self.unit and not self.is_template
67 if item in ['is_service', 'is_target']:
68 return self.unit.split('.')[-1] == item
69 if item is 'base':
70 cls = self.unit.split('.')[-1]
71 base = self.unit.replace('dbus-', '')
72 base = base.replace('.%s' % cls, '')
73 if self.is_instance:
74 base = base.rstrip('@%s' % self.instance)
75 if self.is_template:
76 base = base.rstrip('@')
77 return base
78 if item is 'instance' and self.is_instance:
79 inst = self.unit.rsplit('@')[-1]
80 return inst.rsplit('.')[0]
81 if item is 'template' and self.is_instance:
82 cls = self.unit.split('.')[-1]
83 return '%s@.%s' % (self.base, cls)
84 if item is 'template' and self.is_template:
85 return '.'.join(self.base.split('@')[:-1])
Brad Bishop687146f2016-07-11 13:05:26 -040086
Brad Bishop18645292016-08-16 08:18:07 -040087 raise AttributeError(item)
88 return Unit(unit)
Brad Bishop687146f2016-07-11 13:05:26 -040089
90
91def systemd_parse_unit(d, path):
92 import ConfigParser
93 parser = ConfigParser.SafeConfigParser()
94 parser.optionxform = str
95 parser.read('%s' % path)
96 return parser
97
98
Brad Bishope36358c2016-06-08 22:03:59 -040099python() {
Brad Bishop687146f2016-07-11 13:05:26 -0400100 def check_sd_unit(d, unit):
Brad Bishop9dc56712016-07-07 15:56:02 -0400101 searchpaths = d.getVar('FILESPATH', True)
Brad Bishop18645292016-08-16 08:18:07 -0400102 path = bb.utils.which(searchpaths, '%s' % unit.name)
Brad Bishop9dc56712016-07-07 15:56:02 -0400103 if not os.path.isfile(path):
Brad Bishop18645292016-08-16 08:18:07 -0400104 bb.fatal('Did not find unit file "%s"' % unit.name)
Brad Bishop687146f2016-07-11 13:05:26 -0400105
106 parser = systemd_parse_unit(d, path)
107 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
Brad Bishop18645292016-08-16 08:18:07 -0400108 if unit.is_service and \
109 not unit.is_template and \
110 unit.name not in inhibit and \
Brad Bishop687146f2016-07-11 13:05:26 -0400111 not parser.has_option('Service', 'Restart'):
112 bb.warn('Systemd unit \'%s\' does not '
Brad Bishop18645292016-08-16 08:18:07 -0400113 'have a restart policy defined.' % unit.name)
Brad Bishop687146f2016-07-11 13:05:26 -0400114
115
Brad Bishop522234c2016-08-17 15:42:31 -0400116 def add_default_subs(d, file):
Brad Bishop51528fe2016-07-14 19:34:06 -0400117 for x in [
118 'base_bindir',
119 'bindir',
120 'sbindir',
Brad Bishopafb2e1b2016-08-08 09:29:20 -0400121 'envfiledir',
Brad Bishop51528fe2016-07-14 19:34:06 -0400122 'SYSTEMD_DEFAULT_TARGET' ]:
Brad Bishop58ab4a02016-08-17 15:58:26 -0400123 set_append(d, 'SYSTEMD_SUBSTITUTIONS',
124 '%s:%s:%s' % (x, d.getVar(x, True), file))
Brad Bishop51528fe2016-07-14 19:34:06 -0400125
Brad Bishopf815b2e2016-07-14 19:50:19 -0400126
Brad Bishop522234c2016-08-17 15:42:31 -0400127 def add_sd_unit(d, unit, pkg):
Brad Bishop18645292016-08-16 08:18:07 -0400128 name = unit.name
Brad Bishop522234c2016-08-17 15:42:31 -0400129 unit_dir = d.getVar('systemd_system_unitdir', True)
130 set_append(d, 'SRC_URI', 'file://%s' % name)
131 set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name))
132 set_append(d, '_INSTALL_SD_UNITS', name)
133 add_default_subs(d, name)
134
135
136 def add_sd_user(d, file, pkg):
Brad Bishopf815b2e2016-07-14 19:50:19 -0400137 opts = [
138 '--system',
139 '--home',
140 '/',
141 '--no-create-home',
142 '--shell /sbin/nologin',
143 '--user-group']
144
Brad Bishop522234c2016-08-17 15:42:31 -0400145 var = 'SYSTEMD_USER_%s' % file
Brad Bishop58700d12016-08-15 22:35:58 -0400146 user = listvar_to_list(d, var)
147 if len(user) is 0:
148 var = 'SYSTEMD_USER_%s' % pkg
149 user = listvar_to_list(d, var)
150 if len(user) is not 0:
151 if len(user) is not 1:
152 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
153
154 user = user[0]
Brad Bishop58ab4a02016-08-17 15:58:26 -0400155 set_append(d, 'SYSTEMD_SUBSTITUTIONS',
156 'USER:%s:%s' % (user, file))
Brad Bishop58700d12016-08-15 22:35:58 -0400157 if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
158 set_append(
159 d,
160 'USERADD_PARAM_%s' % pkg,
161 '%s' % (' '.join(opts + [user])),
162 ';')
Brad Bishopf815b2e2016-07-14 19:50:19 -0400163 if pkg not in d.getVar('USERADD_PACKAGES', True):
164 set_append(d, 'USERADD_PACKAGES', pkg)
165
Brad Bishop9dc56712016-07-07 15:56:02 -0400166
Brad Bishopafb2e1b2016-08-08 09:29:20 -0400167 def add_env_file(d, name, pkg):
168 set_append(d, 'SRC_URI', 'file://%s' % name)
169 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
170 % (d.getVar('envfiledir', True), name))
171 set_append(d, '_INSTALL_ENV_FILES', name)
172
173
Brad Bishop0161aff2016-08-17 14:46:41 -0400174 def install_link(d, spec, pkg):
175 tgt, dest = spec.split(':')
176
177 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
178 % (d.getVar('systemd_system_unitdir', True), dest))
179 set_append(d, '_INSTALL_LINKS', spec)
180
181
Brad Bishop62d676a2016-07-11 00:42:58 -0400182 pn = d.getVar('PN', True)
Brad Bishop9dc56712016-07-07 15:56:02 -0400183 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
184 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishope36358c2016-06-08 22:03:59 -0400185
Brad Bishop9dc56712016-07-07 15:56:02 -0400186 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
Brad Bishop18645292016-08-16 08:18:07 -0400187 svc = listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg)
188 svc = [SystemdUnit(x) for x in svc]
189 tmpl = [x.template for x in svc if x.is_instance]
190 tmpl = list(set(tmpl))
191 tmpl = [SystemdUnit(x) for x in tmpl]
192 svc = [x for x in svc if not x.is_instance]
193
194 for unit in tmpl + svc:
Brad Bishop687146f2016-07-11 13:05:26 -0400195 check_sd_unit(d, unit)
Brad Bishop9dc56712016-07-07 15:56:02 -0400196 add_sd_unit(d, unit, pkg)
Brad Bishop522234c2016-08-17 15:42:31 -0400197 add_sd_user(d, unit.name, pkg)
Brad Bishopafb2e1b2016-08-08 09:29:20 -0400198 for name in listvar_to_list(d, 'SYSTEMD_ENVIRONMENT_FILE_%s' % pkg):
199 add_env_file(d, name, pkg)
Brad Bishop0161aff2016-08-17 14:46:41 -0400200 for spec in listvar_to_list(d, 'SYSTEMD_LINK_%s' % pkg):
201 install_link(d, spec, pkg)
Brad Bishope36358c2016-06-08 22:03:59 -0400202}
Brad Bishop93fb5352015-09-09 03:59:20 +0000203
Brad Bishop9dc56712016-07-07 15:56:02 -0400204
Brad Bishop51528fe2016-07-14 19:34:06 -0400205python systemd_do_postinst() {
Brad Bishop522234c2016-08-17 15:42:31 -0400206 def make_subs(d):
Brad Bishop58ab4a02016-08-17 15:58:26 -0400207 all_subs = {}
208 for spec in listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS'):
209 spec, file = spec.rsplit(':', 1)
210 all_subs.setdefault(file, []).append(spec)
211
212 for f, v in all_subs.iteritems():
213 subs = dict([ x.split(':') for x in v])
Brad Bishop522234c2016-08-17 15:42:31 -0400214 if not subs:
215 continue
Brad Bishop51528fe2016-07-14 19:34:06 -0400216
Brad Bishop522234c2016-08-17 15:42:31 -0400217 path = d.getVar('D', True)
218 path += d.getVar('systemd_system_unitdir', True)
219 path += '/%s' % f
220 with open(path, 'r') as fd:
221 content = fd.read()
222 with open(path, 'w+') as fd:
223 try:
224 fd.write(content.format(**subs))
225 except KeyError as e:
226 bb.fatal('No substitution found for %s in '
227 'file \'%s\'' % (e, f))
228
229
Brad Bishopafb2e1b2016-08-08 09:29:20 -0400230 def install_envs(d):
231 install_dir = d.getVar('D', True)
232 install_dir += d.getVar('envfiledir', True)
233 searchpaths = d.getVar('FILESPATH', True)
234
235 for f in listvar_to_list(d, '_INSTALL_ENV_FILES'):
236 src = bb.utils.which(searchpaths, f)
237 if not os.path.isfile(src):
238 bb.fatal('Did not find SYSTEMD_ENVIRONMENT_FILE:'
239 '\'%s\'' % src)
240
241 dest = os.path.join(install_dir, f)
242 parent = os.path.dirname(dest)
243 if not os.path.exists(parent):
244 os.makedirs(parent)
245
246 with open(src, 'r') as fd:
247 content = fd.read()
248 with open(dest, 'w+') as fd:
249 fd.write(content)
250
251
Brad Bishop0161aff2016-08-17 14:46:41 -0400252 def install_links(d):
253 install_dir = d.getVar('D', True)
254 install_dir += d.getVar('systemd_system_unitdir', True)
255
256 for spec in listvar_to_list(d, '_INSTALL_LINKS'):
257 tgt, dest = spec.split(':')
258 dest = os.path.join(install_dir, dest)
259 parent = os.path.dirname(dest)
260 if not os.path.exists(parent):
261 os.makedirs(parent)
262 os.symlink(tgt, dest)
263
264
265 install_links(d)
Brad Bishopafb2e1b2016-08-08 09:29:20 -0400266 install_envs(d)
Brad Bishop522234c2016-08-17 15:42:31 -0400267 make_subs(d)
Brad Bishop51528fe2016-07-14 19:34:06 -0400268}
269
270
Brad Bishop93fb5352015-09-09 03:59:20 +0000271do_install_append() {
Brad Bishope36358c2016-06-08 22:03:59 -0400272 # install systemd service/socket/template files
Brad Bishop9dc56712016-07-07 15:56:02 -0400273 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishope36358c2016-06-08 22:03:59 -0400274 install -d ${D}${systemd_system_unitdir}
Brad Bishop9dc56712016-07-07 15:56:02 -0400275 for s in ${_INSTALL_SD_UNITS}; do
276 install -m 0644 ${WORKDIR}/$s \
277 ${D}${systemd_system_unitdir}/$s
Brad Bishope36358c2016-06-08 22:03:59 -0400278 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
279 -e 's,@BINDIR@,${bindir},g' \
280 -e 's,@SBINDIR@,${sbindir},g' \
281 ${D}${systemd_system_unitdir}/$s
282 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000283}
Brad Bishop51528fe2016-07-14 19:34:06 -0400284
285
286do_install[postfuncs] += "systemd_do_postinst"