Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 1 | # 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 Bishop | 62d676a | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 10 | # ${PN}.service will be added to the main package. |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame^] | 11 | # |
| 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 16 | |
| 17 | inherit obmc-phosphor-utils |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 18 | inherit systemd |
| 19 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 20 | _INSTALL_SD_UNITS="" |
| 21 | |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 22 | |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame^] | 23 | def systemd_is_service(unit): |
| 24 | return unit.endswith('.service') |
| 25 | |
| 26 | |
| 27 | def systemd_is_template(unit): |
| 28 | return '@.' in unit |
| 29 | |
| 30 | |
| 31 | def 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 Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 39 | python() { |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame^] | 40 | def check_sd_unit(d, unit): |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 41 | 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 Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame^] | 45 | |
| 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 57 | 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 Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame^] | 60 | set_append(d, '_INSTALL_SD_UNITS', unit) |
| 61 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 62 | |
Brad Bishop | 62d676a | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 63 | pn = d.getVar('PN', True) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 64 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: |
| 65 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 66 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 67 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): |
| 68 | for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg): |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame^] | 69 | check_sd_unit(d, unit) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 70 | add_sd_unit(d, unit, pkg) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 71 | } |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 72 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 73 | |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 74 | do_install_append() { |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 75 | # install systemd service/socket/template files |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 76 | [ -z "${_INSTALL_SD_UNITS}" ] || \ |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 77 | install -d ${D}${systemd_system_unitdir} |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 78 | for s in ${_INSTALL_SD_UNITS}; do |
| 79 | install -m 0644 ${WORKDIR}/$s \ |
| 80 | ${D}${systemd_system_unitdir}/$s |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 81 | 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 Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 86 | } |