Statistics
| Branch: | Tag: | Revision:

root / lib / rpc.py @ b43dcc5a

History | View | Annotate | Download (44.3 kB)

1
#
2
#
3

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

    
21

    
22
"""Inter-node RPC library.
23

24
"""
25

    
26
# pylint: disable-msg=C0103,R0201,R0904
27
# C0103: Invalid name, since call_ are not valid
28
# R0201: Method could be a function, we keep all rpcs instance methods
29
# as not to change them back and forth between static/instance methods
30
# if they need to start using instance attributes
31
# R0904: Too many public methods
32

    
33
import os
34
import logging
35
import zlib
36
import base64
37
import pycurl
38
import threading
39

    
40
from ganeti import utils
41
from ganeti import objects
42
from ganeti import http
43
from ganeti import serializer
44
from ganeti import constants
45
from ganeti import errors
46
from ganeti import netutils
47
from ganeti import ssconf
48

    
49
# pylint has a bug here, doesn't see this import
50
import ganeti.http.client  # pylint: disable-msg=W0611
51

    
52

    
53
# Timeout for connecting to nodes (seconds)
54
_RPC_CONNECT_TIMEOUT = 5
55

    
56
_RPC_CLIENT_HEADERS = [
57
  "Content-type: %s" % http.HTTP_APP_JSON,
58
  ]
59

    
60
# Various time constants for the timeout table
61
_TMO_URGENT = 60 # one minute
62
_TMO_FAST = 5 * 60 # five minutes
63
_TMO_NORMAL = 15 * 60 # 15 minutes
64
_TMO_SLOW = 3600 # one hour
65
_TMO_4HRS = 4 * 3600
66
_TMO_1DAY = 86400
67

    
68
# Timeout table that will be built later by decorators
69
# Guidelines for choosing timeouts:
70
# - call used during watcher: timeout -> 1min, _TMO_URGENT
71
# - trivial (but be sure it is trivial) (e.g. reading a file): 5min, _TMO_FAST
72
# - other calls: 15 min, _TMO_NORMAL
73
# - special calls (instance add, etc.): either _TMO_SLOW (1h) or huge timeouts
74

    
75
_TIMEOUTS = {
76
}
77

    
78

    
79
def Init():
80
  """Initializes the module-global HTTP client manager.
81

82
  Must be called before using any RPC function and while exactly one thread is
83
  running.
84

85
  """
86
  # curl_global_init(3) and curl_global_cleanup(3) must be called with only
87
  # one thread running. This check is just a safety measure -- it doesn't
88
  # cover all cases.
89
  assert threading.activeCount() == 1, \
90
         "Found more than one active thread when initializing pycURL"
91

    
92
  logging.info("Using PycURL %s", pycurl.version)
93

    
94
  pycurl.global_init(pycurl.GLOBAL_ALL)
95

    
96

    
97
def Shutdown():
98
  """Stops the module-global HTTP client manager.
99

100
  Must be called before quitting the program and while exactly one thread is
101
  running.
102

103
  """
104
  pycurl.global_cleanup()
105

    
106

    
107
def _ConfigRpcCurl(curl):
108
  noded_cert = str(constants.NODED_CERT_FILE)
109

    
110
  curl.setopt(pycurl.FOLLOWLOCATION, False)
111
  curl.setopt(pycurl.CAINFO, noded_cert)
112
  curl.setopt(pycurl.SSL_VERIFYHOST, 0)
113
  curl.setopt(pycurl.SSL_VERIFYPEER, True)
114
  curl.setopt(pycurl.SSLCERTTYPE, "PEM")
115
  curl.setopt(pycurl.SSLCERT, noded_cert)
116
  curl.setopt(pycurl.SSLKEYTYPE, "PEM")
117
  curl.setopt(pycurl.SSLKEY, noded_cert)
118
  curl.setopt(pycurl.CONNECTTIMEOUT, _RPC_CONNECT_TIMEOUT)
119

    
120

    
121
class _RpcThreadLocal(threading.local):
122
  def GetHttpClientPool(self):
123
    """Returns a per-thread HTTP client pool.
124

125
    @rtype: L{http.client.HttpClientPool}
126

127
    """
128
    try:
129
      pool = self.hcp
130
    except AttributeError:
131
      pool = http.client.HttpClientPool(_ConfigRpcCurl)
132
      self.hcp = pool
133

    
134
    return pool
135

    
136

    
137
_thread_local = _RpcThreadLocal()
138

    
139

    
140
def _RpcTimeout(secs):
141
  """Timeout decorator.
142

143
  When applied to a rpc call_* function, it updates the global timeout
144
  table with the given function/timeout.
145

146
  """
147
  def decorator(f):
148
    name = f.__name__
149
    assert name.startswith("call_")
150
    _TIMEOUTS[name[len("call_"):]] = secs
151
    return f
152
  return decorator
153

    
154

    
155
def RunWithRPC(fn):
156
  """RPC-wrapper decorator.
157

158
  When applied to a function, it runs it with the RPC system
159
  initialized, and it shutsdown the system afterwards. This means the
160
  function must be called without RPC being initialized.
161

162
  """
163
  def wrapper(*args, **kwargs):
164
    Init()
165
    try:
166
      return fn(*args, **kwargs)
167
    finally:
168
      Shutdown()
169
  return wrapper
170

    
171

    
172
class RpcResult(object):
173
  """RPC Result class.
174

175
  This class holds an RPC result. It is needed since in multi-node
176
  calls we can't raise an exception just because one one out of many
177
  failed, and therefore we use this class to encapsulate the result.
178

179
  @ivar data: the data payload, for successful results, or None
180
  @ivar call: the name of the RPC call
181
  @ivar node: the name of the node to which we made the call
182
  @ivar offline: whether the operation failed because the node was
183
      offline, as opposed to actual failure; offline=True will always
184
      imply failed=True, in order to allow simpler checking if
185
      the user doesn't care about the exact failure mode
186
  @ivar fail_msg: the error message if the call failed
187

188
  """
189
  def __init__(self, data=None, failed=False, offline=False,
190
               call=None, node=None):
191
    self.offline = offline
192
    self.call = call
193
    self.node = node
194

    
195
    if offline:
196
      self.fail_msg = "Node is marked offline"
197
      self.data = self.payload = None
198
    elif failed:
199
      self.fail_msg = self._EnsureErr(data)
200
      self.data = self.payload = None
201
    else:
202
      self.data = data
203
      if not isinstance(self.data, (tuple, list)):
204
        self.fail_msg = ("RPC layer error: invalid result type (%s)" %
205
                         type(self.data))
206
        self.payload = None
207
      elif len(data) != 2:
