From e5df80a3f54da23858c382a8e491ff9901317e1e Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Tue, 20 Aug 2019 18:53:35 +0200 Subject: T1598: initial implementation of the hosts keeper daemon. --- python/vyos/hostsd_client.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 python/vyos/hostsd_client.py (limited to 'python/vyos/hostsd_client.py') diff --git a/python/vyos/hostsd_client.py b/python/vyos/hostsd_client.py new file mode 100644 index 000000000..e02aefe6f --- /dev/null +++ b/python/vyos/hostsd_client.py @@ -0,0 +1,56 @@ +import json + +import zmq + + +SOCKET_PATH = "ipc:///run/vyos-hostsd.sock" + + +class VyOSHostsdError(Exception): + pass + + +class Client(object): + def __init__(self): + context = zmq.Context() + self.__socket = context.socket(zmq.REQ) + self.__socket.RCVTIMEO = 10000 #ms + self.__socket.setsockopt(zmq.LINGER, 0) + self.__socket.connect(SOCKET_PATH) + + def _communicate(self, msg): + request = json.dumps(msg).encode() + self.__socket.send(request) + + reply_msg = self.__socket.recv().decode() + reply = json.loads(reply_msg) + if 'error' in reply: + raise VyOSHostsdError(reply['error']) + + def set_host_name(self, host_name, domain_name, search_domains): + msg = { + 'type': 'host_name', + 'op': 'set', + 'data': { + 'host_name': host_name, + 'domain_name': domain_name, + 'search_domains': search_domains + } + } + self._communicate(msg) + + def add_hosts(self, tag, hosts): + msg = {'type': 'hosts', 'op': 'add', 'tag': tag, 'data': hosts} + self._communicate(msg) + + def delete_hosts(self, tag): + msg = {'type': 'hosts', 'op': 'delete', 'tag': tag} + self._communicate(msg) + + def add_name_servers(self, tag, servers): + msg = {'type': 'name_servers', 'op': 'add', 'tag': tag, 'data': servers} + self._communicate(msg) + + def delete_name_servers(self, tag): + msg = {'type': 'name_servers', 'op': 'delete', 'tag': tag} + self._communicate(msg) -- cgit v1.2.3 From 683835c1f6020172a270aedc7dcea7f09d5eb208 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Wed, 21 Aug 2019 08:46:14 +0200 Subject: T1598: handle the socket timeout exception in vyos.hostsd_client --- python/vyos/hostsd_client.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'python/vyos/hostsd_client.py') diff --git a/python/vyos/hostsd_client.py b/python/vyos/hostsd_client.py index e02aefe6f..e2f05071b 100644 --- a/python/vyos/hostsd_client.py +++ b/python/vyos/hostsd_client.py @@ -12,20 +12,26 @@ class VyOSHostsdError(Exception): class Client(object): def __init__(self): - context = zmq.Context() - self.__socket = context.socket(zmq.REQ) - self.__socket.RCVTIMEO = 10000 #ms - self.__socket.setsockopt(zmq.LINGER, 0) - self.__socket.connect(SOCKET_PATH) + try: + context = zmq.Context() + self.__socket = context.socket(zmq.REQ) + self.__socket.RCVTIMEO = 10000 #ms + self.__socket.setsockopt(zmq.LINGER, 0) + self.__socket.connect(SOCKET_PATH) + except zmq.error.Again: + raise VyOSHostsdError("Could not connect to vyos-hostsd") def _communicate(self, msg): - request = json.dumps(msg).encode() - self.__socket.send(request) - - reply_msg = self.__socket.recv().decode() - reply = json.loads(reply_msg) - if 'error' in reply: - raise VyOSHostsdError(reply['error']) + try: + request = json.dumps(msg).encode() + self.__socket.send(request) + + reply_msg = self.__socket.recv().decode() + reply = json.loads(reply_msg) + if 'error' in reply: + raise VyOSHostsdError(reply['error']) + except zmq.error.Again: + raise VyOSHostsdError("Could not connect to vyos-hostsd") def set_host_name(self, host_name, domain_name, search_domains): msg = { -- cgit v1.2.3 From b812bae9e317f7bcc91371316af28d07345682ba Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Mon, 26 Aug 2019 19:58:04 +0200 Subject: T1598: add a vyos-hostsd operation for retrieving name servers by tag. --- python/vyos/hostsd_client.py | 7 +++++++ src/services/vyos-hostsd | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'python/vyos/hostsd_client.py') diff --git a/python/vyos/hostsd_client.py b/python/vyos/hostsd_client.py index e2f05071b..f009aba98 100644 --- a/python/vyos/hostsd_client.py +++ b/python/vyos/hostsd_client.py @@ -30,6 +30,8 @@ class Client(object): reply = json.loads(reply_msg) if 'error' in reply: raise VyOSHostsdError(reply['error']) + else: + return reply["data"] except zmq.error.Again: raise VyOSHostsdError("Could not connect to vyos-hostsd") @@ -60,3 +62,8 @@ class Client(object): def delete_name_servers(self, tag): msg = {'type': 'name_servers', 'op': 'delete', 'tag': tag} self._communicate(msg) + + def get_name_servers(self, tag): + msg = {'type': 'name_servers', 'op': 'get', 'tag': tag} + return self._communicate(msg) + diff --git a/src/services/vyos-hostsd b/src/services/vyos-hostsd index 1bcb2e1f5..8f70eb4e9 100755 --- a/src/services/vyos-hostsd +++ b/src/services/vyos-hostsd @@ -171,6 +171,14 @@ def set_host_name(state, data): if data['search_domains']: state['search_domains'] = data['search_domains'] +def get_name_servers(state, tag): + ns = [] + data = state['name_servers'] + for n in data: + if data[n]['tag'] == tag: + ns.append(n) + return ns + def get_option(msg, key): if key in msg: return msg[key] @@ -209,6 +217,13 @@ def handle_message(msg_json): set_host_name(STATE, data) else: raise ValueError("Unknown message type {0}".format(_type)) + elif op == 'get': + tag = get_option(msg, 'tag') + if _type == 'name_servers': + result = get_name_servers(STATE, tag) + else: + raise ValueError("Unimplemented") + return result else: raise ValueError("Unknown operation {0}".format(op)) @@ -254,7 +269,8 @@ if __name__ == '__main__': resp = {} try: - handle_message(message) + result = handle_message(message) + resp['data'] = result except ValueError as e: resp['error'] = str(e) except: -- cgit v1.2.3