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 | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame^] | 28 | # |
| 29 | # SYSTEMD_ENVIRONMENT_FILE_${PN} = "foo" |
| 30 | # One or more environment files to be installed. |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 31 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 32 | |
| 33 | inherit obmc-phosphor-utils |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 34 | inherit systemd |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 35 | inherit useradd |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 36 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 37 | _INSTALL_SD_UNITS="" |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 38 | SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target" |
Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame^] | 39 | envfiledir ?= "${sysconfdir}/default" |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 40 | |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 41 | # Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure. |
| 42 | # If there are users to be added, we'll add them in our post-parse. |
| 43 | # If not...there don't seem to be any ill effects... |
| 44 | USERADD_PACKAGES ?= " " |
| 45 | USERADD_PARAM_${PN} ?= ";" |
| 46 | |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 47 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 48 | def SystemdUnit(unit): |
| 49 | class Unit(object): |
| 50 | def __init__(self, unit): |
| 51 | self.unit = unit |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 52 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 53 | def __getattr__(self, item): |
| 54 | if item is 'name': |
| 55 | return self.unit |
| 56 | if item is 'is_activated': |
| 57 | return self.unit.startswith('dbus-') |
| 58 | if item is 'is_template': |
| 59 | return '@.' in self.unit |
| 60 | if item is 'is_instance': |
| 61 | return '@' in self.unit and not self.is_template |
| 62 | if item in ['is_service', 'is_target']: |
| 63 | return self.unit.split('.')[-1] == item |
| 64 | if item is 'base': |
| 65 | cls = self.unit.split('.')[-1] |
| 66 | base = self.unit.replace('dbus-', '') |
| 67 | base = base.replace('.%s' % cls, '') |
| 68 | if self.is_instance: |
| 69 | base = base.rstrip('@%s' % self.instance) |
| 70 | if self.is_template: |
| 71 | base = base.rstrip('@') |
| 72 | return base |
| 73 | if item is 'instance' and self.is_instance: |
| 74 | inst = self.unit.rsplit('@')[-1] |
| 75 | return inst.rsplit('.')[0] |
| 76 | if item is 'template' and self.is_instance: |
| 77 | cls = self.unit.split('.')[-1] |
| 78 | return '%s@.%s' % (self.base, cls) |
| 79 | if item is 'template' and self.is_template: |
| 80 | return '.'.join(self.base.split('@')[:-1]) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 81 | |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 82 | raise AttributeError(item) |
| 83 | return Unit(unit) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def systemd_parse_unit(d, path): |
| 87 | import ConfigParser |
| 88 | parser = ConfigParser.SafeConfigParser() |
| 89 | parser.optionxform = str |
| 90 | parser.read('%s' % path) |
| 91 | return parser |
| 92 | |
| 93 | |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 94 | python() { |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 95 | def check_sd_unit(d, unit): |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 96 | searchpaths = d.getVar('FILESPATH', True) |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 97 | path = bb.utils.which(searchpaths, '%s' % unit.name) |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 98 | if not os.path.isfile(path): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 99 | bb.fatal('Did not find unit file "%s"' % unit.name) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 100 | |
| 101 | parser = systemd_parse_unit(d, path) |
| 102 | inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING') |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 103 | if unit.is_service and \ |
| 104 | not unit.is_template and \ |
| 105 | unit.name not in inhibit and \ |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 106 | not parser.has_option('Service', 'Restart'): |
| 107 | bb.warn('Systemd unit \'%s\' does not ' |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 108 | 'have a restart policy defined.' % unit.name) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 109 | |
| 110 | |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 111 | def add_default_subs(d, file): |
| 112 | set_append(d, '_MAKE_SUBS', '%s' % file) |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 113 | |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 114 | for x in [ |
| 115 | 'base_bindir', |
| 116 | 'bindir', |
| 117 | 'sbindir', |
Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame^] | 118 | 'envfiledir', |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 119 | 'SYSTEMD_DEFAULT_TARGET' ]: |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 120 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file, |
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 | |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 124 | def add_sd_unit(d, unit, pkg): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 125 | name = unit.name |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 126 | unit_dir = d.getVar('systemd_system_unitdir', True) |
| 127 | set_append(d, 'SRC_URI', 'file://%s' % name) |
| 128 | set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name)) |
| 129 | set_append(d, '_INSTALL_SD_UNITS', name) |
| 130 | add_default_subs(d, name) |
| 131 | |
| 132 | |
| 133 | def add_sd_user(d, file, pkg): |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 134 | opts = [ |
| 135 | '--system', |
| 136 | '--home', |
| 137 | '/', |
| 138 | '--no-create-home', |
| 139 | '--shell /sbin/nologin', |
| 140 | '--user-group'] |
| 141 | |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 142 | var = 'SYSTEMD_USER_%s' % file |
Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 143 | user = listvar_to_list(d, var) |
| 144 | if len(user) is 0: |
| 145 | var = 'SYSTEMD_USER_%s' % pkg |
| 146 | user = listvar_to_list(d, var) |
| 147 | if len(user) is not 0: |
| 148 | if len(user) is not 1: |
| 149 | bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user))) |
| 150 | |
| 151 | user = user[0] |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 152 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file, |
Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 153 | 'USER:%s' % user) |
| 154 | if user not in d.getVar('USERADD_PARAM_%s' % pkg, True): |
| 155 | set_append( |
| 156 | d, |
| 157 | 'USERADD_PARAM_%s' % pkg, |
| 158 | '%s' % (' '.join(opts + [user])), |
| 159 | ';') |
Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 160 | if pkg not in d.getVar('USERADD_PACKAGES', True): |
| 161 | set_append(d, 'USERADD_PACKAGES', pkg) |
| 162 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 163 | |
Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame^] | 164 | def add_env_file(d, name, pkg): |
| 165 | set_append(d, 'SRC_URI', 'file://%s' % name) |
| 166 | set_append(d, 'FILES_%s' % pkg, '%s/%s' \ |
| 167 | % (d.getVar('envfiledir', True), name)) |
| 168 | set_append(d, '_INSTALL_ENV_FILES', name) |
| 169 | |
| 170 | |
Brad Bishop | 8b87560 | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 171 | pn = d.getVar('PN', True) |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 172 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: |
| 173 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 174 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 175 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): |
Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 176 | svc = listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg) |
| 177 | svc = [SystemdUnit(x) for x in svc] |
| 178 | tmpl = [x.template for x in svc if x.is_instance] |
| 179 | tmpl = list(set(tmpl)) |
| 180 | tmpl = [SystemdUnit(x) for x in tmpl] |
| 181 | svc = [x for x in svc if not x.is_instance] |
| 182 | |
| 183 | for unit in tmpl + svc: |
Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 184 | check_sd_unit(d, unit) |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 185 | add_sd_unit(d, unit, pkg) |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 186 | add_sd_user(d, unit.name, pkg) |
Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame^] | 187 | for name in listvar_to_list(d, 'SYSTEMD_ENVIRONMENT_FILE_%s' % pkg): |
| 188 | add_env_file(d, name, pkg) |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 189 | } |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 190 | |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 191 | |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 192 | python systemd_do_postinst() { |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 193 | def make_subs(d): |
| 194 | for f in listvar_to_list(d, '_MAKE_SUBS'): |
| 195 | subs = dict([ x.split(':') for x in |
| 196 | listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)]) |
| 197 | if not subs: |
| 198 | continue |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 199 | |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 200 | path = d.getVar('D', True) |
| 201 | path += d.getVar('systemd_system_unitdir', True) |
| 202 | path += '/%s' % f |
| 203 | with open(path, 'r') as fd: |
| 204 | content = fd.read() |
| 205 | with open(path, 'w+') as fd: |
| 206 | try: |
| 207 | fd.write(content.format(**subs)) |
| 208 | except KeyError as e: |
| 209 | bb.fatal('No substitution found for %s in ' |
| 210 | 'file \'%s\'' % (e, f)) |
| 211 | |
| 212 | |
Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame^] | 213 | def install_envs(d): |
| 214 | install_dir = d.getVar('D', True) |
| 215 | install_dir += d.getVar('envfiledir', True) |
| 216 | searchpaths = d.getVar('FILESPATH', True) |
| 217 | |
| 218 | for f in listvar_to_list(d, '_INSTALL_ENV_FILES'): |
| 219 | src = bb.utils.which(searchpaths, f) |
| 220 | if not os.path.isfile(src): |
| 221 | bb.fatal('Did not find SYSTEMD_ENVIRONMENT_FILE:' |
| 222 | '\'%s\'' % src) |
| 223 | |
| 224 | dest = os.path.join(install_dir, f) |
| 225 | parent = os.path.dirname(dest) |
| 226 | if not os.path.exists(parent): |
| 227 | os.makedirs(parent) |
| 228 | |
| 229 | with open(src, 'r') as fd: |
| 230 | content = fd.read() |
| 231 | with open(dest, 'w+') as fd: |
| 232 | fd.write(content) |
| 233 | |
| 234 | |
| 235 | install_envs(d) |
Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 236 | make_subs(d) |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 240 | do_install_append() { |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 241 | # install systemd service/socket/template files |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 242 | [ -z "${_INSTALL_SD_UNITS}" ] || \ |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 243 | install -d ${D}${systemd_system_unitdir} |
Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 244 | for s in ${_INSTALL_SD_UNITS}; do |
| 245 | install -m 0644 ${WORKDIR}/$s \ |
| 246 | ${D}${systemd_system_unitdir}/$s |
Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 247 | sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \ |
| 248 | -e 's,@BINDIR@,${bindir},g' \ |
| 249 | -e 's,@SBINDIR@,${sbindir},g' \ |
| 250 | ${D}${systemd_system_unitdir}/$s |
| 251 | done |
Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 252 | } |
Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 253 | |
| 254 | |
| 255 | do_install[postfuncs] += "systemd_do_postinst" |