| 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 | 8dcce25 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 17 | # SYSTEMD_SUBSTITUTIONS = "var:val:file" | 
 | 18 | #    A specification for making python style {format} string | 
 | 19 | #    substitutions where: | 
 | 20 | #      var: the format string to search for | 
 | 21 | #      val: the value to replace with | 
 | 22 | #      file: the file in which to make the substitution | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 23 | # | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 24 | # SYSTEMD_USER_${PN}.service = "foo" | 
 | 25 | # SYSTEMD_USER_${unit}.service = "foo" | 
 | 26 | #    The user for the unit/package. | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 27 | # | 
 | 28 | # SYSTEMD_ENVIRONMENT_FILE_${PN} = "foo" | 
 | 29 | #    One or more environment files to be installed. | 
| Brad Bishop | d4bdab2 | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 30 | # | 
 | 31 | # SYSTEMD_LINK_${PN} = "tgt:name" | 
 | 32 | #    A specification for installing arbitrary links in | 
 | 33 | #    the ${systemd_system_unitdir} namespace, where: | 
 | 34 | #      tgt: the link target | 
 | 35 | #      name: the link name, relative to ${systemd_system_unitdir} | 
| Brad Bishop | ed916e7 | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 36 | # | 
 | 37 | # SYSTEMD_OVERRIDE_${PN} = "src:dest" | 
 | 38 | #    A specification for installing unit overrides where: | 
 | 39 | #      src: the override file template | 
 | 40 | #      dest: the override install location, relative to ${systemd_system_unitdir} | 
 | 41 | # | 
 | 42 | #    Typically SYSTEMD_SUBSTITUTIONS is used to deploy a range | 
 | 43 | #    of overrides from a single template file.  To simply install | 
 | 44 | #    a single override use "foo.conf:my-service.d/foo.conf" | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 45 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 46 |  | 
 | 47 | inherit obmc-phosphor-utils | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 48 | inherit systemd | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 49 | inherit useradd | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 50 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 51 | _INSTALL_SD_UNITS="" | 
| Patrick Venture | d607cfb | 2019-04-04 12:15:22 -0700 | [diff] [blame] | 52 | SYSTEMD_DEFAULT_TARGET ?= "multi-user.target" | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 53 | envfiledir ?= "${sysconfdir}/default" | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 54 |  | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 55 | # Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure. | 
 | 56 | # If there are users to be added, we'll add them in our post-parse. | 
 | 57 | # If not...there don't seem to be any ill effects... | 
 | 58 | USERADD_PACKAGES ?= " " | 
 | 59 | USERADD_PARAM_${PN} ?= ";" | 
 | 60 |  | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 61 |  | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 62 | def SystemdUnit(unit): | 
 | 63 |     class Unit(object): | 
 | 64 |         def __init__(self, unit): | 
 | 65 |             self.unit = unit | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 66 |  | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 67 |         def __getattr__(self, item): | 
 | 68 |             if item is 'name': | 
 | 69 |                 return self.unit | 
 | 70 |             if item is 'is_activated': | 
 | 71 |                 return self.unit.startswith('dbus-') | 
 | 72 |             if item is 'is_template': | 
 | 73 |                 return '@.' in self.unit | 
 | 74 |             if item is 'is_instance': | 
 | 75 |                 return '@' in self.unit and not self.is_template | 
 | 76 |             if item in ['is_service', 'is_target']: | 
 | 77 |                 return self.unit.split('.')[-1] == item | 
 | 78 |             if item is 'base': | 
 | 79 |                 cls = self.unit.split('.')[-1] | 
 | 80 |                 base = self.unit.replace('dbus-', '') | 
 | 81 |                 base = base.replace('.%s' % cls, '') | 
 | 82 |                 if self.is_instance: | 
| Robert Lippert | 07f5950 | 2017-11-28 10:59:31 -0800 | [diff] [blame] | 83 |                     base = base.replace('@%s' % self.instance, '') | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 84 |                 if self.is_template: | 
 | 85 |                     base = base.rstrip('@') | 
 | 86 |                 return base | 
 | 87 |             if item is 'instance' and self.is_instance: | 
 | 88 |                 inst = self.unit.rsplit('@')[-1] | 
 | 89 |                 return inst.rsplit('.')[0] | 
 | 90 |             if item is 'template' and self.is_instance: | 
 | 91 |                 cls = self.unit.split('.')[-1] | 
 | 92 |                 return '%s@.%s' % (self.base, cls) | 
 | 93 |             if item is 'template' and self.is_template: | 
 | 94 |                 return '.'.join(self.base.split('@')[:-1]) | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 95 |  | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 96 |             raise AttributeError(item) | 
 | 97 |     return Unit(unit) | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 98 |  | 
 | 99 |  | 
 | 100 | def systemd_parse_unit(d, path): | 
| Saqib Khan | a836fb4 | 2017-03-07 06:59:20 -0600 | [diff] [blame] | 101 |     import configparser | 
 | 102 |     parser = configparser.SafeConfigParser(strict=False) | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 103 |     parser.optionxform = str | 
 | 104 |     parser.read('%s' % path) | 
 | 105 |     return parser | 
 | 106 |  | 
 | 107 |  | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 108 | python() { | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 109 |     def check_sd_unit(d, unit): | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 110 |         searchpaths = d.getVar('FILESPATH', True) | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 111 |         path = bb.utils.which(searchpaths, '%s' % unit.name) | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 112 |         if not os.path.isfile(path): | 
| Lei YU | 5f56aaf | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 113 |             # Unit does not exist in tree. Allow it to install from repo. | 
 | 114 |             # Return False here to indicate it does not exist. | 
 | 115 |             return False | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 116 |  | 
 | 117 |         parser = systemd_parse_unit(d, path) | 
 | 118 |         inhibit = listvar_to_list(d, 'INHIBIT_SYSTEMD_RESTART_POLICY_WARNING') | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 119 |         if unit.is_service and \ | 
 | 120 |                 not unit.is_template and \ | 
 | 121 |                 unit.name not in inhibit and \ | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 122 |                 not parser.has_option('Service', 'Restart'): | 
 | 123 |             bb.warn('Systemd unit \'%s\' does not ' | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 124 |                 'have a restart policy defined.' % unit.name) | 
| Lei YU | 5f56aaf | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 125 |         return True | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 126 |  | 
 | 127 |  | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 128 |     def add_default_subs(d, file): | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 129 |         for x in [ | 
 | 130 |                 'base_bindir', | 
 | 131 |                 'bindir', | 
 | 132 |                 'sbindir', | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 133 |                 'envfiledir', | 
| Xo Wang | e72ff98 | 2017-01-18 17:55:50 -0800 | [diff] [blame] | 134 |                 'sysconfdir', | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 135 |                 'SYSTEMD_DEFAULT_TARGET' ]: | 
| Brad Bishop | 8dcce25 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 136 |             set_append(d, 'SYSTEMD_SUBSTITUTIONS', | 
 | 137 |                 '%s:%s:%s' % (x, d.getVar(x, True), file)) | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 138 |  | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 139 |  | 
| Lei YU | 5f56aaf | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 140 |     def add_sd_unit(d, unit, pkg, unit_exist): | 
 | 141 |         # Do not add unit if it does not exist in tree. | 
 | 142 |         # It will be installed from repo. | 
 | 143 |         if not unit_exist: | 
 | 144 |             return | 
 | 145 |  | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 146 |         name = unit.name | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 147 |         unit_dir = d.getVar('systemd_system_unitdir', True) | 
 | 148 |         set_append(d, 'SRC_URI', 'file://%s' % name) | 
 | 149 |         set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, name)) | 
 | 150 |         set_append(d, '_INSTALL_SD_UNITS', name) | 
 | 151 |         add_default_subs(d, name) | 
 | 152 |  | 
 | 153 |  | 
 | 154 |     def add_sd_user(d, file, pkg): | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 155 |         opts = [ | 
 | 156 |             '--system', | 
 | 157 |             '--home', | 
 | 158 |             '/', | 
 | 159 |             '--no-create-home', | 
 | 160 |             '--shell /sbin/nologin', | 
 | 161 |             '--user-group'] | 
 | 162 |  | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 163 |         var = 'SYSTEMD_USER_%s' % file | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 164 |         user = listvar_to_list(d, var) | 
 | 165 |         if len(user) is 0: | 
 | 166 |             var = 'SYSTEMD_USER_%s' % pkg | 
 | 167 |             user = listvar_to_list(d, var) | 
 | 168 |         if len(user) is not 0: | 
 | 169 |             if len(user) is not 1: | 
 | 170 |                 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user))) | 
 | 171 |  | 
 | 172 |             user = user[0] | 
| Brad Bishop | 8dcce25 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 173 |             set_append(d, 'SYSTEMD_SUBSTITUTIONS', | 
 | 174 |                 'USER:%s:%s' % (user, file)) | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 175 |             if user not in d.getVar('USERADD_PARAM_%s' % pkg, True): | 
 | 176 |                 set_append( | 
 | 177 |                     d, | 
 | 178 |                     'USERADD_PARAM_%s' % pkg, | 
 | 179 |                     '%s' % (' '.join(opts + [user])), | 
 | 180 |                     ';') | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 181 |             if pkg not in d.getVar('USERADD_PACKAGES', True): | 
 | 182 |                 set_append(d, 'USERADD_PACKAGES', pkg) | 
 | 183 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 184 |  | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 185 |     def add_env_file(d, name, pkg): | 
 | 186 |         set_append(d, 'SRC_URI', 'file://%s' % name) | 
 | 187 |         set_append(d, 'FILES_%s' % pkg, '%s/%s' \ | 
 | 188 |             % (d.getVar('envfiledir', True), name)) | 
 | 189 |         set_append(d, '_INSTALL_ENV_FILES', name) | 
 | 190 |  | 
 | 191 |  | 
