Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ be9150ea

History | View | Annotate | Download (134.6 kB)

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

24 360b0dc2 Iustin Pop
@var _ALLOWED_UPLOAD_FILES: denotes which files are accepted in
25 360b0dc2 Iustin Pop
     the L{UploadFile} function
26 714ea7ca Iustin Pop
@var _ALLOWED_CLEAN_DIRS: denotes which directories are accepted
27 714ea7ca Iustin Pop
     in the L{_CleanDirectory} function
28 360b0dc2 Iustin Pop

29 360b0dc2 Iustin Pop
"""
30 a8083063 Iustin Pop
31 b459a848 Andrea Spadaccini
# pylint: disable=E1103
32 6c881c52 Iustin Pop
33 6c881c52 Iustin Pop
# E1103: %s %r has no %r member (but some types could not be
34 6c881c52 Iustin Pop
# inferred), because the _TryOSFromDisk returns either (True, os_obj)
35 6c881c52 Iustin Pop
# or (False, "string") which confuses pylint
36 6c881c52 Iustin Pop
37 a8083063 Iustin Pop
38 a8083063 Iustin Pop
import os
39 a8083063 Iustin Pop
import os.path
40 a8083063 Iustin Pop
import shutil
41 a8083063 Iustin Pop
import time
42 a8083063 Iustin Pop
import stat
43 a8083063 Iustin Pop
import errno
44 a8083063 Iustin Pop
import re
45 b544cfe0 Iustin Pop
import random
46 18682bca Iustin Pop
import logging
47 3b9e6a30 Iustin Pop
import tempfile
48 12bce260 Michael Hanselmann
import zlib
49 12bce260 Michael Hanselmann
import base64
50 f81c4737 Michael Hanselmann
import signal
51 a8083063 Iustin Pop
52 a8083063 Iustin Pop
from ganeti import errors
53 a8083063 Iustin Pop
from ganeti import utils
54 a8083063 Iustin Pop
from ganeti import ssh
55 a8083063 Iustin Pop
from ganeti import hypervisor
56 a8083063 Iustin Pop
from ganeti import constants
57 cde49218 Helga Velroyen
from ganeti.storage import bdev
58 cde49218 Helga Velroyen
from ganeti.storage import drbd
59 13669ecd Helga Velroyen
from ganeti.storage import filestorage
60 a8083063 Iustin Pop
from ganeti import objects
61 880478f8 Iustin Pop
from ganeti import ssconf
62 1651d116 Michael Hanselmann
from ganeti import serializer
63 a744b676 Manuel Franceschini
from ganeti import netutils
64 82b22e19 René Nussbaumer
from ganeti import runtime
65 3ccd3243 Andrea Spadaccini
from ganeti import compat
66 710f30ec Michael Hanselmann
from ganeti import pathutils
67 cffbbae7 Michael Hanselmann
from ganeti import vcluster
68 99e222b1 Michael Hanselmann
from ganeti import ht
69 cde49218 Helga Velroyen
from ganeti.storage.base import BlockDev
70 cde49218 Helga Velroyen
from ganeti.storage.drbd import DRBD8
71 68d95757 Guido Trotter
from ganeti import hooksmaster
72 a8083063 Iustin Pop
73 a8083063 Iustin Pop
74 13998ef2 Michael Hanselmann
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
75 b8028dcf Michael Hanselmann
_ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([
76 710f30ec Michael Hanselmann
  pathutils.DATA_DIR,
77 710f30ec Michael Hanselmann
  pathutils.JOB_QUEUE_ARCHIVE_DIR,
78 710f30ec Michael Hanselmann
  pathutils.QUEUE_DIR,
79 710f30ec Michael Hanselmann
  pathutils.CRYPTO_KEYS_DIR,
80 714ea7ca Iustin Pop
  ])
81 f942a838 Michael Hanselmann
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
82 f942a838 Michael Hanselmann
_X509_KEY_FILE = "key"
83 f942a838 Michael Hanselmann
_X509_CERT_FILE = "cert"
84 1651d116 Michael Hanselmann
_IES_STATUS_FILE = "status"
85 1651d116 Michael Hanselmann
_IES_PID_FILE = "pid"
86 1651d116 Michael Hanselmann
_IES_CA_FILE = "ca"
87 13998ef2 Michael Hanselmann
88 0b5303da Iustin Pop
#: Valid LVS output line regex
89 a1f38213 Iustin Pop
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
90 0b5303da Iustin Pop
91 702eff21 Andrea Spadaccini
# Actions for the master setup script
92 702eff21 Andrea Spadaccini
_MASTER_START = "start"
93 702eff21 Andrea Spadaccini
_MASTER_STOP = "stop"
94 702eff21 Andrea Spadaccini
95 45bc4635 Iustin Pop
#: Maximum file permissions for restricted command directory and executables
96 1a2eb2dc Michael Hanselmann
_RCMD_MAX_MODE = (stat.S_IRWXU |
97 1a2eb2dc Michael Hanselmann
                  stat.S_IRGRP | stat.S_IXGRP |
98 1a2eb2dc Michael Hanselmann
                  stat.S_IROTH | stat.S_IXOTH)
99 1a2eb2dc Michael Hanselmann
100 45bc4635 Iustin Pop
#: Delay before returning an error for restricted commands
101 1a2eb2dc Michael Hanselmann
_RCMD_INVALID_DELAY = 10
102 1a2eb2dc Michael Hanselmann
103 45bc4635 Iustin Pop
#: How long to wait to acquire lock for restricted commands (shorter than
104 1a2eb2dc Michael Hanselmann
#: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
105 1a2eb2dc Michael Hanselmann
#: command requests arrive
106 1a2eb2dc Michael Hanselmann
_RCMD_LOCK_TIMEOUT = _RCMD_INVALID_DELAY * 0.8
107 1a2eb2dc Michael Hanselmann
108 13998ef2 Michael Hanselmann
109 2cc6781a Iustin Pop
class RPCFail(Exception):
110 2cc6781a Iustin Pop
  """Class denoting RPC failure.
111 2cc6781a Iustin Pop

112 2cc6781a Iustin Pop
  Its argument is the error message.
113 2cc6781a Iustin Pop

114 2cc6781a Iustin Pop
  """
115 2cc6781a Iustin Pop
116 13998ef2 Michael Hanselmann
117 584ea340 Michele Tartara
def _GetInstReasonFilename(instance_name):
118 a59d5fa1 Michele Tartara
  """Path of the file containing the reason of the instance status change.
119 a59d5fa1 Michele Tartara

120 a59d5fa1 Michele Tartara
  @type instance_name: string
121 a59d5fa1 Michele Tartara
  @param instance_name: The name of the instance
122 a59d5fa1 Michele Tartara
  @rtype: string
123 a59d5fa1 Michele Tartara
  @return: The path of the file
124 a59d5fa1 Michele Tartara

125 a59d5fa1 Michele Tartara
  """
126 a59d5fa1 Michele Tartara
  return utils.PathJoin(pathutils.INSTANCE_REASON_DIR, instance_name)
127 a59d5fa1 Michele Tartara
128 a59d5fa1 Michele Tartara
129 584ea340 Michele Tartara
def _StoreInstReasonTrail(instance_name, trail):
130 584ea340 Michele Tartara
  """Serialize a reason trail related to an instance change of state to file.
131 584ea340 Michele Tartara

132 584ea340 Michele Tartara
  The exact location of the file depends on the name of the instance and on
133 584ea340 Michele Tartara
  the configuration of the Ganeti cluster defined at deploy time.
134 584ea340 Michele Tartara

135 584ea340 Michele Tartara
  @type instance_name: string
136 584ea340 Michele Tartara
  @param instance_name: The name of the instance
137 584ea340 Michele Tartara
  @rtype: None
138 584ea340 Michele Tartara

139 584ea340 Michele Tartara
  """
140 584ea340 Michele Tartara
  json = serializer.DumpJson(trail)
141 584ea340 Michele Tartara
  filename = _GetInstReasonFilename(instance_name)
142 584ea340 Michele Tartara
  utils.WriteFile(filename, data=json)
143 584ea340 Michele Tartara
144 584ea340 Michele Tartara
145 2cc6781a Iustin Pop
def _Fail(msg, *args, **kwargs):
146 2cc6781a Iustin Pop
  """Log an error and the raise an RPCFail exception.
147 2cc6781a Iustin Pop

148 2cc6781a Iustin Pop
  This exception is then handled specially in the ganeti daemon and
149 2cc6781a Iustin Pop
  turned into a 'failed' return type. As such, this function is a
150 2cc6781a Iustin Pop
  useful shortcut for logging the error and returning it to the master
151 2cc6781a Iustin Pop
  daemon.
152 2cc6781a Iustin Pop

153 2cc6781a Iustin Pop
  @type msg: string
154 2cc6781a Iustin Pop
  @param msg: the text of the exception
155 2cc6781a Iustin Pop
  @raise RPCFail
156 2cc6781a Iustin Pop

157 2cc6781a Iustin Pop
  """
158 2cc6781a Iustin Pop
  if args:
159 2cc6781a Iustin Pop
    msg = msg % args
160 afdc3985 Iustin Pop
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
161 afdc3985 Iustin Pop
    if "exc" in kwargs and kwargs["exc"]:
162 afdc3985 Iustin Pop
      logging.exception(msg)
163 afdc3985 Iustin Pop
    else:
164 afdc3985 Iustin Pop
      logging.error(msg)
165 2cc6781a Iustin Pop
  raise RPCFail(msg)
166 2cc6781a Iustin Pop
167 2cc6781a Iustin Pop
168 c657dcc9 Michael Hanselmann
def _GetConfig():
169 93384844 Iustin Pop
  """Simple wrapper to return a SimpleStore.
170 10c2650b Iustin Pop

171 93384844 Iustin Pop
  @rtype: L{ssconf.SimpleStore}
172 93384844 Iustin Pop
  @return: a SimpleStore instance
173 10c2650b Iustin Pop

174 10c2650b Iustin Pop
  """
175 93384844 Iustin Pop
  return ssconf.SimpleStore()
176 c657dcc9 Michael Hanselmann
177 c657dcc9 Michael Hanselmann
178 62c9ec92 Iustin Pop
def _GetSshRunner(cluster_name):
179 10c2650b Iustin Pop
  """Simple wrapper to return an SshRunner.
180 10c2650b Iustin Pop

181 10c2650b Iustin Pop
  @type cluster_name: str
182 10c2650b Iustin Pop
  @param cluster_name: the cluster name, which is needed
183 10c2650b Iustin Pop
      by the SshRunner constructor
184 10c2650b Iustin Pop
  @rtype: L{ssh.SshRunner}
185 10c2650b Iustin Pop
  @return: an SshRunner instance
186 10c2650b Iustin Pop

187 10c2650b Iustin Pop
  """
188 62c9ec92 Iustin Pop
  return ssh.SshRunner(cluster_name)
189 c92b310a Michael Hanselmann
190 c92b310a Michael Hanselmann
191 12bce260 Michael Hanselmann
def _Decompress(data):
192 12bce260 Michael Hanselmann
  """Unpacks data compressed by the RPC client.
193 12bce260 Michael Hanselmann

194 12bce260 Michael Hanselmann
  @type data: list or tuple
195 12bce260 Michael Hanselmann
  @param data: Data sent by RPC client
196 12bce260 Michael Hanselmann
  @rtype: str
197 12bce260 Michael Hanselmann
  @return: Decompressed data
198 12bce260 Michael Hanselmann

199 12bce260 Michael Hanselmann
  """
200 52e2f66e Michael Hanselmann
  assert isinstance(data, (list, tuple))
201 12bce260 Michael Hanselmann
  assert len(data) == 2
202 12bce260 Michael Hanselmann
  (encoding, content) = data
203 12bce260 Michael Hanselmann
  if encoding == constants.RPC_ENCODING_NONE:
204 12bce260 Michael Hanselmann
    return content
205 12bce260 Michael Hanselmann
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
206 12bce260 Michael Hanselmann
    return zlib.decompress(base64.b64decode(content))
207 12bce260 Michael Hanselmann
  else:
208 12bce260 Michael Hanselmann
    raise AssertionError("Unknown data encoding")
209 12bce260 Michael Hanselmann
210 12bce260 Michael Hanselmann
211 3bc6be5c Iustin Pop
def _CleanDirectory(path, exclude=None):
212 76ab5558 Michael Hanselmann
  """Removes all regular files in a directory.
213 76ab5558 Michael Hanselmann

214 10c2650b Iustin Pop
  @type path: str
215 10c2650b Iustin Pop
  @param path: the directory to clean
216 76ab5558 Michael Hanselmann
  @type exclude: list
217 10c2650b Iustin Pop
  @param exclude: list of files to be excluded, defaults
218 10c2650b Iustin Pop
      to the empty list
219 76ab5558 Michael Hanselmann

220 76ab5558 Michael Hanselmann
  """
221 714ea7ca Iustin Pop
  if path not in _ALLOWED_CLEAN_DIRS:
222 714ea7ca Iustin Pop
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
223 714ea7ca Iustin Pop
          path)
224 714ea7ca Iustin Pop
225 3956cee1 Michael Hanselmann
  if not os.path.isdir(path):
226 3956cee1 Michael Hanselmann
    return
227 3bc6be5c Iustin Pop
  if exclude is None:
228 3bc6be5c Iustin Pop
    exclude = []
229 3bc6be5c Iustin Pop
  else:
230 3bc6be5c Iustin Pop
    # Normalize excluded paths
231 3bc6be5c Iustin Pop
    exclude = [os.path.normpath(i) for i in exclude]
232 76ab5558 Michael Hanselmann
233 3956cee1 Michael Hanselmann
  for rel_name in utils.ListVisibleFiles(path):
234 c4feafe8 Iustin Pop
    full_name = utils.PathJoin(path, rel_name)
235 76ab5558 Michael Hanselmann
    if full_name in exclude:
236 76ab5558 Michael Hanselmann
      continue
237 3956cee1 Michael Hanselmann
    if os.path.isfile(full_name) and not os.path.islink(full_name):
238 3956cee1 Michael Hanselmann
      utils.RemoveFile(full_name)
239 3956cee1 Michael Hanselmann
240 3956cee1 Michael Hanselmann
241 360b0dc2 Iustin Pop
def _BuildUploadFileList():
242 360b0dc2 Iustin Pop
  """Build the list of allowed upload files.
243 360b0dc2 Iustin Pop

244 360b0dc2 Iustin Pop
  This is abstracted so that it's built only once at module import time.
245 360b0dc2 Iustin Pop

246 360b0dc2 Iustin Pop
  """
247 b397a7d2 Iustin Pop
  allowed_files = set([
248 710f30ec Michael Hanselmann
    pathutils.CLUSTER_CONF_FILE,
249 ee045466 Michael Hanselmann
    pathutils.ETC_HOSTS,
250 710f30ec Michael Hanselmann
    pathutils.SSH_KNOWN_HOSTS_FILE,
251 710f30ec Michael Hanselmann
    pathutils.VNC_PASSWORD_FILE,
252 710f30ec Michael Hanselmann
    pathutils.RAPI_CERT_FILE,
253 710f30ec Michael Hanselmann
    pathutils.SPICE_CERT_FILE,
254 710f30ec Michael Hanselmann
    pathutils.SPICE_CACERT_FILE,
255 710f30ec Michael Hanselmann
    pathutils.RAPI_USERS_FILE,
256 710f30ec Michael Hanselmann
    pathutils.CONFD_HMAC_KEY,
257 710f30ec Michael Hanselmann
    pathutils.CLUSTER_DOMAIN_SECRET_FILE,
258 b397a7d2 Iustin Pop
    ])
259 b397a7d2 Iustin Pop
260 b397a7d2 Iustin Pop
  for hv_name in constants.HYPER_TYPES:
261 e5a45a16 Iustin Pop
    hv_class = hypervisor.GetHypervisorClass(hv_name)
262 69ab2e12 Guido Trotter
    allowed_files.update(hv_class.GetAncillaryFiles()[0])
263 b397a7d2 Iustin Pop
264 3439fd6b Michael Hanselmann
  assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \
265 3439fd6b Michael Hanselmann
    "Allowed file storage paths should never be uploaded via RPC"
266 3439fd6b Michael Hanselmann
267 b397a7d2 Iustin Pop
  return frozenset(allowed_files)
268 360b0dc2 Iustin Pop
269 360b0dc2 Iustin Pop
270 360b0dc2 Iustin Pop
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
271 360b0dc2 Iustin Pop
272 360b0dc2 Iustin Pop
273 1bc59f76 Michael Hanselmann
def JobQueuePurge():
274 10c2650b Iustin Pop
  """Removes job queue files and archived jobs.
275 10c2650b Iustin Pop

276 c8457ce7 Iustin Pop
  @rtype: tuple
277 c8457ce7 Iustin Pop
  @return: True, None
278 24fc781f Michael Hanselmann

279 24fc781f Michael Hanselmann
  """
280 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE])
281 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.JOB_QUEUE_ARCHIVE_DIR)
282 24fc781f Michael Hanselmann
283 24fc781f Michael Hanselmann
284 bd1e4562 Iustin Pop
def GetMasterInfo():
285 bd1e4562 Iustin Pop
  """Returns master information.
286 bd1e4562 Iustin Pop

287 bd1e4562 Iustin Pop
  This is an utility function to compute master information, either
