2019-12-14 15:23:31 +00:00
|
|
|
import etcd3
|
|
|
|
import json
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
2020-01-08 19:40:05 +00:00
|
|
|
from uncloud import UncloudException
|
|
|
|
from uncloud.common import logger
|
2019-12-14 15:23:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EtcdEntry:
|
2020-01-08 19:40:05 +00:00
|
|
|
def __init__(self, meta_or_key, value, value_in_json=False):
|
|
|
|
if hasattr(meta_or_key, 'key'):
|
|
|
|
# if meta has attr 'key' then get it
|
|
|
|
self.key = meta_or_key.key.decode('utf-8')
|
|
|
|
else:
|
|
|
|
# otherwise meta is the 'key'
|
|
|
|
self.key = meta_or_key
|
|
|
|
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)
|
2020-01-08 19:40:05 +00:00
|
|
|
except etcd3.exceptions.ConnectionFailedError:
|
|
|
|
raise UncloudException('Cannot connect to etcd: is etcd running as configured in uncloud.conf?')
|
2019-12-22 07:26:48 +00:00
|
|
|
except etcd3.exceptions.ConnectionTimeoutError as err:
|
2020-01-08 19:40:05 +00:00
|
|
|
raise etcd3.exceptions.ConnectionTimeoutError('etcd connection timeout.') from err
|
2019-12-22 07:26:48 +00:00
|
|
|
except Exception:
|
2020-01-08 19:40:05 +00:00
|
|
|
logger.exception('Some etcd error occured. See syslog for details.')
|
2019-12-30 09:35:07 +00:00
|
|
|
|
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):
|
2020-01-08 19:40:05 +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
|
2020-01-08 19:40:05 +00:00
|
|
|
def get_prefix(self, *args, value_in_json=False, raise_exception=True, **kwargs):
|
|
|
|
try:
|
|
|
|
event_iterator = self.client.get_prefix(*args, **kwargs)
|
|
|
|
for e in event_iterator:
|
|
|
|
yield EtcdEntry(*e[::-1], value_in_json=value_in_json)
|
2020-01-10 09:10:37 +00:00
|
|
|
except etcd3.exceptions.ConnectionFailedError as e:
|
|
|
|
raise UncloudException("Cannot connect to etcd: {}".format(e))
|
2020-01-08 19:40:05 +00:00
|
|
|
except Exception as err:
|
|
|
|
if raise_exception:
|
|
|
|
raise Exception('Exception in etcd_wrapper.get_prefix') from err
|
|
|
|
else:
|
|
|
|
logger.exception('Error in etcd_wrapper')
|
|
|
|
return iter([])
|
2019-12-14 15:23:31 +00:00
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
@readable_errors
|
2020-01-08 19:40:05 +00:00
|
|
|
def watch_prefix(self, key, raise_exception=True, value_in_json=False):
|
|
|
|
try:
|
|
|
|
event_iterator, cancel = self.client.watch_prefix(key)
|
|
|
|
for e in event_iterator:
|
|
|
|
if hasattr(e, '_event'):
|
|
|
|
e = e._event
|
|
|
|
if e.type == e.PUT:
|
|
|
|
yield EtcdEntry(e.kv.key, e.kv.value, value_in_json=value_in_json)
|
|
|
|
except Exception as err:
|
|
|
|
if raise_exception:
|
|
|
|
raise Exception('Exception in etcd_wrapper.get_prefix') from err
|
|
|
|
else:
|
|
|
|
logger.exception('Error in etcd_wrapper.watch_prefix')
|
|
|
|
try:
|
|
|
|
cancel()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return iter([])
|