2019-12-14 15:23:31 +00:00
|
|
|
import etcd3
|
|
|
|
import json
|
|
|
|
import queue
|
|
|
|
import copy
|
|
|
|
|
|
|
|
from collections import namedtuple
|
2019-12-22 07:26:48 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from . import logger
|
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
PseudoEtcdMeta = namedtuple("PseudoEtcdMeta", ["key"])
|
2019-12-14 15:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EtcdEntry:
|
|
|
|
# key: str
|
|
|
|
# value: str
|
|
|
|
|
|
|
|
def __init__(self, meta, value, value_in_json=False):
|
2019-12-30 09:35:07 +00:00
|
|
|
self.key = meta.key.decode("utf-8")
|
|
|
|
self.value = value.decode("utf-8")
|
2019-12-14 15:23:31 +00:00
|
|
|
|
|
|
|
if value_in_json:
|
|
|
|
self.value = json.loads(self.value)
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
|
|
|
|
def readable_errors(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
2019-12-23 07:58:04 +00:00
|
|
|
return func(*args, **kwargs)
|
2019-12-22 07:26:48 +00:00
|
|
|
except etcd3.exceptions.ConnectionFailedError as err:
|
2019-12-30 09:35:07 +00:00
|
|
|
raise etcd3.exceptions.ConnectionFailedError(
|
|
|
|
"etcd connection failed."
|
|
|
|
) from err
|
2019-12-22 07:26:48 +00:00
|
|
|
except etcd3.exceptions.ConnectionTimeoutError as err:
|
2019-12-30 09:35:07 +00:00
|
|
|
raise etcd3.exceptions.ConnectionTimeoutError(
|
|
|
|
"etcd connection timeout."
|
|
|
|
) from err
|
2019-12-22 07:26:48 +00:00
|
|
|
except Exception:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.exception(
|
|
|
|
"Some etcd error occured. See syslog for details."
|
|
|
|
)
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
class Etcd3Wrapper:
|
2019-12-29 18:14:39 +00:00
|
|
|
@readable_errors
|
2019-12-14 15:23:31 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.client = etcd3.client(*args, **kwargs)
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
@readable_errors
|
2019-12-14 15:23:31 +00:00
|
|
|
def get(self, *args, value_in_json=False, **kwargs):
|
|
|
|
_value, _key = self.client.get(*args, **kwargs)
|
|
|
|
if _key is None or _value is None:
|
|
|
|
return None
|
|
|
|
return EtcdEntry(_key, _value, value_in_json=value_in_json)
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
@readable_errors
|
2019-12-14 15:23:31 +00:00
|
|
|
def put(self, *args, value_in_json=False, **kwargs):
|
|
|
|
_key, _value = args
|
|
|
|
if value_in_json:
|
|
|
|
_value = json.dumps(_value)
|
|
|
|
|
|
|
|
if not isinstance(_key, str):
|
2019-12-30 09:35:07 +00:00
|
|
|
_key = _key.decode("utf-8")
|
2019-12-14 15:23:31 +00:00
|
|
|
|
|
|
|
return self.client.put(_key, _value, **kwargs)
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
@readable_errors
|
2019-12-14 15:23:31 +00:00
|
|
|
def get_prefix(self, *args, value_in_json=False, **kwargs):
|
|
|
|
r = self.client.get_prefix(*args, **kwargs)
|
|
|
|
for entry in r:
|
|
|
|
e = EtcdEntry(*entry[::-1], value_in_json=value_in_json)
|
|
|
|
if e.value:
|
|
|
|
yield e
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
@readable_errors
|
2019-12-14 15:23:31 +00:00
|
|
|
def watch_prefix(self, key, timeout=0, value_in_json=False):
|
2019-12-30 09:35:07 +00:00
|
|
|
timeout_event = EtcdEntry(
|
|
|
|
PseudoEtcdMeta(key=b"TIMEOUT"),
|
|
|
|
value=str.encode(
|
|
|
|
json.dumps({"status": "TIMEOUT", "type": "TIMEOUT"})
|
|
|
|
),
|
|
|
|
value_in_json=value_in_json,
|
|
|
|
)
|
2019-12-14 15:23:31 +00:00
|
|
|
|
|
|
|
event_queue = queue.Queue()
|
|
|
|
|
|
|
|
def add_event_to_queue(event):
|
2019-12-30 09:35:07 +00:00
|
|
|
if hasattr(event, "events"):
|
2019-12-29 18:14:39 +00:00
|
|
|
for e in event.events:
|
|
|
|
if e.value:
|
2019-12-30 09:35:07 +00:00
|
|
|
event_queue.put(
|
|
|
|
EtcdEntry(
|
|
|
|
e, e.value, value_in_json=value_in_json
|
|
|
|
)
|
|
|
|
)
|
2019-12-14 15:23:31 +00:00
|
|
|
|
|
|
|
self.client.add_watch_prefix_callback(key, add_event_to_queue)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
v = event_queue.get(timeout=timeout)
|
|
|
|
yield v
|
|
|
|
except queue.Empty:
|
|
|
|
event_queue.put(copy.deepcopy(timeout_event))
|
|
|
|
|
|
|
|
|
|
|
|
class PsuedoEtcdEntry(EtcdEntry):
|
|
|
|
def __init__(self, key, value, value_in_json=False):
|
2019-12-30 09:35:07 +00:00
|
|
|
super().__init__(
|
|
|
|
PseudoEtcdMeta(key=key.encode("utf-8")),
|
|
|
|
value,
|
|
|
|
value_in_json=value_in_json,
|
|
|
|
)
|