Statistics
| Branch: | Tag: | Revision:

root / lib / rpc.py @ 84ad6b78

History | View | Annotate | Download (31.2 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 91c17910 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 3ef3c771 Iustin Pop
"""Inter-node RPC library.
23 a8083063 Iustin Pop

24 a8083063 Iustin Pop
"""
25 a8083063 Iustin Pop
26 b459a848 Andrea Spadaccini
# pylint: disable=C0103,R0201,R0904
27 72737a7f Iustin Pop
# C0103: Invalid name, since call_ are not valid
28 72737a7f Iustin Pop
# R0201: Method could be a function, we keep all rpcs instance methods
29 72737a7f Iustin Pop
# as not to change them back and forth between static/instance methods
30 72737a7f Iustin Pop
# if they need to start using instance attributes
31 72737a7f Iustin Pop
# R0904: Too many public methods
32 a8083063 Iustin Pop
33 58b311ca Iustin Pop
import logging
34 12bce260 Michael Hanselmann
import zlib
35 12bce260 Michael Hanselmann
import base64
36 33231500 Michael Hanselmann
import pycurl
37 33231500 Michael Hanselmann
import threading
38 cbe4a0a5 Dimitris Aragiorgis
import copy
39 a8083063 Iustin Pop
40 a8083063 Iustin Pop
from ganeti import utils
41 a8083063 Iustin Pop
from ganeti import objects
42 ecfe9491 Michael Hanselmann
from ganeti import http
43 7c28c575 Michael Hanselmann
from ganeti import serializer
44 eafd8762 Michael Hanselmann
from ganeti import constants
45 781de953 Iustin Pop
from ganeti import errors
46 a744b676 Manuel Franceschini
from ganeti import netutils
47 eb202c13 Manuel Franceschini
from ganeti import ssconf
48 9a914f7a René Nussbaumer
from ganeti import runtime
49 00267bfe Michael Hanselmann
from ganeti import compat
50 cd40dc53 Michael Hanselmann
from ganeti import rpc_defs
51 80ec9f96 Michael Hanselmann
from ganeti import pathutils
52 cffbbae7 Michael Hanselmann
from ganeti import vcluster
53 a8083063 Iustin Pop
54 200de241 Michael Hanselmann
# Special module generated at build time
55 200de241 Michael Hanselmann
from ganeti import _generated_rpc
56 200de241 Michael Hanselmann
57 fe267188 Iustin Pop
# pylint has a bug here, doesn't see this import
58 b459a848 Andrea Spadaccini
import ganeti.http.client  # pylint: disable=W0611
59 ae88ef45 Michael Hanselmann
60 a8083063 Iustin Pop
61 33231500 Michael Hanselmann
_RPC_CLIENT_HEADERS = [
62 33231500 Michael Hanselmann
  "Content-type: %s" % http.HTTP_APP_JSON,
63 8e29563f Iustin Pop
  "Expect:",
64 33231500 Michael Hanselmann
  ]
65 4331f6cd Michael Hanselmann
66 00267bfe Michael Hanselmann
#: Special value to describe an offline host
67 00267bfe Michael Hanselmann
_OFFLINE = object()
68 00267bfe Michael Hanselmann
69 4331f6cd Michael Hanselmann
70 4331f6cd Michael Hanselmann
def Init():
71 4331f6cd Michael Hanselmann
  """Initializes the module-global HTTP client manager.
72 4331f6cd Michael Hanselmann

73 33231500 Michael Hanselmann
  Must be called before using any RPC function and while exactly one thread is
74 33231500 Michael Hanselmann
  running.
75 4331f6cd Michael Hanselmann

76 4331f6cd Michael Hanselmann
  """
77 33231500 Michael Hanselmann
  # curl_global_init(3) and curl_global_cleanup(3) must be called with only
78 33231500 Michael Hanselmann
  # one thread running. This check is just a safety measure -- it doesn't
79 33231500 Michael Hanselmann
  # cover all cases.
80 33231500 Michael Hanselmann
  assert threading.activeCount() == 1, \
81 33231500 Michael Hanselmann
         "Found more than one active thread when initializing pycURL"
82 4331f6cd Michael Hanselmann
83 33231500 Michael Hanselmann
  logging.info("Using PycURL %s", pycurl.version)
84 8d0a4f99 Michael Hanselmann
85 33231500 Michael Hanselmann
  pycurl.global_init(pycurl.GLOBAL_ALL)
86 4331f6cd Michael Hanselmann
87 4331f6cd Michael Hanselmann
88 4331f6cd Michael Hanselmann
def Shutdown():
89 4331f6cd Michael Hanselmann
  """Stops the module-global HTTP client manager.
90 4331f6cd Michael Hanselmann

91 33231500 Michael Hanselmann
  Must be called before quitting the program and while exactly one thread is
92 33231500 Michael Hanselmann
  running.
93 4331f6cd Michael Hanselmann

94 4331f6cd Michael Hanselmann
  """
95 33231500 Michael Hanselmann
  pycurl.global_cleanup()
96 33231500 Michael Hanselmann
97 33231500 Michael Hanselmann
98 33231500 Michael Hanselmann
def _ConfigRpcCurl(curl):
99 80ec9f96 Michael Hanselmann
  noded_cert = str(pathutils.NODED_CERT_FILE)
100 4331f6cd Michael Hanselmann
101 33231500 Michael Hanselmann
  curl.setopt(pycurl.FOLLOWLOCATION, False)
102 33231500 Michael Hanselmann
  curl.setopt(pycurl.CAINFO, noded_cert)
103 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSL_VERIFYHOST, 0)
104 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSL_VERIFYPEER, True)
105 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLCERTTYPE, "PEM")
106 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLCERT, noded_cert)
107 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLKEYTYPE, "PEM")
108 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLKEY, noded_cert)
109 2ff587d4 Agata Murawska
  curl.setopt(pycurl.CONNECTTIMEOUT, constants.RPC_CONNECT_TIMEOUT)
110 33231500 Michael Hanselmann
111 33231500 Michael Hanselmann
112 e0e916fe Iustin Pop
def RunWithRPC(fn):
113 e0e916fe Iustin Pop
  """RPC-wrapper decorator.
114 e0e916fe Iustin Pop

115 e0e916fe Iustin Pop
  When applied to a function, it runs it with the RPC system
116 e0e916fe Iustin Pop
  initialized, and it shutsdown the system afterwards. This means the
117 e0e916fe Iustin Pop
  function must be called without RPC being initialized.
118 e0e916fe Iustin Pop

119 e0e916fe Iustin Pop
  """
120 e0e916fe Iustin Pop
  def wrapper(*args, **kwargs):
121 e0e916fe Iustin Pop
    Init()
122 e0e916fe Iustin Pop
    try:
123 e0e916fe Iustin Pop
      return fn(*args, **kwargs)
124 e0e916fe Iustin Pop
    finally:
125 e0e916fe Iustin Pop
      Shutdown()
126 e0e916fe Iustin Pop
  return wrapper
127 e0e916fe Iustin Pop
128 e0e916fe Iustin Pop
129 0c3d9c7c Thomas Thrainer
def _Compress(_, data):
130 30474135 Michael Hanselmann
  """Compresses a string for transport over RPC.
131 30474135 Michael Hanselmann

132 30474135 Michael Hanselmann
  Small amounts of data are not compressed.
133 30474135 Michael Hanselmann

134 30474135 Michael Hanselmann
  @type data: str
135 30474135 Michael Hanselmann
  @param data: Data
136 30474135 Michael Hanselmann
  @rtype: tuple
137 30474135 Michael Hanselmann
  @return: Encoded data to send
138 30474135 Michael Hanselmann

139 30474135 Michael Hanselmann
  """
140 30474135 Michael Hanselmann
  # Small amounts of data are not compressed
141 30474135 Michael Hanselmann
  if len(data) < 512:
142 30474135 Michael Hanselmann
    return (constants.RPC_ENCODING_NONE, data)
143 30474135 Michael Hanselmann
144 30474135 Michael Hanselmann
  # Compress with zlib and encode in base64
145 30474135 Michael Hanselmann
  return (constants.RPC_ENCODING_ZLIB_BASE64,
146 30474135 Michael Hanselmann
          base64.b64encode(zlib.compress(data, 3)))
147 30474135 Michael Hanselmann
148 30474135 Michael Hanselmann
149 781de953 Iustin Pop
class RpcResult(object):
150 781de953 Iustin Pop
  """RPC Result class.
151 781de953 Iustin Pop

152 781de953 Iustin Pop
  This class holds an RPC result. It is needed since in multi-node
153 05325a35 Bernardo Dal Seno
  calls we can't raise an exception just because one out of many
154 781de953 Iustin Pop
  failed, and therefore we use this class to encapsulate the result.
155 781de953 Iustin Pop

156 5bbd3f7f Michael Hanselmann
  @ivar data: the data payload, for successful results, or None
157 ed83f5cc Iustin Pop
  @ivar call: the name of the RPC call
158 ed83f5cc Iustin Pop
  @ivar node: the name of the node to which we made the call
159 ed83f5cc Iustin Pop
  @ivar offline: whether the operation failed because the node was
160 ed83f5cc Iustin Pop
      offline, as opposed to actual failure; offline=True will always
161 ed83f5cc Iustin Pop
      imply failed=True, in order to allow simpler checking if
162 ed83f5cc Iustin Pop
      the user doesn't care about the exact failure mode
163 4c4e4e1e Iustin Pop
  @ivar fail_msg: the error message if the call failed
164 ed83f5cc Iustin Pop

165 781de953 Iustin Pop
  """
166 ed83f5cc Iustin Pop
  def __init__(self, data=None, failed=False, offline=False,
167 ed83f5cc Iustin Pop
               call=None, node=None):
168 ed83f5cc Iustin Pop
    self.offline = offline
169 ed83f5cc Iustin Pop
    self.call = call
170 ed83f5cc Iustin Pop
    self.node = node
171 1645d22d Michael Hanselmann
172 ed83f5cc Iustin Pop
    if offline:
173 4c4e4e1e Iustin Pop
      self.fail_msg = "Node is marked offline"
174 f2def43a Iustin Pop
      self.data = self.payload = None
175 ed83f5cc Iustin Pop
    elif failed:
176 4c4e4e1e Iustin Pop
      self.fail_msg = self._EnsureErr(data)
177 f2def43a Iustin Pop
      self.data = self.payload = None
178 781de953 Iustin Pop
    else:
179 781de953 Iustin Pop
      self.data = data
180 d3c8b360 Iustin Pop
      if not isinstance(self.data, (tuple, list)):
181 4c4e4e1e Iustin Pop
        self.fail_msg = ("RPC layer error: invalid result type (%s)" %
182 4c4e4e1e Iustin Pop
                         type(self.data))
183 1645d22d Michael Hanselmann
        self.payload = None
184 d3c8b360 Iustin Pop
      elif len(data) != 2:
185 4c4e4e1e Iustin Pop
        self.fail_msg = ("RPC layer error: invalid result length (%d), "
186 4c4e4e1e Iustin Pop
                         "expected 2" % len(self.data))
187 1645d22d Michael Hanselmann
        self.payload = None
188 d3c8b360 Iustin Pop
      elif not self.data[0]:
189 4c4e4e1e Iustin Pop
        self.fail_msg = self._EnsureErr(self.data[1])
190 1645d22d Michael Hanselmann
        self.payload = None
191 f2def43a Iustin Pop
      else:
192 d3c8b360 Iustin Pop
        # finally success
193 4c4e4e1e Iustin Pop
        self.fail_msg = None
194 d3c8b360 Iustin Pop
        self.payload = data[1]
195 d3c8b360 Iustin Pop
196 2c0f74f2 Iustin Pop
    for attr_name in ["call", "data", "fail_msg",
197 2c0f74f2 Iustin Pop
                      "node", "offline", "payload"]:
198 2c0f74f2 Iustin Pop
      assert hasattr(self, attr_name), "Missing attribute %s" % attr_name
199 1645d22d Michael Hanselmann
200 d3c8b360 Iustin Pop
  @staticmethod
201 d3c8b360 Iustin Pop
  def _EnsureErr(val):
202 d3c8b360 Iustin Pop
    """Helper to ensure we return a 'True' value for error."""
203 d3c8b360 Iustin Pop
    if val:
204 d3c8b360 Iustin Pop
      return val
205 d3c8b360 Iustin Pop
    else:
206 d3c8b360 Iustin Pop
      return "No error information"
207 781de953 Iustin Pop
208 045dd6d9 Iustin Pop
  def Raise(self, msg, prereq=False, ecode=None):
209 781de953 Iustin Pop
    """If the result has failed, raise an OpExecError.
210 781de953 Iustin Pop

211 781de953 Iustin Pop
    This is used so that LU code doesn't have to check for each
212 781de953 Iustin Pop
    result, but instead can call this function.
213 781de953 Iustin Pop

214 781de953 Iustin Pop
    """
215 4c4e4e1e Iustin Pop
    if not self.fail_msg:
216 4c4e4e1e Iustin Pop
      return
217 4c4e4e1e Iustin Pop
218 4c4e4e1e Iustin Pop
    if not msg: # one could pass None for default message
219 4c4e4e1e Iustin Pop
      msg = ("Call '%s' to node '%s' has failed: %s" %
220 4c4e4e1e Iustin Pop
             (self.call, self.node, self.fail_msg))
221 4c4e4e1e Iustin Pop
    else:
222 4c4e4e1e Iustin Pop
      msg = "%s: %s" % (msg, self.fail_msg)
223 4c4e4e1e Iustin Pop
    if prereq:
224 4c4e4e1e Iustin Pop
      ec = errors.OpPrereqError
225 4c4e4e1e Iustin Pop
    else:
226 4c4e4e1e Iustin Pop
      ec = errors.OpExecError
227 045dd6d9 Iustin Pop
    if ecode is not None:
228 27137e55 Iustin Pop
      args = (msg, ecode)
229 045dd6d9 Iustin Pop
    else:
230 045dd6d9 Iustin Pop
      args = (msg, )
231 b459a848 Andrea Spadaccini
    raise ec(*args) # pylint: disable=W0142
232 781de953 Iustin Pop
233 95e7e85e Klaus Aehlig
  def Warn(self, msg, feedback_fn):
234 95e7e85e Klaus Aehlig
    """If the result has failed, call the feedback_fn.
235 95e7e85e Klaus Aehlig

236 95e7e85e Klaus Aehlig
    This is used to in cases were LU wants to warn the
237 95e7e85e Klaus Aehlig
    user about a failure, but continue anyway.
238 95e7e85e Klaus Aehlig

239 95e7e85e Klaus Aehlig
    """
240 95e7e85e Klaus Aehlig
    if not self.fail_msg:
241 95e7e85e Klaus Aehlig
      return
242 95e7e85e Klaus Aehlig
243 95e7e85e Klaus Aehlig
    msg = "%s: %s" % (msg, self.fail_msg)
244 95e7e85e Klaus Aehlig
    feedback_fn(msg)
245 95e7e85e Klaus Aehlig
246 781de953 Iustin Pop
247 bd6d1202 René Nussbaumer
def _SsconfResolver(ssconf_ips, node_list, _,
248 00267bfe Michael Hanselmann
                    ssc=ssconf.SimpleStore,
249 00267bfe Michael Hanselmann
                    nslookup_fn=netutils.Hostname.GetIP):
250 eb202c13 Manuel Franceschini
  """Return addresses for given node names.
251 eb202c13 Manuel Franceschini

252 bd6d1202 René Nussbaumer
  @type ssconf_ips: bool
253 bd6d1202 René Nussbaumer
  @param ssconf_ips: Use the ssconf IPs
254 eb202c13 Manuel Franceschini
  @type node_list: list
255 eb202c13 Manuel Franceschini
  @param node_list: List of node names
256 eb202c13 Manuel Franceschini
  @type ssc: class
257 eb202c13 Manuel Franceschini
  @param ssc: SimpleStore class that is used to obtain node->ip mappings
258 17f7fd27 Manuel Franceschini
  @type nslookup_fn: callable
259 17f7fd27 Manuel Franceschini
  @param nslookup_fn: function use to do NS lookup
260 00267bfe Michael Hanselmann
  @rtype: list of tuple; (string, string)
261 00267bfe Michael Hanselmann
  @return: List of tuples containing node name and IP address
262 eb202c13 Manuel Franceschini

263 eb202c13 Manuel Franceschini
  """
264 b43dcc5a Manuel Franceschini
  ss = ssc()
265 b43dcc5a Manuel Franceschini
  family = ss.GetPrimaryIPFamily()
266 bd6d1202 René Nussbaumer
267 bd6d1202 René Nussbaumer
  if ssconf_ips:
268 bd6d1202 René Nussbaumer
    iplist = ss.GetNodePrimaryIPList()
269 bd6d1202 René Nussbaumer
    ipmap = dict(entry.split() for entry in iplist)
270 bd6d1202 René Nussbaumer
  else:
271 bd6d1202 René Nussbaumer
    ipmap = {}
272 00267bfe Michael Hanselmann
273 00267bfe Michael Hanselmann
  result = []
274 b705c7a6 Manuel Franceschini
  for node in node_list:
275 00267bfe Michael Hanselmann
    ip = ipmap.get(node)
276 00267bfe Michael Hanselmann
    if ip is None:
277 00267bfe Michael Hanselmann
      ip = nslookup_fn(node, family=family)
278 1c3231aa Thomas Thrainer
    result.append((node, ip, node))
279 00267bfe Michael Hanselmann
280 00267bfe Michael Hanselmann
  return result
281 00267bfe Michael Hanselmann
282 00267bfe Michael Hanselmann
283 00267bfe Michael Hanselmann
class _StaticResolver:
284 00267bfe Michael Hanselmann
  def __init__(self, addresses):
285 00267bfe Michael Hanselmann
    """Initializes this class.
286 00267bfe Michael Hanselmann

287 00267bfe Michael Hanselmann
    """
288 00267bfe Michael Hanselmann
    self._addresses = addresses
289 00267bfe Michael Hanselmann
290 fce5efd1 Michael Hanselmann
  def __call__(self, hosts, _):
291 00267bfe Michael Hanselmann
    """Returns static addresses for hosts.
292 00267bfe Michael Hanselmann

293 00267bfe Michael Hanselmann
    """
294 00267bfe Michael Hanselmann
    assert len(hosts) == len(self._addresses)
295 1c3231aa Thomas Thrainer
    return zip(hosts, self._addresses, hosts)
296 00267bfe Michael Hanselmann
297 eb202c13 Manuel Franceschini
298 1c3231aa Thomas Thrainer
def _CheckConfigNode(node_uuid_or_name, node, accept_offline_node):
299 00267bfe Michael Hanselmann
  """Checks if a node is online.
300 eb202c13 Manuel Franceschini

301 1c3231aa Thomas Thrainer
  @type node_uuid_or_name: string
302 1c3231aa Thomas Thrainer
  @param node_uuid_or_name: Node UUID
303 00267bfe Michael Hanselmann
  @type node: L{objects.Node} or None
304 00267bfe Michael Hanselmann
  @param node: Node object
305 eb202c13 Manuel Franceschini

306 00267bfe Michael Hanselmann
  """
307 00267bfe Michael Hanselmann
  if node is None:
308 1c3231aa Thomas Thrainer
    # Assume that the passed parameter was actually a node name, so depend on
309 1c3231aa Thomas Thrainer
    # DNS for name resolution
310 1c3231aa Thomas Thrainer
    return (node_uuid_or_name, node_uuid_or_name, node_uuid_or_name)
311 00267bfe Michael Hanselmann
  else:
312 1c3231aa Thomas Thrainer
    if node.offline and not accept_offline_node:
313 1c3231aa Thomas Thrainer
      ip = _OFFLINE
314 1c3231aa Thomas Thrainer
    else:
315 1c3231aa Thomas Thrainer
      ip = node.primary_ip
316 1c3231aa Thomas Thrainer
    return (node.name, ip, node_uuid_or_name)
317 a8083063 Iustin Pop
318 a8083063 Iustin Pop
319 1c3231aa Thomas Thrainer
def _NodeConfigResolver(single_node_fn, all_nodes_fn, node_uuids, opts):
320 00267bfe Michael Hanselmann
  """Calculate node addresses using configuration.
321 a8083063 Iustin Pop

322 1c3231aa Thomas Thrainer
  Note that strings in node_uuids are treated as node names if the UUID is not
323 1c3231aa Thomas Thrainer
  found in the configuration.
324 1c3231aa Thomas Thrainer

325 a8083063 Iustin Pop
  """
326 890ea4ce Michael Hanselmann
  accept_offline_node = (opts is rpc_defs.ACCEPT_OFFLINE_NODE)
327 890ea4ce Michael Hanselmann
328 890ea4ce Michael Hanselmann
  assert accept_offline_node or opts is None, "Unknown option"
329 890ea4ce Michael Hanselmann
330 00267bfe Michael Hanselmann
  # Special case for single-host lookups
331 1c3231aa Thomas Thrainer
  if len(node_uuids) == 1:
332 1c3231aa Thomas Thrainer
    (uuid, ) = node_uuids
333 1c3231aa Thomas Thrainer
    return [_CheckConfigNode(uuid, single_node_fn(uuid), accept_offline_node)]
334 00267bfe Michael Hanselmann
  else:
335 00267bfe Michael Hanselmann
    all_nodes = all_nodes_fn()
336 1c3231aa Thomas Thrainer
    return [_CheckConfigNode(uuid, all_nodes.get(uuid, None),
337 890ea4ce Michael Hanselmann
                             accept_offline_node)
338 1c3231aa Thomas Thrainer
            for uuid in node_uuids]
339 00267bfe Michael Hanselmann
340 00267bfe Michael Hanselmann
341 00267bfe Michael Hanselmann
class _RpcProcessor:
342 aea5caef Michael Hanselmann
  def __init__(self, resolver, port, lock_monitor_cb=None):
343 00267bfe Michael Hanselmann
    """Initializes this class.
344 00267bfe Michael Hanselmann

345 1c3231aa Thomas Thrainer
    @param resolver: callable accepting a list of node UUIDs or hostnames,
346 1c3231aa Thomas Thrainer
      returning a list of tuples containing name, IP address and original name
347 1c3231aa Thomas Thrainer
      of the resolved node. IP address can be the name or the special value
348 1c3231aa Thomas Thrainer
      L{_OFFLINE} to mark offline machines.
349 00267bfe Michael Hanselmann
    @type port: int
350 00267bfe Michael Hanselmann
    @param port: TCP port
351 aea5caef Michael Hanselmann
    @param lock_monitor_cb: Callable for registering with lock monitor
352 3ef3c771 Iustin Pop

353 a8083063 Iustin Pop
    """
354 00267bfe Michael Hanselmann
    self._resolver = resolver
355 00267bfe Michael Hanselmann
    self._port = port
356 aea5caef Michael Hanselmann
    self._lock_monitor_cb = lock_monitor_cb
357 eb202c13 Manuel Franceschini
358 00267bfe Michael Hanselmann
  @staticmethod
359 00267bfe Michael Hanselmann
  def _PrepareRequests(hosts, port, procedure, body, read_timeout):
360 00267bfe Michael Hanselmann
    """Prepares requests by sorting offline hosts into separate list.
361 eb202c13 Manuel Franceschini

362 d9de612c Iustin Pop
    @type body: dict
363 d9de612c Iustin Pop
    @param body: a dictionary with per-host body data
364 d9de612c Iustin Pop

365 00267bfe Michael Hanselmann
    """
366 00267bfe Michael Hanselmann
    results = {}
367 00267bfe Michael Hanselmann
    requests = {}
368 bdf7d8c0 Iustin Pop
369 d9de612c Iustin Pop
    assert isinstance(body, dict)
370 d9de612c Iustin Pop
    assert len(body) == len(hosts)
371 d9de612c Iustin Pop
    assert compat.all(isinstance(v, str) for v in body.values())
372 1c3231aa Thomas Thrainer
    assert frozenset(map(lambda x: x[2], hosts)) == frozenset(body.keys()), \
373 d9de612c Iustin Pop
        "%s != %s" % (hosts, body.keys())
374 d9de612c Iustin Pop
375 1c3231aa Thomas Thrainer
    for (name, ip, original_name) in hosts:
376 00267bfe Michael Hanselmann
      if ip is _OFFLINE:
377 00267bfe Michael Hanselmann
        # Node is marked as offline
378 1c3231aa Thomas Thrainer
        results[original_name] = RpcResult(node=name,
379 1c3231aa Thomas Thrainer
                                           offline=True,
380 1c3231aa Thomas Thrainer
                                           call=procedure)
381 00267bfe Michael Hanselmann
      else:
382 1c3231aa Thomas Thrainer
        requests[original_name] = \
383 00267bfe Michael Hanselmann
          http.client.HttpClientRequest(str(ip), port,
384 7530364d Iustin Pop
                                        http.HTTP_POST, str("/%s" % procedure),
385 00267bfe Michael Hanselmann
                                        headers=_RPC_CLIENT_HEADERS,
386 1c3231aa Thomas Thrainer
                                        post_data=body[original_name],
387 7cb2d205 Michael Hanselmann
                                        read_timeout=read_timeout,
388 abbf2cd9 Michael Hanselmann
                                        nicename="%s/%s" % (name, procedure),
389 abbf2cd9 Michael Hanselmann
                                        curl_config_fn=_ConfigRpcCurl)
390 a8083063 Iustin Pop
391 00267bfe Michael Hanselmann
    return (results, requests)
392 00267bfe Michael Hanselmann
393 00267bfe Michael Hanselmann
  @staticmethod
394 00267bfe Michael Hanselmann
  def _CombineResults(results, requests, procedure):
395 00267bfe Michael Hanselmann
    """Combines pre-computed results for offline hosts with actual call results.
396 bdf7d8c0 Iustin Pop

397 a8083063 Iustin Pop
    """
398 00267bfe Michael Hanselmann
    for name, req in requests.items():
399 00267bfe Michael Hanselmann
      if req.success and req.resp_status_code == http.HTTP_OK:
400 00267bfe Michael Hanselmann
        host_result = RpcResult(data=serializer.LoadJson(req.resp_body),
401 00267bfe Michael Hanselmann
                                node=name, call=procedure)
402 00267bfe Michael Hanselmann
      else:
403 00267bfe Michael Hanselmann
        # TODO: Better error reporting
404 00267bfe Michael Hanselmann
        if req.error:
405 00267bfe Michael Hanselmann
          msg = req.error
406 00267bfe Michael Hanselmann
        else:
407 00267bfe Michael Hanselmann
          msg = req.resp_body
408 eb202c13 Manuel Franceschini
409 00267bfe Michael Hanselmann
        logging.error("RPC error in %s on node %s: %s", procedure, name, msg)
410 00267bfe Michael Hanselmann
        host_result = RpcResult(data=msg, failed=True, node=name,
411 00267bfe Michael Hanselmann
                                call=procedure)
412 ecfe9491 Michael Hanselmann
413 00267bfe Michael Hanselmann
      results[name] = host_result
414 92fd2250 Iustin Pop
415 00267bfe Michael Hanselmann
    return results
416 a8083063 Iustin Pop
417 1c3231aa Thomas Thrainer
  def __call__(self, nodes, procedure, body, read_timeout, resolver_opts,
418 065be3f0 Michael Hanselmann
               _req_process_fn=None):
419 00267bfe Michael Hanselmann
    """Makes an RPC request to a number of nodes.
420 ecfe9491 Michael Hanselmann

421 1c3231aa Thomas Thrainer
    @type nodes: sequence
422 1c3231aa Thomas Thrainer
    @param nodes: node UUIDs or Hostnames
423 00267bfe Michael Hanselmann
    @type procedure: string
424 00267bfe Michael Hanselmann
    @param procedure: Request path
425 d9de612c Iustin Pop
    @type body: dictionary
426 d9de612c Iustin Pop
    @param body: dictionary with request bodies per host
427 00267bfe Michael Hanselmann
    @type read_timeout: int or None
428 00267bfe Michael Hanselmann
    @param read_timeout: Read timeout for request
429 05325a35 Bernardo Dal Seno
    @rtype: dictionary
430 05325a35 Bernardo Dal Seno
    @return: a dictionary mapping host names to rpc.RpcResult objects
431 a8083063 Iustin Pop

432 a8083063 Iustin Pop
    """
433 83e7af18 Michael Hanselmann
    assert read_timeout is not None, \
434 83e7af18 Michael Hanselmann
      "Missing RPC read timeout for procedure '%s'" % procedure
435 a8083063 Iustin Pop
436 065be3f0 Michael Hanselmann
    if _req_process_fn is None:
437 065be3f0 Michael Hanselmann
      _req_process_fn = http.client.ProcessRequests
438 065be3f0 Michael Hanselmann
439 00267bfe Michael Hanselmann
    (results, requests) = \
440 1c3231aa Thomas Thrainer
      self._PrepareRequests(self._resolver(nodes, resolver_opts), self._port,
441 fce5efd1 Michael Hanselmann
                            procedure, body, read_timeout)
442 a8083063 Iustin Pop
443 abbf2cd9 Michael Hanselmann
    _req_process_fn(requests.values(), lock_monitor_cb=self._lock_monitor_cb)
444 a8083063 Iustin Pop
445 00267bfe Michael Hanselmann
    assert not frozenset(results).intersection(requests)
446 ecfe9491 Michael Hanselmann
447 00267bfe Michael Hanselmann
    return self._CombineResults(results, requests, procedure)
448 a8083063 Iustin Pop
449 a8083063 Iustin Pop
450 cd40dc53 Michael Hanselmann
class _RpcClientBase:
451 065be3f0 Michael Hanselmann
  def __init__(self, resolver, encoder_fn, lock_monitor_cb=None,
452 065be3f0 Michael Hanselmann
               _req_process_fn=None):
453 cd40dc53 Michael Hanselmann
    """Initializes this class.
454 cd40dc53 Michael Hanselmann

455 cd40dc53 Michael Hanselmann
    """
456 065be3f0 Michael Hanselmann
    proc = _RpcProcessor(resolver,
457 065be3f0 Michael Hanselmann
                         netutils.GetDaemonPort(constants.NODED),
458 065be3f0 Michael Hanselmann
                         lock_monitor_cb=lock_monitor_cb)
459 065be3f0 Michael Hanselmann
    self._proc = compat.partial(proc, _req_process_fn=_req_process_fn)
460 cd40dc53 Michael Hanselmann
    self._encoder = compat.partial(self._EncodeArg, encoder_fn)
461 cd40dc53 Michael Hanselmann
462 cd40dc53 Michael Hanselmann
  @staticmethod
463 0c3d9c7c Thomas Thrainer
  def _EncodeArg(encoder_fn, node, (argkind, value)):
464 cd40dc53 Michael Hanselmann
    """Encode argument.
465 cd40dc53 Michael Hanselmann

466 cd40dc53 Michael Hanselmann
    """
467 cd40dc53 Michael Hanselmann
    if argkind is None:
468 cd40dc53 Michael Hanselmann
      return value
469 cd40dc53 Michael Hanselmann
    else:
470 0c3d9c7c Thomas Thrainer
      return encoder_fn(argkind)(node, value)
471 cd40dc53 Michael Hanselmann
472 f7d9b3aa Michael Hanselmann
  def _Call(self, cdef, node_list, args):
473 cd40dc53 Michael Hanselmann
    """Entry point for automatically generated RPC wrappers.
474 cd40dc53 Michael Hanselmann

475 cd40dc53 Michael Hanselmann
    """
476 dd6d2d09 Michael Hanselmann
    (procedure, _, resolver_opts, timeout, argdefs,
477 dd6d2d09 Michael Hanselmann
     prep_fn, postproc_fn, _) = cdef
478 f7d9b3aa Michael Hanselmann
479 f7d9b3aa Michael Hanselmann
    if callable(timeout):
480 f7d9b3aa Michael Hanselmann
      read_timeout = timeout(args)
481 f7d9b3aa Michael Hanselmann
    else:
482 f7d9b3aa Michael Hanselmann
      read_timeout = timeout
483 cd40dc53 Michael Hanselmann
484 dd6d2d09 Michael Hanselmann
    if callable(resolver_opts):
485 dd6d2d09 Michael Hanselmann
      req_resolver_opts = resolver_opts(args)
486 dd6d2d09 Michael Hanselmann
    else:
487 dd6d2d09 Michael Hanselmann
      req_resolver_opts = resolver_opts
488 dd6d2d09 Michael Hanselmann
489 e78667fe Michael Hanselmann
    if len(args) != len(argdefs):
490 e78667fe Michael Hanselmann
      raise errors.ProgrammerError("Number of passed arguments doesn't match")
491 e78667fe Michael Hanselmann
492 d9de612c Iustin Pop
    if prep_fn is None:
493 0c3d9c7c Thomas Thrainer
      prep_fn = lambda _, args: args
494 0c3d9c7c Thomas Thrainer
    assert callable(prep_fn)
495 0c3d9c7c Thomas Thrainer
496 0c3d9c7c Thomas Thrainer
    # encode the arguments for each node individually, pass them and the node
497 0c3d9c7c Thomas Thrainer
    # name to the prep_fn, and serialise its return value
498 0c3d9c7c Thomas Thrainer
    encode_args_fn = lambda node: map(compat.partial(self._encoder, node),
499 0c3d9c7c Thomas Thrainer
                                      zip(map(compat.snd, argdefs), args))
500 0c3d9c7c Thomas Thrainer
    pnbody = dict((n, serializer.DumpJson(prep_fn(n, encode_args_fn(n))))
501 0c3d9c7c Thomas Thrainer
                  for n in node_list)
502 d9de612c Iustin Pop
503 fce5efd1 Michael Hanselmann
    result = self._proc(node_list, procedure, pnbody, read_timeout,
504 fce5efd1 Michael Hanselmann
                        req_resolver_opts)
505 26d502d0 Michael Hanselmann
506 26d502d0 Michael Hanselmann
    if postproc_fn:
507 d9da5065 Michael Hanselmann
      return dict(map(lambda (key, value): (key, postproc_fn(value)),
508 d9da5065 Michael Hanselmann
                      result.items()))
509 26d502d0 Michael Hanselmann
    else:
510 26d502d0 Michael Hanselmann
      return result
511 cd40dc53 Michael Hanselmann
512 cd40dc53 Michael Hanselmann
513 0c3d9c7c Thomas Thrainer
def _ObjectToDict(_, value):
514 cd40dc53 Michael Hanselmann
  """Converts an object to a dictionary.
515 cd40dc53 Michael Hanselmann

516 cd40dc53 Michael Hanselmann
  @note: See L{objects}.
517 cd40dc53 Michael Hanselmann

518 cd40dc53 Michael Hanselmann
  """
519 cd40dc53 Michael Hanselmann
  return value.ToDict()
520 cd40dc53 Michael Hanselmann
521 cd40dc53 Michael Hanselmann
522 0c3d9c7c Thomas Thrainer
def _ObjectListToDict(node, value):
523 cd40dc53 Michael Hanselmann
  """Converts a list of L{objects} to dictionaries.
524 cd40dc53 Michael Hanselmann

525 cd40dc53 Michael Hanselmann
  """
526 0c3d9c7c Thomas Thrainer
  return map(compat.partial(_ObjectToDict, node), value)
527 cd40dc53 Michael Hanselmann
528 cd40dc53 Michael Hanselmann
529 0c3d9c7c Thomas Thrainer
def _PrepareFileUpload(getents_fn, node, filename):
530 cd40dc53 Michael Hanselmann
  """Loads a file and prepares it for an upload to nodes.
531 cd40dc53 Michael Hanselmann

532 cd40dc53 Michael Hanselmann
  """
533 2ce40421 Michael Hanselmann
  statcb = utils.FileStatHelper()
534 0c3d9c7c Thomas Thrainer
  data = _Compress(node, utils.ReadFile(filename, preread=statcb))
535 2ce40421 Michael Hanselmann
  st = statcb.st
536 601dfcbb Michael Hanselmann
537 601dfcbb Michael Hanselmann
  if getents_fn is None:
538 601dfcbb Michael Hanselmann
    getents_fn = runtime.GetEnts
539 601dfcbb Michael Hanselmann
540 601dfcbb Michael Hanselmann
  getents = getents_fn()
541 601dfcbb Michael Hanselmann
542 cffbbae7 Michael Hanselmann
  virt_filename = vcluster.MakeVirtualPath(filename)
543 cffbbae7 Michael Hanselmann
544 cffbbae7 Michael Hanselmann
  return [virt_filename, data, st.st_mode, getents.LookupUid(st.st_uid),
545 cd40dc53 Michael Hanselmann
          getents.LookupGid(st.st_gid), st.st_atime, st.st_mtime]
546 cd40dc53 Michael Hanselmann
547 cd40dc53 Michael Hanselmann
548 0c3d9c7c Thomas Thrainer
def _PrepareFinalizeExportDisks(_, snap_disks):
549 cd40dc53 Michael Hanselmann
  """Encodes disks for finalizing export.
550 cd40dc53 Michael Hanselmann

551 cd40dc53 Michael Hanselmann
  """
552 cd40dc53 Michael Hanselmann
  flat_disks = []
553 cd40dc53 Michael Hanselmann
554 cd40dc53 Michael Hanselmann
  for disk in snap_disks:
555 cd40dc53 Michael Hanselmann
    if isinstance(disk, bool):
556 cd40dc53 Michael Hanselmann
      flat_disks.append(disk)
557 cd40dc53 Michael Hanselmann
    else:
558 cd40dc53 Michael Hanselmann
      flat_disks.append(disk.ToDict())
559 cd40dc53 Michael Hanselmann
560 cd40dc53 Michael Hanselmann
  return flat_disks
561 cd40dc53 Michael Hanselmann
562 cd40dc53 Michael Hanselmann
563 0c3d9c7c Thomas Thrainer
def _EncodeBlockdevRename(_, value):
564 cd40dc53 Michael Hanselmann
  """Encodes information for renaming block devices.
565 cd40dc53 Michael Hanselmann

566 cd40dc53 Michael Hanselmann
  """
567 cd40dc53 Michael Hanselmann
  return [(d.ToDict(), uid) for d, uid in value]
568 cd40dc53 Michael Hanselmann
569 cd40dc53 Michael Hanselmann
570 32389d91 Helga Velroyen
def _AddSpindlesToLegacyNodeInfo(result, space_info):
571 32389d91 Helga Velroyen
  """Extracts the spindle information from the space info and adds
572 32389d91 Helga Velroyen
  it to the result dictionary.
573 32389d91 Helga Velroyen

574 32389d91 Helga Velroyen
  @type result: dict of strings
575 32389d91 Helga Velroyen
  @param result: dictionary holding the result of the legacy node info
576 32389d91 Helga Velroyen
  @type space_info: list of dicts of strings
577 32389d91 Helga Velroyen
  @param space_info: list, each row holding space information of one storage
578 32389d91 Helga Velroyen
    unit
579 32389d91 Helga Velroyen
  @rtype: None
580 32389d91 Helga Velroyen
  @return: does not return anything, manipulates the C{result} variable
581 32389d91 Helga Velroyen

582 32389d91 Helga Velroyen
  """
583 32389d91 Helga Velroyen
  lvm_pv_info = utils.storage.LookupSpaceInfoByStorageType(
584 32389d91 Helga Velroyen
      space_info, constants.ST_LVM_PV)
585 32389d91 Helga Velroyen
  if lvm_pv_info:
586 32389d91 Helga Velroyen
    result["spindles_free"] = lvm_pv_info["storage_free"]
587 32389d91 Helga Velroyen
    result["spindles_total"] = lvm_pv_info["storage_size"]
588 20faaa74 Helga Velroyen
  else:
589 20faaa74 Helga Velroyen
    raise errors.OpExecError("No spindle storage information available.")
590 06fb92cf Bernardo Dal Seno
591 06fb92cf Bernardo Dal Seno
592 20faaa74 Helga Velroyen
def _AddDefaultStorageInfoToLegacyNodeInfo(result, space_info):
593 32389d91 Helga Velroyen
  """Extracts the storage space information of the default storage type from
594 32389d91 Helga Velroyen
  the space info and adds it to the result dictionary.
595 32389d91 Helga Velroyen

596 32389d91 Helga Velroyen
  @see: C{_AddSpindlesToLegacyNodeInfo} for parameter information.
597 06fb92cf Bernardo Dal Seno

598 06fb92cf Bernardo Dal Seno
  """
599 32389d91 Helga Velroyen
  # Check if there is at least one row for non-spindle storage info.
600 32389d91 Helga Velroyen
  no_defaults = (len(space_info) < 1) or \
601 32389d91 Helga Velroyen
      (space_info[0]["type"] == constants.ST_LVM_PV and len(space_info) == 1)
602 32389d91 Helga Velroyen
603 32389d91 Helga Velroyen
  default_space_info = None
604 32389d91 Helga Velroyen
  if no_defaults:
605 32389d91 Helga Velroyen
    logging.warning("No storage info provided for default storage type.")
606 06fb92cf Bernardo Dal Seno
  else:
607 32389d91 Helga Velroyen
    default_space_info = space_info[0]
608 32389d91 Helga Velroyen
609 32389d91 Helga Velroyen
  if default_space_info:
610 32389d91 Helga Velroyen
    result["name"] = default_space_info["name"]
611 32389d91 Helga Velroyen
    result["storage_free"] = default_space_info["storage_free"]
612 32389d91 Helga Velroyen
    result["storage_size"] = default_space_info["storage_size"]
613 06fb92cf Bernardo Dal Seno
614 06fb92cf Bernardo Dal Seno
615 20faaa74 Helga Velroyen
def MakeLegacyNodeInfo(data, require_spindles=False):
616 b112bfc4 René Nussbaumer
  """Formats the data returned by L{rpc.RpcRunner.call_node_info}.
617 b112bfc4 René Nussbaumer

618 b112bfc4 René Nussbaumer
  Converts the data into a single dictionary. This is fine for most use cases,
619 b112bfc4 René Nussbaumer
  but some require information from more than one volume group or hypervisor.
620 b112bfc4 René Nussbaumer

621 20faaa74 Helga Velroyen
  @param require_spindles: add spindle storage information to the legacy node
622 20faaa74 Helga Velroyen
      info
623 91c17910 Iustin Pop

624 b112bfc4 René Nussbaumer
  """
625 32389d91 Helga Velroyen
  (bootid, space_info, (hv_info, )) = data
626 91c17910 Iustin Pop
627 91c17910 Iustin Pop
  ret = utils.JoinDisjointDicts(hv_info, {"bootid": bootid})
628 91c17910 Iustin Pop
629 20faaa74 Helga Velroyen
  if require_spindles:
630 20faaa74 Helga Velroyen
    _AddSpindlesToLegacyNodeInfo(ret, space_info)
631 20faaa74 Helga Velroyen
  _AddDefaultStorageInfoToLegacyNodeInfo(ret, space_info)
632 b112bfc4 René Nussbaumer
633 91c17910 Iustin Pop
  return ret
634 b112bfc4 René Nussbaumer
635 b112bfc4 René Nussbaumer
636 cd46491f René Nussbaumer
def _AnnotateDParamsDRBD(disk, (drbd_params, data_params, meta_params)):
637 cd46491f René Nussbaumer
  """Annotates just DRBD disks layouts.
638 cd46491f René Nussbaumer

639 cd46491f René Nussbaumer
  """
640 cd3b4ff4 Helga Velroyen
  assert disk.dev_type == constants.DT_DRBD8
641 cd46491f René Nussbaumer
642 cd46491f René Nussbaumer
  disk.params = objects.FillDict(drbd_params, disk.params)
643 cd46491f René Nussbaumer
  (dev_data, dev_meta) = disk.children
644 cd46491f René Nussbaumer
  dev_data.params = objects.FillDict(data_params, dev_data.params)
645 cd46491f René Nussbaumer
  dev_meta.params = objects.FillDict(meta_params, dev_meta.params)
646 cd46491f René Nussbaumer
647 cd46491f René Nussbaumer
  return disk
648 cd46491f René Nussbaumer
649 cd46491f René Nussbaumer
650 cd46491f René Nussbaumer
def _AnnotateDParamsGeneric(disk, (params, )):
651 cd46491f René Nussbaumer
  """Generic disk parameter annotation routine.
652 cd46491f René Nussbaumer

653 cd46491f René Nussbaumer
  """
654 cd3b4ff4 Helga Velroyen
  assert disk.dev_type != constants.DT_DRBD8
655 cd46491f René Nussbaumer
656 cd46491f René Nussbaumer
  disk.params = objects.FillDict(params, disk.params)
657 cd46491f René Nussbaumer
658 cd46491f René Nussbaumer
  return disk
659 cd46491f René Nussbaumer
660 cd46491f René Nussbaumer
661 0c3d9c7c Thomas Thrainer
def AnnotateDiskParams(disks, disk_params):
662 cd46491f René Nussbaumer
  """Annotates the disk objects with the disk parameters.
663 cd46491f René Nussbaumer

664 cd46491f René Nussbaumer
  @param disks: The list of disks objects to annotate
665 0c3d9c7c Thomas Thrainer
  @param disk_params: The disk parameters for annotation
666 cd46491f René Nussbaumer
  @returns: A list of disk objects annotated
667 cd46491f René Nussbaumer

668 cd46491f René Nussbaumer
  """
669 0c3d9c7c Thomas Thrainer
  def AnnotateDisk(disk):
670 0c3d9c7c Thomas Thrainer
    if disk.dev_type == constants.DT_DISKLESS:
671 0c3d9c7c Thomas Thrainer
      return disk
672 cd46491f René Nussbaumer
673 0c3d9c7c Thomas Thrainer
    ld_params = objects.Disk.ComputeLDParams(disk.dev_type, disk_params)
674 cd46491f René Nussbaumer
675 0c3d9c7c Thomas Thrainer
    if disk.dev_type == constants.DT_DRBD8:
676 0c3d9c7c Thomas Thrainer
      return _AnnotateDParamsDRBD(disk, ld_params)
677 0c3d9c7c Thomas Thrainer
    else:
678 0c3d9c7c Thomas Thrainer
      return _AnnotateDParamsGeneric(disk, ld_params)
679 0c3d9c7c Thomas Thrainer
680 0c3d9c7c Thomas Thrainer
  return [AnnotateDisk(disk.Copy()) for disk in disks]
681 cd46491f René Nussbaumer
682 cd46491f René Nussbaumer
683 da803ff1 Helga Velroyen
def _GetExclusiveStorageFlag(cfg, node_uuid):
684 1c3231aa Thomas Thrainer
  ni = cfg.GetNodeInfo(node_uuid)
685 319322a7 Bernardo Dal Seno
  if ni is None:
686 1c3231aa Thomas Thrainer
    raise errors.OpPrereqError("Invalid node name %s" % node_uuid,
687 319322a7 Bernardo Dal Seno
                               errors.ECODE_NOENT)
688 319322a7 Bernardo Dal Seno
  return cfg.GetNdParams(ni)[constants.ND_EXCLUSIVE_STORAGE]
689 319322a7 Bernardo Dal Seno
690 319322a7 Bernardo Dal Seno
691 da803ff1 Helga Velroyen
def _AddExclusiveStorageFlagToLvmStorageUnits(storage_units, es_flag):
692 da803ff1 Helga Velroyen
  """Adds the exclusive storage flag to lvm units.
693 da803ff1 Helga Velroyen

694 da803ff1 Helga Velroyen
  This function creates a copy of the storage_units lists, with the
695 da803ff1 Helga Velroyen
  es_flag being added to all lvm storage units.
696 da803ff1 Helga Velroyen

697 da803ff1 Helga Velroyen
  @type storage_units: list of pairs (string, string)
698 da803ff1 Helga Velroyen
  @param storage_units: list of 'raw' storage units, consisting only of
699 da803ff1 Helga Velroyen
    (storage_type, storage_key)
700 da803ff1 Helga Velroyen
  @type es_flag: boolean
701 da803ff1 Helga Velroyen
  @param es_flag: exclusive storage flag
702 da803ff1 Helga Velroyen
  @rtype: list of tuples (string, string, list)
703 da803ff1 Helga Velroyen
  @return: list of storage units (storage_type, storage_key, params) with
704 da803ff1 Helga Velroyen
    the params containing the es_flag for lvm-vg storage units
705 da803ff1 Helga Velroyen

706 da803ff1 Helga Velroyen
  """
707 da803ff1 Helga Velroyen
  result = []
708 da803ff1 Helga Velroyen
  for (storage_type, storage_key) in storage_units:
709 52a8a6ae Helga Velroyen
    if storage_type in [constants.ST_LVM_VG, constants.ST_LVM_PV]:
710 52a8a6ae Helga Velroyen
      result.append((storage_type, storage_key, [es_flag]))
711 da803ff1 Helga Velroyen
    else:
712 da803ff1 Helga Velroyen
      result.append((storage_type, storage_key, []))
713 da803ff1 Helga Velroyen
  return result
714 da803ff1 Helga Velroyen
715 da803ff1 Helga Velroyen
716 1c3231aa Thomas Thrainer
def GetExclusiveStorageForNodes(cfg, node_uuids):
717 319322a7 Bernardo Dal Seno
  """Return the exclusive storage flag for all the given nodes.
718 319322a7 Bernardo Dal Seno

719 319322a7 Bernardo Dal Seno
  @type cfg: L{config.ConfigWriter}
720 319322a7 Bernardo Dal Seno
  @param cfg: cluster configuration
721 1c3231aa Thomas Thrainer
  @type node_uuids: list or tuple
722 1c3231aa Thomas Thrainer
  @param node_uuids: node UUIDs for which to read the flag
723 319322a7 Bernardo Dal Seno
  @rtype: dict
724 da803ff1 Helga Velroyen
  @return: mapping from node uuids to exclusive storage flags
725 1c3231aa Thomas Thrainer
  @raise errors.OpPrereqError: if any given node name has no corresponding
726 1c3231aa Thomas Thrainer
  node
727 319322a7 Bernardo Dal Seno

728 319322a7 Bernardo Dal Seno
  """
729 da803ff1 Helga Velroyen
  getflag = lambda n: _GetExclusiveStorageFlag(cfg, n)
730 1c3231aa Thomas Thrainer
  flags = map(getflag, node_uuids)
731 1c3231aa Thomas Thrainer
  return dict(zip(node_uuids, flags))
732 319322a7 Bernardo Dal Seno
733 319322a7 Bernardo Dal Seno
734 da803ff1 Helga Velroyen
def PrepareStorageUnitsForNodes(cfg, storage_units, node_uuids):
735 da803ff1 Helga Velroyen
  """Return the lvm storage unit for all the given nodes.
736 da803ff1 Helga Velroyen

737 da803ff1 Helga Velroyen
  Main purpose of this function is to map the exclusive storage flag, which
738 da803ff1 Helga Velroyen
  can be different for each node, to the default LVM storage unit.
739 da803ff1 Helga Velroyen

740 da803ff1 Helga Velroyen
  @type cfg: L{config.ConfigWriter}
741 da803ff1 Helga Velroyen
  @param cfg: cluster configuration
742 da803ff1 Helga Velroyen
  @type storage_units: list of pairs (string, string)
743 da803ff1 Helga Velroyen
  @param storage_units: list of 'raw' storage units, e.g. pairs of
744 da803ff1 Helga Velroyen
    (storage_type, storage_key)
745 da803ff1 Helga Velroyen
  @type node_uuids: list or tuple
746 da803ff1 Helga Velroyen
  @param node_uuids: node UUIDs for which to read the flag
747 da803ff1 Helga Velroyen
  @rtype: dict
748 da803ff1 Helga Velroyen
  @return: mapping from node uuids to a list of storage units which include
749 da803ff1 Helga Velroyen
    the exclusive storage flag for lvm storage
750 da803ff1 Helga Velroyen
  @raise errors.OpPrereqError: if any given node name has no corresponding
751 da803ff1 Helga Velroyen
  node
752 da803ff1 Helga Velroyen

753 da803ff1 Helga Velroyen
  """
754 da803ff1 Helga Velroyen
  getunit = lambda n: _AddExclusiveStorageFlagToLvmStorageUnits(
755 da803ff1 Helga Velroyen
      storage_units, _GetExclusiveStorageFlag(cfg, n))
756 da803ff1 Helga Velroyen
  flags = map(getunit, node_uuids)
757 da803ff1 Helga Velroyen
  return dict(zip(node_uuids, flags))
758 da803ff1 Helga Velroyen
759 da803ff1 Helga Velroyen
760 cd40dc53 Michael Hanselmann
#: Generic encoders
761 cd40dc53 Michael Hanselmann
_ENCODERS = {
762 cd40dc53 Michael Hanselmann
  rpc_defs.ED_OBJECT_DICT: _ObjectToDict,
763 cd40dc53 Michael Hanselmann
  rpc_defs.ED_OBJECT_DICT_LIST: _ObjectListToDict,
764 cd40dc53 Michael Hanselmann
  rpc_defs.ED_COMPRESS: _Compress,
765 cd40dc53 Michael Hanselmann
  rpc_defs.ED_FINALIZE_EXPORT_DISKS: _PrepareFinalizeExportDisks,
766 cd40dc53 Michael Hanselmann
  rpc_defs.ED_BLOCKDEV_RENAME: _EncodeBlockdevRename,
767 cd40dc53 Michael Hanselmann
  }
768 cd40dc53 Michael Hanselmann
769 cd40dc53 Michael Hanselmann
770 cd40dc53 Michael Hanselmann
class RpcRunner(_RpcClientBase,
771 cd40dc53 Michael Hanselmann
                _generated_rpc.RpcClientDefault,
772 415a7304 Michael Hanselmann
                _generated_rpc.RpcClientBootstrap,
773 bd6d1202 René Nussbaumer
                _generated_rpc.RpcClientDnsOnly,
774 415a7304 Michael Hanselmann
                _generated_rpc.RpcClientConfig):
775 87b3cb26 Michael Hanselmann
  """RPC runner class.
776 a8083063 Iustin Pop

777 87b3cb26 Michael Hanselmann
  """
778 d5ea30e8 Michael Hanselmann
  def __init__(self, cfg, lock_monitor_cb, _req_process_fn=None, _getents=None):
779 87b3cb26 Michael Hanselmann
    """Initialized the RPC runner.
780 a8083063 Iustin Pop

781 d5ea30e8 Michael Hanselmann
    @type cfg: L{config.ConfigWriter}
782 d5ea30e8 Michael Hanselmann
    @param cfg: Configuration
783 d5ea30e8 Michael Hanselmann
    @type lock_monitor_cb: callable
784 d5ea30e8 Michael Hanselmann
    @param lock_monitor_cb: Lock monitor callback
785 a8083063 Iustin Pop

786 72737a7f Iustin Pop
    """
787 d5ea30e8 Michael Hanselmann
    self._cfg = cfg
788 cd40dc53 Michael Hanselmann
789 cd40dc53 Michael Hanselmann
    encoders = _ENCODERS.copy()
790 cd40dc53 Michael Hanselmann
791 cd40dc53 Michael Hanselmann
    encoders.update({
792 601dfcbb Michael Hanselmann
      # Encoders requiring configuration object
793 cd40dc53 Michael Hanselmann
      rpc_defs.ED_INST_DICT: self._InstDict,
794 5fce6a89 Constantinos Venetsanopoulos
      rpc_defs.ED_INST_DICT_HVP_BEP_DP: self._InstDictHvpBepDp,
795 b8291e00 René Nussbaumer
      rpc_defs.ED_INST_DICT_OSP_DP: self._InstDictOspDp,
796 cbe4a0a5 Dimitris Aragiorgis
      rpc_defs.ED_NIC_DICT: self._NicDict,
797 601dfcbb Michael Hanselmann
798 aedf5fd7 René Nussbaumer
      # Encoders annotating disk parameters
799 aedf5fd7 René Nussbaumer
      rpc_defs.ED_DISKS_DICT_DP: self._DisksDictDP,
800 235a6b29 Thomas Thrainer
      rpc_defs.ED_MULTI_DISKS_DICT_DP: self._MultiDiskDictDP,
801 aedf5fd7 René Nussbaumer
      rpc_defs.ED_SINGLE_DISK_DICT_DP: self._SingleDiskDictDP,
802 0c3d9c7c Thomas Thrainer
      rpc_defs.ED_NODE_TO_DISK_DICT_DP: self._EncodeNodeToDiskDictDP,
803 aedf5fd7 René Nussbaumer
804 601dfcbb Michael Hanselmann
      # Encoders with special requirements
805 601dfcbb Michael Hanselmann
      rpc_defs.ED_FILE_DETAILS: compat.partial(_PrepareFileUpload, _getents),
806 0c3d9c7c Thomas Thrainer
807 0c3d9c7c Thomas Thrainer
      rpc_defs.ED_IMPEXP_IO: self._EncodeImportExportIO,
808 cd40dc53 Michael Hanselmann
      })
809 cd40dc53 Michael Hanselmann
810 cd40dc53 Michael Hanselmann
    # Resolver using configuration
811 d5ea30e8 Michael Hanselmann
    resolver = compat.partial(_NodeConfigResolver, cfg.GetNodeInfo,
812 d5ea30e8 Michael Hanselmann
                              cfg.GetAllNodesInfo)
813 cd40dc53 Michael Hanselmann
814 db04ce5d Michael Hanselmann
    # Pylint doesn't recognize multiple inheritance properly, see
815 db04ce5d Michael Hanselmann
    # <http://www.logilab.org/ticket/36586> and
816 db04ce5d Michael Hanselmann
    # <http://www.logilab.org/ticket/35642>
817 db04ce5d Michael Hanselmann
    # pylint: disable=W0233
818 cd40dc53 Michael Hanselmann
    _RpcClientBase.__init__(self, resolver, encoders.get,
819 d5ea30e8 Michael Hanselmann
                            lock_monitor_cb=lock_monitor_cb,
820 d5ea30e8 Michael Hanselmann
                            _req_process_fn=_req_process_fn)
821 415a7304 Michael Hanselmann
    _generated_rpc.RpcClientConfig.__init__(self)
822 db04ce5d Michael Hanselmann
    _generated_rpc.RpcClientBootstrap.__init__(self)
823 bd6d1202 René Nussbaumer
    _generated_rpc.RpcClientDnsOnly.__init__(self)
824 200de241 Michael Hanselmann
    _generated_rpc.RpcClientDefault.__init__(self)
825 200de241 Michael Hanselmann
826 0c3d9c7c Thomas Thrainer
  def _NicDict(self, _, nic):
827 cbe4a0a5 Dimitris Aragiorgis
    """Convert the given nic to a dict and encapsulate netinfo
828 cbe4a0a5 Dimitris Aragiorgis

829 cbe4a0a5 Dimitris Aragiorgis
    """
830 cbe4a0a5 Dimitris Aragiorgis
    n = copy.deepcopy(nic)
831 cbe4a0a5 Dimitris Aragiorgis
    if n.network:
832 cbe4a0a5 Dimitris Aragiorgis
      net_uuid = self._cfg.LookupNetwork(n.network)
833 cbe4a0a5 Dimitris Aragiorgis
      if net_uuid:
834 cbe4a0a5 Dimitris Aragiorgis
        nobj = self._cfg.GetNetwork(net_uuid)
835 cbe4a0a5 Dimitris Aragiorgis
        n.netinfo = objects.Network.ToDict(nobj)
836 cbe4a0a5 Dimitris Aragiorgis
    return n.ToDict()
837 cbe4a0a5 Dimitris Aragiorgis
838 0c3d9c7c Thomas Thrainer
  def _InstDict(self, node, instance, hvp=None, bep=None, osp=None):
839 26ba2bd8 Iustin Pop
    """Convert the given instance to a dict.
840 26ba2bd8 Iustin Pop

841 26ba2bd8 Iustin Pop
    This is done via the instance's ToDict() method and additionally
842 26ba2bd8 Iustin Pop
    we fill the hvparams with the cluster defaults.
843 26ba2bd8 Iustin Pop

844 26ba2bd8 Iustin Pop
    @type instance: L{objects.Instance}
845 26ba2bd8 Iustin Pop
    @param instance: an Instance object
846 0eca8e0c Iustin Pop
    @type hvp: dict or None
847 5bbd3f7f Michael Hanselmann
    @param hvp: a dictionary with overridden hypervisor parameters
848 0eca8e0c Iustin Pop
    @type bep: dict or None
849 5bbd3f7f Michael Hanselmann
    @param bep: a dictionary with overridden backend parameters
850 1bdcbbab Iustin Pop
    @type osp: dict or None
851 8d8c4eff Michael Hanselmann
    @param osp: a dictionary with overridden os parameters
852 26ba2bd8 Iustin Pop
    @rtype: dict
853 26ba2bd8 Iustin Pop
    @return: the instance dict, with the hvparams filled with the
854 26ba2bd8 Iustin Pop
        cluster defaults
855 26ba2bd8 Iustin Pop

856 26ba2bd8 Iustin Pop
    """
857 26ba2bd8 Iustin Pop
    idict = instance.ToDict()
858 5b442704 Iustin Pop
    cluster = self._cfg.GetClusterInfo()
859 5b442704 Iustin Pop
    idict["hvparams"] = cluster.FillHV(instance)
860 0eca8e0c Iustin Pop
    if hvp is not None:
861 0eca8e0c Iustin Pop
      idict["hvparams"].update(hvp)
862 5b442704 Iustin Pop
    idict["beparams"] = cluster.FillBE(instance)
863 0eca8e0c Iustin Pop
    if bep is not None:
864 0eca8e0c Iustin Pop
      idict["beparams"].update(bep)
865 1bdcbbab Iustin Pop
    idict["osparams"] = cluster.SimpleFillOS(instance.os, instance.osparams)
866 1bdcbbab Iustin Pop
    if osp is not None:
867 1bdcbbab Iustin Pop
      idict["osparams"].update(osp)
868 0c3d9c7c Thomas Thrainer
    idict["disks"] = self._DisksDictDP(node, (instance.disks, instance))
869 b848ce79 Guido Trotter
    for nic in idict["nics"]:
870 3ccb3a64 Michael Hanselmann
      nic["nicparams"] = objects.FillDict(
871 b848ce79 Guido Trotter
        cluster.nicparams[constants.PP_DEFAULT],
872 3ccb3a64 Michael Hanselmann
        nic["nicparams"])
873 cbe4a0a5 Dimitris Aragiorgis
      network = nic.get("network", None)
874 cbe4a0a5 Dimitris Aragiorgis
      if network:
875 cbe4a0a5 Dimitris Aragiorgis
        net_uuid = self._cfg.LookupNetwork(network)
876 cbe4a0a5 Dimitris Aragiorgis
        if net_uuid:
877 cbe4a0a5 Dimitris Aragiorgis
          nobj = self._cfg.GetNetwork(net_uuid)
878 cbe4a0a5 Dimitris Aragiorgis
          nic["netinfo"] = objects.Network.ToDict(nobj)
879 26ba2bd8 Iustin Pop
    return idict
880 26ba2bd8 Iustin Pop
881 0c3d9c7c Thomas Thrainer
  def _InstDictHvpBepDp(self, node, (instance, hvp, bep)):
882 c4de9b7a Michael Hanselmann
    """Wrapper for L{_InstDict}.
883 c4de9b7a Michael Hanselmann

884 c4de9b7a Michael Hanselmann
    """
885 0c3d9c7c Thomas Thrainer
    return self._InstDict(node, instance, hvp=hvp, bep=bep)
886 c4de9b7a Michael Hanselmann
887 0c3d9c7c Thomas Thrainer
  def _InstDictOspDp(self, node, (instance, osparams)):
888 c4de9b7a Michael Hanselmann
    """Wrapper for L{_InstDict}.
889 c4de9b7a Michael Hanselmann

890 c4de9b7a Michael Hanselmann
    """
891 0c3d9c7c Thomas Thrainer
    return self._InstDict(node, instance, osp=osparams)
892 c4de9b7a Michael Hanselmann
893 0c3d9c7c Thomas Thrainer
  def _DisksDictDP(self, node, (disks, instance)):
894 aedf5fd7 René Nussbaumer
    """Wrapper for L{AnnotateDiskParams}.
895 aedf5fd7 René Nussbaumer

896 aedf5fd7 René Nussbaumer
    """
897 aedf5fd7 René Nussbaumer
    diskparams = self._cfg.GetInstanceDiskParams(instance)
898 0c3d9c7c Thomas Thrainer
    ret = []
899 0c3d9c7c Thomas Thrainer
    for disk in AnnotateDiskParams(disks, diskparams):
900 0c3d9c7c Thomas Thrainer
      disk_node_uuids = disk.GetNodes(instance.primary_node)
901 0c3d9c7c Thomas Thrainer
      node_ips = dict((uuid, node.secondary_ip) for (uuid, node)
902 0c3d9c7c Thomas Thrainer
                      in self._cfg.GetMultiNodeInfo(disk_node_uuids))
903 0c3d9c7c Thomas Thrainer
904 0c3d9c7c Thomas Thrainer
      disk.UpdateDynamicDiskParams(node, node_ips)
905 0c3d9c7c Thomas Thrainer
906 a0d2a91e Thomas Thrainer
      ret.append(disk.ToDict(include_dynamic_params=True))
907 aedf5fd7 René Nussbaumer
908 0c3d9c7c Thomas Thrainer
    return ret
