mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
Merge pull request #7107 from brunson/develop
Add set and auto to alternatives
This commit is contained in:
commit
3d0c868cae
@ -130,3 +130,41 @@ def remove(name, path):
|
||||
if out['retcode'] > 0:
|
||||
return out['stderr']
|
||||
return out['stdout']
|
||||
|
||||
|
||||
def auto(name):
|
||||
'''
|
||||
Trigger alternatives to set the path for <name> as
|
||||
specified by priority.
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' alternatives.auto name
|
||||
'''
|
||||
|
||||
cmd = '{0} --auto {1}'.format(_get_cmd(), name)
|
||||
out = __salt__['cmd.run_all'](cmd)
|
||||
if out['retcode'] > 0:
|
||||
return out['stderr']
|
||||
return out['stdout']
|
||||
|
||||
|
||||
def set(name, path):
|
||||
'''
|
||||
Manually set the alternative <path> for <name>.
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' alternatives.set name path
|
||||
'''
|
||||
|
||||
cmd = '{0} --set {1} {2}'.format(_get_cmd(), name, path)
|
||||
out = __salt__['cmd.run_all'](cmd)
|
||||
if out['retcode'] > 0:
|
||||
return out['stderr']
|
||||
return out['stdout']
|
||||
|
||||
|
@ -121,3 +121,85 @@ def remove(name, path):
|
||||
).format(name)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def auto(name):
|
||||
'''
|
||||
Instruct alternatives to use the highest priority
|
||||
path for <name>
|
||||
|
||||
name
|
||||
is the master name for this link group
|
||||
(e.g. pager)
|
||||
|
||||
'''
|
||||
ret = {'name': name,
|
||||
'result': True,
|
||||
'comment': '',
|
||||
'changes': {}}
|
||||
|
||||
display = __salt__['alternatives.display'](name)
|
||||
isinstalled = False
|
||||
line = display.splitlines()[0]
|
||||
if line.endswith(' auto mode'):
|
||||
ret['comment'] = '{0} already in auto mode'.format(name)
|
||||
return ret
|
||||
|
||||
ret['changes']['result'] = __salt__['alternatives.auto'](name)
|
||||
return ret
|
||||
|
||||
|
||||
def set(name, path):
|
||||
'''
|
||||
Removes installed alternative for defined <name> and <path>
|
||||
or fallback to default alternative, if some defined before.
|
||||
|
||||
name
|
||||
is the master name for this link group
|
||||
(e.g. pager)
|
||||
|
||||
path
|
||||
is the location of one of the alternative target files.
|
||||
(e.g. /usr/bin/less)
|
||||
'''
|
||||
ret = {'name': name,
|
||||
'path': path,
|
||||
'result': True,
|
||||
'changes': {},
|
||||
'comment': ''}
|
||||
|
||||
current = __salt__['alternatives.show_current'](name)
|
||||
if current == path:
|
||||
ret['comment'] = 'Alternative for {0} already set to {1}'.format(name, path)
|
||||
return ret
|
||||
|
||||
display = __salt__['alternatives.display'](name)
|
||||
isinstalled = False
|
||||
for line in display.splitlines():
|
||||
if line.startswith(path):
|
||||
isinstalled = True
|
||||
break
|
||||
|
||||
if isinstalled:
|
||||
__salt__['alternatives.set'](name, path)
|
||||
current = __salt__['alternatives.show_current'](name)
|
||||
if current == path:
|
||||
ret['result'] = True
|
||||
ret['comment'] = (
|
||||
'Alternative for {0} set to path {1}'
|
||||
).format(name, current)
|
||||
ret['changes'] = {'path': current}
|
||||
else:
|
||||
ret['comment'] = 'Alternative for {0} not updated'.format(name)
|
||||
|
||||
return ret
|
||||
|
||||
else:
|
||||
ret['result'] = False
|
||||
ret['comment'] = (
|
||||
'Alternative {0} for {1} doesn\'t exist'
|
||||
).format(path, name)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user