blob: 0aa5493429df54e17403d2ffe439dc62934d9b35 [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#
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.
24
Brad Bishop9dc56712016-07-07 15:56:02 -040025
26inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000027inherit systemd
28
Brad Bishop9dc56712016-07-07 15:56:02 -040029_INSTALL_SD_UNITS=""
Brad Bishop51528fe2016-07-14 19:34:06 -040030SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
Brad Bishop9dc56712016-07-07 15:56:02 -040031
Brad Bishope36358c2016-06-08 22:03:59 -040032
Brad Bishop687146f2016-07-11 13:05:26 -040033def systemd_is_service(unit):
34 return unit.endswith('.service')
35
36
37def systemd_is_template(unit):
38 return '@.' in unit
39
40
41def systemd_parse_unit(d, path):
42 import ConfigParser
43 parser = ConfigParser.SafeConfigParser()
44 parser.optionxform = str
45 parser.read('%s' % path)
46 return parser
47
48
Brad Bishope36358c2016-06-08 22:03:59 -040049python() {
Brad Bishop687146f2016-07-11 13:05:26 -040050 def check_sd_unit(d, unit):
Brad Bishop9dc56712016-07-07 15:56:02 -040051 searchpaths = d.getVar('FILESPATH', True)
52 path = bb.utils.which(searchpaths, '%s' % unit)
53 if not os.path.isfile(path):
54 bb.fatal('Did not find unit file "%s"' % unit)
Brad Bishop687146f2016-07-11 13:05:26 -040055
56 parser = systemd_parse_unit(d, path)
57 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
58 if systemd_is_service(unit) and \
59 not systemd_is_template(unit) and \
60 unit not in inhibit and \
61 not parser.has_option('Service', 'Restart'):
62 bb.warn('Systemd unit \'%s\' does not '
63 'have a restart policy defined.' % unit)
64
65
66 def add_sd_unit(d, unit, pkg):
Brad Bishop9dc56712016-07-07 15:56:02 -040067 set_append(d, 'SRC_URI', 'file://%s' % unit)
68 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
69 % (d.getVar('systemd_system_unitdir', True), unit))
Brad Bishop687146f2016-07-11 13:05:26 -040070 set_append(d, '_INSTALL_SD_UNITS', unit)
71
Brad Bishop51528fe2016-07-14 19:34:06 -040072 for x in [
73 'base_bindir',
74 'bindir',
75 'sbindir',
76 'SYSTEMD_DEFAULT_TARGET' ]:
77 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
78 '%s:%s' % (x, d.getVar(x, True)))
79
Brad Bishop9dc56712016-07-07 15:56:02 -040080
Brad Bishop62d676a2016-07-11 00:42:58 -040081 pn = d.getVar('PN', True)
Brad Bishop9dc56712016-07-07 15:56:02 -040082 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
83 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishope36358c2016-06-08 22:03:59 -040084
Brad Bishop9dc56712016-07-07 15:56:02 -040085 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
86 for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
Brad Bishop687146f2016-07-11 13:05:26 -040087 check_sd_unit(d, unit)
Brad Bishop9dc56712016-07-07 15:56:02 -040088 add_sd_unit(d, unit, pkg)
Brad Bishope36358c2016-06-08 22:03:59 -040089}
Brad Bishop93fb5352015-09-09 03:59:20 +000090
Brad Bishop9dc56712016-07-07 15:56:02 -040091
Brad Bishop51528fe2016-07-14 19:34:06 -040092python systemd_do_postinst() {
93 for unit in listvar_to_list(d, '_INSTALL_SD_UNITS'):
94 subs = dict([ x.split(':') for x in
95 listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit)])
96 if not subs:
97 continue
98
99 path = d.getVar('D', True)
100 path += d.getVar('systemd_system_unitdir', True)
101 path += '/%s' % unit
102 with open(path, 'r') as fd:
103 content = fd.read()
104 with open(path, 'w+') as fd:
105 try:
106 fd.write(content.format(**subs))
107 except KeyError as e:
108 bb.fatal('No substitution found for %s in '
109 'unit file \'%s\'' % (e, unit))
110}
111
112
Brad Bishop93fb5352015-09-09 03:59:20 +0000113do_install_append() {
Brad Bishope36358c2016-06-08 22:03:59 -0400114 # install systemd service/socket/template files
Brad Bishop9dc56712016-07-07 15:56:02 -0400115 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishope36358c2016-06-08 22:03:59 -0400116 install -d ${D}${systemd_system_unitdir}
Brad Bishop9dc56712016-07-07 15:56:02 -0400117 for s in ${_INSTALL_SD_UNITS}; do
118 install -m 0644 ${WORKDIR}/$s \
119 ${D}${systemd_system_unitdir}/$s
Brad Bishope36358c2016-06-08 22:03:59 -0400120 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
121 -e 's,@BINDIR@,${bindir},g' \
122 -e 's,@SBINDIR@,${sbindir},g' \
123 ${D}${systemd_system_unitdir}/$s
124 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000125}
Brad Bishop51528fe2016-07-14 19:34:06 -0400126
127
128do_install[postfuncs] += "systemd_do_postinst"