208
        self.fail_msg = ("RPC layer error: invalid result length (%d), "
209
                         "expected 2" % len(self.data))
210
        self.payload = None
211
      elif not self.data[0]:
212
        self.fail_msg = self._EnsureErr(self.data[1])
213
        self.payload = None
214
      else:
215
        # finally success
216
        self.fail_msg = None
217
        self.payload = data[1]
218

    
219
    assert hasattr(self, "call")
220
    assert hasattr(self, "data")
221
    assert hasattr(self, "fail_msg")
222
    assert hasattr(self, "node")
223
    assert hasattr(self, "offline")
224
    assert hasattr(self, "payload")
225

    
226
  @staticmethod
227
  def _EnsureErr(val):
228
    """Helper to ensure we return a 'True' value for error."""
229
    if val:
230
      return val
231
    else:
232
      return "No error information"
233

    
234
  def Raise(self, msg, prereq=False, ecode=None):
235
    """If the result has failed, raise an OpExecError.
236

237
    This is used so that LU code doesn't have to check for each
238
    result, but instead can call this function.
239

240
    """
241
    if not self.fail_msg:
242
      return
243

    
244
    if not msg: # one could pass None for default message
245
      msg = ("Call '%s' to node '%s' has failed: %s" %
246
             (self.call, self.node, self.fail_msg))
247
    else:
248
      msg = "%s: %s" % (msg, self.fail_msg)
249
    if prereq:
250
      ec = errors.OpPrereqError
251
    else:
252
      ec = errors.OpExecError
253
    if ecode is not None:
254
      args = (msg, ecode)
255
    else:
256
      args = (msg, )
257
    raise ec(*args) # pylint: disable-msg=W0142
258

    
259

    
260
def _AddressLookup(node_list,
261
                   ssc=ssconf.SimpleStore,
262
                   nslookup_fn=netutils.Hostname.GetIP):
263
  """Return addresses for given node names.
264

265
  @type node_list: list
266
  @param node_list: List of node names
267
  @type ssc: class
268
  @param ssc: SimpleStore class that is used to obtain node->ip mappings
269
  @type lookup_fn: callable
270
  @param lookup_fn: function use to do NS lookup
271
  @rtype: list of addresses and/or None's
272
  @returns: List of corresponding addresses, if found
273

274
  """
275
  ss = ssc()
276
  iplist = ss.GetNodePrimaryIPList()
277
  family = ss.GetPrimaryIPFamily()
278
  addresses = []
279
  ipmap = dict(entry.split() for entry in iplist)
280
  for node in node_list:
281
    address = ipmap.get(node)
282
    if address is None:
283
      address = nslookup_fn(node, family=family)
284
    addresses.append(address)
285

    
286
  return addresses
287

    
288

    
289
class Client:
290
  """RPC Client class.
291

292
  This class, given a (remote) method name, a list of parameters and a
293
  list of nodes, will contact (in parallel) all nodes, and return a
294
  dict of results (key: node name, value: result).
295

296
  One current bug is that generic failure is still signaled by
297
  'False' result, which is not good. This overloading of values can
298
  cause bugs.
299

300
  """
301
  def __init__(self, procedure, body, port, address_lookup_fn=_AddressLookup):
302
    assert procedure in _TIMEOUTS, ("New RPC call not declared in the"
303
                                    " timeouts table")
304
    self.procedure = procedure
305
    self.body = body
306
    self.port = port
307
    self._request = {}
308
    self._address_lookup_fn = address_lookup_fn
309

    
310
  def ConnectList(self, node_list, address_list=None, read_timeout=None):
311
    """Add a list of nodes to the target nodes.
312

313
    @type node_list: list
314
    @param node_list: the list of node names to connect
315
    @type address_list: list or None
316
    @keyword address_list: either None or a list with node addresses,
317
        which must have the same length as the node list
318
    @type read_timeout: int
319
    @param read_timeout: overwrites default timeout for operation
320

321
    """
322
    if address_list is None:
323
      # Always use IP address instead of node name
324
      address_list = self._address_lookup_fn(node_list)
325

    
326
    assert len(node_list) == len(address_list), \
327
           "Name and address lists must have the same length"
328

    
329
    for node, address in zip(node_list, address_list):
330
      self.ConnectNode(node, address, read_timeout=read_timeout)
331

    
332
  def ConnectNode(self, name, address=None, read_timeout=None):
333
    """Add a node to the target list.
334

335
    @type name: str
336
    @param name: the node name
337
    @type address: str
338
    @param address: the node address, if known
339
    @type read_timeout: int
340
    @param read_timeout: overwrites default timeout for operation
341

342
    """
343
    if address is None:
344
      # Always use IP address instead of node name
345
      address = self._address_lookup_fn([name])[0]
346

    
347
    assert(address is not None)
348

    
349
    if read_timeout is None:
350
      read_timeout = _TIMEOUTS[self.procedure]
351

    
352
    self._request[name] = \
353
      http.client.HttpClientRequest(str(address), self.port,
354
                                    http.HTTP_PUT, str("/%s" % self.procedure),
355
                                    headers=_RPC_CLIENT_HEADERS,
356
                                    post_data=str(self.body),
357
                                    read_timeout=read_timeout)
358

    
359
  def GetResults(self, http_pool=None):
360
    """Call nodes and return results.
361

362
    @rtype: list
363
    @return: List of RPC results
364

365
    """
366
    if not http_pool:
367
      http_pool = _thread_local.GetHttpClientPool()
368

    
369
    http_pool.ProcessRequests(self._request.values())
370

    
371
    results = {}
372

    
373
    for name, req in self._request.iteritems():
374
      if req.success and req.resp_status_code == http.HTTP_OK:
375
        results[name] = RpcResult(data=serializer.LoadJson(req.resp_body),
376
                                  node=name, call=self.procedure)
377
        continue
378

    
379
      # TODO: Better error reporting
380
      if req.error:
381
        msg = req.error
382
      else:
383
        msg = req.resp_body
384

    
385
      logging.error("RPC error in %s from node %s: %s",
386
                    self.procedure, name, msg)
387
      results[name] = RpcResult(data=msg, failed=True, node=name,
388
                                call=self.procedure)
389

    
390
    return results
391

    
392

    
393
def _EncodeImportExportIO(ieio, ieioargs):
394
  """Encodes import/export I/O information.
395

396
  """
397
  if ieio == constants.IEIO_RAW_DISK:
398
    assert len(ieioargs) == 1
399
    return (ieioargs[0].ToDict(), )
400

    
401
  if ieio == constants.IEIO_SCRIPT:
402
    assert len(ieioargs) == 2
