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. |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 24 | # |
| 25 | # SYSTEMD_USER_${PN}_${PN}.service = "foo" |
| 26 | # The user for the unit. |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 27 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 28 | |
| 29 | inherit obmc-phosphor-utils |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 30 | inherit systemd |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 31 | inherit useradd |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 32 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 33 | _INSTALL_SD_UNITS="" |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 34 | SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target" |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 35 | |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 36 | # 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... |
| 39 | USERADD_PACKAGES ?= " " |
| 40 | USERADD_PARAM_${PN} ?= ";" |
| 41 | |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 42 | |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 43 | def systemd_is_service(unit): |
| 44 | return unit.endswith('.service') |
| 45 | |
| 46 | |
| 47 | def systemd_is_template(unit): |
| 48 | return '@.' in unit |
| 49 | |
| 50 | |
| 51 | def 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 Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 59 | python() { |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 60 | def check_sd_unit(d, unit): |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 61 | 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 Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 65 | |
| 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 77 | 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 Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 80 | set_append(d, '_INSTALL_SD_UNITS', unit) |
| 81 | |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 82 | 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 Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 90 | 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 Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 117 | |
Brad Bishop | 62d676a | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 118 | pn = d.getVar('PN', True) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 119 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: |
| 120 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 121 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 122 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): |
| 123 | for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg): |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 124 | check_sd_unit(d, unit) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 125 | add_sd_unit(d, unit, pkg) |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 126 | add_sd_user(d, unit, pkg) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 127 | } |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 128 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 129 | |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 130 | python 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 Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 151 | do_install_append() { |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 152 | # install systemd service/socket/template files |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 153 | [ -z "${_INSTALL_SD_UNITS}" ] || \ |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 154 | install -d ${D}${systemd_system_unitdir} |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 155 | for s in ${_INSTALL_SD_UNITS}; do |
| 156 | install -m 0644 ${WORKDIR}/$s \ |
| 157 | ${D}${systemd_system_unitdir}/$s |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 158 | 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 Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 163 | } |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 164 | |
| 165 | |
| 166 | do_install[postfuncs] += "systemd_do_postinst" |