From 8ad708291386cf10af03d18b2a3b6b9774962144 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 5 Oct 2015 16:23:58 -0400 Subject: [PATCH 1/4] Add example beacon that works with salt-proxy --- salt/beacons/__init__.py | 2 ++ salt/beacons/proxy_example.py | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 salt/beacons/proxy_example.py diff --git a/salt/beacons/__init__.py b/salt/beacons/__init__.py index 848c9a3a13..ed85781d4e 100644 --- a/salt/beacons/__init__.py +++ b/salt/beacons/__init__.py @@ -73,6 +73,8 @@ class Beacon(object): if 'id' not in data: data['id'] = self.opts['id'] ret.append({'tag': tag, 'data': data}) + else: + log.debug('Unable to process beacon {0}'.format(mod)) return ret def _process_interval(self, mod, interval): diff --git a/salt/beacons/proxy_example.py b/salt/beacons/proxy_example.py new file mode 100644 index 0000000000..ab671a9282 --- /dev/null +++ b/salt/beacons/proxy_example.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +''' +Example beacon to use with salt-proxy + +.. code-block:: yaml + + beacons: + proxy_example: + foo: bar +''' + +# Import Python libs +from __future__ import absolute_import + +# Important: Required for the beacon to load!!! +__proxyenabled__ = ['*'] + +__virtualname__ = 'proxy_example' + +import logging +log = logging.getLogger(__name__) + + +def __virtual__(): + ''' + Trivially let the beacon load for the test example. + For a production beacon we should probably have some expression here. + ''' + return True + + +def validate(config): + ''' + Validate the beacon configuration + ''' + if not isinstance(config, dict): + log.info('Configuration for rest_example beacon must be a dictionary.') + return False + return True + + +def beacon(config): + ''' + Called several times each second + https://docs.saltstack.com/en/latest/topics/beacons/#the-beacon-function + + .. code-block:: yaml + + beacons: + proxy_example: + foo: bar + ''' + # TBD + # Call rest.py and return the result + ret = [{'foo': config['foo']}] + log.info('Called the beacon function for proxy_test beacon') + + return ret From 497f965c33cb7dafaad6233914833de6c65cfd14 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 5 Oct 2015 17:06:44 -0400 Subject: [PATCH 2/4] Comment --- salt/beacons/proxy_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/salt/beacons/proxy_example.py b/salt/beacons/proxy_example.py index ab671a9282..0b0124e2e3 100644 --- a/salt/beacons/proxy_example.py +++ b/salt/beacons/proxy_example.py @@ -12,7 +12,8 @@ Example beacon to use with salt-proxy # Import Python libs from __future__ import absolute_import -# Important: Required for the beacon to load!!! +# Important: If used with salt-proxy +# this is required for the beacon to load!!! __proxyenabled__ = ['*'] __virtualname__ = 'proxy_example' @@ -53,6 +54,5 @@ def beacon(config): # TBD # Call rest.py and return the result ret = [{'foo': config['foo']}] - log.info('Called the beacon function for proxy_test beacon') return ret From 7fef5ea08c88b59513ff4fce59ccfbd7862122d8 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Tue, 6 Oct 2015 13:46:34 -0400 Subject: [PATCH 3/4] Make a call to beacon end point --- salt/beacons/proxy_example.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/salt/beacons/proxy_example.py b/salt/beacons/proxy_example.py index 0b0124e2e3..bb4d444799 100644 --- a/salt/beacons/proxy_example.py +++ b/salt/beacons/proxy_example.py @@ -6,11 +6,15 @@ Example beacon to use with salt-proxy beacons: proxy_example: - foo: bar + endpoint: beacon ''' # Import Python libs from __future__ import absolute_import +import logging + +# Import salt libs +import salt.utils.http # Important: If used with salt-proxy # this is required for the beacon to load!!! @@ -18,7 +22,6 @@ __proxyenabled__ = ['*'] __virtualname__ = 'proxy_example' -import logging log = logging.getLogger(__name__) @@ -49,10 +52,17 @@ def beacon(config): beacons: proxy_example: - foo: bar + endpoint: beacon ''' - # TBD - # Call rest.py and return the result - ret = [{'foo': config['foo']}] - - return ret + # Important!!! + # Although this toy example makes an HTTP call + # to get beacon information + # please be advised that doing CPU or IO intensive + # operations in this method will cause the beacon loop + # to block. + beacon_url = '{}{}'.format(__opts__['proxy']['url'], + config['endpoint']) + r = salt.utils.http.query(beacon_url, + decode_type='json', + decode=True) + return [r['dict']] From cac3da1ffa2131b62438996e128f8813e1832bbc Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Wed, 7 Oct 2015 09:36:08 -0400 Subject: [PATCH 4/4] Fix pylint error --- salt/beacons/proxy_example.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/salt/beacons/proxy_example.py b/salt/beacons/proxy_example.py index bb4d444799..8c3550f3f9 100644 --- a/salt/beacons/proxy_example.py +++ b/salt/beacons/proxy_example.py @@ -60,9 +60,9 @@ def beacon(config): # please be advised that doing CPU or IO intensive # operations in this method will cause the beacon loop # to block. - beacon_url = '{}{}'.format(__opts__['proxy']['url'], - config['endpoint']) - r = salt.utils.http.query(beacon_url, - decode_type='json', - decode=True) - return [r['dict']] + beacon_url = '{0}{1}'.format(__opts__['proxy']['url'], + config['endpoint']) + ret = salt.utils.http.query(beacon_url, + decode_type='json', + decode=True) + return [ret['dict']]