403
    return (ieioargs[0].ToDict(), ieioargs[1])
404

    
405
  return ieioargs
406

    
407

    
408
class RpcRunner(object):
409
  """RPC runner class"""
410

    
411
  def __init__(self, cfg):
412
    """Initialized the rpc runner.
413

414
    @type cfg:  C{config.ConfigWriter}
415
    @param cfg: the configuration object that will be used to get data
416
                about the cluster
417

418
    """
419
    self._cfg = cfg
420
    self.port = netutils.GetDaemonPort(constants.NODED)
421

    
422
  def _InstDict(self, instance, hvp=None, bep=None, osp=None):
423
    """Convert the given instance to a dict.
424

425
    This is done via the instance's ToDict() method and additionally
426
    we fill the hvparams with the cluster defaults.
427

428
    @type instance: L{objects.Instance}
429
    @param instance: an Instance object
430
    @type hvp: dict or None
431
    @param hvp: a dictionary with overridden hypervisor parameters
432
    @type bep: dict or None
433
    @param bep: a dictionary with overridden backend parameters
434
    @type osp: dict or None
435
    @param osp: a dictionary with overriden os parameters
436
    @rtype: dict
437
    @return: the instance dict, with the hvparams filled with the
438
        cluster defaults
439

440
    """
441
    idict = instance.ToDict()
442
    cluster = self._cfg.GetClusterInfo()
443
    idict["hvparams"] = cluster.FillHV(instance)
444
    if hvp is not None:
445
      idict["hvparams"].update(hvp)
446
    idict["beparams"] = cluster.FillBE(instance)
447
    if bep is not None:
448
      idict["beparams"].update(bep)
449
    idict["osparams"] = cluster.SimpleFillOS(instance.os, instance.osparams)
450
    if osp is not None:
451
      idict["osparams"].update(osp)
452
    for nic in idict["nics"]:
453
      nic['nicparams'] = objects.FillDict(
454
        cluster.nicparams[constants.PP_DEFAULT],
455
        nic['nicparams'])
456
    return idict
457

    
458
  def _ConnectList(self, client, node_list, call, read_timeout=None):
459
    """Helper for computing node addresses.
460

461
    @type client: L{ganeti.rpc.Client}
462
    @param client: a C{Client} instance
463
    @type node_list: list
464
    @param node_list: the node list we should connect
465
    @type call: string
466
    @param call: the name of the remote procedure call, for filling in
467
        correctly any eventual offline nodes' results
468
    @type read_timeout: int
469
    @param read_timeout: overwrites the default read timeout for the
470
        given operation
471

472
    """
473
    all_nodes = self._cfg.GetAllNodesInfo()
474
    name_list = []
475
    addr_list = []
476
    skip_dict = {}
477
    for node in node_list:
478
      if node in all_nodes:
479
        if all_nodes[node].offline:
480
          skip_dict[node] = RpcResult(node=node, offline=True, call=call)
481
          continue
482
        val = all_nodes[node].primary_ip
483
      else:
484
        val = None
485
      addr_list.append(val)
486
      name_list.append(node)
487
    if name_list:
488
      client.ConnectList(name_list, address_list=addr_list,
489
                         read_timeout=read_timeout)
490
    return skip_dict
491

    
492
  def _ConnectNode(self, client, node, call, read_timeout=None):
493
    """Helper for computing one node's address.
494

495
    @type client: L{ganeti.rpc.Client}
496
    @param client: a C{Client} instance
497
    @type node: str
498
    @param node: the node we should connect
499
    @type call: string
500
    @param call: the name of the remote procedure call, for filling in
501
        correctly any eventual offline nodes' results
502
    @type read_timeout: int
503
    @param read_timeout: overwrites the default read timeout for the
504
        given operation
505

506
    """
507
    node_info = self._cfg.GetNodeInfo(node)
508
    if node_info is not None:
509
      if node_info.offline:
510
        return RpcResult(node=node, offline=True, call=call)
511
      addr = node_info.primary_ip
512
    else:
513
      addr = None
514
    client.ConnectNode(node, address=addr, read_timeout=read_timeout)
515

    
516
  def _MultiNodeCall(self, node_list, procedure, args, read_timeout=None):
517
    """Helper for making a multi-node call
518

519
    """
520
    body = serializer.DumpJson(args, indent=False)
521
    c = Client(procedure, body, self.port)
522
    skip_dict = self._ConnectList(c, node_list, procedure,
523
                                  read_timeout=read_timeout)
524
    skip_dict.update(c.GetResults())
525
    return skip_dict
526

    
527
  @classmethod
528
  def _StaticMultiNodeCall(cls, node_list, procedure, args,
529
                           address_list=None, read_timeout=None):
530
    """Helper for making a multi-node static call
531

532
    """
533
    body = serializer.DumpJson(args, indent=False)
534
    c = Client(procedure, body, netutils.GetDaemonPort(constants.NODED))
535
    c.ConnectList(node_list, address_list=address_list,
536
                  read_timeout=read_timeout)
537
    return c.GetResults()
538

    
539
  def _SingleNodeCall(self, node, procedure, args, read_timeout=None):
540
    """Helper for making a single-node call
541

542
    """
543
    body = serializer.DumpJson(args, indent=False)
544
    c = Client(procedure, body, self.port)
545
    result = self._ConnectNode(c, node, procedure, read_timeout=read_timeout)
546
    if result is None:
547
      # we did connect, node is not offline
548
      result = c.GetResults()[node]
549
    return result
550

    
551
  @classmethod
552
  def _StaticSingleNodeCall(cls, node, procedure, args, read_timeout=None):
553
    """Helper for making a single-node static call
554

555
    """
556
    body = serializer.DumpJson(args, indent=False)
557
    c = Client(procedure, body, netutils.GetDaemonPort(constants.NODED))
558
    c.ConnectNode(node, read_timeout=read_timeout)
559
    return c.GetResults()[node]
560

    
561
  @staticmethod
562
  def _Compress(data):
563
    """Compresses a string for transport over RPC.
564

565
    Small amounts of data are not compressed.
566

567
    @type data: str
568
    @param data: Data
569
    @rtype: tuple
570
    @return: Encoded data to send
571

572
    """
573
    # Small amounts of data are not compressed
574
    if len(data) < 512:
575
      return (constants.RPC_ENCODING_NONE, data)
576

    
577
    # Compress with zlib and encode in base64
578
    return (constants.RPC_ENCODING_ZLIB_BASE64,
579
            base64.b64encode(zlib.compress(data, 3)))
580

    
581
  #
582
  # Begin RPC calls
583
  #
584

    
585
  @_RpcTimeout(_TMO_URGENT)
