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 | # |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 17 | # SYSTEMD_SUBSTITUTIONS_${path-relative-to-system_unitdir} |
| 18 | # Variables in this list will be substituted in the specified |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 19 | # file during install (if bitbake finds python {format} strings |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 20 | # in the file itself). List entries take the form: |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 21 | # VAR:VALUE |
| 22 | # where {VAR} is the format string bitbake should look for in the |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 23 | # file and VALUE is the value to substitute. |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 24 | # |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 25 | # SYSTEMD_USER_${PN}.service = "foo" |
| 26 | # SYSTEMD_USER_${unit}.service = "foo" |
| 27 | # The user for the unit/package. |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 28 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 29 | |
| 30 | inherit obmc-phosphor-utils |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 31 | inherit systemd |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 32 | inherit useradd |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 33 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 34 | _INSTALL_SD_UNITS="" |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 35 | SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target" |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 36 | |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 37 | # Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure. |
| 38 | # If there are users to be added, we'll add them in our post-parse. |
| 39 | # If not...there don't seem to be any ill effects... |
| 40 | USERADD_PACKAGES ?= " " |
| 41 | USERADD_PARAM_${PN} ?= ";" |
| 42 | |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 43 | |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 44 | def systemd_is_service(unit): |
| 45 | return unit.endswith('.service') |
| 46 | |
| 47 | |
| 48 | def systemd_is_template(unit): |
| 49 | return '@.' in unit |
| 50 | |
| 51 | |
| 52 | def systemd_parse_unit(d, path): |
| 53 | import ConfigParser |
| 54 | parser = ConfigParser.SafeConfigParser() |
| 55 | parser.optionxform = str |
| 56 | parser.read('%s' % path) |
| 57 | return parser |
| 58 | |
| 59 | |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 60 | python() { |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 61 | def check_sd_unit(d, unit): |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 62 | searchpaths = d.getVar('FILESPATH', True) |
| 63 | path = bb.utils.which(searchpaths, '%s' % unit) |
| 64 | if not os.path.isfile(path): |
| 65 | bb.fatal('Did not find unit file "%s"' % unit) |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 66 | |
| 67 | parser = systemd_parse_unit(d, path) |
| 68 | inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING') |
| 69 | if systemd_is_service(unit) and \ |
| 70 | not systemd_is_template(unit) and \ |
| 71 | unit not in inhibit and \ |
| 72 | not parser.has_option('Service', 'Restart'): |
| 73 | bb.warn('Systemd unit \'%s\' does not ' |
| 74 | 'have a restart policy defined.' % unit) |
| 75 | |
| 76 | |
| 77 | def add_sd_unit(d, unit, pkg): |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 78 | unit_dir = d.getVar('systemd_system_unitdir', True) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 79 | set_append(d, 'SRC_URI', 'file://%s' % unit) |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 80 | set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, unit)) |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 81 | set_append(d, '_INSTALL_SD_UNITS', unit) |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 82 | set_append(d, '_MAKE_SUBS', '%s' % unit) |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 83 | |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 84 | for x in [ |
| 85 | 'base_bindir', |
| 86 | 'bindir', |
| 87 | 'sbindir', |
| 88 | 'SYSTEMD_DEFAULT_TARGET' ]: |
| 89 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit, |
| 90 | '%s:%s' % (x, d.getVar(x, True))) |
| 91 | |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 92 | |
| 93 | def add_sd_user(d, unit, pkg): |
| 94 | opts = [ |
| 95 | '--system', |
| 96 | '--home', |
| 97 | '/', |
| 98 | '--no-create-home', |
| 99 | '--shell /sbin/nologin', |
| 100 | '--user-group'] |
| 101 | |
Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 102 | var = 'SYSTEMD_USER_%s' % unit |
| 103 | user = listvar_to_list(d, var) |
| 104 | if len(user) is 0: |
| 105 | var = 'SYSTEMD_USER_%s' % pkg |
| 106 | user = listvar_to_list(d, var) |
| 107 | if len(user) is not 0: |
| 108 | if len(user) is not 1: |
| 109 | bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user))) |
| 110 | |
| 111 | user = user[0] |
| 112 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit, |
| 113 | 'USER:%s' % user) |
| 114 | if user not in d.getVar('USERADD_PARAM_%s' % pkg, True): |
| 115 | set_append( |
| 116 | d, |
| 117 | 'USERADD_PARAM_%s' % pkg, |
| 118 | '%s' % (' '.join(opts + [user])), |
| 119 | ';') |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 120 | if pkg not in d.getVar('USERADD_PACKAGES', True): |
| 121 | set_append(d, 'USERADD_PACKAGES', pkg) |
| 122 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 123 | |
Brad Bishop | 62d676a | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 124 | pn = d.getVar('PN', True) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 125 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: |
| 126 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 127 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 128 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): |
| 129 | for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg): |
Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 130 | check_sd_unit(d, unit) |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 131 | add_sd_unit(d, unit, pkg) |
Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 132 | add_sd_user(d, unit, pkg) |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 133 | } |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 134 | |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 135 | |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 136 | python systemd_do_postinst() { |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 137 | for f in listvar_to_list(d, '_MAKE_SUBS'): |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 138 | subs = dict([ x.split(':') for x in |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 139 | listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)]) |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 140 | if not subs: |
| 141 | continue |
| 142 | |
| 143 | path = d.getVar('D', True) |
| 144 | path += d.getVar('systemd_system_unitdir', True) |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 145 | path += '/%s' % f |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 146 | with open(path, 'r') as fd: |
| 147 | content = fd.read() |
| 148 | with open(path, 'w+') as fd: |
| 149 | try: |
| 150 | fd.write(content.format(**subs)) |
| 151 | except KeyError as e: |
| 152 | bb.fatal('No substitution found for %s in ' |
Brad Bishop | 5e329ba | 2016-08-17 12:08:16 -0400 | [diff] [blame^] | 153 | 'file \'%s\'' % (e, f)) |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 157 | do_install_append() { |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 158 | # install systemd service/socket/template files |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 159 | [ -z "${_INSTALL_SD_UNITS}" ] || \ |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 160 | install -d ${D}${systemd_system_unitdir} |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 161 | for s in ${_INSTALL_SD_UNITS}; do |
| 162 | install -m 0644 ${WORKDIR}/$s \ |
| 163 | ${D}${systemd_system_unitdir}/$s |
Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 164 | sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \ |
| 165 | -e 's,@BINDIR@,${bindir},g' \ |
| 166 | -e 's,@SBINDIR@,${sbindir},g' \ |
| 167 | ${D}${systemd_system_unitdir}/$s |
| 168 | done |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 169 | } |
Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 170 | |
| 171 | |
| 172 | do_install[postfuncs] += "systemd_do_postinst" |