| Brad Bishop | d4bdab2 | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 192 |     def install_link(d, spec, pkg): | 
 | 193 |         tgt, dest = spec.split(':') | 
 | 194 |  | 
 | 195 |         set_append(d, 'FILES_%s' % pkg, '%s/%s' \ | 
 | 196 |             % (d.getVar('systemd_system_unitdir', True), dest)) | 
 | 197 |         set_append(d, '_INSTALL_LINKS', spec) | 
 | 198 |  | 
 | 199 |  | 
| Brad Bishop | ed916e7 | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 200 |     def add_override(d, spec, pkg): | 
 | 201 |         tmpl, dest = spec.split(':') | 
 | 202 |         set_append(d, '_INSTALL_OVERRIDES', '%s' % spec) | 
 | 203 |         unit_dir = d.getVar('systemd_system_unitdir', True) | 
 | 204 |         set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, dest)) | 
 | 205 |         add_default_subs(d, '%s' % dest) | 
 | 206 |         add_sd_user(d, '%s' % dest, pkg) | 
 | 207 |  | 
 | 208 |  | 
| Brad Bishop | 7a11951 | 2018-01-25 07:00:27 -0500 | [diff] [blame] | 209 |     if d.getVar('CLASSOVERRIDE', True) != 'class-target': | 
 | 210 |         return | 
 | 211 |  | 
 | 212 |     d.appendVarFlag('do_install', 'postfuncs', ' systemd_do_postinst') | 
 | 213 |  | 
| Brad Bishop | 8b87560 | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 214 |     pn = d.getVar('PN', True) | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 215 |     if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: | 
 | 216 |         d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 217 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 218 |     for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): | 
| Brad Bishop | b1ebc60 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 219 |         svc = listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg) | 
 | 220 |         svc = [SystemdUnit(x) for x in svc] | 
 | 221 |         tmpl = [x.template for x in svc if x.is_instance] | 
 | 222 |         tmpl = list(set(tmpl)) | 
 | 223 |         tmpl = [SystemdUnit(x) for x in tmpl] | 
 | 224 |         svc = [x for x in svc if not x.is_instance] | 
 | 225 |  | 
 | 226 |         for unit in tmpl + svc: | 
| Lei YU | 5f56aaf | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 227 |             unit_exist = check_sd_unit(d, unit) | 
 | 228 |             add_sd_unit(d, unit, pkg, unit_exist) | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 229 |             add_sd_user(d, unit.name, pkg) | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 230 |         for name in listvar_to_list(d, 'SYSTEMD_ENVIRONMENT_FILE_%s' % pkg): | 
 | 231 |             add_env_file(d, name, pkg) | 
| Brad Bishop | d4bdab2 | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 232 |         for spec in listvar_to_list(d, 'SYSTEMD_LINK_%s' % pkg): | 
 | 233 |             install_link(d, spec, pkg) | 
