Statistics
| Branch: | Tag: | Revision:

root / lib / rpc.py @ 243cdbcc

History | View | Annotate | Download (16.9 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 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 a8083063 Iustin Pop
"""Script to show add a new node to the cluster
23 a8083063 Iustin Pop

24 a8083063 Iustin Pop
"""
25 a8083063 Iustin Pop
26 a8083063 Iustin Pop
# pylint: disable-msg=C0103
27 a8083063 Iustin Pop
28 a8083063 Iustin Pop
import os
29 81010134 Iustin Pop
import socket
30 81010134 Iustin Pop
import httplib
31 a8083063 Iustin Pop
32 81010134 Iustin Pop
import simplejson
33 a8083063 Iustin Pop
34 a8083063 Iustin Pop
from ganeti import logger
35 a8083063 Iustin Pop
from ganeti import utils
36 a8083063 Iustin Pop
from ganeti import errors
37 a8083063 Iustin Pop
from ganeti import constants
38 a8083063 Iustin Pop
from ganeti import objects
39 a8083063 Iustin Pop
from ganeti import ssconf
40 a8083063 Iustin Pop
41 7c0d6283 Michael Hanselmann
42 a8083063 Iustin Pop
class NodeController:
43 a8083063 Iustin Pop
  """Node-handling class.
44 a8083063 Iustin Pop

45 a8083063 Iustin Pop
  For each node that we speak with, we create an instance of this
46 a8083063 Iustin Pop
  class, so that we have a safe place to store the details of this
47 a8083063 Iustin Pop
  individual call.
48 a8083063 Iustin Pop

49 a8083063 Iustin Pop
  """
50 a8083063 Iustin Pop
  def __init__(self, parent, node):
51 a8083063 Iustin Pop
    self.parent = parent
52 a8083063 Iustin Pop
    self.node = node
53 81010134 Iustin Pop
    self.failed = False
54 a8083063 Iustin Pop
55 81010134 Iustin Pop
    self.http_conn = hc = httplib.HTTPConnection(node, self.parent.port)
56 a8083063 Iustin Pop
    try:
57 81010134 Iustin Pop
      hc.connect()
58 81010134 Iustin Pop
      hc.putrequest('PUT', "/%s" % self.parent.procedure,
59 81010134 Iustin Pop
                    skip_accept_encoding=True)
60 81010134 Iustin Pop
      hc.putheader('Content-Length', str(len(parent.body)))
61 81010134 Iustin Pop
      hc.endheaders()
62 81010134 Iustin Pop
      hc.send(parent.body)
63 81010134 Iustin Pop
    except socket.error, err:
64 81010134 Iustin Pop
      logger.Error("Error connecting to %s: %s" % (node, str(err)))
65 81010134 Iustin Pop
      self.failed = True
66 81010134 Iustin Pop
67 81010134 Iustin Pop
  def get_response(self):
68 81010134 Iustin Pop
    """Try to process the response from the node.
69 a8083063 Iustin Pop

70 a8083063 Iustin Pop
    """
71 81010134 Iustin Pop
    if self.failed:
72 81010134 Iustin Pop
      # we already failed in connect
73 a8083063 Iustin Pop
      return False
74 81010134 Iustin Pop
    resp = self.http_conn.getresponse()
75 81010134 Iustin Pop
    if resp.status != 200:
76 a8083063 Iustin Pop
      return False
77 81010134 Iustin Pop
    try:
78 81010134 Iustin Pop
      length = int(resp.getheader('Content-Length', '0'))
79 81010134 Iustin Pop
    except ValueError:
80 81010134 Iustin Pop
      return False
81 81010134 Iustin Pop
    if not length:
82 81010134 Iustin Pop
      logger.Error("Zero-length reply from %s" % self.node)
83 81010134 Iustin Pop
      return False
84 81010134 Iustin Pop
    payload = resp.read(length)
85 81010134 Iustin Pop
    unload = simplejson.loads(payload)
86 81010134 Iustin Pop
    return unload
87 a8083063 Iustin Pop
88 a8083063 Iustin Pop
89 a8083063 Iustin Pop
class Client:
90 a8083063 Iustin Pop
  """RPC Client class.
91 a8083063 Iustin Pop

92 2f8598a5 Alexander Schreiber
  This class, given a (remote) method name, a list of parameters and a
93 a8083063 Iustin Pop
  list of nodes, will contact (in parallel) all nodes, and return a
94 a8083063 Iustin Pop
  dict of results (key: node name, value: result).
95 a8083063 Iustin Pop

96 a8083063 Iustin Pop
  One current bug is that generic failure is still signalled by
97 a8083063 Iustin Pop
  'False' result, which is not good. This overloading of values can
98 a8083063 Iustin Pop
  cause bugs.
99 a8083063 Iustin Pop

100 a8083063 Iustin Pop
  """
101 a8083063 Iustin Pop
  result_set = False
102 a8083063 Iustin Pop
  result = False
103 a8083063 Iustin Pop
  allresult = []
104 a8083063 Iustin Pop
105 a8083063 Iustin Pop
  def __init__(self, procedure, args):
106 a8083063 Iustin Pop
    ss = ssconf.SimpleStore()
107 a8083063 Iustin Pop
    self.port = ss.GetNodeDaemonPort()
108 a8083063 Iustin Pop
    self.nodepw = ss.GetNodeDaemonPassword()
109 a8083063 Iustin Pop
    self.nc = {}
110 a8083063 Iustin Pop
    self.results = {}
111 a8083063 Iustin Pop
    self.procedure = procedure
112 a8083063 Iustin Pop
    self.args = args
113 81010134 Iustin Pop
    self.body = simplejson.dumps(args)
114 a8083063 Iustin Pop
115 a8083063 Iustin Pop
  #--- generic connector -------------
116 a8083063 Iustin Pop
117 a8083063 Iustin Pop
  def connect_list(self, node_list):
118 a8083063 Iustin Pop
    """Add a list of nodes to the target nodes.
119 a8083063 Iustin Pop

120 a8083063 Iustin Pop
    """
121 a8083063 Iustin Pop
    for node in node_list:
122 a8083063 Iustin Pop
      self.connect(node)
123 a8083063 Iustin Pop
124 a8083063 Iustin Pop
  def connect(self, connect_node):
125 a8083063 Iustin Pop
    """Add a node to the target list.
126 a8083063 Iustin Pop

127 a8083063 Iustin Pop
    """
128 a8083063 Iustin Pop
    self.nc[connect_node] = nc = NodeController(self, connect_node)
129 a8083063 Iustin Pop
130 a8083063 Iustin Pop
  def getresult(self):
131 a8083063 Iustin Pop
    """Return the results of the call.
132 a8083063 Iustin Pop

133 a8083063 Iustin Pop
    """
134 a8083063 Iustin Pop
    return self.results
135 a8083063 Iustin Pop
136 a8083063 Iustin Pop
  def run(self):
137 a8083063 Iustin Pop
    """Wrapper over reactor.run().
138 a8083063 Iustin Pop

139 a8083063 Iustin Pop
    This function simply calls reactor.run() if we have any requests
140 a8083063 Iustin Pop
    queued, otherwise it does nothing.
141 a8083063 Iustin Pop

142 a8083063 Iustin Pop
    """
143 81010134 Iustin Pop
    for node, nc in self.nc.items():
144 81010134 Iustin Pop
      self.results[node] = nc.get_response()
145 a8083063 Iustin Pop
146 a8083063 Iustin Pop
147 a8083063 Iustin Pop
def call_volume_list(node_list, vg_name):
148 a8083063 Iustin Pop
  """Gets the logical volumes present in a given volume group.
149 a8083063 Iustin Pop

150 a8083063 Iustin Pop
  This is a multi-node call.
151 a8083063 Iustin Pop

152 a8083063 Iustin Pop
  """
153 a8083063 Iustin Pop
  c = Client("volume_list", [vg_name])
154 a8083063 Iustin Pop
  c.connect_list(node_list)
155 a8083063 Iustin Pop
  c.run()
156 a8083063 Iustin Pop
  return c.getresult()
157 a8083063 Iustin Pop
158 a8083063 Iustin Pop
159 a8083063 Iustin Pop
def call_vg_list(node_list):
160 a8083063 Iustin Pop
  """Gets the volume group list.
161 a8083063 Iustin Pop

162 a8083063 Iustin Pop
  This is a multi-node call.
163 a8083063 Iustin Pop

164 a8083063 Iustin Pop
  """
165 a8083063 Iustin Pop
  c = Client("vg_list", [])
166 a8083063 Iustin Pop
  c.connect_list(node_list)
167 a8083063 Iustin Pop
  c.run()
168 a8083063 Iustin Pop
  return c.getresult()
169 a8083063 Iustin Pop
170 a8083063 Iustin Pop
171 a8083063 Iustin Pop
def call_bridges_exist(node, bridges_list):
172 a8083063 Iustin Pop
  """Checks if a node has all the bridges given.
173 a8083063 Iustin Pop

174 a8083063 Iustin Pop
  This method checks if all bridges given in the bridges_list are
175 a8083063 Iustin Pop
  present on the remote node, so that an instance that uses interfaces
176 a8083063 Iustin Pop
  on those bridges can be started.
177 a8083063 Iustin Pop

178 a8083063 Iustin Pop
  This is a single-node call.
179 a8083063 Iustin Pop

180 a8083063 Iustin Pop
  """
181 a8083063 Iustin Pop
  c = Client("bridges_exist", [bridges_list])
182 a8083063 Iustin Pop
  c.connect(node)
183 a8083063 Iustin Pop
  c.run()
184 a8083063 Iustin Pop
  return c.getresult().get(node, False)
185 a8083063 Iustin Pop
186 a8083063 Iustin Pop
187 a8083063 Iustin Pop
def call_instance_start(node, instance, extra_args):
188 2f8598a5 Alexander Schreiber
  """Starts an instance.
189 a8083063 Iustin Pop

190 a8083063 Iustin Pop
  This is a single-node call.
191 a8083063 Iustin Pop

192 a8083063 Iustin Pop
  """
193 319856a9 Michael Hanselmann
  c = Client("instance_start", [instance.ToDict(), extra_args])
194 a8083063 Iustin Pop
  c.connect(node)
195 a8083063 Iustin Pop
  c.run()
196 a8083063 Iustin Pop
  return c.getresult().get(node, False)
197 a8083063 Iustin Pop
198 a8083063 Iustin Pop
199 a8083063 Iustin Pop
def call_instance_shutdown(node, instance):
200 a8083063 Iustin Pop
  """Stops an instance.
201 a8083063 Iustin Pop

202 a8083063 Iustin Pop
  This is a single-node call.
203 a8083063 Iustin Pop

204 a8083063 Iustin Pop
  """
205 319856a9 Michael Hanselmann
  c = Client("instance_shutdown", [instance.ToDict()])
206 a8083063 Iustin Pop
  c.connect(node)
207 a8083063 Iustin Pop
  c.run()
208 a8083063 Iustin Pop
  return c.getresult().get(node, False)
209 a8083063 Iustin Pop
210 a8083063 Iustin Pop
211 007a2f3e Alexander Schreiber
def call_instance_reboot(node, instance, reboot_type, extra_args):
212 007a2f3e Alexander Schreiber
  """Reboots an instance.
213 007a2f3e Alexander Schreiber

214 007a2f3e Alexander Schreiber
  This is a single-node call.
215 007a2f3e Alexander Schreiber

216 007a2f3e Alexander Schreiber
  """
217 007a2f3e Alexander Schreiber
  c = Client("instance_reboot", [instance.ToDict(), reboot_type, extra_args])
218 007a2f3e Alexander Schreiber
  c.connect(node)
219 007a2f3e Alexander Schreiber
  c.run()
220 007a2f3e Alexander Schreiber
  return c.getresult().get(node, False)
221 007a2f3e Alexander Schreiber
222 007a2f3e Alexander Schreiber
223 a8083063 Iustin Pop
def call_instance_os_add(node, inst, osdev, swapdev):
224 a8083063 Iustin Pop
  """Installs an OS on the given instance.
225 a8083063 Iustin Pop

226 a8083063 Iustin Pop
  This is a single-node call.
227 a8083063 Iustin Pop

228 a8083063 Iustin Pop
  """
229 319856a9 Michael Hanselmann
  params = [inst.ToDict(), osdev, swapdev]
230 a8083063 Iustin Pop
  c = Client("instance_os_add", params)
231 a8083063 Iustin Pop
  c.connect(node)
232 a8083063 Iustin Pop
  c.run()
233 a8083063 Iustin Pop
  return c.getresult().get(node, False)
234 a8083063 Iustin Pop
235 a8083063 Iustin Pop
236 decd5f45 Iustin Pop
def call_instance_run_rename(node, inst, old_name, osdev, swapdev):
237 decd5f45 Iustin Pop
  """Run the OS rename script for an instance.
238 decd5f45 Iustin Pop

239 decd5f45 Iustin Pop
  This is a single-node call.
240 decd5f45 Iustin Pop

241 decd5f45 Iustin Pop
  """
242 319856a9 Michael Hanselmann
  params = [inst.ToDict(), old_name, osdev, swapdev]
243 decd5f45 Iustin Pop
  c = Client("instance_run_rename", params)
244 decd5f45 Iustin Pop
  c.connect(node)
245 decd5f45 Iustin Pop
  c.run()
246 decd5f45 Iustin Pop
  return c.getresult().get(node, False)
247 decd5f45 Iustin Pop
248 decd5f45 Iustin Pop
249 a8083063 Iustin Pop
def call_instance_info(node, instance):
250 a8083063 Iustin Pop
  """Returns information about a single instance.
251 a8083063 Iustin Pop

252 a8083063 Iustin Pop
  This is a single-node call.
253 a8083063 Iustin Pop

254 a8083063 Iustin Pop
  """
255 a8083063 Iustin Pop
  c = Client("instance_info", [instance])
256 a8083063 Iustin Pop
  c.connect(node)
257 a8083063 Iustin Pop
  c.run()
258 a8083063 Iustin Pop
  return c.getresult().get(node, False)
259 a8083063 Iustin Pop
260 a8083063 Iustin Pop
261 a8083063 Iustin Pop
def call_all_instances_info(node_list):
262 a8083063 Iustin Pop
  """Returns information about all instances on a given node.
263 a8083063 Iustin Pop

264 a8083063 Iustin Pop
  This is a single-node call.
265 a8083063 Iustin Pop

266 a8083063 Iustin Pop
  """
267 a8083063 Iustin Pop
  c = Client("all_instances_info", [])
268 a8083063 Iustin Pop
  c.connect_list(node_list)
269 a8083063 Iustin Pop
  c.run()
270 a8083063 Iustin Pop
  return c.getresult()
271 a8083063 Iustin Pop
272 a8083063 Iustin Pop
273 a8083063 Iustin Pop
def call_instance_list(node_list):
274 a8083063 Iustin Pop
  """Returns the list of running instances on a given node.
275 a8083063 Iustin Pop

276 a8083063 Iustin Pop
  This is a single-node call.
277 a8083063 Iustin Pop

278 a8083063 Iustin Pop
  """
279 a8083063 Iustin Pop
  c = Client("instance_list", [])
280 a8083063 Iustin Pop
  c.connect_list(node_list)
281 a8083063 Iustin Pop
  c.run()
282 a8083063 Iustin Pop
  return c.getresult()
283 a8083063 Iustin Pop
284 a8083063 Iustin Pop
285 16abfbc2 Alexander Schreiber
def call_node_tcp_ping(node, source, target, port, timeout, live_port_needed):
286 16abfbc2 Alexander Schreiber
  """Do a TcpPing on the remote node
287 16abfbc2 Alexander Schreiber

288 16abfbc2 Alexander Schreiber
  This is a single-node call.
289 16abfbc2 Alexander Schreiber
  """
290 16abfbc2 Alexander Schreiber
  c = Client("node_tcp_ping", [source, target, port, timeout,
291 16abfbc2 Alexander Schreiber
                               live_port_needed])
292 16abfbc2 Alexander Schreiber
  c.connect(node)
293 16abfbc2 Alexander Schreiber
  c.run()
294 16abfbc2 Alexander Schreiber
  return c.getresult().get(node, False)
295 16abfbc2 Alexander Schreiber
296 16abfbc2 Alexander Schreiber
297 a8083063 Iustin Pop
def call_node_info(node_list, vg_name):
298 a8083063 Iustin Pop
  """Return node information.
299 a8083063 Iustin Pop

300 a8083063 Iustin Pop
  This will return memory information and volume group size and free
301 a8083063 Iustin Pop
  space.
302 a8083063 Iustin Pop

303 a8083063 Iustin Pop
  This is a multi-node call.
304 a8083063 Iustin Pop

305 a8083063 Iustin Pop
  """
306 a8083063 Iustin Pop
  c = Client("node_info", [vg_name])
307 a8083063 Iustin Pop
  c.connect_list(node_list)
308 a8083063 Iustin Pop
  c.run()
309 a8083063 Iustin Pop
  retux = c.getresult()
310 a8083063 Iustin Pop
311 a8083063 Iustin Pop
  for node_name in retux:
312 a8083063 Iustin Pop
    ret = retux.get(node_name, False)
313 a8083063 Iustin Pop
    if type(ret) != dict:
314 a8083063 Iustin Pop
      logger.Error("could not connect to node %s" % (node_name))
315 a8083063 Iustin Pop
      ret = {}
316 a8083063 Iustin Pop
317 a8083063 Iustin Pop
    utils.CheckDict(ret,
318 a8083063 Iustin Pop
                    { 'memory_total' : '-',
319 a8083063 Iustin Pop
                      'memory_dom0' : '-',
320 a8083063 Iustin Pop
                      'memory_free' : '-',
321 a8083063 Iustin Pop
                      'vg_size' : 'node_unreachable',
322 a8083063 Iustin Pop
                      'vg_free' : '-' },
323 a8083063 Iustin Pop
                    "call_node_info",
324 a8083063 Iustin Pop
                    )
325 a8083063 Iustin Pop
  return retux
326 a8083063 Iustin Pop
327 a8083063 Iustin Pop
328 a8083063 Iustin Pop
def call_node_add(node, dsa, dsapub, rsa, rsapub, ssh, sshpub):
329 a8083063 Iustin Pop
  """Add a node to the cluster.
330 a8083063 Iustin Pop

331 a8083063 Iustin Pop
  This is a single-node call.
332 a8083063 Iustin Pop

333 a8083063 Iustin Pop
  """
334 a8083063 Iustin Pop
  params = [dsa, dsapub, rsa, rsapub, ssh, sshpub]
335 a8083063 Iustin Pop
  c = Client("node_add", params)
336 a8083063 Iustin Pop
  c.connect(node)
337 a8083063 Iustin Pop
  c.run()
338 a8083063 Iustin Pop
  return c.getresult().get(node, False)
339 a8083063 Iustin Pop
340 a8083063 Iustin Pop
341 a8083063 Iustin Pop
def call_node_verify(node_list, checkdict):
342 a8083063 Iustin Pop
  """Request verification of given parameters.
343 a8083063 Iustin Pop

344 a8083063 Iustin Pop
  This is a multi-node call.
345 a8083063 Iustin Pop

346 a8083063 Iustin Pop
  """
347 a8083063 Iustin Pop
  c = Client("node_verify", [checkdict])
348 a8083063 Iustin Pop
  c.connect_list(node_list)
349 a8083063 Iustin Pop
  c.run()
350 a8083063 Iustin Pop
  return c.getresult()
351 a8083063 Iustin Pop
352 a8083063 Iustin Pop
353 a8083063 Iustin Pop
def call_node_start_master(node):
354 a8083063 Iustin Pop
  """Tells a node to activate itself as a master.
355 a8083063 Iustin Pop

356 a8083063 Iustin Pop
  This is a single-node call.
357 a8083063 Iustin Pop

358 a8083063 Iustin Pop
  """
359 a8083063 Iustin Pop
  c = Client("node_start_master", [])
360 a8083063 Iustin Pop
  c.connect(node)
361 a8083063 Iustin Pop
  c.run()
362 a8083063 Iustin Pop
  return c.getresult().get(node, False)
363 a8083063 Iustin Pop
364 a8083063 Iustin Pop
365 a8083063 Iustin Pop
def call_node_stop_master(node):
366 a8083063 Iustin Pop
  """Tells a node to demote itself from master status.
367 a8083063 Iustin Pop

368 a8083063 Iustin Pop
  This is a single-node call.
369 a8083063 Iustin Pop

370 a8083063 Iustin Pop
  """
371 a8083063 Iustin Pop
  c = Client("node_stop_master", [])
372 a8083063 Iustin Pop
  c.connect(node)
373 a8083063 Iustin Pop
  c.run()
374 a8083063 Iustin Pop
  return c.getresult().get(node, False)
375 a8083063 Iustin Pop
376 a8083063 Iustin Pop
377 a8083063 Iustin Pop
def call_version(node_list):
378 a8083063 Iustin Pop
  """Query node version.
379 a8083063 Iustin Pop

380 a8083063 Iustin Pop
  This is a multi-node call.
381 a8083063 Iustin Pop

382 a8083063 Iustin Pop
  """
383 a8083063 Iustin Pop
  c = Client("version", [])
384 a8083063 Iustin Pop
  c.connect_list(node_list)
385 a8083063 Iustin Pop
  c.run()
386 a8083063 Iustin Pop
  return c.getresult()
387 a8083063 Iustin Pop
388 a8083063 Iustin Pop
389 3f78eef2 Iustin Pop
def call_blockdev_create(node, bdev, size, owner, on_primary, info):
390 a8083063 Iustin Pop
  """Request creation of a given block device.
391 a8083063 Iustin Pop

392 a8083063 Iustin Pop
  This is a single-node call.
393 a8083063 Iustin Pop

394 a8083063 Iustin Pop
  """
395 3f78eef2 Iustin Pop
  params = [bdev.ToDict(), size, owner, on_primary, info]
396 a8083063 Iustin Pop
  c = Client("blockdev_create", params)
397 a8083063 Iustin Pop
  c.connect(node)
398 a8083063 Iustin Pop
  c.run()
399 a8083063 Iustin Pop
  return c.getresult().get(node, False)
400 a8083063 Iustin Pop
401 a8083063 Iustin Pop
402 a8083063 Iustin Pop
def call_blockdev_remove(node, bdev):
403 a8083063 Iustin Pop
  """Request removal of a given block device.
404 a8083063 Iustin Pop

405 a8083063 Iustin Pop
  This is a single-node call.
406 a8083063 Iustin Pop

407 a8083063 Iustin Pop
  """
408 319856a9 Michael Hanselmann
  c = Client("blockdev_remove", [bdev.ToDict()])
409 a8083063 Iustin Pop
  c.connect(node)
410 a8083063 Iustin Pop
  c.run()
411 a8083063 Iustin Pop
  return c.getresult().get(node, False)
412 a8083063 Iustin Pop
413 a8083063 Iustin Pop
414 f3e513ad Iustin Pop
def call_blockdev_rename(node, devlist):
415 f3e513ad Iustin Pop
  """Request rename of the given block devices.
416 f3e513ad Iustin Pop

417 f3e513ad Iustin Pop
  This is a single-node call.
418 f3e513ad Iustin Pop

419 f3e513ad Iustin Pop
  """
420 f3e513ad Iustin Pop
  params = [(d.ToDict(), uid) for d, uid in devlist]
421 f3e513ad Iustin Pop
  c = Client("blockdev_rename", params)
422 f3e513ad Iustin Pop
  c.connect(node)
423 f3e513ad Iustin Pop
  c.run()
424 f3e513ad Iustin Pop
  return c.getresult().get(node, False)
425 f3e513ad Iustin Pop
426 f3e513ad Iustin Pop
427 3f78eef2 Iustin Pop
def call_blockdev_assemble(node, disk, owner, on_primary):
428 a8083063 Iustin Pop
  """Request assembling of a given block device.
429 a8083063 Iustin Pop

430 a8083063 Iustin Pop
  This is a single-node call.
431 a8083063 Iustin Pop

432 a8083063 Iustin Pop
  """
433 3f78eef2 Iustin Pop
  params = [disk.ToDict(), owner, on_primary]
434 a8083063 Iustin Pop
  c = Client("blockdev_assemble", params)
435 a8083063 Iustin Pop
  c.connect(node)
436 a8083063 Iustin Pop
  c.run()
437 a8083063 Iustin Pop
  return c.getresult().get(node, False)
438 a8083063 Iustin Pop
439 a8083063 Iustin Pop
440 a8083063 Iustin Pop
def call_blockdev_shutdown(node, disk):
441 a8083063 Iustin Pop
  """Request shutdown of a given block device.
442 a8083063 Iustin Pop

443 a8083063 Iustin Pop
  This is a single-node call.
444 a8083063 Iustin Pop

445 a8083063 Iustin Pop
  """
446 319856a9 Michael Hanselmann
  c = Client("blockdev_shutdown", [disk.ToDict()])
447 a8083063 Iustin Pop
  c.connect(node)
448 a8083063 Iustin Pop
  c.run()
449 a8083063 Iustin Pop
  return c.getresult().get(node, False)
450 a8083063 Iustin Pop
451 a8083063 Iustin Pop
452 153d9724 Iustin Pop
def call_blockdev_addchildren(node, bdev, ndevs):
453 153d9724 Iustin Pop
  """Request adding a list of children to a (mirroring) device.
454 a8083063 Iustin Pop

455 a8083063 Iustin Pop
  This is a single-node call.
456 a8083063 Iustin Pop

457 a8083063 Iustin Pop
  """
458 153d9724 Iustin Pop
  params = [bdev.ToDict(), [disk.ToDict() for disk in ndevs]]
459 153d9724 Iustin Pop
  c = Client("blockdev_addchildren", params)
460 a8083063 Iustin Pop
  c.connect(node)
461 a8083063 Iustin Pop
  c.run()
462 a8083063 Iustin Pop
  return c.getresult().get(node, False)
463 a8083063 Iustin Pop
464 a8083063 Iustin Pop
465 153d9724 Iustin Pop
def call_blockdev_removechildren(node, bdev, ndevs):
466 153d9724 Iustin Pop
  """Request removing a list of children from a (mirroring) device.
467 a8083063 Iustin Pop

468 a8083063 Iustin Pop
  This is a single-node call.
469 a8083063 Iustin Pop

470 a8083063 Iustin Pop
  """
471 153d9724 Iustin Pop
  params = [bdev.ToDict(), [disk.ToDict() for disk in ndevs]]
472 153d9724 Iustin Pop
  c = Client("blockdev_removechildren", params)
473 a8083063 Iustin Pop
  c.connect(node)
474 a8083063 Iustin Pop
  c.run()
475 a8083063 Iustin Pop
  return c.getresult().get(node, False)
476 a8083063 Iustin Pop
477 a8083063 Iustin Pop
478 a8083063 Iustin Pop
def call_blockdev_getmirrorstatus(node, disks):
479 a8083063 Iustin Pop
  """Request status of a (mirroring) device.
480 a8083063 Iustin Pop

481 a8083063 Iustin Pop
  This is a single-node call.
482 a8083063 Iustin Pop

483 a8083063 Iustin Pop
  """
484 319856a9 Michael Hanselmann
  params = [dsk.ToDict() for dsk in disks]
485 a8083063 Iustin Pop
  c = Client("blockdev_getmirrorstatus", params)
486 a8083063 Iustin Pop
  c.connect(node)
487 a8083063 Iustin Pop
  c.run()
488 a8083063 Iustin Pop
  return c.getresult().get(node, False)
489 a8083063 Iustin Pop
490 a8083063 Iustin Pop
491 a8083063 Iustin Pop
def call_blockdev_find(node, disk):
492 a8083063 Iustin Pop
  """Request identification of a given block device.
493 a8083063 Iustin Pop

494 a8083063 Iustin Pop
  This is a single-node call.
495 a8083063 Iustin Pop

496 a8083063 Iustin Pop
  """
497 319856a9 Michael Hanselmann
  c = Client("blockdev_find", [disk.ToDict()])
498 a8083063 Iustin Pop
  c.connect(node)
499 a8083063 Iustin Pop
  c.run()
500 a8083063 Iustin Pop
  return c.getresult().get(node, False)
501 a8083063 Iustin Pop
502 a8083063 Iustin Pop
503 a8083063 Iustin Pop
def call_upload_file(node_list, file_name):
504 a8083063 Iustin Pop
  """Upload a file.
505 a8083063 Iustin Pop

506 a8083063 Iustin Pop
  The node will refuse the operation in case the file is not on the
507 a8083063 Iustin Pop
  approved file list.
508 a8083063 Iustin Pop

509 a8083063 Iustin Pop
  This is a multi-node call.
510 a8083063 Iustin Pop

511 a8083063 Iustin Pop
  """
512 a8083063 Iustin Pop
  fh = file(file_name)
513 a8083063 Iustin Pop
  try:
514 a8083063 Iustin Pop
    data = fh.read()
515 a8083063 Iustin Pop
  finally:
516 a8083063 Iustin Pop
    fh.close()
517 a8083063 Iustin Pop
  st = os.stat(file_name)
518 a8083063 Iustin Pop
  params = [file_name, data, st.st_mode, st.st_uid, st.st_gid,
519 a8083063 Iustin Pop
            st.st_atime, st.st_mtime]
520 a8083063 Iustin Pop
  c = Client("upload_file", params)
521 a8083063 Iustin Pop
  c.connect_list(node_list)
522 a8083063 Iustin Pop
  c.run()
523 a8083063 Iustin Pop
  return c.getresult()
524 a8083063 Iustin Pop
525 a8083063 Iustin Pop
526 a8083063 Iustin Pop
def call_os_diagnose(node_list):
527 a8083063 Iustin Pop
  """Request a diagnose of OS definitions.
528 a8083063 Iustin Pop

529 a8083063 Iustin Pop
  This is a multi-node call.
530 a8083063 Iustin Pop

531 a8083063 Iustin Pop
  """
532 a8083063 Iustin Pop
  c = Client("os_diagnose", [])
533 a8083063 Iustin Pop
  c.connect_list(node_list)
534 a8083063 Iustin Pop
  c.run()
535 a8083063 Iustin Pop
  result = c.getresult()
536 a8083063 Iustin Pop
  new_result = {}
537 a8083063 Iustin Pop
  for node_name in result:
538 a8083063 Iustin Pop
    if result[node_name]:
539 4e679f11 Guido Trotter
      nr = [objects.OS.FromDict(oss) for oss in result[node_name]]
540 4e679f11 Guido Trotter
    else:
541 4e679f11 Guido Trotter
      nr = []
542 a8083063 Iustin Pop
    new_result[node_name] = nr
543 a8083063 Iustin Pop
  return new_result
544 a8083063 Iustin Pop
545 a8083063 Iustin Pop
546 00fe9e38 Guido Trotter
def call_os_get(node, name):
547 a8083063 Iustin Pop
  """Returns an OS definition.
548 a8083063 Iustin Pop

549 00fe9e38 Guido Trotter
  This is a single-node call.
550 a8083063 Iustin Pop

551 a8083063 Iustin Pop
  """
552 a8083063 Iustin Pop
  c = Client("os_get", [name])
553 00fe9e38 Guido Trotter
  c.connect(node)
554 a8083063 Iustin Pop
  c.run()
555 00fe9e38 Guido Trotter
  result = c.getresult().get(node, False)
556 00fe9e38 Guido Trotter
  if isinstance(result, dict):
557 dfa96ded Guido Trotter
    return objects.OS.FromDict(result)
558 00fe9e38 Guido Trotter
  else:
559 dfa96ded Guido Trotter
    return result
560 a8083063 Iustin Pop
561 a8083063 Iustin Pop
562 a8083063 Iustin Pop
def call_hooks_runner(node_list, hpath, phase, env):
563 a8083063 Iustin Pop
  """Call the hooks runner.
564 a8083063 Iustin Pop

565 a8083063 Iustin Pop
  Args:
566 a8083063 Iustin Pop
    - op: the OpCode instance
567 a8083063 Iustin Pop
    - env: a dictionary with the environment
568 a8083063 Iustin Pop

569 a8083063 Iustin Pop
  This is a multi-node call.
570 a8083063 Iustin Pop

571 a8083063 Iustin Pop
  """
572 a8083063 Iustin Pop
  params = [hpath, phase, env]
573 a8083063 Iustin Pop
  c = Client("hooks_runner", params)
574 a8083063 Iustin Pop
  c.connect_list(node_list)
575 a8083063 Iustin Pop
  c.run()
576 a8083063 Iustin Pop
  result = c.getresult()
577 a8083063 Iustin Pop
  return result
578 a8083063 Iustin Pop
579 a8083063 Iustin Pop
580 8d528b7c Iustin Pop
def call_iallocator_runner(node, name, idata):
581 8d528b7c Iustin Pop
  """Call an iallocator on a remote node
582 8d528b7c Iustin Pop

583 8d528b7c Iustin Pop
  Args:
584 8d528b7c Iustin Pop
    - name: the iallocator name
585 8d528b7c Iustin Pop
    - input: the json-encoded input string
586 8d528b7c Iustin Pop

587 8d528b7c Iustin Pop
  This is a single-node call.
588 8d528b7c Iustin Pop

589 8d528b7c Iustin Pop
  """
590 8d528b7c Iustin Pop
  params = [name, idata]
591 8d528b7c Iustin Pop
  c = Client("iallocator_runner", params)
592 8d528b7c Iustin Pop
  c.connect(node)
593 8d528b7c Iustin Pop
  c.run()
594 8d528b7c Iustin Pop
  result = c.getresult().get(node, False)
595 8d528b7c Iustin Pop
  return result
596 8d528b7c Iustin Pop
597 8d528b7c Iustin Pop
598 a8083063 Iustin Pop
def call_blockdev_snapshot(node, cf_bdev):
599 a8083063 Iustin Pop
  """Request a snapshot of the given block device.
600 a8083063 Iustin Pop

601 a8083063 Iustin Pop
  This is a single-node call.
602 a8083063 Iustin Pop

603 a8083063 Iustin Pop
  """
604 319856a9 Michael Hanselmann
  c = Client("blockdev_snapshot", [cf_bdev.ToDict()])
605 a8083063 Iustin Pop
  c.connect(node)
606 a8083063 Iustin Pop
  c.run()
607 a8083063 Iustin Pop
  return c.getresult().get(node, False)
608 a8083063 Iustin Pop
609 a8083063 Iustin Pop
610 a8083063 Iustin Pop
def call_snapshot_export(node, snap_bdev, dest_node, instance):
611 a8083063 Iustin Pop
  """Request the export of a given snapshot.
612 a8083063 Iustin Pop

613 a8083063 Iustin Pop
  This is a single-node call.
614 a8083063 Iustin Pop

615 a8083063 Iustin Pop
  """
616 319856a9 Michael Hanselmann
  params = [snap_bdev.ToDict(), dest_node, instance.ToDict()]
617 a8083063 Iustin Pop
  c = Client("snapshot_export", params)
618 a8083063 Iustin Pop
  c.connect(node)
619 a8083063 Iustin Pop
  c.run()
620 a8083063 Iustin Pop
  return c.getresult().get(node, False)
621 a8083063 Iustin Pop
622 a8083063 Iustin Pop
623 a8083063 Iustin Pop
def call_finalize_export(node, instance, snap_disks):
624 a8083063 Iustin Pop
  """Request the completion of an export operation.
625 a8083063 Iustin Pop

626 a8083063 Iustin Pop
  This writes the export config file, etc.
627 a8083063 Iustin Pop

628 a8083063 Iustin Pop
  This is a single-node call.
629 a8083063 Iustin Pop

630 a8083063 Iustin Pop
  """
631 a8083063 Iustin Pop
  flat_disks = []
632 a8083063 Iustin Pop
  for disk in snap_disks:
633 319856a9 Michael Hanselmann
    flat_disks.append(disk.ToDict())
634 319856a9 Michael Hanselmann
  params = [instance.ToDict(), flat_disks]
635 a8083063 Iustin Pop
  c = Client("finalize_export", params)
636 a8083063 Iustin Pop
  c.connect(node)
637 a8083063 Iustin Pop
  c.run()
638 a8083063 Iustin Pop
  return c.getresult().get(node, False)
639 a8083063 Iustin Pop
640 a8083063 Iustin Pop
641 a8083063 Iustin Pop
def call_export_info(node, path):
642 a8083063 Iustin Pop
  """Queries the export information in a given path.
643 a8083063 Iustin Pop

644 a8083063 Iustin Pop
  This is a single-node call.
645 a8083063 Iustin Pop

646 a8083063 Iustin Pop
  """
647 a8083063 Iustin Pop
  c = Client("export_info", [path])
648 a8083063 Iustin Pop
  c.connect(node)
649 a8083063 Iustin Pop
  c.run()
650 a8083063 Iustin Pop
  result = c.getresult().get(node, False)
651 a8083063 Iustin Pop
  if not result:
652 a8083063 Iustin Pop
    return result
653 2d3e73c4 Michael Hanselmann
  return objects.SerializableConfigParser.Loads(str(result))
654 a8083063 Iustin Pop
655 a8083063 Iustin Pop
656 a8083063 Iustin Pop
def call_instance_os_import(node, inst, osdev, swapdev, src_node, src_image):
657 a8083063 Iustin Pop
  """Request the import of a backup into an instance.
658 a8083063 Iustin Pop

659 a8083063 Iustin Pop
  This is a single-node call.
660 a8083063 Iustin Pop

661 a8083063 Iustin Pop
  """
662 319856a9 Michael Hanselmann
  params = [inst.ToDict(), osdev, swapdev, src_node, src_image]
663 a8083063 Iustin Pop
  c = Client("instance_os_import", params)
664 a8083063 Iustin Pop
  c.connect(node)
665 a8083063 Iustin Pop
  c.run()
666 a8083063 Iustin Pop
  return c.getresult().get(node, False)
667 a8083063 Iustin Pop
668 a8083063 Iustin Pop
669 a8083063 Iustin Pop
def call_export_list(node_list):
670 a8083063 Iustin Pop
  """Gets the stored exports list.
671 a8083063 Iustin Pop

672 a8083063 Iustin Pop
  This is a multi-node call.
673 a8083063 Iustin Pop

674 a8083063 Iustin Pop
  """
675 a8083063 Iustin Pop
  c = Client("export_list", [])
676 a8083063 Iustin Pop
  c.connect_list(node_list)
677 a8083063 Iustin Pop
  c.run()
678 a8083063 Iustin Pop
  result = c.getresult()
679 a8083063 Iustin Pop
  return result
680 a8083063 Iustin Pop
681 a8083063 Iustin Pop
682 a8083063 Iustin Pop
def call_export_remove(node, export):
683 a8083063 Iustin Pop
  """Requests removal of a given export.
684 a8083063 Iustin Pop

685 a8083063 Iustin Pop
  This is a single-node call.
686 a8083063 Iustin Pop

687 a8083063 Iustin Pop
  """
688 a8083063 Iustin Pop
  c = Client("export_remove", [export])
689 a8083063 Iustin Pop
  c.connect(node)
690 a8083063 Iustin Pop
  c.run()
691 a8083063 Iustin Pop
  return c.getresult().get(node, False)
692 a8083063 Iustin Pop
693 a8083063 Iustin Pop
694 a8083063 Iustin Pop
def call_node_leave_cluster(node):
695 a8083063 Iustin Pop
  """Requests a node to clean the cluster information it has.
696 a8083063 Iustin Pop

697 a8083063 Iustin Pop
  This will remove the configuration information from the ganeti data
698 a8083063 Iustin Pop
  dir.
699 a8083063 Iustin Pop

700 a8083063 Iustin Pop
  This is a single-node call.
701 a8083063 Iustin Pop

702 a8083063 Iustin Pop
  """
703 a8083063 Iustin Pop
  c = Client("node_leave_cluster", [])
704 a8083063 Iustin Pop
  c.connect(node)
705 a8083063 Iustin Pop
  c.run()
706 a8083063 Iustin Pop
  return c.getresult().get(node, False)
707 dcb93971 Michael Hanselmann
708 dcb93971 Michael Hanselmann
709 dcb93971 Michael Hanselmann
def call_node_volumes(node_list):
710 dcb93971 Michael Hanselmann
  """Gets all volumes on node(s).
711 dcb93971 Michael Hanselmann

712 dcb93971 Michael Hanselmann
  This is a multi-node call.
713 dcb93971 Michael Hanselmann

714 dcb93971 Michael Hanselmann
  """
715 dcb93971 Michael Hanselmann
  c = Client("node_volumes", [])
716 dcb93971 Michael Hanselmann
  c.connect_list(node_list)
717 dcb93971 Michael Hanselmann
  c.run()
718 dcb93971 Michael Hanselmann
  return c.getresult()
719 06009e27 Iustin Pop
720 06009e27 Iustin Pop
721 06009e27 Iustin Pop
def call_test_delay(node_list, duration):
722 06009e27 Iustin Pop
  """Sleep for a fixed time on given node(s).
723 06009e27 Iustin Pop

724 06009e27 Iustin Pop
  This is a multi-node call.
725 06009e27 Iustin Pop

726 06009e27 Iustin Pop
  """
727 06009e27 Iustin Pop
  c = Client("test_delay", [duration])
728 06009e27 Iustin Pop
  c.connect_list(node_list)
729 06009e27 Iustin Pop
  c.run()
730 06009e27 Iustin Pop
  return c.getresult()
731 5e04ed8b Manuel Franceschini
732 5e04ed8b Manuel Franceschini
733 5e04ed8b Manuel Franceschini
def call_file_storage_dir_create(node, file_storage_dir):
734 5e04ed8b Manuel Franceschini
  """Create the given file storage directory.
735 5e04ed8b Manuel Franceschini

736 5e04ed8b Manuel Franceschini
  This is a single-node call.
737 5e04ed8b Manuel Franceschini

738 5e04ed8b Manuel Franceschini
  """
739 5e04ed8b Manuel Franceschini
  c = Client("file_storage_dir_create", [file_storage_dir])
740 5e04ed8b Manuel Franceschini
  c.connect(node)
741 5e04ed8b Manuel Franceschini
  c.run()
742 5e04ed8b Manuel Franceschini
  return c.getresult().get(node, False)
743 5e04ed8b Manuel Franceschini
744 5e04ed8b Manuel Franceschini
745 5e04ed8b Manuel Franceschini
def call_file_storage_dir_remove(node, file_storage_dir):
746 5e04ed8b Manuel Franceschini
  """Remove the given file storage directory.
747 5e04ed8b Manuel Franceschini

748 5e04ed8b Manuel Franceschini
  This is a single-node call.
749 5e04ed8b Manuel Franceschini

750 5e04ed8b Manuel Franceschini
  """
751 5e04ed8b Manuel Franceschini
  c = Client("file_storage_dir_remove", [file_storage_dir])
752 5e04ed8b Manuel Franceschini
  c.connect(node)
753 5e04ed8b Manuel Franceschini
  c.run()
754 5e04ed8b Manuel Franceschini
  return c.getresult().get(node, False)
755 5e04ed8b Manuel Franceschini
756 5e04ed8b Manuel Franceschini
757 5e04ed8b Manuel Franceschini
def call_file_storage_dir_rename(node, old_file_storage_dir,
758 5e04ed8b Manuel Franceschini
                                 new_file_storage_dir):
759 5e04ed8b Manuel Franceschini
  """Rename file storage directory.
760 5e04ed8b Manuel Franceschini

761 5e04ed8b Manuel Franceschini
  This is a single-node call.
762 5e04ed8b Manuel Franceschini

763 5e04ed8b Manuel Franceschini
  """
764 5e04ed8b Manuel Franceschini
  c = Client("file_storage_dir_rename",
765 5e04ed8b Manuel Franceschini
             [old_file_storage_dir, new_file_storage_dir])
766 5e04ed8b Manuel Franceschini
  c.connect(node)
767 5e04ed8b Manuel Franceschini
  c.run()
768 5e04ed8b Manuel Franceschini
  return c.getresult().get(node, False)