mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Additions to beacons execution module to allow enabling, disabling and saving beacons.
This commit is contained in:
parent
d88cdc1446
commit
2cffd7572e
3 changed files with 151 additions and 3 deletions
|
@ -35,7 +35,11 @@ class Beacon(object):
|
|||
'''
|
||||
ret = []
|
||||
b_config = copy.deepcopy(config)
|
||||
if 'enabled' in b_config and not b_config['enabled']:
|
||||
return
|
||||
for mod in config:
|
||||
if mod == 'enabled':
|
||||
continue
|
||||
log.trace('Beacon processing: {0}'.format(mod))
|
||||
fun_str = '{0}.beacon'.format(mod)
|
||||
if fun_str in self.beacons:
|
||||
|
@ -113,3 +117,31 @@ class Beacon(object):
|
|||
tag='/salt/minion/minion_beacon_delete_complete')
|
||||
|
||||
return True
|
||||
|
||||
def enable_beacons(self):
|
||||
'''
|
||||
Enable beacons
|
||||
'''
|
||||
|
||||
self.opts['beacons']['enabled'] = True
|
||||
|
||||
# Fire the complete event back along with updated list of beacons
|
||||
evt = salt.utils.event.get_event('minion', opts=self.opts)
|
||||
evt.fire_event({'complete': True, 'beacons': self.opts['beacons']},
|
||||
tag='/salt/minion/minion_beacons_enabled_complete')
|
||||
|
||||
return True
|
||||
|
||||
def disable_beacons(self):
|
||||
'''
|
||||
Enable beacons
|
||||
'''
|
||||
|
||||
self.opts['beacons']['enabled'] = False
|
||||
|
||||
# Fire the complete event back along with updated list of beacons
|
||||
evt = salt.utils.event.get_event('minion', opts=self.opts)
|
||||
evt.fire_event({'complete': True, 'beacons': self.opts['beacons']},
|
||||
tag='/salt/minion/minion_beacons_disabled_complete')
|
||||
|
||||
return True
|
||||
|
|
|
@ -1351,8 +1351,12 @@ class Minion(MinionBase):
|
|||
|
||||
if func == 'add':
|
||||
self.beacons.add_beacon(name, beacon_data)
|
||||
if func == 'delete':
|
||||
elif func == 'delete':
|
||||
self.beacons.delete_beacon(name)
|
||||
elif func == 'enable':
|
||||
self.beacons.enable_beacons()
|
||||
elif func == 'disable':
|
||||
self.beacons.disable_beacons()
|
||||
|
||||
def environ_setenv(self, package):
|
||||
'''
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Module for managing the Salt schedule on a minion
|
||||
Module for managing the Salt beacons on a minion
|
||||
|
||||
.. versionadded:: Beryllium
|
||||
|
||||
|
@ -8,6 +8,7 @@ Module for managing the Salt schedule on a minion
|
|||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import
|
||||
import os
|
||||
import yaml
|
||||
|
||||
import salt.utils
|
||||
|
@ -51,7 +52,7 @@ def list_(return_yaml=True):
|
|||
|
||||
def add(name, beacon_data, **kwargs):
|
||||
'''
|
||||
List the jobs currently scheduled on the minion
|
||||
Add a beacon on the minion
|
||||
|
||||
CLI Example:
|
||||
|
||||
|
@ -140,3 +141,114 @@ def delete(name, **kwargs):
|
|||
# Effectively a no-op, since we can't really return without an event system
|
||||
ret['comment'] = 'Event module not available. Beacon add failed.'
|
||||
return ret
|
||||
|
||||
|
||||
def save():
|
||||
'''
|
||||
Save all beacons on the minion
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' beacons.save
|
||||
'''
|
||||
|
||||
ret = {'comment': [],
|
||||
'result': True}
|
||||
|
||||
beacons = list_(return_yaml=False)
|
||||
|
||||
# move this file into an configurable opt
|
||||
sfn = '{0}/{1}/beacons.conf'.format(__opts__['config_dir'],
|
||||
os.path.dirname(__opts__['default_include']))
|
||||
if beacons:
|
||||
tmp = {'beacons': beacons}
|
||||
yaml_out = yaml.safe_dump(tmp, default_flow_style=False)
|
||||
else:
|
||||
yaml_out = ''
|
||||
|
||||
try:
|
||||
with salt.utils.fopen(sfn, 'w+') as fp_:
|
||||
fp_.write(yaml_out)
|
||||
ret['comment'] = 'Beacons saved to {0}.'.format(sfn)
|
||||
except (IOError, OSError):
|
||||
ret['comment'] = 'Unable to write to beacons file at {0}. Check permissions.'.format(sfn)
|
||||
ret['result'] = False
|
||||
return ret
|
||||
|
||||
|
||||
def enable(**kwargs):
|
||||
'''
|
||||
Enable all beacons on the minion
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' beacons.enable
|
||||
'''
|
||||
|
||||
ret = {'comment': [],
|
||||
'result': True}
|
||||
|
||||
if 'test' in kwargs and kwargs['test']:
|
||||
ret['comment'] = 'Beacons would be enabled.'
|
||||
else:
|
||||
try:
|
||||
eventer = salt.utils.event.get_event('minion', opts=__opts__)
|
||||
res = __salt__['event.fire']({'func': 'enable'}, 'manage_beacons')
|
||||
if res:
|
||||
event_ret = eventer.get_event(tag='/salt/minion/minion_beacons_enabled_complete', wait=30)
|
||||
if event_ret and event_ret['complete']:
|
||||
beacons = event_ret['beacons']
|
||||
if 'enabled' in beacons and beacons['enabled']:
|
||||
ret['result'] = True
|
||||
ret['comment'] = 'Enabled beacons on minion.'
|
||||
else:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Failed to enable beacons on minion.'
|
||||
return ret
|
||||
except KeyError:
|
||||
# Effectively a no-op, since we can't really return without an event system
|
||||
ret['comment'] = 'Event module not available. Beacons enable job failed.'
|
||||
return ret
|
||||
|
||||
|
||||
def disable(**kwargs):
|
||||
'''
|
||||
Disable all beaconsd jobs on the minion
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' beacons.disable
|
||||
'''
|
||||
|
||||
ret = {'comment': [],
|
||||
'result': True}
|
||||
|
||||
if 'test' in kwargs and kwargs['test']:
|
||||
ret['comment'] = 'Beacons would be disabled.'
|
||||
else:
|
||||
try:
|
||||
eventer = salt.utils.event.get_event('minion', opts=__opts__)
|
||||
res = __salt__['event.fire']({'func': 'disable'}, 'manage_beacons')
|
||||
if res:
|
||||
event_ret = eventer.get_event(tag='/salt/minion/minion_beacons_disabled_complete', wait=30)
|
||||
log.debug('event_ret {0}'.format(event_ret))
|
||||
if event_ret and event_ret['complete']:
|
||||
beacons = event_ret['beacons']
|
||||
if 'enabled' in beacons and not beacons['enabled']:
|
||||
ret['result'] = True
|
||||
ret['comment'] = 'Disabled beacons on minion.'
|
||||
else:
|
||||
ret['result'] = False
|
||||
ret['comment'] = 'Failed to disable beacons on minion.'
|
||||
return ret
|
||||
except KeyError:
|
||||
# Effectively a no-op, since we can't really return without an event system
|
||||
ret['comment'] = 'Event module not available. Beacons enable job failed.'
|
||||
return ret
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue