{"revision": {"id": "f3de22a8-2f95-11f1-9333-e86a64d24d78", "node_id": "f3dd655c-2f95-11f1-ab31-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Salt Stack Piggy Back Encrypted ZMQ Event Bus\r\n##################################################\r\n\r\nSalt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each side.\r\n\r\nRequires:\r\n\r\n* the salt-minion alive and running on the minion host\r\n* the salt-master alive and running on the master host \r\n\r\nFire event to minion bus and listen to minion bus\r\n====================================================\r\n\r\nlisten_to_minion_bus.py\r\n------------------------------\r\n\r\n3rd Party Subscriber / Listener:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n # opts must have the valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.MinionEvent(**opts)\r\n\r\n tag = 'mytag'\r\n\r\n print('Listening for events tagged \\'{}\\' on Salt Minion bus.'.format(tag))\r\n\r\n # generator iterator yields events forever, we filter on tag\r\n for data in event.iter_events(tag=tag):\r\n     print(data)\r\n\r\nemit_to_minion_bus.py\r\n-------------------------------\r\n\r\n3rd Party Publisher / Emitter:\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\n # debug to STDOUT\r\n import salt.log\r\n salt.log.setup_console_logger('all')\r\n\r\n # event libary for events over ZMQ\r\n import salt.utils.event\r\n\r\n tag = 'mytag'\r\n data = {'message':'a drop in the bucket'}\r\n\r\n payload = {\r\n   'id': opts['id'],\r\n   'tag': tag,\r\n   'data': data,\r\n }\r\n \r\n print (payload)\r\n\r\n # opts must valid minion ID else it binds to invalid socket\r\n event = salt.utils.event.SaltEvent('minion', **opts)\r\n\r\n # Fire event payload with tag\r\n event.fire_event(payload, tag)\r\n\r\nNotes on the implementation\r\n----------------------------------\r\n\r\nNotice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitter? \r\nThe `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent` so you can use\r\neither or...\r\n\r\nThe configuration code could be reduced to static variables, for instance replace -\r\n\r\n.. code-block:: python\r\n\r\n # needed for config to opts processing\r\n import os\r\n import salt.syspaths as syspaths\r\n import salt.config\r\n\r\n # get opts from minion config file, this function also looks in drop dir!\r\n opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion'))\r\n\r\nwith this \r\n\r\n.. code-block:: python\r\n\r\n opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'}\r\n\r\n", "source_format": "rst", "revision_number": 7, "created": 1393765159000}}