| 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 | # | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 5 | # SYSTEMD_SERVICE:${PN} = "foo.service bar.socket baz@.service" | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 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 | 58ab4a0 | 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 | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 23 | # | 
| Brad Bishop | 58700d1 | 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 | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 27 | # | 
| Patrick Williams | e83c220 | 2021-09-01 16:17:33 -0500 | [diff] [blame] | 28 | # SYSTEMD_ENVIRONMENT_FILE:${PN} = "foo" | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 29 | #    One or more environment files to be installed. | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 30 | # | 
| Patrick Williams | e83c220 | 2021-09-01 16:17:33 -0500 | [diff] [blame] | 31 | # SYSTEMD_LINK:${PN} = "tgt:name" | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 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 | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 36 | # | 
| Patrick Williams | e83c220 | 2021-09-01 16:17:33 -0500 | [diff] [blame] | 37 | # SYSTEMD_OVERRIDE:${PN} = "src:dest" | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 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 | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 45 |  | 
| Brad Bishop | 9dc5671 | 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 | f815b2e | 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 | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 51 | _INSTALL_SD_UNITS="" | 
| Brad Bishop | fc81e38 | 2019-04-06 13:58:07 -0400 | [diff] [blame] | 52 | SYSTEMD_DEFAULT_TARGET ?= "multi-user.target" | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 53 | envfiledir ?= "${sysconfdir}/default" | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 54 |  | 
| Brad Bishop | f815b2e | 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 ?= " " | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 59 | USERADD_PARAM:${PN} ?= ";" | 
| Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 60 |  | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 61 |  | 
| Brad Bishop | 1864529 | 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 | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 66 |  | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 67 |         def __getattr__(self, item): | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 68 |             if item == 'name': | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 69 |                 return self.unit | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 70 |             if item == 'is_activated': | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 71 |                 return self.unit.startswith('dbus-') | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 72 |             if item == 'is_template': | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 73 |                 return '@.' in self.unit | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 74 |             if item == 'is_instance': | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 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 | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 78 |             if item == 'base': | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 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 | 0ccabae | 2017-11-28 10:59:31 -0800 | [diff] [blame] | 83 |                     base = base.replace('@%s' % self.instance, '') | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 84 |                 if self.is_template: | 
 | 85 |                     base = base.rstrip('@') | 
 | 86 |                 return base | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 87 |             if item == 'instance' and self.is_instance: | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 88 |                 inst = self.unit.rsplit('@')[-1] | 
 | 89 |                 return inst.rsplit('.')[0] | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 90 |             if item == 'template' and self.is_instance: | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 91 |                 cls = self.unit.split('.')[-1] | 
 | 92 |                 return '%s@.%s' % (self.base, cls) | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 93 |             if item == 'template' and self.is_template: | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 94 |                 return '.'.join(self.base.split('@')[:-1]) | 
| Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 95 |  | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 96 |             raise AttributeError(item) | 
 | 97 |     return Unit(unit) | 
| Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 98 |  | 
 | 99 |  | 
 | 100 | def systemd_parse_unit(d, path): | 
| Saqib Khan | c140158 | 2017-03-07 06:59:20 -0600 | [diff] [blame] | 101 |     import configparser | 
| George Liu | 400d9da | 2021-10-13 16:23:01 +0800 | [diff] [blame] | 102 |     parser = configparser.ConfigParser(strict=False) | 
| Brad Bishop | 687146f | 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 | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 108 | python() { | 
| Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 109 |     def check_sd_unit(d, unit): | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 110 |         searchpaths = d.getVar('FILESPATH', True) | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 111 |         path = bb.utils.which(searchpaths, '%s' % unit.name) | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 112 |         if not os.path.isfile(path): | 
| Lei YU | f33c69c | 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 | 687146f | 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 | 1864529 | 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 | 687146f | 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 | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 124 |                 'have a restart policy defined.' % unit.name) | 
| Lei YU | f33c69c | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 125 |         return True | 
| Brad Bishop | 687146f | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 126 |  | 
 | 127 |  | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 128 |     def add_default_subs(d, file): | 
| Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 129 |         for x in [ | 
 | 130 |                 'base_bindir', | 
 | 131 |                 'bindir', | 
 | 132 |                 'sbindir', | 
| William A. Kennington III | 25e1057 | 2020-04-03 11:53:09 -0700 | [diff] [blame] | 133 |                 'libexecdir', | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 134 |                 'envfiledir', | 
| Xo Wang | d509c4c | 2017-01-18 17:55:50 -0800 | [diff] [blame] | 135 |                 'sysconfdir', | 
| William A. Kennington III | 25e1057 | 2020-04-03 11:53:09 -0700 | [diff] [blame] | 136 |                 'localstatedir', | 
 | 137 |                 'datadir', | 
| Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 138 |                 'SYSTEMD_DEFAULT_TARGET' ]: | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 139 |             set_doappend(d, 'SYSTEMD_SUBSTITUTIONS', | 
| Brad Bishop | 58ab4a0 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 140 |                 '%s:%s:%s' % (x, d.getVar(x, True), file)) | 
| Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 141 |  | 
| Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 142 |  | 
| Lei YU | f33c69c | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 143 |     def add_sd_unit(d, unit, pkg, unit_exist): | 
 | 144 |         # Do not add unit if it does not exist in tree. | 
 | 145 |         # It will be installed from repo. | 
 | 146 |         if not unit_exist: | 
 | 147 |             return | 
 | 148 |  | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 149 |         name = unit.name | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 150 |         unit_dir = d.getVar('systemd_system_unitdir', True) | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 151 |         set_doappend(d, 'SRC_URI', 'file://%s' % name) | 
 | 152 |         set_doappend(d, 'FILES:%s' % pkg, '%s/%s' % (unit_dir, name)) | 
 | 153 |         set_doappend(d, '_INSTALL_SD_UNITS', name) | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 154 |         add_default_subs(d, name) | 
 | 155 |  | 
 | 156 |  | 
 | 157 |     def add_sd_user(d, file, pkg): | 
| Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 158 |         opts = [ | 
 | 159 |             '--system', | 
 | 160 |             '--home', | 
 | 161 |             '/', | 
 | 162 |             '--no-create-home', | 
 | 163 |             '--shell /sbin/nologin', | 
 | 164 |             '--user-group'] | 
 | 165 |  | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 166 |         var = 'SYSTEMD_USER_%s' % file | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 167 |         user = listvar_to_list(d, var) | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 168 |         if len(user) == 0: | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 169 |             var = 'SYSTEMD_USER_%s' % pkg | 
 | 170 |             user = listvar_to_list(d, var) | 
| Joel Stanley | 37c8ead | 2021-02-25 15:00:34 +1030 | [diff] [blame] | 171 |         if len(user) != 0: | 
 | 172 |             if len(user) != 1: | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 173 |                 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user))) | 
 | 174 |  | 
 | 175 |             user = user[0] | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 176 |             set_doappend(d, 'SYSTEMD_SUBSTITUTIONS', | 
| Brad Bishop | 58ab4a0 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 177 |                 'USER:%s:%s' % (user, file)) | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 178 |             if user not in d.getVar('USERADD_PARAM:%s' % pkg, True): | 
 | 179 |                 set_doappend( | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 180 |                     d, | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 181 |                     'USERADD_PARAM:%s' % pkg, | 
| Brad Bishop | 58700d1 | 2016-08-15 22:35:58 -0400 | [diff] [blame] | 182 |                     '%s' % (' '.join(opts + [user])), | 
 | 183 |                     ';') | 
| Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 184 |             if pkg not in d.getVar('USERADD_PACKAGES', True): | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 185 |                 set_doappend(d, 'USERADD_PACKAGES', pkg) | 
| Brad Bishop | f815b2e | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 186 |  | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 187 |  | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 188 |     def add_env_file(d, name, pkg): | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 189 |         set_doappend(d, 'SRC_URI', 'file://%s' % name) | 
 | 190 |         set_doappend(d, 'FILES:%s' % pkg, '%s/%s' \ | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 191 |             % (d.getVar('envfiledir', True), name)) | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 192 |         set_doappend(d, '_INSTALL_ENV_FILES', name) | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 193 |  | 
 | 194 |  | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 195 |     def install_link(d, spec, pkg): | 
 | 196 |         tgt, dest = spec.split(':') | 
 | 197 |  | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 198 |         set_doappend(d, 'FILES:%s' % pkg, '%s/%s' \ | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 199 |             % (d.getVar('systemd_system_unitdir', True), dest)) | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 200 |         set_doappend(d, '_INSTALL_LINKS', spec) | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 201 |  | 
 | 202 |  | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 203 |     def add_override(d, spec, pkg): | 
 | 204 |         tmpl, dest = spec.split(':') | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 205 |         set_doappend(d, '_INSTALL_OVERRIDES', '%s' % spec) | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 206 |         unit_dir = d.getVar('systemd_system_unitdir', True) | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 207 |         set_doappend(d, 'FILES:%s' % pkg, '%s/%s' % (unit_dir, dest)) | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 208 |         add_default_subs(d, '%s' % dest) | 
 | 209 |         add_sd_user(d, '%s' % dest, pkg) | 
 | 210 |  | 
 | 211 |  | 
