Codebase list debian-goodies / aed5839
Preliminary support for systemd. Checkrestart now looks for service files and, if the system is running systemd, it will suggest to use systemctl instead of 'service' to restart those packages that require it. Javier Fernandez-Sanguino 9 years ago
1 changed file(s) with 45 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
5858 return location
5959 return 1
6060
61 def checksystemd():
62 # Check if systemd is installed in the system
63 if os.path.exists('/bin/systemd'):
64 return 0
65 return 1
66
6167 def usage():
6268 sys.stderr.write('usage: checkrestart [-vhpa] [-bblacklist] [-iignore]\n')
6369
6975 lc_all_c_env = os.environ
7076 lc_all_c_env['LC_ALL'] = 'C'
7177 file_query_check = {}
78 is_systemd = False
7279 blacklistFiles = []
7380 blacklist = []
74 ignorelist = [ 'screen' ]
81 ignorelist = [ 'screen', 'systemd' ]
7582
7683 # Process options
7784 try:
119126
120127 # Start checking
121128
129 if checksystemd() == 0:
130 is_systemd = True
131
122132 if find_cmd('lsof') == 1:
123133 sys.stderr.write('ERROR: This program needs lsof in order to run.\n')
124134 sys.stderr.write('Please install the lsof package in your system.\n')
147157 else:
148158 print "(%d distinct programs)" % len(programs)
149159
150 # Verbose information
160 #services Verbose information
151161 if verbose:
152162 for process in toRestart:
153163 print "Process %s (PID: %d) " % (process.program, process.pid)
224234 if path.endswith('.sh'):
225235 continue
226236 package.initscripts.add(path[12:])
237 # If running on a systemd system, extract the systemd's service files from the package
238 if is_systemd and path.startswith('/lib/systemd/system/') and path.endswith('.service') and path.find('.wants') == -1:
239 # Read the service file and make sure it is not of type 'oneshot'
240 servicefile = open (path)
241 is_oneshot = False
242 for line in servicefile.readlines ():
243 if line.find ('Type=oneshot') > 0:
244 is_oneshot = True
245 continue
246 servicefile.close ()
247 if not is_oneshot:
248 package.systemdservice.add(path[20:])
227249 sys.stdout.flush()
228250 dpkgProc.stdout.close()
229251
230252 # Alternatively, find init.d scripts that match the process name
231 if len(package.initscripts) == 0:
253 if len(package.initscripts) == 0 and len(package.systemdservice) == 0:
232254 for process in package.processes:
233255 proc_name = os.path.basename(process.program)
234256 if os.path.exists('/etc/init.d/' + proc_name):
236258
237259 restartable = []
238260 nonrestartable = []
239 restartCommands = []
261 restartInitCommands = []
262 restartServiceCommands = []
240263 for package in packages.values():
241264 if len(package.initscripts) > 0:
242265 restartable.append(package)
243 restartCommands.extend(map(lambda s: 'service ' + s + ' restart',package.initscripts))
266 restartInitCommands.extend(map(lambda s: 'service ' + s + ' restart', package.initscripts))
267 elif len(package.systemdservice) > 0:
268 restartable.append(package)
269 restartServiceCommands.extend(map(lambda s: 'systemctl restart ' + s, package.systemdservice))
244270 else:
245271 nonrestartable.append(package)
246272
247273 if len(restartable) > 0:
248274 print
249 print "Of these, %d seem to contain init scripts which can be used to restart them:" % len(restartable)
275 print "Of these, %d seem to contain systemd service definitions or init scripts which can be used to restart them." % len(restartable)
250276 # TODO - consider putting this in a --verbose option
251 print "The following packages seem to have init scripts that could be used\nto restart them:"
277 print "The following packages seem to have definitions that could be used\nto restart their services:"
252278 for package in restartable:
253279 print package.name + ':'
254280 for process in package.processes:
255281 print "\t%s\t%s" % (process.pid,process.program)
256282
257 print
258 print "These are the init scripts:"
259 print '\n'.join(restartCommands)
260 print
283 if len(restartServiceCommands)>0:
284 print
285 print "These are the systemd services:"
286 print '\n'.join(restartServiceCommands)
287 print
288
289 if len(restartInitCommands)>0:
290 print "These are the initd scripts:"
291 print '\n'.join(restartInitCommands)
292 print
261293
262294 if len(nonrestartable) == 0:
263295 sys.exit(0)
264296
265297 # TODO - consider putting this in a --verbose option
266 print "These processes do not seem to have an associated init script to restart them:"
298 print "These processes (%d) do not seem to have an associated init script to restart them:" %len(nonrestartable)
267299 for package in nonrestartable:
268300 skip = False
269301 if ignorelist:
633665 self.name = name
634666 # use a set, we don't need duplicates
635667 self.initscripts = set()
668 self.systemdservice = set()
636669 self.processes = []
637670
638671 if __name__ == '__main__':