288 bd1e4562 Iustin Pop
  for consumption here or from the node daemon.
289 bd1e4562 Iustin Pop

290 bd1e4562 Iustin Pop
  @rtype: tuple
291 909b3a0e Andrea Spadaccini
  @return: master_netdev, master_ip, master_name, primary_ip_family,
292 909b3a0e Andrea Spadaccini
    master_netmask
293 2a52a064 Iustin Pop
  @raise RPCFail: in case of errors
294 b1b6ea87 Iustin Pop

295 b1b6ea87 Iustin Pop
  """
296 b1b6ea87 Iustin Pop
  try:
297 c657dcc9 Michael Hanselmann
    cfg = _GetConfig()
298 c657dcc9 Michael Hanselmann
    master_netdev = cfg.GetMasterNetdev()
299 c657dcc9 Michael Hanselmann
    master_ip = cfg.GetMasterIP()
300 5a8648eb Andrea Spadaccini
    master_netmask = cfg.GetMasterNetmask()
301 c657dcc9 Michael Hanselmann
    master_node = cfg.GetMasterNode()
302 d8e0caa6 Manuel Franceschini
    primary_ip_family = cfg.GetPrimaryIPFamily()
303 b1b6ea87 Iustin Pop
  except errors.ConfigurationError, err:
304 29921401 Iustin Pop
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
305 909b3a0e Andrea Spadaccini
  return (master_netdev, master_ip, master_node, primary_ip_family,
306 5ae4945a Iustin Pop
          master_netmask)
307 b1b6ea87 Iustin Pop
308 b1b6ea87 Iustin Pop
309 0fa481f5 Andrea Spadaccini
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
310 0fa481f5 Andrea Spadaccini
  """Decorator that runs hooks before and after the decorated function.
311 0fa481f5 Andrea Spadaccini

312 0fa481f5 Andrea Spadaccini
  @type hook_opcode: string
313 0fa481f5 Andrea Spadaccini
  @param hook_opcode: opcode of the hook
314 0fa481f5 Andrea Spadaccini
  @type hooks_path: string
315 0fa481f5 Andrea Spadaccini
  @param hooks_path: path of the hooks
316 0fa481f5 Andrea Spadaccini
  @type env_builder_fn: function
317 0fa481f5 Andrea Spadaccini
  @param env_builder_fn: function that returns a dictionary containing the
318 3ccd3243 Andrea Spadaccini
    environment variables for the hooks. Will get all the parameters of the
319 3ccd3243 Andrea Spadaccini
    decorated function.
320 0fa481f5 Andrea Spadaccini
  @raise RPCFail: in case of pre-hook failure
321 0fa481f5 Andrea Spadaccini

322 0fa481f5 Andrea Spadaccini
  """
323 0fa481f5 Andrea Spadaccini
  def decorator(fn):
324 0fa481f5 Andrea Spadaccini
    def wrapper(*args, **kwargs):
325 0fa481f5 Andrea Spadaccini
      _, myself = ssconf.GetMasterAndMyself()
326 0fa481f5 Andrea Spadaccini
      nodes = ([myself], [myself])  # these hooks run locally
327 0fa481f5 Andrea Spadaccini
328 3ccd3243 Andrea Spadaccini
      env_fn = compat.partial(env_builder_fn, *args, **kwargs)
329 3ccd3243 Andrea Spadaccini
330 0fa481f5 Andrea Spadaccini
      cfg = _GetConfig()
331 0fa481f5 Andrea Spadaccini
      hr = HooksRunner()
332 68d95757 Guido Trotter
      hm = hooksmaster.HooksMaster(hook_opcode, hooks_path, nodes,
333 68d95757 Guido Trotter
                                   hr.RunLocalHooks, None, env_fn,
334 68d95757 Guido Trotter
                                   logging.warning, cfg.GetClusterName(),
335 68d95757 Guido Trotter
                                   cfg.GetMasterNode())
336 0fa481f5 Andrea Spadaccini
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
337 0fa481f5 Andrea Spadaccini
      result = fn(*args, **kwargs)
338 0fa481f5 Andrea Spadaccini
      hm.RunPhase(constants.HOOKS_PHASE_POST)
339 0fa481f5 Andrea Spadaccini
340 0fa481f5 Andrea Spadaccini
      return result
341 0fa481f5 Andrea Spadaccini
    return wrapper
342 0fa481f5 Andrea Spadaccini
  return decorator
343 0fa481f5 Andrea Spadaccini
344 0fa481f5 Andrea Spadaccini
345 57c7bc57 Andrea Spadaccini
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
346 2d88fdd3 Andrea Spadaccini
  """Builds environment variables for master IP hooks.
347 2d88fdd3 Andrea Spadaccini

348 3ccd3243 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
349 3ccd3243 Andrea Spadaccini
  @param master_params: network parameters of the master
350 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
351 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
352 57c7bc57 Andrea Spadaccini
    address setup script (unused, but necessary per the implementation of the
353 57c7bc57 Andrea Spadaccini
    _RunLocalHooks decorator)
354 3ccd3243 Andrea Spadaccini

355 2d88fdd3 Andrea Spadaccini
  """
356 57c7bc57 Andrea Spadaccini
  # pylint: disable=W0613
357 3ccd3243 Andrea Spadaccini
  ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
358 2d88fdd3 Andrea Spadaccini
  env = {
359 3ccd3243 Andrea Spadaccini
    "MASTER_NETDEV": master_params.netdev,
360 3ccd3243 Andrea Spadaccini
    "MASTER_IP": master_params.ip,
361 702eff21 Andrea Spadaccini
    "MASTER_NETMASK": str(master_params.netmask),
362 3ccd3243 Andrea Spadaccini
    "CLUSTER_IP_VERSION": str(ver),
363 2d88fdd3 Andrea Spadaccini
  }
364 2d88fdd3 Andrea Spadaccini
365 2d88fdd3 Andrea Spadaccini
  return env
366 2d88fdd3 Andrea Spadaccini
367 2d88fdd3 Andrea Spadaccini
368 702eff21 Andrea Spadaccini
def _RunMasterSetupScript(master_params, action, use_external_mip_script):
369 702eff21 Andrea Spadaccini
  """Execute the master IP address setup script.
370 702eff21 Andrea Spadaccini

371 702eff21 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
372 702eff21 Andrea Spadaccini
  @param master_params: network parameters of the master
373 702eff21 Andrea Spadaccini
  @type action: string
374 702eff21 Andrea Spadaccini
  @param action: action to pass to the script. Must be one of
375 702eff21 Andrea Spadaccini
    L{backend._MASTER_START} or L{backend._MASTER_STOP}
376 702eff21 Andrea Spadaccini
  @type use_external_mip_script: boolean
377 702eff21 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
378 702eff21 Andrea Spadaccini
    address setup script
379 702eff21 Andrea Spadaccini
  @raise backend.RPCFail: if there are errors during the execution of the
380 702eff21 Andrea Spadaccini
    script
381 702eff21 Andrea Spadaccini

382 702eff21 Andrea Spadaccini
  """
383 702eff21 Andrea Spadaccini
  env = _BuildMasterIpEnv(master_params)
384 702eff21 Andrea Spadaccini
385 702eff21 Andrea Spadaccini
  if use_external_mip_script:
386 710f30ec Michael Hanselmann
    setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT
387 702eff21 Andrea Spadaccini
  else:
388 710f30ec Michael Hanselmann
    setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT
389 702eff21 Andrea Spadaccini
390 702eff21 Andrea Spadaccini
  result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
391 702eff21 Andrea Spadaccini
392 702eff21 Andrea Spadaccini
  if result.failed:
393 19e1b715 Iustin Pop
    _Fail("Failed to %s the master IP. Script return value: %s, output: '%s'" %
394 19e1b715 Iustin Pop
          (action, result.exit_code, result.output), log=True)
395 702eff21 Andrea Spadaccini
396 702eff21 Andrea Spadaccini
397 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
398 3a3e4f1e Andrea Spadaccini
               _BuildMasterIpEnv)
399 57c7bc57 Andrea Spadaccini
def ActivateMasterIp(master_params, use_external_mip_script):
400 fb460cf7 Andrea Spadaccini
  """Activate the IP address of the master daemon.
401 fb460cf7 Andrea Spadaccini

402 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
403 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
404 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
405 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
406 57c7bc57 Andrea Spadaccini
    address setup script
407 702eff21 Andrea Spadaccini
  @raise RPCFail: in case of errors during the IP startup
408 8da2bd43 Andrea Spadaccini

409 fb460cf7 Andrea Spadaccini
  """
410 702eff21 Andrea Spadaccini
  _RunMasterSetupScript(master_params, _MASTER_START,
411 702eff21 Andrea Spadaccini
                        use_external_mip_script)
412 fb460cf7 Andrea Spadaccini
413 fb460cf7 Andrea Spadaccini
414 fb460cf7 Andrea Spadaccini
def StartMasterDaemons(no_voting):
415 a8083063 Iustin Pop
  """Activate local node as master node.
416 a8083063 Iustin Pop

417 fb460cf7 Andrea Spadaccini
  The function will start the master daemons (ganeti-masterd and ganeti-rapi).
418 10c2650b Iustin Pop

419 3583908a Guido Trotter
  @type no_voting: boolean
420 3583908a Guido Trotter
  @param no_voting: whether to start ganeti-masterd without a node vote
421 fb460cf7 Andrea Spadaccini
      but still non-interactively
422 10c2650b Iustin Pop
  @rtype: None
423 a8083063 Iustin Pop

424 a8083063 Iustin Pop
  """
425 a8083063 Iustin Pop
426 fb460cf7 Andrea Spadaccini
  if no_voting:
427 fb460cf7 Andrea Spadaccini
    masterd_args = "--no-voting --yes-do-it"
428 fb460cf7 Andrea Spadaccini
  else:
429 fb460cf7 Andrea Spadaccini
    masterd_args = ""
430 f154a7a3 Michael Hanselmann
431 fb460cf7 Andrea Spadaccini
  env = {
432 fb460cf7 Andrea Spadaccini
    "EXTRA_MASTERD_ARGS": masterd_args,
433 fb460cf7 Andrea Spadaccini
    }
434 fb460cf7 Andrea Spadaccini
435 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
436 fb460cf7 Andrea Spadaccini
  if result.failed:
437 fb460cf7 Andrea Spadaccini
    msg = "Can't start Ganeti master: %s" % result.output
438 fb460cf7 Andrea Spadaccini
    logging.error(msg)
439 fb460cf7 Andrea Spadaccini
    _Fail(msg)
440 f154a7a3 Michael Hanselmann
441 fb460cf7 Andrea Spadaccini
442 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
443 3a3e4f1e Andrea Spadaccini
               _BuildMasterIpEnv)
444 57c7bc57 Andrea Spadaccini
def DeactivateMasterIp(master_params, use_external_mip_script):
445 fb460cf7 Andrea Spadaccini
  """Deactivate the master IP on this node.
446 a8083063 Iustin Pop

447 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
448 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
449 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
450 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
451 57c7bc57 Andrea Spadaccini
    address setup script
452 702eff21 Andrea Spadaccini
  @raise RPCFail: in case of errors during the IP turndown
453 96e0d5cc Andrea Spadaccini

454 a8083063 Iustin Pop
  """
455 702eff21 Andrea Spadaccini
  _RunMasterSetupScript(master_params, _MASTER_STOP,
456 702eff21 Andrea Spadaccini
                        use_external_mip_script)
457 b1b6ea87 Iustin Pop
458 fb460cf7 Andrea Spadaccini
459 fb460cf7 Andrea Spadaccini
def StopMasterDaemons():
460 fb460cf7 Andrea Spadaccini
  """Stop the master daemons on this node.
461 fb460cf7 Andrea Spadaccini

462 fb460cf7 Andrea Spadaccini
  Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
463 fb460cf7 Andrea Spadaccini

464 fb460cf7 Andrea Spadaccini
  @rtype: None
465 fb460cf7 Andrea Spadaccini

466 fb460cf7 Andrea Spadaccini
  """
467 fb460cf7 Andrea Spadaccini
  # TODO: log and report back to the caller the error failures; we
468 fb460cf7 Andrea Spadaccini
  # need to decide in which case we fail the RPC for this
469 fb460cf7 Andrea Spadaccini
470 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
471 fb460cf7 Andrea Spadaccini
  if result.failed:
472 fb460cf7 Andrea Spadaccini
    logging.error("Could not stop Ganeti master, command %s had exitcode %s"
473 fb460cf7 Andrea Spadaccini
                  " and error %s",
474 fb460cf7 Andrea Spadaccini
                  result.cmd, result.exit_code, result.output)
475 a8083063 Iustin Pop
476 a8083063 Iustin Pop
477 41e079ce Andrea Spadaccini
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
478 5a8648eb Andrea Spadaccini
  """Change the netmask of the master IP.
479 5a8648eb Andrea Spadaccini

480 41e079ce Andrea Spadaccini
  @param old_netmask: the old value of the netmask
481 41e079ce Andrea Spadaccini
  @param netmask: the new value of the netmask
482 41e079ce Andrea Spadaccini
  @param master_ip: the master IP
483 41e079ce Andrea Spadaccini
  @param master_netdev: the master network device
484 41e079ce Andrea Spadaccini

485 5a8648eb Andrea Spadaccini
  """
486 5a8648eb Andrea Spadaccini
  if old_netmask == netmask:
487 5a8648eb Andrea Spadaccini
    return
488 5a8648eb Andrea Spadaccini
489 9e6014b9 Andrea Spadaccini
  if not netutils.IPAddress.Own(master_ip):
490 9e6014b9 Andrea Spadaccini
    _Fail("The master IP address is not up, not attempting to change its"
491 9e6014b9 Andrea Spadaccini
          " netmask")
492 9e6014b9 Andrea Spadaccini
493 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
494 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, netmask),
495 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
496 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
497 5a8648eb Andrea Spadaccini
  if result.failed:
498 9e6014b9 Andrea Spadaccini
    _Fail("Could not set the new netmask on the master IP address")
499 5a8648eb Andrea Spadaccini
500 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
501 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, old_netmask),
502 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
503 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
504 5a8648eb Andrea Spadaccini
  if result.failed:
505 9e6014b9 Andrea Spadaccini
    _Fail("Could not bring down the master IP address with the old netmask")
506 5a8648eb Andrea Spadaccini
507 5a8648eb Andrea Spadaccini
508 19ddc57a René Nussbaumer
def EtcHostsModify(mode, host, ip):
509 19ddc57a René Nussbaumer
  """Modify a host entry in /etc/hosts.
510 19ddc57a René Nussbaumer

511 19ddc57a René Nussbaumer
  @param mode: The mode to operate. Either add or remove entry
512 19ddc57a René Nussbaumer
  @param host: The host to operate on
513 19ddc57a René Nussbaumer
  @param ip: The ip associated with the entry
514 19ddc57a René Nussbaumer

515 19ddc57a René Nussbaumer
  """
516 19ddc57a René Nussbaumer
  if mode == constants.ETC_HOSTS_ADD:
517 19ddc57a René Nussbaumer
    if not ip:
518 19ddc57a René Nussbaumer
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
519 19ddc57a René Nussbaumer
              " present")
520 19ddc57a René Nussbaumer
    utils.AddHostToEtcHosts(host, ip)
521 19ddc57a René Nussbaumer
  elif mode == constants.ETC_HOSTS_REMOVE:
522 19ddc57a René Nussbaumer
    if ip:
523 19ddc57a René Nussbaumer
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
524 19ddc57a René Nussbaumer
              " parameter is present")
525 19ddc57a René Nussbaumer
    utils.RemoveHostFromEtcHosts(host)
526 19ddc57a René Nussbaumer
  else:
527 19ddc57a René Nussbaumer
    RPCFail("Mode not supported")
528 19ddc57a René Nussbaumer
529 19ddc57a René Nussbaumer
530 b989b9d9 Ken Wehr
def LeaveCluster(modify_ssh_setup):
531 10c2650b Iustin Pop
  """Cleans up and remove the current node.
532 10c2650b Iustin Pop

533 10c2650b Iustin Pop
  This function cleans up and prepares the current node to be removed
534 10c2650b Iustin Pop
  from the cluster.
535 10c2650b Iustin Pop

536 10c2650b Iustin Pop
  If processing is successful, then it raises an
537 c41eea6e Iustin Pop
  L{errors.QuitGanetiException} which is used as a special case to
538 10c2650b Iustin Pop
  shutdown the node daemon.
539 a8083063 Iustin Pop

540 b989b9d9 Ken Wehr
  @param modify_ssh_setup: boolean
541 b989b9d9 Ken Wehr