| Brad Bishop | 038b44b | 2018-01-25 07:00:27 -0500 | [diff] [blame] | 212 |     if d.getVar('CLASSOVERRIDE', True) != 'class-target': | 
 | 213 |         return | 
 | 214 |  | 
 | 215 |     d.appendVarFlag('do_install', 'postfuncs', ' systemd_do_postinst') | 
 | 216 |  | 
| Brad Bishop | 62d676a | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 217 |     pn = d.getVar('PN', True) | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 218 |     if d.getVar('SYSTEMD_SERVICE:%s' % pn, True) is None: | 
 | 219 |         d.setVar('SYSTEMD_SERVICE:%s' % pn, '%s.service' % pn) | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 220 |  | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 221 |     for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 222 |         svc = listvar_to_list(d, 'SYSTEMD_SERVICE:%s' % pkg) | 
| Brad Bishop | 1864529 | 2016-08-16 08:18:07 -0400 | [diff] [blame] | 223 |         svc = [SystemdUnit(x) for x in svc] | 
 | 224 |         tmpl = [x.template for x in svc if x.is_instance] | 
 | 225 |         tmpl = list(set(tmpl)) | 
 | 226 |         tmpl = [SystemdUnit(x) for x in tmpl] | 
 | 227 |         svc = [x for x in svc if not x.is_instance] | 
 | 228 |  | 
 | 229 |         for unit in tmpl + svc: | 
| Lei YU | f33c69c | 2019-03-14 13:29:07 +0800 | [diff] [blame] | 230 |             unit_exist = check_sd_unit(d, unit) | 
 | 231 |             add_sd_unit(d, unit, pkg, unit_exist) | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 232 |             add_sd_user(d, unit.name, pkg) | 
