blob: dba39d35600ea40b02e65a8d16444de7cdc99409 [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 Bishop9dc56712016-07-07 15:56:02 -040016
17inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000018inherit systemd
19
Brad Bishop9dc56712016-07-07 15:56:02 -040020_INSTALL_SD_UNITS=""
21
Brad Bishope36358c2016-06-08 22:03:59 -040022
Brad Bishop687146f2016-07-11 13:05:26 -040023def systemd_is_service(unit):
24 return unit.endswith('.service')
25
26
27def systemd_is_template(unit):
28 return '@.' in unit
29
30
31def systemd_parse_unit(d, path):
32 import ConfigParser
33 parser = ConfigParser.SafeConfigParser()
34 parser.optionxform = str
35 parser.read('%s' % path)
36 return parser
37
38
Brad Bishope36358c2016-06-08 22:03:59 -040039python() {
Brad Bishop687146f2016-07-11 13:05:26 -040040 def check_sd_unit(d, unit):
Brad Bishop9dc56712016-07-07 15:56:02 -040041 searchpaths = d.getVar('FILESPATH', True)
42 path = bb.utils.which(searchpaths, '%s' % unit)
43 if not os.path.isfile(path):
44 bb.fatal('Did not find unit file "%s"' % unit)
Brad Bishop687146f2016-07-11 13:05:26 -040045
46 parser = systemd_parse_unit(d, path)
47 inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING')
48 if systemd_is_service(unit) and \
49 not systemd_is_template(unit) and \
50 unit not in inhibit and \
51 not parser.has_option('Service', 'Restart'):
52 bb.warn('Systemd unit \'%s\' does not '
53 'have a restart policy defined.' % unit)
54
55
56 def add_sd_unit(d, unit, pkg):
Brad Bishop9dc56712016-07-07 15:56:02 -040057 set_append(d, 'SRC_URI', 'file://%s' % unit)
58 set_append(d, 'FILES_%s' % pkg, '%s/%s' \
59 % (d.getVar('systemd_system_unitdir', True), unit))
Brad Bishop687146f2016-07-11 13:05:26 -040060 set_append(d, '_INSTALL_SD_UNITS', unit)
61
Brad Bishop9dc56712016-07-07 15:56:02 -040062
Brad Bishop62d676a2016-07-11 00:42:58 -040063 pn = d.getVar('PN', True)
Brad Bishop9dc56712016-07-07 15:56:02 -040064 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
65 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishope36358c2016-06-08 22:03:59 -040066
Brad Bishop9dc56712016-07-07 15:56:02 -040067 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
68 for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
Brad Bishop687146f2016-07-11 13:05:26 -040069 check_sd_unit(d, unit)
Brad Bishop9dc56712016-07-07 15:56:02 -040070 add_sd_unit(d, unit, pkg)
Brad Bishope36358c2016-06-08 22:03:59 -040071}
Brad Bishop93fb5352015-09-09 03:59:20 +000072
Brad Bishop9dc56712016-07-07 15:56:02 -040073
Brad Bishop93fb5352015-09-09 03:59:20 +000074do_install_append() {
Brad Bishope36358c2016-06-08 22:03:59 -040075 # install systemd service/socket/template files
Brad Bishop9dc56712016-07-07 15:56:02 -040076 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishope36358c2016-06-08 22:03:59 -040077 install -d ${D}${systemd_system_unitdir}
Brad Bishop9dc56712016-07-07 15:56:02 -040078 for s in ${_INSTALL_SD_UNITS}; do
79 install -m 0644 ${WORKDIR}/$s \
80 ${D}${systemd_system_unitdir}/$s
Brad Bishope36358c2016-06-08 22:03:59 -040081 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
82 -e 's,@BINDIR@,${bindir},g' \
83 -e 's,@SBINDIR@,${sbindir},g' \
84 ${D}${systemd_system_unitdir}/$s
85 done
Brad Bishop93fb5352015-09-09 03:59:20 +000086}