From 20c2c91bba17da0f3ccd4f922ce97f6aa14b9dd8 Mon Sep 17 00:00:00 2001 From: Clint Armstrong Date: Fri, 23 Sep 2016 14:45:16 -0400 Subject: [PATCH 1/2] daemon-reload on call to service.avaliable --- salt/modules/systemd.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/salt/modules/systemd.py b/salt/modules/systemd.py index 15e96fb884..795f8e0ec7 100644 --- a/salt/modules/systemd.py +++ b/salt/modules/systemd.py @@ -298,7 +298,7 @@ def _untracked_custom_unit_found(name): ''' unit_path = os.path.join('/etc/systemd/system', _canonical_unit_name(name)) - return os.access(unit_path, os.R_OK) and not available(name) + return os.access(unit_path, os.R_OK) and not available(name, False) def _unit_file_changed(name): @@ -468,7 +468,7 @@ def get_all(): return sorted(ret) -def available(name): +def available(name, check_units=True): ''' .. versionadded:: 0.10.4 @@ -481,6 +481,8 @@ def available(name): salt '*' service.available sshd ''' + if check_units: + _check_for_unit_changes(name) out = _systemctl_status(name).lower() for line in salt.utils.itertools.split(out, '\n'): match = re.match(r'\s+loaded:\s+(\S+)', line) From c4060ba2c1b802257ac84156b2351f64b5c1a0df Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Wed, 28 Sep 2016 15:29:25 -0500 Subject: [PATCH 2/2] Move check for service availability to a helper function This allows for us to check for unit file changes at the beginning of the available() function without invoking the available() function a second time, potentially resulting in a recursive loop if not handled delicately. --- salt/modules/systemd.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/salt/modules/systemd.py b/salt/modules/systemd.py index 795f8e0ec7..f01bb15528 100644 --- a/salt/modules/systemd.py +++ b/salt/modules/systemd.py @@ -71,6 +71,23 @@ def _canonical_unit_name(name): return '%s.service' % name +def _check_available(name): + ''' + Returns boolean telling whether or not the named service is available + ''' + out = _systemctl_status(name).lower() + for line in salt.utils.itertools.split(out, '\n'): + match = re.match(r'\s+loaded:\s+(\S+)', line) + if match: + ret = match.group(1) != 'not-found' + break + else: + raise CommandExecutionError( + 'Failed to get information on unit \'%s\'' % name + ) + return ret + + def _check_for_unit_changes(name): ''' Check for modified/updated unit files, and run a daemon-reload if any are @@ -298,7 +315,7 @@ def _untracked_custom_unit_found(name): ''' unit_path = os.path.join('/etc/systemd/system', _canonical_unit_name(name)) - return os.access(unit_path, os.R_OK) and not available(name, False) + return os.access(unit_path, os.R_OK) and not _check_available(name) def _unit_file_changed(name): @@ -468,7 +485,7 @@ def get_all(): return sorted(ret) -def available(name, check_units=True): +def available(name): ''' .. versionadded:: 0.10.4 @@ -481,19 +498,8 @@ def available(name, check_units=True): salt '*' service.available sshd ''' - if check_units: - _check_for_unit_changes(name) - out = _systemctl_status(name).lower() - for line in salt.utils.itertools.split(out, '\n'): - match = re.match(r'\s+loaded:\s+(\S+)', line) - if match: - ret = match.group(1) != 'not-found' - break - else: - raise CommandExecutionError( - 'Failed to get information on unit \'%s\'' % name - ) - return ret + _check_for_unit_changes(name) + return _check_available(name) def missing(name):