| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Optional |
| 4 | |
| 5 | from consul.callback import CB |
| 6 | |
| 7 | |
| 8 | class Event: |
| 9 | """ |
| 10 | The event command provides a mechanism to fire a custom user event to |
| 11 | an entire datacenter. These events are opaque to Consul, but they can |
| 12 | be used to build scripting infrastructure to do automated deploys, |
| 13 | restart services, or perform any other orchestration action. |
| 14 | |
| 15 | Unlike most Consul data, which is replicated using consensus, event |
| 16 | data is purely peer-to-peer over gossip. |
| 17 | |
| 18 | This means it is not persisted and does not have a total ordering. In |
| 19 | practice, this means you cannot rely on the order of message delivery. |
| 20 | An advantage however is that events can still be used even in the |
| 21 | absence of server nodes or during an outage.""" |
| 22 | |
| 23 | def __init__(self, agent) -> None: |
| 24 | self.agent = agent |
| 25 | |
| 26 | def fire(self, name: str, body: str = "", node=None, service=None, tag=None, token: str | None = None): |
| 27 | """ |
| 28 | Sends an event to Consul's gossip protocol. |
| 29 | |
| 30 | *name* is the Consul-opaque name of the event. This can be filtered |
| 31 | on in calls to list, below |
| 32 | |
| 33 | *body* is the Consul-opaque body to be delivered with the event. |
| 34 | From the Consul documentation: |
| 35 | The underlying gossip also sets limits on the size of a user |
| 36 | event message. It is hard to give an exact number, as it |
| 37 | depends on various parameters of the event, but the payload |
| 38 | should be kept very small (< 100 bytes²). Specifying too large |
| 39 | of an event will return an error. |
| 40 | |
| 41 | *node*, *service*, and *tag* are regular expressions which remote |
| 42 | agents will filter against to determine if they should store the |
| 43 | event |
| 44 | |
| 45 | *token* is an optional `ACL token`_ to apply to this request. If |
| 46 | the token's policy is not allowed to fire an event of this *name* |
| 47 | an *ACLPermissionDenied* exception will be raised. |
| 48 | """ |
| 49 | assert not name.startswith("/"), "keys should not start with a forward slash" |
| 50 | params = [] |
| 51 | if node is not None: |
| 52 | params.append(("node", node)) |
| 53 | if service is not None: |
| 54 | params.append(("service", service)) |
| 55 | if tag is not None: |
| 56 | params.append(("tag", tag)) |
| 57 | |
| 58 | headers = self.agent.prepare_headers(token) |
| 59 | return self.agent.http.put(CB.json(), f"/v1/event/fire/{name}", params=params, headers=headers, data=body) |
| 60 | |
| 61 | def list(self, name: Optional[str] = None, index=None, wait=None): |
| 62 | """ |
| 63 | Returns a tuple of (*index*, *events*) |
| 64 | Note: Since Consul's event protocol uses gossip, there is no |
| 65 | ordering, and instead index maps to the newest event that |
| 66 | matches the query. |
| 67 | |
| 68 | *name* is the type of events to list, if None, lists all available. |
| 69 | |
| 70 | *index* is the current event Consul index, suitable for making |
| 71 | subsequent calls to wait for changes since this query was last run. |
| 72 | Check https://consul.io/docs/agent/http/event.html#event_list for |
| 73 | more infos about indexes on events. |
| 74 | |
| 75 | *wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 76 | a given index. This parameter is only applied if *index* is also |
| 77 | specified. the wait time by default is 5 minutes. |
| 78 | |
| 79 | Consul agents only buffer the most recent entries. The current |
| 80 | buffer size is 256, but this value could change in the future. |
| 81 | |
| 82 | Each *event* looks like this:: |
| 83 | |
| 84 | { |
| 85 | { |
| 86 | "ID": "b54fe110-7af5-cafc-d1fb-afc8ba432b1c", |
| 87 | "Name": "deploy", |
| 88 | "Payload": "1609030", |
| 89 | "NodeFilter": "", |
| 90 | "ServiceFilter": "", |
| 91 | "TagFilter": "", |
| 92 | "Version": 1, |
| 93 | "LTime": 19 |
| 94 | }, |
| 95 | } |
| 96 | """ |
| 97 | params = [] |
| 98 | if name is not None: |
| 99 | params.append(("name", name)) |
| 100 | if index: |
| 101 | params.append(("index", index)) |
| 102 | if wait: |
| 103 | params.append(("wait", wait)) |
| 104 | return self.agent.http.get(CB.json(index=True, decode="Payload"), "/v1/event/list", params=params) |