blob: 46e72c7a4a894b298ed694efac09c05a56d41170 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# The list of packages that should have systemd packaging scripts added. For
2# each entry, optionally have a SYSTEMD_SERVICE_[package] that lists the service
3# files in this package. If this variable isn't set, [package].service is used.
4SYSTEMD_PACKAGES ?= "${PN}"
5SYSTEMD_PACKAGES_class-native ?= ""
6SYSTEMD_PACKAGES_class-nativesdk ?= ""
7
8# Whether to enable or disable the services on installation.
9SYSTEMD_AUTO_ENABLE ??= "enable"
10
11# This class will be included in any recipe that supports systemd init scripts,
12# even if systemd is not in DISTRO_FEATURES. As such don't make any changes
13# directly but check the DISTRO_FEATURES first.
14python __anonymous() {
15 # If the distro features have systemd but not sysvinit, inhibit update-rcd
16 # from doing any work so that pure-systemd images don't have redundant init
17 # files.
18 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
19 d.appendVar("DEPENDS", " systemd-systemctl-native")
20 if not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d):
21 d.setVar("INHIBIT_UPDATERCD_BBCLASS", "1")
22}
23
24systemd_postinst() {
25OPTS=""
26
27if [ -n "$D" ]; then
28 OPTS="--root=$D"
29fi
30
31if type systemctl >/dev/null 2>/dev/null; then
32 systemctl $OPTS ${SYSTEMD_AUTO_ENABLE} ${SYSTEMD_SERVICE}
33
34 if [ -z "$D" -a "${SYSTEMD_AUTO_ENABLE}" = "enable" ]; then
35 systemctl restart ${SYSTEMD_SERVICE}
36 fi
37fi
38}
39
40systemd_prerm() {
41OPTS=""
42
43if [ -n "$D" ]; then
44 OPTS="--root=$D"
45fi
46
47if type systemctl >/dev/null 2>/dev/null; then
48 if [ -z "$D" ]; then
49 systemctl stop ${SYSTEMD_SERVICE}
50 fi
51
52 systemctl $OPTS disable ${SYSTEMD_SERVICE}
53fi
54}
55
56
57systemd_populate_packages[vardeps] += "systemd_prerm systemd_postinst"
58systemd_populate_packages[vardepsexclude] += "OVERRIDES"
59
60
61python systemd_populate_packages() {
62 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
63 return
64
65 def get_package_var(d, var, pkg):
66 val = (d.getVar('%s_%s' % (var, pkg), True) or "").strip()
67 if val == "":
68 val = (d.getVar(var, True) or "").strip()
69 return val
70
71 # Check if systemd-packages already included in PACKAGES
72 def systemd_check_package(pkg_systemd):
73 packages = d.getVar('PACKAGES', True)
74 if not pkg_systemd in packages.split():
75 bb.error('%s does not appear in package list, please add it' % pkg_systemd)
76
77
78 def systemd_generate_package_scripts(pkg):
79 bb.debug(1, 'adding systemd calls to postinst/postrm for %s' % pkg)
80
81 # Add pkg to the overrides so that it finds the SYSTEMD_SERVICE_pkg
82 # variable.
83 localdata = d.createCopy()
84 localdata.prependVar("OVERRIDES", pkg + ":")
85 bb.data.update_data(localdata)
86
87 postinst = d.getVar('pkg_postinst_%s' % pkg, True)
88 if not postinst:
89 postinst = '#!/bin/sh\n'
90 postinst += localdata.getVar('systemd_postinst', True)
91 d.setVar('pkg_postinst_%s' % pkg, postinst)
92
93 prerm = d.getVar('pkg_prerm_%s' % pkg, True)
94 if not prerm:
95 prerm = '#!/bin/sh\n'
96 prerm += localdata.getVar('systemd_prerm', True)
97 d.setVar('pkg_prerm_%s' % pkg, prerm)
98
99
100 # Add files to FILES_*-systemd if existent and not already done
101 def systemd_append_file(pkg_systemd, file_append):
102 appended = False
103 if os.path.exists(oe.path.join(d.getVar("D", True), file_append)):
104 var_name = "FILES_" + pkg_systemd
105 files = d.getVar(var_name, False) or ""
106 if file_append not in files.split():
107 d.appendVar(var_name, " " + file_append)
108 appended = True
109 return appended
110
111 # Add systemd files to FILES_*-systemd, parse for Also= and follow recursive
112 def systemd_add_files_and_parse(pkg_systemd, path, service, keys):
113 # avoid infinite recursion
114 if systemd_append_file(pkg_systemd, oe.path.join(path, service)):
115 fullpath = oe.path.join(d.getVar("D", True), path, service)
116 if service.find('.service') != -1:
117 # for *.service add *@.service
118 service_base = service.replace('.service', '')
119 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
120 if service.find('.socket') != -1:
121 # for *.socket add *.service and *@.service
122 service_base = service.replace('.socket', '')
123 systemd_add_files_and_parse(pkg_systemd, path, service_base + '.service', keys)
124 systemd_add_files_and_parse(pkg_systemd, path, service_base + '@.service', keys)
125 for key in keys.split():
126 # recurse all dependencies found in keys ('Also';'Conflicts';..) and add to files
127 cmd = "grep %s %s | sed 's,%s=,,g' | tr ',' '\\n'" % (key, fullpath, key)
128 pipe = os.popen(cmd, 'r')
129 line = pipe.readline()
130 while line:
131 line = line.replace('\n', '')
132 systemd_add_files_and_parse(pkg_systemd, path, line, keys)
133 line = pipe.readline()
134 pipe.close()
135
136 # Check service-files and call systemd_add_files_and_parse for each entry
137 def systemd_check_services():
138 searchpaths = [oe.path.join(d.getVar("sysconfdir", True), "systemd", "system"),]
139 searchpaths.append(d.getVar("systemd_system_unitdir", True))
140 systemd_packages = d.getVar('SYSTEMD_PACKAGES', True)
141
142 keys = 'Also'
143 # scan for all in SYSTEMD_SERVICE[]
144 for pkg_systemd in systemd_packages.split():
145 for service in get_package_var(d, 'SYSTEMD_SERVICE', pkg_systemd).split():
146 path_found = ''
147 for path in searchpaths:
148 if os.path.exists(oe.path.join(d.getVar("D", True), path, service)):
149 path_found = path
150 break
151 if path_found != '':
152 systemd_add_files_and_parse(pkg_systemd, path_found, service, keys)
153 else:
154 raise bb.build.FuncFailed("SYSTEMD_SERVICE_%s value %s does not exist" % \
155 (pkg_systemd, service))
156
157 # Run all modifications once when creating package
158 if os.path.exists(d.getVar("D", True)):
159 for pkg in d.getVar('SYSTEMD_PACKAGES', True).split():
160 systemd_check_package(pkg)
161 if d.getVar('SYSTEMD_SERVICE_' + pkg, True):
162 systemd_generate_package_scripts(pkg)
163 systemd_check_services()
164}
165
166PACKAGESPLITFUNCS_prepend = "systemd_populate_packages "
167
168python rm_systemd_unitdir (){
169 import shutil
170 if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
171 systemd_unitdir = oe.path.join(d.getVar("D", True), d.getVar('systemd_unitdir', True))
172 if os.path.exists(systemd_unitdir):
173 shutil.rmtree(systemd_unitdir)
174 systemd_libdir = os.path.dirname(systemd_unitdir)
175 if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
176 os.rmdir(systemd_libdir)
177}
178do_install[postfuncs] += "rm_systemd_unitdir "
179
180python rm_sysvinit_initddir (){
181 import shutil
182 sysv_initddir = oe.path.join(d.getVar("D", True), (d.getVar('INIT_D_DIR', True) or "/etc/init.d"))
183
184 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
185 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
186 os.path.exists(sysv_initddir):
187 systemd_system_unitdir = oe.path.join(d.getVar("D", True), d.getVar('systemd_system_unitdir', True))
188
189 # If systemd_system_unitdir contains anything, delete sysv_initddir
190 if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
191 shutil.rmtree(sysv_initddir)
192}
193do_install[postfuncs] += "rm_sysvinit_initddir "