mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
Merge pull request #5143 from faust/gentoo-eselect
Execution and state modules for Gentoo's "eselect"
This commit is contained in:
commit
859f454969
68
salt/modules/eselect.py
Normal file
68
salt/modules/eselect.py
Normal file
@ -0,0 +1,68 @@
|
||||
'''
|
||||
Support for eselect: the Gentoo's configuration and management tool.
|
||||
'''
|
||||
|
||||
def __virtual__():
|
||||
'''
|
||||
Only work on Gentoo systems with eselect installed
|
||||
'''
|
||||
if __grains__['os'] == 'Gentoo' and salt.utils.which('eselect'):
|
||||
return 'eselect'
|
||||
return False
|
||||
|
||||
def exec_action(module,action,parameter='',state_only=False):
|
||||
'''
|
||||
Execute an arbitrary action on a module.
|
||||
|
||||
CLI Example::
|
||||
|
||||
salt '*' eselect.exec_action <module name> <action> [parameter]
|
||||
'''
|
||||
out = __salt__['cmd.run']("eselect --brief --colour=no {0} {1} {2}".format(module,action,parameter)).strip().split('\n')
|
||||
if out[0].startswith('!!! Error'):
|
||||
return False
|
||||
elif state_only:
|
||||
return True
|
||||
else:
|
||||
return out
|
||||
|
||||
def get_modules():
|
||||
'''
|
||||
Get available modules list.
|
||||
|
||||
CLI Example::
|
||||
|
||||
salt '*' eselect.get_modules
|
||||
'''
|
||||
return exec_action('modules','list')
|
||||
|
||||
def get_target_list(module):
|
||||
'''
|
||||
Get available target for the given module.
|
||||
|
||||
CLI Example::
|
||||
|
||||
salt '*' eselect.get_target_list <module name>
|
||||
'''
|
||||
return exec_action(module,'list')
|
||||
|
||||
def get_current_target(module):
|
||||
'''
|
||||
Get the currently selected target for the given module.
|
||||
|
||||
CLI Example::
|
||||
|
||||
salt '*' eselect.get_current_target <module name>
|
||||
'''
|
||||
return exec_action(module,'show')[0]
|
||||
|
||||
def set_target(module,target):
|
||||
'''
|
||||
Set the target for the given module.
|
||||
Target can be specified by index or name.
|
||||
|
||||
CLI Example::
|
||||
|
||||
salt '*' eselect.set_target <module name> <target>
|
||||
'''
|
||||
return exec_action(module,'set',target, state_only=True)
|
52
salt/states/eselect.py
Normal file
52
salt/states/eselect.py
Normal file
@ -0,0 +1,52 @@
|
||||
'''
|
||||
Management of Gentoo configuration using eselect
|
||||
================================================
|
||||
|
||||
A state module to manage Gentoo configuration via eselect
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
profile:
|
||||
eselect.setted:
|
||||
target: hardened/linux/amd64
|
||||
'''
|
||||
|
||||
def __virtual__():
|
||||
'''
|
||||
Only load if the eselect module is available in __salt__
|
||||
'''
|
||||
return 'eselect' if 'eselect.exec_action' in __salt__ else False
|
||||
|
||||
def setted(name, target):
|
||||
'''
|
||||
Verify that the given module is set to the given target
|
||||
|
||||
name
|
||||
The name of the module
|
||||
'''
|
||||
ret = {'changes': {},
|
||||
'comment': '',
|
||||
'name': name,
|
||||
'result': True}
|
||||
|
||||
old_target = __salt__['eselect.get_current_target'](name)
|
||||
|
||||
if target == old_target:
|
||||
ret['comment'] = 'Target "{0}" is already set on "{1}" module.'.format(target,name)
|
||||
elif target not in __salt__['eselect.get_target_list'](name):
|
||||
ret['comment'] = 'Target "{0}" is not available for "{1}" module.'.format(target,name)
|
||||
ret['result'] = False
|
||||
elif __opts__['test']:
|
||||
ret['comment'] = 'Target "{0}" will be set on "{1}" module.'.format(target,name)
|
||||
ret['result'] = None
|
||||
return ret
|
||||
else:
|
||||
result = __salt__['eselect.set_target'](name,target)
|
||||
if result:
|
||||
ret['comment'] = 'Target "{0}" failed to be set on "{1}" module.'.format(target,name)
|
||||
ret['result'] = False
|
||||
else:
|
||||
ret['changes'][name] = {'old':old_target,'new':target}
|
||||
ret['comment'] = 'Target "{0}" set on "{1}" module.'.format(target,name)
|
||||
|
||||
return ret
|
Loading…
Reference in New Issue
Block a user