blob: 85089f807e053004d8ea7222d1b95a71e1b64c7b [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 Bishop975b2cd2016-08-17 12:08:16 -040017# SYSTEMD_SUBSTITUTIONS_${path-relative-to-system_unitdir}
18# Variables in this list will be substituted in the specified
Brad Bishopb7e2a882016-07-14 19:34:06 -040019# file during install (if bitbake finds python {format} strings
Brad Bishop975b2cd2016-08-17 12:08:16 -040020# in the file itself). List entries take the form:
Brad Bishopb7e2a882016-07-14 19:34:06 -040021# VAR:VALUE
22# where {VAR} is the format string bitbake should look for in the
Brad Bishop975b2cd2016-08-17 12:08:16 -040023# file and VALUE is the value to substitute.
Brad Bishop039c66b2016-07-14 19:50:19 -040024#
Brad Bishopbcd1b652016-08-15 22:35:58 -040025# SYSTEMD_USER_${PN}.service = "foo"
26# SYSTEMD_USER_${unit}.service = "foo"
27# The user for the unit/package.
Brad Bishopb7e2a882016-07-14 19:34:06 -040028
Brad Bishopbfef6ff2016-07-07 15:56:02 -040029
30inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000031inherit systemd
Brad Bishop039c66b2016-07-14 19:50:19 -040032inherit useradd
Brad Bishop93fb5352015-09-09 03:59:20 +000033
Brad Bishopbfef6ff2016-07-07 15:56:02 -040034_INSTALL_SD_UNITS=""
Brad Bishopb7e2a882016-07-14 19:34:06 -040035SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
Brad Bishopbfef6ff2016-07-07 15:56:02 -040036
Brad Bishop039c66b2016-07-14 19:50:19 -040037# Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure.
38# If there are users to be added, we'll add them in our post-parse.
39# If not...there don't seem to be any ill effects...
40USERADD_PACKAGES ?= " "
41USERADD_PARAM_${PN} ?= ";"
42
Brad Bishop1bb8be52016-06-08 22:03:59 -040043
Brad Bishopb1ebc602016-08-16 08:18:07 -040044def SystemdUnit(unit):
45 class Unit(object):
46 def __init__(self, unit):
47 self.unit = unit
Brad Bishop7aeda7b2016-07-11 13:05:26 -040048
Brad Bishopb1ebc602016-08-16 08:18:07 -040049 def __getattr__(self, item):
50 if item is 'name':
51 return self.unit
52 if item is 'is_activated':
53 return self.unit.startswith('dbus-')
54 if item is 'is_template':
55 return '@.' in self.unit
56 if item is 'is_instance':
57 return '@' in self.unit and not self.is_template
58 if item in ['is_service', 'is_target']:
59 return self.unit.split('.')[-1] == item
60 if item is 'base':
61 cls = self.unit.split('.')[-1]
62 base = self.unit.replace('dbus-', '')
63 base = base.replace('.%s' % cls, '')
64 if self.is_instance:
65 base = base.rstrip('@%s' % self.instance)
66 if self.is_template:
67 base = base.rstrip('@')
68 return base
69 if item is 'instance' and self.is_instance:
70 inst = self.unit.rsplit('@')[-1]
71 return inst.rsplit('.')[0]
72 if item is 'template' and self.is_instance:
73 cls = self.unit.split('.')[-1]
74 return '%s@.%s' % (self.base, cls)
75 if item is 'template' and self.is_template:
76 return '.'.join(self.base.split('@')[:-1])
Brad Bishop7aeda7b2016-07-11 13:05:26 -040077
Brad Bishopb1ebc602016-08-16 08:18:07 -040078 raise AttributeError(item)
79 return Unit(unit)
Brad Bishop7aeda7b2016-07-11 13:05:26 -040080
81
82def systemd_parse_unit(d, path):
83 import ConfigParser
84 parser = ConfigParser.SafeConfigParser()
85 parser.optionxform = str
86 parser.read('%s' % path)
87 return parser
88
89
Brad Bishop1bb8be52016-06-08 22:03:59 -040090python() {
Brad Bishop7aeda7b2016-07-11 13:05:26 -040091 def check_sd_unit(d, unit):
Brad Bishopbfef6ff2016-07-07 15:56:02 -040092 searchpaths = d.getVar('FILESPATH', True)
Brad Bishopb1ebc602016-08-16 08:18:07 -040093 path = bb.utils.which(searchpaths, '%s' % unit.name)
Brad Bishopbfef6ff2016-07-07 15:56:02 -040094 if not os.path.isfile(path):
Brad Bishopb1ebc602016-08-16 08:18:07 -040095 bb.fatal('Did not find unit file "%s"' % unit.name)
Brad Bishop7aeda7b2016-07-11 13:05:26 -040096
97 parser = systemd_parse_unit(d, path)
98 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
Brad Bishopb1ebc602016-08-16 08:18:07 -040099 if unit.is_service and \
100 not unit.is_template and \
101 unit.name not in inhibit and \
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400102 not parser.has_option('Service', 'Restart'):
103 bb.warn('Systemd unit \'%s\' does not '
Brad Bishopb1ebc602016-08-16 08:18:07 -0400104 'have a restart policy defined.' % unit.name)
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400105
106
Brad Bishopaab8d362016-08-17 15:42:31 -0400107 def add_default_subs(d, file):
108 set_append(d, '_MAKE_SUBS', '%s' % file)
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400109
Brad Bishopb7e2a882016-07-14 19:34:06 -0400110 for x in [
111 'base_bindir',
112 'bindir',
113 'sbindir',
114 'SYSTEMD_DEFAULT_TARGET' ]:
Brad Bishopaab8d362016-08-17 15:42:31 -0400115 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file,
Brad Bishopb7e2a882016-07-14 19:34:06 -0400116 '%s:%s' % (x, d.getVar(x, True)))
117
Brad Bishop039c66b2016-07-14 19:50:19 -0400118
Brad Bishopaab8d362016-08-17 15:42:31 -0400119 def add_sd_unit(d, unit, pkg):
Brad Bishopb1ebc602016-08-16 08:18:07 -0400120 name = unit.name
Brad Bishopaab8d362016-08-17 15:42:31 -0400121 unit_dir = d.getVar('systemd_system_unitdir', True)
122 set_append(d, 'SRC_URI', 'file://%s' % name)
123 set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name))
124 set_append(d, '_INSTALL_SD_UNITS', name)
125 add_default_subs(d, name)
126
127
128 def add_sd_user(d, file, pkg):
Brad Bishop039c66b2016-07-14 19:50:19 -0400129 opts = [
130 '--system',
131 '--home',
132 '/',
133 '--no-create-home',
134 '--shell /sbin/nologin',
135 '--user-group']
136
Brad Bishopaab8d362016-08-17 15:42:31 -0400137 var = 'SYSTEMD_USER_%s' % file
Brad Bishopbcd1b652016-08-15 22:35:58 -0400138 user = listvar_to_list(d, var)
139 if len(user) is 0:
140 var = 'SYSTEMD_USER_%s' % pkg
141 user = listvar_to_list(d, var)
142 if len(user) is not 0:
143 if len(user) is not 1:
144 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
145
146 user = user[0]
Brad Bishopaab8d362016-08-17 15:42:31 -0400147 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file,
Brad Bishopbcd1b652016-08-15 22:35:58 -0400148 'USER:%s' % user)
149 if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
150 set_append(
151 d,
152 'USERADD_PARAM_%s' % pkg,
153 '%s' % (' '.join(opts + [user])),
154 ';')
Brad Bishop039c66b2016-07-14 19:50:19 -0400155 if pkg not in d.getVar('USERADD_PACKAGES', True):
156 set_append(d, 'USERADD_PACKAGES', pkg)
157
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400158
Brad Bishop8b875602016-07-11 00:42:58 -0400159 pn = d.getVar('PN', True)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400160 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
161 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400162
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400163 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
Brad Bishopb1ebc602016-08-16 08:18:07 -0400164 svc = listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg)
165 svc = [SystemdUnit(x) for x in svc]
166 tmpl = [x.template for x in svc if x.is_instance]
167 tmpl = list(set(tmpl))
168 tmpl = [SystemdUnit(x) for x in tmpl]
169 svc = [x for x in svc if not x.is_instance]
170
171 for unit in tmpl + svc:
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400172 check_sd_unit(d, unit)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400173 add_sd_unit(d, unit, pkg)
Brad Bishopaab8d362016-08-17 15:42:31 -0400174 add_sd_user(d, unit.name, pkg)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400175}
Brad Bishop93fb5352015-09-09 03:59:20 +0000176
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400177
Brad Bishopb7e2a882016-07-14 19:34:06 -0400178python systemd_do_postinst() {
Brad Bishopaab8d362016-08-17 15:42:31 -0400179 def make_subs(d):
180 for f in listvar_to_list(d, '_MAKE_SUBS'):
181 subs = dict([ x.split(':') for x in
182 listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)])
183 if not subs:
184 continue
Brad Bishopb7e2a882016-07-14 19:34:06 -0400185
Brad Bishopaab8d362016-08-17 15:42:31 -0400186 path = d.getVar('D', True)
187 path += d.getVar('systemd_system_unitdir', True)
188 path += '/%s' % f
189 with open(path, 'r') as fd:
190 content = fd.read()
191 with open(path, 'w+') as fd:
192 try:
193 fd.write(content.format(**subs))
194 except KeyError as e:
195 bb.fatal('No substitution found for %s in '
196 'file \'%s\'' % (e, f))
197
198
199 make_subs(d)
Brad Bishopb7e2a882016-07-14 19:34:06 -0400200}
201
202
Brad Bishop93fb5352015-09-09 03:59:20 +0000203do_install_append() {
Brad Bishop1bb8be52016-06-08 22:03:59 -0400204 # install systemd service/socket/template files
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400205 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishop1bb8be52016-06-08 22:03:59 -0400206 install -d ${D}${systemd_system_unitdir}
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400207 for s in ${_INSTALL_SD_UNITS}; do
208 install -m 0644 ${WORKDIR}/$s \
209 ${D}${systemd_system_unitdir}/$s
Brad Bishop1bb8be52016-06-08 22:03:59 -0400210 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
211 -e 's,@BINDIR@,${bindir},g' \
212 -e 's,@SBINDIR@,${sbindir},g' \
213 ${D}${systemd_system_unitdir}/$s
214 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000215}
Brad Bishopb7e2a882016-07-14 19:34:06 -0400216
217
218do_install[postfuncs] += "systemd_do_postinst"