| 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 | # | 
|  | 17 | # SYSTEMD_SUBSTITUTIONS_${unit} | 
|  | 18 | #    Variables in this list will be substituted in the specified unit | 
|  | 19 | #    file during install (if bitbake finds python {format} strings | 
|  | 20 | #    in the unit file itself).  List entries take the form: | 
|  | 21 | #      VAR:VALUE | 
|  | 22 | #    where {VAR} is the format string bitbake should look for in the | 
|  | 23 | #    unit 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 | 7aeda7b | 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 | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 60 | python() { | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 61 | def check_sd_unit(d, unit): | 
| Brad Bishop | bfef6ff | 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 | 7aeda7b | 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 | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 78 | set_append(d, 'SRC_URI', 'file://%s' % unit) | 
|  | 79 | set_append(d, 'FILES_%s' % pkg, '%s/%s' \ | 
|  | 80 | % (d.getVar('systemd_system_unitdir', True), unit)) | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 81 | set_append(d, '_INSTALL_SD_UNITS', unit) | 
|  | 82 |  | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 83 | for x in [ | 
|  | 84 | 'base_bindir', | 
|  | 85 | 'bindir', | 
|  | 86 | 'sbindir', | 
|  | 87 | 'SYSTEMD_DEFAULT_TARGET' ]: | 
|  | 88 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit, | 
|  | 89 | '%s:%s' % (x, d.getVar(x, True))) | 
|  | 90 |  | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 91 |  | 
|  | 92 | def add_sd_user(d, unit, pkg): | 
|  | 93 | opts = [ | 
|  | 94 | '--system', | 
|  | 95 | '--home', | 
|  | 96 | '/', | 
|  | 97 | '--no-create-home', | 
|  | 98 | '--shell /sbin/nologin', | 
|  | 99 | '--user-group'] | 
|  | 100 |  | 
| Brad Bishop | bcd1b65 | 2016-08-15 22:35:58 -0400 | [diff] [blame^] | 101 | var = 'SYSTEMD_USER_%s' % unit | 
|  | 102 | user = listvar_to_list(d, var) | 
|  | 103 | if len(user) is 0: | 
|  | 104 | var = 'SYSTEMD_USER_%s' % pkg | 
|  | 105 | user = listvar_to_list(d, var) | 
|  | 106 | if len(user) is not 0: | 
|  | 107 | if len(user) is not 1: | 
|  | 108 | bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user))) | 
|  | 109 |  | 
|  | 110 | user = user[0] | 
|  | 111 | set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit, | 
|  | 112 | 'USER:%s' % user) | 
|  | 113 | if user not in d.getVar('USERADD_PARAM_%s' % pkg, True): | 
|  | 114 | set_append( | 
|  | 115 | d, | 
|  | 116 | 'USERADD_PARAM_%s' % pkg, | 
|  | 117 | '%s' % (' '.join(opts + [user])), | 
|  | 118 | ';') | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 119 | if pkg not in d.getVar('USERADD_PACKAGES', True): | 
|  | 120 | set_append(d, 'USERADD_PACKAGES', pkg) | 
|  | 121 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 122 |  | 
| Brad Bishop | 8b87560 | 2016-07-11 00:42:58 -0400 | [diff] [blame] | 123 | pn = d.getVar('PN', True) | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 124 | if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None: | 
|  | 125 | d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn) | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 126 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 127 | for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'): | 
|  | 128 | for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg): | 
| Brad Bishop | 7aeda7b | 2016-07-11 13:05:26 -0400 | [diff] [blame] | 129 | check_sd_unit(d, unit) | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 130 | add_sd_unit(d, unit, pkg) | 
| Brad Bishop | 039c66b | 2016-07-14 19:50:19 -0400 | [diff] [blame] | 131 | add_sd_user(d, unit, pkg) | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 132 | } | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 133 |  | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 134 |  | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 135 | python systemd_do_postinst() { | 
|  | 136 | for unit in listvar_to_list(d, '_INSTALL_SD_UNITS'): | 
|  | 137 | subs = dict([ x.split(':') for x in | 
|  | 138 | listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit)]) | 
|  | 139 | if not subs: | 
|  | 140 | continue | 
|  | 141 |  | 
|  | 142 | path = d.getVar('D', True) | 
|  | 143 | path += d.getVar('systemd_system_unitdir', True) | 
|  | 144 | path += '/%s' % unit | 
|  | 145 | with open(path, 'r') as fd: | 
|  | 146 | content = fd.read() | 
|  | 147 | with open(path, 'w+') as fd: | 
|  | 148 | try: | 
|  | 149 | fd.write(content.format(**subs)) | 
|  | 150 | except KeyError as e: | 
|  | 151 | bb.fatal('No substitution found for %s in ' | 
|  | 152 | 'unit file \'%s\'' % (e, unit)) | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 |  | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 156 | do_install_append() { | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 157 | # install systemd service/socket/template files | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 158 | [ -z "${_INSTALL_SD_UNITS}" ] || \ | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 159 | install -d ${D}${systemd_system_unitdir} | 
| Brad Bishop | bfef6ff | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 160 | for s in ${_INSTALL_SD_UNITS}; do | 
|  | 161 | install -m 0644 ${WORKDIR}/$s \ | 
|  | 162 | ${D}${systemd_system_unitdir}/$s | 
| Brad Bishop | 1bb8be5 | 2016-06-08 22:03:59 -0400 | [diff] [blame] | 163 | sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \ | 
|  | 164 | -e 's,@BINDIR@,${bindir},g' \ | 
|  | 165 | -e 's,@SBINDIR@,${sbindir},g' \ | 
|  | 166 | ${D}${systemd_system_unitdir}/$s | 
|  | 167 | done | 
| Brad Bishop | 93fb535 | 2015-09-09 03:59:20 +0000 | [diff] [blame] | 168 | } | 
| Brad Bishop | b7e2a88 | 2016-07-14 19:34:06 -0400 | [diff] [blame] | 169 |  | 
|  | 170 |  | 
|  | 171 | do_install[postfuncs] += "systemd_do_postinst" |