blob: 04b813506e5ddd1f62918349d56091773ff5fc93 [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#
25# SYSTEMD_USER_${PN}_${PN}.service = "foo"
26# The user for the unit.
Brad Bishopb7e2a882016-07-14 19:34:06 -040027
Brad Bishopbfef6ff2016-07-07 15:56:02 -040028
29inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000030inherit systemd
Brad Bishop039c66b2016-07-14 19:50:19 -040031inherit useradd
Brad Bishop93fb5352015-09-09 03:59:20 +000032
Brad Bishopbfef6ff2016-07-07 15:56:02 -040033_INSTALL_SD_UNITS=""
Brad Bishopb7e2a882016-07-14 19:34:06 -040034SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
Brad Bishopbfef6ff2016-07-07 15:56:02 -040035
Brad Bishop039c66b2016-07-14 19:50:19 -040036# Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure.
37# If there are users to be added, we'll add them in our post-parse.
38# If not...there don't seem to be any ill effects...
39USERADD_PACKAGES ?= " "
40USERADD_PARAM_${PN} ?= ";"
41
Brad Bishop1bb8be52016-06-08 22:03:59 -040042
Brad Bishop7aeda7b2016-07-11 13:05:26 -040043def systemd_is_service(unit):
44 return unit.endswith('.service')
45
46
47def systemd_is_template(unit):
48 return '@.' in unit
49
50
51def systemd_parse_unit(d, path):
52 import ConfigParser
53 parser = ConfigParser.SafeConfigParser()
54 parser.optionxform = str
55 parser.read('%s' % path)
56 return parser
57
58
Brad Bishop1bb8be52016-06-08 22:03:59 -040059python() {
Brad Bishop7aeda7b2016-07-11 13:05:26 -040060 def check_sd_unit(d, unit):
Brad Bishopbfef6ff2016-07-07 15:56:02 -040061 searchpaths = d.getVar('FILESPATH', True)
62 path = bb.utils.which(searchpaths, '%s' % unit)
63 if not os.path.isfile(path):
64 bb.fatal('Did not find unit file "%s"' % unit)
Brad Bishop7aeda7b2016-07-11 13:05:26 -040065
66 parser = systemd_parse_unit(d, path)
67 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
68 if systemd_is_service(unit) and \
69 not systemd_is_template(unit) and \
70 unit not in inhibit and \
71 not parser.has_option('Service', 'Restart'):
72 bb.warn('Systemd unit \'%s\' does not '
73 'have a restart policy defined.' % unit)
74
75
76 def add_sd_unit(d, unit, pkg):
Brad Bishopbfef6ff2016-07-07 15:56:02 -040077 set_append(d, 'SRC_URI', 'file://%s' % unit)
78 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
79 % (d.getVar('systemd_system_unitdir', True), unit))
Brad Bishop7aeda7b2016-07-11 13:05:26 -040080 set_append(d, '_INSTALL_SD_UNITS', unit)
81
Brad Bishopb7e2a882016-07-14 19:34:06 -040082 for x in [
83 'base_bindir',
84 'bindir',
85 'sbindir',
86 'SYSTEMD_DEFAULT_TARGET' ]:
87 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
88 '%s:%s' % (x, d.getVar(x, True)))
89
Brad Bishop039c66b2016-07-14 19:50:19 -040090 user = d.getVar(
91 'SYSTEMD_USER_%s_%s' % (pkg, unit), True)
92 if user:
93 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
94 'USER:%s' % d.getVar('SYSTEMD_USER_%s_%s' % (pkg, unit), True))
95
96
97 def add_sd_user(d, unit, pkg):
98 opts = [
99 '--system',
100 '--home',
101 '/',
102 '--no-create-home',
103 '--shell /sbin/nologin',
104 '--user-group']
105
106 user = d.getVar(
107 'SYSTEMD_USER_%s_%s' % (pkg, unit), True)
108 if user:
109 set_append(
110 d,
111 'USERADD_PARAM_%s' % pkg,
112 '%s' % (' '.join(opts + [user])),
113 ';')
114 if pkg not in d.getVar('USERADD_PACKAGES', True):
115 set_append(d, 'USERADD_PACKAGES', pkg)
116
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400117
Brad Bishop8b875602016-07-11 00:42:58 -0400118 pn = d.getVar('PN', True)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400119 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
120 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400121
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400122 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
123 for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
Brad Bishop7aeda7b2016-07-11 13:05:26 -0400124 check_sd_unit(d, unit)
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400125 add_sd_unit(d, unit, pkg)
Brad Bishop039c66b2016-07-14 19:50:19 -0400126 add_sd_user(d, unit, pkg)
Brad Bishop1bb8be52016-06-08 22:03:59 -0400127}
Brad Bishop93fb5352015-09-09 03:59:20 +0000128
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400129
Brad Bishopb7e2a882016-07-14 19:34:06 -0400130python systemd_do_postinst() {
131 for unit in listvar_to_list(d, '_INSTALL_SD_UNITS'):
132 subs = dict([ x.split(':') for x in
133 listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit)])
134 if not subs:
135 continue
136
137 path = d.getVar('D', True)
138 path += d.getVar('systemd_system_unitdir', True)
139 path += '/%s' % unit
140 with open(path, 'r') as fd:
141 content = fd.read()
142 with open(path, 'w+') as fd:
143 try:
144 fd.write(content.format(**subs))
145 except KeyError as e:
146 bb.fatal('No substitution found for %s in '
147 'unit file \'%s\'' % (e, unit))
148}
149
150
Brad Bishop93fb5352015-09-09 03:59:20 +0000151do_install_append() {
Brad Bishop1bb8be52016-06-08 22:03:59 -0400152 # install systemd service/socket/template files
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400153 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishop1bb8be52016-06-08 22:03:59 -0400154 install -d ${D}${systemd_system_unitdir}
Brad Bishopbfef6ff2016-07-07 15:56:02 -0400155 for s in ${_INSTALL_SD_UNITS}; do
156 install -m 0644 ${WORKDIR}/$s \
157 ${D}${systemd_system_unitdir}/$s
Brad Bishop1bb8be52016-06-08 22:03:59 -0400158 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
159 -e 's,@BINDIR@,${bindir},g' \
160 -e 's,@SBINDIR@,${sbindir},g' \
161 ${D}${systemd_system_unitdir}/$s
162 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000163}
Brad Bishopb7e2a882016-07-14 19:34:06 -0400164
165
166do_install[postfuncs] += "systemd_do_postinst"