blob: 2cb060ccedf5ef7a97b0e6e6eabf52021577968d [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#
17# SYSTEMD_SUBSTITUTIONS_${unit}
18# Variables in this list will be substituted in the specified unit
19# file during install (if bitbake finds python {format} strings
20# in the unit file itself). List entries take the form:
21# VAR:VALUE
22# where {VAR} is the format string bitbake should look for in the
23# unit 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 Bishop7aeda7b2016-07-11 13:05:26 -040044def systemd_is_service(unit):
45 return unit.endswith('.service')
46
47
48def systemd_is_template(unit):
49 return '@.' in unit
50
51
52def systemd_parse_unit(d, path):
53 import ConfigParser
54 parser = ConfigParser.SafeConfigParser()
55 parser.optionxform = str
56 parser.read('%s' % path)
57 return parser
58
59
Brad Bishop1bb8be52016-06-08 22:03:59 -040060python() {
Brad Bishop7aeda7b2016-07-11 13:05:26 -040061 def check_sd_unit(d, unit):
Brad Bishopbfef6ff2016-07-07 15:56:02 -040062 searchpaths = d.getVar('FILESPATH', True)
63 path = bb.utils.which(searchpaths, '%s' % unit)
64 if not os.path.isfile(path):
65 bb.fatal('Did not find unit file "%s"' % unit)
Brad Bishop7aeda7b2016-07-11 13:05:26 -040066
67 parser = systemd_parse_unit(d, path)
68 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
69 if systemd_is_service(unit) and \
70 not systemd_is_template(unit) and \
71 unit not in inhibit and \
72 not parser.has_option('Service', 'Restart'):
73 bb.warn('Systemd unit \'%s\' does not '
74 'have a restart policy defined.' % unit)
75
76
77 def add_sd_unit(d, unit, pkg):
Brad Bishopbfef6ff2016-07-07 15:56:02 -040078 set_append(d, 'SRC_URI', 'file://%s' % unit)
79 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
80 % (d.getVar('systemd_system_unitdir', True), unit))
Brad Bishop7aeda7b2016-07-11 13:05:26 -040081 set_append(d, '_INSTALL_SD_UNITS', unit)
82
Brad Bishopb7e2a882016-07-14 19:34:06 -040083 for x in [
84 'base_bindir',
85 'bindir',
86 'sbindir',
87 'SYSTEMD_DEFAULT_TARGET' ]:
88 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
89 '%s:%s' % (x, d.getVar(x, True)))
90
Brad Bishop039c66b2016-07-14 19:50:19 -040091
92 def add_sd_user(d, unit, pkg):
93 opts = [
94 '--system',
95 '--home',
96 '/',
97 '--no-create-home',
98 '--shell /sbin/nologin',
99 '--user-group']
100
Brad Bishopbcd1b652016-08-15 22:35:58 -0400101 var = 'SYSTEMD_USER_%s' % unit
102 user = listvar_to_list(d, var)
103 if len(user) is 0:
104 var = 'SYSTEMD_USER_%s' % pkg
105 user = listvar_to_list(d, var)
106 if len(user) is not 0:
107 if len(user) is not 1:
108 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
109
110 user = user[0]
111 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
112 'USER:%s' % user)
113 if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
114 set_append(
115 d,
116 'USERADD_PARAM_%s' % pkg,
117 '%s' % (' '.join(opts + [user])),
118 ';')
Brad Bishop039c66b2016-07-14 19:50:19 -0400119 if pkg not in d.getVar('USERADD_PACKAGES', True):
120 set_append(d, 'USERADD_PACKAGES', pkg)
121
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400122
Brad Bishop8b875602016-07-11 00:42:58 -0400123 pn = d.getVar('PN', True)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400124 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
125 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400126
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400127 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
128 for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400129 check_sd_unit(d, unit)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400130 add_sd_unit(d, unit, pkg)
Brad Bishop039c66b2016-07-14 19:50:19 -0400131 add_sd_user(d, unit, pkg)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400132}
Brad Bishop93fb5352015-09-09 03:59:20 +0000133
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400134
Brad Bishopb7e2a882016-07-14 19:34:06 -0400135python systemd_do_postinst() {
136 for unit in listvar_to_list(d, '_INSTALL_SD_UNITS'):
137 subs = dict([ x.split(':') for x in
138 listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit)])
139 if not subs:
140 continue
141
142 path = d.getVar('D', True)
143 path += d.getVar('systemd_system_unitdir', True)
144 path += '/%s' % unit
145 with open(path, 'r') as fd:
146 content = fd.read()
147 with open(path, 'w+') as fd:
148 try:
149 fd.write(content.format(**subs))
150 except KeyError as e:
151 bb.fatal('No substitution found for %s in '
152 'unit file \'%s\'' % (e, unit))
153}
154
155
Brad Bishop93fb5352015-09-09 03:59:20 +0000156do_install_append() {
Brad Bishop1bb8be52016-06-08 22:03:59 -0400157 # install systemd service/socket/template files
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400158 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishop1bb8be52016-06-08 22:03:59 -0400159 install -d ${D}${systemd_system_unitdir}
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400160 for s in ${_INSTALL_SD_UNITS}; do
161 install -m 0644 ${WORKDIR}/$s \
162 ${D}${systemd_system_unitdir}/$s
Brad Bishop1bb8be52016-06-08 22:03:59 -0400163 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
164 -e 's,@BINDIR@,${bindir},g' \
165 -e 's,@SBINDIR@,${sbindir},g' \
166 ${D}${systemd_system_unitdir}/$s
167 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000168}
Brad Bishopb7e2a882016-07-14 19:34:06 -0400169
170
171do_install[postfuncs] += "systemd_do_postinst"