Fix issues in naming and few other things
This commit is contained in:
parent
f919719b1e
commit
71279a968f
21 changed files with 274 additions and 281 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from etcd3_wrapper import EtcdEntry
|
||||
from .etcd_wrapper import EtcdEntry
|
||||
|
||||
|
||||
class SpecificEtcdEntryBase:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from etcd3_wrapper import Etcd3Wrapper
|
||||
from .etcd_wrapper import Etcd3Wrapper
|
||||
|
||||
|
||||
def increment_etcd_counter(etcd_client: Etcd3Wrapper, key):
|
||||
|
|
|
|||
74
ucloud/common/etcd_wrapper.py
Normal file
74
ucloud/common/etcd_wrapper.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import etcd3
|
||||
import json
|
||||
import queue
|
||||
import copy
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
PseudoEtcdMeta = namedtuple("PseudoEtcdMeta", ["key"])
|
||||
|
||||
class EtcdEntry:
|
||||
# key: str
|
||||
# value: str
|
||||
|
||||
def __init__(self, meta, value, value_in_json=False):
|
||||
self.key = meta.key.decode("utf-8")
|
||||
self.value = value.decode("utf-8")
|
||||
|
||||
if value_in_json:
|
||||
self.value = json.loads(self.value)
|
||||
|
||||
class Etcd3Wrapper:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.client = etcd3.client(*args, **kwargs)
|
||||
|
||||
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)
|
||||
|
||||
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):
|
||||
_key = _key.decode("utf-8")
|
||||
|
||||
return self.client.put(_key, _value, **kwargs)
|
||||
|
||||
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
|
||||
|
||||
def watch_prefix(self, key, timeout=0, value_in_json=False):
|
||||
timeout_event = EtcdEntry(PseudoEtcdMeta(key=b"TIMEOUT"),
|
||||
value=str.encode(json.dumps({"status": "TIMEOUT",
|
||||
"type": "TIMEOUT"})),
|
||||
value_in_json=value_in_json)
|
||||
|
||||
event_queue = queue.Queue()
|
||||
|
||||
def add_event_to_queue(event):
|
||||
for e in event.events:
|
||||
if e.value:
|
||||
event_queue.put(EtcdEntry(e, e.value, value_in_json=value_in_json))
|
||||
|
||||
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):
|
||||
super().__init__(PseudoEtcdMeta(key=key.encode("utf-8")), value, value_in_json=value_in_json)
|
||||
|
|
@ -6,21 +6,7 @@ import json
|
|||
from ipaddress import ip_address
|
||||
|
||||
from os.path import join as join_path
|
||||
|
||||
|
||||
def create_package_loggers(packages, base_path, mode="a"):
|
||||
loggers = {}
|
||||
for pkg in packages:
|
||||
logger = logging.getLogger(pkg)
|
||||
logger_handler = logging.FileHandler(
|
||||
join_path(base_path, "{}.txt".format(pkg)),
|
||||
mode=mode
|
||||
)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger_handler.setFormatter(logging.Formatter(fmt="%(asctime)s: %(levelname)s - %(message)s",
|
||||
datefmt="%d-%b-%y %H:%M:%S"))
|
||||
logger.addHandler(logger_handler)
|
||||
loggers[pkg] = logger
|
||||
from . import logger
|
||||
|
||||
|
||||
# TODO: Should be removed as soon as migration
|
||||
|
|
@ -35,7 +21,7 @@ def get_ipv4_address():
|
|||
except socket.timeout:
|
||||
address = "127.0.0.1"
|
||||
except Exception as e:
|
||||
logging.getLogger().exception(e)
|
||||
logger.exception(e)
|
||||
address = "127.0.0.1"
|
||||
else:
|
||||
address = s.getsockname()[0]
|
||||
|
|
@ -49,6 +35,6 @@ def get_ipv6_address():
|
|||
content = json.loads(r.content.decode("utf-8"))
|
||||
ip = ip_address(content["ip"]).exploded
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
logger.exception(e)
|
||||
else:
|
||||
return ip
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@ import json
|
|||
from os.path import join
|
||||
from uuid import uuid4
|
||||
|
||||
from etcd3_wrapper.etcd3_wrapper import PsuedoEtcdEntry
|
||||
|
||||
from .etcd_wrapper import PsuedoEtcdEntry
|
||||
from .classes import SpecificEtcdEntryBase
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue