summaryrefslogtreecommitdiff
path: root/python/vyos
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2019-08-20 18:53:35 +0200
committerDaniil Baturin <daniil@baturin.org>2019-08-27 08:14:20 +0200
commit33672c81a8a1b0a9e5508482a5a765243e21849c (patch)
tree62c80e563329e5f8018c24c662f81f4a929ea258 /python/vyos
parent8c471a772153ae7dc6d3b934c5a96706bc9a6ba5 (diff)
downloadvyos-1x-33672c81a8a1b0a9e5508482a5a765243e21849c.tar.gz
vyos-1x-33672c81a8a1b0a9e5508482a5a765243e21849c.zip
T1598: initial implementation of the hosts keeper daemon.
Conflicts: debian/control
Diffstat (limited to 'python/vyos')
-rw-r--r--python/vyos/hostsd_client.py56
1 files changed, 56 insertions, 0 deletions
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)