586
  def call_lv_list(self, node_list, vg_name):
587
    """Gets the logical volumes present in a given volume group.
588

589
    This is a multi-node call.
590

591
    """
592
    return self._MultiNodeCall(node_list, "lv_list", [vg_name])
593

    
594
  @_RpcTimeout(_TMO_URGENT)
595
  def call_vg_list(self, node_list):
596
    """Gets the volume group list.
597

598
    This is a multi-node call.
599

600
    """
601
    return self._MultiNodeCall(node_list, "vg_list", [])
602

    
603
  @_RpcTimeout(_TMO_NORMAL)
604
  def call_storage_list(self, node_list, su_name, su_args, name, fields):
605
    """Get list of storage units.
606

607
    This is a multi-node call.
608

609
    """
610
    return self._MultiNodeCall(node_list, "storage_list",
611
                               [su_name, su_args, name, fields])
612

    
613
  @_RpcTimeout(_TMO_NORMAL)
614
  def call_storage_modify(self, node, su_name, su_args, name, changes):
615
    """Modify a storage unit.
616

617
    This is a single-node call.
618

619
    """
620
    return self._SingleNodeCall(node, "storage_modify",
621
                                [su_name, su_args, name, changes])
622

    
623
  @_RpcTimeout(_TMO_NORMAL)
624
  def call_storage_execute(self, node, su_name, su_args, name, op):
625
    """Executes an operation on a storage unit.
626

627
    This is a single-node call.
628

629
    """
630
    return self._SingleNodeCall(node, "storage_execute",
631
                                [su_name, su_args, name, op])
632

    
633
  @_RpcTimeout(_TMO_URGENT)
634
  def call_bridges_exist(self, node, bridges_list):
635
    """Checks if a node has all the bridges given.
636

637
    This method checks if all bridges given in the bridges_list are
638
    present on the remote node, so that an instance that uses interfaces
639
    on those bridges can be started.
640

641
    This is a single-node call.
642

643
    """
644
    return self._SingleNodeCall(node, "bridges_exist", [bridges_list])
645

    
646
  @_RpcTimeout(_TMO_NORMAL)
647
  def call_instance_start(self, node, instance, hvp, bep):
648
    """Starts an instance.
649

650
    This is a single-node call.
651

652
    """
653
    idict = self._InstDict(instance, hvp=hvp, bep=bep)
654
    return self._SingleNodeCall(node, "instance_start", [idict])
655

    
656
  @_RpcTimeout(_TMO_NORMAL)
657
  def call_instance_shutdown(self, node, instance, timeout):
658
    """Stops an instance.
659

660
    This is a single-node call.
661

662
    """
663
    return self._SingleNodeCall(node, "instance_shutdown",
664
                                [self._InstDict(instance), timeout])
665

    
666
  @_RpcTimeout(_TMO_NORMAL)
667
  def call_migration_info(self, node, instance):
668
    """Gather the information necessary to prepare an instance migration.
669

670
    This is a single-node call.
671

672
    @type node: string
673
    @param node: the node on which the instance is currently running
674
    @type instance: C{objects.Instance}
675
    @param instance: the instance definition
676

677
    """
678
    return self._SingleNodeCall(node, "migration_info",
679
                                [self._InstDict(instance)])
680

    
681
  @_RpcTimeout(_TMO_NORMAL)
682
  def call_accept_instance(self, node, instance, info, target):
683
    """Prepare a node to accept an instance.
684

685
    This is a single-node call.
686

687
    @type node: string
688
    @param node: the target node for the migration
689
    @type instance: C{objects.Instance}
690
    @param instance: the instance definition
691
    @type info: opaque/hypervisor specific (string/data)
692
    @param info: result for the call_migration_info call
693
    @type target: string
694
    @param target: target hostname (usually ip address) (on the node itself)
695

696
    """
697
    return self._SingleNodeCall(node, "accept_instance",
698
                                [self._InstDict(instance), info, target])
699

    
700
  @_RpcTimeout(_TMO_NORMAL)
701
  def call_finalize_migration(self, node, instance, info, success):
702
    """Finalize any target-node migration specific operation.
703

704
    This is called both in case of a successful migration and in case of error
705
    (in which case it should abort the migration).
706

707
    This is a single-node call.
708

709
    @type node: string
710
    @param node: the target node for the migration
711
    @type instance: C{objects.Instance}
712
    @param instance: the instance definition
713
    @type info: opaque/hypervisor specific (string/data)
714
    @param info: result for the call_migration_info call
715
    @type success: boolean
716
    @param success: whether the migration was a success or a failure
717

718
    """
719
    return self._SingleNodeCall(node, "finalize_migration",
720
                                [self._InstDict(instance), info, success])
721

    
722
  @_RpcTimeout(_TMO_SLOW)
723
  def call_instance_migrate(self, node, instance, target, live):
724
    """Migrate an instance.
725

726
    This is a single-node call.
727

728
    @type node: string
729
    @param node: the node on which the instance is currently running
730
    @type instance: C{objects.Instance}
731
    @param instance: the instance definition
732
    @type target: string
733
    @param target: the target node name
734
    @type live: boolean
735
    @param live: whether the migration should be done live or not (the
736
        interpretation of this parameter is left to the hypervisor)
737

738
    """
739
    return self._SingleNodeCall(node, "instance_migrate",
740
                                [self._InstDict(instance), target, live])
741

    
742
  @_RpcTimeout(_TMO_NORMAL)
743
  def call_instance_reboot(self, node, inst, reboot_type, shutdown_timeout):
744
    """Reboots an instance.
745

746
    This is a single-node call.
747

748
    """
749
    return self._SingleNodeCall(node, "instance_reboot",
750
                                [self._InstDict(inst), reboot_type,
751
                                 shutdown_timeout])
752

    
753
  @_RpcTimeout(_TMO_1DAY)
754
  def call_instance_os_add(self, node, inst, reinstall, debug):
755
    """Installs an OS on the given instance.
756

757
    This is a single-node call.
758

759
    """
760
    return self._SingleNodeCall(node, "instance_os_add",
761
                                [self._InstDict(inst), reinstall, debug])
762

    
763
  @_RpcTimeout(_TMO_SLOW)
764
  def call_instance_run_rename(self, node, inst, old_name, debug):
765
    """Run the OS rename script for an instance.
766

767
    This is a single-node call.
768

769
    """
770
    return self._SingleNodeCall(node, "instance_run_rename",
771
                                [self._InstDict(inst), old_name, debug])
772

    
773
  @_RpcTimeout(_TMO_URGENT)
774
  def call_instance_info(self, node, instance, hname):
775
    """Returns information about a single instance.
776

777
    This is a single-node call.
778

779
    @type node: list
780
    @param node: the list of nodes to query
781
    @type instance: string
782
    @param instance: the instance name
783
    @type hname: string
784
    @param hname: the hypervisor type of the instance
785

786
    """
787
    return self._SingleNodeCall(node, "instance_info", [instance, hname])
788

    
789
  @_RpcTimeout(_TMO_NORMAL)
790
  def call_instance_migratable(self, node, instance):
791
    """Checks whether the given instance can be migrated.
792

793
    This is a single-node call.
794

795
    @param node: the node to query
796
    @type instance: L{objects.Instance}
797
    @param instance: the instance to check
798

799

800
    """
801
    return self._SingleNodeCall(node, "instance_migratable",
802
                                [self._InstDict(instance)])
803

    
804
  @_RpcTimeout(_TMO_URGENT)
805
  def call_all_instances_info(self, node_list, hypervisor_list):
806
    """Returns information about all instances on the given nodes.
807

808
    This is a multi-node call.
809

810
    @type node_list: list
811
    @param node_list: the list of nodes to query
812
    @type hypervisor_list: list
813
    @param hypervisor_list: the hypervisors to query for instances
814

815
    """
816
    return self._MultiNodeCall(node_list, "all_instances_info",
817
                               [hypervisor_list])
818

    
819
  @_RpcTimeout(_TMO_URGENT)
820
  def call_instance_list(self, node_list, hypervisor_list):
821
    """Returns the list of running instances on a given node.
822

823
    This is a multi-node call.
824

825
    @type node_list: list
826
    @param node_list: the list of nodes to query
827
    @type hypervisor_list: list
828
    @param hypervisor_list: the hypervisors to query for instances
829

830
    """
831
    return self._MultiNodeCall(node_list, "instance_list", [hypervisor_list])
832

    
833
  @_RpcTimeout(_TMO_FAST)
834
  def call_node_tcp_ping(self, node, source, target, port, timeout,
835
                         live_port_needed):
836
    """Do a TcpPing on the remote node
837

838
    This is a single-node call.
839

840
    """
841
    return self._SingleNodeCall(node, "node_tcp_ping",
842
                                [source, target, port, timeout,
843
                                 live_port_needed])
844

    
845
  @_RpcTimeout(_TMO_FAST)
846
  def call_node_has_ip_address(self, node, address):
847
    """Checks if a node has the given IP address.
848

849
    This is a single-node call.
850

851
    """
852
    return self._SingleNodeCall(node, "node_has_ip_address", [address])
853

    
854
  @_RpcTimeout(_TMO_URGENT)
855
  def call_node_info(self, node_list, vg_name, hypervisor_type):
856
    """Return node information.
857

858
    This will return memory information and volume group size and free
859
    space.
860

861
    This is a multi-node call.
862

863
    @type node_list: list
864
    @param node_list: the list of nodes to query
865
    @type vg_name: C{string}
866
    @param vg_name: the name of the volume group to ask for disk space
867
        information
868
    @type hypervisor_type: C{str}
869
    @param hypervisor_type: the name of the hypervisor to ask for
870
        memory information
871

872
    """
873
    return self._MultiNodeCall(node_list, "node_info",
874
                               [vg_name, hypervisor_type])
875

    
876
  @_RpcTimeout(_TMO_NORMAL)
877
  def call_node_add(self, node, dsa, dsapub, rsa, rsapub, ssh, sshpub):
878
    """Add a node to the cluster.
879

880
    This is a single-node call.
881

882
    """
883
    return self._SingleNodeCall(node, "node_add",
884
                                [dsa, dsapub, rsa, rsapub, ssh, sshpub])
885

    
886
  @_RpcTimeout(_TMO_NORMAL)
887
  def call_node_verify(self, node_list, checkdict, cluster_name):
888
    """Request verification of given parameters.
889

890
    This is a multi-node call.
891

892
    """
893
    return self._MultiNodeCall(node_list, "node_verify",
894
                               [checkdict, cluster_name])
895

    
896
  @classmethod
897
  @_RpcTimeout(_TMO_FAST)
898
  def call_node_start_master(cls, node, start_daemons, no_voting):
899
    """Tells a node to activate itself as a master.
900

901
    This is a single-node call.
902

903
    """
904
    return cls._StaticSingleNodeCall(node, "node_start_master",
905
                                     [start_daemons, no_voting])
906

    
907
  @classmethod
908
  @_RpcTimeout(_TMO_FAST)
909
  def call_node_stop_master(cls, node, stop_daemons):
910
    """Tells a node to demote itself from master status.
911

912
    This is a single-node call.
913

914
    """
915
    return cls._StaticSingleNodeCall(node, "node_stop_master", [stop_daemons])
916

    
917
  @classmethod
918
  @_RpcTimeout(_TMO_URGENT)
919
  def call_master_info(cls, node_list):
920
    """Query master info.
921

922
    This is a multi-node call.
923

924
    """
925
    # TODO: should this method query down nodes?
926
    return cls._StaticMultiNodeCall(node_list, "master_info", [])
927

    
928
  @classmethod
929
  @_RpcTimeout(_TMO_URGENT)
930
  def call_version(cls, node_list):
931
    """Query node version.
932

933
    This is a multi-node call.
934

935
    """
936
    return cls._StaticMultiNodeCall(node_list, "version", [])
937

    
938
  @_RpcTimeout(_TMO_NORMAL)
939
  def call_blockdev_create(self, node, bdev, size, owner, on_primary, info):
940
    """Request creation of a given block device.
941

942
    This is a single-node call.
943

944
    """
945
    return self._SingleNodeCall(node, "blockdev_create",
946
                                [bdev.ToDict(), size, owner, on_primary, info])
947

    
948
  @_RpcTimeout(_TMO_NORMAL)
949
  def call_blockdev_remove(self, node, bdev):
950
    """Request removal of a given block device.
951

952
    This is a single-node call.
953

954
    """
955
    return self._SingleNodeCall(node, "blockdev_remove", [bdev.ToDict()])
956

    
957
  @_RpcTimeout(_TMO_NORMAL)
958
  def call_blockdev_rename(self, node, devlist):
959
    """Request rename of the given block devices.
960

961
    This is a single-node call.
962

963
    """
964
    return self._SingleNodeCall(node, "blockdev_rename",
965
                                [(d.ToDict(), uid) for d, uid in devlist])
966

    
967
  @_RpcTimeout(_TMO_NORMAL)
968
  def call_blockdev_assemble(self, node, disk, owner, on_primary):
969
    """Request assembling of a given block device.
970

971
    This is a single-node call.
972

973
    """
974
    return self._SingleNodeCall(node, "blockdev_assemble",
975
                                [disk.ToDict(), owner, on_primary])
976

    
977
  @_RpcTimeout(_TMO_NORMAL)
978
  def call_blockdev_shutdown(self, node, disk):
979
    """Request shutdown of a given block device.
980

981
    This is a single-node call.
982

983
    """
984
    return self._SingleNodeCall(node, "blockdev_shutdown", [disk.ToDict()])
985

    
986
  @_RpcTimeout(_TMO_NORMAL)
987
  def call_blockdev_addchildren(self, node, bdev, ndevs):
988
    """Request adding a list of children to a (mirroring) device.
989

990
    This is a single-node call.
991

992
    """
993
    return self._SingleNodeCall(node, "blockdev_addchildren",
994
                                [bdev.ToDict(),
995
                                 [disk.ToDict() for disk in ndevs]])
996

    
997
  @_RpcTimeout(_TMO_NORMAL)
998
  def call_blockdev_removechildren(self, node, bdev, ndevs):
999
    """Request removing a list of children from a (mirroring) device.
1000

1001
    This is a single-node call.
1002

1003
    """
1004
    return self._SingleNodeCall(node, "blockdev_removechildren",
1005
                                [bdev.ToDict(),
1006
                                 [disk.ToDict() for disk in ndevs]])
1007

    
1008
  @_RpcTimeout(_TMO_NORMAL)
1009
  def call_blockdev_getmirrorstatus(self, node, disks):
1010
    """Request status of a (mirroring) device.
1011

1012
    This is a single-node call.
1013

1014
    """
1015
    result = self._SingleNodeCall(node, "blockdev_getmirrorstatus",
1016
                                  [dsk.ToDict() for dsk in disks])
1017
    if not result.fail_msg:
1018
      result.payload = [objects.BlockDevStatus.FromDict(i)
1019
                        for i in result.payload]
1020
    return result
1021

    
1022
  @_RpcTimeout(_TMO_NORMAL)
1023
  def call_blockdev_find(self, node, disk):
1024
    """Request identification of a given block device.
1025

1026
    This is a single-node call.
1027

1028
    """
1029
    result = self._SingleNodeCall(node, "blockdev_find", [disk.ToDict()])
1030
    if not result.fail_msg and result.payload is not None:
1031
      result.payload = objects.BlockDevStatus.FromDict(result.payload)
1032
    return result
1033

    
1034
  @_RpcTimeout(_TMO_NORMAL)
1035
  def call_blockdev_close(self, node, instance_name, disks):
1036
    """Closes the given block devices.
1037

1038
    This is a single-node call.
1039

1040
    """
1041
    params = [instance_name, [cf.ToDict() for cf in disks]]
1042
    return self._SingleNodeCall(node, "blockdev_close", params)
1043

    
1044
  @_RpcTimeout(_TMO_NORMAL)
1045
  def call_blockdev_getsizes(self, node, disks):
1046
    """Returns the size of the given disks.
1047

1048
    This is a single-node call.
1049

1050
    """
1051
    params = [[cf.ToDict() for cf in disks]]
1052
    return self._SingleNodeCall(node, "blockdev_getsize", params)
1053

    
1054
  @_RpcTimeout(_TMO_NORMAL)
1055
  def call_drbd_disconnect_net(self, node_list, nodes_ip, disks):
1056
    """Disconnects the network of the given drbd devices.
1057

1058
    This is a multi-node call.
1059

1060
    """
1061
    return self._MultiNodeCall(node_list, "drbd_disconnect_net",
1062
                               [nodes_ip, [cf.ToDict() for cf in disks]])
1063

    
1064
  @_RpcTimeout(_TMO_NORMAL)
1065
  def call_drbd_attach_net(self, node_list, nodes_ip,
1066
                           disks, instance_name, multimaster):
1067
    """Disconnects the given drbd devices.
1068

1069
    This is a multi-node call.
1070

1071
    """
1072
    return self._MultiNodeCall(node_list, "drbd_attach_net",
1073
                               [nodes_ip, [cf.ToDict() for cf in disks],
1074
                                instance_name, multimaster])
1075

    
1076
  @_RpcTimeout(_TMO_SLOW)
1077
  def call_drbd_wait_sync(self, node_list, nodes_ip, disks):
1078
    """Waits for the synchronization of drbd devices is complete.
1079

1080
    This is a multi-node call.
1081

1082
    """
1083
    return self._MultiNodeCall(node_list, "drbd_wait_sync",
1084
                               [nodes_ip, [cf.ToDict() for cf in disks]])
1085

    
1086
  @_RpcTimeout(_TMO_URGENT)
1087
  def call_drbd_helper(self, node_list):
1088
    """Gets drbd helper.
1089

1090
    This is a multi-node call.
1091

1092
    """
1093
    return self._MultiNodeCall(node_list, "drbd_helper", [])
1094

    
1095
  @classmethod
1096
  @_RpcTimeout(_TMO_NORMAL)
1097
  def call_upload_file(cls, node_list, file_name, address_list=None):
1098
    """Upload a file.
1099

1100
    The node will refuse the operation in case the file is not on the
1101
    approved file list.
1102

1103
    This is a multi-node call.
1104

1105
    @type node_list: list
1106
    @param node_list: the list of node names to upload to
1107
    @type file_name: str
1108
    @param file_name: the filename to upload
1109
    @type address_list: list or None
1110
    @keyword address_list: an optional list of node addresses, in order
1111
        to optimize the RPC speed
1112

1113
    """
1114
    file_contents = utils.ReadFile(file_name)
1115
    data = cls._Compress(file_contents)
1116
    st = os.stat(file_name)
1117
    params = [file_name, data, st.st_mode, st.st_uid, st.st_gid,
1118
              st.st_atime, st.st_mtime]
1119
    return cls._StaticMultiNodeCall(node_list, "upload_file", params,
1120
                                    address_list=address_list)
1121

    
1122
  @classmethod
1123
  @_RpcTimeout(_TMO_NORMAL)
1124
  def call_write_ssconf_files(cls, node_list, values):
1125
    """Write ssconf files.
1126

1127
    This is a multi-node call.
1128

1129
    """
1130
    return cls._StaticMultiNodeCall(node_list, "write_ssconf_files", [values])
1131

    
1132
  @_RpcTimeout(_TMO_FAST)
1133
  def call_os_diagnose(self, node_list):
1134
    """Request a diagnose of OS definitions.
1135

1136
    This is a multi-node call.
1137

1138
    """
1139
    return self._MultiNodeCall(node_list, "os_diagnose", [])
1140

    
1141
  @_RpcTimeout(_TMO_FAST)
1142
  def call_os_get(self, node, name):
1143
    """Returns an OS definition.
1144

1145
    This is a single-node call.
1146

1147
    """
1148
    result = self._SingleNodeCall(node, "os_get", [name])
1149
    if not result.fail_msg and isinstance(result.payload, dict):
1150
      result.payload = objects.OS.FromDict(result.payload)
1151
    return result
1152

    
1153
  @_RpcTimeout(_TMO_FAST)
1154
  def call_os_validate(self, required, nodes, name, checks, params):
1155
    """Run a validation routine for a given OS.
1156

1157
    This is a multi-node call.
1158

1159
    """
1160
    return self._MultiNodeCall(nodes, "os_validate",
1161
                               [required, name, checks, params])
1162

    
1163
  @_RpcTimeout(_TMO_NORMAL)
1164
  def call_hooks_runner(self, node_list, hpath, phase, env):
1165
    """Call the hooks runner.
1166

1167
    Args:
1168
      - op: the OpCode instance
1169
      - env: a dictionary with the environment
1170

1171
    This is a multi-node call.
1172

1173
    """
1174
    params = [hpath, phase, env]
1175
    return self._MultiNodeCall(node_list, "hooks_runner", params)
1176

    
1177
  @_RpcTimeout(_TMO_NORMAL)
1178
  def call_iallocator_runner(self, node, name, idata):
1179
    """Call an iallocator on a remote node
1180

1181
    Args:
1182
      - name: the iallocator name
1183
      - input: the json-encoded input string
1184

1185
    This is a single-node call.
1186

1187
    """
1188
    return self._SingleNodeCall(node, "iallocator_runner", [name, idata])
1189

    
1190
  @_RpcTimeout(_TMO_NORMAL)
1191
  def call_blockdev_grow(self, node, cf_bdev, amount):
1192
    """Request a snapshot of the given block device.
1193

1194
    This is a single-node call.
1195

1196
    """
1197
    return self._SingleNodeCall(node, "blockdev_grow",
1198
                                [cf_bdev.ToDict(), amount])
1199

    
1200
  @_RpcTimeout(_TMO_1DAY)
1201
  def call_blockdev_export(self, node, cf_bdev,
1202
                           dest_node, dest_path, cluster_name):
1203
    """Export a given disk to another node.
1204

1205
    This is a single-node call.
1206

1207
    """
1208
    return self._SingleNodeCall(node, "blockdev_export",
1209
                                [cf_bdev.ToDict(), dest_node, dest_path,
1210
                                 cluster_name])
1211

    
1212
  @_RpcTimeout(_TMO_NORMAL)
1213
  def call_blockdev_snapshot(self, node, cf_bdev):
1214
    """Request a snapshot of the given block device.
1215

1216
    This is a single-node call.
1217

1218
    """
1219
    return self._SingleNodeCall(node, "blockdev_snapshot", [cf_bdev.ToDict()])
1220

    
1221
  @_RpcTimeout(_TMO_NORMAL)
1222
  def call_finalize_export(self, node, instance, snap_disks):
1223
    """Request the completion of an export operation.
1224

1225
    This writes the export config file, etc.
1226

1227
    This is a single-node call.
1228

1229
    """
1230
    flat_disks = []
1231
    for disk in snap_disks:
1232
      if isinstance(disk, bool):
1233
        flat_disks.append(disk)
1234
      else:
1235
        flat_disks.append(disk.ToDict())
1236

    
1237
    return self._SingleNodeCall(node, "finalize_export",
1238
                                [self._InstDict(instance), flat_disks])
1239

    
1240
  @_RpcTimeout(_TMO_FAST)
1241
  def call_export_info(self, node, path):
1242
    """Queries the export information in a given path.
1243

1244
    This is a single-node call.
1245

1246
    """
1247
    return self._SingleNodeCall(node, "export_info", [path])
1248

    
1249
  @_RpcTimeout(_TMO_FAST)
1250
  def call_export_list(self, node_list):
1251
    """Gets the stored exports list.
1252

1253
    This is a multi-node call.
1254

1255
    """
1256
    return self._MultiNodeCall(node_list, "export_list", [])
1257

    
1258
  @_RpcTimeout(_TMO_FAST)
1259
  def call_export_remove(self, node, export):
1260
    """Requests removal of a given export.
1261

1262
    This is a single-node call.
1263

1264
    """
1265
    return self._SingleNodeCall(node, "export_remove", [export])
1266

    
1267
  @classmethod
1268
  @_RpcTimeout(_TMO_NORMAL)
1269
  def call_node_leave_cluster(cls, node, modify_ssh_setup):
1270
    """Requests a node to clean the cluster information it has.
1271

1272
    This will remove the configuration information from the ganeti data
1273
    dir.
1274

1275
    This is a single-node call.
1276

1277
    """
1278
    return cls._StaticSingleNodeCall(node, "node_leave_cluster",
1279
                                     [modify_ssh_setup])
1280

    
1281
  @_RpcTimeout(_TMO_FAST)
1282
  def call_node_volumes(self, node_list):
1283
    """Gets all volumes on node(s).
1284

1285
    This is a multi-node call.
1286

1287
    """
1288
    return self._MultiNodeCall(node_list, "node_volumes", [])
1289

    
1290
  @_RpcTimeout(_TMO_FAST)
1291
  def call_node_demote_from_mc(self, node):
1292
    """Demote a node from the master candidate role.
1293

1294
    This is a single-node call.
1295

1296
    """
1297
    return self._SingleNodeCall(node, "node_demote_from_mc", [])
1298

    
1299
  @_RpcTimeout(_TMO_NORMAL)
1300
  def call_node_powercycle(self, node, hypervisor):
1301
    """Tries to powercycle a node.
1302

1303
    This is a single-node call.
1304

1305
    """
1306
    return self._SingleNodeCall(node, "node_powercycle", [hypervisor])
1307

    
1308
  @_RpcTimeout(None)
1309
  def call_test_delay(self, node_list, duration):
1310
    """Sleep for a fixed time on given node(s).
1311

1312
    This is a multi-node call.
1313

1314
    """
1315
    return self._MultiNodeCall(node_list, "test_delay", [duration],
1316
                               read_timeout=int(duration + 5))
1317

    
1318
  @_RpcTimeout(_TMO_FAST)
1319
  def call_file_storage_dir_create(self, node, file_storage_dir):
1320
    """Create the given file storage directory.
1321

1322
    This is a single-node call.
1323

1324
    """
1325
    return self._SingleNodeCall(node, "file_storage_dir_create",
1326
                                [file_storage_dir])
1327

    
1328
  @_RpcTimeout(_TMO_FAST)