909 0c3d9c7c Thomas Thrainer
910 0c3d9c7c Thomas Thrainer
  def _MultiDiskDictDP(self, node, disks_insts):
911 235a6b29 Thomas Thrainer
    """Wrapper for L{AnnotateDiskParams}.
912 235a6b29 Thomas Thrainer

913 235a6b29 Thomas Thrainer
    Supports a list of (disk, instance) tuples.
914 235a6b29 Thomas Thrainer
    """
915 235a6b29 Thomas Thrainer
    return [disk for disk_inst in disks_insts
916 0c3d9c7c Thomas Thrainer
            for disk in self._DisksDictDP(node, disk_inst)]
917 235a6b29 Thomas Thrainer
918 0c3d9c7c Thomas Thrainer
  def _SingleDiskDictDP(self, node, (disk, instance)):
919 aedf5fd7 René Nussbaumer
    """Wrapper for L{AnnotateDiskParams}.
920 aedf5fd7 René Nussbaumer

921 aedf5fd7 René Nussbaumer
    """
922 0c3d9c7c Thomas Thrainer
    (anno_disk,) = self._DisksDictDP(node, ([disk], instance))
923 aedf5fd7 René Nussbaumer
    return anno_disk
924 aedf5fd7 René Nussbaumer
925 0c3d9c7c Thomas Thrainer
  def _EncodeNodeToDiskDictDP(self, node, value):
926 0c3d9c7c Thomas Thrainer
    """Encode dict of node name -> list of (disk, instance) tuples as values.
927 0c3d9c7c Thomas Thrainer

928 0c3d9c7c Thomas Thrainer
    """
929 0c3d9c7c Thomas Thrainer
    return dict((name, [self._SingleDiskDictDP(node, disk) for disk in disks])
930 0c3d9c7c Thomas Thrainer
                for name, disks in value.items())
931 0c3d9c7c Thomas Thrainer
932 0c3d9c7c Thomas Thrainer
  def _EncodeImportExportIO(self, node, (ieio, ieioargs)):
933 0c3d9c7c Thomas Thrainer
    """Encodes import/export I/O information.
934 0c3d9c7c Thomas Thrainer

935 0c3d9c7c Thomas Thrainer
    """
936 0c3d9c7c Thomas Thrainer
    if ieio == constants.IEIO_RAW_DISK:
937 0c3d9c7c Thomas Thrainer
      assert len(ieioargs) == 1
938 0c3d9c7c Thomas Thrainer
      return (ieio, (self._SingleDiskDictDP(node, ieioargs[0]), ))
939 0c3d9c7c Thomas Thrainer
940 0c3d9c7c Thomas Thrainer
    if ieio == constants.IEIO_SCRIPT:
941 0c3d9c7c Thomas Thrainer
      assert len(ieioargs) == 2
942 0c3d9c7c Thomas Thrainer
      return (ieio, (self._SingleDiskDictDP(node, ieioargs[0]), ieioargs[1]))
943 0c3d9c7c Thomas Thrainer
944 0c3d9c7c Thomas Thrainer
    return (ieio, ieioargs)
945 0c3d9c7c Thomas Thrainer
946 fb1ffbca Michael Hanselmann
947 cd40dc53 Michael Hanselmann
class JobQueueRunner(_RpcClientBase, _generated_rpc.RpcClientJobQueue):
948 fb1ffbca Michael Hanselmann
  """RPC wrappers for job queue.
949 fb1ffbca Michael Hanselmann

950 fb1ffbca Michael Hanselmann
  """
951 fb1ffbca Michael Hanselmann
  def __init__(self, context, address_list):
952 fb1ffbca Michael Hanselmann
    """Initializes this class.
953 fb1ffbca Michael Hanselmann

954 fb1ffbca Michael Hanselmann
    """
955 fb1ffbca Michael Hanselmann
    if address_list is None:
956 bd6d1202 René Nussbaumer
      resolver = compat.partial(_SsconfResolver, True)
957 fb1ffbca Michael Hanselmann
    else:
958 fb1ffbca Michael Hanselmann
      # Caller provided an address list
959 fb1ffbca Michael Hanselmann
      resolver = _StaticResolver(address_list)
960 fb1ffbca Michael Hanselmann
961 cd40dc53 Michael Hanselmann
    _RpcClientBase.__init__(self, resolver, _ENCODERS.get,
962 cd40dc53 Michael Hanselmann
                            lock_monitor_cb=context.glm.AddToLockMonitor)
963 cd40dc53 Michael Hanselmann
    _generated_rpc.RpcClientJobQueue.__init__(self)
964 db04ce5d Michael Hanselmann
965 db04ce5d Michael Hanselmann
966 bd6d1202 René Nussbaumer
class BootstrapRunner(_RpcClientBase,
967 bd6d1202 René Nussbaumer
                      _generated_rpc.RpcClientBootstrap,
968 bd6d1202 René Nussbaumer
                      _generated_rpc.RpcClientDnsOnly):
969 db04ce5d Michael Hanselmann
  """RPC wrappers for bootstrapping.
970 db04ce5d Michael Hanselmann

971 db04ce5d Michael Hanselmann
  """
972 db04ce5d Michael Hanselmann
  def __init__(self):
973 db04ce5d Michael Hanselmann
    """Initializes this class.
974 db04ce5d Michael Hanselmann

975 db04ce5d Michael Hanselmann
    """
976 bd6d1202 René Nussbaumer
    # Pylint doesn't recognize multiple inheritance properly, see
977 bd6d1202 René Nussbaumer
    # <http://www.logilab.org/ticket/36586> and
978 bd6d1202 René Nussbaumer
    # <http://www.logilab.org/ticket/35642>
979 bd6d1202 René Nussbaumer
    # pylint: disable=W0233
980 bd6d1202 René Nussbaumer
    _RpcClientBase.__init__(self, compat.partial(_SsconfResolver, True),
981 bd6d1202 René Nussbaumer
                            _ENCODERS.get)
982 db04ce5d Michael Hanselmann
    _generated_rpc.RpcClientBootstrap.__init__(self)
983 bd6d1202 René Nussbaumer
    _generated_rpc.RpcClientDnsOnly.__init__(self)
984 bd6d1202 René Nussbaumer
985 bd6d1202 René Nussbaumer
986 bd6d1202 René Nussbaumer
class DnsOnlyRunner(_RpcClientBase, _generated_rpc.RpcClientDnsOnly):
987 bd6d1202 René Nussbaumer
  """RPC wrappers for calls using only DNS.
988 bd6d1202 René Nussbaumer

989 bd6d1202 René Nussbaumer
  """
990 bd6d1202 René Nussbaumer
  def __init__(self):
991 bd6d1202 René Nussbaumer
    """Initialize this class.
992 bd6d1202 René Nussbaumer

993 bd6d1202 René Nussbaumer
    """
994 bd6d1202 René Nussbaumer
    _RpcClientBase.__init__(self, compat.partial(_SsconfResolver, False),
995 bd6d1202 René Nussbaumer
                            _ENCODERS.get)
996 bd6d1202 René Nussbaumer
    _generated_rpc.RpcClientDnsOnly.__init__(self)
997 db04ce5d Michael Hanselmann
998 415a7304 Michael Hanselmann
999 cd40dc53 Michael Hanselmann
class ConfigRunner(_RpcClientBase, _generated_rpc.RpcClientConfig):
1000 415a7304 Michael Hanselmann
  """RPC wrappers for L{config}.
1001 415a7304 Michael Hanselmann

1002 415a7304 Michael Hanselmann
  """
1003 c2dc025a Michael Hanselmann
  def __init__(self, context, address_list, _req_process_fn=None,
1004 c2dc025a Michael Hanselmann
               _getents=None):
1005 415a7304 Michael Hanselmann
    """Initializes this class.
1006 415a7304 Michael Hanselmann

1007 415a7304 Michael Hanselmann
    """
1008 b2acdbdc Michael Hanselmann
    if context:
1009 b2acdbdc Michael Hanselmann
      lock_monitor_cb = context.glm.AddToLockMonitor
1010 b2acdbdc Michael Hanselmann
    else:
1011 b2acdbdc Michael Hanselmann
      lock_monitor_cb = None
1012 b2acdbdc Michael Hanselmann
1013 415a7304 Michael Hanselmann
    if address_list is None:
1014 bd6d1202 René Nussbaumer
      resolver = compat.partial(_SsconfResolver, True)
1015 415a7304 Michael Hanselmann
    else:
1016 415a7304 Michael Hanselmann
      # Caller provided an address list
1017 415a7304 Michael Hanselmann
      resolver = _StaticResolver(address_list)
1018 415a7304 Michael Hanselmann
1019 c2dc025a Michael Hanselmann
    encoders = _ENCODERS.copy()
1020 c2dc025a Michael Hanselmann
1021 c2dc025a Michael Hanselmann
    encoders.update({
1022 c2dc025a Michael Hanselmann
      rpc_defs.ED_FILE_DETAILS: compat.partial(_PrepareFileUpload, _getents),
1023 c2dc025a Michael Hanselmann
      })
1024 c2dc025a Michael Hanselmann
1025 c2dc025a Michael Hanselmann
    _RpcClientBase.__init__(self, resolver, encoders.get,
1026 c2dc025a Michael Hanselmann
                            lock_monitor_cb=lock_monitor_cb,
1027 c2dc025a Michael Hanselmann
                            _req_process_fn=_req_process_fn)
1028 cd40dc53 Michael Hanselmann
    _generated_rpc.RpcClientConfig.__init__(self)