Skip to content

Better handling of systemd parametrised and other special units not listed by list-unit-files. #11921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions salt/modules/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ def _systemctl_cmd(action, name):
return 'systemctl {0} {1}'.format(action, _canonical_unit_name(name))


def _get_all_units():
'''
Get all units and their state. Units ending in .service
are normalized so that they can be referenced without a type suffix.
'''
rexp = re.compile(r'(?m)^(?P<name>.+)\.(?P<type>' +
'|'.join(VALID_UNIT_TYPES) +
r')\s+loaded\s+(?P<active>[^\s]+)')

out = __salt__['cmd.run_stdout'](
'systemctl --full --no-legend --no-pager list-units | col -b'
)

ret = {}
for match in rexp.finditer(out):
name = match.group('name')
if match.group('type') != 'service':
name += '.' + match.group('type')
ret[name] = match.group('active')
return ret


def _get_all_unit_files():
'''
Get all unit files and their state. Unit files ending in .service
Expand All @@ -82,7 +104,7 @@ def _get_all_unit_files():
r')\s+(?P<state>.+)$')

out = __salt__['cmd.run_stdout'](
'systemctl --full list-unit-files | col -b'
'systemctl --full --no-legend --no-pager list-unit-files | col -b'
)

ret = {}
Expand Down Expand Up @@ -173,7 +195,7 @@ def get_all():

salt '*' service.get_all
'''
return sorted(_get_all_unit_files().keys())
return sorted(set(_get_all_units().keys() + _get_all_unit_files().keys()))


def available(name):
Expand All @@ -187,7 +209,15 @@ def available(name):

salt '*' service.available sshd
'''
return _canonical_template_unit_name(name) in get_all()
name = _canonical_template_unit_name(name)
units = get_all()
if name in units:
return True
elif '@' in name:
templatename = name[:name.find('@') + 1]
return templatename in units
else:
return False


def missing(name):
Expand All @@ -202,7 +232,7 @@ def missing(name):

salt '*' service.missing sshd
'''
return not _canonical_template_unit_name(name) in get_all()
return not available(name)


def start(name):
Expand Down