Brad Bishop | 1bb8be5 | 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 | 8b87560 | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 10 | # ${PN}.service will be added to the main package. |
Brad Bishop | 7aeda7b | 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 | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 16 | # |
Brad Bishop | 975b2cd | 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 | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 19 | # file during install (if bitbake finds python {format} strings |
Brad Bishop | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 20 | # in the file itself). List entries take the form: |
Brad Bishop | b7e2a88 | 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 | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 23 | # file and VALUE is the value to substitute. |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 24 | # |
Brad Bishop | bcd1b65 | 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 | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 28 | |
Brad Bishop | bfef6ff | 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 | 039c66b | 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 | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 34 | _INSTALL_SD_UNITS="" |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 35 | SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target" |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 36 | |
Brad Bishop | 039c66b | 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 | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 43 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 44 | def SystemdUnit(unit): |
| 45 | class Unit(object): |
| 46 | def __init__(self, unit): |
| 47 | self.unit = unit |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 48 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 49 | def __getattr__(self, item): |
| 50 | if item is 'name': |
| 51 | return self.unit |
| 52 | if item is 'is_activated': |
| 53 | return self.unit.startswith('dbus-') |
| 54 | if item is 'is_template': |
| 55 | return '@.' in self.unit |
| 56 | if item is 'is_instance': |
| 57 | return '@' in self.unit and not self.is_template |
| 58 | if item in ['is_service', 'is_target']: |
| 59 | return self.unit.split('.')[-1] == item |
| 60 | if item is 'base': |
| 61 | cls = self.unit.split('.')[-1] |
| 62 | base = self.unit.replace('dbus-', '') |
| 63 | base = base.replace('.%s' % cls, '') |
| 64 | if self.is_instance: |
| 65 | base = base.rstrip('@%s' % self.instance) |
| 66 | if self.is_template: |
| 67 | base = base.rstrip('@') |
| 68 | return base |
| 69 | if item is 'instance' and self.is_instance: |
| 70 | inst = self.unit.rsplit('@')[-1] |
| 71 | return inst.rsplit('.')[0] |
| 72 | if item is 'template' and self.is_instance: |
| 73 | cls = self.unit.split('.')[-1] |
| 74 | return '%s@.%s' % (self.base, cls) |
| 75 | if item is 'template' and self.is_template: |
| 76 | return '.'.join(self.base.split('@')[:-1]) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 77 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 78 | raise AttributeError(item) |
| 79 | return Unit(unit) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def systemd_parse_unit(d, path): |
| 83 | import ConfigParser |
| 84 | parser = ConfigParser.SafeConfigParser() |
| 85 | parser.optionxform = str |
| 86 | parser.read('%s' % path) |
| 87 | return parser |
| 88 | |
| 89 | |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 90 | python() { |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 91 | def check_sd_unit(d, unit): |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 92 | searchpaths = d.getVar('FILESPATH', True) |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 93 | path = bb.utils.which(searchpaths, '%s' % unit.name) |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 94 | if not os.path.isfile(path): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 95 | bb.fatal('Did not find unit file "%s"' % unit.name) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 96 | |
| 97 | parser = systemd_parse_unit(d, path) |
| 98 | inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING') |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 99 | if unit.is_service and \ |
| 100 | not unit.is_template and \ |
| 101 | unit.name not in inhibit and \ |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 102 | not parser.has_option('Service', 'Restart'): |
| 103 | bb.warn('Systemd unit \'%s\' does not ' |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 104 | 'have a restart policy defined.' % unit.name) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def add_sd_unit(d, unit, pkg): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 108 | name = unit.name |
Brad Bishop | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 109 | unit_dir = d.getVar('systemd_system_unitdir', True) |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 110 | set_append(d, 'SRC_URI', 'file://%s' % name) |
| 111 | set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name)) |
| 112 | set_append(d, '_INSTALL_SD_UNITS', name) |
| 113 | set_append(d, '_MAKE_SUBS', '%s' % name) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 114 | |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 115 | for x in [ |
| 116 | 'base_bindir', |
| 117 | 'bindir', |
| 118 | 'sbindir', |
| 119 | 'SYSTEMD_DEFAULT_TARGET' ]: |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 120 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % name, |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 121 | '%s:%s' % (x, d.getVar(x, True))) |
| 122 | |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 123 | |
| 124 | def add_sd_user(d, unit, pkg): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 125 | name = unit.name |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 126 | opts = [ |
| 127 | '--system', |
| 128 | '--home', |
| 129 | '/', |
| 130 | '--no-create-home', |
| 131 | '--shell /sbin/nologin', |
| 132 | '--user-group'] |
| 133 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 134 | var = 'SYSTEMD_USER_%s' % name |
Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 135 | user = listvar_to_list(d, var) |
| 136 | if len(user) is 0: |
| 137 | var = 'SYSTEMD_USER_%s' % pkg |
| 138 | user = listvar_to_list(d, var) |
| 139 | if len(user) is not 0: |
| 140 | if len(user) is not 1: |
| 141 | bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user))) |
| 142 | |
| 143 | user = user[0] |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 144 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % name, |
Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 145 | 'USER:%s' % user) |
| 146 | if user not in d.getVar('USERADD_PARAM_%s' % pkg, True): |
| 147 | set_append( |
| 148 | d, |
| 149 | 'USERADD_PARAM_%s' % pkg, |
| 150 | '%s' % (' '.join(opts + [user])), |
| 151 | ';') |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 152 | if pkg not in d.getVar('USERADD_PACKAGES', True): |
| 153 | set_append(d, 'USERADD_PACKAGES', pkg) |
| 154 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 155 | |
Brad Bishop | 8b87560 | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 156 | pn = d.getVar('PN', True) |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 157 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: |
| 158 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 159 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 160 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame^] | 161 | svc = listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg) |
| 162 | svc = [SystemdUnit(x) for x in svc] |
| 163 | tmpl = [x.template for x in svc if x.is_instance] |
| 164 | tmpl = list(set(tmpl)) |
| 165 | tmpl = [SystemdUnit(x) for x in tmpl] |
| 166 | svc = [x for x in svc if not x.is_instance] |
| 167 | |
| 168 | for unit in tmpl + svc: |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 169 | check_sd_unit(d, unit) |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 170 | add_sd_unit(d, unit, pkg) |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 171 | add_sd_user(d, unit, pkg) |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 172 | } |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 173 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 174 | |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 175 | python systemd_do_postinst() { |
Brad Bishop | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 176 | for f in listvar_to_list(d, '_MAKE_SUBS'): |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 177 | subs = dict([ x.split(':') for x in |
Brad Bishop | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 178 | listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)]) |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 179 | if not subs: |
| 180 | continue |
| 181 | |
| 182 | path = d.getVar('D', True) |
| 183 | path += d.getVar('systemd_system_unitdir', True) |
Brad Bishop | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 184 | path += '/%s' % f |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 185 | with open(path, 'r') as fd: |
| 186 | content = fd.read() |
| 187 | with open(path, 'w+') as fd: |
| 188 | try: |
| 189 | fd.write(content.format(**subs)) |
| 190 | except KeyError as e: |
| 191 | bb.fatal('No substitution found for %s in ' |
Brad Bishop | 975b2cd | 2016-08-17 12:08:16 -0400 | [diff] [blame] | 192 | 'file \'%s\'' % (e, f)) |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 196 | do_install_append() { |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 197 | # install systemd service/socket/template files |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 198 | [ -z "${_INSTALL_SD_UNITS}" ] || \ |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 199 | install -d ${D}${systemd_system_unitdir} |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 200 | for s in ${_INSTALL_SD_UNITS}; do |
| 201 | install -m 0644 ${WORKDIR}/$s \ |
| 202 | ${D}${systemd_system_unitdir}/$s |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 203 | sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \ |
| 204 | -e 's,@BINDIR@,${bindir},g' \ |
| 205 | -e 's,@SBINDIR@,${sbindir},g' \ |
| 206 | ${D}${systemd_system_unitdir}/$s |
| 207 | done |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 208 | } |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 209 | |
| 210 | |
| 211 | do_install[postfuncs] += "systemd_do_postinst" |