salt-stack-piggy-back-encrypted-zmq-event-bus
| rev 33 | rev 34 | ||||
|---|---|---|---|---|---|
| 260 | On the master we could have a 3rd party script subscribed to the special channel | 260 | On the master we could have a 3rd party script subscribed to the special channel | ||
| > | tag and perform further manipulation or persist the output somewhere. | > | tag and perform further manipulation or persist the output somewhere. | ||
| 261 | 261 | ||||
| t | t | 262 | UPDATE: I wrote a <returner that tagged data back to masters zermq bus here <htt | ||
| > | p://russell.ballestrini.net/replace-the-nagios-scheduler-and-nrpe-with-salt-stac | ||||
| > | k/>`_. | ||||
| 263 | |||||
| 262 | 264 | ||||
| 263 | references | 265 | references | ||
| rev 32 | rev 33 | ||||
|---|---|---|---|---|---|
| 267 | 267 | ||||
| 268 | 268 | ||||
| n | n | 269 | related work | ||
| 270 | ================= | ||||
| 269 | 271 | ||||
| n | n | 272 | https://github.com/felskrone/salt-eventsd | ||
| 270 | 273 | ||||
| t | t | 274 | |||
| 275 | |||||
| 276 | |||||
| 277 | |||||
| 278 | |||||
| rev 31 | rev 32 | ||||
|---|---|---|---|---|---|
| 179 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | 179 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | ||
| 180 | 180 | ||||
| n | 181 | From My understanding the minion to master communication could work like this: | n | 181 | From my understanding the minion to master communication could work like this: |
| 182 | 182 | ||||
| 183 | #. A 3rd party script publishes to minion bus | 183 | #. A 3rd party script publishes to minion bus | ||
| n | 184 | #. salt-minion daemon sees the properly formatted "package" event and runs _fire | n | 184 | #. salt-minion daemon sees the properly formatted event tagged 'fire_master' and |
| > | _master | > | runs _fire_master() method | ||
| 185 | #. the "package" event ends up on master bus | 185 | #. the event ends up on master bus | ||
| 186 | #. A 3rd party script subscribes to the tag and gains access to the data | 186 | #. A 3rd party script listens to the master bus and subscribes (filters) on the | ||
| > | nested tag and gains access to the message | ||||
| 187 | 187 | ||||
| n | 188 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | n | 188 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- |
| > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | ||
| > | ms my "package" format is incorrect so it never gets forwarded to the master ... | > | ms my event "package" format is incorrect so it never gets forwarded to the mast | ||
| > | er ... | ||||
| 189 | 189 | ||||
| n | 190 | This is the area in the minion daemon loop that tests if the package/event shoul | n | 190 | This area in the salt-minion daemon loop has conditionals which determine if the |
| > | d be forwarded ('fire_master'): | > | package/event should be forwarded. | ||
| 191 | It seems to look for the 'fire_master' tag: | ||||
| 191 | 192 | ||||
| 192 | * https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_ma | 193 | * https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_ma | ||
| > | ster' tag | > | ster' tag | ||
| 193 | * https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_ma | 194 | * https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_ma | ||
| > | ster' event | > | ster' event | ||
| t | t | 195 | |||
| 196 | |||||
| 194 | 197 | ||||
| 195 | emit_to_minion_bus_forward_to_master_bus.py | 198 | emit_to_minion_bus_forward_to_master_bus.py | ||
| rev 30 | rev 31 | ||||
|---|---|---|---|---|---|
| 224 | ] | 224 | ] | ||
| 225 | 225 | ||||
| t | 226 | # multi event example, supports a list of event ret objects | t | 226 | # multi event example, supports a list of event ret objects |
| 227 | payload = { | 227 | payload = { | ||
| 228 | 'id': opts['id'], | 228 | 'id': opts['id'], | ||
| 229 | 'events': events, | 229 | 'events': events, | ||
| 230 | 'tag': None, | 230 | 'tag': None, | ||
| 231 | 'pretag': None, | 231 | 'pretag': None, | ||
| 232 | 'data': None | 232 | 'data': None | ||
| 233 | } | 233 | } | ||
| 234 | |||||
| 235 | 234 | ||||
| 236 | print (payload) | 235 | print (payload) | ||
| rev 29 | rev 30 | ||||
|---|---|---|---|---|---|
| 188 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | 188 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | ||
| > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | ||
| > | ms my "package" format is incorrect so it never gets forwarded to the master ... | > | ms my "package" format is incorrect so it never gets forwarded to the master ... | ||
| 189 | 189 | ||||
| n | 190 | This is the area in the minion daemon loop that tests if the package/event shoul | n | 190 | This is the area in the minion daemon loop that tests if the package/event shoul |
| > | d be forwarded: | > | d be forwarded ('fire_master'): | ||
| 191 | 191 | ||||
| t | 192 | * https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | t | 192 | * https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | 'fire_ma |
| > | ster' tag | ||||
| 193 | 193 | * https://github.com/saltstack/salt/pull/10890 | possible fix to single 'fire_ma | |||
| > | ster' event | ||||
| 194 | 194 | ||||
| 195 | emit_to_minion_bus_forward_to_master_bus.py | 195 | emit_to_minion_bus_forward_to_master_bus.py | ||
| rev 28 | rev 29 | ||||
|---|---|---|---|---|---|
| 214 | 214 | ||||
| 215 | tag = 'mytag' | 215 | tag = 'mytag' | ||
| n | n | 216 | |||
| 217 | # a list of event ret objects | ||||
| 218 | events = [ | ||||
| 219 | {'message':'a drap in the bucket', 'tag': tag }, | ||||
| 220 | {'message':'a drep in the bucket', 'tag': tag }, | ||||
| 221 | {'message':'a drip in the bucket', 'tag': tag }, | ||||
| 216 | data = {'message':'a drop in the bucket', 'tag': tag } | 222 | {'message':'a drop in the bucket', 'tag': tag }, | ||
| 223 | {'message':'a drup in the bucket', 'tag': tag }, | ||||
| 224 | ] | ||||
| 217 | 225 | ||||
| t | 218 | # multi event example, supports a list of event ret objects | t | 226 | # multi event example, supports a list of event ret objects |
| 219 | payload = { | 227 | payload = { | ||
| 220 | 'id': opts['id'], | 228 | 'id': opts['id'], | ||
| 221 | 'events': [ data ], | 229 | 'events': events, | ||
| 222 | 'tag': None, | 230 | 'tag': None, | ||
| 223 | 'pretag': None, | 231 | 'pretag': None, | ||
| 224 | 'data': None | 232 | 'data': None | ||
| 225 | } | 233 | } | ||
| 234 | |||||
| 226 | 235 | ||||
| 227 | print (payload) | 236 | print (payload) | ||
| rev 27 | rev 28 | ||||
|---|---|---|---|---|---|
| 8 | Requires: | 8 | Requires: | ||
| 9 | 9 | ||||
| t | 10 | * the salt-minion alive and running on the minion host | t | 10 | * the salt-minion daemon alive and running on the minion host |
| 11 | * the salt-master alive and running on the master host | 11 | * the salt-master daemon alive and running on the master host | ||
| 12 | * the 3rd party scripts must be run as the same user as the salt-minion or salt- | 12 | * the 3rd party scripts must execute as the same user who runs the salt-minion o | ||
| > | master | > | r salt-master daemon | ||
| 13 | so the script has the ability to bind to the ZMQ sockets | 13 | because the 3rd party script needs to bind to the ZMQ sockets managed by the d | ||
| > | aemons. | ||||
| 14 | 14 | ||||
| 15 | .. contents:: | 15 | .. contents:: | ||
| rev 26 | rev 27 | ||||
|---|---|---|---|---|---|
| 9 | 9 | ||||
| 10 | * the salt-minion alive and running on the minion host | 10 | * the salt-minion alive and running on the minion host | ||
| t | 11 | * the salt-master alive and running on the master host | t | 11 | * the salt-master alive and running on the master host |
| 12 | * the 3rd party scripts must be run as the same user as the salt-minion or salt- | ||||
| > | master | ||||
| 13 | so the script has the ability to bind to the ZMQ sockets | ||||
| 12 | 14 | ||||
| 13 | .. contents:: | 15 | .. contents:: | ||
| rev 25 | rev 26 | ||||
|---|---|---|---|---|---|
| 191 | 191 | ||||
| 192 | 192 | ||||
| t | t | 193 | emit_to_minion_bus_forward_to_master_bus.py | ||
| 194 | ------------------------------------------------------- | ||||
| 195 | |||||
| 196 | .. code-block:: python | ||||
| 197 | |||||
| 198 | # needed for config to opts processing | ||||
| 199 | import os | ||||
| 200 | import salt.syspaths as syspaths | ||||
| 201 | import salt.config | ||||
| 202 | |||||
| 203 | # get opts from minion config file, this function also looks in drop dir! | ||||
| 204 | opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion')) | ||||
| 205 | |||||
| 206 | # debug to STDOUT | ||||
| 207 | import salt.log | ||||
| 208 | salt.log.setup_console_logger('all') | ||||
| 209 | |||||
| 210 | # event libary for events over ZMQ | ||||
| 211 | import salt.utils.event | ||||
| 212 | |||||
| 213 | tag = 'mytag' | ||||
| 214 | data = {'message':'a drop in the bucket', 'tag': tag } | ||||
| 215 | |||||
| 216 | # multi event example, supports a list of event ret objects | ||||
| 217 | payload = { | ||||
| 218 | 'id': opts['id'], | ||||
| 219 | 'events': [ data ], | ||||
| 220 | 'tag': None, | ||||
| 221 | 'pretag': None, | ||||
| 222 | 'data': None | ||||
| 223 | } | ||||
| 224 | |||||
| 225 | print (payload) | ||||
| 226 | |||||
| 227 | # opts must valid minion ID else it binds to invalid socket | ||||
| 228 | event = salt.utils.event.SaltEvent('minion', **opts) | ||||
| 229 | |||||
| 230 | # Fire event payload with 'fire_master' tag which the | ||||
| 231 | # salt-minion daemon will forward to the master event bus! | ||||
| 232 | event.fire_event(payload, 'fire_master') | ||||
| 233 | |||||
| 193 | ideas | 234 | ideas | ||
| 194 | ============ | 235 | ============ | ||
| rev 24 | rev 25 | ||||
|---|---|---|---|---|---|
| 194 | ============ | 194 | ============ | ||
| 195 | 195 | ||||
| n | 196 | I think it could be neat to build a returner which accepted a ZMQ "channel" tag. | n | 196 | I think it could be neat to build a returner which accepted a ZMQ "channel" tag. |
| > | Then after a remote execution the and ret data could be passed from minion to | > | Then after a remote execution the ret data could be passed from minion to mast | ||
| > | master on a special channel. Then we could have a 3rd party script on the salt | > | er on a special channel. Then we could have a 3rd party script on the salt mast | ||
| > | master that subscribes to this special channel and performs further processing o | > | er that subscribes to this special channel and performs further processing or pr | ||
| > | r presents the data in a neat way. | > | esents the data in a neat way. | ||
| 197 | 197 | ||||
| 198 | for example: | 198 | for example: | ||
| 202 | salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plug | 202 | salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plug | ||
| > | ins/check_procs -w 150 -c 200' | > | ins/check_procs -w 150 -c 200' | ||
| 203 | 203 | ||||
| n | 204 | The minion would see the remote execution command, run the job, get data back, a | n | 204 | The minion would see the remote execution command, run the job, get data back, a |
| > | nd return to master on a special tagged channel. | > | nd return to master on a special channel tag. | ||
| 205 | 205 | ||||
| t | 206 | On the master we could have a 3rd party script subscribed to the special tagged | t | 206 | On the master we could have a 3rd party script subscribed to the special channel |
| > | channel and perform further manipulation or persist the output somewhere. | > | tag and perform further manipulation or persist the output somewhere. | ||
| 207 | 207 | ||||
| 208 | 208 | ||||
| rev 23 | rev 24 | ||||
|---|---|---|---|---|---|
| 186 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | 186 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | ||
| > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | ||
| > | ms my "package" format is incorrect so it never gets forwarded to the master ... | > | ms my "package" format is incorrect so it never gets forwarded to the master ... | ||
| 187 | 187 | ||||
| t | t | 188 | This is the area in the minion daemon loop that tests if the package/event shoul | ||
| > | d be forwarded: | ||||
| 189 | |||||
| 190 | * https://github.com/saltstack/salt/blob/develop/salt/minion.py#L1337 | ||||
| 191 | |||||
| 188 | 192 | ||||
| 189 | ideas | 193 | ideas | ||
| rev 22 | rev 23 | ||||
|---|---|---|---|---|---|
| 179 | From My understanding the minion to master communication could work like this: | 179 | From My understanding the minion to master communication could work like this: | ||
| 180 | 180 | ||||
| t | 181 | .. code-block:: text | t | ||
| 182 | |||||
| 183 | A 3rd party script publishes to minion bus -> | 181 | #. A 3rd party script publishes to minion bus | ||
| 184 | salt-minion daemon sees the properly formatted "package" event and runs _fire_m | 182 | #. salt-minion daemon sees the properly formatted "package" event and runs _fire | ||
| > | aster -> | > | _master | ||
| 185 | the "package" event ends up on master bus -> | 183 | #. the "package" event ends up on master bus | ||
| 186 | A 3rd party script subscribes to the tag and gains access to the data. | 184 | #. A 3rd party script subscribes to the tag and gains access to the data | ||
| 187 | 185 | ||||
| 188 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | 186 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | ||
| > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | ||
| > | ms my "package" format is incorrect so it never gets forwarded to the master ... | > | ms my "package" format is incorrect so it never gets forwarded to the master ... | ||
| rev 21 | rev 22 | ||||
|---|---|---|---|---|---|
| f | 1 | Salt Stack Piggy Back Encrypted ZMQ Event Bus | f | 1 | Salt Stack Piggy Back Encrypted ZMQ Event Bus |
| 2 | ################################################## | 2 | ################################################## | ||
| n | n | 3 | |||
| 3 | Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus. | 4 | Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus. | ||
| 4 | These code examples show how 3rd party scripts on each side may communicate over | 5 | These code examples show how 3rd party scripts on each side may communicate over | ||
| > | > | ||||
| 186 | 187 | ||||
| 187 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | 188 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | ||
| > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | ||
| > | ms my "package" format is incorrect so it never gets forwarded to the master ... | > | ms my "package" format is incorrect so it never gets forwarded to the master ... | ||
| t | t | 189 | |||
| 190 | |||||
| 188 | ideas | 191 | ideas | ||
| 189 | ============ | 192 | ============ | ||
| rev 20 | rev 21 | ||||
|---|---|---|---|---|---|
| 176 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | 176 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | ||
| 177 | 177 | ||||
| n | n | 178 | From My understanding the minion to master communication could work like this: | ||
| 178 | 179 | ||||
| t | t | 180 | .. code-block:: text | ||
| 181 | |||||
| 182 | A 3rd party script publishes to minion bus -> | ||||
| 183 | salt-minion daemon sees the properly formatted "package" event and runs _fire_m | ||||
| > | aster -> | ||||
| 184 | the "package" event ends up on master bus -> | ||||
| 185 | A 3rd party script subscribes to the tag and gains access to the data. | ||||
| 186 | |||||
| 187 | The code in `emit-to-minion-bus.py <http://www.foxhop.net/salt-stack-piggy-back- | ||||
| > | encrypted-zmq-event-bus#emit-to-minion-bus-py>`_ does the first step, but it see | ||||
| > | ms my "package" format is incorrect so it never gets forwarded to the master ... | ||||
| 179 | ideas | 188 | ideas | ||
| 180 | ============ | 189 | ============ | ||
| rev 19 | rev 20 | ||||
|---|---|---|---|---|---|
| 180 | ============ | 180 | ============ | ||
| 181 | 181 | ||||
| t | 182 | I think it could be neat to build a returner which accepted a tag and returned d | t | 182 | I think it could be neat to build a returner which accepted a ZMQ "channel" tag. |
| > | ata (events) from minion to master on a special channel. then we could have a 3 | > | Then after a remote execution the and ret data could be passed from minion to | ||
| > | rd party script on the salt master that subscribes to this special channel and p | > | master on a special channel. Then we could have a 3rd party script on the salt | ||
| > | erforms further processing on the and data presents it in a neat way. | > | master that subscribes to this special channel and performs further processing o | ||
| > | r presents the data in a neat way. | ||||
| 183 | 183 | ||||
| 184 | for example: | 184 | for example: | ||
| rev 18 | rev 19 | ||||
|---|---|---|---|---|---|
| 176 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | 176 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | ||
| 177 | 177 | ||||
| t | t | 178 | |||
| 179 | ideas | ||||
| 180 | ============ | ||||
| 181 | |||||
| 182 | I think it could be neat to build a returner which accepted a tag and returned d | ||||
| > | ata (events) from minion to master on a special channel. then we could have a 3 | ||||
| > | rd party script on the salt master that subscribes to this special channel and p | ||||
| > | erforms further processing on the and data presents it in a neat way. | ||||
| 183 | |||||
| 184 | for example: | ||||
| 185 | |||||
| 186 | .. code-block:: text | ||||
| 187 | |||||
| 188 | salt '*' --return=master_bus --tag=monitoring cmd.run_all '/usr/lib/nagios/plug | ||||
| > | ins/check_procs -w 150 -c 200' | ||||
| 189 | |||||
| 190 | The minion would see the remote execution command, run the job, get data back, a | ||||
| > | nd return to master on a special tagged channel. | ||||
| 191 | |||||
| 192 | On the master we could have a 3rd party script subscribed to the special tagged | ||||
| > | channel and perform further manipulation or persist the output somewhere. | ||||
| 193 | |||||
| 194 | |||||
| 178 | references | 195 | references | ||
| 179 | =============== | 196 | =============== | ||
| rev 17 | rev 18 | ||||
|---|---|---|---|---|---|
| 165 | 165 | ||||
| 166 | 166 | ||||
| t | t | 167 | Fire events between minion and master | ||
| 168 | ==================================================== | ||||
| 169 | |||||
| 170 | There is where I get stuck ... I want to allow minions to fire events which end | ||||
| > | up on the master bus ... | ||||
| 171 | |||||
| 172 | I'm able to do this with `salt-call`, for example: | ||||
| 173 | |||||
| 174 | .. code-block:: python | ||||
| 175 | |||||
| 176 | salt-call event.fire_master '{"data": "message for the master"}' 'mytag' | ||||
| 177 | |||||
| 178 | references | ||||
| 179 | =============== | ||||
| 180 | |||||
| 181 | http://docs.saltstack.com/topics/event/index.html | ||||
| 182 | |||||
| 183 | |||||
| 184 | |||||
| 185 | |||||
| rev 16 | rev 17 | ||||
|---|---|---|---|---|---|
| 16 | ==================================================== | 16 | ==================================================== | ||
| 17 | In this example we write a listener/subscriber script that connects to the minio | 17 | In this example we write a listener/subscriber script that connects to the minio | ||
| > | n bus. | > | n bus. | ||
| n | 18 | We then write a emitter/publisher script that connects to the minion bus and po | n | 18 | We then write a emitter/publisher script that connects to the minion bus and pos |
| > | sts events. | > | ts events. | ||
| 19 | 19 | ||||
| 20 | listen_to_minion_bus.py | 20 | listen_to_minion_bus.py | ||
| 114 | Fire event to master bus and listen to master bus | 114 | Fire event to master bus and listen to master bus | ||
| 115 | ==================================================== | 115 | ==================================================== | ||
| t | 116 | In this example we write a listener/subscriber script that connects to the minio | t | 116 | In this example we write a listener/subscriber script that connects to the maste |
| > | n bus. | > | r bus. | ||
| 117 | We then write a emitter/publisher script that connects to the minion bus and po | 117 | We then write a emitter/publisher script that connects to the master bus and pos | ||
| > | sts events. | > | ts events. | ||
| 118 | 118 | ||||
| 119 | listen_to_master_bus.py | 119 | listen_to_master_bus.py | ||
| rev 15 | rev 16 | ||||
|---|---|---|---|---|---|
| 138 | # generator iterator yields events forever, we filter on tag | 138 | # generator iterator yields events forever, we filter on tag | ||
| 139 | for data in event.iter_events(tag=tag): | 139 | for data in event.iter_events(tag=tag): | ||
| t | 140 | print(data)emit_to_master_bus.py | t | 140 | print(data) |
| 141 | |||||
| 142 | |||||
| 143 | emit_to_master_bus.py | ||||
| 141 | --------------------------------- | 144 | --------------------------------- | ||
| 142 | 145 | ||||
| rev 14 | rev 15 | ||||
|---|---|---|---|---|---|
| 117 | We then write a emitter/publisher script that connects to the minion bus and po | 117 | We then write a emitter/publisher script that connects to the minion bus and po | ||
| > | sts events. | > | sts events. | ||
| 118 | 118 | ||||
| n | 119 | n | |||
| 120 | |||||
| 121 | listen_to_master_bus.py | 119 | listen_to_master_bus.py | ||
| 122 | -------------------------------- | 120 | -------------------------------- | ||
| 140 | # generator iterator yields events forever, we filter on tag | 138 | # generator iterator yields events forever, we filter on tag | ||
| 141 | for data in event.iter_events(tag=tag): | 139 | for data in event.iter_events(tag=tag): | ||
| t | t | 140 | print(data)emit_to_master_bus.py | ||
| 141 | --------------------------------- | ||||
| 142 | |||||
| 143 | .. code-block:: python | ||||
| 144 | |||||
| 145 | # debug logging to STDOUT | ||||
| 146 | import salt.log | ||||
| 147 | salt.log.setup_console_logger('all') | ||||
| 148 | |||||
| 149 | # event libary for events over ZMQ | ||||
| 150 | import salt.utils.event | ||||
| 151 | |||||
| 152 | payload = {'sample-msg': 'this is a test', | ||||
| 153 | 'example': 'this is the same test'} | ||||
| 154 | |||||
| 155 | sock_dir = '/var/run/salt/master' | ||||
| 156 | |||||
| 157 | # create event object, attach to master socket ... | ||||
| 158 | event = salt.utils.event.SaltEvent('master', sock_dir) | ||||
| 159 | |||||
| 160 | # post the event | ||||
| 161 | event.fire_event(payload, 'mytag') | ||||
| 162 | |||||
| 163 | |||||
| rev 13 | rev 14 | ||||
|---|---|---|---|---|---|
| 117 | We then write a emitter/publisher script that connects to the minion bus and po | 117 | We then write a emitter/publisher script that connects to the minion bus and po | ||
| > | sts events. | > | sts events. | ||
| 118 | 118 | ||||
| n | n | 119 | |||
| 120 | |||||
| 119 | listen_to_master_bus.py | 121 | listen_to_master_bus.py | ||
| 120 | -------------------------------- | 122 | -------------------------------- | ||
| 138 | # generator iterator yields events forever, we filter on tag | 140 | # generator iterator yields events forever, we filter on tag | ||
| 139 | for data in event.iter_events(tag=tag): | 141 | for data in event.iter_events(tag=tag): | ||
| t | 140 | print(data)emit_to_master_bus.py | t | ||
| 141 | --------------------------------- | ||||
| 142 | |||||
| 143 | .. code-block:: python | ||||
| 144 | |||||
| 145 | # debug logging to STDOUT | ||||
| 146 | import salt.log | ||||
| 147 | salt.log.setup_console_logger('all') | ||||
| 148 | |||||
| 149 | # event libary for events over ZMQ | ||||
| 150 | import salt.utils.event | ||||
| 151 | |||||
| 152 | payload = {'sample-msg': 'this is a test', | ||||
| 153 | 'example': 'this is the same test'} | ||||
| 154 | |||||
| 155 | sock_dir = '/var/run/salt/master' | ||||
| 156 | |||||
| 157 | # create event object, attach to master socket ... | ||||
| 158 | event = salt.utils.event.SaltEvent('master', sock_dir) | ||||
| 159 | |||||
| 160 | # post the event | ||||
| 161 | event.fire_event(payload, 'mytag') | ||||
| 162 | |||||
| 163 | |||||
| rev 12 | rev 13 | ||||
|---|---|---|---|---|---|
| f | 1 | Salt Stack Piggy Back Encrypted ZMQ Event Bus | f | 1 | Salt Stack Piggy Back Encrypted ZMQ Event Bus |
| 2 | ################################################## | 2 | ################################################## | ||
| n | 3 | n | |||
| 4 | Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus. | 3 | Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus. | ||
| 5 | These code examples show how 3rd party scripts on each side may communicate over | 4 | These code examples show how 3rd party scripts on each side may communicate over | ||
| > | > | ||||
| 16 | Fire event to minion bus and listen to minion bus | 15 | Fire event to minion bus and listen to minion bus | ||
| 17 | ==================================================== | 16 | ==================================================== | ||
| n | 18 | n | |||
| 19 | In this example we write a listener/subscriber script that connects to the minio | 17 | In this example we write a listener/subscriber script that connects to the minio | ||
| > | n bus. | > | n bus. | ||
| 20 | We then write a emitter/publisher script that connects to the minion bus and po | 18 | We then write a emitter/publisher script that connects to the minion bus and po | ||
| > | sts events. | > | sts events. | ||
| 22 | listen_to_minion_bus.py | 20 | listen_to_minion_bus.py | ||
| 23 | ------------------------------ | 21 | ------------------------------ | ||
| n | 24 | n | |||
| 25 | 3rd Party Subscriber / Listener: | 22 | 3rd Party Subscriber / Listener: | ||
| 26 | 23 | ||||
| 55 | emit_to_minion_bus.py | 52 | emit_to_minion_bus.py | ||
| 56 | ------------------------------- | 53 | ------------------------------- | ||
| n | 57 | n | |||
| 58 | 3rd Party Publisher / Emitter: | 54 | 3rd Party Publisher / Emitter: | ||
| 59 | 55 | ||||
| 94 | Notes on the implementation | 90 | Notes on the implementation | ||
| 95 | ---------------------------------- | 91 | ---------------------------------- | ||
| n | 96 | n | |||
| 97 | Notice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitte | 92 | Notice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitte | ||
| > | r? | > | r? | ||
| 98 | The `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent | 93 | The `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent | ||
| > | ` so you can use | > | ` so you can use | ||
| 119 | Fire event to master bus and listen to master bus | 114 | Fire event to master bus and listen to master bus | ||
| 120 | ==================================================== | 115 | ==================================================== | ||
| n | 121 | n | |||
| 122 | In this example we write a listener/subscriber script that connects to the minio | 116 | In this example we write a listener/subscriber script that connects to the minio | ||
| > | n bus. | > | n bus. | ||
| 123 | We then write a emitter/publisher script that connects to the minion bus and po | 117 | We then write a emitter/publisher script that connects to the minion bus and po | ||
| > | sts events. | > | sts events. | ||
| 126 | -------------------------------- | 120 | -------------------------------- | ||
| 127 | 121 | ||||
| t | 128 | emit_to_master_bus.py | t | 122 | .. code-block:: python |
| 123 | |||||
| 124 | # debug logging to STDOUT | ||||
| 125 | import salt.log | ||||
| 126 | salt.log.setup_console_logger('all') | ||||
| 127 | |||||
| 128 | # event libary for events over ZMQ | ||||
| 129 | import salt.utils.event | ||||
| 130 | |||||
| 131 | # create event object, attach to master socket ... | ||||
| 132 | event = salt.utils.event.MasterEvent('/var/run/salt/master') | ||||
| 133 | |||||
| 134 | tag = 'mytag' | ||||
| 135 | |||||
| 136 | print('Listening for events tagged \'{}\' on Salt Master bus.'.format(tag)) | ||||
| 137 | |||||
| 138 | # generator iterator yields events forever, we filter on tag | ||||
| 139 | for data in event.iter_events(tag=tag): | ||||
| 140 | print(data)emit_to_master_bus.py | ||||
| 129 | --------------------------------- | 141 | --------------------------------- | ||
| 130 | 142 | ||||
| rev 11 | rev 12 | ||||
|---|---|---|---|---|---|
| 123 | We then write a emitter/publisher script that connects to the minion bus and po | 123 | We then write a emitter/publisher script that connects to the minion bus and po | ||
| > | sts events. | > | sts events. | ||
| 124 | 124 | ||||
| t | t | 125 | listen_to_master_bus.py | ||
| 126 | -------------------------------- | ||||
| 127 | |||||
| 128 | emit_to_master_bus.py | ||||
| 129 | --------------------------------- | ||||
| 130 | |||||
| 131 | .. code-block:: python | ||||
| 132 | |||||
| 133 | # debug logging to STDOUT | ||||
| 134 | import salt.log | ||||
| 135 | salt.log.setup_console_logger('all') | ||||
| 136 | |||||
| 137 | # event libary for events over ZMQ | ||||
| 138 | import salt.utils.event | ||||
| 139 | |||||
| 140 | payload = {'sample-msg': 'this is a test', | ||||
| 141 | 'example': 'this is the same test'} | ||||
| 142 | |||||
| 143 | sock_dir = '/var/run/salt/master' | ||||
| 144 | |||||
| 145 | # create event object, attach to master socket ... | ||||
| 146 | event = salt.utils.event.SaltEvent('master', sock_dir) | ||||
| 147 | |||||
| 148 | # post the event | ||||
| 149 | event.fire_event(payload, 'mytag') | ||||
| 150 | |||||
| 151 | |||||
| rev 10 | rev 11 | ||||
|---|---|---|---|---|---|
| 117 | opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'} | 117 | opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'} | ||
| 118 | 118 | ||||
| t | t | 119 | Fire event to master bus and listen to master bus | ||
| 120 | ==================================================== | ||||
| 121 | |||||
| 122 | In this example we write a listener/subscriber script that connects to the minio | ||||
| > | n bus. | ||||
| 123 | We then write a emitter/publisher script that connects to the minion bus and po | ||||
| > | sts events. | ||||
| 124 | |||||
| rev 9 | rev 10 | ||||
|---|---|---|---|---|---|
| 10 | * the salt-minion alive and running on the minion host | 10 | * the salt-minion alive and running on the minion host | ||
| 11 | * the salt-master alive and running on the master host | 11 | * the salt-master alive and running on the master host | ||
| t | t | 12 | |||
| 13 | .. contents:: | ||||
| 12 | 14 | ||||
| 13 | 15 | ||||
| rev 8 | rev 9 | ||||
|---|---|---|---|---|---|
| 2 | ################################################## | 2 | ################################################## | ||
| 3 | 3 | ||||
| n | 4 | Salt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each sid | n | 4 | Some code examples that piggy back on Salt Stack's encrypted ZMQ event bus. |
| > | e. | ||||
| 5 | These code examples show how 3rd party scripts on each side may communicate over | ||||
| > | |||||
| 6 | secure and fast transport! | ||||
| 5 | 7 | ||||
| 6 | Requires: | 8 | Requires: | ||
| 8 | * the salt-minion alive and running on the minion host | 10 | * the salt-minion alive and running on the minion host | ||
| 9 | * the salt-master alive and running on the master host | 11 | * the salt-master alive and running on the master host | ||
| t | t | 12 | |||
| 10 | 13 | ||||
| 11 | Fire event to minion bus and listen to minion bus | 14 | Fire event to minion bus and listen to minion bus | ||
| rev 7 | rev 8 | ||||
|---|---|---|---|---|---|
| 11 | Fire event to minion bus and listen to minion bus | 11 | Fire event to minion bus and listen to minion bus | ||
| 12 | ==================================================== | 12 | ==================================================== | ||
| t | t | 13 | |||
| 14 | In this example we write a listener/subscriber script that connects to the minio | ||||
| > | n bus. | ||||
| 15 | We then write a emitter/publisher script that connects to the minion bus and po | ||||
| > | sts events. | ||||
| 13 | 16 | ||||
| 14 | listen_to_minion_bus.py | 17 | listen_to_minion_bus.py | ||
| rev 6 | rev 7 | ||||
|---|---|---|---|---|---|
| 12 | ==================================================== | 12 | ==================================================== | ||
| 13 | 13 | ||||
| n | n | 14 | listen_to_minion_bus.py | ||
| 15 | ------------------------------ | ||||
| 16 | |||||
| 14 | 3rd Party Subscriber / Listener: | 17 | 3rd Party Subscriber / Listener: | ||
| n | 15 | n | |||
| 16 | **listen_to_minion_bus.py** | ||||
| 17 | 18 | ||||
| 18 | .. code-block:: python | 19 | .. code-block:: python | ||
| 44 | print(data) | 45 | print(data) | ||
| 45 | 46 | ||||
| n | 46 | **emit_to_minion_bus.py** | n | 47 | emit_to_minion_bus.py |
| 48 | ------------------------------- | ||||
| 49 | |||||
| 50 | 3rd Party Publisher / Emitter: | ||||
| 47 | 51 | ||||
| 48 | .. code-block:: python | 52 | .. code-block:: python | ||
| 80 | event.fire_event(payload, tag) | 84 | event.fire_event(payload, tag) | ||
| 81 | 85 | ||||
| n | 82 | Notice how I use event.MinionEvent in listener and event.SaltEvent in emitter? | n | 86 | Notes on the implementation |
| > | event.MinionEvent is just a very light weight subclass of event.SaltEvent, you c | ||||
| > | an use either or... | ||||
| 87 | ---------------------------------- | ||||
| 83 | 88 | ||||
| n | n | 89 | Notice how I use `event.MinionEvent` in listener and `event.SaltEvent` in emitte | ||
| > | r? | ||||
| 90 | The `event.MinionEvent` is just a very light weight subclass of `event.SaltEvent | ||||
| > | ` so you can use | ||||
| 91 | either or... | ||||
| 92 | |||||
| 84 | Also the configuration code could be reduced to static varibles, for instance re | 93 | The configuration code could be reduced to static variables, for instance replac | ||
| > | place - | > | e - | ||
| 85 | 94 | ||||
| 86 | .. code-block:: python | 95 | .. code-block:: python | ||
| 96 | with this | 105 | with this | ||
| 97 | 106 | ||||
| t | 98 | t | |||
| 99 | .. code-block:: python | 107 | .. code-block:: python | ||
| 100 | 108 | ||||
| rev 5 | rev 6 | ||||
|---|---|---|---|---|---|
| 80 | event.fire_event(payload, tag) | 80 | event.fire_event(payload, tag) | ||
| 81 | 81 | ||||
| t | t | 82 | Notice how I use event.MinionEvent in listener and event.SaltEvent in emitter? | ||
| > | event.MinionEvent is just a very light weight subclass of event.SaltEvent, you c | ||||
| > | an use either or... | ||||
| 83 | |||||
| 84 | Also the configuration code could be reduced to static varibles, for instance re | ||||
| > | place - | ||||
| 85 | |||||
| 86 | .. code-block:: python | ||||
| 87 | |||||
| 88 | # needed for config to opts processing | ||||
| 89 | import os | ||||
| 90 | import salt.syspaths as syspaths | ||||
| 91 | import salt.config | ||||
| 92 | |||||
| 93 | # get opts from minion config file, this function also looks in drop dir! | ||||
| 94 | opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion')) | ||||
| 95 | |||||
| 96 | with this | ||||
| 97 | |||||
| 98 | |||||
| 99 | .. code-block:: python | ||||
| 100 | |||||
| 101 | opts = {'sock_dir':'/var/run/salt/minion', 'id':'id-of-this-minion'} | ||||
| 102 | |||||
| rev 4 | rev 5 | ||||
|---|---|---|---|---|---|
| 48 | .. code-block:: python | 48 | .. code-block:: python | ||
| 49 | 49 | ||||
| n | 50 | print(1) | n | 50 | # needed for config to opts processing |
| 51 | import os | ||||
| 52 | import salt.syspaths as syspaths | ||||
| 53 | import salt.config | ||||
| 51 | 54 | ||||
| n | n | 55 | # get opts from minion config file, this function also looks in drop dir! | ||
| 56 | opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion')) | ||||
| 52 | 57 | ||||
| t | t | 58 | # debug to STDOUT | ||
| 59 | import salt.log | ||||
| 60 | salt.log.setup_console_logger('all') | ||||
| 61 | |||||
| 62 | # event libary for events over ZMQ | ||||
| 63 | import salt.utils.event | ||||
| 64 | |||||
| 65 | tag = 'mytag' | ||||
| 66 | data = {'message':'a drop in the bucket'} | ||||
| 67 | |||||
| 68 | payload = { | ||||
| 69 | 'id': opts['id'], | ||||
| 70 | 'tag': tag, | ||||
| 71 | 'data': data, | ||||
| 72 | } | ||||
| 73 | |||||
| 74 | print (payload) | ||||
| 75 | |||||
| 76 | # opts must valid minion ID else it binds to invalid socket | ||||
| 77 | event = salt.utils.event.SaltEvent('minion', **opts) | ||||
| 78 | |||||
| 79 | # Fire event payload with tag | ||||
| 80 | event.fire_event(payload, tag) | ||||
| 81 | |||||
| rev 3 | rev 4 | ||||
|---|---|---|---|---|---|
| 9 | * the salt-master alive and running on the master host | 9 | * the salt-master alive and running on the master host | ||
| 10 | 10 | ||||
| n | 11 | Fire event from minion to minion bus | n | 11 | Fire event to minion bus and listen to minion bus |
| 12 | ====================================== | 12 | ==================================================== | ||
| 13 | 13 | ||||
| 14 | 3rd Party Subscriber / Listener: | 14 | 3rd Party Subscriber / Listener: | ||
| 44 | print(data) | 44 | print(data) | ||
| 45 | 45 | ||||
| t | t | 46 | **emit_to_minion_bus.py** | ||
| 47 | |||||
| 48 | .. code-block:: python | ||||
| 49 | |||||
| 50 | print(1) | ||||
| 46 | 51 | ||||
| 47 | 52 | ||||
| rev 2 | rev 3 | ||||
|---|---|---|---|---|---|
| 33 | import salt.utils.event | 33 | import salt.utils.event | ||
| 34 | 34 | ||||
| t | t | 35 | # opts must have the valid minion ID else it binds to invalid socket | ||
| 35 | event = salt.utils.event.MinionEvent(**opts) | 36 | event = salt.utils.event.MinionEvent(**opts) | ||
| 36 | 37 | ||||
| rev 1 | rev 2 | ||||
|---|---|---|---|---|---|
| 39 | print('Listening for events tagged \'{}\' on Salt Minion bus.'.format(tag)) | 39 | print('Listening for events tagged \'{}\' on Salt Minion bus.'.format(tag)) | ||
| 40 | 40 | ||||
| t | 41 | #for data in event.iter_events(tag='nagios'): | t | 41 | # generator iterator yields events forever, we filter on tag |
| 42 | for data in event.iter_events(tag=tag): | 42 | for data in event.iter_events(tag=tag): | ||
| 43 | print(data) | 43 | print(data) | ||
| empty | rev 1 | ||||
|---|---|---|---|---|---|
| t | t | 1 | Salt Stack Piggy Back Encrypted ZMQ Event Bus | ||
| 2 | ################################################## | ||||
| 3 | |||||
| 4 | Salt Stack Piggy Back Encrypted ZMQ Event Bus with 3rd party scripts on each sid | ||||
| > | e. | ||||
| 5 | |||||
| 6 | Requires: | ||||
| 7 | |||||
| 8 | * the salt-minion alive and running on the minion host | ||||
| 9 | * the salt-master alive and running on the master host | ||||
| 10 | |||||
| 11 | Fire event from minion to minion bus | ||||
| 12 | ====================================== | ||||
| 13 | |||||
| 14 | 3rd Party Subscriber / Listener: | ||||
| 15 | |||||
| 16 | **listen_to_minion_bus.py** | ||||
| 17 | |||||
| 18 | .. code-block:: python | ||||
| 19 | |||||
| 20 | # needed for config to opts processing | ||||
| 21 | import os | ||||
| 22 | import salt.syspaths as syspaths | ||||
| 23 | import salt.config | ||||
| 24 | |||||
| 25 | # get opts from minion config file, this function also looks in drop dir! | ||||
| 26 | opts = salt.config.minion_config(os.path.join(syspaths.CONFIG_DIR, 'minion')) | ||||
| 27 | |||||
| 28 | # debug to STDOUT | ||||
| 29 | import salt.log | ||||
| 30 | salt.log.setup_console_logger('all') | ||||
| 31 | |||||
| 32 | # event libary for events over ZMQ | ||||
| 33 | import salt.utils.event | ||||
| 34 | |||||
| 35 | event = salt.utils.event.MinionEvent(**opts) | ||||
| 36 | |||||
| 37 | tag = 'mytag' | ||||
| 38 | |||||
| 39 | print('Listening for events tagged \'{}\' on Salt Minion bus.'.format(tag)) | ||||
| 40 | |||||
| 41 | #for data in event.iter_events(tag='nagios'): | ||||
| 42 | for data in event.iter_events(tag=tag): | ||||
| 43 | print(data) | ||||
| 44 | |||||
| 45 | |||||
| 46 | |||||
Remarkbox