Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

8 changed files with 100 additions and 423 deletions

37
bin/sau
View File

@ -8,7 +8,6 @@ import sys
import time
import sau
import sau.errors
import sau.services
import sau.platforms
@ -45,23 +44,12 @@ def init():
log.addHandler(handler)
def fork_and_reboot(report_success=True):
def fork_and_reboot():
log = logging.getLogger(sau.LOGNAME)
if report_success:
exit_code=0
else:
exit_code=1
if os.path.exists('/proc/1/comm'):
with open('/proc/1/comm', 'r') as f:
if f.readline().strip() == 'systemd':
os.execl('/usr/bin/systemctl', 'reboot')
log.error("Failed to execl?")
sys.exit(1)
try:
pid = os.fork()
if pid != 0:
sys.exit(exit_code)
sys.exit(0)
except OSError as err:
log.error("Fork #1 failed when going for reboot: {}".format(err))
sys.exit(1)
@ -88,6 +76,8 @@ def fork_and_reboot(report_success=True):
os.dup2(stdout.fileno(), sys.stdout.fileno())
os.dup2(stderr.fileno(), sys.stderr.fileno())
log.warning("New fork!")
# sleep for a short while to give parent time to exit
time.sleep(30)
try:
@ -105,18 +95,11 @@ def main():
reboot_required = False
reboot_recommended = False
try:
if conf.getboolean('default', 'do_system_upgrade', fallback=True):
reboot_required = platform.system_upgrade()
if conf.getboolean('default', 'do_package_upgrade', fallback=True):
reboot_required = reboot_required or platform.pkg_upgrade()
except sau.errors.UpgradeError as e:
log.error(f'Upgrade failed: {e}')
return 1
if not conf.getboolean('default', 'live_system', fallback=True):
return 0
if conf.getboolean('default', 'do_service_restart', fallback=True):
reboot_recommended = sau.services.restart_services()
@ -127,17 +110,7 @@ def main():
elif reboot_recommended:
log.info('Rebooting because service restarts did not close all deleted files')
if reboot_required or reboot_recommended:
fork_and_reboot(report_success=conf.getboolean('default', 'reboot_is_success', fallback=True))
if reboot_required:
log.warning("Upgrade was success, but a reboot is required due to a system upgrade")
return 1
elif reboot_recommended:
log.warning("Some services still uses old, deleted, files. You probably want to reboot")
return 1
return 0
fork_and_reboot()
def _conf_level_to_logging_level(conf_level):
if conf_level.lower() == 'debug':

View File

@ -12,25 +12,13 @@
# 1.0.1 -> 1.0.1.1 (3)
version_sensitivity=1
# Set to no if you're using sau in an environment where running processes
# shouldn't be touched and reboots shouldn't be done, for example in chroots
live_system=yes
# sau can reboot on system upgrades (FreeBSD) or if the service restarts does
# not close all deleted files (any platform)
do_reboot=no
# Set to no to exit with failure code when going for reboot.
reboot_is_success=yes
# Attempt to do a system upgrade
# FreeBSD: upgrade to latest patch version using freebsd-update fetch install
# Gentoo: allow upgrade of sys-kernel/-packages, clean old kernels, and update grub-config
# FreeBSD system update (freebsd-update fetch install, not freebsd-update upgrade)
do_system_upgrade=yes
# On Gentoo kernel upgrades, remove all but the last keep_kernels kernels from /boot
keep_kernels=4
# upgrade packages
do_package_upgrade=yes
@ -45,11 +33,6 @@ do_depclean=yes
# do eix-sync on Gentoo
do_reposync=yes
# do live-rebuild, go-rebuild, rust-rebuild, perl-cleaner etc. on Gentoo
# set to no if using binary packages that are bumped when needed.
# Leave as yes on package builders and if not using binary packages.
do_rebuilds=yes
# to only write to stderr when something unexpected happens or manual action is required
# set stderr_loglevel to warning
stderr_loglevel=debug
@ -63,7 +46,9 @@ syslog_loglevel=info
# Gentoo uses the category/package naming scheme
dev-db/postgresql=1
# It's safer to upgrade zfs manually
# Gentoo kernel stuff should be updated manually
sys-kernel/gentoo-sources=99
sys-kernel/spl=99
sys-fs/zfs-kmod=99
# FreeBSD uses the short package name (without category)

