Add support for beacon config as a list

This commit is contained in:
makearl 2015-11-18 09:20:23 -06:00
parent 590847195a
commit 9ccd0b2d4c
2 changed files with 83 additions and 23 deletions

View file

@ -16,7 +16,7 @@ log = logging.getLogger(__name__)
class Beacon(object):
'''
This class is used to eveluate and execute on the beacon system
This class is used to evaluate and execute on the beacon system
'''
def __init__(self, opts, functions):
self.opts = opts
@ -26,12 +26,12 @@ class Beacon(object):
def process(self, config):
'''
Process the configured beacons
The config must be a dict and looks like this in yaml
The config must be a list and looks like this in yaml
code_block:: yaml
beacons:
inotify:
/etc/fstab: {}
/var/cache/foo: {}
- /etc/fstab: {}
- /var/cache/foo: {}
'''
ret = []
b_config = copy.deepcopy(config)
@ -40,26 +40,39 @@ 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']
# Convert beacons that are lists to a dict to make processing easier
current_beacon_config = None
if isinstance(config[mod], list):
current_beacon_config = {}
map(current_beacon_config.update, config[mod])
elif isinstance(config[mod], dict):
salt.utils.warn_until(
'Boron',
'Beacon configuration should be a list instead of a dictionary.'
)
current_beacon_config = config[mod]
if current_beacon_config.has_key('enabled'):
if not current_beacon_config['enabled']:
log.trace('Beacon {0} disabled'.format(mod))
continue
else:
# remove 'enabled' item before processing the beacon
if isinstance(config[mod], dict):
del config[mod]['enabled']
else:
self._remove_list_item(config[mod], 'enabled')
log.trace('Beacon processing: {0}'.format(mod))
fun_str = '{0}.beacon'.format(mod)
if fun_str in self.beacons:
if isinstance(config[mod], list):
interval = None
interval_config = [arg for arg in config[mod] if 'interval' in arg]
if interval_config:
interval = interval_config[0]['interval']
elif isinstance(config[mod], dict):
interval = config[mod].get('interval', False)
if isinstance(current_beacon_config, dict):
interval = current_beacon_config.get('interval', False)
if interval:
if isinstance(b_config[mod], list):
b_config[mod].remove(interval_config[0])
self._remove_list_item(b_config[mod], 'interval')
elif isinstance(b_config[mod], dict):
b_config[mod].pop('interval')
if not self._process_interval(mod, interval):
@ -96,6 +109,40 @@ class Beacon(object):
self.interval_map[mod] = 1
return False
def _get_index(self, beacon_config, label):
'''
Return the index of a labeled config item in the beacon config, -1 if the index is not found
'''
indexes = [index for index, item in enumerate(beacon_config) if item.has_key(label)]
if len(indexes) < 1:
return -1
else:
return indexes[0]
def _remove_list_item(self, beacon_config, label):
'''
Remove an item from a beacon config list
'''
index = self._get_index(beacon_config, label)
del beacon_config[index]
def _update_enabled(self, name, enabled_value):
'''
Update whether an individual beacon is enabled
'''
if isinstance(self.opts['beacons'][name], dict):
# Backwards compatibility
self.opts['beacons'][name]['enabled'] = enabled_value
else:
enabled_index = self._get_index(self.opts['beacons'][name], 'enabled')
if enabled_index >= 0:
self.opts['beacons'][name][enabled_index]['enabled'] = enabled_value
else:
self.opts['beacons'][name].append({'enabled': enabled_value})
def list_beacons(self):
'''
List the beacon items
@ -197,7 +244,7 @@ class Beacon(object):
Enable a beacon
'''
self.opts['beacons'][name]['enabled'] = True
self._update_enabled(name, True)
# Fire the complete event back along with updated list of beacons
evt = salt.utils.event.get_event('minion', opts=self.opts)
@ -210,8 +257,8 @@ class Beacon(object):
'''
Disable a beacon
'''
self.opts['beacons'][name]['enabled'] = False
self._update_enabled(name, False)
# Fire the complete event back along with updated list of beacons
evt = salt.utils.event.get_event('minion', opts=self.opts)

View file

@ -364,6 +364,15 @@ def disable(**kwargs):
return ret
def _get_beacon_config_dict(beacon_config):
beacon_config_dict = {}
if isinstance(beacon_config, list):
map(beacon_config_dict.update, beacon_config)
else:
beacon_config_dict = beacon_config
return beacon_config_dict
def enable_beacon(name, **kwargs):
'''
Enable beacon on the minion
@ -399,7 +408,9 @@ def enable_beacon(name, **kwargs):
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']:
beacon_config_dict = _get_beacon_config_dict(beacons[name])
if 'enabled' in beacon_config_dict and beacon_config_dict['enabled']:
ret['result'] = True
ret['comment'] = 'Enabled beacon {0} on minion.'.format(name)
else:
@ -447,7 +458,9 @@ def disable_beacon(name, **kwargs):
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']:
beacon_config_dict = _get_beacon_config_dict(beacons[name])
if 'enabled' in beacon_config_dict and beacon_config_dict['enabled']:
ret['result'] = True
ret['comment'] = 'Disabled beacon on minion.'
else: