Statistics
| Branch: | Tag: | Revision:

root / lib / rpc.py @ 065be3f0

History | View | Annotate | Download (20.6 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 d2cd6944 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 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 a8083063 Iustin Pop
import os
34 58b311ca Iustin Pop
import logging
35 12bce260 Michael Hanselmann
import zlib
36 12bce260 Michael Hanselmann
import base64
37 33231500 Michael Hanselmann
import pycurl
38 33231500 Michael Hanselmann
import threading
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 a8083063 Iustin Pop
52 200de241 Michael Hanselmann
# Special module generated at build time
53 200de241 Michael Hanselmann
from ganeti import _generated_rpc
54 200de241 Michael Hanselmann
55 fe267188 Iustin Pop
# pylint has a bug here, doesn't see this import
56 b459a848 Andrea Spadaccini
import ganeti.http.client  # pylint: disable=W0611
57 ae88ef45 Michael Hanselmann
58 a8083063 Iustin Pop
59 33231500 Michael Hanselmann
# Timeout for connecting to nodes (seconds)
60 33231500 Michael Hanselmann
_RPC_CONNECT_TIMEOUT = 5
61 33231500 Michael Hanselmann
62 33231500 Michael Hanselmann
_RPC_CLIENT_HEADERS = [
63 33231500 Michael Hanselmann
  "Content-type: %s" % http.HTTP_APP_JSON,
64 8e29563f Iustin Pop
  "Expect:",
65 33231500 Michael Hanselmann
  ]
66 4331f6cd Michael Hanselmann
67 92fd2250 Iustin Pop
# Various time constants for the timeout table
68 92fd2250 Iustin Pop
_TMO_URGENT = 60 # one minute
69 92fd2250 Iustin Pop
_TMO_FAST = 5 * 60 # five minutes
70 92fd2250 Iustin Pop
_TMO_NORMAL = 15 * 60 # 15 minutes
71 92fd2250 Iustin Pop
_TMO_SLOW = 3600 # one hour
72 92fd2250 Iustin Pop
_TMO_4HRS = 4 * 3600
73 92fd2250 Iustin Pop
_TMO_1DAY = 86400
74 92fd2250 Iustin Pop
75 00267bfe Michael Hanselmann
#: Special value to describe an offline host
76 00267bfe Michael Hanselmann
_OFFLINE = object()
77 00267bfe Michael Hanselmann
78 4331f6cd Michael Hanselmann
79 4331f6cd Michael Hanselmann
def Init():
80 4331f6cd Michael Hanselmann
  """Initializes the module-global HTTP client manager.
81 4331f6cd Michael Hanselmann

82 33231500 Michael Hanselmann
  Must be called before using any RPC function and while exactly one thread is
83 33231500 Michael Hanselmann
  running.
84 4331f6cd Michael Hanselmann

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

100 33231500 Michael Hanselmann
  Must be called before quitting the program and while exactly one thread is
101 33231500 Michael Hanselmann
  running.
102 4331f6cd Michael Hanselmann

103 4331f6cd Michael Hanselmann
  """
104 33231500 Michael Hanselmann
  pycurl.global_cleanup()
105 33231500 Michael Hanselmann
106 33231500 Michael Hanselmann
107 33231500 Michael Hanselmann
def _ConfigRpcCurl(curl):
108 33231500 Michael Hanselmann
  noded_cert = str(constants.NODED_CERT_FILE)
109 4331f6cd Michael Hanselmann
110 33231500 Michael Hanselmann
  curl.setopt(pycurl.FOLLOWLOCATION, False)
111 33231500 Michael Hanselmann
  curl.setopt(pycurl.CAINFO, noded_cert)
112 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSL_VERIFYHOST, 0)
113 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSL_VERIFYPEER, True)
114 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLCERTTYPE, "PEM")
115 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLCERT, noded_cert)
116 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLKEYTYPE, "PEM")
117 33231500 Michael Hanselmann
  curl.setopt(pycurl.SSLKEY, noded_cert)
118 33231500 Michael Hanselmann
  curl.setopt(pycurl.CONNECTTIMEOUT, _RPC_CONNECT_TIMEOUT)
119 33231500 Michael Hanselmann
120 33231500 Michael Hanselmann
121 e0e916fe Iustin Pop
def RunWithRPC(fn):
122 e0e916fe Iustin Pop
  """RPC-wrapper decorator.
123 e0e916fe Iustin Pop

124 e0e916fe Iustin Pop
  When applied to a function, it runs it with the RPC system
125 e0e916fe Iustin Pop
  initialized, and it shutsdown the system afterwards. This means the
126 e0e916fe Iustin Pop
  function must be called without RPC being initialized.
127 e0e916fe Iustin Pop

128 e0e916fe Iustin Pop
  """
129 e0e916fe Iustin Pop
  def wrapper(*args, **kwargs):
130 e0e916fe Iustin Pop
    Init()
131 e0e916fe Iustin Pop
    try:
132 e0e916fe Iustin Pop
      return fn(*args, **kwargs)
133 e0e916fe Iustin Pop
    finally:
134 e0e916fe Iustin Pop
      Shutdown()
135 e0e916fe Iustin Pop
  return wrapper
136 e0e916fe Iustin Pop
137 e0e916fe Iustin Pop
138 30474135 Michael Hanselmann
def _Compress(data):
139 30474135 Michael Hanselmann
  """Compresses a string for transport over RPC.
140 30474135 Michael Hanselmann

141 30474135 Michael Hanselmann
  Small amounts of data are not compressed.
142 30474135 Michael Hanselmann

143 30474135 Michael Hanselmann
  @type data: str
144 30474135 Michael Hanselmann
  @param data: Data
145 30474135 Michael Hanselmann
  @rtype: tuple
146 30474135 Michael Hanselmann
  @return: Encoded data to send
147 30474135 Michael Hanselmann

148 30474135 Michael Hanselmann
  """
149 30474135 Michael Hanselmann
  # Small amounts of data are not compressed
150 30474135 Michael Hanselmann
  if len(data) < 512:
151 30474135 Michael Hanselmann
    return (constants.RPC_ENCODING_NONE, data)
152 30474135 Michael Hanselmann
153 30474135 Michael Hanselmann
  # Compress with zlib and encode in base64
154 30474135 Michael Hanselmann
  return (constants.RPC_ENCODING_ZLIB_BASE64,
155 30474135 Michael Hanselmann
          base64.b64encode(zlib.compress(data, 3)))
156 30474135 Michael Hanselmann
157 30474135 Michael Hanselmann
158 781de953 Iustin Pop
class RpcResult(object):
159 781de953 Iustin Pop
  """RPC Result class.
160 781de953 Iustin Pop

161 781de953 Iustin Pop
  This class holds an RPC result. It is needed since in multi-node
162 781de953 Iustin Pop
  calls we can't raise an exception just because one one out of many
163 781de953 Iustin Pop
  failed, and therefore we use this class to encapsulate the result.
164 781de953 Iustin Pop

165 5bbd3f7f Michael Hanselmann
  @ivar data: the data payload, for successful results, or None
166 ed83f5cc Iustin Pop
  @ivar call: the name of the RPC call
167 ed83f5cc Iustin Pop
  @ivar node: the name of the node to which we made the call
168 ed83f5cc Iustin Pop
  @ivar offline: whether the operation failed because the node was
169 ed83f5cc Iustin Pop
      offline, as opposed to actual failure; offline=True will always
170 ed83f5cc Iustin Pop
      imply failed=True, in order to allow simpler checking if
171 ed83f5cc Iustin Pop
      the user doesn't care about the exact failure mode
172 4c4e4e1e Iustin Pop
  @ivar fail_msg: the error message if the call failed
173 ed83f5cc Iustin Pop

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

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

223 781de953 Iustin Pop
    """
224 4c4e4e1e Iustin Pop
    if not self.fail_msg:
225 4c4e4e1e Iustin Pop
      return
226 4c4e4e1e Iustin Pop
227 4c4e4e1e Iustin Pop
    if not msg: # one could pass None for default message
228 4c4e4e1e Iustin Pop
      msg = ("Call '%s' to node '%s' has failed: %s" %
229 4c4e4e1e Iustin Pop
             (self.call, self.node, self.fail_msg))
230 4c4e4e1e Iustin Pop
    else:
231 4c4e4e1e Iustin Pop
      msg = "%s: %s" % (msg, self.fail_msg)
232 4c4e4e1e Iustin Pop
    if prereq:
233 4c4e4e1e Iustin Pop
      ec = errors.OpPrereqError
234 4c4e4e1e Iustin Pop
    else:
235 4c4e4e1e Iustin Pop
      ec = errors.OpExecError
236 045dd6d9 Iustin Pop
    if ecode is not None:
237 27137e55 Iustin Pop
      args = (msg, ecode)
238 045dd6d9 Iustin Pop
    else:
239 045dd6d9 Iustin Pop
      args = (msg, )
240 b459a848 Andrea Spadaccini
    raise ec(*args) # pylint: disable=W0142
241 781de953 Iustin Pop
242 781de953 Iustin Pop
243 fce5efd1 Michael Hanselmann
def _SsconfResolver(node_list, _,
244 00267bfe Michael Hanselmann
                    ssc=ssconf.SimpleStore,
245 00267bfe Michael Hanselmann
                    nslookup_fn=netutils.Hostname.GetIP):
246 eb202c13 Manuel Franceschini
  """Return addresses for given node names.
247 eb202c13 Manuel Franceschini

248 eb202c13 Manuel Franceschini
  @type node_list: list
249 eb202c13 Manuel Franceschini
  @param node_list: List of node names
250 eb202c13 Manuel Franceschini
  @type ssc: class
251 eb202c13 Manuel Franceschini
  @param ssc: SimpleStore class that is used to obtain node->ip mappings
252 17f7fd27 Manuel Franceschini
  @type nslookup_fn: callable
253 17f7fd27 Manuel Franceschini
  @param nslookup_fn: function use to do NS lookup
254 00267bfe Michael Hanselmann
  @rtype: list of tuple; (string, string)
255 00267bfe Michael Hanselmann
  @return: List of tuples containing node name and IP address
256 eb202c13 Manuel Franceschini

257 eb202c13 Manuel Franceschini
  """
258 b43dcc5a Manuel Franceschini
  ss = ssc()
259 b43dcc5a Manuel Franceschini
  iplist = ss.GetNodePrimaryIPList()
260 b43dcc5a Manuel Franceschini
  family = ss.GetPrimaryIPFamily()
261 b705c7a6 Manuel Franceschini
  ipmap = dict(entry.split() for entry in iplist)
262 00267bfe Michael Hanselmann
263 00267bfe Michael Hanselmann
  result = []
264 b705c7a6 Manuel Franceschini
  for node in node_list:
265 00267bfe Michael Hanselmann
    ip = ipmap.get(node)
266 00267bfe Michael Hanselmann
    if ip is None:
267 00267bfe Michael Hanselmann
      ip = nslookup_fn(node, family=family)
268 00267bfe Michael Hanselmann
    result.append((node, ip))
269 00267bfe Michael Hanselmann
270 00267bfe Michael Hanselmann
  return result
271 00267bfe Michael Hanselmann
272 00267bfe Michael Hanselmann
273 00267bfe Michael Hanselmann
class _StaticResolver:
274 00267bfe Michael Hanselmann
  def __init__(self, addresses):
275 00267bfe Michael Hanselmann
    """Initializes this class.
276 00267bfe Michael Hanselmann

277 00267bfe Michael Hanselmann
    """
278 00267bfe Michael Hanselmann
    self._addresses = addresses
279 00267bfe Michael Hanselmann
280 fce5efd1 Michael Hanselmann
  def __call__(self, hosts, _):
281 00267bfe Michael Hanselmann
    """Returns static addresses for hosts.
282 00267bfe Michael Hanselmann

283 00267bfe Michael Hanselmann
    """
284 00267bfe Michael Hanselmann
    assert len(hosts) == len(self._addresses)
285 00267bfe Michael Hanselmann
    return zip(hosts, self._addresses)
286 00267bfe Michael Hanselmann
287 eb202c13 Manuel Franceschini
288 890ea4ce Michael Hanselmann
def _CheckConfigNode(name, node, accept_offline_node):
289 00267bfe Michael Hanselmann
  """Checks if a node is online.
290 eb202c13 Manuel Franceschini

291 00267bfe Michael Hanselmann
  @type name: string
292 00267bfe Michael Hanselmann
  @param name: Node name
293 00267bfe Michael Hanselmann
  @type node: L{objects.Node} or None
294 00267bfe Michael Hanselmann
  @param node: Node object
295 eb202c13 Manuel Franceschini

296 00267bfe Michael Hanselmann
  """
297 00267bfe Michael Hanselmann
  if node is None:
298 00267bfe Michael Hanselmann
    # Depend on DNS for name resolution
299 00267bfe Michael Hanselmann
    ip = name
300 890ea4ce Michael Hanselmann
  elif node.offline and not accept_offline_node:
301 00267bfe Michael Hanselmann
    ip = _OFFLINE
302 00267bfe Michael Hanselmann
  else:
303 00267bfe Michael Hanselmann
    ip = node.primary_ip
304 00267bfe Michael Hanselmann
  return (name, ip)
305 a8083063 Iustin Pop
306 a8083063 Iustin Pop
307 890ea4ce Michael Hanselmann
def _NodeConfigResolver(single_node_fn, all_nodes_fn, hosts, opts):
308 00267bfe Michael Hanselmann
  """Calculate node addresses using configuration.
309 a8083063 Iustin Pop

310 a8083063 Iustin Pop
  """
311 890ea4ce Michael Hanselmann
  accept_offline_node = (opts is rpc_defs.ACCEPT_OFFLINE_NODE)
312 890ea4ce Michael Hanselmann
313 890ea4ce Michael Hanselmann
  assert accept_offline_node or opts is None, "Unknown option"
314 890ea4ce Michael Hanselmann
315 00267bfe Michael Hanselmann
  # Special case for single-host lookups
316 00267bfe Michael Hanselmann
  if len(hosts) == 1:
317 00267bfe Michael Hanselmann
    (name, ) = hosts
318 890ea4ce Michael Hanselmann
    return [_CheckConfigNode(name, single_node_fn(name), accept_offline_node)]
319 00267bfe Michael Hanselmann
  else:
320 00267bfe Michael Hanselmann
    all_nodes = all_nodes_fn()
321 890ea4ce Michael Hanselmann
    return [_CheckConfigNode(name, all_nodes.get(name, None),
322 890ea4ce Michael Hanselmann
                             accept_offline_node)
323 00267bfe Michael Hanselmann
            for name in hosts]
324 00267bfe Michael Hanselmann
325 00267bfe Michael Hanselmann
326 00267bfe Michael Hanselmann
class _RpcProcessor:
327 aea5caef Michael Hanselmann
  def __init__(self, resolver, port, lock_monitor_cb=None):
328 00267bfe Michael Hanselmann
    """Initializes this class.
329 00267bfe Michael Hanselmann

330 00267bfe Michael Hanselmann
    @param resolver: callable accepting a list of hostnames, returning a list
331 00267bfe Michael Hanselmann
      of tuples containing name and IP address (IP address can be the name or
332 00267bfe Michael Hanselmann
      the special value L{_OFFLINE} to mark offline machines)
333 00267bfe Michael Hanselmann
    @type port: int
334 00267bfe Michael Hanselmann
    @param port: TCP port
335 aea5caef Michael Hanselmann
    @param lock_monitor_cb: Callable for registering with lock monitor
336 3ef3c771 Iustin Pop

337 a8083063 Iustin Pop
    """
338 00267bfe Michael Hanselmann
    self._resolver = resolver
339 00267bfe Michael Hanselmann
    self._port = port
340 aea5caef Michael Hanselmann
    self._lock_monitor_cb = lock_monitor_cb
341 eb202c13 Manuel Franceschini
342 00267bfe Michael Hanselmann
  @staticmethod
343 00267bfe Michael Hanselmann
  def _PrepareRequests(hosts, port, procedure, body, read_timeout):
344 00267bfe Michael Hanselmann
    """Prepares requests by sorting offline hosts into separate list.
345 eb202c13 Manuel Franceschini

346 d9de612c Iustin Pop
    @type body: dict
347 d9de612c Iustin Pop
    @param body: a dictionary with per-host body data
348 d9de612c Iustin Pop

349 00267bfe Michael Hanselmann
    """
350 00267bfe Michael Hanselmann
    results = {}
351 00267bfe Michael Hanselmann
    requests = {}
352 bdf7d8c0 Iustin Pop
353 d9de612c Iustin Pop
    assert isinstance(body, dict)
354 d9de612c Iustin Pop
    assert len(body) == len(hosts)
355 d9de612c Iustin Pop
    assert compat.all(isinstance(v, str) for v in body.values())
356 d9de612c Iustin Pop
    assert frozenset(map(compat.fst, hosts)) == frozenset(body.keys()), \
357 d9de612c Iustin Pop
        "%s != %s" % (hosts, body.keys())
358 d9de612c Iustin Pop
359 00267bfe Michael Hanselmann
    for (name, ip) in hosts:
360 00267bfe Michael Hanselmann
      if ip is _OFFLINE:
361 00267bfe Michael Hanselmann
        # Node is marked as offline
362 00267bfe Michael Hanselmann
        results[name] = RpcResult(node=name, offline=True, call=procedure)
363 00267bfe Michael Hanselmann
      else:
364 00267bfe Michael Hanselmann
        requests[name] = \
365 00267bfe Michael Hanselmann
          http.client.HttpClientRequest(str(ip), port,
366 00267bfe Michael Hanselmann
                                        http.HTTP_PUT, str("/%s" % procedure),
367 00267bfe Michael Hanselmann
                                        headers=_RPC_CLIENT_HEADERS,
368 d9de612c Iustin Pop
                                        post_data=body[name],
369 7cb2d205 Michael Hanselmann
                                        read_timeout=read_timeout,
370 abbf2cd9 Michael Hanselmann
                                        nicename="%s/%s" % (name, procedure),
371 abbf2cd9 Michael Hanselmann
                                        curl_config_fn=_ConfigRpcCurl)
372 a8083063 Iustin Pop
373 00267bfe Michael Hanselmann
    return (results, requests)
374 00267bfe Michael Hanselmann
375 00267bfe Michael Hanselmann
  @staticmethod
376 00267bfe Michael Hanselmann
  def _CombineResults(results, requests, procedure):
377 00267bfe Michael Hanselmann
    """Combines pre-computed results for offline hosts with actual call results.
378 bdf7d8c0 Iustin Pop

379 a8083063 Iustin Pop
    """
380 00267bfe Michael Hanselmann
    for name, req in requests.items():
381 00267bfe Michael Hanselmann
      if req.success and req.resp_status_code == http.HTTP_OK:
382 00267bfe Michael Hanselmann
        host_result = RpcResult(data=serializer.LoadJson(req.resp_body),
383 00267bfe Michael Hanselmann
                                node=name, call=procedure)
384 00267bfe Michael Hanselmann
      else:
385 00267bfe Michael Hanselmann
        # TODO: Better error reporting
386 00267bfe Michael Hanselmann
        if req.error:
387 00267bfe Michael Hanselmann
          msg = req.error
388 00267bfe Michael Hanselmann
        else:
389 00267bfe Michael Hanselmann
          msg = req.resp_body
390 eb202c13 Manuel Franceschini
391 00267bfe Michael Hanselmann
        logging.error("RPC error in %s on node %s: %s", procedure, name, msg)
392 00267bfe Michael Hanselmann
        host_result = RpcResult(data=msg, failed=True, node=name,
393 00267bfe Michael Hanselmann
                                call=procedure)
394 ecfe9491 Michael Hanselmann
395 00267bfe Michael Hanselmann
      results[name] = host_result
396 92fd2250 Iustin Pop
397 00267bfe Michael Hanselmann
    return results
398 a8083063 Iustin Pop
399 fce5efd1 Michael Hanselmann
  def __call__(self, hosts, procedure, body, read_timeout, resolver_opts,
400 065be3f0 Michael Hanselmann
               _req_process_fn=None):
401 00267bfe Michael Hanselmann
    """Makes an RPC request to a number of nodes.
402 ecfe9491 Michael Hanselmann

403 00267bfe Michael Hanselmann
    @type hosts: sequence
404 00267bfe Michael Hanselmann
    @param hosts: Hostnames
405 00267bfe Michael Hanselmann
    @type procedure: string
406 00267bfe Michael Hanselmann
    @param procedure: Request path
407 d9de612c Iustin Pop
    @type body: dictionary
408 d9de612c Iustin Pop
    @param body: dictionary with request bodies per host
409 00267bfe Michael Hanselmann
    @type read_timeout: int or None
410 00267bfe Michael Hanselmann
    @param read_timeout: Read timeout for request
411 a8083063 Iustin Pop

412 a8083063 Iustin Pop
    """
413 83e7af18 Michael Hanselmann
    assert read_timeout is not None, \
414 83e7af18 Michael Hanselmann
      "Missing RPC read timeout for procedure '%s'" % procedure
415 a8083063 Iustin Pop
416 065be3f0 Michael Hanselmann
    if _req_process_fn is None:
417 065be3f0 Michael Hanselmann
      _req_process_fn = http.client.ProcessRequests
418 065be3f0 Michael Hanselmann
419 00267bfe Michael Hanselmann
    (results, requests) = \
420 fce5efd1 Michael Hanselmann
      self._PrepareRequests(self._resolver(hosts, resolver_opts), self._port,
421 fce5efd1 Michael Hanselmann
                            procedure, body, read_timeout)
422 a8083063 Iustin Pop
423 abbf2cd9 Michael Hanselmann
    _req_process_fn(requests.values(), lock_monitor_cb=self._lock_monitor_cb)
424 a8083063 Iustin Pop
425 00267bfe Michael Hanselmann
    assert not frozenset(results).intersection(requests)
426 ecfe9491 Michael Hanselmann
427 00267bfe Michael Hanselmann
    return self._CombineResults(results, requests, procedure)
428 a8083063 Iustin Pop
429 a8083063 Iustin Pop
430 cd40dc53 Michael Hanselmann
class _RpcClientBase:
431 065be3f0 Michael Hanselmann
  def __init__(self, resolver, encoder_fn, lock_monitor_cb=None,
432 065be3f0 Michael Hanselmann
               _req_process_fn=None):
433 cd40dc53 Michael Hanselmann
    """Initializes this class.
434 cd40dc53 Michael Hanselmann

435 cd40dc53 Michael Hanselmann
    """
436 065be3f0 Michael Hanselmann
    proc = _RpcProcessor(resolver,
437 065be3f0 Michael Hanselmann
                         netutils.GetDaemonPort(constants.NODED),
438 065be3f0 Michael Hanselmann
                         lock_monitor_cb=lock_monitor_cb)
439 065be3f0 Michael Hanselmann
    self._proc = compat.partial(proc, _req_process_fn=_req_process_fn)
440 cd40dc53 Michael Hanselmann
    self._encoder = compat.partial(self._EncodeArg, encoder_fn)
441 cd40dc53 Michael Hanselmann
442 cd40dc53 Michael Hanselmann
  @staticmethod
443 cd40dc53 Michael Hanselmann
  def _EncodeArg(encoder_fn, (argkind, value)):
444 cd40dc53 Michael Hanselmann
    """Encode argument.
445 cd40dc53 Michael Hanselmann

446 cd40dc53 Michael Hanselmann
    """
447 cd40dc53 Michael Hanselmann
    if argkind is None:
448 cd40dc53 Michael Hanselmann
      return value
449 cd40dc53 Michael Hanselmann
    else:
450 cd40dc53 Michael Hanselmann
      return encoder_fn(argkind)(value)
451 cd40dc53 Michael Hanselmann
452 f7d9b3aa Michael Hanselmann
  def _Call(self, cdef, node_list, args):
453 cd40dc53 Michael Hanselmann
    """Entry point for automatically generated RPC wrappers.
454 cd40dc53 Michael Hanselmann

455 cd40dc53 Michael Hanselmann
    """
456 dd6d2d09 Michael Hanselmann
    (procedure, _, resolver_opts, timeout, argdefs,
457 dd6d2d09 Michael Hanselmann
     prep_fn, postproc_fn, _) = cdef
458 f7d9b3aa Michael Hanselmann
459 f7d9b3aa Michael Hanselmann
    if callable(timeout):
460 f7d9b3aa Michael Hanselmann
      read_timeout = timeout(args)
461 f7d9b3aa Michael Hanselmann
    else:
462 f7d9b3aa Michael Hanselmann
      read_timeout = timeout
463 cd40dc53 Michael Hanselmann
464 dd6d2d09 Michael Hanselmann
    if callable(resolver_opts):
465 dd6d2d09 Michael Hanselmann
      req_resolver_opts = resolver_opts(args)
466 dd6d2d09 Michael Hanselmann
    else:
467 dd6d2d09 Michael Hanselmann
      req_resolver_opts = resolver_opts
468 dd6d2d09 Michael Hanselmann
469 e78667fe Michael Hanselmann
    if len(args) != len(argdefs):
470 e78667fe Michael Hanselmann
      raise errors.ProgrammerError("Number of passed arguments doesn't match")
471 e78667fe Michael Hanselmann
472 d9de612c Iustin Pop
    enc_args = map(self._encoder, zip(map(compat.snd, argdefs), args))
473 d9de612c Iustin Pop
    if prep_fn is None:
474 d9de612c Iustin Pop
      # for a no-op prep_fn, we serialise the body once, and then we
475 d9de612c Iustin Pop
      # reuse it in the dictionary values
476 d9de612c Iustin Pop
      body = serializer.DumpJson(enc_args)
477 d9de612c Iustin Pop
      pnbody = dict((n, body) for n in node_list)
478 d9de612c Iustin Pop
    else:
479 d9de612c Iustin Pop
      # for a custom prep_fn, we pass the encoded arguments and the
480 d9de612c Iustin Pop
      # node name to the prep_fn, and we serialise its return value
481 764ff2eb Michael Hanselmann
      assert callable(prep_fn)
482 d9de612c Iustin Pop
      pnbody = dict((n, serializer.DumpJson(prep_fn(n, enc_args)))
483 d9de612c Iustin Pop
                    for n in node_list)
484 d9de612c Iustin Pop
485 fce5efd1 Michael Hanselmann
    result = self._proc(node_list, procedure, pnbody, read_timeout,
486 fce5efd1 Michael Hanselmann
                        req_resolver_opts)
487 26d502d0 Michael Hanselmann
488 26d502d0 Michael Hanselmann
    if postproc_fn:
489 d9da5065 Michael Hanselmann
      return dict(map(lambda (key, value): (key, postproc_fn(value)),
490 d9da5065 Michael Hanselmann
                      result.items()))
491 26d502d0 Michael Hanselmann
    else:
492 26d502d0 Michael Hanselmann
      return result
493 cd40dc53 Michael Hanselmann
494 cd40dc53 Michael Hanselmann
495 cd40dc53 Michael Hanselmann
def _ObjectToDict(value):
496 cd40dc53 Michael Hanselmann
  """Converts an object to a dictionary.
497 cd40dc53 Michael Hanselmann

498 cd40dc53 Michael Hanselmann
  @note: See L{objects}.
499 cd40dc53 Michael Hanselmann

500 cd40dc53 Michael Hanselmann
  """
501 cd40dc53 Michael Hanselmann
  return value.ToDict()
502 cd40dc53 Michael Hanselmann
503 cd40dc53 Michael Hanselmann
504 cd40dc53 Michael Hanselmann
def _ObjectListToDict(value):
505 cd40dc53 Michael Hanselmann
  """Converts a list of L{objects} to dictionaries.
506 cd40dc53 Michael Hanselmann

507 cd40dc53 Michael Hanselmann
  """
508 cd40dc53 Michael Hanselmann
  return map(_ObjectToDict, value)
509 cd40dc53 Michael Hanselmann
510 cd40dc53 Michael Hanselmann
511 cd40dc53 Michael Hanselmann
def _EncodeNodeToDiskDict(value):
512 cd40dc53 Michael Hanselmann
  """Encodes a dictionary with node name as key and disk objects as values.
513 cd40dc53 Michael Hanselmann

514 cd40dc53 Michael Hanselmann
  """
515 cd40dc53 Michael Hanselmann
  return dict((name, _ObjectListToDict(disks))
516 cd40dc53 Michael Hanselmann
              for name, disks in value.items())
517 cd40dc53 Michael Hanselmann
518 cd40dc53 Michael Hanselmann
519 cd40dc53 Michael Hanselmann
def _PrepareFileUpload(filename):
520 cd40dc53 Michael Hanselmann
  """Loads a file and prepares it for an upload to nodes.
521 cd40dc53 Michael Hanselmann

522 cd40dc53 Michael Hanselmann
  """
523 cd40dc53 Michael Hanselmann
  data = _Compress(utils.ReadFile(filename))
524 cd40dc53 Michael Hanselmann
  st = os.stat(filename)
525 cd40dc53 Michael Hanselmann
  getents = runtime.GetEnts()
526 cd40dc53 Michael Hanselmann
  return [filename, data, st.st_mode, getents.LookupUid(st.st_uid),
527 cd40dc53 Michael Hanselmann
          getents.LookupGid(st.st_gid), st.st_atime, st.st_mtime]
528 cd40dc53 Michael Hanselmann
529 cd40dc53 Michael Hanselmann
530 cd40dc53 Michael Hanselmann
def _PrepareFinalizeExportDisks(snap_disks):
531 cd40dc53 Michael Hanselmann
  """Encodes disks for finalizing export.
532 cd40dc53 Michael Hanselmann

533 cd40dc53 Michael Hanselmann
  """
534 cd40dc53 Michael Hanselmann
  flat_disks = []
535 cd40dc53 Michael Hanselmann
536 cd40dc53 Michael Hanselmann
  for disk in snap_disks:
537 cd40dc53 Michael Hanselmann
    if isinstance(disk, bool):
538 cd40dc53 Michael Hanselmann
      flat_disks.append(disk)
539 cd40dc53 Michael Hanselmann
    else:
540 cd40dc53 Michael Hanselmann
      flat_disks.append(disk.ToDict())
541 cd40dc53 Michael Hanselmann
542 cd40dc53 Michael Hanselmann
  return flat_disks
543 cd40dc53 Michael Hanselmann
544 cd40dc53 Michael Hanselmann
545 cd40dc53 Michael Hanselmann
def _EncodeImportExportIO((ieio, ieioargs)):
546 cd40dc53 Michael Hanselmann
  """Encodes import/export I/O information.
547 cd40dc53 Michael Hanselmann

548 cd40dc53 Michael Hanselmann
  """
549 cd40dc53 Michael Hanselmann
  if ieio == constants.IEIO_RAW_DISK:
550 cd40dc53 Michael Hanselmann
    assert len(ieioargs) == 1
551 cd40dc53 Michael Hanselmann
    return (ieio, (ieioargs[0].ToDict(), ))
552 cd40dc53 Michael Hanselmann
553 cd40dc53 Michael Hanselmann
  if ieio == constants.IEIO_SCRIPT:
554 cd40dc53 Michael Hanselmann
    assert len(ieioargs) == 2
555 cd40dc53 Michael Hanselmann
    return (ieio, (ieioargs[0].ToDict(), ieioargs[1]))
556 cd40dc53 Michael Hanselmann
557 cd40dc53 Michael Hanselmann
  return (ieio, ieioargs)
558 cd40dc53 Michael Hanselmann
559 cd40dc53 Michael Hanselmann
560 cd40dc53 Michael Hanselmann
def _EncodeBlockdevRename(value):
561 cd40dc53 Michael Hanselmann
  """Encodes information for renaming block devices.
562 cd40dc53 Michael Hanselmann

563 cd40dc53 Michael Hanselmann
  """
564 cd40dc53 Michael Hanselmann
  return [(d.ToDict(), uid) for d, uid in value]
565 cd40dc53 Michael Hanselmann
566 cd40dc53 Michael Hanselmann
567 cd40dc53 Michael Hanselmann
#: Generic encoders
568 cd40dc53 Michael Hanselmann
_ENCODERS = {
569 cd40dc53 Michael Hanselmann
  rpc_defs.ED_OBJECT_DICT: _ObjectToDict,
570 cd40dc53 Michael Hanselmann
  rpc_defs.ED_OBJECT_DICT_LIST: _ObjectListToDict,
571 cd40dc53 Michael Hanselmann
  rpc_defs.ED_NODE_TO_DISK_DICT: _EncodeNodeToDiskDict,
572 cd40dc53 Michael Hanselmann
  rpc_defs.ED_FILE_DETAILS: _PrepareFileUpload,
573 cd40dc53 Michael Hanselmann
  rpc_defs.ED_COMPRESS: _Compress,
574 cd40dc53 Michael Hanselmann
  rpc_defs.ED_FINALIZE_EXPORT_DISKS: _PrepareFinalizeExportDisks,
575 cd40dc53 Michael Hanselmann
  rpc_defs.ED_IMPEXP_IO: _EncodeImportExportIO,
576 cd40dc53 Michael Hanselmann
  rpc_defs.ED_BLOCKDEV_RENAME: _EncodeBlockdevRename,
577 cd40dc53 Michael Hanselmann
  }
578 cd40dc53 Michael Hanselmann
579 cd40dc53 Michael Hanselmann
580 cd40dc53 Michael Hanselmann
class RpcRunner(_RpcClientBase,
581 cd40dc53 Michael Hanselmann
                _generated_rpc.RpcClientDefault,
582 415a7304 Michael Hanselmann
                _generated_rpc.RpcClientBootstrap,
583 415a7304 Michael Hanselmann
                _generated_rpc.RpcClientConfig):
584 87b3cb26 Michael Hanselmann
  """RPC runner class.
585 a8083063 Iustin Pop

586 87b3cb26 Michael Hanselmann
  """
587 87b3cb26 Michael Hanselmann
  def __init__(self, context):
588 87b3cb26 Michael Hanselmann
    """Initialized the RPC runner.
589 a8083063 Iustin Pop

590 87b3cb26 Michael Hanselmann
    @type context: C{masterd.GanetiContext}
591 87b3cb26 Michael Hanselmann
    @param context: Ganeti context
592 a8083063 Iustin Pop

593 72737a7f Iustin Pop
    """
594 cd40dc53 Michael Hanselmann
    self._cfg = context.cfg
595 cd40dc53 Michael Hanselmann
596 cd40dc53 Michael Hanselmann
    encoders = _ENCODERS.copy()
597 cd40dc53 Michael Hanselmann
598 cd40dc53 Michael Hanselmann
    # Add encoders requiring configuration object
599 cd40dc53 Michael Hanselmann
    encoders.update({
600 cd40dc53 Michael Hanselmann
      rpc_defs.ED_INST_DICT: self._InstDict,
601 cd40dc53 Michael Hanselmann
      rpc_defs.ED_INST_DICT_HVP_BEP: self._InstDictHvpBep,
602 cd40dc53 Michael Hanselmann
      rpc_defs.ED_INST_DICT_OSP: self._InstDictOsp,
603 cd40dc53 Michael Hanselmann
      })
604 cd40dc53 Michael Hanselmann
605 cd40dc53 Michael Hanselmann
    # Resolver using configuration
606 cd40dc53 Michael Hanselmann
    resolver = compat.partial(_NodeConfigResolver, self._cfg.GetNodeInfo,
607 cd40dc53 Michael Hanselmann
                              self._cfg.GetAllNodesInfo)
608 cd40dc53 Michael Hanselmann
609 db04ce5d Michael Hanselmann
    # Pylint doesn't recognize multiple inheritance properly, see
610 db04ce5d Michael Hanselmann
    # <http://www.logilab.org/ticket/36586> and
611 db04ce5d Michael Hanselmann
    # <http://www.logilab.org/ticket/35642>
612 db04ce5d Michael Hanselmann
    # pylint: disable=W0233
613 cd40dc53 Michael Hanselmann
    _RpcClientBase.__init__(self, resolver, encoders.get,
614 cd40dc53 Michael Hanselmann
                            lock_monitor_cb=context.glm.AddToLockMonitor)
615 415a7304 Michael Hanselmann
    _generated_rpc.RpcClientConfig.__init__(self)
616 db04ce5d Michael Hanselmann
    _generated_rpc.RpcClientBootstrap.__init__(self)
617 200de241 Michael Hanselmann
    _generated_rpc.RpcClientDefault.__init__(self)
618 200de241 Michael Hanselmann
619 1bdcbbab Iustin Pop
  def _InstDict(self, instance, hvp=None, bep=None, osp=None):
620 26ba2bd8 Iustin Pop
    """Convert the given instance to a dict.
621 26ba2bd8 Iustin Pop

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

625 26ba2bd8 Iustin Pop
    @type instance: L{objects.Instance}
626 26ba2bd8 Iustin Pop
    @param instance: an Instance object
627 0eca8e0c Iustin Pop
    @type hvp: dict or None
628 5bbd3f7f Michael Hanselmann
    @param hvp: a dictionary with overridden hypervisor parameters
629 0eca8e0c Iustin Pop
    @type bep: dict or None
630 5bbd3f7f Michael Hanselmann
    @param bep: a dictionary with overridden backend parameters
631 1bdcbbab Iustin Pop
    @type osp: dict or None
632 8d8c4eff Michael Hanselmann
    @param osp: a dictionary with overridden os parameters
633 26ba2bd8 Iustin Pop
    @rtype: dict
634 26ba2bd8 Iustin Pop
    @return: the instance dict, with the hvparams filled with the
635 26ba2bd8 Iustin Pop
        cluster defaults
636 26ba2bd8 Iustin Pop

637 26ba2bd8 Iustin Pop
    """
638 26ba2bd8 Iustin Pop
    idict = instance.ToDict()
639 5b442704 Iustin Pop
    cluster = self._cfg.GetClusterInfo()
640 5b442704 Iustin Pop
    idict["hvparams"] = cluster.FillHV(instance)
641 0eca8e0c Iustin Pop
    if hvp is not None:
642 0eca8e0c Iustin Pop
      idict["hvparams"].update(hvp)
643 5b442704 Iustin Pop
    idict["beparams"] = cluster.FillBE(instance)
644 0eca8e0c Iustin Pop
    if bep is not None:
645 0eca8e0c Iustin Pop
      idict["beparams"].update(bep)
646 1bdcbbab Iustin Pop
    idict["osparams"] = cluster.SimpleFillOS(instance.os, instance.osparams)
