blob: a0a31571073fa22336f8e75526dbf914a3fd9f89 [file] [log] [blame]
Brad Bishope36358c2016-06-08 22:03:59 -04001# 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 Bishop62d676a2016-07-11 00:42:58 -040010# ${PN}.service will be added to the main package.
Brad Bishop687146f2016-07-11 13:05:26 -040011#
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 Bishop51528fe2016-07-14 19:34:06 -040016#
Brad Bishop5e329ba2016-08-17 12:08:16 -040017# SYSTEMD_SUBSTITUTIONS_${path-relative-to-system_unitdir}
18# Variables in this list will be substituted in the specified
Brad Bishop51528fe2016-07-14 19:34:06 -040019# file during install (if bitbake finds python {format} strings
Brad Bishop5e329ba2016-08-17 12:08:16 -040020# in the file itself). List entries take the form:
Brad Bishop51528fe2016-07-14 19:34:06 -040021# VAR:VALUE
22# where {VAR} is the format string bitbake should look for in the
Brad Bishop5e329ba2016-08-17 12:08:16 -040023# file and VALUE is the value to substitute.
Brad Bishopf815b2e2016-07-14 19:50:19 -040024#
Brad Bishop58700d12016-08-15 22:35:58 -040025# SYSTEMD_USER_${PN}.service = "foo"
26# SYSTEMD_USER_${unit}.service = "foo"
27# The user for the unit/package.
Brad Bishop51528fe2016-07-14 19:34:06 -040028
Brad Bishop9dc56712016-07-07 15:56:02 -040029
30inherit obmc-phosphor-utils
Brad Bishop93fb5352015-09-09 03:59:20 +000031inherit systemd
Brad Bishopf815b2e2016-07-14 19:50:19 -040032inherit useradd
Brad Bishop93fb5352015-09-09 03:59:20 +000033
Brad Bishop9dc56712016-07-07 15:56:02 -040034_INSTALL_SD_UNITS=""
Brad Bishop51528fe2016-07-14 19:34:06 -040035SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
Brad Bishop9dc56712016-07-07 15:56:02 -040036
Brad Bishopf815b2e2016-07-14 19:50:19 -040037# 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...
40USERADD_PACKAGES ?= " "
41USERADD_PARAM_${PN} ?= ";"
42
Brad Bishope36358c2016-06-08 22:03:59 -040043
Brad Bishop687146f2016-07-11 13:05:26 -040044def systemd_is_service(unit):
45 return unit.endswith('.service')
46
47
48def systemd_is_template(unit):
49 return '@.' in unit
50
51
52def 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 Bishope36358c2016-06-08 22:03:59 -040060python() {
Brad Bishop687146f2016-07-11 13:05:26 -040061 def check_sd_unit(d, unit):
Brad Bishop9dc56712016-07-07 15:56:02 -040062 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 Bishop687146f2016-07-11 13:05:26 -040066
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 Bishop5e329ba2016-08-17 12:08:16 -040078 unit_dir = d.getVar('systemd_system_unitdir', True)
Brad Bishop9dc56712016-07-07 15:56:02 -040079 set_append(d, 'SRC_URI', 'file://%s' % unit)
Brad Bishop5e329ba2016-08-17 12:08:16 -040080 set_append(d, 'FILES_%s' % pkg, '%s/%s' % (unit_dir, unit))
Brad Bishop687146f2016-07-11 13:05:26 -040081 set_append(d, '_INSTALL_SD_UNITS', unit)
Brad Bishop5e329ba2016-08-17 12:08:16 -040082 set_append(d, '_MAKE_SUBS', '%s' % unit)
Brad Bishop687146f2016-07-11 13:05:26 -040083
Brad Bishop51528fe2016-07-14 19:34:06 -040084 for x in [
85 'base_bindir',
86 'bindir',
87 'sbindir',
88 'SYSTEMD_DEFAULT_TARGET' ]:
89 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
90 '%s:%s' % (x, d.getVar(x, True)))
91
Brad Bishopf815b2e2016-07-14 19:50:19 -040092
93 def add_sd_user(d, unit, pkg):
94 opts = [
95 '--system',
96 '--home',
97 '/',
98 '--no-create-home',
99 '--shell /sbin/nologin',
100 '--user-group']
101
Brad Bishop58700d12016-08-15 22:35:58 -0400102 var = 'SYSTEMD_USER_%s' % unit
103 user = listvar_to_list(d, var)
104 if len(user) is 0:
105 var = 'SYSTEMD_USER_%s' % pkg
106 user = listvar_to_list(d, var)
107 if len(user) is not 0:
108 if len(user) is not 1:
109 bb.fatal('Too many users assigned to %s: \'%s\'' % (var, ' '.join(user)))
110
111 user = user[0]
112 set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % unit,
113 'USER:%s' % user)
114 if user not in d.getVar('USERADD_PARAM_%s' % pkg, True):
115 set_append(
116 d,
117 'USERADD_PARAM_%s' % pkg,
118 '%s' % (' '.join(opts + [user])),
119 ';')
Brad Bishopf815b2e2016-07-14 19:50:19 -0400120 if pkg not in d.getVar('USERADD_PACKAGES', True):
121 set_append(d, 'USERADD_PACKAGES', pkg)
122
Brad Bishop9dc56712016-07-07 15:56:02 -0400123
Brad Bishop62d676a2016-07-11 00:42:58 -0400124 pn = d.getVar('PN', True)
Brad Bishop9dc56712016-07-07 15:56:02 -0400125 if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
126 d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
Brad Bishope36358c2016-06-08 22:03:59 -0400127
Brad Bishop9dc56712016-07-07 15:56:02 -0400128 for pkg in listvar_to_list(d, 'SYSTEMD_PACKAGES'):
129 for unit in listvar_to_list(d, 'SYSTEMD_SERVICE_%s' % pkg):
Brad Bishop687146f2016-07-11 13:05:26 -0400130 check_sd_unit(d, unit)
Brad Bishop9dc56712016-07-07 15:56:02 -0400131 add_sd_unit(d, unit, pkg)
Brad Bishopf815b2e2016-07-14 19:50:19 -0400132 add_sd_user(d, unit, pkg)
Brad Bishope36358c2016-06-08 22:03:59 -0400133}
Brad Bishop93fb5352015-09-09 03:59:20 +0000134
Brad Bishop9dc56712016-07-07 15:56:02 -0400135
Brad Bishop51528fe2016-07-14 19:34:06 -0400136python systemd_do_postinst() {
Brad Bishop5e329ba2016-08-17 12:08:16 -0400137 for f in listvar_to_list(d, '_MAKE_SUBS'):
Brad Bishop51528fe2016-07-14 19:34:06 -0400138 subs = dict([ x.split(':') for x in
Brad Bishop5e329ba2016-08-17 12:08:16 -0400139 listvar_to_list(d, 'SYSTEMD_SUBSTITUTIONS_%s' % f)])
Brad Bishop51528fe2016-07-14 19:34:06 -0400140 if not subs:
141 continue
142
143 path = d.getVar('D', True)
144 path += d.getVar('systemd_system_unitdir', True)
Brad Bishop5e329ba2016-08-17 12:08:16 -0400145 path += '/%s' % f
Brad Bishop51528fe2016-07-14 19:34:06 -0400146 with open(path, 'r') as fd:
147 content = fd.read()
148 with open(path, 'w+') as fd:
149 try:
150 fd.write(content.format(**subs))
151 except KeyError as e:
152 bb.fatal('No substitution found for %s in '
Brad Bishop5e329ba2016-08-17 12:08:16 -0400153 'file \'%s\'' % (e, f))
Brad Bishop51528fe2016-07-14 19:34:06 -0400154}
155
156
Brad Bishop93fb5352015-09-09 03:59:20 +0000157do_install_append() {
Brad Bishope36358c2016-06-08 22:03:59 -0400158 # install systemd service/socket/template files
Brad Bishop9dc56712016-07-07 15:56:02 -0400159 [ -z "${_INSTALL_SD_UNITS}" ] || \
Brad Bishope36358c2016-06-08 22:03:59 -0400160 install -d ${D}${systemd_system_unitdir}
Brad Bishop9dc56712016-07-07 15:56:02 -0400161 for s in ${_INSTALL_SD_UNITS}; do
162 install -m 0644 ${WORKDIR}/$s \
163 ${D}${systemd_system_unitdir}/$s
Brad Bishope36358c2016-06-08 22:03:59 -0400164 sed -i -e 's,@BASE_BINDIR@,${base_bindir},g' \
165 -e 's,@BINDIR@,${bindir},g' \
166 -e 's,@SBINDIR@,${sbindir},g' \
167 ${D}${systemd_system_unitdir}/$s
168 done
Brad Bishop93fb5352015-09-09 03:59:20 +0000169}
Brad Bishop51528fe2016-07-14 19:34:06 -0400170
171
172do_install[postfuncs] += "systemd_do_postinst"