542 a8083063 Iustin Pop
  """
543 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.DATA_DIR)
544 710f30ec Michael Hanselmann
  _CleanDirectory(pathutils.CRYPTO_KEYS_DIR)
545 1bc59f76 Michael Hanselmann
  JobQueuePurge()
546 f78346f5 Michael Hanselmann
547 b989b9d9 Ken Wehr
  if modify_ssh_setup:
548 b989b9d9 Ken Wehr
    try:
549 052783ff Michael Hanselmann
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
550 7900ed01 Iustin Pop
551 b989b9d9 Ken Wehr
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
552 a8083063 Iustin Pop
553 b989b9d9 Ken Wehr
      utils.RemoveFile(priv_key)
554 b989b9d9 Ken Wehr
      utils.RemoveFile(pub_key)
555 b989b9d9 Ken Wehr
    except errors.OpExecError:
556 b989b9d9 Ken Wehr
      logging.exception("Error while processing ssh files")
557 a8083063 Iustin Pop
558 ed008420 Guido Trotter
  try:
559 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.CONFD_HMAC_KEY)
560 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.RAPI_CERT_FILE)
561 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.SPICE_CERT_FILE)
562 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.SPICE_CACERT_FILE)
563 710f30ec Michael Hanselmann
    utils.RemoveFile(pathutils.NODED_CERT_FILE)
564 b459a848 Andrea Spadaccini
  except: # pylint: disable=W0702
565 ed008420 Guido Trotter
    logging.exception("Error while removing cluster secrets")
566 ed008420 Guido Trotter
567 710f30ec Michael Hanselmann
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop", constants.CONFD])
568 f154a7a3 Michael Hanselmann
  if result.failed:
569 f154a7a3 Michael Hanselmann
    logging.error("Command %s failed with exitcode %s and error %s",
570 f154a7a3 Michael Hanselmann
                  result.cmd, result.exit_code, result.output)
571 ed008420 Guido Trotter
572 0623d351 Iustin Pop
  # Raise a custom exception (handled in ganeti-noded)
573 d0c8c01d Iustin Pop
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
574 6d8b6238 Guido Trotter
575 a8083063 Iustin Pop
576 1a3c5d4e Bernardo Dal Seno
def _GetVgInfo(name, excl_stor):
577 78519c10 Michael Hanselmann
  """Retrieves information about a LVM volume group.
578 78519c10 Michael Hanselmann

579 78519c10 Michael Hanselmann
  """
580 78519c10 Michael Hanselmann
  # TODO: GetVGInfo supports returning information for multiple VGs at once
581 1a3c5d4e Bernardo Dal Seno
  vginfo = bdev.LogicalVolume.GetVGInfo([name], excl_stor)
582 78519c10 Michael Hanselmann
  if vginfo:
583 78519c10 Michael Hanselmann
    vg_free = int(round(vginfo[0][0], 0))
584 78519c10 Michael Hanselmann
    vg_size = int(round(vginfo[0][1], 0))
585 78519c10 Michael Hanselmann
  else:
586 78519c10 Michael Hanselmann
    vg_free = None
587 78519c10 Michael Hanselmann
    vg_size = None
588 78519c10 Michael Hanselmann
589 78519c10 Michael Hanselmann
  return {
590 0f0f6d7d Helga Velroyen
    "type": constants.ST_LVM_VG,
591 78519c10 Michael Hanselmann
    "name": name,
592 32389d91 Helga Velroyen
    "storage_free": vg_free,
593 32389d91 Helga Velroyen
    "storage_size": vg_size,
594 78519c10 Michael Hanselmann
    }
595 78519c10 Michael Hanselmann
596 78519c10 Michael Hanselmann
597 a1860404 Bernardo Dal Seno
def _GetVgSpindlesInfo(name, excl_stor):
598 a1860404 Bernardo Dal Seno
  """Retrieves information about spindles in an LVM volume group.
599 a1860404 Bernardo Dal Seno

600 a1860404 Bernardo Dal Seno
  @type name: string
601 a1860404 Bernardo Dal Seno
  @param name: VG name
602 a1860404 Bernardo Dal Seno
  @type excl_stor: bool
603 a1860404 Bernardo Dal Seno
  @param excl_stor: exclusive storage
604 a1860404 Bernardo Dal Seno
  @rtype: dict
605 a1860404 Bernardo Dal Seno
  @return: dictionary whose keys are "name", "vg_free", "vg_size" for VG name,
606 a1860404 Bernardo Dal Seno
      free spindles, total spindles respectively
607 a1860404 Bernardo Dal Seno

608 a1860404 Bernardo Dal Seno
  """
609 a1860404 Bernardo Dal Seno
  if excl_stor:
610 a1860404 Bernardo Dal Seno
    (vg_free, vg_size) = bdev.LogicalVolume.GetVgSpindlesInfo(name)
611 a1860404 Bernardo Dal Seno
  else:
612 a1860404 Bernardo Dal Seno
    vg_free = 0
613 a1860404 Bernardo Dal Seno
    vg_size = 0
614 a1860404 Bernardo Dal Seno
  return {
615 0f0f6d7d Helga Velroyen
    "type": constants.ST_LVM_PV,
616 a1860404 Bernardo Dal Seno
    "name": name,
617 32389d91 Helga Velroyen
    "storage_free": vg_free,
618 32389d91 Helga Velroyen
    "storage_size": vg_size,
619 a1860404 Bernardo Dal Seno
    }
620 a1860404 Bernardo Dal Seno
621 a1860404 Bernardo Dal Seno
622 439e1d3f Helga Velroyen
def _GetHvInfo(name, hvparams, get_hv_fn=hypervisor.GetHypervisor):
623 78519c10 Michael Hanselmann
  """Retrieves node information from a hypervisor.
624 78519c10 Michael Hanselmann

625 78519c10 Michael Hanselmann
  The information returned depends on the hypervisor. Common items:
626 78519c10 Michael Hanselmann

627 78519c10 Michael Hanselmann
    - vg_size is the size of the configured volume group in MiB
628 78519c10 Michael Hanselmann
    - vg_free is the free size of the volume group in MiB
629 78519c10 Michael Hanselmann
    - memory_dom0 is the memory allocated for domain0 in MiB
630 78519c10 Michael Hanselmann
    - memory_free is the currently available (free) ram in MiB
631 78519c10 Michael Hanselmann
    - memory_total is the total number of ram in MiB
632 78519c10 Michael Hanselmann
    - hv_version: the hypervisor version, if available
633 78519c10 Michael Hanselmann

634 439e1d3f Helga Velroyen
  @type hvparams: dict of string
635 439e1d3f Helga Velroyen
  @param hvparams: the hypervisor's hvparams
636 439e1d3f Helga Velroyen

637 78519c10 Michael Hanselmann
  """
638 439e1d3f Helga Velroyen
  return get_hv_fn(name).GetNodeInfo(hvparams=hvparams)
639 439e1d3f Helga Velroyen
640 439e1d3f Helga Velroyen
641 439e1d3f Helga Velroyen
def _GetHvInfoAll(hv_specs, get_hv_fn=hypervisor.GetHypervisor):
642 439e1d3f Helga Velroyen
  """Retrieves node information for all hypervisors.
643 439e1d3f Helga Velroyen

644 439e1d3f Helga Velroyen
  See C{_GetHvInfo} for information on the output.
645 439e1d3f Helga Velroyen

646 439e1d3f Helga Velroyen
  @type hv_specs: list of pairs (string, dict of strings)
647 439e1d3f Helga Velroyen
  @param hv_specs: list of pairs of a hypervisor's name and its hvparams
648 439e1d3f Helga Velroyen

649 439e1d3f Helga Velroyen
  """
650 439e1d3f Helga Velroyen
  if hv_specs is None:
651 439e1d3f Helga Velroyen
    return None
652 439e1d3f Helga Velroyen
653 439e1d3f Helga Velroyen
  result = []
654 439e1d3f Helga Velroyen
  for hvname, hvparams in hv_specs:
655 439e1d3f Helga Velroyen
    result.append(_GetHvInfo(hvname, hvparams, get_hv_fn))
656 439e1d3f Helga Velroyen
  return result
657 78519c10 Michael Hanselmann
658 78519c10 Michael Hanselmann
659 78519c10 Michael Hanselmann
def _GetNamedNodeInfo(names, fn):
660 78519c10 Michael Hanselmann
  """Calls C{fn} for all names in C{names} and returns a dictionary.
661 78519c10 Michael Hanselmann

662 78519c10 Michael Hanselmann
  @rtype: None or dict
663 78519c10 Michael Hanselmann

664 78519c10 Michael Hanselmann
  """
665 78519c10 Michael Hanselmann
  if names is None:
666 78519c10 Michael Hanselmann
    return None
667 78519c10 Michael Hanselmann
  else:
668 ff3be305 Michael Hanselmann
    return map(fn, names)
669 78519c10 Michael Hanselmann
670 78519c10 Michael Hanselmann
671 439e1d3f Helga Velroyen
def GetNodeInfo(storage_units, hv_specs, excl_stor):
672 5bbd3f7f Michael Hanselmann
  """Gives back a hash with different information about the node.
673 a8083063 Iustin Pop

674 4b92e992 Helga Velroyen
  @type storage_units: list of pairs (string, string)
675 4b92e992 Helga Velroyen
  @param storage_units: List of pairs (storage unit, identifier) to ask for disk
676 4b92e992 Helga Velroyen
                        space information. In case of lvm-vg, the identifier is
677 4b92e992 Helga Velroyen
                        the VG name.
678 439e1d3f Helga Velroyen
  @type hv_specs: list of pairs (string, dict of strings)
679 439e1d3f Helga Velroyen
  @param hv_specs: list of pairs of a hypervisor's name and its hvparams
680 1a3c5d4e Bernardo Dal Seno
  @type excl_stor: boolean
681 1a3c5d4e Bernardo Dal Seno
  @param excl_stor: Whether exclusive_storage is active
682 78519c10 Michael Hanselmann
  @rtype: tuple; (string, None/dict, None/dict)
683 78519c10 Michael Hanselmann
  @return: Tuple containing boot ID, volume group information and hypervisor
684 78519c10 Michael Hanselmann
    information
685 a8083063 Iustin Pop

686 098c0958 Michael Hanselmann
  """
687 78519c10 Michael Hanselmann
  bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
688 4b92e992 Helga Velroyen
  storage_info = _GetNamedNodeInfo(
689 4b92e992 Helga Velroyen
    storage_units,
690 4b92e992 Helga Velroyen
    (lambda storage_unit: _ApplyStorageInfoFunction(storage_unit[0],
691 4b92e992 Helga Velroyen
                                                    storage_unit[1],
692 4b92e992 Helga Velroyen
                                                    excl_stor)))
693 439e1d3f Helga Velroyen
  hv_info = _GetHvInfoAll(hv_specs)
694 4b92e992 Helga Velroyen
  return (bootid, storage_info, hv_info)
695 4b92e992 Helga Velroyen
696 4b92e992 Helga Velroyen
697 13669ecd Helga Velroyen
# pylint: disable=W0613
698 13669ecd Helga Velroyen
def _GetFileStorageSpaceInfo(path, *args):
699 13669ecd Helga Velroyen
  """Wrapper around filestorage.GetSpaceInfo.
700 13669ecd Helga Velroyen

701 13669ecd Helga Velroyen
  The purpose of this wrapper is to call filestorage.GetFileStorageSpaceInfo
702 13669ecd Helga Velroyen
  and ignore the *args parameter to not leak it into the filestorage
703 13669ecd Helga Velroyen
  module's code.
704 13669ecd Helga Velroyen

705 13669ecd Helga Velroyen
  @see: C{filestorage.GetFileStorageSpaceInfo} for description of the
706 13669ecd Helga Velroyen
    parameters.
707 13669ecd Helga Velroyen

708 13669ecd Helga Velroyen
  """
709 13669ecd Helga Velroyen
  return filestorage.GetFileStorageSpaceInfo(path)
710 13669ecd Helga Velroyen
711 13669ecd Helga Velroyen
712 4b92e992 Helga Velroyen
# FIXME: implement storage reporting for all missing storage types.
713 4b92e992 Helga Velroyen
_STORAGE_TYPE_INFO_FN = {
714 4b92e992 Helga Velroyen
  constants.ST_BLOCK: None,
715 4b92e992 Helga Velroyen
  constants.ST_DISKLESS: None,
716 4b92e992 Helga Velroyen
  constants.ST_EXT: None,
717 13669ecd Helga Velroyen
  constants.ST_FILE: _GetFileStorageSpaceInfo,
718 a1860404 Bernardo Dal Seno
  constants.ST_LVM_PV: _GetVgSpindlesInfo,
719 4b92e992 Helga Velroyen
  constants.ST_LVM_VG: _GetVgInfo,
720 4b92e992 Helga Velroyen
  constants.ST_RADOS: None,
721 4b92e992 Helga Velroyen
}
722 4b92e992 Helga Velroyen
723 4b92e992 Helga Velroyen
724 4b92e992 Helga Velroyen
def _ApplyStorageInfoFunction(storage_type, storage_key, *args):
725 4b92e992 Helga Velroyen
  """Looks up and applies the correct function to calculate free and total
726 4b92e992 Helga Velroyen
  storage for the given storage type.
727 4b92e992 Helga Velroyen

728 4b92e992 Helga Velroyen
  @type storage_type: string
729 4b92e992 Helga Velroyen
  @param storage_type: the storage type for which the storage shall be reported.
730 4b92e992 Helga Velroyen
  @type storage_key: string
731 4b92e992 Helga Velroyen
  @param storage_key: identifier of a storage unit, e.g. the volume group name
732 4b92e992 Helga Velroyen
    of an LVM storage unit
733 4b92e992 Helga Velroyen
  @type args: any
734 4b92e992 Helga Velroyen
  @param args: various parameters that can be used for storage reporting. These
735 4b92e992 Helga Velroyen
    parameters and their semantics vary from storage type to storage type and
736 4b92e992 Helga Velroyen
    are just propagated in this function.
737 4b92e992 Helga Velroyen
  @return: the results of the application of the storage space function (see
738 4b92e992 Helga Velroyen
    _STORAGE_TYPE_INFO_FN) if storage space reporting is implemented for that
739 4b92e992 Helga Velroyen
    storage type
740 4b92e992 Helga Velroyen
  @raises NotImplementedError: for storage types who don't support space
741 4b92e992 Helga Velroyen
    reporting yet
742 4b92e992 Helga Velroyen
  """
743 4b92e992 Helga Velroyen
  fn = _STORAGE_TYPE_INFO_FN[storage_type]
744 4b92e992 Helga Velroyen
  if fn is not None:
745 4b92e992 Helga Velroyen
    return fn(storage_key, *args)
746 4b92e992 Helga Velroyen
  else:
747 4b92e992 Helga Velroyen
    raise NotImplementedError
748 a8083063 Iustin Pop
749 a8083063 Iustin Pop
750 d5a690cb Bernardo Dal Seno
def _CheckExclusivePvs(pvi_list):
751 d5a690cb Bernardo Dal Seno
  """Check that PVs are not shared among LVs
752 d5a690cb Bernardo Dal Seno

753 d5a690cb Bernardo Dal Seno
  @type pvi_list: list of L{objects.LvmPvInfo} objects
754 d5a690cb Bernardo Dal Seno
  @param pvi_list: information about the PVs
755 d5a690cb Bernardo Dal Seno

756 d5a690cb Bernardo Dal Seno
  @rtype: list of tuples (string, list of strings)
757 d5a690cb Bernardo Dal Seno
  @return: offending volumes, as tuples: (pv_name, [lv1_name, lv2_name...])
758 d5a690cb Bernardo Dal Seno

759 d5a690cb Bernardo Dal Seno
  """
760 d5a690cb Bernardo Dal Seno
  res = []
761 d5a690cb Bernardo Dal Seno
  for pvi in pvi_list:
762 d5a690cb Bernardo Dal Seno
    if len(pvi.lv_list) > 1:
763 d5a690cb Bernardo Dal Seno
      res.append((pvi.name, pvi.lv_list))
764 d5a690cb Bernardo Dal Seno
  return res
765 d5a690cb Bernardo Dal Seno
766 d5a690cb Bernardo Dal Seno
767 75bf3149 Helga Velroyen
def _VerifyHypervisors(what, vm_capable, result, all_hvparams,
768 75bf3149 Helga Velroyen
                       get_hv_fn=hypervisor.GetHypervisor):
769 75bf3149 Helga Velroyen
  """Verifies the hypervisor. Appends the results to the 'results' list.
770 75bf3149 Helga Velroyen

771 75bf3149 Helga Velroyen
  @type what: C{dict}
772 75bf3149 Helga Velroyen
  @param what: a dictionary of things to check
773 75bf3149 Helga Velroyen
  @type vm_capable: boolean
774 5b0dfcef Helga Velroyen
  @param vm_capable: whether or not this node is vm capable
775 75bf3149 Helga Velroyen
  @type result: dict
776 75bf3149 Helga Velroyen
  @param result: dictionary of verification results; results of the
777 75bf3149 Helga Velroyen
    verifications in this function will be added here
778 75bf3149 Helga Velroyen
  @type all_hvparams: dict of dict of string
779 75bf3149 Helga Velroyen
  @param all_hvparams: dictionary mapping hypervisor names to hvparams
780 75bf3149 Helga Velroyen
  @type get_hv_fn: function
781 75bf3149 Helga Velroyen
  @param get_hv_fn: function to retrieve the hypervisor, to improve testability
782 75bf3149 Helga Velroyen

