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 | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame^] | 16 | # |
| 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 25 | |
| 26 | inherit obmc-phosphor-utils |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 27 | inherit systemd |
| 28 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 29 | _INSTALL_SD_UNITS="" |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame^] | 30 | SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target" |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 31 | |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 32 | |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 33 | def systemd_is_service(unit): |
| 34 | return unit.endswith('.service') |
| 35 | |
| 36 | |
| 37 | def systemd_is_template(unit): |
| 38 | return '@.' in unit |
| 39 | |
| 40 | |
| 41 | def 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 Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 49 | python() { |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 50 | def check_sd_unit(d, unit): |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 51 | 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 Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 55 | |
| 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 67 | 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 Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 70 | set_append(d, '_INSTALL_SD_UNITS', unit) |
| 71 | |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame^] | 72 | 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 80 | |
Brad Bishop | 62d676a | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 81 | pn = d.getVar('PN', True) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 82 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: |
| 83 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 84 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 85 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): |
| 86 | for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg): |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 87 | check_sd_unit(d, unit) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 88 | add_sd_unit(d, unit, pkg) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 89 | } |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 90 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 91 | |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame^] | 92 | python 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 Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 113 | do_install_append() { |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 114 | # install systemd service/socket/template files |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 115 | [ -z "${_INSTALL_SD_UNITS}" ] || \ |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 116 | install -d ${D}${systemd_system_unitdir} |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 117 | for s in ${_INSTALL_SD_UNITS}; do |
| 118 | install -m 0644 ${WORKDIR}/$s \ |
| 119 | ${D}${systemd_system_unitdir}/$s |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 120 | 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 Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 125 | } |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame^] | 126 | |
| 127 | |
| 128 | do_install[postfuncs] += "systemd_do_postinst" |