Add a special lexer for sphinx/pygments
[ganeti-local] / lib / confd / client.py
index e2e0a13..900b5f7 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2009 Google Inc.
+# Copyright (C) 2009, 2010 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -45,7 +45,7 @@ confirming what you already got.
 
 """
 
-# pylint: disable-msg=E0203
+# pylint: disable=E0203
 
 # E0203: Access to member %r before its definition, since we use
 # objects.py which doesn't explicitely initialise its members
@@ -62,6 +62,7 @@ from ganeti import errors
 from ganeti import confd
 from ganeti import ssconf
 from ganeti import compat
+from ganeti import netutils
 
 
 class ConfdAsyncUDPClient(daemon.AsyncUDPSocket):
@@ -71,14 +72,14 @@ class ConfdAsyncUDPClient(daemon.AsyncUDPSocket):
   implement a non-asyncore based client library.
 
   """
-  def __init__(self, client):
+  def __init__(self, client, family):
     """Constructor for ConfdAsyncUDPClient
 
     @type client: L{ConfdClient}
     @param client: client library, to pass the datagrams to
 
     """
-    daemon.AsyncUDPSocket.__init__(self)
+    daemon.AsyncUDPSocket.__init__(self, family)
     self.client = client
 
   # this method is overriding a daemon.AsyncUDPSocket method
@@ -136,15 +137,16 @@ class ConfdClient:
       raise errors.ProgrammerError("callback must be callable")
 
     self.UpdatePeerList(peers)
+    self._SetPeersAddressFamily()
     self._hmac_key = hmac_key
-    self._socket = ConfdAsyncUDPClient(self)
+    self._socket = ConfdAsyncUDPClient(self, self._family)
     self._callback = callback
     self._confd_port = port
     self._logger = logger
     self._requests = {}
 
     if self._confd_port is None:
-      self._confd_port = utils.GetDaemonPort(constants.CONFD)
+      self._confd_port = netutils.GetDaemonPort(constants.CONFD)
 
   def UpdatePeerList(self, peers):
     """Update the list of peers
@@ -154,7 +156,7 @@ class ConfdClient:
 
     """
     # we are actually called from init, so:
-    # pylint: disable-msg=W0201
+    # pylint: disable=W0201
     if not isinstance(peers, list):
       raise errors.ProgrammerError("peers must be a list")
     # make a copy of peers, since we're going to shuffle the list, later
@@ -169,7 +171,7 @@ class ConfdClient:
     """
     if now is None:
       now = time.time()
-    tstamp = '%d' % now
+    tstamp = "%d" % now
     req = serializer.DumpSignedJson(request.ToDict(), self._hmac_key, tstamp)
     return confd.PackMagic(req)
 
@@ -331,7 +333,7 @@ class ConfdClient:
     elif peer_cnt < 5:
       return peer_cnt - 1
     else:
-      return int(peer_cnt/2) + 1
+      return int(peer_cnt / 2) + 1
 
   def WaitForReply(self, salt, timeout=constants.CONFD_CLIENT_EXPIRE_TIMEOUT):
     """Wait for replies to a given request.
@@ -384,6 +386,18 @@ class ConfdClient:
       else:
         return MISSING
 
+  def _SetPeersAddressFamily(self):
+    if not self._peers:
+      raise errors.ConfdClientError("Peer list empty")
+    try:
+      peer = self._peers[0]
+      self._family = netutils.IPAddress.GetAddressFamily(peer)
+      for peer in self._peers[1:]:
+        if netutils.IPAddress.GetAddressFamily(peer) != self._family:
+          raise errors.ConfdClientError("Peers must be of same address family")
+    except errors.IPAddressError:
+      raise errors.ConfdClientError("Peer address %s invalid" % peer)
+
 
 # UPCALL_REPLY: server reply upcall
 # has all ConfdUpcallPayload fields populated