783 75bf3149 Helga Velroyen
  """
784 75bf3149 Helga Velroyen
  if not vm_capable:
785 75bf3149 Helga Velroyen
    return
786 75bf3149 Helga Velroyen
787 75bf3149 Helga Velroyen
  if constants.NV_HYPERVISOR in what:
788 75bf3149 Helga Velroyen
    result[constants.NV_HYPERVISOR] = {}
789 75bf3149 Helga Velroyen
    for hv_name in what[constants.NV_HYPERVISOR]:
790 75bf3149 Helga Velroyen
      hvparams = all_hvparams[hv_name]
791 75bf3149 Helga Velroyen
      try:
792 75bf3149 Helga Velroyen
        val = get_hv_fn(hv_name).Verify(hvparams=hvparams)
793 75bf3149 Helga Velroyen
      except errors.HypervisorError, err:
794 75bf3149 Helga Velroyen
        val = "Error while checking hypervisor: %s" % str(err)
795 75bf3149 Helga Velroyen
      result[constants.NV_HYPERVISOR][hv_name] = val
796 75bf3149 Helga Velroyen
797 75bf3149 Helga Velroyen
798 75bf3149 Helga Velroyen
def _VerifyHvparams(what, vm_capable, result,
799 75bf3149 Helga Velroyen
                    get_hv_fn=hypervisor.GetHypervisor):
800 75bf3149 Helga Velroyen
  """Verifies the hvparams. Appends the results to the 'results' list.
801 75bf3149 Helga Velroyen

802 75bf3149 Helga Velroyen
  @type what: C{dict}
803 75bf3149 Helga Velroyen
  @param what: a dictionary of things to check
804 75bf3149 Helga Velroyen
  @type vm_capable: boolean
805 5b0dfcef Helga Velroyen
  @param vm_capable: whether or not this node is vm capable
806 75bf3149 Helga Velroyen
  @type result: dict
807 75bf3149 Helga Velroyen
  @param result: dictionary of verification results; results of the
808 75bf3149 Helga Velroyen
    verifications in this function will be added here
809 75bf3149 Helga Velroyen
  @type get_hv_fn: function
810 75bf3149 Helga Velroyen
  @param get_hv_fn: function to retrieve the hypervisor, to improve testability
811 75bf3149 Helga Velroyen

812 75bf3149 Helga Velroyen
  """
813 75bf3149 Helga Velroyen
  if not vm_capable:
814 75bf3149 Helga Velroyen
    return
815 75bf3149 Helga Velroyen
816 75bf3149 Helga Velroyen
  if constants.NV_HVPARAMS in what:
817 75bf3149 Helga Velroyen
    result[constants.NV_HVPARAMS] = []
818 75bf3149 Helga Velroyen
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
819 75bf3149 Helga Velroyen
      try:
820 75bf3149 Helga Velroyen
        logging.info("Validating hv %s, %s", hv_name, hvparms)
821 75bf3149 Helga Velroyen
        get_hv_fn(hv_name).ValidateParameters(hvparms)
822 75bf3149 Helga Velroyen
      except errors.HypervisorError, err:
823 75bf3149 Helga Velroyen
        result[constants.NV_HVPARAMS].append((source, hv_name, str(err)))
824 75bf3149 Helga Velroyen
825 75bf3149 Helga Velroyen
826 5b0dfcef Helga Velroyen
def _VerifyInstanceList(what, vm_capable, result, all_hvparams):
827 5b0dfcef Helga Velroyen
  """Verifies the instance list.
828 5b0dfcef Helga Velroyen

829 5b0dfcef Helga Velroyen
  @type what: C{dict}
830 5b0dfcef Helga Velroyen
  @param what: a dictionary of things to check
831 5b0dfcef Helga Velroyen
  @type vm_capable: boolean
832 5b0dfcef Helga Velroyen
  @param vm_capable: whether or not this node is vm capable
833 5b0dfcef Helga Velroyen
  @type result: dict
834 5b0dfcef Helga Velroyen
  @param result: dictionary of verification results; results of the
835 5b0dfcef Helga Velroyen
    verifications in this function will be added here
836 5b0dfcef Helga Velroyen
  @type all_hvparams: dict of dict of string
837 5b0dfcef Helga Velroyen
  @param all_hvparams: dictionary mapping hypervisor names to hvparams
838 5b0dfcef Helga Velroyen

839 5b0dfcef Helga Velroyen
  """
840 5b0dfcef Helga Velroyen
  if constants.NV_INSTANCELIST in what and vm_capable:
841 5b0dfcef Helga Velroyen
    # GetInstanceList can fail
842 5b0dfcef Helga Velroyen
    try:
843 5b0dfcef Helga Velroyen
      val = GetInstanceList(what[constants.NV_INSTANCELIST],
844 5b0dfcef Helga Velroyen
                            all_hvparams=all_hvparams)
845 5b0dfcef Helga Velroyen
    except RPCFail, err:
846 5b0dfcef Helga Velroyen
      val = str(err)
847 5b0dfcef Helga Velroyen
    result[constants.NV_INSTANCELIST] = val
848 5b0dfcef Helga Velroyen
849 5b0dfcef Helga Velroyen
850 5b0dfcef Helga Velroyen
def _VerifyNodeInfo(what, vm_capable, result, all_hvparams):
851 5b0dfcef Helga Velroyen
  """Verifies the node info.
852 5b0dfcef Helga Velroyen

853 5b0dfcef Helga Velroyen
  @type what: C{dict}
854 5b0dfcef Helga Velroyen
  @param what: a dictionary of things to check
855 5b0dfcef Helga Velroyen
  @type vm_capable: boolean
856 5b0dfcef Helga Velroyen
  @param vm_capable: whether or not this node is vm capable
857 5b0dfcef Helga Velroyen
  @type result: dict
858 5b0dfcef Helga Velroyen
  @param result: dictionary of verification results; results of the
859 5b0dfcef Helga Velroyen
    verifications in this function will be added here
860 5b0dfcef Helga Velroyen
  @type all_hvparams: dict of dict of string
861 5b0dfcef Helga Velroyen
  @param all_hvparams: dictionary mapping hypervisor names to hvparams
862 5b0dfcef Helga Velroyen

863 5b0dfcef Helga Velroyen
  """
864 5b0dfcef Helga Velroyen
  if constants.NV_HVINFO in what and vm_capable:
865 5b0dfcef Helga Velroyen
    hvname = what[constants.NV_HVINFO]
866 5b0dfcef Helga Velroyen
    hyper = hypervisor.GetHypervisor(hvname)
867 5b0dfcef Helga Velroyen
    hvparams = all_hvparams[hvname]
868 5b0dfcef Helga Velroyen
    result[constants.NV_HVINFO] = hyper.GetNodeInfo(hvparams=hvparams)
869 5b0dfcef Helga Velroyen
870 5b0dfcef Helga Velroyen
871 5b0dfcef Helga Velroyen
def VerifyNode(what, cluster_name, all_hvparams):
872 a8083063 Iustin Pop
  """Verify the status of the local node.
873 a8083063 Iustin Pop

874 e69d05fd Iustin Pop
  Based on the input L{what} parameter, various checks are done on the
875 e69d05fd Iustin Pop
  local node.
876 e69d05fd Iustin Pop

877 e69d05fd Iustin Pop
  If the I{filelist} key is present, this list of
878 e69d05fd Iustin Pop
  files is checksummed and the file/checksum pairs are returned.
879 e69d05fd Iustin Pop

880 e69d05fd Iustin Pop
  If the I{nodelist} key is present, we check that we have
881 e69d05fd Iustin Pop
  connectivity via ssh with the target nodes (and check the hostname
882 e69d05fd Iustin Pop
  report).
883 a8083063 Iustin Pop

884 e69d05fd Iustin Pop
  If the I{node-net-test} key is present, we check that we have
885 e69d05fd Iustin Pop
  connectivity to the given nodes via both primary IP and, if
886 e69d05fd Iustin Pop
  applicable, secondary IPs.
887 e69d05fd Iustin Pop

888 e69d05fd Iustin Pop
  @type what: C{dict}
889 e69d05fd Iustin Pop
  @param what: a dictionary of things to check:
890 e69d05fd Iustin Pop
      - filelist: list of files for which to compute checksums
891 e69d05fd Iustin Pop
      - nodelist: list of nodes we should check ssh communication with
892 e69d05fd Iustin Pop
      - node-net-test: list of nodes we should check node daemon port
893 e69d05fd Iustin Pop
        connectivity with
894 e69d05fd Iustin Pop
      - hypervisor: list with hypervisors to run the verify for
895 5b0dfcef Helga Velroyen
  @type cluster_name: string
896 5b0dfcef Helga Velroyen
  @param cluster_name: the cluster's name
897 5b0dfcef Helga Velroyen
  @type all_hvparams: dict of dict of strings
898 5b0dfcef Helga Velroyen
  @param all_hvparams: a dictionary mapping hypervisor names to hvparams
899 10c2650b Iustin Pop
  @rtype: dict
900 10c2650b Iustin Pop
  @return: a dictionary with the same keys as the input dict, and
901 10c2650b Iustin Pop
      values representing the result of the checks
902 a8083063 Iustin Pop

903 a8083063 Iustin Pop
  """
904 a8083063 Iustin Pop
  result = {}
905 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
906 a744b676 Manuel Franceschini
  port = netutils.GetDaemonPort(constants.NODED)
907 8964ee14 Iustin Pop
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
908 a8083063 Iustin Pop
909 75bf3149 Helga Velroyen
  _VerifyHypervisors(what, vm_capable, result, all_hvparams)
910 75bf3149 Helga Velroyen
  _VerifyHvparams(what, vm_capable, result)
911 58a59652 Iustin Pop
912 25361b9a Iustin Pop
  if constants.NV_FILELIST in what:
913 47130d50 Michael Hanselmann
    fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
914 47130d50 Michael Hanselmann
                                              what[constants.NV_FILELIST]))
915 47130d50 Michael Hanselmann
    result[constants.NV_FILELIST] = \
916 47130d50 Michael Hanselmann
      dict((vcluster.MakeVirtualPath(key), value)
917 47130d50 Michael Hanselmann
           for (key, value) in fingerprints.items())
918 25361b9a Iustin Pop
919 25361b9a Iustin Pop
  if constants.NV_NODELIST in what:
920 64c7b383 Michael Hanselmann
    (nodes, bynode) = what[constants.NV_NODELIST]
921 64c7b383 Michael Hanselmann
922 64c7b383 Michael Hanselmann
    # Add nodes from other groups (different for each node)
923 64c7b383 Michael Hanselmann
    try:
924 64c7b383 Michael Hanselmann
      nodes.extend(bynode[my_name])
925 64c7b383 Michael Hanselmann
    except KeyError:
926 64c7b383 Michael Hanselmann
      pass
927 64c7b383 Michael Hanselmann
928 64c7b383 Michael Hanselmann
    # Use a random order
929 64c7b383 Michael Hanselmann
    random.shuffle(nodes)
930 64c7b383 Michael Hanselmann
931 64c7b383 Michael Hanselmann
    # Try to contact all nodes
932 64c7b383 Michael Hanselmann
    val = {}
933 64c7b383 Michael Hanselmann
    for node in nodes:
934 62c9ec92 Iustin Pop
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
935 a8083063 Iustin Pop
      if not success:
936 64c7b383 Michael Hanselmann
        val[node] = message
937 64c7b383 Michael Hanselmann
938 64c7b383 Michael Hanselmann
    result[constants.NV_NODELIST] = val
939 25361b9a Iustin Pop
940 25361b9a Iustin Pop
  if constants.NV_NODENETTEST in what:
941 25361b9a Iustin Pop
    result[constants.NV_NODENETTEST] = tmp = {}
942 9d4bfc96 Iustin Pop
    my_pip = my_sip = None
943 25361b9a Iustin Pop
    for name, pip, sip in what[constants.NV_NODENETTEST]:
944 9d4bfc96 Iustin Pop
      if name == my_name:
945 9d4bfc96 Iustin Pop
        my_pip = pip
946 9d4bfc96 Iustin Pop
        my_sip = sip
947 9d4bfc96 Iustin Pop
        break
948 9d4bfc96 Iustin Pop
    if not my_pip:
949 25361b9a Iustin Pop
      tmp[my_name] = ("Can't find my own primary/secondary IP"
950 25361b9a Iustin Pop
                      " in the node list")
951 9d4bfc96 Iustin Pop
    else:
952 25361b9a Iustin Pop
      for name, pip, sip in what[constants.NV_NODENETTEST]:
953 9d4bfc96 Iustin Pop
        fail = []
954 a744b676 Manuel Franceschini
        if not netutils.TcpPing(pip, port, source=my_pip):
955 9d4bfc96 Iustin Pop
          fail.append("primary")
956 9d4bfc96 Iustin Pop
        if sip != pip:
957 a744b676 Manuel Franceschini
          if not netutils.TcpPing(sip, port, source=my_sip):
958 9d4bfc96 Iustin Pop
            fail.append("secondary")
959 9d4bfc96 Iustin Pop
        if fail:
960 25361b9a Iustin Pop
          tmp[name] = ("failure using the %s interface(s)" %
961 25361b9a Iustin Pop
                       " and ".join(fail))
962 25361b9a Iustin Pop
963 a3a5f850 Iustin Pop
  if constants.NV_MASTERIP in what:
964 a3a5f850 Iustin Pop
    # FIXME: add checks on incoming data structures (here and in the
965 a3a5f850 Iustin Pop
    # rest of the function)
966 a3a5f850 Iustin Pop
    master_name, master_ip = what[constants.NV_MASTERIP]
967 a3a5f850 Iustin Pop
    if master_name == my_name:
968 9769bb78 Manuel Franceschini
      source = constants.IP4_ADDRESS_LOCALHOST
969 a3a5f850 Iustin Pop
    else:
970 a3a5f850 Iustin Pop
      source = None
971 a744b676 Manuel Franceschini
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
972 5ae4945a Iustin Pop
                                                     source=source)
973 a3a5f850 Iustin Pop
974 17b0b812 Andrea Spadaccini
  if constants.NV_USERSCRIPTS in what:
975 17b0b812 Andrea Spadaccini
    result[constants.NV_USERSCRIPTS] = \
976 17b0b812 Andrea Spadaccini
      [script for script in what[constants.NV_USERSCRIPTS]
977 10b86782 Michael Hanselmann
       if not utils.IsExecutable(script)]
978 17b0b812 Andrea Spadaccini
979 16f41f24 René Nussbaumer
  if constants.NV_OOB_PATHS in what:
980 16f41f24 René Nussbaumer
    result[constants.NV_OOB_PATHS] = tmp = []
981 16f41f24 René Nussbaumer
    for path in what[constants.NV_OOB_PATHS]:
982 16f41f24 René Nussbaumer
      try:
983 16f41f24 René Nussbaumer
        st = os.stat(path)
984 16f41f24 René Nussbaumer
      except OSError, err:
985 16f41f24 René Nussbaumer
        tmp.append("error stating out of band helper: %s" % err)
986 16f41f24 René Nussbaumer
      else:
987 16f41f24 René Nussbaumer
        if stat.S_ISREG(st.st_mode):
988 16f41f24 René Nussbaumer
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
989 16f41f24 René Nussbaumer
            tmp.append(None)
990 16f41f24 René Nussbaumer
          else:
991 16f41f24 René Nussbaumer
            tmp.append("out of band helper %s is not executable" % path)
992 16f41f24 René Nussbaumer
        else:
993 16f41f24 René Nussbaumer
          tmp.append("out of band helper %s is not a file" % path)
994 16f41f24 René Nussbaumer
995 8964ee14 Iustin Pop
  if constants.NV_LVLIST in what and vm_capable:
996 ed904904 Iustin Pop
    try:
997 84d7e26b Dmitry Chernyak
      val = GetVolumeList(utils.ListVolumeGroups().keys())
998 ed904904 Iustin Pop
    except RPCFail, err:
999 ed904904 Iustin Pop
      val = str(err)
1000 ed904904 Iustin Pop
    result[constants.NV_LVLIST] = val
1001 25361b9a Iustin Pop
1002 5b0dfcef Helga Velroyen
  _VerifyInstanceList(what, vm_capable, result, all_hvparams)
1003 25361b9a Iustin Pop
1004 8964ee14 Iustin Pop
  if constants.NV_VGLIST in what and vm_capable:
1005 e480923b Iustin Pop
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
1006 25361b9a Iustin Pop
1007 8964ee14 Iustin Pop
  if constants.NV_PVLIST in what and vm_capable:
1008 d5a690cb Bernardo Dal Seno
    check_exclusive_pvs = constants.NV_EXCLUSIVEPVS in what
1009 59726e15 Bernardo Dal Seno
    val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
1010 d5a690cb Bernardo Dal Seno
                                       filter_allocatable=False,
1011 d5a690cb Bernardo Dal Seno
                                       include_lvs=check_exclusive_pvs)
1012 d5a690cb Bernardo Dal Seno
    if check_exclusive_pvs:
1013 d5a690cb Bernardo Dal Seno
      result[constants.NV_EXCLUSIVEPVS] = _CheckExclusivePvs(val)
1014 d5a690cb Bernardo Dal Seno
      for pvi in val:
1015 d5a690cb Bernardo Dal Seno
        # Avoid sending useless data on the wire
1016 d5a690cb Bernardo Dal Seno
        pvi.lv_list = []
1017 59726e15 Bernardo Dal Seno
    result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
1018 d091393e Iustin Pop
1019 25361b9a Iustin Pop
  if constants.NV_VERSION in what:
1020 e9ce0a64 Iustin Pop
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
1021 e9ce0a64 Iustin Pop
                                    constants.RELEASE_VERSION)
1022 25361b9a Iustin Pop
1023 5b0dfcef Helga Velroyen
  _VerifyNodeInfo(what, vm_capable, result, all_hvparams)
1024 9d4bfc96 Iustin Pop
1025 5bb0a1cb Thomas Thrainer
  if constants.NV_DRBDVERSION in what and vm_capable:
1026 5bb0a1cb Thomas Thrainer
    try:
1027 47e0abee Thomas Thrainer
      drbd_version = DRBD8.GetProcInfo().GetVersionString()
1028 5bb0a1cb Thomas Thrainer
    except errors.BlockDeviceError, err:
1029 5bb0a1cb Thomas Thrainer
      logging.warning("Can't get DRBD version", exc_info=True)
1030 5bb0a1cb Thomas Thrainer
      drbd_version = str(err)
1031 5bb0a1cb Thomas Thrainer
    result[constants.NV_DRBDVERSION] = drbd_version
1032 5bb0a1cb Thomas Thrainer
1033 8964ee14 Iustin Pop
  if constants.NV_DRBDLIST in what and vm_capable:
1034 6d2e83d5 Iustin Pop
    try:
1035 47e0abee Thomas Thrainer
      used_minors = drbd.DRBD8.GetUsedDevs()
1036 f6eaed12 Iustin Pop
    except errors.BlockDeviceError, err:
1037 6d2e83d5 Iustin Pop
      logging.warning("Can't get used minors list", exc_info=True)
1038 f6eaed12 Iustin Pop
      used_minors = str(err)
1039 6d2e83d5 Iustin Pop
    result[constants.NV_DRBDLIST] = used_minors
1040 6d2e83d5 Iustin Pop
1041 8964ee14 Iustin Pop
  if constants.NV_DRBDHELPER in what and vm_capable:
1042 7ef40fbe Luca Bigliardi
    status = True
1043 7ef40fbe Luca Bigliardi
    try:
1044 47e0abee Thomas Thrainer
      payload = drbd.DRBD8.GetUsermodeHelper()
1045 7ef40fbe Luca Bigliardi
    except errors.BlockDeviceError, err:
1046 7ef40fbe Luca Bigliardi
      logging.error("Can't get DRBD usermode helper: %s", str(err))
1047 7ef40fbe Luca Bigliardi
      status = False
1048 7ef40fbe Luca Bigliardi
      payload = str(err)
1049 7ef40fbe Luca Bigliardi
    result[constants.NV_DRBDHELPER] = (status, payload)
1050 7ef40fbe Luca Bigliardi
1051 7c0aa8e9 Iustin Pop
  if constants.NV_NODESETUP in what:
1052 7c0aa8e9 Iustin Pop
    result[constants.NV_NODESETUP] = tmpr = []
1053 7c0aa8e9 Iustin Pop
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
1054 7c0aa8e9 Iustin Pop
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
1055 7c0aa8e9 Iustin Pop
                  " under /sys, missing required directories /sys/block"
1056 7c0aa8e9 Iustin Pop
                  " and /sys/class/net")
1057 7c0aa8e9 Iustin Pop
    if (not os.path.isdir("/proc/sys") or
1058 7c0aa8e9 Iustin Pop
        not os.path.isfile("/proc/sysrq-trigger")):
1059 7c0aa8e9 Iustin Pop
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
1060 7c0aa8e9 Iustin Pop
                  " under /proc, missing required directory /proc/sys and"
1061 7c0aa8e9 Iustin Pop
                  " the file /proc/sysrq-trigger")
1062 313b2dd4 Michael Hanselmann
1063 313b2dd4 Michael Hanselmann
  if constants.NV_TIME in what:
1064 313b2dd4 Michael Hanselmann
    result[constants.NV_TIME] = utils.SplitTime(time.time())
1065 313b2dd4 Michael Hanselmann
1066 8964ee14 Iustin Pop
  if constants.NV_OSLIST in what and vm_capable:
1067 b0d85178 Iustin Pop
    result[constants.NV_OSLIST] = DiagnoseOS()
1068 b0d85178 Iustin Pop
1069 20d317d4 Iustin Pop
  if constants.NV_BRIDGES in what and vm_capable:
1070 20d317d4 Iustin Pop
    result[constants.NV_BRIDGES] = [bridge
1071 20d317d4 Iustin Pop
                                    for bridge in what[constants.NV_BRIDGES]
1072 20d317d4 Iustin Pop
                                    if not utils.BridgeExists(bridge)]
1073 23e3c9b7 Michael Hanselmann
1074 72b35807 Michael Hanselmann
  if what.get(constants.NV_FILE_STORAGE_PATHS) == my_name:
1075 72b35807 Michael Hanselmann
    result[constants.NV_FILE_STORAGE_PATHS] = \
1076 72b35807 Michael Hanselmann
      bdev.ComputeWrongFileStoragePaths()
1077 72b35807 Michael Hanselmann
1078 c26a6bd2 Iustin Pop
  return result
1079 a8083063 Iustin Pop
1080 a8083063 Iustin Pop
1081 2be7273c Apollon Oikonomopoulos
def GetBlockDevSizes(devices):
1082 2be7273c Apollon Oikonomopoulos
  """Return the size of the given block devices
