Welcome to pyetcd’s documentation!¶
Contents:
pyetcd¶
Python library to work with Etcd.
- Free software: Apache Software License 2.0
- Documentation: https://pyetcd.readthedocs.io.
Features¶
- Basic key operations
Credits¶
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Installation¶
Stable release¶
To install pyetcd, run this command in your terminal:
$ pip install pyetcd
This is the preferred method to install pyetcd, as it will always install the most recent stable release.
If you don’t have pip installed, this Python installation guide can guide you through the process.
From sources¶
The sources for pyetcd can be downloaded from the Github repo.
You can either clone the public repository:
$ git clone git://github.com/twindb/pyetcd
Or download the tarball:
$ curl -OL https://github.com/twindb/pyetcd/tarball/master
Once you have a copy of the source, you can install it with:
$ python setup.py install
Usage¶
Connect to local etcd instance. Write a key and read it after:
from pyetcd.client import Client
client = Client()
client.write('/message', 'Hello world')
response = client.read('/message')
print(response.node['value'])
Write and read from a remote etcd cluster:
from pyetcd.client import Client
client = Client([
'10.0.1.10',
'10.0.1.11',
'10.0.1.12'
])
client.write('/message', 'Hello world')
response = client.read('/message')
print(response.node['value'])
Watch a key:
from pyetcd.client import Client
client = Client([
'10.0.1.10',
'10.0.1.11',
'10.0.1.12'
])
client.write('/message', 'Hello world')
response = client.read('/message', wait=True)
print(response.node['value'])
pyetcd¶
pyetcd package¶
Submodules¶
pyetcd.client module¶
module to connect to an etcd node and perform low rest API requests.
-
class
pyetcd.client.
Client
(**kwargs)[source]¶ Bases:
object
Etcd Client class.
Parameters: kwargs –
Keyword arguments:
- host (str, list(str), list(tuple)) - etcd node hostname
- or list of hostnames or list of tuples (hostname, port). Default is ‘127.0.0.1’.
- port (int) - TCP port to connect to. Default is 2379.
- srv_domain (str) - Domain name if DNS discovery is used
- version_prefix (str) - API version prefix. Default is ‘v2’.
- allow_reconnect (bool) - If client fails to connect to
- a cluster node connect to the next node in the cluster. Default is True.
- protocol (str) - Protocol to connect to the cluster.
- Default is ‘http’.
Raises: - ClientException – if any errors
- NotImplementedError – if there is an attempt to use unsupported DNS discovery.
-
add_member
(peer_urls)[source]¶ Add a node to the cluster.
Parameters: peer_urls – List of URL for inter-peer communication. For example:
["http://10.0.0.10:2380"]
Returns: Information about the newly added node. Return type: EtcdResult
-
compare_and_delete
(key, prev_value=None, prev_index=None)[source]¶ This command will delete a key only if the client-provided conditions are equal to the current conditions.
Parameters: - key – the key
- prev_value – checks the previous value of the key.
- prev_index – checks the previous modifiedIndex of the key.
Returns: Result of operation.
Return type: Raises: - EtcdException – if etcd responds with error or HTTP error.
- EtcdNodeExist – if any condition fails.
-
compare_and_swap
(key, value, prev_value=None, prev_index=None, prev_exist=None, ttl=None)[source]¶ This command will set the value of a key only if the client-provided conditions are equal to the current conditions.
Parameters: - key – key string
- value – key value
- prev_value – checks the previous value of the key.
- prev_index – checks the previous modifiedIndex of the key.
- prev_exist – checks existence of the key: if prevExist is True, it is an update request; if prevExist is False, it is a create request.
- ttl – set ttl on the key in seconds
Returns: Result of operation.
Return type: Raises: - EtcdException – if etcd responds with error or HTTP error.
- EtcdNodeExist – if condition
prev_exist=False
fails. - EtcdTestFailed – if condition
prev_value='bar'
fails.
-
delete
(key)[source]¶ Delete a key
Parameters: key – Key Returns: Result of operation. Return type: EtcdResult Raises: EtcdException – if etcd responds with error or HTTP error
-
health
¶ Returns: True if the node is healthy Return type: bool
-
mkdir
(directory)[source]¶ Create directory
Parameters: directory – string with directory name Returns: Result of operation. Return type: EtcdResult Raises: EtcdException – if etcd responds with error or HTTP error
-
read
(key, **kwargs)[source]¶ Read key value
Parameters: key – Key Returns: Result of operation. Return type: EtcdResult Raises: EtcdException – if etcd responds with error or HTTP error
-
remove_member
(member_id)[source]¶ Remove a node from the cluster.
Parameters: member_id – etcd identifier of the node. For example, 272e204152
.
-
rmdir
(directory, recursive=False)[source]¶ Delete directory
Parameters: - directory – string with directory name
- recursive – recursively delete directory if not empty
Returns: Result of operation.
Return type: Raises: EtcdException – if etcd responds with error or HTTP error
-
update_ttl
(key, ttl)[source]¶ Update key’s ttl
Parameters: - key – the key
- ttl – new ttl
Returns: Result of operation.
Return type: Raises: - EtcdException – if etcd responds with error or HTTP error.
- EtcdKeyNotFound – if the key doesn’t exist
-
version
()[source]¶ Return Etcd server version
Returns: string with Etcd server version. E.g. ‘2.3.7’ Return type: str
-
version_cluster
()[source]¶ Return Etcd cluster version
Returns: string with Etcd cluster version. E.g. ‘2.3.0’ Return type: str
-
version_server
()[source]¶ Same as .version()
Returns: string with Etcd server version. E.g. ‘2.3.7’ Return type: str
-
write
(key, value, ttl=None)[source]¶ Write value to a key
Parameters: - key – Key
- value – Value
- ttl – Keys in etcd can be set to expire after a specified number of seconds. You can do this by setting a TTL (time to live) on the key.
Returns: Result of operation.
Return type: Raises: EtcdException – if etcd responds with error or HTTP error
Module contents¶
pyetcd is a module to work with etcd cluster
-
exception
pyetcd.
EtcdClientInternal
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeClientInternal (http code 500)
-
exception
pyetcd.
EtcdDirNotEmpty
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeDirNotEmpty (http code 108)
-
exception
pyetcd.
EtcdEmptyResponse
[source]¶ Bases:
pyetcd.EtcdInvalidResponse
Error that raises if response from etcd is empty
-
exception
pyetcd.
EtcdEventIndexCleared
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeEventIndexCleared (http code 401)
-
exception
pyetcd.
EtcdExistingPeerAddr
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeExistingPeerAddr (http code 109)
-
exception
pyetcd.
EtcdIndexNaN
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeIndexNaN (http code 203)
-
exception
pyetcd.
EtcdIndexOrValueRequired
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeIndexOrValueRequired (http code 207)
-
exception
pyetcd.
EtcdIndexValueMutex
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeIndexValueMutex (http code 208)
-
exception
pyetcd.
EtcdInvalidActiveSize
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeInvalidActiveSize (http code 403)
-
exception
pyetcd.
EtcdInvalidField
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeInvalidField (http code 209)
-
exception
pyetcd.
EtcdInvalidForm
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeInvalidForm (http code 210)
-
exception
pyetcd.
EtcdInvalidRemoveDelay
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeInvalidRemoveDelay (http code 404)
-
exception
pyetcd.
EtcdInvalidResponse
[source]¶ Bases:
pyetcd.EtcdException
Error that raises if response from etcd is invalid
-
exception
pyetcd.
EtcdKeyIsPreserved
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeKeyIsPreserved (http code 106)
-
exception
pyetcd.
EtcdKeyNotFound
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeKeyNotFound (http code 100)
-
exception
pyetcd.
EtcdLeaderElect
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeLeaderElect (http code 301)
-
exception
pyetcd.
EtcdNameRequired
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeNameRequired (http code 206)
-
exception
pyetcd.
EtcdNoMorePeer
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeNoMorePeer (http code 103)
-
exception
pyetcd.
EtcdNodeExist
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeNodeExist (http code 105)
-
exception
pyetcd.
EtcdNotDir
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeNotDir (http code 104)
-
exception
pyetcd.
EtcdNotFile
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeNotFile (http code 102)
-
exception
pyetcd.
EtcdPrevValueRequired
[source]¶ Bases:
pyetcd.EtcdException
Error EcodePrevValueRequired (http code 201)
-
exception
pyetcd.
EtcdRaftInternal
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeRaftInternal (http code 300)
-
exception
pyetcd.
EtcdRefreshTTLRequired
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeRefreshTTLRequired (http code 212)
-
exception
pyetcd.
EtcdRefreshValue
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeRefreshValue (http code 211)
-
class
pyetcd.
EtcdResult
(response)[source]¶ Bases:
object
Response from Etcd API.
Parameters: response (requests.Response) – Response from server as
requests.(get|post|put)
returns.Raises: - EtcdException – if response contains non-200 errorCode.
- EtcdInvalidResponse – if payload is invalid.
- EtcdEmptyResponse – if response content from etcd is empty.
-
action
¶ Action type
-
followers
¶ Followers of leader
-
health
¶ name
-
id
¶ etcd identifier of the etcd node.
-
leader
¶ Leader of cluster
-
leaderInfo
¶
-
name
¶ etcd name of the node.
-
node
¶ Node class instance. It holds the current key value.
-
prevNode
¶ Node class instance. It holds the previous key value.
-
recvAppendRequestCnt
¶
-
sendAppendRequestCnt
¶
-
startTime
¶
-
state
¶
-
version_etcdcluster
¶ Version of Etcd cluster
-
version_etcdserver
¶ Version of Etcd server
-
x_etcd_index
¶ current etcd index that represents key modification version.
-
exception
pyetcd.
EtcdRootROnly
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeRootROnly (http code 107)
-
exception
pyetcd.
EtcdStandbyInternal
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeStandbyInternal (http code 402)
-
exception
pyetcd.
EtcdTTLNaN
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeTTLNaN (http code 202)
-
exception
pyetcd.
EtcdTestFailed
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeTestFailed (http code 101)
-
exception
pyetcd.
EtcdTimeoutNaN
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeTimeoutNaN (http code 205)
Bases:
pyetcd.EtcdException
Error EcodeUnauthorized (http code 110)
-
exception
pyetcd.
EtcdValueOrTTLRequired
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeValueOrTTLRequired (http code 204)
-
exception
pyetcd.
EtcdValueRequired
[source]¶ Bases:
pyetcd.EtcdException
Error ecodeValueRequired (http code 200)
-
exception
pyetcd.
EtcdWatcherCleared
[source]¶ Bases:
pyetcd.EtcdException
Error EcodeWatcherCleared (http code 400)
Contributing¶
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions¶
Report Bugs¶
Report bugs at https://github.com/twindb/pyetcd/issues.
If you are reporting a bug, please include:
- Your operating system name and version.
- Any details about your local setup that might be helpful in troubleshooting.
- Detailed steps to reproduce the bug.
Fix Bugs¶
Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.
Implement Features¶
Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is open to whoever wants to implement it.
Write Documentation¶
pyetcd could always use more documentation, whether as part of the official pyetcd docs, in docstrings, or even on the web in blog posts, articles, and such.
Submit Feedback¶
The best way to send feedback is to file an issue at https://github.com/twindb/pyetcd/issues.
If you are proposing a feature:
- Explain in detail how it would work.
- Keep the scope as narrow as possible, to make it easier to implement.
- Remember that this is a volunteer-driven project, and that contributions are welcome :)
Get Started!¶
Ready to contribute? Here’s how to set up pyetcd for local development.
Fork the pyetcd repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/pyetcd.git
Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
$ mkvirtualenv pyetcd $ cd pyetcd/ $ python setup.py develop
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
$ flake8 pyetcd tests $ python setup.py test or py.test $ tox
To get flake8 and tox, just pip install them into your virtualenv.
Commit your changes and push your branch to GitHub:
$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
Before you submit a pull request, check that it meets these guidelines:
- The pull request should include tests.
- If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
- The pull request should work for Python 2.6, 2.7, 3.4 and 3.5, and for PyPy. Check https://travis-ci.org/twindb/pyetcd/pull_requests and make sure that the tests pass for all supported Python versions.
Tips¶
To run a subset of tests:
$ py.test tests/test_client.py
$ py.test tests/test_etcdresult.py
To run individual tests:
$ py.test tests/test_client.py::test_write
Credits¶
Development Lead¶
- TwinDB Development Team <dev@twindb.com>
Contributors¶
None yet. Why not be the first?