| Brad Bishop | ed916e7 | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 234 |         for spec in listvar_to_list(d, 'SYSTEMD_OVERRIDE_%s' % pkg): | 
 | 235 |             add_override(d, spec, pkg) | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 236 | } | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 237 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 238 |  | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 239 | python systemd_do_postinst() { | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 240 |     def make_subs(d): | 
| Brad Bishop | 8dcce25 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 241 |         all_subs = {} | 
 | 242 |         for spec in listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS'): | 
 | 243 |             spec, file = spec.rsplit(':', 1) | 
 | 244 |             all_subs.setdefault(file, []).append(spec) | 
 | 245 |  | 
| Saqib Khan | 8e75c83 | 2017-03-07 07:15:29 -0600 | [diff] [blame] | 246 |         for f, v in all_subs.items(): | 
| Brad Bishop | 8dcce25 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 247 |             subs = dict([ x.split(':') for x in v]) | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 248 |             if not subs: | 
 | 249 |                 continue | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 250 |  | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 251 |             path = d.getVar('D', True) | 
 | 252 |             path += d.getVar('systemd_system_unitdir', True) | 
 | 253 |             path += '/%s' % f | 
 | 254 |             with open(path, 'r') as fd: | 
 | 255 |                 content = fd.read() | 
 | 256 |             with open(path, 'w+') as fd: | 
 | 257 |                 try: | 
 | 258 |                     fd.write(content.format(**subs)) | 
 | 259 |                 except KeyError as e: | 
 | 260 |                     bb.fatal('No substitution found for %s in ' | 
 | 261 |                         'file \'%s\'' % (e, f)) | 
 | 262 |  | 
 | 263 |  | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 264 |     def install_envs(d): | 
 | 265 |         install_dir = d.getVar('D', True) | 
 | 266 |         install_dir += d.getVar('envfiledir', True) | 
 | 267 |         searchpaths = d.getVar('FILESPATH', True) | 
 | 268 |  | 
 | 269 |         for f in listvar_to_list(d, '_INSTALL_ENV_FILES'): | 
 | 270 |             src = bb.utils.which(searchpaths, f) | 
 | 271 |             if not os.path.isfile(src): | 
 | 272 |                 bb.fatal('Did not find SYSTEMD_ENVIRONMENT_FILE:' | 
 | 273 |                     '\'%s\'' % src) | 
 | 274 |  | 
 | 275 |             dest = os.path.join(install_dir, f) | 
 | 276 |             parent = os.path.dirname(dest) | 
 | 277 |             if not os.path.exists(parent): | 
 | 278 |                 os.makedirs(parent) | 
 | 279 |  | 
 | 280 |             with open(src, 'r') as fd: | 
 | 281 |                 content = fd.read() | 
 | 282 |             with open(dest, 'w+') as fd: | 
 | 283 |                 fd.write(content) | 
 | 284 |  | 
 | 285 |  | 
| Brad Bishop | d4bdab2 | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 286 |     def install_links(d): | 
 | 287 |         install_dir = d.getVar('D', True) | 
 | 288 |         install_dir += d.getVar('systemd_system_unitdir', True) | 
 | 289 |  | 
 | 290 |         for spec in listvar_to_list(d, '_INSTALL_LINKS'): | 
 | 291 |             tgt, dest = spec.split(':') | 
 | 292 |             dest = os.path.join(install_dir, dest) | 
 | 293 |             parent = os.path.dirname(dest) | 
 | 294 |             if not os.path.exists(parent): | 
 | 295 |                 os.makedirs(parent) | 
 | 296 |             os.symlink(tgt, dest) | 
 | 297 |  | 
 | 298 |  | 
| Brad Bishop | ed916e7 | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 299 |     def install_overrides(d): | 
 | 300 |         install_dir = d.getVar('D', True) | 
 | 301 |         install_dir += d.getVar('systemd_system_unitdir', True) | 
 | 302 |         searchpaths = d.getVar('FILESPATH', True) | 
 | 303 |  | 
 | 304 |         for spec in listvar_to_list(d, '_INSTALL_OVERRIDES'): | 
 | 305 |             tmpl, dest = spec.split(':') | 
 | 306 |             source = bb.utils.which(searchpaths, tmpl) | 
 | 307 |             if not os.path.isfile(source): | 
 | 308 |                 bb.fatal('Did not find SYSTEMD_OVERRIDE ' | 
 | 309 |                     'template: \'%s\'' % source) | 
 | 310 |  | 
 | 311 |             dest = os.path.join(install_dir, dest) | 
 | 312 |             parent = os.path.dirname(dest) | 
 | 313 |             if not os.path.exists(parent): | 
 | 314 |                 os.makedirs(parent) | 
 | 315 |  | 
 | 316 |             with open(source, 'r') as fd: | 
 | 317 |                 content = fd.read() | 
 | 318 |             with open('%s' % dest, 'w+') as fd: | 
 | 319 |                 fd.write(content) | 
 | 320 |  | 
 | 321 |  | 
| Brad Bishop | d4bdab2 | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 322 |     install_links(d) | 
| Brad Bishop | 0d703a0 | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 323 |     install_envs(d) | 
| Brad Bishop | ed916e7 | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 324 |     install_overrides(d) | 
| Brad Bishop | aab8d36 | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 325 |     make_subs(d) | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 326 | } | 
 | 327 |  | 
 | 328 |  | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 329 | do_install_append() { | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 330 |         # install systemd service/socket/template files | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 331 |         [ -z "${_INSTALL_SD_UNITS}" ] || \ | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 332 |                 install -d ${D}${systemd_system_unitdir} | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 333 |         for s in ${_INSTALL_SD_UNITS}; do | 
 | 334 |                 install -m 0644 ${WORKDIR}/$s \ | 
 | 335 |                         ${D}${systemd_system_unitdir}/$s | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 336 |                 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \ | 
 | 337 |                         -e 's,@BINDIR@,${bindir},g' \ | 
 | 338 |                         -e 's,@SBINDIR@,${sbindir},g' \ | 
 | 339 |                         ${D}${systemd_system_unitdir}/$s | 
 | 340 |         done | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 341 | } |