Commit 8bb108c2 authored by Olivier Mauras's avatar Olivier Mauras Committed by Jakub Jirutka
Browse files

community/salt: pkg/service modules improvements

parent 756fa0ec
Patch has been merged upstream through PR https://github.com/saltstack/salt/pull/36207
Will keep the patch until the feature appears in the next version
Upstream code not yet released
---
diff --git a/salt/modules/apk.py b/salt/modules/apk.py
new file mode 100644
index 0000000..40bf38f
index 0000000..c7cf312
--- /dev/null
+++ b/salt/modules/apk.py
@@ -0,0 +1,571 @@
@@ -0,0 +1,583 @@
+# -*- coding: utf-8 -*-
+'''
+Support for apk
......@@ -17,7 +16,7 @@ index 0000000..40bf38f
+ *'pkg.install' is not available*), see :ref:`here
+ <module-provider-override>`.
+
+.. versionadded: 2016.3.0
+.. versionadded: Nitrogen
+
+'''
+from __future__ import absolute_import
......@@ -46,9 +45,6 @@ index 0000000..40bf38f
+ return __virtualname__
+ return (False, "Module apk only works on Alpine Linux based systems")
+
+# Unavailable functions
+#def version(*names, **kwargs):
+# return 'Not available'
+#def autoremove(list_only=False, purge=False):
+# return 'Not available'
+#def hold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613
......@@ -78,6 +74,21 @@ index 0000000..40bf38f
+#def info_installed(*names):
+# return 'Not available'
+
+def version(*names, **kwargs):
+ '''
+ Returns a string representing the package version or an empty string if not
+ installed. If more than one package name is specified, a dict of
+ name/version pairs is returned.
+
+ CLI Example:
+
+ .. code-block:: bash
+
+ salt '*' pkg.version <package name>
+ salt '*' pkg.version <package1> <package2> <package3> ...
+ '''
+ return __salt__['pkg_resource.version'](*names, **kwargs)
+
+
+def refresh_db():
+ '''
......@@ -579,15 +590,314 @@ index 0000000..40bf38f
+
+ return ret
diff --git a/salt/modules/gentoo_service.py b/salt/modules/gentoo_service.py
index 6345ae3..32dfcde 100644
index 92cf48a..32dfcde 100644
--- a/salt/modules/gentoo_service.py
+++ b/salt/modules/gentoo_service.py
@@ -31,6 +31,8 @@ def __virtual__():
@@ -12,6 +12,14 @@ to the correct service manager
# Import Python libs
from __future__ import absolute_import
+import logging
+
+# Import salt libs
+import salt.utils.systemd
+import salt.utils.odict as odict
+
+# Set up logging
+log = logging.getLogger(__name__)
# Define the module's virtual name
__virtualname__ = 'service'
@@ -19,12 +27,65 @@ __virtualname__ = 'service'
def __virtual__():
'''
if __grains__['os'] == 'Gentoo' and not salt.utils.systemd.booted(__context__):
return __virtualname__
+ if __grains__['os'] == 'Alpine':
- Only work on systems which default to systemd
+ Only work on systems which default to OpenRC
'''
- if __grains__['os'] == 'Gentoo':
+ if __grains__['os'] == 'Gentoo' and not salt.utils.systemd.booted(__context__):
+ return __virtualname__
+ if __grains__['os'] == 'Alpine':
return __virtualname__
return (False, 'The gentoo_service execution module cannot be loaded: '
'only available on Gentoo/Open-RC systems.')
- 'only available on Gentoo systems.')
+ 'only available on Gentoo/Open-RC systems.')
+
+
+def _ret_code(cmd):
+ log.debug('executing [{0}]'.format(cmd))
+ sts = __salt__['cmd.retcode'](cmd, python_shell=False)
+ return sts
+
+
+def _list_services():
+ return __salt__['cmd.run']('rc-update -v show').splitlines()
+
+
+def _get_service_list(include_enabled=True, include_disabled=False):
+ enabled_services = dict()
+ disabled_services = set()
+ lines = _list_services()
+ for line in lines:
+ if '|' not in line:
+ continue
+ service = [l.strip() for l in line.split('|')]
+ # enabled service should have runlevels
+ if service[1]:
+ if include_enabled:
+ enabled_services.update({service[0]: sorted(service[1].split())})
+ continue
+ # in any other case service is disabled
+ if include_disabled:
+ disabled_services.update({service[0]: []})
+ return enabled_services, disabled_services
+
+
+def _enable_delta(name, requested_runlevels):
+ all_enabled = get_enabled()
+ current_levels = set(all_enabled[name] if name in all_enabled else [])
+ enabled_levels = requested_runlevels - current_levels
+ disabled_levels = current_levels - requested_runlevels
+ return enabled_levels, disabled_levels
+
+
+def _disable_delta(name, requested_runlevels):
+ all_enabled = get_enabled()
+ current_levels = set(all_enabled[name] if name in all_enabled else [])
+ return current_levels & requested_runlevels
+
+
+def _service_cmd(*args):
+ return '/etc/init.d/{0} {1}'.format(args[0], ' '.join(args[1:]))
+
+
+def _enable_disable_cmd(name, command, runlevels=()):
+ return 'rc-update {0} {1} {2}'.format(command, name, ' '.join(sorted(runlevels))).strip()
def get_enabled():
@@ -37,15 +98,8 @@ def get_enabled():
salt '*' service.get_enabled
'''
- ret = set()
- lines = __salt__['cmd.run']('rc-update show').splitlines()
- for line in lines:
- if '|' not in line:
- continue
- if 'shutdown' in line:
- continue
- ret.add(line.split('|')[0].strip())
- return sorted(ret)
+ (enabled_services, disabled_services) = _get_service_list()
+ return odict.OrderedDict(enabled_services)
def get_disabled():
@@ -58,17 +112,9 @@ def get_disabled():
salt '*' service.get_disabled
'''
- ret = set()
- lines = __salt__['cmd.run']('rc-update -v show').splitlines()
- for line in lines:
- if '|' not in line:
- continue
- elif 'shutdown' in line:
- continue
- comps = line.split()
- if len(comps) < 3:
- ret.add(comps[0])
- return sorted(ret)
+ (enabled_services, disabled_services) = _get_service_list(include_enabled=False,
+ include_disabled=True)
+ return sorted(disabled_services)
def available(name):
@@ -82,7 +128,9 @@ def available(name):
salt '*' service.available sshd
'''
- return name in get_all()
+ (enabled_services, disabled_services) = _get_service_list(include_enabled=True,
+ include_disabled=True)
+ return name in enabled_services or name in disabled_services
def missing(name):
@@ -97,7 +145,7 @@ def missing(name):
salt '*' service.missing sshd
'''
- return name not in get_all()
+ return not available(name)
def get_all():
@@ -110,7 +158,10 @@ def get_all():
salt '*' service.get_all
'''
- return sorted(get_enabled() + get_disabled())
+ (enabled_services, disabled_services) = _get_service_list(include_enabled=True,
+ include_disabled=True)
+ enabled_services.update(dict([(s, []) for s in disabled_services]))
+ return odict.OrderedDict(enabled_services)
def start(name):
@@ -123,8 +174,8 @@ def start(name):
salt '*' service.start <service name>
'''
- cmd = '/etc/init.d/{0} start'.format(name)
- return not __salt__['cmd.retcode'](cmd, python_shell=False)
+ cmd = _service_cmd(name, 'start')
+ return not _ret_code(cmd)
def stop(name):
@@ -137,8 +188,8 @@ def stop(name):
salt '*' service.stop <service name>
'''
- cmd = '/etc/init.d/{0} stop'.format(name)
- return not __salt__['cmd.retcode'](cmd, python_shell=False)
+ cmd = _service_cmd(name, 'stop')
+ return not _ret_code(cmd)
def restart(name):
@@ -151,8 +202,36 @@ def restart(name):
salt '*' service.restart <service name>
'''
- cmd = '/etc/init.d/{0} restart'.format(name)
- return not __salt__['cmd.retcode'](cmd, python_shell=False)
+ cmd = _service_cmd(name, 'restart')
+ return not _ret_code(cmd)
+
+
+def reload_(name):
+ '''
+ Reload the named service
+
+ CLI Example:
+
+ .. code-block:: bash
+
+ salt '*' service.reload <service name>
+ '''
+ cmd = _service_cmd(name, 'reload')
+ return not _ret_code(cmd)
+
+
+def zap(name):
+ '''
+ Resets service state
+
+ CLI Example:
+
+ .. code-block:: bash
+
+ salt '*' service.zap <service name>
+ '''
+ cmd = _service_cmd(name, 'zap')
+ return not _ret_code(cmd)
def status(name, sig=None):
@@ -167,7 +246,10 @@ def status(name, sig=None):
salt '*' service.status <service name> [service signature]
'''
- return __salt__['status.pid'](sig if sig else name)
+ if sig:
+ return bool(__salt__['status.pid'](sig))
+ cmd = _service_cmd(name, 'status')
+ return not _ret_code(cmd)
def enable(name, **kwargs):
@@ -178,10 +260,26 @@ def enable(name, **kwargs):
.. code-block:: bash
- salt '*' service.enable <service name>
+ salt '*' service.enable <service name> <runlevels=single-runlevel>
+ salt '*' service.enable <service name> <runlevels=[runlevel1,runlevel2]>
'''
- cmd = 'rc-update add {0} default'.format(name)
- return not __salt__['cmd.retcode'](cmd, python_shell=False)
+ if 'runlevels' in kwargs:
+ requested_levels = set(kwargs['runlevels'] if isinstance(kwargs['runlevels'],
+ list) else [kwargs['runlevels']])
+ enabled_levels, disabled_levels = _enable_delta(name, requested_levels)
+ commands = []
+ if disabled_levels:
+ commands.append(_enable_disable_cmd(name, 'delete', disabled_levels))
+ if enabled_levels:
+ commands.append(_enable_disable_cmd(name, 'add', enabled_levels))
+ if not commands:
+ return True
+ else:
+ commands = [_enable_disable_cmd(name, 'add')]
+ for cmd in commands:
+ if _ret_code(cmd):
+ return False
+ return True
def disable(name, **kwargs):
@@ -192,10 +290,18 @@ def disable(name, **kwargs):
.. code-block:: bash
- salt '*' service.disable <service name>
+ salt '*' service.disable <service name> <runlevels=single-runlevel>
+ salt '*' service.disable <service name> <runlevels=[runlevel1,runlevel2]>
'''
- cmd = 'rc-update delete {0} default'.format(name)
- return not __salt__['cmd.retcode'](cmd, python_shell=False)
+ levels = []
+ if 'runlevels' in kwargs:
+ requested_levels = set(kwargs['runlevels'] if isinstance(kwargs['runlevels'],
+ list) else [kwargs['runlevels']])
+ levels = _disable_delta(name, requested_levels)
+ if not levels:
+ return True
+ cmd = _enable_disable_cmd(name, 'delete', levels)
+ return not _ret_code(cmd)
def enabled(name, **kwargs):
@@ -206,9 +312,17 @@ def enabled(name, **kwargs):
.. code-block:: bash
- salt '*' service.enabled <service name>
+ salt '*' service.enabled <service name> <runlevels=single-runlevel>
+ salt '*' service.enabled <service name> <runlevels=[runlevel1,runlevel2]>
'''
- return name in get_enabled()
+ enabled_services = get_enabled()
+ if name not in enabled_services:
+ return False
+ if 'runlevels' not in kwargs:
+ return True
+ requested_levels = set(kwargs['runlevels'] if isinstance(kwargs['runlevels'],
+ list) else [kwargs['runlevels']])
+ return len(requested_levels - set(enabled_services[name])) == 0
def disabled(name):
@@ -219,6 +333,6 @@ def disabled(name):
.. code-block:: bash
- salt '*' service.disabled <service name>
+ salt '*' service.disabled <service name> <runlevels=[runlevel]>
'''
return name in get_disabled()
......@@ -3,7 +3,7 @@
# Maintainer: Olivier Mauras <olivier@mauras.ch>
pkgname=salt
pkgver=2016.3.3
pkgrel=1
pkgrel=2
pkgdesc="A parallel remote execution system"
url="https://github.com/saltstack/salt"
arch="noarch"
......@@ -119,7 +119,7 @@ md5sums="5db25ad762a0780ff5d74561516a97fa salt-2016.3.3.tar.gz
c8326b9cff0df6065a1320eefea09b2c salt-minion.initd
a24d13b018a35b31b34167bcaa749db5 salt-syndic.confd
dffce15d3a16a2dc40dd02d0c24fb4c6 salt-syndic.initd
87331025b2b76a4b608b54304efd8f53 0001-alpine-support.patch"
ac184dd7aaf0706e69a2941a44b7a43a 0001-alpine-support.patch"
sha256sums="7d09bc9142d96ecdfe779cf813e43ab62b9b4632f6e0711af383809661b87e1d salt-2016.3.3.tar.gz
b25cfdb769305f2245b27f6753adee590bac10faeb8c43ba605dbf7e931fe258 salt-api.confd
f8918f2819b81e69af1b8564b90ec370942ed733aefd4b97e5d2446a892880eb salt-api.initd
......@@ -129,7 +129,7 @@ f8918f2819b81e69af1b8564b90ec370942ed733aefd4b97e5d2446a892880eb salt-api.initd
ae9236919c3fee3eef0ef8ad54334d6f833a51bbd4d42c40214614498acbe573 salt-minion.initd
66a663c426e2fa157ea78f7f9b2f33f17b72dbc48e119f8dd2609aab8f8370e7 salt-syndic.confd
6a453f63e51f2cc1ecb024ee8e7fa1beab7fbcea010f3eb10ea23bdb2383e7f5 salt-syndic.initd
b3680210e046ca19eba34169f17eba2604e7dfe5b05984c29ac5663d4f3bd17f 0001-alpine-support.patch"
f8f2d169706ec75f6019178ef3e53291648b816715166fb3f237195b473ad559 0001-alpine-support.patch"
sha512sums="89a6f9e7d8a337ec5addda2374c833d251162150c4e57ce34f7f9e81a75f540539071064c0e7c4486ef6e447c6422363f868e9b035e4c5af2f3d320e98e0226d salt-2016.3.3.tar.gz
975ba2f5e681fbd62045da61cc3dc065b148683a07b5df7eca9f131e47314eb6bfa8660ca1c06a3bd93683c7097d0ff9f8e514273dd24d82fb2de6a255e6b275 salt-api.confd
435d399bfecf431d0c713031e2ae57ce25b5c6edc98b62f33bd7a4ff1c587e3cdeb988445ae0c3e9ffc1911555c3694654d98815f9562b8a14bf0688ec1ebea6 salt-api.initd
......@@ -139,4 +139,4 @@ cfbbeb8023a383e7c42d84e3346edfd068c9ec7650c4ddc3caa38534da325a67497e1f06ca02cc1f
c6634a592c6f3e65dd2b704cb500486bf8004c5b287d4a4d42b09af36ef129c59d6a89f005af058cf7911e8587d927b3db931186569084f13ebaca56f6ef93a0 salt-minion.initd
bafc6ea10cdafd0aef868feb35aecbe4ae6a7dff0ae42862bded85715ad763eb89e1ed27437866a7e5f2b9f7064e3c2a3fb59814487744ba4227238d95cf3818 salt-syndic.confd
d71133e834685304e0167554035ebbc861252f972bbe981cc71e45b70f15d94a28a02a369463c9a641372919689f96b62a0408b14f824ad986d536e52b1e5ec0 salt-syndic.initd
6e4d3129b661184317f60dda7217fec3815f59d126c368ff73e89a32a46e96c1b243614506f5f81b4f42130ef64153e90c9828e0d7c743d52dcff980d068c0fa 0001-alpine-support.patch"
a937cfd3d546dbb15bd7bafac63873010c6207ffffa2c83a7857c140af5806a505b75ed3959fa6ea2e2fd2a8117314e5300269826d7b808a20eda696983ef701 0001-alpine-support.patch"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment