adding code to allow enabling and disabling specific beacons.

This commit is contained in:
Gareth J. Greenaway 2015-05-12 08:56:48 -07:00
parent efd2a8e281
commit 66b86a0ae4
3 changed files with 129 additions and 0 deletions

View file

@ -40,6 +40,13 @@ class Beacon(object):
for mod in config:
if mod == 'enabled':
continue
if 'enabled' in config[mod] and not config[mod]['enabled']:
log.trace('Beacon {0} disabled'.format(mod))
continue
elif 'enabled' in config[mod] and config[mod]['enabled']:
# remove 'enabled' item before processing the beacon
del config[mod]['enabled']
log.trace('Beacon processing: {0}'.format(mod))
fun_str = '{0}.beacon'.format(mod)
if fun_str in self.beacons:
@ -154,3 +161,31 @@ class Beacon(object):
tag='/salt/minion/minion_beacons_disabled_complete')
return True
def enable_beacon(self, name):
'''
Enable a beacon
'''
self.opts['beacons'][name]['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_beacon_enabled_complete')
return True
def disable_beacon(self, name):
'''
Disable a beacon
'''
self.opts['beacons'][name]['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_beacon_disabled_complete')
return True

View file

@ -1357,6 +1357,10 @@ class Minion(MinionBase):
self.beacons.enable_beacons()
elif func == 'disable':
self.beacons.disable_beacons()
elif func == 'enable_beacon':
self.beacons.enable_beacon(name)
elif func == 'disable_beacon':
self.beacons.disable_beacon(name)
def environ_setenv(self, package):
'''

View file

@ -251,3 +251,93 @@ def disable(**kwargs):
# 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 enable_beacon(name, **kwargs):
'''
Enable beacon on the minion
CLI Example:
.. code-block:: bash
salt '*' beacons.enable_beacon ps
'''
ret = {'comment': [],
'result': True}
if not name:
ret['comment'] = 'Beacon name is required.'
ret['result'] = False
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Beacon {0} would be enabled.'.format(name)
else:
if name not in list_(return_yaml=True):
ret['comment'] = 'Beacon {0} is not currently configured.'.format(name)
ret['result'] = False
try:
eventer = salt.utils.event.get_event('minion', opts=__opts__)
res = __salt__['event.fire']({'func': 'enable_beacon', 'name': name}, 'manage_beacons')
if res:
event_ret = eventer.get_event(tag='/salt/minion/minion_beacon_enabled_complete', wait=30)
if event_ret and event_ret['complete']:
beacons = event_ret['beacons']
if 'enabled' in beacons[name] and beacons[name]['enabled']:
ret['result'] = True
ret['comment'] = 'Enabled beacon {0} on minion.'.format(name)
else:
ret['result'] = False
ret['comment'] = 'Failed to enable beacon {0} on minion.'.format(name)
return ret
except KeyError:
# Effectively a no-op, since we can't really return without an event system
ret['comment'] = 'Event module not available. Beacon enable job failed.'
return ret
def disable_beacon(name, **kwargs):
'''
Disable beacon on the minion
CLI Example:
.. code-block:: bash
salt '*' beacons.disable_beacon ps
'''
ret = {'comment': [],
'result': True}
if not name:
ret['comment'] = 'Beacon name is required.'
ret['result'] = False
if 'test' in kwargs and kwargs['test']:
ret['comment'] = 'Beacons would be enabled.'
else:
if name not in list_(return_yaml=True):
ret['comment'] = 'Beacon {0} is not currently configured.'.format(name)
ret['result'] = False
try:
eventer = salt.utils.event.get_event('minion', opts=__opts__)
res = __salt__['event.fire']({'func': 'disable_beacon', 'name': name}, 'manage_beacons')
if res:
event_ret = eventer.get_event(tag='/salt/minion/minion_beacon_disabled_complete', wait=30)
if event_ret and event_ret['complete']:
beacons = event_ret['beacons']
if 'enabled' in beacons[name] and not beacons[name]['enabled']:
ret['result'] = True
ret['comment'] = 'Disabled beacon on minion.'
else:
ret['result'] = False
ret['comment'] = 'Failed to disable beacon 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. Beacon disable job failed.'
return ret