| Patrick Williams | e83c220 | 2021-09-01 16:17:33 -0500 | [diff] [blame] | 233 |         for name in listvar_to_list(d, 'SYSTEMD_ENVIRONMENT_FILE:%s' % pkg): | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 234 |             add_env_file(d, name, pkg) | 
| Patrick Williams | e83c220 | 2021-09-01 16:17:33 -0500 | [diff] [blame] | 235 |         for spec in listvar_to_list(d, 'SYSTEMD_LINK:%s' % pkg): | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 236 |             install_link(d, spec, pkg) | 
| Patrick Williams | e83c220 | 2021-09-01 16:17:33 -0500 | [diff] [blame] | 237 |         for spec in listvar_to_list(d, 'SYSTEMD_OVERRIDE:%s' % pkg): | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 238 |             add_override(d, spec, pkg) | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 239 | } | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 240 |  | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 241 |  | 
| Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 242 | python systemd_do_postinst() { | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 243 |     def make_subs(d): | 
| Brad Bishop | 58ab4a0 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 244 |         all_subs = {} | 
 | 245 |         for spec in listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS'): | 
 | 246 |             spec, file = spec.rsplit(':', 1) | 
 | 247 |             all_subs.setdefault(file, []).append(spec) | 
 | 248 |  | 
| Saqib Khan | cef980a | 2017-03-07 07:15:29 -0600 | [diff] [blame] | 249 |         for f, v in all_subs.items(): | 
| Brad Bishop | 58ab4a0 | 2016-08-17 15:58:26 -0400 | [diff] [blame] | 250 |             subs = dict([ x.split(':') for x in v]) | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 251 |             if not subs: | 
 | 252 |                 continue | 
| Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 253 |  | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 254 |             path = d.getVar('D', True) | 
 | 255 |             path += d.getVar('systemd_system_unitdir', True) | 
 | 256 |             path += '/%s' % f | 
 | 257 |             with open(path, 'r') as fd: | 
 | 258 |                 content = fd.read() | 
 | 259 |             with open(path, 'w+') as fd: | 
 | 260 |                 try: | 
 | 261 |                     fd.write(content.format(**subs)) | 
 | 262 |                 except KeyError as e: | 
 | 263 |                     bb.fatal('No substitution found for %s in ' | 
 | 264 |                         'file \'%s\'' % (e, f)) | 
 | 265 |  | 
 | 266 |  | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 267 |     def install_envs(d): | 
 | 268 |         install_dir = d.getVar('D', True) | 
 | 269 |         install_dir += d.getVar('envfiledir', True) | 
 | 270 |         searchpaths = d.getVar('FILESPATH', True) | 
 | 271 |  | 
 | 272 |         for f in listvar_to_list(d, '_INSTALL_ENV_FILES'): | 
 | 273 |             src = bb.utils.which(searchpaths, f) | 
 | 274 |             if not os.path.isfile(src): | 
 | 275 |                 bb.fatal('Did not find SYSTEMD_ENVIRONMENT_FILE:' | 
 | 276 |                     '\'%s\'' % src) | 
 | 277 |  | 
 | 278 |             dest = os.path.join(install_dir, f) | 
 | 279 |             parent = os.path.dirname(dest) | 
 | 280 |             if not os.path.exists(parent): | 
 | 281 |                 os.makedirs(parent) | 
 | 282 |  | 
 | 283 |             with open(src, 'r') as fd: | 
 | 284 |                 content = fd.read() | 
 | 285 |             with open(dest, 'w+') as fd: | 
 | 286 |                 fd.write(content) | 
 | 287 |  | 
 | 288 |  | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 289 |     def install_links(d): | 
 | 290 |         install_dir = d.getVar('D', True) | 
 | 291 |         install_dir += d.getVar('systemd_system_unitdir', True) | 
 | 292 |  | 
 | 293 |         for spec in listvar_to_list(d, '_INSTALL_LINKS'): | 
 | 294 |             tgt, dest = spec.split(':') | 
 | 295 |             dest = os.path.join(install_dir, dest) | 
 | 296 |             parent = os.path.dirname(dest) | 
 | 297 |             if not os.path.exists(parent): | 
 | 298 |                 os.makedirs(parent) | 
 | 299 |             os.symlink(tgt, dest) | 
 | 300 |  | 
 | 301 |  | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 302 |     def install_overrides(d): | 
 | 303 |         install_dir = d.getVar('D', True) | 
 | 304 |         install_dir += d.getVar('systemd_system_unitdir', True) | 
 | 305 |         searchpaths = d.getVar('FILESPATH', True) | 
 | 306 |  | 
 | 307 |         for spec in listvar_to_list(d, '_INSTALL_OVERRIDES'): | 
 | 308 |             tmpl, dest = spec.split(':') | 
 | 309 |             source = bb.utils.which(searchpaths, tmpl) | 
 | 310 |             if not os.path.isfile(source): | 
 | 311 |                 bb.fatal('Did not find SYSTEMD_OVERRIDE ' | 
 | 312 |                     'template: \'%s\'' % source) | 
 | 313 |  | 
 | 314 |             dest = os.path.join(install_dir, dest) | 
 | 315 |             parent = os.path.dirname(dest) | 
 | 316 |             if not os.path.exists(parent): | 
 | 317 |                 os.makedirs(parent) | 
 | 318 |  | 
 | 319 |             with open(source, 'r') as fd: | 
 | 320 |                 content = fd.read() | 
 | 321 |             with open('%s' % dest, 'w+') as fd: | 
 | 322 |                 fd.write(content) | 
 | 323 |  | 
 | 324 |  | 
| Brad Bishop | 0161aff | 2016-08-17 14:46:41 -0400 | [diff] [blame] | 325 |     install_links(d) | 
| Brad Bishop | afb2e1b | 2016-08-08 09:29:20 -0400 | [diff] [blame] | 326 |     install_envs(d) | 
| Brad Bishop | 9ca070c | 2016-08-18 23:59:44 -0400 | [diff] [blame] | 327 |     install_overrides(d) | 
| Brad Bishop | 522234c | 2016-08-17 15:42:31 -0400 | [diff] [blame] | 328 |     make_subs(d) | 
| Brad Bishop | 51528fe | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 329 | } | 
 | 330 |  | 
 | 331 |  | 
| Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 332 | do_install:append() { | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 333 |         # install systemd service/socket/template files | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 334 |         [ -z "${_INSTALL_SD_UNITS}" ] || \ | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 335 |                 install -d ${D}${systemd_system_unitdir} | 
| Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 336 |         for s in ${_INSTALL_SD_UNITS}; do | 
 | 337 |                 install -m 0644 ${WORKDIR}/$s \ | 
 | 338 |                         ${D}${systemd_system_unitdir}/$s | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 339 |                 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \ | 
 | 340 |                         -e 's,@BINDIR@,${bindir},g' \ | 
 | 341 |                         -e 's,@SBINDIR@,${sbindir},g' \ | 
| William A. Kennington III | 25e1057 | 2020-04-03 11:53:09 -0700 | [diff] [blame] | 342 |                         -e 's,@LIBEXECDIR@,${libexecdir},g' \ | 
 | 343 |                         -e 's,@LOCALSTATEDIR@,${localstatedir},g' \ | 
 | 344 |                         -e 's,@DATADIR@,${datadir},g' \ | 
| Brad Bishop | e36358c | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 345 |                         ${D}${systemd_system_unitdir}/$s | 
 | 346 |         done | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 347 | } |