647 1bdcbbab Iustin Pop
    if osp is not None:
648 1bdcbbab Iustin Pop
      idict["osparams"].update(osp)
649 b848ce79 Guido Trotter
    for nic in idict["nics"]:
650 b848ce79 Guido Trotter
      nic['nicparams'] = objects.FillDict(
651 b848ce79 Guido Trotter
        cluster.nicparams[constants.PP_DEFAULT],
652 b848ce79 Guido Trotter
        nic['nicparams'])
653 26ba2bd8 Iustin Pop
    return idict
654 26ba2bd8 Iustin Pop
655 c4de9b7a Michael Hanselmann
  def _InstDictHvpBep(self, (instance, hvp, bep)):
656 c4de9b7a Michael Hanselmann
    """Wrapper for L{_InstDict}.
657 c4de9b7a Michael Hanselmann

658 c4de9b7a Michael Hanselmann
    """
659 c4de9b7a Michael Hanselmann
    return self._InstDict(instance, hvp=hvp, bep=bep)
660 c4de9b7a Michael Hanselmann
661 c4de9b7a Michael Hanselmann
  def _InstDictOsp(self, (instance, osparams)):
662 c4de9b7a Michael Hanselmann
    """Wrapper for L{_InstDict}.
663 c4de9b7a Michael Hanselmann

664 c4de9b7a Michael Hanselmann
    """
665 c4de9b7a Michael Hanselmann
    return self._InstDict(instance, osp=osparams)
666 c4de9b7a Michael Hanselmann
667 fb1ffbca Michael Hanselmann
668 cd40dc53 Michael Hanselmann
class JobQueueRunner(_RpcClientBase, _generated_rpc.RpcClientJobQueue):
669 fb1ffbca Michael Hanselmann
  """RPC wrappers for job queue.
670 fb1ffbca Michael Hanselmann

671 fb1ffbca Michael Hanselmann
  """
672 fb1ffbca Michael Hanselmann
  def __init__(self, context, address_list):
673 fb1ffbca Michael Hanselmann
    """Initializes this class.
674 fb1ffbca Michael Hanselmann

675 fb1ffbca Michael Hanselmann
    """
676 fb1ffbca Michael Hanselmann
    if address_list is None:
677 fb1ffbca Michael Hanselmann
      resolver = _SsconfResolver
678 fb1ffbca Michael Hanselmann
    else:
679 fb1ffbca Michael Hanselmann
      # Caller provided an address list
680 fb1ffbca Michael Hanselmann
      resolver = _StaticResolver(address_list)
681 fb1ffbca Michael Hanselmann
682 cd40dc53 Michael Hanselmann
    _RpcClientBase.__init__(self, resolver, _ENCODERS.get,
683 cd40dc53 Michael Hanselmann
                            lock_monitor_cb=context.glm.AddToLockMonitor)
684 cd40dc53 Michael Hanselmann
    _generated_rpc.RpcClientJobQueue.__init__(self)
685 db04ce5d Michael Hanselmann
686 db04ce5d Michael Hanselmann
687 cd40dc53 Michael Hanselmann
class BootstrapRunner(_RpcClientBase, _generated_rpc.RpcClientBootstrap):
688 db04ce5d Michael Hanselmann
  """RPC wrappers for bootstrapping.
689 db04ce5d Michael Hanselmann

690 db04ce5d Michael Hanselmann
  """
691 db04ce5d Michael Hanselmann
  def __init__(self):
692 db04ce5d Michael Hanselmann
    """Initializes this class.
693 db04ce5d Michael Hanselmann

694 db04ce5d Michael Hanselmann
    """
695 cd40dc53 Michael Hanselmann
    _RpcClientBase.__init__(self, _SsconfResolver, _ENCODERS.get)
696 db04ce5d Michael Hanselmann
    _generated_rpc.RpcClientBootstrap.__init__(self)
697 db04ce5d Michael Hanselmann
698 415a7304 Michael Hanselmann
699 cd40dc53 Michael Hanselmann
class ConfigRunner(_RpcClientBase, _generated_rpc.RpcClientConfig):
700 415a7304 Michael Hanselmann
  """RPC wrappers for L{config}.
701 415a7304 Michael Hanselmann

702 415a7304 Michael Hanselmann
  """
703 b2acdbdc Michael Hanselmann
  def __init__(self, context, address_list):
704 415a7304 Michael Hanselmann
    """Initializes this class.
705 415a7304 Michael Hanselmann

706 415a7304 Michael Hanselmann
    """
707 b2acdbdc Michael Hanselmann
    if context:
708 b2acdbdc Michael Hanselmann
      lock_monitor_cb = context.glm.AddToLockMonitor
709 b2acdbdc Michael Hanselmann
    else:
710 b2acdbdc Michael Hanselmann
      lock_monitor_cb = None
711 b2acdbdc Michael Hanselmann
712 415a7304 Michael Hanselmann
    if address_list is None:
713 415a7304 Michael Hanselmann
      resolver = _SsconfResolver
714 415a7304 Michael Hanselmann
    else:
715 415a7304 Michael Hanselmann
      # Caller provided an address list
716 415a7304 Michael Hanselmann
      resolver = _StaticResolver(address_list)
717 415a7304 Michael Hanselmann
718 b2acdbdc Michael Hanselmann
    _RpcClientBase.__init__(self, resolver, _ENCODERS.get,
719 b2acdbdc Michael Hanselmann
                            lock_monitor_cb=lock_monitor_cb)
720 cd40dc53 Michael Hanselmann
    _generated_rpc.RpcClientConfig.__init__(self)