1083 2be7273c Apollon Oikonomopoulos

1084 2be7273c Apollon Oikonomopoulos
  @type devices: list
1085 2be7273c Apollon Oikonomopoulos
  @param devices: list of block device nodes to query
1086 2be7273c Apollon Oikonomopoulos
  @rtype: dict
1087 2be7273c Apollon Oikonomopoulos
  @return:
1088 2be7273c Apollon Oikonomopoulos
    dictionary of all block devices under /dev (key). The value is their
1089 2be7273c Apollon Oikonomopoulos
    size in MiB.
1090 2be7273c Apollon Oikonomopoulos

1091 2be7273c Apollon Oikonomopoulos
    {'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
1092 2be7273c Apollon Oikonomopoulos

1093 2be7273c Apollon Oikonomopoulos
  """
1094 2be7273c Apollon Oikonomopoulos
  DEV_PREFIX = "/dev/"
1095 2be7273c Apollon Oikonomopoulos
  blockdevs = {}
1096 2be7273c Apollon Oikonomopoulos
1097 2be7273c Apollon Oikonomopoulos
  for devpath in devices:
1098 cf00dba0 René Nussbaumer
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
1099 2be7273c Apollon Oikonomopoulos
      continue
1100 2be7273c Apollon Oikonomopoulos
1101 2be7273c Apollon Oikonomopoulos
    try:
1102 2be7273c Apollon Oikonomopoulos
      st = os.stat(devpath)
1103 2be7273c Apollon Oikonomopoulos
    except EnvironmentError, err:
1104 2be7273c Apollon Oikonomopoulos
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
1105 2be7273c Apollon Oikonomopoulos
      continue
1106 2be7273c Apollon Oikonomopoulos
1107 2be7273c Apollon Oikonomopoulos
    if stat.S_ISBLK(st.st_mode):
1108 2be7273c Apollon Oikonomopoulos
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
1109 2be7273c Apollon Oikonomopoulos
      if result.failed:
1110 2be7273c Apollon Oikonomopoulos
        # We don't want to fail, just do not list this device as available
1111 2be7273c Apollon Oikonomopoulos
        logging.warning("Cannot get size for block device %s", devpath)
1112 2be7273c Apollon Oikonomopoulos
        continue
1113 2be7273c Apollon Oikonomopoulos
1114 2be7273c Apollon Oikonomopoulos
      size = int(result.stdout) / (1024 * 1024)
1115 2be7273c Apollon Oikonomopoulos
      blockdevs[devpath] = size
1116 2be7273c Apollon Oikonomopoulos
  return blockdevs
1117 2be7273c Apollon Oikonomopoulos
1118 2be7273c Apollon Oikonomopoulos
1119 84d7e26b Dmitry Chernyak
def GetVolumeList(vg_names):
1120 a8083063 Iustin Pop
  """Compute list of logical volumes and their size.
1121 a8083063 Iustin Pop

1122 84d7e26b Dmitry Chernyak
  @type vg_names: list
1123 397693d3 Iustin Pop
  @param vg_names: the volume groups whose LVs we should list, or
1124 397693d3 Iustin Pop
      empty for all volume groups
1125 10c2650b Iustin Pop
  @rtype: dict
1126 10c2650b Iustin Pop
  @return:
1127 10c2650b Iustin Pop
      dictionary of all partions (key) with value being a tuple of
1128 10c2650b Iustin Pop
      their size (in MiB), inactive and online status::
1129 10c2650b Iustin Pop

1130 84d7e26b Dmitry Chernyak
        {'xenvg/test1': ('20.06', True, True)}
1131 10c2650b Iustin Pop

1132 10c2650b Iustin Pop
      in case of errors, a string is returned with the error
1133 10c2650b Iustin Pop
      details.
1134 a8083063 Iustin Pop

1135 a8083063 Iustin Pop
  """
1136 cb2037a2 Iustin Pop
  lvs = {}
1137 d0c8c01d Iustin Pop
  sep = "|"
1138 397693d3 Iustin Pop
  if not vg_names:
1139 397693d3 Iustin Pop
    vg_names = []
1140 cb2037a2 Iustin Pop
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
1141 cb2037a2 Iustin Pop
                         "--separator=%s" % sep,
1142 84d7e26b Dmitry Chernyak
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
1143 a8083063 Iustin Pop
  if result.failed:
1144 29d376ec Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
1145 cb2037a2 Iustin Pop
1146 cb2037a2 Iustin Pop
  for line in result.stdout.splitlines():
1147 df4c2628 Iustin Pop
    line = line.strip()
1148 0b5303da Iustin Pop
    match = _LVSLINE_REGEX.match(line)
1149 df4c2628 Iustin Pop
    if not match:
1150 18682bca Iustin Pop
      logging.error("Invalid line returned from lvs output: '%s'", line)
1151 df4c2628 Iustin Pop
      continue
1152 84d7e26b Dmitry Chernyak
    vg_name, name, size, attr = match.groups()
1153 d0c8c01d Iustin Pop
    inactive = attr[4] == "-"
1154 d0c8c01d Iustin Pop
    online = attr[5] == "o"
1155 d0c8c01d Iustin Pop
    virtual = attr[0] == "v"
1156 33f2a81a Iustin Pop
    if virtual:
1157 33f2a81a Iustin Pop
      # we don't want to report such volumes as existing, since they
1158 33f2a81a Iustin Pop
      # don't really hold data
1159 33f2a81a Iustin Pop
      continue
1160 e687ec01 Michael Hanselmann
    lvs[vg_name + "/" + name] = (size, inactive, online)
1161 cb2037a2 Iustin Pop
1162 cb2037a2 Iustin Pop
  return lvs
1163 a8083063 Iustin Pop
1164 a8083063 Iustin Pop
1165 a8083063 Iustin Pop
def ListVolumeGroups():
1166 2f8598a5 Alexander Schreiber
  """List the volume groups and their size.
1167 a8083063 Iustin Pop

1168 10c2650b Iustin Pop
  @rtype: dict
1169 10c2650b Iustin Pop
  @return: dictionary with keys volume name and values the
1170 10c2650b Iustin Pop
      size of the volume
1171 a8083063 Iustin Pop

1172 a8083063 Iustin Pop
  """
1173 c26a6bd2 Iustin Pop
  return utils.ListVolumeGroups()
1174 a8083063 Iustin Pop
1175 a8083063 Iustin Pop
1176 dcb93971 Michael Hanselmann
def NodeVolumes():
1177 dcb93971 Michael Hanselmann
  """List all volumes on this node.
1178 dcb93971 Michael Hanselmann

1179 10c2650b Iustin Pop
  @rtype: list
1180 10c2650b Iustin Pop
  @return:
1181 10c2650b Iustin Pop
    A list of dictionaries, each having four keys:
1182 10c2650b Iustin Pop
      - name: the logical volume name,
1183 10c2650b Iustin Pop
      - size: the size of the logical volume
1184 10c2650b Iustin Pop
      - dev: the physical device on which the LV lives
1185 10c2650b Iustin Pop
      - vg: the volume group to which it belongs
1186 10c2650b Iustin Pop

1187 10c2650b Iustin Pop
    In case of errors, we return an empty list and log the
1188 10c2650b Iustin Pop
    error.
1189 10c2650b Iustin Pop

1190 10c2650b Iustin Pop
    Note that since a logical volume can live on multiple physical
1191 10c2650b Iustin Pop
    volumes, the resulting list might include a logical volume
1192 10c2650b Iustin Pop
    multiple times.
1193 10c2650b Iustin Pop

1194 dcb93971 Michael Hanselmann
  """
1195 dcb93971 Michael Hanselmann
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
1196 dcb93971 Michael Hanselmann
                         "--separator=|",
1197 dcb93971 Michael Hanselmann
                         "--options=lv_name,lv_size,devices,vg_name"])
1198 dcb93971 Michael Hanselmann
  if result.failed:
1199 10bfe6cb Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s",
1200 10bfe6cb Iustin Pop
          result.output)
1201 dcb93971 Michael Hanselmann
1202 dcb93971 Michael Hanselmann
  def parse_dev(dev):
1203 d0c8c01d Iustin Pop
    return dev.split("(")[0]
1204 89e5ab02 Iustin Pop
1205 89e5ab02 Iustin Pop
  def handle_dev(dev):
1206 89e5ab02 Iustin Pop
    return [parse_dev(x) for x in dev.split(",")]
1207 dcb93971 Michael Hanselmann
1208 dcb93971 Michael Hanselmann
  def map_line(line):
1209 89e5ab02 Iustin Pop
    line = [v.strip() for v in line]
1210 d0c8c01d Iustin Pop
    return [{"name": line[0], "size": line[1],
1211 d0c8c01d Iustin Pop
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
1212 89e5ab02 Iustin Pop
1213 89e5ab02 Iustin Pop
  all_devs = []
1214 89e5ab02 Iustin Pop
  for line in result.stdout.splitlines():
1215 d0c8c01d Iustin Pop
    if line.count("|") >= 3:
1216 d0c8c01d Iustin Pop
      all_devs.extend(map_line(line.split("|")))
1217 89e5ab02 Iustin Pop
    else:
1218 89e5ab02 Iustin Pop
      logging.warning("Strange line in the output from lvs: '%s'", line)
1219 89e5ab02 Iustin Pop
  return all_devs
1220 dcb93971 Michael Hanselmann
1221 dcb93971 Michael Hanselmann
1222 a8083063 Iustin Pop
def BridgesExist(bridges_list):
1223 2f8598a5 Alexander Schreiber
  """Check if a list of bridges exist on the current node.
1224 a8083063 Iustin Pop

1225 b1206984 Iustin Pop
  @rtype: boolean
1226 b1206984 Iustin Pop
  @return: C{True} if all of them exist, C{False} otherwise
1227 a8083063 Iustin Pop

1228 a8083063 Iustin Pop
  """
1229 35c0c8da Iustin Pop
  missing = []
1230 a8083063 Iustin Pop
  for bridge in bridges_list:
1231 a8083063 Iustin Pop
    if not utils.BridgeExists(bridge):
1232 35c0c8da Iustin Pop
      missing.append(bridge)
1233 a8083063 Iustin Pop
1234 35c0c8da Iustin Pop
  if missing:
1235 1f864b60 Iustin Pop
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
1236 35c0c8da Iustin Pop
1237 a8083063 Iustin Pop
1238 2bff1928 Helga Velroyen
def GetInstanceListForHypervisor(hname, hvparams=None,
1239 2bff1928 Helga Velroyen
                                 get_hv_fn=hypervisor.GetHypervisor):
1240 2bff1928 Helga Velroyen
  """Provides a list of instances of the given hypervisor.
1241 2bff1928 Helga Velroyen

1242 2bff1928 Helga Velroyen
  @type hname: string
1243 2bff1928 Helga Velroyen
  @param hname: name of the hypervisor
1244 2bff1928 Helga Velroyen
  @type hvparams: dict of strings
1245 2bff1928 Helga Velroyen
  @param hvparams: hypervisor parameters for the given hypervisor
1246 2bff1928 Helga Velroyen
  @type get_hv_fn: function
1247 2bff1928 Helga Velroyen
  @param get_hv_fn: function that returns a hypervisor for the given hypervisor
1248 2bff1928 Helga Velroyen
    name; optional parameter to increase testability
1249 2bff1928 Helga Velroyen

1250 2bff1928 Helga Velroyen
  @rtype: list
1251 2bff1928 Helga Velroyen
  @return: a list of all running instances on the current node
1252 2bff1928 Helga Velroyen
    - instance1.example.com
1253 2bff1928 Helga Velroyen
    - instance2.example.com
1254 2bff1928 Helga Velroyen

1255 2bff1928 Helga Velroyen
  """
1256 2bff1928 Helga Velroyen
  results = []
1257 2bff1928 Helga Velroyen
  try:
1258 2bff1928 Helga Velroyen
    hv = get_hv_fn(hname)
1259 5b0dfcef Helga Velroyen
    names = hv.ListInstances(hvparams=hvparams)
1260 2bff1928 Helga Velroyen
    results.extend(names)
1261 2bff1928 Helga Velroyen
  except errors.HypervisorError, err:
1262 2bff1928 Helga Velroyen
    _Fail("Error enumerating instances (hypervisor %s): %s",
1263 2bff1928 Helga Velroyen
          hname, err, exc=True)
1264 2bff1928 Helga Velroyen
  return results
1265 2bff1928 Helga Velroyen
1266 2bff1928 Helga Velroyen
1267 fac83f8a Helga Velroyen
def GetInstanceList(hypervisor_list, all_hvparams=None,
1268 fac83f8a Helga Velroyen
                    get_hv_fn=hypervisor.GetHypervisor):
1269 2f8598a5 Alexander Schreiber
  """Provides a list of instances.
1270 a8083063 Iustin Pop

1271 e69d05fd Iustin Pop
  @type hypervisor_list: list
1272 e69d05fd Iustin Pop
  @param hypervisor_list: the list of hypervisors to query information
1273 fac83f8a Helga Velroyen
  @type all_hvparams: dict of dict of strings
1274 fac83f8a Helga Velroyen
  @param all_hvparams: a dictionary mapping hypervisor types to respective
1275 fac83f8a Helga Velroyen
    cluster-wide hypervisor parameters
1276 fac83f8a Helga Velroyen
  @type get_hv_fn: function
1277 fac83f8a Helga Velroyen
  @param get_hv_fn: function that returns a hypervisor for the given hypervisor
1278 fac83f8a Helga Velroyen
    name; optional parameter to increase testability
1279 e69d05fd Iustin Pop

1280 e69d05fd Iustin Pop
  @rtype: list
1281 e69d05fd Iustin Pop
  @return: a list of all running instances on the current node
1282 10c2650b Iustin Pop
    - instance1.example.com
1283 10c2650b Iustin Pop
    - instance2.example.com
1284 a8083063 Iustin Pop

1285 098c0958 Michael Hanselmann
  """
1286 e69d05fd Iustin Pop
  results = []
1287 e69d05fd Iustin Pop
  for hname in hypervisor_list:
1288 5b0dfcef Helga Velroyen
    hvparams = all_hvparams[hname]
1289 5b0dfcef Helga Velroyen
    results.extend(GetInstanceListForHypervisor(hname, hvparams=hvparams,
1290 2bff1928 Helga Velroyen
                                                get_hv_fn=get_hv_fn))
1291 e69d05fd Iustin Pop
  return results
1292 a8083063 Iustin Pop
1293 a8083063 Iustin Pop
1294 0bbec3af Helga Velroyen
def GetInstanceInfo(instance, hname, hvparams=None):
1295 5bbd3f7f Michael Hanselmann
  """Gives back the information about an instance as a dictionary.
1296 a8083063 Iustin Pop

1297 e69d05fd Iustin Pop
  @type instance: string
1298 e69d05fd Iustin Pop
  @param instance: the instance name
1299 e69d05fd Iustin Pop
  @type hname: string
1300 e69d05fd Iustin Pop
  @param hname: the hypervisor type of the instance
1301 0bbec3af Helga Velroyen
  @type hvparams: dict of strings
1302 0bbec3af Helga Velroyen
  @param hvparams: the instance's hvparams
1303 a8083063 Iustin Pop

1304 e69d05fd Iustin Pop
  @rtype: dict
1305 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
1306 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1307 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1308 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1309 1cb97324 Agata Murawska
      - vcpus: the number of vcpus (int)
1310 a8083063 Iustin Pop

1311 098c0958 Michael Hanselmann
  """
1312 a8083063 Iustin Pop
  output = {}
1313 a8083063 Iustin Pop
1314 0bbec3af Helga Velroyen
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance,
1315 0bbec3af Helga Velroyen
                                                          hvparams=hvparams)
1316 a8083063 Iustin Pop
  if iinfo is not None:
1317 d0c8c01d Iustin Pop
    output["memory"] = iinfo[2]
1318 1cb97324 Agata Murawska
    output["vcpus"] = iinfo[3]
1319 d0c8c01d Iustin Pop
    output["state"] = iinfo[4]
1320 d0c8c01d Iustin Pop
    output["time"] = iinfo[5]
1321 a8083063 Iustin Pop
1322 c26a6bd2 Iustin Pop
  return output
1323 a8083063 Iustin Pop
1324 a8083063 Iustin Pop
1325 56e7640c Iustin Pop
def GetInstanceMigratable(instance):
1326 3361ab37 Helga Velroyen
  """Computes whether an instance can be migrated.
1327 56e7640c Iustin Pop

1328 56e7640c Iustin Pop
  @type instance: L{objects.Instance}
1329 56e7640c Iustin Pop
  @param instance: object representing the instance to be checked.
1330 56e7640c Iustin Pop

1331 56e7640c Iustin Pop
  @rtype: tuple
1332 56e7640c Iustin Pop
  @return: tuple of (result, description) where:
1333 56e7640c Iustin Pop
      - result: whether the instance can be migrated or not
1334 56e7640c Iustin Pop
      - description: a description of the issue, if relevant
1335 56e7640c Iustin Pop

1336 56e7640c Iustin Pop
  """
1337 56e7640c Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1338 afdc3985 Iustin Pop
  iname = instance.name
1339 3361ab37 Helga Velroyen
  if iname not in hyper.ListInstances(instance.hvparams):
1340 afdc3985 Iustin Pop
    _Fail("Instance %s is not running", iname)
1341 56e7640c Iustin Pop
1342 56e7640c Iustin Pop
  for idx in range(len(instance.disks)):
1343 afdc3985 Iustin Pop
    link_name = _GetBlockDevSymlinkPath(iname, idx)
1344 56e7640c Iustin Pop
    if not os.path.islink(link_name):
1345 b8ebd37b Iustin Pop
      logging.warning("Instance %s is missing symlink %s for disk %d",
1346 b8ebd37b Iustin Pop
                      iname, link_name, idx)
1347 56e7640c Iustin Pop
1348 56e7640c Iustin Pop
1349 0200a1af Helga Velroyen
def GetAllInstancesInfo(hypervisor_list, all_hvparams):
1350 a8083063 Iustin Pop
  """Gather data about all instances.
1351 a8083063 Iustin Pop

1352 10c2650b Iustin Pop
  This is the equivalent of L{GetInstanceInfo}, except that it
1353 a8083063 Iustin Pop
  computes data for all instances at once, thus being faster if one
1354 a8083063 Iustin Pop
  needs data about more than one instance.
1355 a8083063 Iustin Pop

1356 e69d05fd Iustin Pop
  @type hypervisor_list: list
1357 e69d05fd Iustin Pop
  @param hypervisor_list: list of hypervisors to query for instance data
1358 0200a1af Helga Velroyen
  @type all_hvparams: dict of dict of strings
1359 0200a1af Helga Velroyen
  @param all_hvparams: mapping of hypervisor names to hvparams
1360 e69d05fd Iustin Pop

1361 955db481 Guido Trotter
  @rtype: dict
1362 e69d05fd Iustin Pop
  @return: dictionary of instance: data, with data having the following keys:
1363 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1364 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1365 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1366 10c2650b Iustin Pop
      - vcpus: the number of vcpus
1367 a8083063 Iustin Pop

1368 098c0958 Michael Hanselmann
  """
1369 a8083063 Iustin Pop
  output = {}
1370 a8083063 Iustin Pop
1371 e69d05fd Iustin Pop
  for hname in hypervisor_list:
1372 0200a1af Helga Velroyen
    hvparams = all_hvparams[hname]
1373 0200a1af Helga Velroyen
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo(hvparams)
1374 e69d05fd Iustin Pop
    if iinfo:
1375 29921401 Iustin Pop
      for name, _, memory, vcpus, state, times in iinfo:
1376 f23b5ae8 Iustin Pop
        value = {
1377 d0c8c01d Iustin Pop
          "memory": memory,
1378 d0c8c01d Iustin Pop
          "vcpus": vcpus,
1379 d0c8c01d Iustin Pop
          "state": state,
1380 d0c8c01d Iustin Pop
          "time": times,
1381 e69d05fd Iustin Pop
          }
1382 b33b6f55 Iustin Pop
        if name in output:
1383 b33b6f55 Iustin Pop
          # we only check static parameters, like memory and vcpus,
1384 b33b6f55 Iustin Pop
          # and not state and time which can change between the
1385 b33b6f55 Iustin Pop
          # invocations of the different hypervisors
1386 d0c8c01d Iustin Pop
          for key in "memory", "vcpus":
1387 b33b6f55 Iustin Pop
            if value[key] != output[name][key]:
1388 2fa74ef4 Iustin Pop
              _Fail("Instance %s is running twice"
1389 2fa74ef4 Iustin Pop
                    " with different parameters", name)
1390 f23b5ae8 Iustin Pop
        output[name] = value
1391 a8083063 Iustin Pop
1392 c26a6bd2 Iustin Pop
  return output
1393 a8083063 Iustin Pop
1394 a8083063 Iustin Pop
1395 6aa7a354 Iustin Pop
def _InstanceLogName(kind, os_name, instance, component):
1396 81a3406c Iustin Pop
  """Compute the OS log filename for a given instance and operation.
1397 81a3406c Iustin Pop

1398 81a3406c Iustin Pop
  The instance name and os name are passed in as strings since not all
1399 81a3406c Iustin Pop
  operations have these as part of an instance object.
1400 81a3406c Iustin Pop

1401 81a3406c Iustin Pop
  @type kind: string
1402 81a3406c Iustin Pop
  @param kind: the operation type (e.g. add, import, etc.)
1403 81a3406c Iustin Pop
  @type os_name: string
1404 81a3406c Iustin Pop
  @param os_name: the os name
1405 81a3406c Iustin Pop
  @type instance: string
1406 81a3406c Iustin Pop
  @param instance: the name of the instance being imported/added/etc.
1407 6aa7a354 Iustin Pop
  @type component: string or None
1408 6aa7a354 Iustin Pop
  @param component: the name of the component of the instance being
1409 6aa7a354 Iustin Pop
      transferred
1410 81a3406c Iustin Pop

1411 81a3406c Iustin Pop
  """
1412 1651d116 Michael Hanselmann
  # TODO: Use tempfile.mkstemp to create unique filename
1413 6aa7a354 Iustin Pop
  if component:
1414 6aa7a354 Iustin Pop
    assert "/" not in component
1415 6aa7a354 Iustin Pop
    c_msg = "-%s" % component
1416 6aa7a354 Iustin Pop
  else:
1417 6aa7a354 Iustin Pop
    c_msg = ""
1418 6aa7a354 Iustin Pop
  base = ("%s-%s-%s%s-%s.log" %
1419 6aa7a354 Iustin Pop
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
1420 710f30ec Michael Hanselmann
  return utils.PathJoin(pathutils.LOG_OS_DIR, base)
1421 81a3406c Iustin Pop
1422 81a3406c Iustin Pop
1423 4a0e011f Iustin Pop
def InstanceOsAdd(instance, reinstall, debug):
1424 2f8598a5 Alexander Schreiber
  """Add an OS to an instance.
1425 a8083063 Iustin Pop

1426 d15a9ad3 Guido Trotter
  @type instance: L{objects.Instance}
1427 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1428 e557bae9 Guido Trotter
  @type reinstall: boolean
1429 e557bae9 Guido Trotter
  @param reinstall: whether this is an instance reinstall
1430 4a0e011f Iustin Pop
  @type debug: integer
1431 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1432 c26a6bd2 Iustin Pop
  @rtype: None
1433 a8083063 Iustin Pop

1434 a8083063 Iustin Pop
  """
1435 255dcebd Iustin Pop
  inst_os = OSFromDisk(instance.os)
1436 255dcebd Iustin Pop
1437 4a0e011f Iustin Pop
  create_env = OSEnvironment(instance, inst_os, debug)
1438 e557bae9 Guido Trotter
  if reinstall:
1439 d0c8c01d Iustin Pop
    create_env["INSTANCE_REINSTALL"] = "1"
1440 a8083063 Iustin Pop
1441 6aa7a354 Iustin Pop
  logfile = _InstanceLogName("add", instance.os, instance.name, None)
1442 decd5f45 Iustin Pop
1443 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.create_script], env=create_env,
1444 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1445 decd5f45 Iustin Pop
  if result.failed:
1446 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
1447 d868edb4 Iustin Pop
                  " output: %s", result.cmd, result.fail_reason, logfile,
1448 18682bca Iustin Pop
                  result.output)
1449 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1450 20e01edd Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1451 afdc3985 Iustin Pop
    _Fail("OS create script failed (%s), last lines in the"
1452 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1453 decd5f45 Iustin Pop
1454 decd5f45 Iustin Pop
1455 4a0e011f Iustin Pop
def RunRenameInstance(instance, old_name, debug):
1456 decd5f45 Iustin Pop
  """Run the OS rename script for an instance.
1457 decd5f45 Iustin Pop

1458 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1459 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1460 d15a9ad3 Guido Trotter
  @type old_name: string
1461 d15a9ad3 Guido Trotter
  @param old_name: previous instance name
1462 4a0e011f Iustin Pop
  @type debug: integer
1463 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1464 10c2650b Iustin Pop
  @rtype: boolean
1465 10c2650b Iustin Pop
  @return: the success of the operation
1466 decd5f45 Iustin Pop

1467 decd5f45 Iustin Pop
  """
1468 decd5f45 Iustin Pop
  inst_os = OSFromDisk(instance.os)
1469 decd5f45 Iustin Pop
1470 4a0e011f Iustin Pop
  rename_env = OSEnvironment(instance, inst_os, debug)
1471 d0c8c01d Iustin Pop
  rename_env["OLD_INSTANCE_NAME"] = old_name
1472 decd5f45 Iustin Pop
1473 81a3406c Iustin Pop
  logfile = _InstanceLogName("rename", instance.os,
1474 6aa7a354 Iustin Pop
                             "%s-%s" % (old_name, instance.name), None)
1475 a8083063 Iustin Pop
1476 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1477 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1478 a8083063 Iustin Pop
1479 a8083063 Iustin Pop
  if result.failed:
1480 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s output: %s",
1481 d868edb4 Iustin Pop
                  result.cmd, result.fail_reason, result.output)
1482 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1483 96841384 Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1484 afdc3985 Iustin Pop
    _Fail("OS rename script failed (%s), last lines in the"
1485 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1486 a8083063 Iustin Pop
1487 a8083063 Iustin Pop
1488 3b721842 Michael Hanselmann
def _GetBlockDevSymlinkPath(instance_name, idx, _dir=None):
1489 3b721842 Michael Hanselmann
  """Returns symlink path for block device.
1490 3b721842 Michael Hanselmann

1491 3b721842 Michael Hanselmann
  """
1492 3b721842 Michael Hanselmann
  if _dir is None:
1493 3b721842 Michael Hanselmann
    _dir = pathutils.DISK_LINKS_DIR
1494 3b721842 Michael Hanselmann
1495 3b721842 Michael Hanselmann
  return utils.PathJoin(_dir,
1496 3b721842 Michael Hanselmann
                        ("%s%s%s" %
1497 3b721842 Michael Hanselmann
                         (instance_name, constants.DISK_SEPARATOR, idx)))
1498 5282084b Iustin Pop
1499 5282084b Iustin Pop
1500 5282084b Iustin Pop
def _SymlinkBlockDev(instance_name, device_path, idx):
1501 9332fd8a Iustin Pop
  """Set up symlinks to a instance's block device.
1502 9332fd8a Iustin Pop

1503 9332fd8a Iustin Pop
  This is an auxiliary function run when an instance is start (on the primary
1504 9332fd8a Iustin Pop
  node) or when an instance is migrated (on the target node).
1505 9332fd8a Iustin Pop

1506 9332fd8a Iustin Pop

1507 5282084b Iustin Pop
  @param instance_name: the name of the target instance
1508 5282084b Iustin Pop
  @param device_path: path of the physical block device, on the node
1509 5282084b Iustin Pop
  @param idx: the disk index
1510 5282084b Iustin Pop
  @return: absolute path to the disk's symlink
1511 9332fd8a Iustin Pop

1512 9332fd8a Iustin Pop
  """
1513 5282084b Iustin Pop
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1514 9332fd8a Iustin Pop
  try:
1515 9332fd8a Iustin Pop
    os.symlink(device_path, link_name)
1516 5282084b Iustin Pop
  except OSError, err:
1517 5282084b Iustin Pop
    if err.errno == errno.EEXIST:
1518 9332fd8a Iustin Pop
      if (not os.path.islink(link_name) or
1519 9332fd8a Iustin Pop
          os.readlink(link_name) != device_path):
1520 9332fd8a Iustin Pop
        os.remove(link_name)
1521 9332fd8a Iustin Pop
        os.symlink(device_path, link_name)
1522 9332fd8a Iustin Pop
    else:
1523 9332fd8a Iustin Pop
      raise
1524 9332fd8a Iustin Pop
1525 9332fd8a Iustin Pop
  return link_name
1526 9332fd8a Iustin Pop
1527 9332fd8a Iustin Pop
1528 5282084b Iustin Pop
def _RemoveBlockDevLinks(instance_name, disks):
1529 3c9c571d Iustin Pop
  """Remove the block device symlinks belonging to the given instance.
1530 3c9c571d Iustin Pop

1531 3c9c571d Iustin Pop
  """
1532 29921401 Iustin Pop
  for idx, _ in enumerate(disks):
1533 5282084b Iustin Pop
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1534 5282084b Iustin Pop
    if os.path.islink(link_name):
1535 3c9c571d Iustin Pop
      try:
1536 03dfa658 Iustin Pop
        os.remove(link_name)
1537 03dfa658 Iustin Pop
      except OSError:
1538 03dfa658 Iustin Pop
        logging.exception("Can't remove symlink '%s'", link_name)
1539 3c9c571d Iustin Pop
1540 3c9c571d Iustin Pop
1541 9332fd8a Iustin Pop
def _GatherAndLinkBlockDevs(instance):
1542 a8083063 Iustin Pop
  """Set up an instance's block device(s).
1543 a8083063 Iustin Pop

1544 a8083063 Iustin Pop
  This is run on the primary node at instance startup. The block
1545 a8083063 Iustin Pop
  devices must be already assembled.
1546 a8083063 Iustin Pop

1547 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1548 10c2650b Iustin Pop
  @param instance: the instance whose disks we shoul assemble
1549 069cfbf1 Iustin Pop
  @rtype: list
1550 069cfbf1 Iustin Pop
  @return: list of (disk_object, device_path)
1551 10c2650b Iustin Pop

1552 a8083063 Iustin Pop
  """
1553 a8083063 Iustin Pop
  block_devices = []
1554 9332fd8a Iustin Pop
  for idx, disk in enumerate(instance.disks):
1555 a8083063 Iustin Pop
    device = _RecursiveFindBD(disk)
1556 a8083063 Iustin Pop
    if device is None:
1557 a8083063 Iustin Pop
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1558 a8083063 Iustin Pop
                                    str(disk))
1559 a8083063 Iustin Pop
    device.Open()
1560 9332fd8a Iustin Pop
    try:
1561 5282084b Iustin Pop
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1562 9332fd8a Iustin Pop
    except OSError, e:
1563 9332fd8a Iustin Pop
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1564 9332fd8a Iustin Pop
                                    e.strerror)
1565 9332fd8a Iustin Pop
1566 9332fd8a Iustin Pop
    block_devices.append((disk, link_name))
1567 9332fd8a Iustin Pop
1568 a8083063 Iustin Pop
  return block_devices
1569 a8083063 Iustin Pop
1570 a8083063 Iustin Pop
1571 1fa6fcba Michele Tartara
def StartInstance(instance, startup_paused, reason, store_reason=True):
1572 a8083063 Iustin Pop
  """Start an instance.
1573 a8083063 Iustin Pop

1574 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1575 e69d05fd Iustin Pop
  @param instance: the instance object
1576 323f9095 Stephen Shirley
  @type startup_paused: bool
1577 323f9095 Stephen Shirley
  @param instance: pause instance at startup?
1578 1fa6fcba Michele Tartara
  @type reason: list of reasons
1579 1fa6fcba Michele Tartara
  @param reason: the reason trail for this startup
1580 1fa6fcba Michele Tartara
  @type store_reason: boolean
1581 1fa6fcba Michele Tartara
  @param store_reason: whether to store the shutdown reason trail on file
1582 c26a6bd2 Iustin Pop
  @rtype: None
1583 a8083063 Iustin Pop

1584 098c0958 Michael Hanselmann
  """
1585 3361ab37 Helga Velroyen
  running_instances = GetInstanceListForHypervisor(instance.hypervisor,
1586 3361ab37 Helga Velroyen
                                                   instance.hvparams)
1587 a8083063 Iustin Pop
1588 a8083063 Iustin Pop
  if instance.name in running_instances:
1589 c26a6bd2 Iustin Pop
    logging.info("Instance %s already running, not starting", instance.name)
1590 c26a6bd2 Iustin Pop
    return
1591 a8083063 Iustin Pop
1592 a8083063 Iustin Pop
  try:
1593 ec596c24 Iustin Pop
    block_devices = _GatherAndLinkBlockDevs(instance)
1594 ec596c24 Iustin Pop
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1595 323f9095 Stephen Shirley
    hyper.StartInstance(instance, block_devices, startup_paused)
1596 1fa6fcba Michele Tartara
    if store_reason:
1597 1fa6fcba Michele Tartara
      _StoreInstReasonTrail(instance.name, reason)
1598 ec596c24 Iustin Pop
  except errors.BlockDeviceError, err:
1599 2cc6781a Iustin Pop
    _Fail("Block device error: %s", err, exc=True)
1600 a8083063 Iustin Pop
  except errors.HypervisorError, err:
1601 5282084b Iustin Pop
    _RemoveBlockDevLinks(instance.name, instance.disks)
1602 2cc6781a Iustin Pop
    _Fail("Hypervisor error: %s", err, exc=True)
1603 a8083063 Iustin Pop
1604 a8083063 Iustin Pop
1605 1f350e0f Michele Tartara
def InstanceShutdown(instance, timeout, reason, store_reason=True):
1606 a8083063 Iustin Pop
  """Shut an instance down.
1607 a8083063 Iustin Pop

1608 10c2650b Iustin Pop
  @note: this functions uses polling with a hardcoded timeout.
1609 10c2650b Iustin Pop

1610 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1611 e69d05fd Iustin Pop
  @param instance: the instance object
1612 6263189c Guido Trotter
  @type timeout: integer
1613 6263189c Guido Trotter
  @param timeout: maximum timeout for soft shutdown
1614 1f350e0f Michele Tartara
  @type reason: list of reasons
1615 1f350e0f Michele Tartara
  @param reason: the reason trail for this shutdown
1616 1f350e0f Michele Tartara
  @type store_reason: boolean
1617 1f350e0f Michele Tartara
  @param store_reason: whether to store the shutdown reason trail on file
1618 c26a6bd2 Iustin Pop
  @rtype: None
1619 a8083063 Iustin Pop

1620 098c0958 Michael Hanselmann
  """
1621 e69d05fd Iustin Pop
  hv_name = instance.hypervisor
1622 e4e9b806 Guido Trotter
  hyper = hypervisor.GetHypervisor(hv_name)
1623 c26a6bd2 Iustin Pop
  iname = instance.name
1624 a8083063 Iustin Pop
1625 3361ab37 Helga Velroyen
  if instance.name not in hyper.ListInstances(instance.hvparams):
1626 c26a6bd2 Iustin Pop
    logging.info("Instance %s not running, doing nothing", iname)
1627 c26a6bd2 Iustin Pop
    return
1628 a8083063 Iustin Pop
1629 3c0cdc83 Michael Hanselmann
  class _TryShutdown:
1630 3c0cdc83 Michael Hanselmann
    def __init__(self):
1631 3c0cdc83 Michael Hanselmann
      self.tried_once = False
1632 a8083063 Iustin Pop
1633 3c0cdc83 Michael Hanselmann
    def __call__(self):
1634 3361ab37 Helga Velroyen
      if iname not in hyper.ListInstances(instance.hvparams):
1635 3c0cdc83 Michael Hanselmann
        return
1636 3c0cdc83 Michael Hanselmann
1637 3c0cdc83 Michael Hanselmann
      try:
1638 3c0cdc83 Michael Hanselmann
        hyper.StopInstance(instance, retry=self.tried_once)
1639 1f350e0f Michele Tartara
        if store_reason:
1640 1f350e0f Michele Tartara
          _StoreInstReasonTrail(instance.name, reason)
1641 3c0cdc83 Michael Hanselmann
      except errors.HypervisorError, err:
1642 3361ab37 Helga Velroyen
        if iname not in hyper.ListInstances(instance.hvparams):
1643 3c0cdc83 Michael Hanselmann
          # if the instance is no longer existing, consider this a
1644 3c0cdc83 Michael Hanselmann
          # success and go to cleanup
1645 3c0cdc83 Michael Hanselmann
          return
1646 3c0cdc83 Michael Hanselmann
1647 3c0cdc83 Michael Hanselmann
        _Fail("Failed to stop instance %s: %s", iname, err)
1648 3c0cdc83 Michael Hanselmann
1649 3c0cdc83 Michael Hanselmann
      self.tried_once = True
1650 3c0cdc83 Michael Hanselmann
1651 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
1652 3c0cdc83 Michael Hanselmann
1653 3c0cdc83 Michael Hanselmann
  try:
1654 3c0cdc83 Michael Hanselmann
    utils.Retry(_TryShutdown(), 5, timeout)
1655 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
1656 a8083063 Iustin Pop
    # the shutdown did not succeed
1657 e4e9b806 Guido Trotter
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1658 a8083063 Iustin Pop
1659 a8083063 Iustin Pop
    try:
1660 a8083063 Iustin Pop
      hyper.StopInstance(instance, force=True)
1661 a8083063 Iustin Pop
    except errors.HypervisorError, err:
1662 3361ab37 Helga Velroyen
      if iname in hyper.ListInstances(instance.hvparams):
1663 3782acd7 Iustin Pop
        # only raise an error if the instance still exists, otherwise
1664 3782acd7 Iustin Pop
        # the error could simply be "instance ... unknown"!
1665 3782acd7 Iustin Pop
        _Fail("Failed to force stop instance %s: %s", iname, err)
1666 a8083063 Iustin Pop
1667 a8083063 Iustin Pop
    time.sleep(1)
1668 3c0cdc83 Michael Hanselmann
1669 3361ab37 Helga Velroyen
    if iname in hyper.ListInstances(instance.hvparams):
1670 c26a6bd2 Iustin Pop
      _Fail("Could not shutdown instance %s even by destroy", iname)
1671 3c9c571d Iustin Pop
1672 f28ec899 Guido Trotter
  try:
1673 f28ec899 Guido Trotter
    hyper.CleanupInstance(instance.name)
1674 f28ec899 Guido Trotter
  except errors.HypervisorError, err:
1675 f28ec899 Guido Trotter
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1676 f28ec899 Guido Trotter
1677 c26a6bd2 Iustin Pop
  _RemoveBlockDevLinks(iname, instance.disks)
1678 a8083063 Iustin Pop
1679 a8083063 Iustin Pop
1680 55cec070 Michele Tartara
def InstanceReboot(instance, reboot_type, shutdown_timeout, reason):
1681 007a2f3e Alexander Schreiber
  """Reboot an instance.
1682 007a2f3e Alexander Schreiber

1683 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1684 10c2650b Iustin Pop
  @param instance: the instance object to reboot
1685 10c2650b Iustin Pop
  @type reboot_type: str
1686 10c2650b Iustin Pop
  @param reboot_type: the type of reboot, one the following
1687 10c2650b Iustin Pop
    constants:
1688 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1689 10c2650b Iustin Pop
        instance OS, do not recreate the VM
1690 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1691 10c2650b Iustin Pop
        restart the VM (at the hypervisor level)
1692 73e5a4f4 Iustin Pop
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1693 73e5a4f4 Iustin Pop
        not accepted here, since that mode is handled differently, in
1694 73e5a4f4 Iustin Pop
        cmdlib, and translates into full stop and start of the
1695 73e5a4f4 Iustin Pop
        instance (instead of a call_instance_reboot RPC)
1696 23057d29 Michael Hanselmann
  @type shutdown_timeout: integer
1697 23057d29 Michael Hanselmann
  @param shutdown_timeout: maximum timeout for soft shutdown
1698 55cec070 Michele Tartara
  @type reason: list of reasons
1699 55cec070 Michele Tartara
  @param reason: the reason trail for this reboot
1700 c26a6bd2 Iustin Pop
  @rtype: None
1701 007a2f3e Alexander Schreiber

1702 007a2f3e Alexander Schreiber
  """
1703 3361ab37 Helga Velroyen
  running_instances = GetInstanceListForHypervisor(instance.hypervisor,
1704 3361ab37 Helga Velroyen
                                                   instance.hvparams)
1705 007a2f3e Alexander Schreiber
1706 007a2f3e Alexander Schreiber
  if instance.name not in running_instances:
1707 2cc6781a Iustin Pop
    _Fail("Cannot reboot instance %s that is not running", instance.name)
1708 007a2f3e Alexander Schreiber
1709 e69d05fd Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1710 007a2f3e Alexander Schreiber
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1711 007a2f3e Alexander Schreiber
    try:
1712 007a2f3e Alexander Schreiber
      hyper.RebootInstance(instance)
1713 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1714 2cc6781a Iustin Pop
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1715 007a2f3e Alexander Schreiber
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1716 007a2f3e Alexander Schreiber
    try:
1717 1f350e0f Michele Tartara
      InstanceShutdown(instance, shutdown_timeout, reason, store_reason=False)
1718 1fa6fcba Michele Tartara
      result = StartInstance(instance, False, reason, store_reason=False)
1719 55cec070 Michele Tartara
      _StoreInstReasonTrail(instance.name, reason)
1720 4a90bd4f Michele Tartara
      return result
1721 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1722 2cc6781a Iustin Pop
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1723 007a2f3e Alexander Schreiber
  else:
1724 2cc6781a Iustin Pop
    _Fail("Invalid reboot_type received: %s", reboot_type)
1725 007a2f3e Alexander Schreiber
1726 007a2f3e Alexander Schreiber
1727 ebe466d8 Guido Trotter
def InstanceBalloonMemory(instance, memory):
1728 ebe466d8 Guido Trotter
  """Resize an instance's memory.
1729 ebe466d8 Guido Trotter

1730 ebe466d8 Guido Trotter
  @type instance: L{objects.Instance}
1731 ebe466d8 Guido Trotter
  @param instance: the instance object
1732 ebe466d8 Guido Trotter
  @type memory: int
1733 ebe466d8 Guido Trotter
  @param memory: new memory amount in MB
1734 ebe466d8 Guido Trotter
  @rtype: None
1735 ebe466d8 Guido Trotter

1736 ebe466d8 Guido Trotter
  """
1737 ebe466d8 Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1738 3361ab37 Helga Velroyen
  running = hyper.ListInstances(instance.hvparams)
1739 ebe466d8 Guido Trotter
  if instance.name not in running:
1740 ebe466d8 Guido Trotter
    logging.info("Instance %s is not running, cannot balloon", instance.name)
1741 ebe466d8 Guido Trotter
    return
1742 ebe466d8 Guido Trotter
  try:
1743 ebe466d8 Guido Trotter
    hyper.BalloonInstanceMemory(instance, memory)
1744 ebe466d8 Guido Trotter
  except errors.HypervisorError, err:
1745 ebe466d8 Guido Trotter
    _Fail("Failed to balloon instance memory: %s", err, exc=True)
1746 ebe466d8 Guido Trotter
1747 ebe466d8 Guido Trotter
1748 6906a9d8 Guido Trotter
def MigrationInfo(instance):
1749 6906a9d8 Guido Trotter
  """Gather information about an instance to be migrated.
1750 6906a9d8 Guido Trotter

1751 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1752 6906a9d8 Guido Trotter
  @param instance: the instance definition
1753 6906a9d8 Guido Trotter

1754 6906a9d8 Guido Trotter
  """
1755 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1756 cd42d0ad Guido Trotter
  try:
1757 cd42d0ad Guido Trotter
    info = hyper.MigrationInfo(instance)
1758 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1759 2cc6781a Iustin Pop
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1760 c26a6bd2 Iustin Pop
  return info
1761 6906a9d8 Guido Trotter
1762 6906a9d8 Guido Trotter
1763 6906a9d8 Guido Trotter
def AcceptInstance(instance, info, target):
1764 6906a9d8 Guido Trotter
  """Prepare the node to accept an instance.
1765 6906a9d8 Guido Trotter

1766 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1767 6906a9d8 Guido Trotter
  @param instance: the instance definition
1768 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1769 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1770 6906a9d8 Guido Trotter
  @type target: string
1771 6906a9d8 Guido Trotter
  @param target: target host (usually ip), on this node
1772 6906a9d8 Guido Trotter

1773 6906a9d8 Guido Trotter
  """
1774 77fcff4a Apollon Oikonomopoulos
  # TODO: why is this required only for DTS_EXT_MIRROR?
1775 77fcff4a Apollon Oikonomopoulos
  if instance.disk_template in constants.DTS_EXT_MIRROR:
1776 77fcff4a Apollon Oikonomopoulos
    # Create the symlinks, as the disks are not active
1777 77fcff4a Apollon Oikonomopoulos
    # in any way
1778 77fcff4a Apollon Oikonomopoulos
    try:
1779 77fcff4a Apollon Oikonomopoulos
      _GatherAndLinkBlockDevs(instance)
1780 77fcff4a Apollon Oikonomopoulos
    except errors.BlockDeviceError, err:
1781 77fcff4a Apollon Oikonomopoulos
      _Fail("Block device error: %s", err, exc=True)
1782 77fcff4a Apollon Oikonomopoulos
1783 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1784 cd42d0ad Guido Trotter
  try:
1785 cd42d0ad Guido Trotter
    hyper.AcceptInstance(instance, info, target)
1786 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1787 77fcff4a Apollon Oikonomopoulos
    if instance.disk_template in constants.DTS_EXT_MIRROR:
1788 77fcff4a Apollon Oikonomopoulos
      _RemoveBlockDevLinks(instance.name, instance.disks)
1789 2cc6781a Iustin Pop
    _Fail("Failed to accept instance: %s", err, exc=True)
1790 6906a9d8 Guido Trotter
1791 6906a9d8 Guido Trotter
1792 6a1434d7 Andrea Spadaccini
def FinalizeMigrationDst(instance, info, success):
1793 6906a9d8 Guido Trotter
  """Finalize any preparation to accept an instance.
1794 6906a9d8 Guido Trotter

1795 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1796 6906a9d8 Guido Trotter
  @param instance: the instance definition
1797 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1798 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1799 6906a9d8 Guido Trotter
  @type success: boolean
1800 6906a9d8 Guido Trotter
  @param success: whether the migration was a success or a failure
1801 6906a9d8 Guido Trotter

1802 6906a9d8 Guido Trotter
  """
1803 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1804 cd42d0ad Guido Trotter
  try:
1805 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationDst(instance, info, success)
1806 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1807 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize migration on the target node: %s", err, exc=True)
1808 6906a9d8 Guido Trotter
1809 6906a9d8 Guido Trotter
1810 bc0a2284 Helga Velroyen
def MigrateInstance(cluster_name, instance, target, live):
1811 2a10865c Iustin Pop
  """Migrates an instance to another node.
1812 2a10865c Iustin Pop

1813 bc0a2284 Helga Velroyen
  @type cluster_name: string
1814 bc0a2284 Helga Velroyen
  @param cluster_name: name of the cluster
1815 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1816 9f0e6b37 Iustin Pop
  @param instance: the instance definition
1817 9f0e6b37 Iustin Pop
  @type target: string
1818 9f0e6b37 Iustin Pop
  @param target: the target node name
1819 9f0e6b37 Iustin Pop
  @type live: boolean
1820 9f0e6b37 Iustin Pop
  @param live: whether the migration should be done live or not (the
1821 9f0e6b37 Iustin Pop
      interpretation of this parameter is left to the hypervisor)
1822 c03fe62b Andrea Spadaccini
  @raise RPCFail: if migration fails for some reason
1823 9f0e6b37 Iustin Pop

1824 2a10865c Iustin Pop
  """
1825 53c776b5 Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1826 2a10865c Iustin Pop
1827 2a10865c Iustin Pop
  try:
1828 bc0a2284 Helga Velroyen
    hyper.MigrateInstance(cluster_name, instance, target, live)
1829 2a10865c Iustin Pop
  except errors.HypervisorError, err:
1830 2cc6781a Iustin Pop
    _Fail("Failed to migrate instance: %s", err, exc=True)
1831 2a10865c Iustin Pop
1832 2a10865c Iustin Pop
1833 6a1434d7 Andrea Spadaccini
def FinalizeMigrationSource(instance, success, live):
1834 6a1434d7 Andrea Spadaccini
  """Finalize the instance migration on the source node.
1835 6a1434d7 Andrea Spadaccini

1836 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1837 6a1434d7 Andrea Spadaccini
  @param instance: the instance definition of the migrated instance
1838 6a1434d7 Andrea Spadaccini
  @type success: bool
1839 6a1434d7 Andrea Spadaccini
  @param success: whether the migration succeeded or not
1840 6a1434d7 Andrea Spadaccini
  @type live: bool
1841 6a1434d7 Andrea Spadaccini
  @param live: whether the user requested a live migration or not
1842 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the execution fails for some reason
1843 6a1434d7 Andrea Spadaccini

1844 6a1434d7 Andrea Spadaccini
  """
1845 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1846 6a1434d7 Andrea Spadaccini
1847 6a1434d7 Andrea Spadaccini
  try:
1848 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationSource(instance, success, live)
1849 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1850 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize the migration on the source node: %s", err,
1851 6a1434d7 Andrea Spadaccini
          exc=True)
1852 6a1434d7 Andrea Spadaccini
1853 6a1434d7 Andrea Spadaccini
1854 6a1434d7 Andrea Spadaccini
def GetMigrationStatus(instance):
1855 6a1434d7 Andrea Spadaccini
  """Get the migration status
1856 6a1434d7 Andrea Spadaccini

1857 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1858 6a1434d7 Andrea Spadaccini
  @param instance: the instance that is being migrated
1859 6a1434d7 Andrea Spadaccini
  @rtype: L{objects.MigrationStatus}
1860 6a1434d7 Andrea Spadaccini
  @return: the status of the current migration (one of
1861 6a1434d7 Andrea Spadaccini
           L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
1862 6a1434d7 Andrea Spadaccini
           progress info that can be retrieved from the hypervisor
1863 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the migration status cannot be retrieved
1864 6a1434d7 Andrea Spadaccini

1865 6a1434d7 Andrea Spadaccini
  """
1866 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1867 6a1434d7 Andrea Spadaccini
  try:
1868 6a1434d7 Andrea Spadaccini
    return hyper.GetMigrationStatus(instance)
1869 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1870 6a1434d7 Andrea Spadaccini
    _Fail("Failed to get migration status: %s", err, exc=True)
1871 6a1434d7 Andrea Spadaccini
1872 6a1434d7 Andrea Spadaccini
1873 ee1478e5 Bernardo Dal Seno
def BlockdevCreate(disk, size, owner, on_primary, info, excl_stor):
1874 a8083063 Iustin Pop
  """Creates a block device for an instance.
1875 a8083063 Iustin Pop

1876 b1206984 Iustin Pop
  @type disk: L{objects.Disk}
1877 b1206984 Iustin Pop
  @param disk: the object describing the disk we should create
1878 b1206984 Iustin Pop
  @type size: int
1879 b1206984 Iustin Pop
  @param size: the size of the physical underlying device, in MiB
1880 b1206984 Iustin Pop
  @type owner: str
1881 b1206984 Iustin Pop
  @param owner: the name of the instance for which disk is created,
1882 b1206984 Iustin Pop
      used for device cache data
1883 b1206984 Iustin Pop
  @type on_primary: boolean
1884 b1206984 Iustin Pop
  @param on_primary:  indicates if it is the primary node or not
1885 b1206984 Iustin Pop
  @type info: string
1886 b1206984 Iustin Pop
  @param info: string that will be sent to the physical device
1887 b1206984 Iustin Pop
      creation, used for example to set (LVM) tags on LVs
1888 ee1478e5 Bernardo Dal Seno
  @type excl_stor: boolean
1889 ee1478e5 Bernardo Dal Seno
  @param excl_stor: Whether exclusive_storage is active
1890 b1206984 Iustin Pop

1891 b1206984 Iustin Pop
  @return: the new unique_id of the device (this can sometime be
1892 b1206984 Iustin Pop
      computed only after creation), or None. On secondary nodes,
1893 b1206984 Iustin Pop
      it's not required to return anything.
1894 a8083063 Iustin Pop

1895 a8083063 Iustin Pop
  """
1896 d0c8c01d Iustin Pop
  # TODO: remove the obsolete "size" argument
1897 b459a848 Andrea Spadaccini
  # pylint: disable=W0613
1898 a8083063 Iustin Pop
  clist = []
1899 a8083063 Iustin Pop
  if disk.children:
1900 a8083063 Iustin Pop
    for child in disk.children:
1901 1063abd1 Iustin Pop
      try:
1902 1063abd1 Iustin Pop
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1903 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1904 2cc6781a Iustin Pop
        _Fail("Can't assemble device %s: %s", child, err)
1905 a8083063 Iustin Pop
      if on_primary or disk.AssembleOnSecondary():
1906 a8083063 Iustin Pop
        # we need the children open in case the device itself has to
1907 a8083063 Iustin Pop
        # be assembled
1908 1063abd1 Iustin Pop
        try:
1909 b459a848 Andrea Spadaccini
          # pylint: disable=E1103
1910 1063abd1 Iustin Pop
          crdev.Open()
1911 1063abd1 Iustin Pop
        except errors.BlockDeviceError, err:
1912 2cc6781a Iustin Pop
          _Fail("Can't make child '%s' read-write: %s", child, err)
1913 a8083063 Iustin Pop
      clist.append(crdev)
1914 a8083063 Iustin Pop
1915 dab69e97 Iustin Pop
  try:
1916 ee1478e5 Bernardo Dal Seno
    device = bdev.Create(disk, clist, excl_stor)
1917 1063abd1 Iustin Pop
  except errors.BlockDeviceError, err:
1918 2cc6781a Iustin Pop
    _Fail("Can't create block device: %s", err)
1919 6c626518 Iustin Pop
1920 a8083063 Iustin Pop
  if on_primary or disk.AssembleOnSecondary():
1921 1063abd1 Iustin Pop
    try:
1922 1063abd1 Iustin Pop
      device.Assemble()
1923 1063abd1 Iustin Pop
    except errors.BlockDeviceError, err:
1924 2cc6781a Iustin Pop
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1925 a8083063 Iustin Pop
    if on_primary or disk.OpenOnSecondary():
1926 1063abd1 Iustin Pop
      try:
1927 1063abd1 Iustin Pop
        device.Open(force=True)
1928 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1929 2cc6781a Iustin Pop
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1930 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(device.dev_path, owner,
1931 3f78eef2 Iustin Pop
                                on_primary, disk.iv_name)
1932 a0c3fea1 Michael Hanselmann
1933 a0c3fea1 Michael Hanselmann
  device.SetInfo(info)
1934 a0c3fea1 Michael Hanselmann
1935 c26a6bd2 Iustin Pop
  return device.unique_id
1936 a8083063 Iustin Pop
1937 a8083063 Iustin Pop
1938 da63bb4e René Nussbaumer
def _WipeDevice(path, offset, size):
1939 69dd363f René Nussbaumer
  """This function actually wipes the device.
1940 69dd363f René Nussbaumer

1941 69dd363f René Nussbaumer
  @param path: The path to the device to wipe
1942 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1943 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1944 69dd363f René Nussbaumer

1945 69dd363f René Nussbaumer
  """
1946 0188611b Michael Hanselmann
  # Internal sizes are always in Mebibytes; if the following "dd" command
1947 0188611b Michael Hanselmann
  # should use a different block size the offset and size given to this
1948 0188611b Michael Hanselmann
  # function must be adjusted accordingly before being passed to "dd".
1949 0188611b Michael Hanselmann
  block_size = 1024 * 1024
1950 0188611b Michael Hanselmann
1951 da63bb4e René Nussbaumer
  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1952 0188611b Michael Hanselmann
         "bs=%s" % block_size, "oflag=direct", "of=%s" % path,
1953 da63bb4e René Nussbaumer
         "count=%d" % size]
1954 da63bb4e René Nussbaumer
  result = utils.RunCmd(cmd)
1955 69dd363f René Nussbaumer
1956 69dd363f René Nussbaumer
  if result.failed:
1957 69dd363f René Nussbaumer
    _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1958 69dd363f René Nussbaumer
          result.fail_reason, result.output)
1959 69dd363f René Nussbaumer
1960 69dd363f René Nussbaumer
1961 da63bb4e René Nussbaumer
def BlockdevWipe(disk, offset, size):
1962 69dd363f René Nussbaumer
  """Wipes a block device.
1963 69dd363f René Nussbaumer

1964 69dd363f René Nussbaumer
  @type disk: L{objects.Disk}
1965 69dd363f René Nussbaumer
  @param disk: the disk object we want to wipe
1966 da63bb4e René Nussbaumer
  @type offset: int
1967 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1968 da63bb4e René Nussbaumer
  @type size: int
1969 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1970 69dd363f René Nussbaumer

1971 69dd363f René Nussbaumer
  """
1972 69dd363f René Nussbaumer
  try:
1973 69dd363f René Nussbaumer
    rdev = _RecursiveFindBD(disk)
1974 da63bb4e René Nussbaumer
  except errors.BlockDeviceError:
1975 da63bb4e René Nussbaumer
    rdev = None
1976 da63bb4e René Nussbaumer
1977 da63bb4e René Nussbaumer
  if not rdev:
1978 da63bb4e René Nussbaumer
    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1979 da63bb4e René Nussbaumer
1980 da63bb4e René Nussbaumer
  # Do cross verify some of the parameters
1981 0188611b Michael Hanselmann
  if offset < 0:
1982 0188611b Michael Hanselmann
    _Fail("Negative offset")
1983 0188611b Michael Hanselmann
  if size < 0:
1984 0188611b Michael Hanselmann
    _Fail("Negative size")
1985 da63bb4e René Nussbaumer
  if offset > rdev.size:
1986 da63bb4e René Nussbaumer
    _Fail("Offset is bigger than device size")
1987 da63bb4e René Nussbaumer
  if (offset + size) > rdev.size:
1988 da63bb4e René Nussbaumer
    _Fail("The provided offset and size to wipe is bigger than device size")
1989 69dd363f René Nussbaumer
1990 da63bb4e René Nussbaumer
  _WipeDevice(rdev.dev_path, offset, size)
1991 69dd363f René Nussbaumer
1992 69dd363f René Nussbaumer
1993 5119c79e René Nussbaumer
def BlockdevPauseResumeSync(disks, pause):
1994 5119c79e René Nussbaumer
  """Pause or resume the sync of the block device.
1995 5119c79e René Nussbaumer

1996 0f39886a René Nussbaumer
  @type disks: list of L{objects.Disk}
1997 0f39886a René Nussbaumer
  @param disks: the disks object we want to pause/resume
1998 5119c79e René Nussbaumer
  @type pause: bool
1999 5119c79e René Nussbaumer
  @param pause: Wheater to pause or resume
2000 5119c79e René Nussbaumer

2001 5119c79e René Nussbaumer
  """
2002 5119c79e René Nussbaumer
  success = []
2003 5119c79e René Nussbaumer
  for disk in disks:
2004 5119c79e René Nussbaumer
    try:
2005 5119c79e René Nussbaumer
      rdev = _RecursiveFindBD(disk)
2006 5119c79e René Nussbaumer
    except errors.BlockDeviceError:
2007 5119c79e René Nussbaumer
      rdev = None
2008 5119c79e René Nussbaumer
2009 5119c79e René Nussbaumer
    if not rdev:
2010 5119c79e René Nussbaumer
      success.append((False, ("Cannot change sync for device %s:"
2011 5119c79e René Nussbaumer
                              " device not found" % disk.iv_name)))
2012 5119c79e René Nussbaumer
      continue
2013 5119c79e René Nussbaumer
2014 5119c79e René Nussbaumer
    result = rdev.PauseResumeSync(pause)
2015 5119c79e René Nussbaumer
2016 5119c79e René Nussbaumer
    if result:
2017 5119c79e René Nussbaumer
      success.append((result, None))
2018 5119c79e René Nussbaumer
    else:
2019 5119c79e René Nussbaumer
      if pause:
2020 5119c79e René Nussbaumer
        msg = "Pause"
2021 5119c79e René Nussbaumer
      else:
2022 5119c79e René Nussbaumer
        msg = "Resume"
2023 5119c79e René Nussbaumer
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
2024 5119c79e René Nussbaumer
2025 5119c79e René Nussbaumer
  return success
2026 5119c79e René Nussbaumer
2027 5119c79e René Nussbaumer
2028 821d1bd1 Iustin Pop
def BlockdevRemove(disk):
2029 a8083063 Iustin Pop
  """Remove a block device.
2030 a8083063 Iustin Pop

2031 10c2650b Iustin Pop
  @note: This is intended to be called recursively.
2032 10c2650b Iustin Pop

2033 c41eea6e Iustin Pop
  @type disk: L{objects.Disk}
2034 10c2650b Iustin Pop
  @param disk: the disk object we should remove
2035 10c2650b Iustin Pop
  @rtype: boolean
2036 10c2650b Iustin Pop
  @return: the success of the operation
2037 a8083063 Iustin Pop

2038 a8083063 Iustin Pop
  """
2039 e1bc0878 Iustin Pop
  msgs = []
2040 a8083063 Iustin Pop
  try:
2041 bca2e7f4 Iustin Pop
    rdev = _RecursiveFindBD(disk)
2042 a8083063 Iustin Pop
  except errors.BlockDeviceError, err:
2043 a8083063 Iustin Pop
    # probably can't attach
2044 18682bca Iustin Pop
    logging.info("Can't attach to device %s in remove", disk)
2045 a8083063 Iustin Pop
    rdev = None
2046 a8083063 Iustin Pop
  if rdev is not None:
2047 3f78eef2 Iustin Pop
    r_path = rdev.dev_path
2048 e1bc0878 Iustin Pop
    try:
2049 0c6c04ec Iustin Pop
      rdev.Remove()
2050 e1bc0878 Iustin Pop
    except errors.BlockDeviceError, err:
2051 e1bc0878 Iustin Pop
      msgs.append(str(err))
2052 c26a6bd2 Iustin Pop
    if not msgs:
2053 3f78eef2 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
2054 e1bc0878 Iustin Pop
2055 a8083063 Iustin Pop
  if disk.children:
2056 a8083063 Iustin Pop
    for child in disk.children:
2057 c26a6bd2 Iustin Pop
      try:
2058 c26a6bd2 Iustin Pop
        BlockdevRemove(child)
2059 c26a6bd2 Iustin Pop
      except RPCFail, err:
2060 c26a6bd2 Iustin Pop
        msgs.append(str(err))
2061 e1bc0878 Iustin Pop
2062 c26a6bd2 Iustin Pop
  if msgs:
2063 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
2064 afdc3985 Iustin Pop
2065 a8083063 Iustin Pop
2066 3f78eef2 Iustin Pop
def _RecursiveAssembleBD(disk, owner, as_primary):
2067 a8083063 Iustin Pop
  """Activate a block device for an instance.
2068 a8083063 Iustin Pop

2069 a8083063 Iustin Pop
  This is run on the primary and secondary nodes for an instance.
2070 a8083063 Iustin Pop

2071 10c2650b Iustin Pop
  @note: this function is called recursively.
2072 a8083063 Iustin Pop

2073 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
2074