View File

@ -1 +1,2 @@
LOGNAME="sau"

View File

@ -5,5 +5,3 @@ class PlatformNotSupported(Exception):
class UnknownServiceError(Exception):
pass
class UpgradeError(Exception):
pass

View File

@ -132,14 +132,5 @@ def pkg_upgrade():
for line in err.splitlines():
log.warning('stderr: {}'.format(line))
if conf.getboolean('default', 'do_depclean', fallback=False):
cmd = [ PKG_PATH, 'autoremove', '-yq' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600)
if ret != 0 or err:
log.warning('{} failed:'.format(' '.join(cmd)))
for line in out.splitlines():
log.warning('stdout: {}'.format(line))
for line in err.splitlines():
log.warning('stderr: {}'.format(line))
return True

View File

@ -4,28 +4,21 @@ import re
import sau
import sau.helpers
import sau.services
EIX_SYNC_PATH='/usr/bin/eix-sync'
RC_SERVICE_PATH='/sbin/rc-service'
SYSTEMCTL='/usr/bin/systemctl'
EMERGE_PATH='/usr/bin/emerge'
EQUERY_PATH='/usr/bin/equery'
EMAINT_PATH='/usr/sbin/emaint'
PCLEAN_PATH='/usr/bin/perl-cleaner'
GRUB_MKCONFIG='/usr/sbin/grub-mkconfig'
# parsing output from eix -Ttnc
package_re = re.compile(r'^\[([^\]])\] ([^ ]*) \((.*)\): .*$')
package_re = re.compile('^\[([^\]])\] ([^ ]*) \((.*)\): .*$')
# parsing version information from substrings of the above
slot_re = re.compile(r'^(\(~\))?([^\(]+)(\([^\)]+\))$')
slot_re = re.compile('^(\(~\))?([^\(]+)(\([^\)]+\))$')
def identify_service_from_bin(exe):
log = logging.getLogger(sau.LOGNAME)
if sau.services.on_systemd():
init_script_re = re.compile(r'[^/]*(.*)\.service$')
else:
init_script_re = re.compile(r'/etc/init\.d/(.*)')
cmd = [ EQUERY_PATH, '-Cq', 'b', exe ]
@ -61,9 +54,6 @@ def identify_service_from_bin(exe):
def restart_service(service):
log = logging.getLogger(sau.LOGNAME)
if sau.services.on_systemd():
cmd = [ SYSTEMCTL, 'restart', service ]
else:
cmd = [ RC_SERVICE_PATH, service, 'restart' ]
ret, out, err = sau.helpers.exec_cmd(cmd)
@ -92,89 +82,12 @@ def _sync_portage():
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600)
if ret != 0:
log.error("Portage sync failed:")
for line in out.splitlines():
log.error("stdout: {}".format(line))
for line in err.splitlines():
log.error("stderr: {}".format(line))
raise sau.errors.UpgradeError(f'Sync command {cmd} failed')
cmd = [ EMAINT_PATH, '-f', 'all' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600)
if ret != 0:
log.warning("emaint failed:")
log.warning("Portage sync failed:")
for line in out.splitlines():
log.warning("stdout: {}".format(line))
for line in err.splitlines():
log.warning("stderr: {}".format(line))
def is_system_package(atom, eclasses):
log = logging.getLogger(sau.LOGNAME)
name=re.sub(r'^[<=>]*(.*?)(?:-\d)?(?:::\w+)?$', r'\1', atom)
# sys-boot/ category should probably always be considered
# system-packages
if name.split('/')[0] == 'sys-boot':
log.debug(f"{name} is a sys-boot package")
return True
if eclasses is True:
return True
# libc-packages should be considered system-packages as they generally
# requires the system to be restarted. Not sure if there is a better way
# then just checking for specific packages here, but as far as I know there
# are not many of them anyway...
if re.search(r'^sys-libs/(glibc|musl)', name):
log.debug(f"{name} is a libc package")
return True
if any([
x in eclasses for x in [
'dist-kernel-utils',
'linux-mod',
'kernel-install' ]
]):
log.debug(f"{name} is of system eclass (eclasses: {eclasses})")
return True
return False
def get_eclasses(atom):
log = logging.getLogger(sau.LOGNAME)
eclasses = []
name=re.sub(r'^[<=>]*(.*?)(?:-\d+)?(?:::\w+)?$', r'\1', atom)
test_re = re.compile(r'^\s*inherit\s+')
cmd=[ EQUERY_PATH, 'w', name ]
ret, out, err = sau.helpers.exec_cmd(cmd)
if not ret == 0:
log.warning(f'Unable to locate ebuild for {atom}')
# better safe than sorry; if we don't know, let's pretend it's a system
# package
return True
path = out.strip()
if not os.path.isfile(path):
log.warning(f"This path doesn't look lika a path to the ebuild for {name}: {path}")
return True
with open(path, 'r', encoding='utf-8') as f:
for line in f.readlines():
if eclasses and eclasses[-1] == '\\':
eclasses = eclasses[:-1]
eclasses.extend(line.split())
if re.search(test_re, line):
if re.search(test_re, line):
eclasses.extend(line.split()[1:])
# Remove revisions from eclasses, hopefully makes it less messy if they get
# updated
eclasses = [re.sub(r'^(.*?)-r\d+', r'\1', x) for x in eclasses]
return eclasses
def get_dependencies(atom):
cmd=[ EQUERY_PATH, '-q', 'd', '-F', '$cp', atom ]
ret, out, err = sau.helpers.exec_cmd(cmd)
dependencies = [l.strip() for l in out.splitlines()]
return dependencies
def pkg_upgrade():
@ -191,7 +104,6 @@ def pkg_upgrade():
default_version_sens = conf.getint('default', 'version_sensitivity', fallback=1)
## Query upgradeable packages
cmd = [ EMERGE_PATH, '--color', 'n', '-uDNpq', '--with-bdeps=y', '@world' ]
ret, out, err = sau.helpers.exec_cmd(cmd)
@ -201,11 +113,10 @@ def pkg_upgrade():
log.error('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Failed to calculate upgrade path')
return False
do_rebuild = True
do_grub = False
rebuild_packages = {}
for line in out.splitlines():
if re.match(ignore_re, line):
continue
@ -226,201 +137,71 @@ def pkg_upgrade():
if sens <= common:
log.info('{} -- {} -> {} configured level {} <= pkg level {}'.format(name, old, new, sens, common))
else:
log.error('{} -- {} -> {} configured level {} > pkg level {}'.format(name, old, new, sens, common))
log.warning('{} -- {} -> {} configured level {} > pkg level {}'.format(name, old, new, sens, common))
do_rebuild = False
nameversion = f'{name}-{new}'
eclasses = get_eclasses(nameversion)
rebuild_packages[name] = eclasses
for package,eclasses in rebuild_packages.items():
if is_system_package(package, eclasses):
if name.startswith('sys-kernel/'):
if do_system_upgrade:
do_grub = True
else:
raise sau.errors.UpgradeError(f"System package {package} has an update, but system upgrade is disabled")
log.warning(f"Kernel package {name} has an update, but system upgrade is disabled")
do_rebuild = False
if not do_rebuild:
raise sau.errors.UpgradeError('Some packages require manual attention, did not upgrade')
if not rebuild_packages:
log.info('No packages to upgrade')
log.warning('Some packages require manual attention, did not upgrade')
return False
## Actual upgrade
cmd = [ EMERGE_PATH, '--color', 'n', '-uDNq', '--with-bdeps=y', '@world' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000)
if ret != 0 or err:
log.error('emerge returned {}'.format(ret))
log.warning('emerge returned {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
log.warning('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during upgrade')
log.warning('stderr: {}'.format(line))
else:
log.info('upgrade complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
## rebuild as needed
do_rebuild = conf.getboolean('default', 'do_rebuilds', fallback=True)
if do_rebuild:
# from here on we shouldn't need to rebuild the upgraded packages again
exclude_list = ' --exclude '.join(rebuild_packages.keys()).split()
# Rebuild go
go_packages = []
cmd = None
for package,eclasses in rebuild_packages.items():
if 'go-module' in eclasses or package == 'dev-lang/go':
go_packages.append(package)
if 'dev-lang/go' in go_packages:
log.info("Running golang-rebuild due to update of dev-lang/go")
cmd = [
EMERGE_PATH,
'--color', 'n',
'-q',
'--usepkg', 'n',
'@golang-rebuild',
'--exclude' ] + exclude_list
elif go_packages:
dependencies = []
for package in go_packages:
dependencies.extend(get_dependencies(package))
dependencies = set(dependencies)
upgraded = set(rebuild_packages.keys())
not_upgraded = dependencies-upgraded
if not_upgraded:
log.info(f'Rebuilding packages dependant of go modules {", ".join(go_packages)}')
cmd = [
EMERGE_PATH,
'--color', 'n',
'-q',
'--usepkg', 'n'] + not_upgraded
if cmd:
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000)
if ret != 0 or err:
log.error('Rebuild of go packages returned {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during go rebuild')
else:
log.info('go rebuild complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
# rebuild rust
if any([x in rebuild_packages for x in ('dev-lang/rust', 'dev-lang/rust-bin')]):
log.info("Running rust-rebuild due to update of rust")
cmd = [
EMERGE_PATH,
'--color', 'n',
'-q',
'--usepkg', 'n',
'@rust-rebuild',
'--exclude' ] + exclude_list
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000)
if ret != 0 or err:
log.error('Rebuild of rust packages returned {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during rust rebuild')
else:
log.info('rust rebuild complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
# run perl-cleaner
if 'dev-lang/perl' in rebuild_packages:
log.info("Running perl-cleaner due to perl upgrade")
cmd = [ PCLEAN_PATH, '--all', '--', '-q', '--usepkg', 'n']
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000)
if ret != 0 or err:
log.error('perl-cleaner failed with code {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during perl-cleaner')
else:
log.info('perl-cleaner complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
# rebuild live packages
cmd = [ EMERGE_PATH, '--color', 'n', '-q', '--usepkg', 'n', '@live-rebuild' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600)
if ret != 0 or err:
log.error('live-rebuild returned {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during live-rebuild')
else:
log.info('live-rebuild complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
## Depclean
if conf.getboolean('default', 'do_depclean', fallback=False):
cmd = [ EMERGE_PATH, '--color', 'n', '-q', '--depclean' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600)
if ret != 0 or err:
log.error('depclean returned {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during depclean')
else:
log.info('depclean complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
## Preserved rebuild
cmd = [ EMERGE_PATH, '--color', 'n', '--usepkg', 'n', '-q', '@preserved-rebuild' ]
cmd = [ EMERGE_PATH, '--color', 'n', '-q', '@preserved-rebuild' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=72000)
if ret != 0 or err:
log.error('preserved-rebuild returned {}'.format(ret))
log.warning('preserved-rebuild returned {}'.format(ret))
for line in out.splitlines():
log.error('stdout: {}'.format(line))
log.warning('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Error during preserved-rebuild')
log.warning('stderr: {}'.format(line))
else:
log.info('preserved-rebuild complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
if conf.getboolean('default', 'do_depclean', fallback=False):
cmd = [ EMERGE_PATH, '--color', 'n', '-q', '--depclean' ]
ret, out, err = sau.helpers.exec_cmd(cmd, timeout=3600)
if ret != 0 or err:
log.warning('depclean returned {}'.format(ret))
for line in out.splitlines():
log.warning('stdout: {}'.format(line))
for line in err.splitlines():
log.warning('stderr: {}'.format(line))
else:
log.info('depclean complete')
for line in out.splitlines():
if line.startswith(' * '):
log.warning(line)
# doing grub reconfig and clean old kernels
if do_grub and os.path.exists(GRUB_MKCONFIG):
keep_kernels = conf.getint('default', 'keep_kernels', fallback=4)
if keep_kernels < 1:
log.error('keep_kernels cannot be less than one; falling back to default')
keep_kernels = 4
if do_grub:
for root, dirs, files in os.walk('/boot'):
for sysfile in ['config', 'initramfs', 'System.map', 'vmlinuz', 'kernel']:
for sysfile in ['config', 'initramfs', 'System.map', 'vmlinuz']:
match = sorted(
[f for f in files if f.startswith(f'{sysfile}-')],
reverse=True)
for f in match[keep_kernels:]:
for f in match[4:]:
log.debug(f"Removing old kernel file {f}")
os.remove(os.path.join(root, f))
break
@ -428,12 +209,11 @@ def pkg_upgrade():
cmd = [ GRUB_MKCONFIG, '-o', '/boot/grub/grub.cfg' ]
ret, out, err = sau.helpers.exec_cmd(cmd)
if ret != 0:
log.error(f"grub-mkconfig returned {ret}:")
log.warning(f"grub-mkconfig returned {ret}:")
for line in out.splitlines():
log.error('stdout: {}'.format(line))
log.warning('stdout: {}'.format(line))
for line in err.splitlines():
log.error('stderr: {}'.format(line))
raise sau.errors.UpgradeError(f'Failed to reconfiugre grub')
log.warning('stderr: {}'.format(line))
else:
log.info("grub reconfigured")
return True

View File

@ -7,20 +7,16 @@ import psutil
import sau
import sau.errors
import sau.helpers
import sau.platforms
proc_fd_map_re = re.compile(r'^.*(/(?:usr|lib|opt|etc|s?bin)[^\(]*) \(deleted\)$')
valid_service_policies=('restart', 'warn', 'ignore', 'silent-restart',
'reboot', 'silent-reboot')
proc_fd_map_re = re.compile(r'^.*(/[^\(]*) \(deleted\)$')
def _warn(policy, msg):
log = logging.getLogger(sau.LOGNAME)
if not policy.startswith('silent'):
log.warning(msg)
def get_deleted_open_files(proc):
def _get_deleted_open_files(proc):
log = logging.getLogger(sau.LOGNAME)
files = set()
@ -31,8 +27,7 @@ def get_deleted_open_files(proc):
for line in f:
match = re.match(proc_fd_map_re, line)
if match:
fname = match.group(1)
files.add(fname)
files.add(match.group(1))
return files
# on FreeBSD psutils open_files() helpfully returns a null path if a file
@ -67,42 +62,25 @@ def get_exe_file(name):
log.debug('Found binary for {} at {}'.format(name, root))
return os.path.join(root, name)
# return all processes with open files
def _get_processes():
log = logging.getLogger(sau.LOGNAME)
check_procs = set()
for proc in psutil.process_iter():
files = get_deleted_open_files(proc)
if files:
log.debug('{} has open deleted files'.format(proc))
check_procs.add(proc)
return check_procs
# Just return True if system is running on systemd
def on_systemd():
try:
init_proc = psutil.Process(pid=1)
if init_proc.name() == 'systemd':
return True
except psutil.NoSuchProcess:
pass
return False
def restart_services():
log = logging.getLogger(sau.LOGNAME)
platform = sau.platforms.get_platform()
conf = sau.config
check_procs = set()
for proc in psutil.process_iter():
files = _get_deleted_open_files(proc)
if files:
log.info('{} has open deleted files'.format(proc))
check_procs.add(proc)
check_procs = _get_processes()
# wait before the second test
time.sleep(5)
time.sleep(1)
# perform a second check to remove potential false positives
service_procs = set()
retest_procs = set()
for proc in check_procs:
files = get_deleted_open_files(proc)
files = _get_deleted_open_files(proc)
if not files:
# no deleted open files for this process any longer
continue
@ -111,50 +89,36 @@ def restart_services():
except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied):
# either of the above exceptions means the process has quit
continue
if on_systemd():
service_procs.add(proc)
else:
parent = _get_top_parent(proc)
service_procs.add(parent)
service_procs.add(parent)
retest_procs.add(proc)
recommend_restart = False
processes = {}
services = {}
for proc in service_procs:
if not proc:
continue
service_name = None
try:
service_exe = proc.exe()
proc_name = proc.name()
except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied):
log.debug('{} died before it could be restarted'.format(proc))
continue
service_name = _get_service_from_proc(proc)
if not service_name:
log.warning('no service for process {}'.format(proc))
recommend_restart = True
continue
if service_name == 'systemd':
log.info("Upgrade of systemd detected; doing daemon-reexec")
sau.helpers.exec_cmd([ '/usr/bin/systemctl', 'daemon-reexec' ])
continue
elif service_name == '@ignore':
log.info(f"Process {proc} ignored by configuration")
retest_procs.discard(proc)
log.debug('no service for process {}'.format(proc))
continue
services[proc_name] = service_name
processes[service_name] = [proc]
recommend_restart = False
for service in set([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))
[retest_procs.discard(x) for x,y in services.items() if y == service]
continue
elif policy == 'warn':
log.warning('Service "{}" has open deleted files and should be restarted'.format(service))
@ -167,17 +131,34 @@ def restart_services():
tested_parents = set()
for proc in retest_procs:
try:
proc_name = proc.name()
if proc_name not in services:
parent = _get_top_parent(proc)
if not parent:
continue
except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied):
parent_name = parent.name()
if parent in tested_parents:
log.debug('{} belongs to already tested parent {}'.format(proc, parent))
continue
if get_deleted_open_files(proc):
service = services[proc_name]
if _get_deleted_open_files(proc):
tested_parents.add(parent)
service = _get_service_from_proc(parent)
if not service:
log.warning('could not re-check process {} - failed to identify service'.format(proc))
recommend_restart = True
continue
policy = _get_service_restart_policy(service)
_warn(policy, f'{proc} still has deleted files open')
log.debug('{} is in service {}'.format(proc, service))
if parent_name in services and not services[parent_name]:
_warn(policy, '{} (parent {}) does not belong to a service and could not be restarted'.format(proc, parent))
recommend_restart = True
continue
elif parent_name in services:
policy = _get_service_restart_policy(service)
log.debug('service {} has policy {}'.format(service, policy))
if policy in ('ignore', 'warn'):
continue
_warn(policy, '{} (parent {}) still has deleted files open'.format(proc, parent))
recommend_restart = True
return recommend_restart
@ -185,7 +166,7 @@ def _get_service_restart_policy(service):
log = logging.getLogger(sau.LOGNAME)
conf = sau.config
policy = conf.get('services', service, fallback=None)
if policy and policy.lower() in valid_service_policies:
if policy and policy.lower() in ('restart', 'warn', 'ignore', 'silent-restart', 'reboot', 'silent-reboot'):
return policy.lower()
elif policy:
log.warning('service policy {} for {} is invalid'.format(policy, service))
@ -199,7 +180,6 @@ def _get_service_restart_policy(service):
def _get_service_from_proc(proc):
conf = sau.config
platform = sau.platforms.get_platform()
if not on_systemd():
proc = _get_top_parent(proc)
log = logging.getLogger(sau.LOGNAME)
try:
@ -207,45 +187,14 @@ def _get_service_from_proc(proc):
service_exe = proc.exe()
except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied):
log.debug('{} died'.format(proc))
return '@ignore'
return None
service_name = conf.get('processes', proc_name, fallback=None)
log.debug(f'configuration of process "{proc_name}" in config: "{service_name}"')
if service_name == '':
log.debug('Ignoring process {}'.format(proc))
return '@ignore'
return None
if not service_name:
# Systemd has it's own way...
if on_systemd():
if proc.pid == 1:
return 'systemd'
ret, unit, err = sau.helpers.exec_cmd([ '/usr/bin/systemctl', 'whoami', f'{proc.pid}' ])
unit = unit.strip()
name, unit_type = unit.split('.')
if ret != 0:
log.debug(f'Non-success ({ret}) when checking unit for process: {err}')
return None
elif unit_type != 'service':
log.warning(f'not restarting non-service unit "{unit}"; owner of {proc}')
return None
elif name.startswith('user@'):
log.warning(f'Not restarting user service {unit}; please log out and log in again')
return None
else:
policy = conf.get('services', name, fallback=None)
if policy and policy.lower() in valid_service_policies:
return name
_ret, enabled, _err = sau.helpers.exec_cmd([ '/usr/bin/systemctl', 'is-enabled', unit ])
enabled = enabled.strip()
if enabled not in ('enabled', 'static'):
log.warning(f'Unit {name}.service has enable status: {enabled} - will only restart "enabled" services')
return None
else:
return name
log.error(f'This should be an unreachable path when checking process {proc}')
return None
# if the exe file has been deleted since started, service_exe will be empty
# and we'll have to guess
if not service_exe:

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python3
#!/usr/bin/env python3.7
from os import environ
from setuptools import setup, find_packages
setup(
name='sau',
version='1.4.5',
version='0.9.4',
description='Tool for auto-updating OS and packages',
author='Feffe',
author_email='feffe@fulh.ax',