added more options to set restart policy on services and ignore certain

processes
This commit is contained in:
Fredrik Eriksson 2019-04-09 20:56:55 +02:00
parent b60d5a4e5c
commit c16cdec7c4
No known key found for this signature in database
GPG Key ID: 8825C73A0FD1502A
2 changed files with 54 additions and 7 deletions

View File

@ -24,6 +24,8 @@ do_package_upgrade=yes
# restart services affected by previous upgrades # restart services affected by previous upgrades
do_service_restart=yes do_service_restart=yes
# what to do with identified services by default. See services section.
default_service_policy=ignore
# do depclean on Gentoo # do depclean on Gentoo
do_depclean=yes do_depclean=yes
@ -48,8 +50,15 @@ dev-db/postgresql=1
gitlab=1 gitlab=1
# The services section contains <process-name>=<service-name> # The processes section contains <process-name>=<service-name>
# The process name is whatever psutil returns and I haven't checked if it's # The process name is whatever psutil returns and I haven't checked if it's
# trustworthy, so it's probably not... Use with care. # trustworthy, so it's probably not... Use with care.
[services] # To ignore processes, just add them with empty values
[processes]
gitlab-workhorse=gitlab gitlab-workhorse=gitlab
qemu-system-x86_64=
# The services section contains restart policy for specific services.
# valid policies are 'ignore', 'warn' and 'restart'.
[services]
libvirtd=ignore

View File

@ -100,6 +100,8 @@ def restart_services():
retest_procs.add(proc) retest_procs.add(proc)
services = set() services = set()
processes = set()
services = {}
for proc in service_procs: for proc in service_procs:
try: try:
service_exe = proc.exe() service_exe = proc.exe()
@ -108,7 +110,15 @@ def restart_services():
log.debug('{} died before it could be restarted'.format(proc)) log.debug('{} died before it could be restarted'.format(proc))
continue continue
service_name = conf.get('services', proc_name, fallback=None) if proc_name in services:
# we have already checked a process with this name
continue
service_name = conf.get('processes', proc_name, fallback=False)
if service_name == None:
log.debug('Ignoring process {}'.format(proc))
continue
if not service_name: if not service_name:
# if the exe file has been deleted since started, service_exe will be empty # if the exe file has been deleted since started, service_exe will be empty
# and we'll have to guess # and we'll have to guess
@ -125,23 +135,51 @@ def restart_services():
log.warning('Could not find service for process {}'.format(proc)) log.warning('Could not find service for process {}'.format(proc))
continue continue
if service_name: services[proc_name] = service_name
services.add(service_name)
for service in [x for x in services.values() if x]:
policy = _get_service_restart_policy(service)
if policy == 'ignore':
log.info('Service "{}" ignored by configuration'.format(service))
continue
elif policy == 'warn':
log.warning('Service "{}" has open deleted files and should be restarted'.format(service))
continue
for service in services:
log.info('Restarting service {}'.format(service)) log.info('Restarting service {}'.format(service))
platform.restart_service(service) platform.restart_service(service)
recommend_restart = False recommend_restart = False
for proc in retest_procs: for proc in retest_procs:
try: try:
exe = proc.exe()
name = proc.name() name = proc.name()
except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied): except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied):
log.debug('{} was successfully killed'.format(proc)) log.debug('{} was successfully killed'.format(proc))
continue continue
if _get_deleted_open_files(proc): if _get_deleted_open_files(proc):
if name in services and not services[name]:
log.warning('{} does not belong to a service and could not be restarted'.format(proc))
recommend_restart = True
continue
elif name in services:
policy = _get_service_restart_policy(services[name])
if policy == 'ignore':
continue
log.warning('{} still has deleted files open'.format(proc)) log.warning('{} still has deleted files open'.format(proc))
recommend_restart = True recommend_restart = True
return recommend_restart return recommend_restart
def _get_service_restart_policy(service):
conf = sau.config
policy = conf.get('services', service, fallback=None)
if policy and policy.lower() in ('restart', 'warn', 'ignore'):
return policy.lower()
elif policy:
log.warning('service policy {} for {} is invalid'.format(policy, service))
default_policy = conf.get('services', 'default_service_policy', fallback='warn')
if default_policy.lower() in ('restart', 'warn', 'ignore'):
return default_policy.lower()
log.warning('default service policy {} is invalid'.format(default_policy))
return 'warn'