File: index.rst

package info (click to toggle)
python-consul 1.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 480 kB
  • sloc: python: 2,842; makefile: 197
file content (267 lines) | stat: -rw-r--r-- 5,558 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
.. include:: ../README.rst

Clients
-------

This library is designed to be easily adapted for a number of clients.
Particularly asynchronous clients. The following clients are currently
supported.

Standard
~~~~~~~~

This is a standard blocking python client. It isn't particularly useful for
creating server components - but it does serve as a base. It makes use of the
`requests`_ library for http requests.

.. code:: python

    >>> import consul

    >>> c = consul.Consul()

    >>> c.kv.put('foo', 'bar')
    True

    >>> index, data = c.kv.get('foo')
    >>> data['Value']
    'bar'

    # this will block until there's an update or a timeout
    >>> index, data = c.kv.get('foo', index=index)

Vanilla
~~~~~~~

An asynchronous `Vanilla`_ plugin based on this library is available at:
https://github.com/cablehead/vanilla.consul

gevent
~~~~~~

The terribly awful thing about `gevent`_ is that anything that uses the socket
library from the python standard lib, including the `requests`_ library can be
made non-blocking via monkey patching. This means the standard python-consul
client will just work asynchronously with `gevent`_.

asyncio
~~~~~~~

There is a `asyncio`_ (using aiohttp_) client which works with `Python3.4` and
makes use of `asyncio.coroutine`_. The API for this client is identical to
the standard python-consul client except that you need to ``yield from`` the
result of each API call. This client is available in *consul.aio*.

.. code:: python

    import asyncio
    import consul.aio


    loop = asyncio.get_running_loop()

    @asyncio.coroutine
    def go():

        # always better to pass ``loop`` explicitly, but this
        # is not mandatory, you can relay on global event loop
        c = consul.aio.Consul(port=consul_port, loop=loop)

        # set value, same as default api but with ``yield from``
        response = yield from c.kv.put(b'foo', b'bar')
        assert response is True

        # get value
        index, data = yield from c.kv.get(b'foo')
        assert data['Value'] == b'bar'

        # delete value
        response = yield from c.kv.delete(b'foo2')
        assert response is True

    loop.run_until_complete(go())


Tools
-----

Handy tools built on python-consul.

`ianitor`_
~~~~~~~~~~

`ianitor`_ is a doorkeeper for your services discovered using consul. It can
automatically register new services through consul API and manage TTL health
checks.

Example Uses
------------

ACLs
~~~~

.. code:: python

    import consul

    # master_token is a *management* token, for example the *acl_master_token*
    # you started the Consul server with
    master = consul.Consul(token=master_token)

    master.kv.put('foo', 'bar')
    master.kv.put('private/foo', 'bar')

    rules = """
        key "" {
            policy = "read"
        }
        key "private/" {
            policy = "deny"
        }
    """
    token = master.acl.create(rules=rules)

    client = consul.Consul(token=token)

    client.kv.get('foo')          # OK
    client.kv.put('foo', 'bar2')  # raises ACLPermissionDenied

    client.kv.get('private/foo')  # returns None, as though the key doesn't
                                  # exist - slightly unintuitive
    client.kv.put('private/foo', 'bar2')  # raises ACLPermissionDenied

API Documentation
-----------------

Check
~~~~~

.. autoclass:: consul.Check

Check.docker
++++++++++++

.. automethod:: consul.Check.docker

Check.script
++++++++++++

.. automethod:: consul.Check.script

Check.http
++++++++++

.. automethod:: consul.Check.http

Check.tcp
+++++++++

.. automethod:: consul.Check.tcp

Check.ttl
+++++++++

.. automethod:: consul.Check.ttl


Consul
~~~~~~

.. autoclass:: consul.Consul

Consul.acl
++++++++++

.. autoclass:: consul.base::Consul.ACL()
   :members:
   :undoc-members:

Consul.agent
++++++++++++

.. autoclass:: consul.base::Consul.Agent()
   :members:
   :exclude-members: Service

.. autoclass:: consul.base::Consul.Agent.Service()
   :members:

.. autoclass:: consul.base::Consul.Agent.Check()
   :members:

Consul.catalog
++++++++++++++

.. autoclass:: consul.base::Consul.Catalog()
   :members:
   :undoc-members:

Consul.event
++++++++++++

.. autoclass:: consul.base::Consul.Event()
   :members:
   :undoc-members:

Consul.coordinate
+++++++++++++++++

.. autoclass:: consul.base::Consul.Coordinate()
   :members:
   :undoc-members:

Consul.health
+++++++++++++

.. autoclass:: consul.base::Consul.Health()
   :members:
   :undoc-members:
   :exclude-members: Check

Consul.kv
+++++++++

.. autoclass:: consul.base::Consul.KV()
   :members:
   :undoc-members:

Consul.query
++++++++++++

.. autoclass:: consul.base::Consul.Query()
   :members:
   :undoc-members:

Consul.session
++++++++++++++

.. autoclass:: consul.base::Consul.Session()
   :members:
   :undoc-members:

Consul.status
+++++++++++++

.. autoclass:: consul.base::Consul.Status()
   :members:
   :undoc-members:

Consul.txn
++++++++++

.. autoclass:: consul.base::Consul.Txn()
   :members:
   :undoc-members:


.. _ACL Token: http://www.consul.io/docs/internals/acl.html
.. _HCL: https://github.com/hashicorp/hcl/
.. _requests: http://python-requests.org
.. _Vanilla: https://github.com/cablehead/vanilla
.. _gevent: http://www.gevent.org
.. _asyncio.coroutine: https://docs.python.org/3/library/asyncio-task.html#coroutines
.. _aiohttp: https://github.com/KeepSafe/aiohttp
.. _asyncio: https://docs.python.org/3/library/asyncio.html
.. _thread pool: https://docs.python.org/2/library/threading.html

.. _ianitor: https://github.com/ClearcodeHQ/ianitor