1329
  def call_file_storage_dir_remove(self, node, file_storage_dir):
1330
    """Remove the given file storage directory.
1331

1332
    This is a single-node call.
1333

1334
    """
1335
    return self._SingleNodeCall(node, "file_storage_dir_remove",
1336
                                [file_storage_dir])
1337

    
1338
  @_RpcTimeout(_TMO_FAST)
1339
  def call_file_storage_dir_rename(self, node, old_file_storage_dir,
1340
                                   new_file_storage_dir):
1341
    """Rename file storage directory.
1342

1343
    This is a single-node call.
1344

1345
    """
1346
    return self._SingleNodeCall(node, "file_storage_dir_rename",
1347
                                [old_file_storage_dir, new_file_storage_dir])
1348

    
1349
  @classmethod
1350
  @_RpcTimeout(_TMO_FAST)
1351
  def call_jobqueue_update(cls, node_list, address_list, file_name, content):
1352
    """Update job queue.
1353

1354
    This is a multi-node call.
1355

1356
    """
1357
    return cls._StaticMultiNodeCall(node_list, "jobqueue_update",
1358
                                    [file_name, cls._Compress(content)],
1359
                                    address_list=address_list)
1360

    
1361
  @classmethod
1362
  @_RpcTimeout(_TMO_NORMAL)
1363
  def call_jobqueue_purge(cls, node):
1364
    """Purge job queue.
1365

1366
    This is a single-node call.
1367

1368
    """
1369
    return cls._StaticSingleNodeCall(node, "jobqueue_purge", [])
1370

    
1371
  @classmethod
1372
  @_RpcTimeout(_TMO_FAST)
1373
  def call_jobqueue_rename(cls, node_list, address_list, rename):
1374
    """Rename a job queue file.
1375

1376
    This is a multi-node call.
1377

1378
    """
1379
    return cls._StaticMultiNodeCall(node_list, "jobqueue_rename", rename,
1380
                                    address_list=address_list)
1381

    
1382
  @_RpcTimeout(_TMO_NORMAL)
1383
  def call_hypervisor_validate_params(self, node_list, hvname, hvparams):
1384
    """Validate the hypervisor params.
1385

1386
    This is a multi-node call.
1387

1388
    @type node_list: list
1389
    @param node_list: the list of nodes to query
1390
    @type hvname: string
1391
    @param hvname: the hypervisor name
1392
    @type hvparams: dict
1393
    @param hvparams: the hypervisor parameters to be validated
1394

1395
    """
1396
    cluster = self._cfg.GetClusterInfo()
1397
    hv_full = objects.FillDict(cluster.hvparams.get(hvname, {}), hvparams)
1398
    return self._MultiNodeCall(node_list, "hypervisor_validate_params",
1399
                               [hvname, hv_full])
1400

    
1401
  @_RpcTimeout(_TMO_NORMAL)
1402
  def call_x509_cert_create(self, node, validity):
1403
    """Creates a new X509 certificate for SSL/TLS.
1404

1405
    This is a single-node call.
1406

1407
    @type validity: int
1408
    @param validity: Validity in seconds
1409

1410
    """
1411
    return self._SingleNodeCall(node, "x509_cert_create", [validity])
1412

    
1413
  @_RpcTimeout(_TMO_NORMAL)
1414
  def call_x509_cert_remove(self, node, name):
1415
    """Removes a X509 certificate.
1416

1417
    This is a single-node call.
1418

1419
    @type name: string
1420
    @param name: Certificate name
1421

1422
    """
1423
    return self._SingleNodeCall(node, "x509_cert_remove", [name])
1424

    
1425
  @_RpcTimeout(_TMO_NORMAL)
1426
  def call_import_start(self, node, opts, instance, dest, dest_args):
1427
    """Starts a listener for an import.
1428

1429
    This is a single-node call.
1430

1431
    @type node: string
1432
    @param node: Node name
1433
    @type instance: C{objects.Instance}
1434
    @param instance: Instance object
1435

1436
    """
1437
    return self._SingleNodeCall(node, "import_start",
1438
                                [opts.ToDict(),
1439
                                 self._InstDict(instance), dest,
1440
                                 _EncodeImportExportIO(dest, dest_args)])
1441

    
1442
  @_RpcTimeout(_TMO_NORMAL)
1443
  def call_export_start(self, node, opts, host, port,
1444
                        instance, source, source_args):
1445
    """Starts an export daemon.
1446

1447
    This is a single-node call.
1448

1449
    @type node: string
1450
    @param node: Node name
1451
    @type instance: C{objects.Instance}
1452
    @param instance: Instance object
1453

1454
    """
1455
    return self._SingleNodeCall(node, "export_start",
1456
                                [opts.ToDict(), host, port,
1457
                                 self._InstDict(instance), source,
1458
                                 _EncodeImportExportIO(source, source_args)])
1459

    
1460
  @_RpcTimeout(_TMO_FAST)
1461
  def call_impexp_status(self, node, names):
1462
    """Gets the status of an import or export.
1463

1464
    This is a single-node call.
1465

1466
    @type node: string
1467
    @param node: Node name
1468
    @type names: List of strings
1469
    @param names: Import/export names
1470
    @rtype: List of L{objects.ImportExportStatus} instances
1471
    @return: Returns a list of the state of each named import/export or None if
1472
             a status couldn't be retrieved
1473

1474
    """
1475
    result = self._SingleNodeCall(node, "impexp_status", [names])
1476

    
1477
    if not result.fail_msg:
1478
      decoded = []
1479

    
1480
      for i in result.payload:
1481
        if i is None:
1482
          decoded.append(None)
1483
          continue
1484
        decoded.append(objects.ImportExportStatus.FromDict(i))
1485

    
1486
      result.payload = decoded
1487

    
1488
    return result
1489

    
1490
  @_RpcTimeout(_TMO_NORMAL)
1491
  def call_impexp_abort(self, node, name):
1492
    """Aborts an import or export.
1493

1494
    This is a single-node call.
1495

1496
    @type node: string
1497
    @param node: Node name
1498
    @type name: string
1499
    @param name: Import/export name
1500

1501
    """
1502
    return self._SingleNodeCall(node, "impexp_abort", [name])
1503

    
1504
  @_RpcTimeout(_TMO_NORMAL)
1505
  def call_impexp_cleanup(self, node, name):
1506
    """Cleans up after an import or export.
1507

1508
    This is a single-node call.
1509

1510
    @type node: string
1511
    @param node: Node name
1512
    @type name: string
1513
    @param name: Import/export name
1514

1515
    """
1516
    return self._SingleNodeCall(node, "impexp_cleanup", [name])