Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ 57c7bc57

History | View | Annotate | Download (112.3 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 397693d3 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 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 a8083063 Iustin Pop
from ganeti import bdev
58 a8083063 Iustin Pop
from ganeti import objects
59 880478f8 Iustin Pop
from ganeti import ssconf
60 1651d116 Michael Hanselmann
from ganeti import serializer
61 a744b676 Manuel Franceschini
from ganeti import netutils
62 82b22e19 René Nussbaumer
from ganeti import runtime
63 0fa481f5 Andrea Spadaccini
from ganeti import mcpu
64 3ccd3243 Andrea Spadaccini
from ganeti import compat
65 a8083063 Iustin Pop
66 a8083063 Iustin Pop
67 13998ef2 Michael Hanselmann
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
68 714ea7ca Iustin Pop
_ALLOWED_CLEAN_DIRS = frozenset([
69 714ea7ca Iustin Pop
  constants.DATA_DIR,
70 714ea7ca Iustin Pop
  constants.JOB_QUEUE_ARCHIVE_DIR,
71 714ea7ca Iustin Pop
  constants.QUEUE_DIR,
72 f942a838 Michael Hanselmann
  constants.CRYPTO_KEYS_DIR,
73 714ea7ca Iustin Pop
  ])
74 f942a838 Michael Hanselmann
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
75 f942a838 Michael Hanselmann
_X509_KEY_FILE = "key"
76 f942a838 Michael Hanselmann
_X509_CERT_FILE = "cert"
77 1651d116 Michael Hanselmann
_IES_STATUS_FILE = "status"
78 1651d116 Michael Hanselmann
_IES_PID_FILE = "pid"
79 1651d116 Michael Hanselmann
_IES_CA_FILE = "ca"
80 13998ef2 Michael Hanselmann
81 0b5303da Iustin Pop
#: Valid LVS output line regex
82 84d7e26b Dmitry Chernyak
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
83 0b5303da Iustin Pop
84 13998ef2 Michael Hanselmann
85 2cc6781a Iustin Pop
class RPCFail(Exception):
86 2cc6781a Iustin Pop
  """Class denoting RPC failure.
87 2cc6781a Iustin Pop

88 2cc6781a Iustin Pop
  Its argument is the error message.
89 2cc6781a Iustin Pop

90 2cc6781a Iustin Pop
  """
91 2cc6781a Iustin Pop
92 13998ef2 Michael Hanselmann
93 2cc6781a Iustin Pop
def _Fail(msg, *args, **kwargs):
94 2cc6781a Iustin Pop
  """Log an error and the raise an RPCFail exception.
95 2cc6781a Iustin Pop

96 2cc6781a Iustin Pop
  This exception is then handled specially in the ganeti daemon and
97 2cc6781a Iustin Pop
  turned into a 'failed' return type. As such, this function is a
98 2cc6781a Iustin Pop
  useful shortcut for logging the error and returning it to the master
99 2cc6781a Iustin Pop
  daemon.
100 2cc6781a Iustin Pop

101 2cc6781a Iustin Pop
  @type msg: string
102 2cc6781a Iustin Pop
  @param msg: the text of the exception
103 2cc6781a Iustin Pop
  @raise RPCFail
104 2cc6781a Iustin Pop

105 2cc6781a Iustin Pop
  """
106 2cc6781a Iustin Pop
  if args:
107 2cc6781a Iustin Pop
    msg = msg % args
108 afdc3985 Iustin Pop
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
109 afdc3985 Iustin Pop
    if "exc" in kwargs and kwargs["exc"]:
110 afdc3985 Iustin Pop
      logging.exception(msg)
111 afdc3985 Iustin Pop
    else:
112 afdc3985 Iustin Pop
      logging.error(msg)
113 2cc6781a Iustin Pop
  raise RPCFail(msg)
114 2cc6781a Iustin Pop
115 2cc6781a Iustin Pop
116 c657dcc9 Michael Hanselmann
def _GetConfig():
117 93384844 Iustin Pop
  """Simple wrapper to return a SimpleStore.
118 10c2650b Iustin Pop

119 93384844 Iustin Pop
  @rtype: L{ssconf.SimpleStore}
120 93384844 Iustin Pop
  @return: a SimpleStore instance
121 10c2650b Iustin Pop

122 10c2650b Iustin Pop
  """
123 93384844 Iustin Pop
  return ssconf.SimpleStore()
124 c657dcc9 Michael Hanselmann
125 c657dcc9 Michael Hanselmann
126 62c9ec92 Iustin Pop
def _GetSshRunner(cluster_name):
127 10c2650b Iustin Pop
  """Simple wrapper to return an SshRunner.
128 10c2650b Iustin Pop

129 10c2650b Iustin Pop
  @type cluster_name: str
130 10c2650b Iustin Pop
  @param cluster_name: the cluster name, which is needed
131 10c2650b Iustin Pop
      by the SshRunner constructor
132 10c2650b Iustin Pop
  @rtype: L{ssh.SshRunner}
133 10c2650b Iustin Pop
  @return: an SshRunner instance
134 10c2650b Iustin Pop

135 10c2650b Iustin Pop
  """
136 62c9ec92 Iustin Pop
  return ssh.SshRunner(cluster_name)
137 c92b310a Michael Hanselmann
138 c92b310a Michael Hanselmann
139 12bce260 Michael Hanselmann
def _Decompress(data):
140 12bce260 Michael Hanselmann
  """Unpacks data compressed by the RPC client.
141 12bce260 Michael Hanselmann

142 12bce260 Michael Hanselmann
  @type data: list or tuple
143 12bce260 Michael Hanselmann
  @param data: Data sent by RPC client
144 12bce260 Michael Hanselmann
  @rtype: str
145 12bce260 Michael Hanselmann
  @return: Decompressed data
146 12bce260 Michael Hanselmann

147 12bce260 Michael Hanselmann
  """
148 52e2f66e Michael Hanselmann
  assert isinstance(data, (list, tuple))
149 12bce260 Michael Hanselmann
  assert len(data) == 2
150 12bce260 Michael Hanselmann
  (encoding, content) = data
151 12bce260 Michael Hanselmann
  if encoding == constants.RPC_ENCODING_NONE:
152 12bce260 Michael Hanselmann
    return content
153 12bce260 Michael Hanselmann
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
154 12bce260 Michael Hanselmann
    return zlib.decompress(base64.b64decode(content))
155 12bce260 Michael Hanselmann
  else:
156 12bce260 Michael Hanselmann
    raise AssertionError("Unknown data encoding")
157 12bce260 Michael Hanselmann
158 12bce260 Michael Hanselmann
159 3bc6be5c Iustin Pop
def _CleanDirectory(path, exclude=None):
160 76ab5558 Michael Hanselmann
  """Removes all regular files in a directory.
161 76ab5558 Michael Hanselmann

162 10c2650b Iustin Pop
  @type path: str
163 10c2650b Iustin Pop
  @param path: the directory to clean
164 76ab5558 Michael Hanselmann
  @type exclude: list
165 10c2650b Iustin Pop
  @param exclude: list of files to be excluded, defaults
166 10c2650b Iustin Pop
      to the empty list
167 76ab5558 Michael Hanselmann

168 76ab5558 Michael Hanselmann
  """
169 714ea7ca Iustin Pop
  if path not in _ALLOWED_CLEAN_DIRS:
170 714ea7ca Iustin Pop
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
171 714ea7ca Iustin Pop
          path)
172 714ea7ca Iustin Pop
173 3956cee1 Michael Hanselmann
  if not os.path.isdir(path):
174 3956cee1 Michael Hanselmann
    return
175 3bc6be5c Iustin Pop
  if exclude is None:
176 3bc6be5c Iustin Pop
    exclude = []
177 3bc6be5c Iustin Pop
  else:
178 3bc6be5c Iustin Pop
    # Normalize excluded paths
179 3bc6be5c Iustin Pop
    exclude = [os.path.normpath(i) for i in exclude]
180 76ab5558 Michael Hanselmann
181 3956cee1 Michael Hanselmann
  for rel_name in utils.ListVisibleFiles(path):
182 c4feafe8 Iustin Pop
    full_name = utils.PathJoin(path, rel_name)
183 76ab5558 Michael Hanselmann
    if full_name in exclude:
184 76ab5558 Michael Hanselmann
      continue
185 3956cee1 Michael Hanselmann
    if os.path.isfile(full_name) and not os.path.islink(full_name):
186 3956cee1 Michael Hanselmann
      utils.RemoveFile(full_name)
187 3956cee1 Michael Hanselmann
188 3956cee1 Michael Hanselmann
189 360b0dc2 Iustin Pop
def _BuildUploadFileList():
190 360b0dc2 Iustin Pop
  """Build the list of allowed upload files.
191 360b0dc2 Iustin Pop

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

194 360b0dc2 Iustin Pop
  """
195 b397a7d2 Iustin Pop
  allowed_files = set([
196 b397a7d2 Iustin Pop
    constants.CLUSTER_CONF_FILE,
197 b397a7d2 Iustin Pop
    constants.ETC_HOSTS,
198 b397a7d2 Iustin Pop
    constants.SSH_KNOWN_HOSTS_FILE,
199 b397a7d2 Iustin Pop
    constants.VNC_PASSWORD_FILE,
200 b397a7d2 Iustin Pop
    constants.RAPI_CERT_FILE,
201 bfe86c76 Andrea Spadaccini
    constants.SPICE_CERT_FILE,
202 bfe86c76 Andrea Spadaccini
    constants.SPICE_CACERT_FILE,
203 b397a7d2 Iustin Pop
    constants.RAPI_USERS_FILE,
204 6b7d5878 Michael Hanselmann
    constants.CONFD_HMAC_KEY,
205 ff89a747 Michael Hanselmann
    constants.CLUSTER_DOMAIN_SECRET_FILE,
206 b397a7d2 Iustin Pop
    ])
207 b397a7d2 Iustin Pop
208 b397a7d2 Iustin Pop
  for hv_name in constants.HYPER_TYPES:
209 e5a45a16 Iustin Pop
    hv_class = hypervisor.GetHypervisorClass(hv_name)
210 69ab2e12 Guido Trotter
    allowed_files.update(hv_class.GetAncillaryFiles()[0])
211 b397a7d2 Iustin Pop
212 b397a7d2 Iustin Pop
  return frozenset(allowed_files)
213 360b0dc2 Iustin Pop
214 360b0dc2 Iustin Pop
215 360b0dc2 Iustin Pop
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
216 360b0dc2 Iustin Pop
217 360b0dc2 Iustin Pop
218 1bc59f76 Michael Hanselmann
def JobQueuePurge():
219 10c2650b Iustin Pop
  """Removes job queue files and archived jobs.
220 10c2650b Iustin Pop

221 c8457ce7 Iustin Pop
  @rtype: tuple
222 c8457ce7 Iustin Pop
  @return: True, None
223 24fc781f Michael Hanselmann

224 24fc781f Michael Hanselmann
  """
225 1bc59f76 Michael Hanselmann
  _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE])
226 24fc781f Michael Hanselmann
  _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
227 24fc781f Michael Hanselmann
228 24fc781f Michael Hanselmann
229 bd1e4562 Iustin Pop
def GetMasterInfo():
230 bd1e4562 Iustin Pop
  """Returns master information.
231 bd1e4562 Iustin Pop

232 bd1e4562 Iustin Pop
  This is an utility function to compute master information, either
233 bd1e4562 Iustin Pop
  for consumption here or from the node daemon.
234 bd1e4562 Iustin Pop

235 bd1e4562 Iustin Pop
  @rtype: tuple
236 909b3a0e Andrea Spadaccini
  @return: master_netdev, master_ip, master_name, primary_ip_family,
237 909b3a0e Andrea Spadaccini
    master_netmask
238 2a52a064 Iustin Pop
  @raise RPCFail: in case of errors
239 b1b6ea87 Iustin Pop

240 b1b6ea87 Iustin Pop
  """
241 b1b6ea87 Iustin Pop
  try:
242 c657dcc9 Michael Hanselmann
    cfg = _GetConfig()
243 c657dcc9 Michael Hanselmann
    master_netdev = cfg.GetMasterNetdev()
244 c657dcc9 Michael Hanselmann
    master_ip = cfg.GetMasterIP()
245 5a8648eb Andrea Spadaccini
    master_netmask = cfg.GetMasterNetmask()
246 c657dcc9 Michael Hanselmann
    master_node = cfg.GetMasterNode()
247 d8e0caa6 Manuel Franceschini
    primary_ip_family = cfg.GetPrimaryIPFamily()
248 b1b6ea87 Iustin Pop
  except errors.ConfigurationError, err:
249 29921401 Iustin Pop
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
250 909b3a0e Andrea Spadaccini
  return (master_netdev, master_ip, master_node, primary_ip_family,
251 909b3a0e Andrea Spadaccini
      master_netmask)
252 b1b6ea87 Iustin Pop
253 b1b6ea87 Iustin Pop
254 0fa481f5 Andrea Spadaccini
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
255 0fa481f5 Andrea Spadaccini
  """Decorator that runs hooks before and after the decorated function.
256 0fa481f5 Andrea Spadaccini

257 0fa481f5 Andrea Spadaccini
  @type hook_opcode: string
258 0fa481f5 Andrea Spadaccini
  @param hook_opcode: opcode of the hook
259 0fa481f5 Andrea Spadaccini
  @type hooks_path: string
260 0fa481f5 Andrea Spadaccini
  @param hooks_path: path of the hooks
261 0fa481f5 Andrea Spadaccini
  @type env_builder_fn: function
262 0fa481f5 Andrea Spadaccini
  @param env_builder_fn: function that returns a dictionary containing the
263 3ccd3243 Andrea Spadaccini
    environment variables for the hooks. Will get all the parameters of the
264 3ccd3243 Andrea Spadaccini
    decorated function.
265 0fa481f5 Andrea Spadaccini
  @raise RPCFail: in case of pre-hook failure
266 0fa481f5 Andrea Spadaccini

267 0fa481f5 Andrea Spadaccini
  """
268 0fa481f5 Andrea Spadaccini
  def decorator(fn):
269 0fa481f5 Andrea Spadaccini
    def wrapper(*args, **kwargs):
270 0fa481f5 Andrea Spadaccini
      _, myself = ssconf.GetMasterAndMyself()
271 0fa481f5 Andrea Spadaccini
      nodes = ([myself], [myself])  # these hooks run locally
272 0fa481f5 Andrea Spadaccini
273 3ccd3243 Andrea Spadaccini
      env_fn = compat.partial(env_builder_fn, *args, **kwargs)
274 3ccd3243 Andrea Spadaccini
275 0fa481f5 Andrea Spadaccini
      cfg = _GetConfig()
276 0fa481f5 Andrea Spadaccini
      hr = HooksRunner()
277 0fa481f5 Andrea Spadaccini
      hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
278 3ccd3243 Andrea Spadaccini
                            None, env_fn, logging.warning, cfg.GetClusterName(),
279 3ccd3243 Andrea Spadaccini
                            cfg.GetMasterNode())
280 0fa481f5 Andrea Spadaccini
281 0fa481f5 Andrea Spadaccini
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
282 0fa481f5 Andrea Spadaccini
      result = fn(*args, **kwargs)
283 0fa481f5 Andrea Spadaccini
      hm.RunPhase(constants.HOOKS_PHASE_POST)
284 0fa481f5 Andrea Spadaccini
285 0fa481f5 Andrea Spadaccini
      return result
286 0fa481f5 Andrea Spadaccini
    return wrapper
287 0fa481f5 Andrea Spadaccini
  return decorator
288 0fa481f5 Andrea Spadaccini
289 0fa481f5 Andrea Spadaccini
290 57c7bc57 Andrea Spadaccini
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
291 2d88fdd3 Andrea Spadaccini
  """Builds environment variables for master IP hooks.
292 2d88fdd3 Andrea Spadaccini

293 3ccd3243 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
294 3ccd3243 Andrea Spadaccini
  @param master_params: network parameters of the master
295 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
296 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
297 57c7bc57 Andrea Spadaccini
    address setup script (unused, but necessary per the implementation of the
298 57c7bc57 Andrea Spadaccini
    _RunLocalHooks decorator)
299 3ccd3243 Andrea Spadaccini

300 2d88fdd3 Andrea Spadaccini
  """
301 57c7bc57 Andrea Spadaccini
  # pylint: disable=W0613
302 3ccd3243 Andrea Spadaccini
  ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
303 2d88fdd3 Andrea Spadaccini
  env = {
304 3ccd3243 Andrea Spadaccini
    "MASTER_NETDEV": master_params.netdev,
305 3ccd3243 Andrea Spadaccini
    "MASTER_IP": master_params.ip,
306 3ccd3243 Andrea Spadaccini
    "MASTER_NETMASK": master_params.netmask,
307 3ccd3243 Andrea Spadaccini
    "CLUSTER_IP_VERSION": str(ver),
308 2d88fdd3 Andrea Spadaccini
  }
309 2d88fdd3 Andrea Spadaccini
310 2d88fdd3 Andrea Spadaccini
  return env
311 2d88fdd3 Andrea Spadaccini
312 2d88fdd3 Andrea Spadaccini
313 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
314 3a3e4f1e Andrea Spadaccini
               _BuildMasterIpEnv)
315 57c7bc57 Andrea Spadaccini
def ActivateMasterIp(master_params, use_external_mip_script):
316 fb460cf7 Andrea Spadaccini
  """Activate the IP address of the master daemon.
317 fb460cf7 Andrea Spadaccini

318 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
319 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
320 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
321 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
322 57c7bc57 Andrea Spadaccini
    address setup script
323 8da2bd43 Andrea Spadaccini

324 fb460cf7 Andrea Spadaccini
  """
325 57c7bc57 Andrea Spadaccini
  # pylint: disable=W0613
326 fb460cf7 Andrea Spadaccini
  err_msg = None
327 c79198a0 Andrea Spadaccini
  if netutils.TcpPing(master_params.ip, constants.DEFAULT_NODED_PORT):
328 c79198a0 Andrea Spadaccini
    if netutils.IPAddress.Own(master_params.ip):
329 fb460cf7 Andrea Spadaccini
      # we already have the ip:
330 fb460cf7 Andrea Spadaccini
      logging.debug("Master IP already configured, doing nothing")
331 fb460cf7 Andrea Spadaccini
    else:
332 fb460cf7 Andrea Spadaccini
      err_msg = "Someone else has the master ip, not activating"
333 fb460cf7 Andrea Spadaccini
      logging.error(err_msg)
334 fb460cf7 Andrea Spadaccini
  else:
335 c79198a0 Andrea Spadaccini
    ipcls = netutils.IPAddress.GetClassFromIpFamily(master_params.ip_family)
336 fb460cf7 Andrea Spadaccini
337 fb460cf7 Andrea Spadaccini
    result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
338 c79198a0 Andrea Spadaccini
                           "%s/%s" % (master_params.ip, master_params.netmask),
339 c79198a0 Andrea Spadaccini
                           "dev", master_params.netdev, "label",
340 c79198a0 Andrea Spadaccini
                           "%s:0" % master_params.netdev])
341 fb460cf7 Andrea Spadaccini
    if result.failed:
342 fb460cf7 Andrea Spadaccini
      err_msg = "Can't activate master IP: %s" % result.output
343 fb460cf7 Andrea Spadaccini
      logging.error(err_msg)
344 fb460cf7 Andrea Spadaccini
345 9888b9e6 Andrea Spadaccini
    else:
346 9888b9e6 Andrea Spadaccini
      # we ignore the exit code of the following cmds
347 9888b9e6 Andrea Spadaccini
      if ipcls == netutils.IP4Address:
348 c79198a0 Andrea Spadaccini
        utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_params.netdev,
349 c79198a0 Andrea Spadaccini
                      "-s", master_params.ip, master_params.ip])
350 9888b9e6 Andrea Spadaccini
      elif ipcls == netutils.IP6Address:
351 9888b9e6 Andrea Spadaccini
        try:
352 c79198a0 Andrea Spadaccini
          utils.RunCmd(["ndisc6", "-q", "-r 3", master_params.ip,
353 c79198a0 Andrea Spadaccini
                        master_params.netdev])
354 9888b9e6 Andrea Spadaccini
        except errors.OpExecError:
355 9888b9e6 Andrea Spadaccini
          # TODO: Better error reporting
356 9888b9e6 Andrea Spadaccini
          logging.warning("Can't execute ndisc6, please install if missing")
357 fb460cf7 Andrea Spadaccini
358 fb460cf7 Andrea Spadaccini
  if err_msg:
359 fb460cf7 Andrea Spadaccini
    _Fail(err_msg)
360 fb460cf7 Andrea Spadaccini
361 fb460cf7 Andrea Spadaccini
362 fb460cf7 Andrea Spadaccini
def StartMasterDaemons(no_voting):
363 a8083063 Iustin Pop
  """Activate local node as master node.
364 a8083063 Iustin Pop

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

367 3583908a Guido Trotter
  @type no_voting: boolean
368 3583908a Guido Trotter
  @param no_voting: whether to start ganeti-masterd without a node vote
369 fb460cf7 Andrea Spadaccini
      but still non-interactively
370 10c2650b Iustin Pop
  @rtype: None
371 a8083063 Iustin Pop

372 a8083063 Iustin Pop
  """
373 a8083063 Iustin Pop
374 fb460cf7 Andrea Spadaccini
  if no_voting:
375 fb460cf7 Andrea Spadaccini
    masterd_args = "--no-voting --yes-do-it"
376 fb460cf7 Andrea Spadaccini
  else:
377 fb460cf7 Andrea Spadaccini
    masterd_args = ""
378 f154a7a3 Michael Hanselmann
379 fb460cf7 Andrea Spadaccini
  env = {
380 fb460cf7 Andrea Spadaccini
    "EXTRA_MASTERD_ARGS": masterd_args,
381 fb460cf7 Andrea Spadaccini
    }
382 fb460cf7 Andrea Spadaccini
383 fb460cf7 Andrea Spadaccini
  result = utils.RunCmd([constants.DAEMON_UTIL, "start-master"], env=env)
384 fb460cf7 Andrea Spadaccini
  if result.failed:
385 fb460cf7 Andrea Spadaccini
    msg = "Can't start Ganeti master: %s" % result.output
386 fb460cf7 Andrea Spadaccini
    logging.error(msg)
387 fb460cf7 Andrea Spadaccini
    _Fail(msg)
388 f154a7a3 Michael Hanselmann
389 fb460cf7 Andrea Spadaccini
390 2d88fdd3 Andrea Spadaccini
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
391 3a3e4f1e Andrea Spadaccini
               _BuildMasterIpEnv)
392 57c7bc57 Andrea Spadaccini
def DeactivateMasterIp(master_params, use_external_mip_script):
393 fb460cf7 Andrea Spadaccini
  """Deactivate the master IP on this node.
394 a8083063 Iustin Pop

395 c79198a0 Andrea Spadaccini
  @type master_params: L{objects.MasterNetworkParameters}
396 c79198a0 Andrea Spadaccini
  @param master_params: network parameters of the master
397 57c7bc57 Andrea Spadaccini
  @type use_external_mip_script: boolean
398 57c7bc57 Andrea Spadaccini
  @param use_external_mip_script: whether to use an external master IP
399 57c7bc57 Andrea Spadaccini
    address setup script
400 96e0d5cc Andrea Spadaccini

401 a8083063 Iustin Pop
  """
402 57c7bc57 Andrea Spadaccini
  # pylint: disable=W0613
403 6c00d19a Iustin Pop
  # TODO: log and report back to the caller the error failures; we
404 6c00d19a Iustin Pop
  # need to decide in which case we fail the RPC for this
405 2a52a064 Iustin Pop
406 c4dfb0b6 Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
407 c79198a0 Andrea Spadaccini
                         "%s/%s" % (master_params.ip, master_params.netmask),
408 c79198a0 Andrea Spadaccini
                         "dev", master_params.netdev])
409 a8083063 Iustin Pop
  if result.failed:
410 3b9e6a30 Iustin Pop
    logging.error("Can't remove the master IP, error: %s", result.output)
411 b1b6ea87 Iustin Pop
    # but otherwise ignore the failure
412 b1b6ea87 Iustin Pop
413 fb460cf7 Andrea Spadaccini
414 fb460cf7 Andrea Spadaccini
def StopMasterDaemons():
415 fb460cf7 Andrea Spadaccini
  """Stop the master daemons on this node.
416 fb460cf7 Andrea Spadaccini

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

419 fb460cf7 Andrea Spadaccini
  @rtype: None
420 fb460cf7 Andrea Spadaccini

421 fb460cf7 Andrea Spadaccini
  """
422 fb460cf7 Andrea Spadaccini
  # TODO: log and report back to the caller the error failures; we
423 fb460cf7 Andrea Spadaccini
  # need to decide in which case we fail the RPC for this
424 fb460cf7 Andrea Spadaccini
425 fb460cf7 Andrea Spadaccini
  result = utils.RunCmd([constants.DAEMON_UTIL, "stop-master"])
426 fb460cf7 Andrea Spadaccini
  if result.failed:
427 fb460cf7 Andrea Spadaccini
    logging.error("Could not stop Ganeti master, command %s had exitcode %s"
428 fb460cf7 Andrea Spadaccini
                  " and error %s",
429 fb460cf7 Andrea Spadaccini
                  result.cmd, result.exit_code, result.output)
430 a8083063 Iustin Pop
431 a8083063 Iustin Pop
432 41e079ce Andrea Spadaccini
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
433 5a8648eb Andrea Spadaccini
  """Change the netmask of the master IP.
434 5a8648eb Andrea Spadaccini

435 41e079ce Andrea Spadaccini
  @param old_netmask: the old value of the netmask
436 41e079ce Andrea Spadaccini
  @param netmask: the new value of the netmask
437 41e079ce Andrea Spadaccini
  @param master_ip: the master IP
438 41e079ce Andrea Spadaccini
  @param master_netdev: the master network device
439 41e079ce Andrea Spadaccini

440 5a8648eb Andrea Spadaccini
  """
441 5a8648eb Andrea Spadaccini
  if old_netmask == netmask:
442 5a8648eb Andrea Spadaccini
    return
443 5a8648eb Andrea Spadaccini
444 9e6014b9 Andrea Spadaccini
  if not netutils.IPAddress.Own(master_ip):
445 9e6014b9 Andrea Spadaccini
    _Fail("The master IP address is not up, not attempting to change its"
446 9e6014b9 Andrea Spadaccini
          " netmask")
447 9e6014b9 Andrea Spadaccini
448 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
449 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, netmask),
450 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
451 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
452 5a8648eb Andrea Spadaccini
  if result.failed:
453 9e6014b9 Andrea Spadaccini
    _Fail("Could not set the new netmask on the master IP address")
454 5a8648eb Andrea Spadaccini
455 5a8648eb Andrea Spadaccini
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
456 5a8648eb Andrea Spadaccini
                         "%s/%s" % (master_ip, old_netmask),
457 5a8648eb Andrea Spadaccini
                         "dev", master_netdev, "label",
458 5a8648eb Andrea Spadaccini
                         "%s:0" % master_netdev])
459 5a8648eb Andrea Spadaccini
  if result.failed:
460 9e6014b9 Andrea Spadaccini
    _Fail("Could not bring down the master IP address with the old netmask")
461 5a8648eb Andrea Spadaccini
462 5a8648eb Andrea Spadaccini
463 19ddc57a René Nussbaumer
def EtcHostsModify(mode, host, ip):
464 19ddc57a René Nussbaumer
  """Modify a host entry in /etc/hosts.
465 19ddc57a René Nussbaumer

466 19ddc57a René Nussbaumer
  @param mode: The mode to operate. Either add or remove entry
467 19ddc57a René Nussbaumer
  @param host: The host to operate on
468 19ddc57a René Nussbaumer
  @param ip: The ip associated with the entry
469 19ddc57a René Nussbaumer

470 19ddc57a René Nussbaumer
  """
471 19ddc57a René Nussbaumer
  if mode == constants.ETC_HOSTS_ADD:
472 19ddc57a René Nussbaumer
    if not ip:
473 19ddc57a René Nussbaumer
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
474 19ddc57a René Nussbaumer
              " present")
475 19ddc57a René Nussbaumer
    utils.AddHostToEtcHosts(host, ip)
476 19ddc57a René Nussbaumer
  elif mode == constants.ETC_HOSTS_REMOVE:
477 19ddc57a René Nussbaumer
    if ip:
478 19ddc57a René Nussbaumer
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
479 19ddc57a René Nussbaumer
              " parameter is present")
480 19ddc57a René Nussbaumer
    utils.RemoveHostFromEtcHosts(host)
481 19ddc57a René Nussbaumer
  else:
482 19ddc57a René Nussbaumer
    RPCFail("Mode not supported")
483 19ddc57a René Nussbaumer
484 19ddc57a René Nussbaumer
485 b989b9d9 Ken Wehr
def LeaveCluster(modify_ssh_setup):
486 10c2650b Iustin Pop
  """Cleans up and remove the current node.
487 10c2650b Iustin Pop

488 10c2650b Iustin Pop
  This function cleans up and prepares the current node to be removed
489 10c2650b Iustin Pop
  from the cluster.
490 10c2650b Iustin Pop

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

495 b989b9d9 Ken Wehr
  @param modify_ssh_setup: boolean
496 b989b9d9 Ken Wehr

497 a8083063 Iustin Pop
  """
498 f78346f5 Michael Hanselmann
  _CleanDirectory(constants.DATA_DIR)
499 f942a838 Michael Hanselmann
  _CleanDirectory(constants.CRYPTO_KEYS_DIR)
500 1bc59f76 Michael Hanselmann
  JobQueuePurge()
501 f78346f5 Michael Hanselmann
502 b989b9d9 Ken Wehr
  if modify_ssh_setup:
503 b989b9d9 Ken Wehr
    try:
504 b989b9d9 Ken Wehr
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
505 7900ed01 Iustin Pop
506 b989b9d9 Ken Wehr
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
507 a8083063 Iustin Pop
508 b989b9d9 Ken Wehr
      utils.RemoveFile(priv_key)
509 b989b9d9 Ken Wehr
      utils.RemoveFile(pub_key)
510 b989b9d9 Ken Wehr
    except errors.OpExecError:
511 b989b9d9 Ken Wehr
      logging.exception("Error while processing ssh files")
512 a8083063 Iustin Pop
513 ed008420 Guido Trotter
  try:
514 6b7d5878 Michael Hanselmann
    utils.RemoveFile(constants.CONFD_HMAC_KEY)
515 ed008420 Guido Trotter
    utils.RemoveFile(constants.RAPI_CERT_FILE)
516 bfe86c76 Andrea Spadaccini
    utils.RemoveFile(constants.SPICE_CERT_FILE)
517 bfe86c76 Andrea Spadaccini
    utils.RemoveFile(constants.SPICE_CACERT_FILE)
518 168c1de2 Michael Hanselmann
    utils.RemoveFile(constants.NODED_CERT_FILE)
519 b459a848 Andrea Spadaccini
  except: # pylint: disable=W0702
520 ed008420 Guido Trotter
    logging.exception("Error while removing cluster secrets")
521 ed008420 Guido Trotter
522 f154a7a3 Michael Hanselmann
  result = utils.RunCmd([constants.DAEMON_UTIL, "stop", constants.CONFD])
523 f154a7a3 Michael Hanselmann
  if result.failed:
524 f154a7a3 Michael Hanselmann
    logging.error("Command %s failed with exitcode %s and error %s",
525 f154a7a3 Michael Hanselmann
                  result.cmd, result.exit_code, result.output)
526 ed008420 Guido Trotter
527 0623d351 Iustin Pop
  # Raise a custom exception (handled in ganeti-noded)
528 d0c8c01d Iustin Pop
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
529 6d8b6238 Guido Trotter
530 a8083063 Iustin Pop
531 e69d05fd Iustin Pop
def GetNodeInfo(vgname, hypervisor_type):
532 5bbd3f7f Michael Hanselmann
  """Gives back a hash with different information about the node.
533 a8083063 Iustin Pop

534 e69d05fd Iustin Pop
  @type vgname: C{string}
535 e69d05fd Iustin Pop
  @param vgname: the name of the volume group to ask for disk space information
536 e69d05fd Iustin Pop
  @type hypervisor_type: C{str}
537 e69d05fd Iustin Pop
  @param hypervisor_type: the name of the hypervisor to ask for
538 e69d05fd Iustin Pop
      memory information
539 e69d05fd Iustin Pop
  @rtype: C{dict}
540 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
541 e69d05fd Iustin Pop
      - vg_size is the size of the configured volume group in MiB
542 e69d05fd Iustin Pop
      - vg_free is the free size of the volume group in MiB
543 e69d05fd Iustin Pop
      - memory_dom0 is the memory allocated for domain0 in MiB
544 e69d05fd Iustin Pop
      - memory_free is the currently available (free) ram in MiB
545 e69d05fd Iustin Pop
      - memory_total is the total number of ram in MiB
546 34fbc862 Andrea Spadaccini
      - hv_version: the hypervisor version, if available
547 a8083063 Iustin Pop

548 098c0958 Michael Hanselmann
  """
549 a8083063 Iustin Pop
  outputarray = {}
550 673cd9c4 René Nussbaumer
551 cb6a0296 Iustin Pop
  if vgname is not None:
552 cb6a0296 Iustin Pop
    vginfo = bdev.LogicalVolume.GetVGInfo([vgname])
553 cb6a0296 Iustin Pop
    vg_free = vg_size = None
554 cb6a0296 Iustin Pop
    if vginfo:
555 cb6a0296 Iustin Pop
      vg_free = int(round(vginfo[0][0], 0))
556 cb6a0296 Iustin Pop
      vg_size = int(round(vginfo[0][1], 0))
557 d0c8c01d Iustin Pop
    outputarray["vg_size"] = vg_size
558 d0c8c01d Iustin Pop
    outputarray["vg_free"] = vg_free
559 cb6a0296 Iustin Pop
560 cb6a0296 Iustin Pop
  if hypervisor_type is not None:
561 cb6a0296 Iustin Pop
    hyper = hypervisor.GetHypervisor(hypervisor_type)
562 cb6a0296 Iustin Pop
    hyp_info = hyper.GetNodeInfo()
563 cb6a0296 Iustin Pop
    if hyp_info is not None:
564 cb6a0296 Iustin Pop
      outputarray.update(hyp_info)
565 a8083063 Iustin Pop
566 13998ef2 Michael Hanselmann
  outputarray["bootid"] = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
567 3ef10550 Michael Hanselmann
568 c26a6bd2 Iustin Pop
  return outputarray
569 a8083063 Iustin Pop
570 a8083063 Iustin Pop
571 62c9ec92 Iustin Pop
def VerifyNode(what, cluster_name):
572 a8083063 Iustin Pop
  """Verify the status of the local node.
573 a8083063 Iustin Pop

574 e69d05fd Iustin Pop
  Based on the input L{what} parameter, various checks are done on the
575 e69d05fd Iustin Pop
  local node.
576 e69d05fd Iustin Pop

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

580 e69d05fd Iustin Pop
  If the I{nodelist} key is present, we check that we have
581 e69d05fd Iustin Pop
  connectivity via ssh with the target nodes (and check the hostname
582 e69d05fd Iustin Pop
  report).
583 a8083063 Iustin Pop

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

588 e69d05fd Iustin Pop
  @type what: C{dict}
589 e69d05fd Iustin Pop
  @param what: a dictionary of things to check:
590 e69d05fd Iustin Pop
      - filelist: list of files for which to compute checksums
591 e69d05fd Iustin Pop
      - nodelist: list of nodes we should check ssh communication with
592 e69d05fd Iustin Pop
      - node-net-test: list of nodes we should check node daemon port
593 e69d05fd Iustin Pop
        connectivity with
594 e69d05fd Iustin Pop
      - hypervisor: list with hypervisors to run the verify for
595 10c2650b Iustin Pop
  @rtype: dict
596 10c2650b Iustin Pop
  @return: a dictionary with the same keys as the input dict, and
597 10c2650b Iustin Pop
      values representing the result of the checks
598 a8083063 Iustin Pop

599 a8083063 Iustin Pop
  """
600 a8083063 Iustin Pop
  result = {}
601 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
602 a744b676 Manuel Franceschini
  port = netutils.GetDaemonPort(constants.NODED)
603 8964ee14 Iustin Pop
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
604 a8083063 Iustin Pop
605 8964ee14 Iustin Pop
  if constants.NV_HYPERVISOR in what and vm_capable:
606 25361b9a Iustin Pop
    result[constants.NV_HYPERVISOR] = tmp = {}
607 25361b9a Iustin Pop
    for hv_name in what[constants.NV_HYPERVISOR]:
608 0cf5e7f5 Iustin Pop
      try:
609 0cf5e7f5 Iustin Pop
        val = hypervisor.GetHypervisor(hv_name).Verify()
610 0cf5e7f5 Iustin Pop
      except errors.HypervisorError, err:
611 0cf5e7f5 Iustin Pop
        val = "Error while checking hypervisor: %s" % str(err)
612 0cf5e7f5 Iustin Pop
      tmp[hv_name] = val
613 25361b9a Iustin Pop
614 58a59652 Iustin Pop
  if constants.NV_HVPARAMS in what and vm_capable:
615 58a59652 Iustin Pop
    result[constants.NV_HVPARAMS] = tmp = []
616 58a59652 Iustin Pop
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
617 58a59652 Iustin Pop
      try:
618 58a59652 Iustin Pop
        logging.info("Validating hv %s, %s", hv_name, hvparms)
619 58a59652 Iustin Pop
        hypervisor.GetHypervisor(hv_name).ValidateParameters(hvparms)
620 58a59652 Iustin Pop
      except errors.HypervisorError, err:
621 58a59652 Iustin Pop
        tmp.append((source, hv_name, str(err)))
622 58a59652 Iustin Pop
623 25361b9a Iustin Pop
  if constants.NV_FILELIST in what:
624 25361b9a Iustin Pop
    result[constants.NV_FILELIST] = utils.FingerprintFiles(
625 25361b9a Iustin Pop
      what[constants.NV_FILELIST])
626 25361b9a Iustin Pop
627 25361b9a Iustin Pop
  if constants.NV_NODELIST in what:
628 64c7b383 Michael Hanselmann
    (nodes, bynode) = what[constants.NV_NODELIST]
629 64c7b383 Michael Hanselmann
630 64c7b383 Michael Hanselmann
    # Add nodes from other groups (different for each node)
631 64c7b383 Michael Hanselmann
    try:
632 64c7b383 Michael Hanselmann
      nodes.extend(bynode[my_name])
633 64c7b383 Michael Hanselmann
    except KeyError:
634 64c7b383 Michael Hanselmann
      pass
635 64c7b383 Michael Hanselmann
636 64c7b383 Michael Hanselmann
    # Use a random order
637 64c7b383 Michael Hanselmann
    random.shuffle(nodes)
638 64c7b383 Michael Hanselmann
639 64c7b383 Michael Hanselmann
    # Try to contact all nodes
640 64c7b383 Michael Hanselmann
    val = {}
641 64c7b383 Michael Hanselmann
    for node in nodes:
642 62c9ec92 Iustin Pop
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
643 a8083063 Iustin Pop
      if not success:
644 64c7b383 Michael Hanselmann
        val[node] = message
645 64c7b383 Michael Hanselmann
646 64c7b383 Michael Hanselmann
    result[constants.NV_NODELIST] = val
647 25361b9a Iustin Pop
648 25361b9a Iustin Pop
  if constants.NV_NODENETTEST in what:
649 25361b9a Iustin Pop
    result[constants.NV_NODENETTEST] = tmp = {}
650 9d4bfc96 Iustin Pop
    my_pip = my_sip = None
651 25361b9a Iustin Pop
    for name, pip, sip in what[constants.NV_NODENETTEST]:
652 9d4bfc96 Iustin Pop
      if name == my_name:
653 9d4bfc96 Iustin Pop
        my_pip = pip
654 9d4bfc96 Iustin Pop
        my_sip = sip
655 9d4bfc96 Iustin Pop
        break
656 9d4bfc96 Iustin Pop
    if not my_pip:
657 25361b9a Iustin Pop
      tmp[my_name] = ("Can't find my own primary/secondary IP"
658 25361b9a Iustin Pop
                      " in the node list")
659 9d4bfc96 Iustin Pop
    else:
660 25361b9a Iustin Pop
      for name, pip, sip in what[constants.NV_NODENETTEST]:
661 9d4bfc96 Iustin Pop
        fail = []
662 a744b676 Manuel Franceschini
        if not netutils.TcpPing(pip, port, source=my_pip):
663 9d4bfc96 Iustin Pop
          fail.append("primary")
664 9d4bfc96 Iustin Pop
        if sip != pip:
665 a744b676 Manuel Franceschini
          if not netutils.TcpPing(sip, port, source=my_sip):
666 9d4bfc96 Iustin Pop
            fail.append("secondary")
667 9d4bfc96 Iustin Pop
        if fail:
668 25361b9a Iustin Pop
          tmp[name] = ("failure using the %s interface(s)" %
669 25361b9a Iustin Pop
                       " and ".join(fail))
670 25361b9a Iustin Pop
671 a3a5f850 Iustin Pop
  if constants.NV_MASTERIP in what:
672 a3a5f850 Iustin Pop
    # FIXME: add checks on incoming data structures (here and in the
673 a3a5f850 Iustin Pop
    # rest of the function)
674 a3a5f850 Iustin Pop
    master_name, master_ip = what[constants.NV_MASTERIP]
675 a3a5f850 Iustin Pop
    if master_name == my_name:
676 9769bb78 Manuel Franceschini
      source = constants.IP4_ADDRESS_LOCALHOST
677 a3a5f850 Iustin Pop
    else:
678 a3a5f850 Iustin Pop
      source = None
679 a744b676 Manuel Franceschini
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
680 a3a5f850 Iustin Pop
                                                  source=source)
681 a3a5f850 Iustin Pop
682 17b0b812 Andrea Spadaccini
  if constants.NV_USERSCRIPTS in what:
683 17b0b812 Andrea Spadaccini
    result[constants.NV_USERSCRIPTS] = \
684 17b0b812 Andrea Spadaccini
      [script for script in what[constants.NV_USERSCRIPTS]
685 17b0b812 Andrea Spadaccini
       if not (os.path.exists(script) and os.access(script, os.X_OK))]
686 17b0b812 Andrea Spadaccini
687 16f41f24 René Nussbaumer
  if constants.NV_OOB_PATHS in what:
688 16f41f24 René Nussbaumer
    result[constants.NV_OOB_PATHS] = tmp = []
689 16f41f24 René Nussbaumer
    for path in what[constants.NV_OOB_PATHS]:
690 16f41f24 René Nussbaumer
      try:
691 16f41f24 René Nussbaumer
        st = os.stat(path)
692 16f41f24 René Nussbaumer
      except OSError, err:
693 16f41f24 René Nussbaumer
        tmp.append("error stating out of band helper: %s" % err)
694 16f41f24 René Nussbaumer
      else:
695 16f41f24 René Nussbaumer
        if stat.S_ISREG(st.st_mode):
696 16f41f24 René Nussbaumer
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
697 16f41f24 René Nussbaumer
            tmp.append(None)
698 16f41f24 René Nussbaumer
          else:
699 16f41f24 René Nussbaumer
            tmp.append("out of band helper %s is not executable" % path)
700 16f41f24 René Nussbaumer
        else:
701 16f41f24 René Nussbaumer
          tmp.append("out of band helper %s is not a file" % path)
702 16f41f24 René Nussbaumer
703 8964ee14 Iustin Pop
  if constants.NV_LVLIST in what and vm_capable:
704 ed904904 Iustin Pop
    try:
705 84d7e26b Dmitry Chernyak
      val = GetVolumeList(utils.ListVolumeGroups().keys())
706 ed904904 Iustin Pop
    except RPCFail, err:
707 ed904904 Iustin Pop
      val = str(err)
708 ed904904 Iustin Pop
    result[constants.NV_LVLIST] = val
709 25361b9a Iustin Pop
710 8964ee14 Iustin Pop
  if constants.NV_INSTANCELIST in what and vm_capable:
711 0cf5e7f5 Iustin Pop
    # GetInstanceList can fail
712 0cf5e7f5 Iustin Pop
    try:
713 0cf5e7f5 Iustin Pop
      val = GetInstanceList(what[constants.NV_INSTANCELIST])
714 0cf5e7f5 Iustin Pop
    except RPCFail, err:
715 0cf5e7f5 Iustin Pop
      val = str(err)
716 0cf5e7f5 Iustin Pop
    result[constants.NV_INSTANCELIST] = val
717 25361b9a Iustin Pop
718 8964ee14 Iustin Pop
  if constants.NV_VGLIST in what and vm_capable:
719 e480923b Iustin Pop
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
720 25361b9a Iustin Pop
721 8964ee14 Iustin Pop
  if constants.NV_PVLIST in what and vm_capable:
722 d091393e Iustin Pop
    result[constants.NV_PVLIST] = \
723 d091393e Iustin Pop
      bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
724 d091393e Iustin Pop
                                   filter_allocatable=False)
725 d091393e Iustin Pop
726 25361b9a Iustin Pop
  if constants.NV_VERSION in what:
727 e9ce0a64 Iustin Pop
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
728 e9ce0a64 Iustin Pop
                                    constants.RELEASE_VERSION)
729 25361b9a Iustin Pop
730 8964ee14 Iustin Pop
  if constants.NV_HVINFO in what and vm_capable:
731 25361b9a Iustin Pop
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
732 25361b9a Iustin Pop
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
733 9d4bfc96 Iustin Pop
734 8964ee14 Iustin Pop
  if constants.NV_DRBDLIST in what and vm_capable:
735 6d2e83d5 Iustin Pop
    try:
736 6d2e83d5 Iustin Pop
      used_minors = bdev.DRBD8.GetUsedDevs().keys()
737 f6eaed12 Iustin Pop
    except errors.BlockDeviceError, err:
738 6d2e83d5 Iustin Pop
      logging.warning("Can't get used minors list", exc_info=True)
739 f6eaed12 Iustin Pop
      used_minors = str(err)
740 6d2e83d5 Iustin Pop
    result[constants.NV_DRBDLIST] = used_minors
741 6d2e83d5 Iustin Pop
742 8964ee14 Iustin Pop
  if constants.NV_DRBDHELPER in what and vm_capable:
743 7ef40fbe Luca Bigliardi
    status = True
744 7ef40fbe Luca Bigliardi
    try:
745 7ef40fbe Luca Bigliardi
      payload = bdev.BaseDRBD.GetUsermodeHelper()
746 7ef40fbe Luca Bigliardi
    except errors.BlockDeviceError, err:
747 7ef40fbe Luca Bigliardi
      logging.error("Can't get DRBD usermode helper: %s", str(err))
748 7ef40fbe Luca Bigliardi
      status = False
749 7ef40fbe Luca Bigliardi
      payload = str(err)
750 7ef40fbe Luca Bigliardi
    result[constants.NV_DRBDHELPER] = (status, payload)
751 7ef40fbe Luca Bigliardi
752 7c0aa8e9 Iustin Pop
  if constants.NV_NODESETUP in what:
753 7c0aa8e9 Iustin Pop
    result[constants.NV_NODESETUP] = tmpr = []
754 7c0aa8e9 Iustin Pop
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
755 7c0aa8e9 Iustin Pop
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
756 7c0aa8e9 Iustin Pop
                  " under /sys, missing required directories /sys/block"
757 7c0aa8e9 Iustin Pop
                  " and /sys/class/net")
758 7c0aa8e9 Iustin Pop
    if (not os.path.isdir("/proc/sys") or
759 7c0aa8e9 Iustin Pop
        not os.path.isfile("/proc/sysrq-trigger")):
760 7c0aa8e9 Iustin Pop
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
761 7c0aa8e9 Iustin Pop
                  " under /proc, missing required directory /proc/sys and"
762 7c0aa8e9 Iustin Pop
                  " the file /proc/sysrq-trigger")
763 313b2dd4 Michael Hanselmann
764 313b2dd4 Michael Hanselmann
  if constants.NV_TIME in what:
765 313b2dd4 Michael Hanselmann
    result[constants.NV_TIME] = utils.SplitTime(time.time())
766 313b2dd4 Michael Hanselmann
767 8964ee14 Iustin Pop
  if constants.NV_OSLIST in what and vm_capable:
768 b0d85178 Iustin Pop
    result[constants.NV_OSLIST] = DiagnoseOS()
769 b0d85178 Iustin Pop
770 20d317d4 Iustin Pop
  if constants.NV_BRIDGES in what and vm_capable:
771 20d317d4 Iustin Pop
    result[constants.NV_BRIDGES] = [bridge
772 20d317d4 Iustin Pop
                                    for bridge in what[constants.NV_BRIDGES]
773 20d317d4 Iustin Pop
                                    if not utils.BridgeExists(bridge)]
774 c26a6bd2 Iustin Pop
  return result
775 a8083063 Iustin Pop
776 a8083063 Iustin Pop
777 2be7273c Apollon Oikonomopoulos
def GetBlockDevSizes(devices):
778 2be7273c Apollon Oikonomopoulos
  """Return the size of the given block devices
779 2be7273c Apollon Oikonomopoulos

780 2be7273c Apollon Oikonomopoulos
  @type devices: list
781 2be7273c Apollon Oikonomopoulos
  @param devices: list of block device nodes to query
782 2be7273c Apollon Oikonomopoulos
  @rtype: dict
783 2be7273c Apollon Oikonomopoulos
  @return:
784 2be7273c Apollon Oikonomopoulos
    dictionary of all block devices under /dev (key). The value is their
785 2be7273c Apollon Oikonomopoulos
    size in MiB.
786 2be7273c Apollon Oikonomopoulos

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

789 2be7273c Apollon Oikonomopoulos
  """
790 2be7273c Apollon Oikonomopoulos
  DEV_PREFIX = "/dev/"
791 2be7273c Apollon Oikonomopoulos
  blockdevs = {}
792 2be7273c Apollon Oikonomopoulos
793 2be7273c Apollon Oikonomopoulos
  for devpath in devices:
794 cf00dba0 René Nussbaumer
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
795 2be7273c Apollon Oikonomopoulos
      continue
796 2be7273c Apollon Oikonomopoulos
797 2be7273c Apollon Oikonomopoulos
    try:
798 2be7273c Apollon Oikonomopoulos
      st = os.stat(devpath)
799 2be7273c Apollon Oikonomopoulos
    except EnvironmentError, err:
800 2be7273c Apollon Oikonomopoulos
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
801 2be7273c Apollon Oikonomopoulos
      continue
802 2be7273c Apollon Oikonomopoulos
803 2be7273c Apollon Oikonomopoulos
    if stat.S_ISBLK(st.st_mode):
804 2be7273c Apollon Oikonomopoulos
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
805 2be7273c Apollon Oikonomopoulos
      if result.failed:
806 2be7273c Apollon Oikonomopoulos
        # We don't want to fail, just do not list this device as available
807 2be7273c Apollon Oikonomopoulos
        logging.warning("Cannot get size for block device %s", devpath)
808 2be7273c Apollon Oikonomopoulos
        continue
809 2be7273c Apollon Oikonomopoulos
810 2be7273c Apollon Oikonomopoulos
      size = int(result.stdout) / (1024 * 1024)
811 2be7273c Apollon Oikonomopoulos
      blockdevs[devpath] = size
812 2be7273c Apollon Oikonomopoulos
  return blockdevs
813 2be7273c Apollon Oikonomopoulos
814 2be7273c Apollon Oikonomopoulos
815 84d7e26b Dmitry Chernyak
def GetVolumeList(vg_names):
816 a8083063 Iustin Pop
  """Compute list of logical volumes and their size.
817 a8083063 Iustin Pop

818 84d7e26b Dmitry Chernyak
  @type vg_names: list
819 397693d3 Iustin Pop
  @param vg_names: the volume groups whose LVs we should list, or
820 397693d3 Iustin Pop
      empty for all volume groups
821 10c2650b Iustin Pop
  @rtype: dict
822 10c2650b Iustin Pop
  @return:
823 10c2650b Iustin Pop
      dictionary of all partions (key) with value being a tuple of
824 10c2650b Iustin Pop
      their size (in MiB), inactive and online status::
825 10c2650b Iustin Pop

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

828 10c2650b Iustin Pop
      in case of errors, a string is returned with the error
829 10c2650b Iustin Pop
      details.
830 a8083063 Iustin Pop

831 a8083063 Iustin Pop
  """
832 cb2037a2 Iustin Pop
  lvs = {}
833 d0c8c01d Iustin Pop
  sep = "|"
834 397693d3 Iustin Pop
  if not vg_names:
835 397693d3 Iustin Pop
    vg_names = []
836 cb2037a2 Iustin Pop
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
837 cb2037a2 Iustin Pop
                         "--separator=%s" % sep,
838 84d7e26b Dmitry Chernyak
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
839 a8083063 Iustin Pop
  if result.failed:
840 29d376ec Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
841 cb2037a2 Iustin Pop
842 cb2037a2 Iustin Pop
  for line in result.stdout.splitlines():
843 df4c2628 Iustin Pop
    line = line.strip()
844 0b5303da Iustin Pop
    match = _LVSLINE_REGEX.match(line)
845 df4c2628 Iustin Pop
    if not match:
846 18682bca Iustin Pop
      logging.error("Invalid line returned from lvs output: '%s'", line)
847 df4c2628 Iustin Pop
      continue
848 84d7e26b Dmitry Chernyak
    vg_name, name, size, attr = match.groups()
849 d0c8c01d Iustin Pop
    inactive = attr[4] == "-"
850 d0c8c01d Iustin Pop
    online = attr[5] == "o"
851 d0c8c01d Iustin Pop
    virtual = attr[0] == "v"
852 33f2a81a Iustin Pop
    if virtual:
853 33f2a81a Iustin Pop
      # we don't want to report such volumes as existing, since they
854 33f2a81a Iustin Pop
      # don't really hold data
855 33f2a81a Iustin Pop
      continue
856 e687ec01 Michael Hanselmann
    lvs[vg_name + "/" + name] = (size, inactive, online)
857 cb2037a2 Iustin Pop
858 cb2037a2 Iustin Pop
  return lvs
859 a8083063 Iustin Pop
860 a8083063 Iustin Pop
861 a8083063 Iustin Pop
def ListVolumeGroups():
862 2f8598a5 Alexander Schreiber
  """List the volume groups and their size.
863 a8083063 Iustin Pop

864 10c2650b Iustin Pop
  @rtype: dict
865 10c2650b Iustin Pop
  @return: dictionary with keys volume name and values the
866 10c2650b Iustin Pop
      size of the volume
867 a8083063 Iustin Pop

868 a8083063 Iustin Pop
  """
869 c26a6bd2 Iustin Pop
  return utils.ListVolumeGroups()
870 a8083063 Iustin Pop
871 a8083063 Iustin Pop
872 dcb93971 Michael Hanselmann
def NodeVolumes():
873 dcb93971 Michael Hanselmann
  """List all volumes on this node.
874 dcb93971 Michael Hanselmann

875 10c2650b Iustin Pop
  @rtype: list
876 10c2650b Iustin Pop
  @return:
877 10c2650b Iustin Pop
    A list of dictionaries, each having four keys:
878 10c2650b Iustin Pop
      - name: the logical volume name,
879 10c2650b Iustin Pop
      - size: the size of the logical volume
880 10c2650b Iustin Pop
      - dev: the physical device on which the LV lives
881 10c2650b Iustin Pop
      - vg: the volume group to which it belongs
882 10c2650b Iustin Pop

883 10c2650b Iustin Pop
    In case of errors, we return an empty list and log the
884 10c2650b Iustin Pop
    error.
885 10c2650b Iustin Pop

886 10c2650b Iustin Pop
    Note that since a logical volume can live on multiple physical
887 10c2650b Iustin Pop
    volumes, the resulting list might include a logical volume
888 10c2650b Iustin Pop
    multiple times.
889 10c2650b Iustin Pop

890 dcb93971 Michael Hanselmann
  """
891 dcb93971 Michael Hanselmann
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
892 dcb93971 Michael Hanselmann
                         "--separator=|",
893 dcb93971 Michael Hanselmann
                         "--options=lv_name,lv_size,devices,vg_name"])
894 dcb93971 Michael Hanselmann
  if result.failed:
895 10bfe6cb Iustin Pop
    _Fail("Failed to list logical volumes, lvs output: %s",
896 10bfe6cb Iustin Pop
          result.output)
897 dcb93971 Michael Hanselmann
898 dcb93971 Michael Hanselmann
  def parse_dev(dev):
899 d0c8c01d Iustin Pop
    return dev.split("(")[0]
900 89e5ab02 Iustin Pop
901 89e5ab02 Iustin Pop
  def handle_dev(dev):
902 89e5ab02 Iustin Pop
    return [parse_dev(x) for x in dev.split(",")]
903 dcb93971 Michael Hanselmann
904 dcb93971 Michael Hanselmann
  def map_line(line):
905 89e5ab02 Iustin Pop
    line = [v.strip() for v in line]
906 d0c8c01d Iustin Pop
    return [{"name": line[0], "size": line[1],
907 d0c8c01d Iustin Pop
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
908 89e5ab02 Iustin Pop
909 89e5ab02 Iustin Pop
  all_devs = []
910 89e5ab02 Iustin Pop
  for line in result.stdout.splitlines():
911 d0c8c01d Iustin Pop
    if line.count("|") >= 3:
912 d0c8c01d Iustin Pop
      all_devs.extend(map_line(line.split("|")))
913 89e5ab02 Iustin Pop
    else:
914 89e5ab02 Iustin Pop
      logging.warning("Strange line in the output from lvs: '%s'", line)
915 89e5ab02 Iustin Pop
  return all_devs
916 dcb93971 Michael Hanselmann
917 dcb93971 Michael Hanselmann
918 a8083063 Iustin Pop
def BridgesExist(bridges_list):
919 2f8598a5 Alexander Schreiber
  """Check if a list of bridges exist on the current node.
920 a8083063 Iustin Pop

921 b1206984 Iustin Pop
  @rtype: boolean
922 b1206984 Iustin Pop
  @return: C{True} if all of them exist, C{False} otherwise
923 a8083063 Iustin Pop

924 a8083063 Iustin Pop
  """
925 35c0c8da Iustin Pop
  missing = []
926 a8083063 Iustin Pop
  for bridge in bridges_list:
927 a8083063 Iustin Pop
    if not utils.BridgeExists(bridge):
928 35c0c8da Iustin Pop
      missing.append(bridge)
929 a8083063 Iustin Pop
930 35c0c8da Iustin Pop
  if missing:
931 1f864b60 Iustin Pop
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
932 35c0c8da Iustin Pop
933 a8083063 Iustin Pop
934 e69d05fd Iustin Pop
def GetInstanceList(hypervisor_list):
935 2f8598a5 Alexander Schreiber
  """Provides a list of instances.
936 a8083063 Iustin Pop

937 e69d05fd Iustin Pop
  @type hypervisor_list: list
938 e69d05fd Iustin Pop
  @param hypervisor_list: the list of hypervisors to query information
939 e69d05fd Iustin Pop

940 e69d05fd Iustin Pop
  @rtype: list
941 e69d05fd Iustin Pop
  @return: a list of all running instances on the current node
942 10c2650b Iustin Pop
    - instance1.example.com
943 10c2650b Iustin Pop
    - instance2.example.com
944 a8083063 Iustin Pop

945 098c0958 Michael Hanselmann
  """
946 e69d05fd Iustin Pop
  results = []
947 e69d05fd Iustin Pop
  for hname in hypervisor_list:
948 e69d05fd Iustin Pop
    try:
949 e69d05fd Iustin Pop
      names = hypervisor.GetHypervisor(hname).ListInstances()
950 e69d05fd Iustin Pop
      results.extend(names)
951 e69d05fd Iustin Pop
    except errors.HypervisorError, err:
952 aca13712 Iustin Pop
      _Fail("Error enumerating instances (hypervisor %s): %s",
953 aca13712 Iustin Pop
            hname, err, exc=True)
954 a8083063 Iustin Pop
955 e69d05fd Iustin Pop
  return results
956 a8083063 Iustin Pop
957 a8083063 Iustin Pop
958 e69d05fd Iustin Pop
def GetInstanceInfo(instance, hname):
959 5bbd3f7f Michael Hanselmann
  """Gives back the information about an instance as a dictionary.
960 a8083063 Iustin Pop

961 e69d05fd Iustin Pop
  @type instance: string
962 e69d05fd Iustin Pop
  @param instance: the instance name
963 e69d05fd Iustin Pop
  @type hname: string
964 e69d05fd Iustin Pop
  @param hname: the hypervisor type of the instance
965 a8083063 Iustin Pop

966 e69d05fd Iustin Pop
  @rtype: dict
967 e69d05fd Iustin Pop
  @return: dictionary with the following keys:
968 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
969 e69d05fd Iustin Pop
      - state: xen state of instance (string)
970 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
971 a8083063 Iustin Pop

972 098c0958 Michael Hanselmann
  """
973 a8083063 Iustin Pop
  output = {}
974 a8083063 Iustin Pop
975 e69d05fd Iustin Pop
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
976 a8083063 Iustin Pop
  if iinfo is not None:
977 d0c8c01d Iustin Pop
    output["memory"] = iinfo[2]
978 d0c8c01d Iustin Pop
    output["state"] = iinfo[4]
979 d0c8c01d Iustin Pop
    output["time"] = iinfo[5]
980 a8083063 Iustin Pop
981 c26a6bd2 Iustin Pop
  return output
982 a8083063 Iustin Pop
983 a8083063 Iustin Pop
984 56e7640c Iustin Pop
def GetInstanceMigratable(instance):
985 56e7640c Iustin Pop
  """Gives whether an instance can be migrated.
986 56e7640c Iustin Pop

987 56e7640c Iustin Pop
  @type instance: L{objects.Instance}
988 56e7640c Iustin Pop
  @param instance: object representing the instance to be checked.
989 56e7640c Iustin Pop

990 56e7640c Iustin Pop
  @rtype: tuple
991 56e7640c Iustin Pop
  @return: tuple of (result, description) where:
992 56e7640c Iustin Pop
      - result: whether the instance can be migrated or not
993 56e7640c Iustin Pop
      - description: a description of the issue, if relevant
994 56e7640c Iustin Pop

995 56e7640c Iustin Pop
  """
996 56e7640c Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
997 afdc3985 Iustin Pop
  iname = instance.name
998 afdc3985 Iustin Pop
  if iname not in hyper.ListInstances():
999 afdc3985 Iustin Pop
    _Fail("Instance %s is not running", iname)
1000 56e7640c Iustin Pop
1001 56e7640c Iustin Pop
  for idx in range(len(instance.disks)):
1002 afdc3985 Iustin Pop
    link_name = _GetBlockDevSymlinkPath(iname, idx)
1003 56e7640c Iustin Pop
    if not os.path.islink(link_name):
1004 b8ebd37b Iustin Pop
      logging.warning("Instance %s is missing symlink %s for disk %d",
1005 b8ebd37b Iustin Pop
                      iname, link_name, idx)
1006 56e7640c Iustin Pop
1007 56e7640c Iustin Pop
1008 e69d05fd Iustin Pop
def GetAllInstancesInfo(hypervisor_list):
1009 a8083063 Iustin Pop
  """Gather data about all instances.
1010 a8083063 Iustin Pop

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

1015 e69d05fd Iustin Pop
  @type hypervisor_list: list
1016 e69d05fd Iustin Pop
  @param hypervisor_list: list of hypervisors to query for instance data
1017 e69d05fd Iustin Pop

1018 955db481 Guido Trotter
  @rtype: dict
1019 e69d05fd Iustin Pop
  @return: dictionary of instance: data, with data having the following keys:
1020 e69d05fd Iustin Pop
      - memory: memory size of instance (int)
1021 e69d05fd Iustin Pop
      - state: xen state of instance (string)
1022 e69d05fd Iustin Pop
      - time: cpu time of instance (float)
1023 10c2650b Iustin Pop
      - vcpus: the number of vcpus
1024 a8083063 Iustin Pop

1025 098c0958 Michael Hanselmann
  """
1026 a8083063 Iustin Pop
  output = {}
1027 a8083063 Iustin Pop
1028 e69d05fd Iustin Pop
  for hname in hypervisor_list:
1029 e69d05fd Iustin Pop
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
1030 e69d05fd Iustin Pop
    if iinfo:
1031 29921401 Iustin Pop
      for name, _, memory, vcpus, state, times in iinfo:
1032 f23b5ae8 Iustin Pop
        value = {
1033 d0c8c01d Iustin Pop
          "memory": memory,
1034 d0c8c01d Iustin Pop
          "vcpus": vcpus,
1035 d0c8c01d Iustin Pop
          "state": state,
1036 d0c8c01d Iustin Pop
          "time": times,
1037 e69d05fd Iustin Pop
          }
1038 b33b6f55 Iustin Pop
        if name in output:
1039 b33b6f55 Iustin Pop
          # we only check static parameters, like memory and vcpus,
1040 b33b6f55 Iustin Pop
          # and not state and time which can change between the
1041 b33b6f55 Iustin Pop
          # invocations of the different hypervisors
1042 d0c8c01d Iustin Pop
          for key in "memory", "vcpus":
1043 b33b6f55 Iustin Pop
            if value[key] != output[name][key]:
1044 2fa74ef4 Iustin Pop
              _Fail("Instance %s is running twice"
1045 2fa74ef4 Iustin Pop
                    " with different parameters", name)
1046 f23b5ae8 Iustin Pop
        output[name] = value
1047 a8083063 Iustin Pop
1048 c26a6bd2 Iustin Pop
  return output
1049 a8083063 Iustin Pop
1050 a8083063 Iustin Pop
1051 6aa7a354 Iustin Pop
def _InstanceLogName(kind, os_name, instance, component):
1052 81a3406c Iustin Pop
  """Compute the OS log filename for a given instance and operation.
1053 81a3406c Iustin Pop

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

1057 81a3406c Iustin Pop
  @type kind: string
1058 81a3406c Iustin Pop
  @param kind: the operation type (e.g. add, import, etc.)
1059 81a3406c Iustin Pop
  @type os_name: string
1060 81a3406c Iustin Pop
  @param os_name: the os name
1061 81a3406c Iustin Pop
  @type instance: string
1062 81a3406c Iustin Pop
  @param instance: the name of the instance being imported/added/etc.
1063 6aa7a354 Iustin Pop
  @type component: string or None
1064 6aa7a354 Iustin Pop
  @param component: the name of the component of the instance being
1065 6aa7a354 Iustin Pop
      transferred
1066 81a3406c Iustin Pop

1067 81a3406c Iustin Pop
  """
1068 1651d116 Michael Hanselmann
  # TODO: Use tempfile.mkstemp to create unique filename
1069 6aa7a354 Iustin Pop
  if component:
1070 6aa7a354 Iustin Pop
    assert "/" not in component
1071 6aa7a354 Iustin Pop
    c_msg = "-%s" % component
1072 6aa7a354 Iustin Pop
  else:
1073 6aa7a354 Iustin Pop
    c_msg = ""
1074 6aa7a354 Iustin Pop
  base = ("%s-%s-%s%s-%s.log" %
1075 6aa7a354 Iustin Pop
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
1076 81a3406c Iustin Pop
  return utils.PathJoin(constants.LOG_OS_DIR, base)
1077 81a3406c Iustin Pop
1078 81a3406c Iustin Pop
1079 4a0e011f Iustin Pop
def InstanceOsAdd(instance, reinstall, debug):
1080 2f8598a5 Alexander Schreiber
  """Add an OS to an instance.
1081 a8083063 Iustin Pop

1082 d15a9ad3 Guido Trotter
  @type instance: L{objects.Instance}
1083 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1084 e557bae9 Guido Trotter
  @type reinstall: boolean
1085 e557bae9 Guido Trotter
  @param reinstall: whether this is an instance reinstall
1086 4a0e011f Iustin Pop
  @type debug: integer
1087 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1088 c26a6bd2 Iustin Pop
  @rtype: None
1089 a8083063 Iustin Pop

1090 a8083063 Iustin Pop
  """
1091 255dcebd Iustin Pop
  inst_os = OSFromDisk(instance.os)
1092 255dcebd Iustin Pop
1093 4a0e011f Iustin Pop
  create_env = OSEnvironment(instance, inst_os, debug)
1094 e557bae9 Guido Trotter
  if reinstall:
1095 d0c8c01d Iustin Pop
    create_env["INSTANCE_REINSTALL"] = "1"
1096 a8083063 Iustin Pop
1097 6aa7a354 Iustin Pop
  logfile = _InstanceLogName("add", instance.os, instance.name, None)
1098 decd5f45 Iustin Pop
1099 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.create_script], env=create_env,
1100 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1101 decd5f45 Iustin Pop
  if result.failed:
1102 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
1103 d868edb4 Iustin Pop
                  " output: %s", result.cmd, result.fail_reason, logfile,
1104 18682bca Iustin Pop
                  result.output)
1105 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1106 20e01edd Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1107 afdc3985 Iustin Pop
    _Fail("OS create script failed (%s), last lines in the"
1108 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1109 decd5f45 Iustin Pop
1110 decd5f45 Iustin Pop
1111 4a0e011f Iustin Pop
def RunRenameInstance(instance, old_name, debug):
1112 decd5f45 Iustin Pop
  """Run the OS rename script for an instance.
1113 decd5f45 Iustin Pop

1114 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1115 d15a9ad3 Guido Trotter
  @param instance: Instance whose OS is to be installed
1116 d15a9ad3 Guido Trotter
  @type old_name: string
1117 d15a9ad3 Guido Trotter
  @param old_name: previous instance name
1118 4a0e011f Iustin Pop
  @type debug: integer
1119 4a0e011f Iustin Pop
  @param debug: debug level, passed to the OS scripts
1120 10c2650b Iustin Pop
  @rtype: boolean
1121 10c2650b Iustin Pop
  @return: the success of the operation
1122 decd5f45 Iustin Pop

1123 decd5f45 Iustin Pop
  """
1124 decd5f45 Iustin Pop
  inst_os = OSFromDisk(instance.os)
1125 decd5f45 Iustin Pop
1126 4a0e011f Iustin Pop
  rename_env = OSEnvironment(instance, inst_os, debug)
1127 d0c8c01d Iustin Pop
  rename_env["OLD_INSTANCE_NAME"] = old_name
1128 decd5f45 Iustin Pop
1129 81a3406c Iustin Pop
  logfile = _InstanceLogName("rename", instance.os,
1130 6aa7a354 Iustin Pop
                             "%s-%s" % (old_name, instance.name), None)
1131 a8083063 Iustin Pop
1132 d868edb4 Iustin Pop
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1133 896a03f6 Iustin Pop
                        cwd=inst_os.path, output=logfile, reset_env=True)
1134 a8083063 Iustin Pop
1135 a8083063 Iustin Pop
  if result.failed:
1136 18682bca Iustin Pop
    logging.error("os create command '%s' returned error: %s output: %s",
1137 d868edb4 Iustin Pop
                  result.cmd, result.fail_reason, result.output)
1138 26f15862 Iustin Pop
    lines = [utils.SafeEncode(val)
1139 96841384 Iustin Pop
             for val in utils.TailFile(logfile, lines=20)]
1140 afdc3985 Iustin Pop
    _Fail("OS rename script failed (%s), last lines in the"
1141 afdc3985 Iustin Pop
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1142 a8083063 Iustin Pop
1143 a8083063 Iustin Pop
1144 5282084b Iustin Pop
def _GetBlockDevSymlinkPath(instance_name, idx):
1145 3536c792 Iustin Pop
  return utils.PathJoin(constants.DISK_LINKS_DIR, "%s%s%d" %
1146 3536c792 Iustin Pop
                        (instance_name, constants.DISK_SEPARATOR, idx))
1147 5282084b Iustin Pop
1148 5282084b Iustin Pop
1149 5282084b Iustin Pop
def _SymlinkBlockDev(instance_name, device_path, idx):
1150 9332fd8a Iustin Pop
  """Set up symlinks to a instance's block device.
1151 9332fd8a Iustin Pop

1152 9332fd8a Iustin Pop
  This is an auxiliary function run when an instance is start (on the primary
1153 9332fd8a Iustin Pop
  node) or when an instance is migrated (on the target node).
1154 9332fd8a Iustin Pop

1155 9332fd8a Iustin Pop

1156 5282084b Iustin Pop
  @param instance_name: the name of the target instance
1157 5282084b Iustin Pop
  @param device_path: path of the physical block device, on the node
1158 5282084b Iustin Pop
  @param idx: the disk index
1159 5282084b Iustin Pop
  @return: absolute path to the disk's symlink
1160 9332fd8a Iustin Pop

1161 9332fd8a Iustin Pop
  """
1162 5282084b Iustin Pop
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1163 9332fd8a Iustin Pop
  try:
1164 9332fd8a Iustin Pop
    os.symlink(device_path, link_name)
1165 5282084b Iustin Pop
  except OSError, err:
1166 5282084b Iustin Pop
    if err.errno == errno.EEXIST:
1167 9332fd8a Iustin Pop
      if (not os.path.islink(link_name) or
1168 9332fd8a Iustin Pop
          os.readlink(link_name) != device_path):
1169 9332fd8a Iustin Pop
        os.remove(link_name)
1170 9332fd8a Iustin Pop
        os.symlink(device_path, link_name)
1171 9332fd8a Iustin Pop
    else:
1172 9332fd8a Iustin Pop
      raise
1173 9332fd8a Iustin Pop
1174 9332fd8a Iustin Pop
  return link_name
1175 9332fd8a Iustin Pop
1176 9332fd8a Iustin Pop
1177 5282084b Iustin Pop
def _RemoveBlockDevLinks(instance_name, disks):
1178 3c9c571d Iustin Pop
  """Remove the block device symlinks belonging to the given instance.
1179 3c9c571d Iustin Pop

1180 3c9c571d Iustin Pop
  """
1181 29921401 Iustin Pop
  for idx, _ in enumerate(disks):
1182 5282084b Iustin Pop
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1183 5282084b Iustin Pop
    if os.path.islink(link_name):
1184 3c9c571d Iustin Pop
      try:
1185 03dfa658 Iustin Pop
        os.remove(link_name)
1186 03dfa658 Iustin Pop
      except OSError:
1187 03dfa658 Iustin Pop
        logging.exception("Can't remove symlink '%s'", link_name)
1188 3c9c571d Iustin Pop
1189 3c9c571d Iustin Pop
1190 9332fd8a Iustin Pop
def _GatherAndLinkBlockDevs(instance):
1191 a8083063 Iustin Pop
  """Set up an instance's block device(s).
1192 a8083063 Iustin Pop

1193 a8083063 Iustin Pop
  This is run on the primary node at instance startup. The block
1194 a8083063 Iustin Pop
  devices must be already assembled.
1195 a8083063 Iustin Pop

1196 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1197 10c2650b Iustin Pop
  @param instance: the instance whose disks we shoul assemble
1198 069cfbf1 Iustin Pop
  @rtype: list
1199 069cfbf1 Iustin Pop
  @return: list of (disk_object, device_path)
1200 10c2650b Iustin Pop

1201 a8083063 Iustin Pop
  """
1202 a8083063 Iustin Pop
  block_devices = []
1203 9332fd8a Iustin Pop
  for idx, disk in enumerate(instance.disks):
1204 a8083063 Iustin Pop
    device = _RecursiveFindBD(disk)
1205 a8083063 Iustin Pop
    if device is None:
1206 a8083063 Iustin Pop
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1207 a8083063 Iustin Pop
                                    str(disk))
1208 a8083063 Iustin Pop
    device.Open()
1209 9332fd8a Iustin Pop
    try:
1210 5282084b Iustin Pop
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1211 9332fd8a Iustin Pop
    except OSError, e:
1212 9332fd8a Iustin Pop
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1213 9332fd8a Iustin Pop
                                    e.strerror)
1214 9332fd8a Iustin Pop
1215 9332fd8a Iustin Pop
    block_devices.append((disk, link_name))
1216 9332fd8a Iustin Pop
1217 a8083063 Iustin Pop
  return block_devices
1218 a8083063 Iustin Pop
1219 a8083063 Iustin Pop
1220 323f9095 Stephen Shirley
def StartInstance(instance, startup_paused):
1221 a8083063 Iustin Pop
  """Start an instance.
1222 a8083063 Iustin Pop

1223 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1224 e69d05fd Iustin Pop
  @param instance: the instance object
1225 323f9095 Stephen Shirley
  @type startup_paused: bool
1226 323f9095 Stephen Shirley
  @param instance: pause instance at startup?
1227 c26a6bd2 Iustin Pop
  @rtype: None
1228 a8083063 Iustin Pop

1229 098c0958 Michael Hanselmann
  """
1230 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1231 a8083063 Iustin Pop
1232 a8083063 Iustin Pop
  if instance.name in running_instances:
1233 c26a6bd2 Iustin Pop
    logging.info("Instance %s already running, not starting", instance.name)
1234 c26a6bd2 Iustin Pop
    return
1235 a8083063 Iustin Pop
1236 a8083063 Iustin Pop
  try:
1237 ec596c24 Iustin Pop
    block_devices = _GatherAndLinkBlockDevs(instance)
1238 ec596c24 Iustin Pop
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1239 323f9095 Stephen Shirley
    hyper.StartInstance(instance, block_devices, startup_paused)
1240 ec596c24 Iustin Pop
  except errors.BlockDeviceError, err:
1241 2cc6781a Iustin Pop
    _Fail("Block device error: %s", err, exc=True)
1242 a8083063 Iustin Pop
  except errors.HypervisorError, err:
1243 5282084b Iustin Pop
    _RemoveBlockDevLinks(instance.name, instance.disks)
1244 2cc6781a Iustin Pop
    _Fail("Hypervisor error: %s", err, exc=True)
1245 a8083063 Iustin Pop
1246 a8083063 Iustin Pop
1247 6263189c Guido Trotter
def InstanceShutdown(instance, timeout):
1248 a8083063 Iustin Pop
  """Shut an instance down.
1249 a8083063 Iustin Pop

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

1252 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1253 e69d05fd Iustin Pop
  @param instance: the instance object
1254 6263189c Guido Trotter
  @type timeout: integer
1255 6263189c Guido Trotter
  @param timeout: maximum timeout for soft shutdown
1256 c26a6bd2 Iustin Pop
  @rtype: None
1257 a8083063 Iustin Pop

1258 098c0958 Michael Hanselmann
  """
1259 e69d05fd Iustin Pop
  hv_name = instance.hypervisor
1260 e4e9b806 Guido Trotter
  hyper = hypervisor.GetHypervisor(hv_name)
1261 c26a6bd2 Iustin Pop
  iname = instance.name
1262 a8083063 Iustin Pop
1263 3c0cdc83 Michael Hanselmann
  if instance.name not in hyper.ListInstances():
1264 c26a6bd2 Iustin Pop
    logging.info("Instance %s not running, doing nothing", iname)
1265 c26a6bd2 Iustin Pop
    return
1266 a8083063 Iustin Pop
1267 3c0cdc83 Michael Hanselmann
  class _TryShutdown:
1268 3c0cdc83 Michael Hanselmann
    def __init__(self):
1269 3c0cdc83 Michael Hanselmann
      self.tried_once = False
1270 a8083063 Iustin Pop
1271 3c0cdc83 Michael Hanselmann
    def __call__(self):
1272 3c0cdc83 Michael Hanselmann
      if iname not in hyper.ListInstances():
1273 3c0cdc83 Michael Hanselmann
        return
1274 3c0cdc83 Michael Hanselmann
1275 3c0cdc83 Michael Hanselmann
      try:
1276 3c0cdc83 Michael Hanselmann
        hyper.StopInstance(instance, retry=self.tried_once)
1277 3c0cdc83 Michael Hanselmann
      except errors.HypervisorError, err:
1278 3c0cdc83 Michael Hanselmann
        if iname not in hyper.ListInstances():
1279 3c0cdc83 Michael Hanselmann
          # if the instance is no longer existing, consider this a
1280 3c0cdc83 Michael Hanselmann
          # success and go to cleanup
1281 3c0cdc83 Michael Hanselmann
          return
1282 3c0cdc83 Michael Hanselmann
1283 3c0cdc83 Michael Hanselmann
        _Fail("Failed to stop instance %s: %s", iname, err)
1284 3c0cdc83 Michael Hanselmann
1285 3c0cdc83 Michael Hanselmann
      self.tried_once = True
1286 3c0cdc83 Michael Hanselmann
1287 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
1288 3c0cdc83 Michael Hanselmann
1289 3c0cdc83 Michael Hanselmann
  try:
1290 3c0cdc83 Michael Hanselmann
    utils.Retry(_TryShutdown(), 5, timeout)
1291 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
1292 a8083063 Iustin Pop
    # the shutdown did not succeed
1293 e4e9b806 Guido Trotter
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1294 a8083063 Iustin Pop
1295 a8083063 Iustin Pop
    try:
1296 a8083063 Iustin Pop
      hyper.StopInstance(instance, force=True)
1297 a8083063 Iustin Pop
    except errors.HypervisorError, err:
1298 3c0cdc83 Michael Hanselmann
      if iname in hyper.ListInstances():
1299 3782acd7 Iustin Pop
        # only raise an error if the instance still exists, otherwise
1300 3782acd7 Iustin Pop
        # the error could simply be "instance ... unknown"!
1301 3782acd7 Iustin Pop
        _Fail("Failed to force stop instance %s: %s", iname, err)
1302 a8083063 Iustin Pop
1303 a8083063 Iustin Pop
    time.sleep(1)
1304 3c0cdc83 Michael Hanselmann
1305 3c0cdc83 Michael Hanselmann
    if iname in hyper.ListInstances():
1306 c26a6bd2 Iustin Pop
      _Fail("Could not shutdown instance %s even by destroy", iname)
1307 3c9c571d Iustin Pop
1308 f28ec899 Guido Trotter
  try:
1309 f28ec899 Guido Trotter
    hyper.CleanupInstance(instance.name)
1310 f28ec899 Guido Trotter
  except errors.HypervisorError, err:
1311 f28ec899 Guido Trotter
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1312 f28ec899 Guido Trotter
1313 c26a6bd2 Iustin Pop
  _RemoveBlockDevLinks(iname, instance.disks)
1314 a8083063 Iustin Pop
1315 a8083063 Iustin Pop
1316 17c3f802 Guido Trotter
def InstanceReboot(instance, reboot_type, shutdown_timeout):
1317 007a2f3e Alexander Schreiber
  """Reboot an instance.
1318 007a2f3e Alexander Schreiber

1319 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
1320 10c2650b Iustin Pop
  @param instance: the instance object to reboot
1321 10c2650b Iustin Pop
  @type reboot_type: str
1322 10c2650b Iustin Pop
  @param reboot_type: the type of reboot, one the following
1323 10c2650b Iustin Pop
    constants:
1324 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1325 10c2650b Iustin Pop
        instance OS, do not recreate the VM
1326 10c2650b Iustin Pop
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1327 10c2650b Iustin Pop
        restart the VM (at the hypervisor level)
1328 73e5a4f4 Iustin Pop
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1329 73e5a4f4 Iustin Pop
        not accepted here, since that mode is handled differently, in
1330 73e5a4f4 Iustin Pop
        cmdlib, and translates into full stop and start of the
1331 73e5a4f4 Iustin Pop
        instance (instead of a call_instance_reboot RPC)
1332 23057d29 Michael Hanselmann
  @type shutdown_timeout: integer
1333 23057d29 Michael Hanselmann
  @param shutdown_timeout: maximum timeout for soft shutdown
1334 c26a6bd2 Iustin Pop
  @rtype: None
1335 007a2f3e Alexander Schreiber

1336 007a2f3e Alexander Schreiber
  """
1337 e69d05fd Iustin Pop
  running_instances = GetInstanceList([instance.hypervisor])
1338 007a2f3e Alexander Schreiber
1339 007a2f3e Alexander Schreiber
  if instance.name not in running_instances:
1340 2cc6781a Iustin Pop
    _Fail("Cannot reboot instance %s that is not running", instance.name)
1341 007a2f3e Alexander Schreiber
1342 e69d05fd Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1343 007a2f3e Alexander Schreiber
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1344 007a2f3e Alexander Schreiber
    try:
1345 007a2f3e Alexander Schreiber
      hyper.RebootInstance(instance)
1346 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1347 2cc6781a Iustin Pop
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1348 007a2f3e Alexander Schreiber
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1349 007a2f3e Alexander Schreiber
    try:
1350 17c3f802 Guido Trotter
      InstanceShutdown(instance, shutdown_timeout)
1351 82bc21e2 Stephen Shirley
      return StartInstance(instance, False)
1352 007a2f3e Alexander Schreiber
    except errors.HypervisorError, err:
1353 2cc6781a Iustin Pop
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1354 007a2f3e Alexander Schreiber
  else:
1355 2cc6781a Iustin Pop
    _Fail("Invalid reboot_type received: %s", reboot_type)
1356 007a2f3e Alexander Schreiber
1357 007a2f3e Alexander Schreiber
1358 6906a9d8 Guido Trotter
def MigrationInfo(instance):
1359 6906a9d8 Guido Trotter
  """Gather information about an instance to be migrated.
1360 6906a9d8 Guido Trotter

1361 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1362 6906a9d8 Guido Trotter
  @param instance: the instance definition
1363 6906a9d8 Guido Trotter

1364 6906a9d8 Guido Trotter
  """
1365 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1366 cd42d0ad Guido Trotter
  try:
1367 cd42d0ad Guido Trotter
    info = hyper.MigrationInfo(instance)
1368 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1369 2cc6781a Iustin Pop
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1370 c26a6bd2 Iustin Pop
  return info
1371 6906a9d8 Guido Trotter
1372 6906a9d8 Guido Trotter
1373 6906a9d8 Guido Trotter
def AcceptInstance(instance, info, target):
1374 6906a9d8 Guido Trotter
  """Prepare the node to accept an instance.
1375 6906a9d8 Guido Trotter

1376 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1377 6906a9d8 Guido Trotter
  @param instance: the instance definition
1378 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1379 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1380 6906a9d8 Guido Trotter
  @type target: string
1381 6906a9d8 Guido Trotter
  @param target: target host (usually ip), on this node
1382 6906a9d8 Guido Trotter

1383 6906a9d8 Guido Trotter
  """
1384 77fcff4a Apollon Oikonomopoulos
  # TODO: why is this required only for DTS_EXT_MIRROR?
1385 77fcff4a Apollon Oikonomopoulos
  if instance.disk_template in constants.DTS_EXT_MIRROR:
1386 77fcff4a Apollon Oikonomopoulos
    # Create the symlinks, as the disks are not active
1387 77fcff4a Apollon Oikonomopoulos
    # in any way
1388 77fcff4a Apollon Oikonomopoulos
    try:
1389 77fcff4a Apollon Oikonomopoulos
      _GatherAndLinkBlockDevs(instance)
1390 77fcff4a Apollon Oikonomopoulos
    except errors.BlockDeviceError, err:
1391 77fcff4a Apollon Oikonomopoulos
      _Fail("Block device error: %s", err, exc=True)
1392 77fcff4a Apollon Oikonomopoulos
1393 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1394 cd42d0ad Guido Trotter
  try:
1395 cd42d0ad Guido Trotter
    hyper.AcceptInstance(instance, info, target)
1396 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1397 77fcff4a Apollon Oikonomopoulos
    if instance.disk_template in constants.DTS_EXT_MIRROR:
1398 77fcff4a Apollon Oikonomopoulos
      _RemoveBlockDevLinks(instance.name, instance.disks)
1399 2cc6781a Iustin Pop
    _Fail("Failed to accept instance: %s", err, exc=True)
1400 6906a9d8 Guido Trotter
1401 6906a9d8 Guido Trotter
1402 6a1434d7 Andrea Spadaccini
def FinalizeMigrationDst(instance, info, success):
1403 6906a9d8 Guido Trotter
  """Finalize any preparation to accept an instance.
1404 6906a9d8 Guido Trotter

1405 6906a9d8 Guido Trotter
  @type instance: L{objects.Instance}
1406 6906a9d8 Guido Trotter
  @param instance: the instance definition
1407 6906a9d8 Guido Trotter
  @type info: string/data (opaque)
1408 6906a9d8 Guido Trotter
  @param info: migration information, from the source node
1409 6906a9d8 Guido Trotter
  @type success: boolean
1410 6906a9d8 Guido Trotter
  @param success: whether the migration was a success or a failure
1411 6906a9d8 Guido Trotter

1412 6906a9d8 Guido Trotter
  """
1413 cd42d0ad Guido Trotter
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1414 cd42d0ad Guido Trotter
  try:
1415 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationDst(instance, info, success)
1416 cd42d0ad Guido Trotter
  except errors.HypervisorError, err:
1417 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize migration on the target node: %s", err, exc=True)
1418 6906a9d8 Guido Trotter
1419 6906a9d8 Guido Trotter
1420 2a10865c Iustin Pop
def MigrateInstance(instance, target, live):
1421 2a10865c Iustin Pop
  """Migrates an instance to another node.
1422 2a10865c Iustin Pop

1423 b1206984 Iustin Pop
  @type instance: L{objects.Instance}
1424 9f0e6b37 Iustin Pop
  @param instance: the instance definition
1425 9f0e6b37 Iustin Pop
  @type target: string
1426 9f0e6b37 Iustin Pop
  @param target: the target node name
1427 9f0e6b37 Iustin Pop
  @type live: boolean
1428 9f0e6b37 Iustin Pop
  @param live: whether the migration should be done live or not (the
1429 9f0e6b37 Iustin Pop
      interpretation of this parameter is left to the hypervisor)
1430 c03fe62b Andrea Spadaccini
  @raise RPCFail: if migration fails for some reason
1431 9f0e6b37 Iustin Pop

1432 2a10865c Iustin Pop
  """
1433 53c776b5 Iustin Pop
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1434 2a10865c Iustin Pop
1435 2a10865c Iustin Pop
  try:
1436 58d38b02 Iustin Pop
    hyper.MigrateInstance(instance, target, live)
1437 2a10865c Iustin Pop
  except errors.HypervisorError, err:
1438 2cc6781a Iustin Pop
    _Fail("Failed to migrate instance: %s", err, exc=True)
1439 2a10865c Iustin Pop
1440 2a10865c Iustin Pop
1441 6a1434d7 Andrea Spadaccini
def FinalizeMigrationSource(instance, success, live):
1442 6a1434d7 Andrea Spadaccini
  """Finalize the instance migration on the source node.
1443 6a1434d7 Andrea Spadaccini

1444 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1445 6a1434d7 Andrea Spadaccini
  @param instance: the instance definition of the migrated instance
1446 6a1434d7 Andrea Spadaccini
  @type success: bool
1447 6a1434d7 Andrea Spadaccini
  @param success: whether the migration succeeded or not
1448 6a1434d7 Andrea Spadaccini
  @type live: bool
1449 6a1434d7 Andrea Spadaccini
  @param live: whether the user requested a live migration or not
1450 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the execution fails for some reason
1451 6a1434d7 Andrea Spadaccini

1452 6a1434d7 Andrea Spadaccini
  """
1453 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1454 6a1434d7 Andrea Spadaccini
1455 6a1434d7 Andrea Spadaccini
  try:
1456 6a1434d7 Andrea Spadaccini
    hyper.FinalizeMigrationSource(instance, success, live)
1457 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1458 6a1434d7 Andrea Spadaccini
    _Fail("Failed to finalize the migration on the source node: %s", err,
1459 6a1434d7 Andrea Spadaccini
          exc=True)
1460 6a1434d7 Andrea Spadaccini
1461 6a1434d7 Andrea Spadaccini
1462 6a1434d7 Andrea Spadaccini
def GetMigrationStatus(instance):
1463 6a1434d7 Andrea Spadaccini
  """Get the migration status
1464 6a1434d7 Andrea Spadaccini

1465 6a1434d7 Andrea Spadaccini
  @type instance: L{objects.Instance}
1466 6a1434d7 Andrea Spadaccini
  @param instance: the instance that is being migrated
1467 6a1434d7 Andrea Spadaccini
  @rtype: L{objects.MigrationStatus}
1468 6a1434d7 Andrea Spadaccini
  @return: the status of the current migration (one of
1469 6a1434d7 Andrea Spadaccini
           L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
1470 6a1434d7 Andrea Spadaccini
           progress info that can be retrieved from the hypervisor
1471 6a1434d7 Andrea Spadaccini
  @raise RPCFail: If the migration status cannot be retrieved
1472 6a1434d7 Andrea Spadaccini

1473 6a1434d7 Andrea Spadaccini
  """
1474 6a1434d7 Andrea Spadaccini
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1475 6a1434d7 Andrea Spadaccini
  try:
1476 6a1434d7 Andrea Spadaccini
    return hyper.GetMigrationStatus(instance)
1477 6a1434d7 Andrea Spadaccini
  except Exception, err:  # pylint: disable=W0703
1478 6a1434d7 Andrea Spadaccini
    _Fail("Failed to get migration status: %s", err, exc=True)
1479 6a1434d7 Andrea Spadaccini
1480 6a1434d7 Andrea Spadaccini
1481 821d1bd1 Iustin Pop
def BlockdevCreate(disk, size, owner, on_primary, info):
1482 a8083063 Iustin Pop
  """Creates a block device for an instance.
1483 a8083063 Iustin Pop

1484 b1206984 Iustin Pop
  @type disk: L{objects.Disk}
1485 b1206984 Iustin Pop
  @param disk: the object describing the disk we should create
1486 b1206984 Iustin Pop
  @type size: int
1487 b1206984 Iustin Pop
  @param size: the size of the physical underlying device, in MiB
1488 b1206984 Iustin Pop
  @type owner: str
1489 b1206984 Iustin Pop
  @param owner: the name of the instance for which disk is created,
1490 b1206984 Iustin Pop
      used for device cache data
1491 b1206984 Iustin Pop
  @type on_primary: boolean
1492 b1206984 Iustin Pop
  @param on_primary:  indicates if it is the primary node or not
1493 b1206984 Iustin Pop
  @type info: string
1494 b1206984 Iustin Pop
  @param info: string that will be sent to the physical device
1495 b1206984 Iustin Pop
      creation, used for example to set (LVM) tags on LVs
1496 b1206984 Iustin Pop

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

1501 a8083063 Iustin Pop
  """
1502 d0c8c01d Iustin Pop
  # TODO: remove the obsolete "size" argument
1503 b459a848 Andrea Spadaccini
  # pylint: disable=W0613
1504 a8083063 Iustin Pop
  clist = []
1505 a8083063 Iustin Pop
  if disk.children:
1506 a8083063 Iustin Pop
    for child in disk.children:
1507 1063abd1 Iustin Pop
      try:
1508 1063abd1 Iustin Pop
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1509 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1510 2cc6781a Iustin Pop
        _Fail("Can't assemble device %s: %s", child, err)
1511 a8083063 Iustin Pop
      if on_primary or disk.AssembleOnSecondary():
1512 a8083063 Iustin Pop
        # we need the children open in case the device itself has to
1513 a8083063 Iustin Pop
        # be assembled
1514 1063abd1 Iustin Pop
        try:
1515 b459a848 Andrea Spadaccini
          # pylint: disable=E1103
1516 1063abd1 Iustin Pop
          crdev.Open()
1517 1063abd1 Iustin Pop
        except errors.BlockDeviceError, err:
1518 2cc6781a Iustin Pop
          _Fail("Can't make child '%s' read-write: %s", child, err)
1519 a8083063 Iustin Pop
      clist.append(crdev)
1520 a8083063 Iustin Pop
1521 dab69e97 Iustin Pop
  try:
1522 464f8daf Iustin Pop
    device = bdev.Create(disk.dev_type, disk.physical_id, clist, disk.size)
1523 1063abd1 Iustin Pop
  except errors.BlockDeviceError, err:
1524 2cc6781a Iustin Pop
    _Fail("Can't create block device: %s", err)
1525 6c626518 Iustin Pop
1526 a8083063 Iustin Pop
  if on_primary or disk.AssembleOnSecondary():
1527 1063abd1 Iustin Pop
    try:
1528 1063abd1 Iustin Pop
      device.Assemble()
1529 1063abd1 Iustin Pop
    except errors.BlockDeviceError, err:
1530 2cc6781a Iustin Pop
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1531 e31c43f7 Michael Hanselmann
    device.SetSyncSpeed(constants.SYNC_SPEED)
1532 a8083063 Iustin Pop
    if on_primary or disk.OpenOnSecondary():
1533 1063abd1 Iustin Pop
      try:
1534 1063abd1 Iustin Pop
        device.Open(force=True)
1535 1063abd1 Iustin Pop
      except errors.BlockDeviceError, err:
1536 2cc6781a Iustin Pop
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1537 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(device.dev_path, owner,
1538 3f78eef2 Iustin Pop
                                on_primary, disk.iv_name)
1539 a0c3fea1 Michael Hanselmann
1540 a0c3fea1 Michael Hanselmann
  device.SetInfo(info)
1541 a0c3fea1 Michael Hanselmann
1542 c26a6bd2 Iustin Pop
  return device.unique_id
1543 a8083063 Iustin Pop
1544 a8083063 Iustin Pop
1545 da63bb4e René Nussbaumer
def _WipeDevice(path, offset, size):
1546 69dd363f René Nussbaumer
  """This function actually wipes the device.
1547 69dd363f René Nussbaumer

1548 69dd363f René Nussbaumer
  @param path: The path to the device to wipe
1549 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1550 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1551 69dd363f René Nussbaumer

1552 69dd363f René Nussbaumer
  """
1553 da63bb4e René Nussbaumer
  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1554 da63bb4e René Nussbaumer
         "bs=%d" % constants.WIPE_BLOCK_SIZE, "oflag=direct", "of=%s" % path,
1555 da63bb4e René Nussbaumer
         "count=%d" % size]
1556 da63bb4e René Nussbaumer
  result = utils.RunCmd(cmd)
1557 69dd363f René Nussbaumer
1558 69dd363f René Nussbaumer
  if result.failed:
1559 69dd363f René Nussbaumer
    _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1560 69dd363f René Nussbaumer
          result.fail_reason, result.output)
1561 69dd363f René Nussbaumer
1562 69dd363f René Nussbaumer
1563 da63bb4e René Nussbaumer
def BlockdevWipe(disk, offset, size):
1564 69dd363f René Nussbaumer
  """Wipes a block device.
1565 69dd363f René Nussbaumer

1566 69dd363f René Nussbaumer
  @type disk: L{objects.Disk}
1567 69dd363f René Nussbaumer
  @param disk: the disk object we want to wipe
1568 da63bb4e René Nussbaumer
  @type offset: int
1569 da63bb4e René Nussbaumer
  @param offset: The offset in MiB in the file
1570 da63bb4e René Nussbaumer
  @type size: int
1571 da63bb4e René Nussbaumer
  @param size: The size in MiB to write
1572 69dd363f René Nussbaumer

1573 69dd363f René Nussbaumer
  """
1574 69dd363f René Nussbaumer
  try:
1575 69dd363f René Nussbaumer
    rdev = _RecursiveFindBD(disk)
1576 da63bb4e René Nussbaumer
  except errors.BlockDeviceError:
1577 da63bb4e René Nussbaumer
    rdev = None
1578 da63bb4e René Nussbaumer
1579 da63bb4e René Nussbaumer
  if not rdev:
1580 da63bb4e René Nussbaumer
    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1581 da63bb4e René Nussbaumer
1582 da63bb4e René Nussbaumer
  # Do cross verify some of the parameters
1583 da63bb4e René Nussbaumer
  if offset > rdev.size:
1584 da63bb4e René Nussbaumer
    _Fail("Offset is bigger than device size")
1585 da63bb4e René Nussbaumer
  if (offset + size) > rdev.size:
1586 da63bb4e René Nussbaumer
    _Fail("The provided offset and size to wipe is bigger than device size")
1587 69dd363f René Nussbaumer
1588 da63bb4e René Nussbaumer
  _WipeDevice(rdev.dev_path, offset, size)
1589 69dd363f René Nussbaumer
1590 69dd363f René Nussbaumer
1591 5119c79e René Nussbaumer
def BlockdevPauseResumeSync(disks, pause):
1592 5119c79e René Nussbaumer
  """Pause or resume the sync of the block device.
1593 5119c79e René Nussbaumer

1594 0f39886a René Nussbaumer
  @type disks: list of L{objects.Disk}
1595 0f39886a René Nussbaumer
  @param disks: the disks object we want to pause/resume
1596 5119c79e René Nussbaumer
  @type pause: bool
1597 5119c79e René Nussbaumer
  @param pause: Wheater to pause or resume
1598 5119c79e René Nussbaumer

1599 5119c79e René Nussbaumer
  """
1600 5119c79e René Nussbaumer
  success = []
1601 5119c79e René Nussbaumer
  for disk in disks:
1602 5119c79e René Nussbaumer
    try:
1603 5119c79e René Nussbaumer
      rdev = _RecursiveFindBD(disk)
1604 5119c79e René Nussbaumer
    except errors.BlockDeviceError:
1605 5119c79e René Nussbaumer
      rdev = None
1606 5119c79e René Nussbaumer
1607 5119c79e René Nussbaumer
    if not rdev:
1608 5119c79e René Nussbaumer
      success.append((False, ("Cannot change sync for device %s:"
1609 5119c79e René Nussbaumer
                              " device not found" % disk.iv_name)))
1610 5119c79e René Nussbaumer
      continue
1611 5119c79e René Nussbaumer
1612 5119c79e René Nussbaumer
    result = rdev.PauseResumeSync(pause)
1613 5119c79e René Nussbaumer
1614 5119c79e René Nussbaumer
    if result:
1615 5119c79e René Nussbaumer
      success.append((result, None))
1616 5119c79e René Nussbaumer
    else:
1617 5119c79e René Nussbaumer
      if pause:
1618 5119c79e René Nussbaumer
        msg = "Pause"
1619 5119c79e René Nussbaumer
      else:
1620 5119c79e René Nussbaumer
        msg = "Resume"
1621 5119c79e René Nussbaumer
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
1622 5119c79e René Nussbaumer
1623 5119c79e René Nussbaumer
  return success
1624 5119c79e René Nussbaumer
1625 5119c79e René Nussbaumer
1626 821d1bd1 Iustin Pop
def BlockdevRemove(disk):
1627 a8083063 Iustin Pop
  """Remove a block device.
1628 a8083063 Iustin Pop

1629 10c2650b Iustin Pop
  @note: This is intended to be called recursively.
1630 10c2650b Iustin Pop

1631 c41eea6e Iustin Pop
  @type disk: L{objects.Disk}
1632 10c2650b Iustin Pop
  @param disk: the disk object we should remove
1633 10c2650b Iustin Pop
  @rtype: boolean
1634 10c2650b Iustin Pop
  @return: the success of the operation
1635 a8083063 Iustin Pop

1636 a8083063 Iustin Pop
  """
1637 e1bc0878 Iustin Pop
  msgs = []
1638 a8083063 Iustin Pop
  try:
1639 bca2e7f4 Iustin Pop
    rdev = _RecursiveFindBD(disk)
1640 a8083063 Iustin Pop
  except errors.BlockDeviceError, err:
1641 a8083063 Iustin Pop
    # probably can't attach
1642 18682bca Iustin Pop
    logging.info("Can't attach to device %s in remove", disk)
1643 a8083063 Iustin Pop
    rdev = None
1644 a8083063 Iustin Pop
  if rdev is not None:
1645 3f78eef2 Iustin Pop
    r_path = rdev.dev_path
1646 e1bc0878 Iustin Pop
    try:
1647 0c6c04ec Iustin Pop
      rdev.Remove()
1648 e1bc0878 Iustin Pop
    except errors.BlockDeviceError, err:
1649 e1bc0878 Iustin Pop
      msgs.append(str(err))
1650 c26a6bd2 Iustin Pop
    if not msgs:
1651 3f78eef2 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
1652 e1bc0878 Iustin Pop
1653 a8083063 Iustin Pop
  if disk.children:
1654 a8083063 Iustin Pop
    for child in disk.children:
1655 c26a6bd2 Iustin Pop
      try:
1656 c26a6bd2 Iustin Pop
        BlockdevRemove(child)
1657 c26a6bd2 Iustin Pop
      except RPCFail, err:
1658 c26a6bd2 Iustin Pop
        msgs.append(str(err))
1659 e1bc0878 Iustin Pop
1660 c26a6bd2 Iustin Pop
  if msgs:
1661 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1662 afdc3985 Iustin Pop
1663 a8083063 Iustin Pop
1664 3f78eef2 Iustin Pop
def _RecursiveAssembleBD(disk, owner, as_primary):
1665 a8083063 Iustin Pop
  """Activate a block device for an instance.
1666 a8083063 Iustin Pop

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

1669 10c2650b Iustin Pop
  @note: this function is called recursively.
1670 a8083063 Iustin Pop

1671 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1672 10c2650b Iustin Pop
  @param disk: the disk we try to assemble
1673 10c2650b Iustin Pop
  @type owner: str
1674 10c2650b Iustin Pop
  @param owner: the name of the instance which owns the disk
1675 10c2650b Iustin Pop
  @type as_primary: boolean
1676 10c2650b Iustin Pop
  @param as_primary: if we should make the block device
1677 10c2650b Iustin Pop
      read/write
1678 a8083063 Iustin Pop

1679 10c2650b Iustin Pop
  @return: the assembled device or None (in case no device
1680 10c2650b Iustin Pop
      was assembled)
1681 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: in case there is an error
1682 10c2650b Iustin Pop
      during the activation of the children or the device
1683 10c2650b Iustin Pop
      itself
1684 a8083063 Iustin Pop

1685 a8083063 Iustin Pop
  """
1686 a8083063 Iustin Pop
  children = []
1687 a8083063 Iustin Pop
  if disk.children:
1688 fc1dc9d7 Iustin Pop
    mcn = disk.ChildrenNeeded()
1689 fc1dc9d7 Iustin Pop
    if mcn == -1:
1690 fc1dc9d7 Iustin Pop
      mcn = 0 # max number of Nones allowed
1691 fc1dc9d7 Iustin Pop
    else:
1692 fc1dc9d7 Iustin Pop
      mcn = len(disk.children) - mcn # max number of Nones
1693 a8083063 Iustin Pop
    for chld_disk in disk.children:
1694 fc1dc9d7 Iustin Pop
      try:
1695 fc1dc9d7 Iustin Pop
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1696 fc1dc9d7 Iustin Pop
      except errors.BlockDeviceError, err:
1697 7803d4d3 Iustin Pop
        if children.count(None) >= mcn:
1698 fc1dc9d7 Iustin Pop
          raise
1699 fc1dc9d7 Iustin Pop
        cdev = None
1700 1063abd1 Iustin Pop
        logging.error("Error in child activation (but continuing): %s",
1701 1063abd1 Iustin Pop
                      str(err))
1702 fc1dc9d7 Iustin Pop
      children.append(cdev)
1703 a8083063 Iustin Pop
1704 a8083063 Iustin Pop
  if as_primary or disk.AssembleOnSecondary():
1705 464f8daf Iustin Pop
    r_dev = bdev.Assemble(disk.dev_type, disk.physical_id, children, disk.size)
1706 e31c43f7 Michael Hanselmann
    r_dev.SetSyncSpeed(constants.SYNC_SPEED)
1707 a8083063 Iustin Pop
    result = r_dev
1708 a8083063 Iustin Pop
    if as_primary or disk.OpenOnSecondary():
1709 a8083063 Iustin Pop
      r_dev.Open()
1710 3f78eef2 Iustin Pop
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1711 3f78eef2 Iustin Pop
                                as_primary, disk.iv_name)
1712 3f78eef2 Iustin Pop
1713 a8083063 Iustin Pop
  else:
1714 a8083063 Iustin Pop
    result = True
1715 a8083063 Iustin Pop
  return result
1716 a8083063 Iustin Pop
1717 a8083063 Iustin Pop
1718 c417e115 Iustin Pop
def BlockdevAssemble(disk, owner, as_primary, idx):
1719 a8083063 Iustin Pop
  """Activate a block device for an instance.
1720 a8083063 Iustin Pop

1721 a8083063 Iustin Pop
  This is a wrapper over _RecursiveAssembleBD.
1722 a8083063 Iustin Pop

1723 b1206984 Iustin Pop
  @rtype: str or boolean
1724 b1206984 Iustin Pop
  @return: a C{/dev/...} path for primary nodes, and
1725 b1206984 Iustin Pop
      C{True} for secondary nodes
1726 a8083063 Iustin Pop

1727 a8083063 Iustin Pop
  """
1728 53c14ef1 Iustin Pop
  try:
1729 53c14ef1 Iustin Pop
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1730 53c14ef1 Iustin Pop
    if isinstance(result, bdev.BlockDev):
1731 b459a848 Andrea Spadaccini
      # pylint: disable=E1103
1732 53c14ef1 Iustin Pop
      result = result.dev_path
1733 c417e115 Iustin Pop
      if as_primary:
1734 c417e115 Iustin Pop
        _SymlinkBlockDev(owner, result, idx)
1735 53c14ef1 Iustin Pop
  except errors.BlockDeviceError, err:
1736 afdc3985 Iustin Pop
    _Fail("Error while assembling disk: %s", err, exc=True)
1737 c417e115 Iustin Pop
  except OSError, err:
1738 c417e115 Iustin Pop
    _Fail("Error while symlinking disk: %s", err, exc=True)
1739 afdc3985 Iustin Pop
1740 c26a6bd2 Iustin Pop
  return result
1741 a8083063 Iustin Pop
1742 a8083063 Iustin Pop
1743 821d1bd1 Iustin Pop
def BlockdevShutdown(disk):
1744 a8083063 Iustin Pop
  """Shut down a block device.
1745 a8083063 Iustin Pop

1746 5bbd3f7f Michael Hanselmann
  First, if the device is assembled (Attach() is successful), then
1747 c41eea6e Iustin Pop
  the device is shutdown. Then the children of the device are
1748 c41eea6e Iustin Pop
  shutdown.
1749 a8083063 Iustin Pop

1750 a8083063 Iustin Pop
  This function is called recursively. Note that we don't cache the
1751 a8083063 Iustin Pop
  children or such, as oppossed to assemble, shutdown of different
1752 a8083063 Iustin Pop
  devices doesn't require that the upper device was active.
1753 a8083063 Iustin Pop

1754 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1755 10c2650b Iustin Pop
  @param disk: the description of the disk we should
1756 10c2650b Iustin Pop
      shutdown
1757 c26a6bd2 Iustin Pop
  @rtype: None
1758 10c2650b Iustin Pop

1759 a8083063 Iustin Pop
  """
1760 cacfd1fd Iustin Pop
  msgs = []
1761 a8083063 Iustin Pop
  r_dev = _RecursiveFindBD(disk)
1762 a8083063 Iustin Pop
  if r_dev is not None:
1763 3f78eef2 Iustin Pop
    r_path = r_dev.dev_path
1764 cacfd1fd Iustin Pop
    try:
1765 746f7476 Iustin Pop
      r_dev.Shutdown()
1766 746f7476 Iustin Pop
      DevCacheManager.RemoveCache(r_path)
1767 cacfd1fd Iustin Pop
    except errors.BlockDeviceError, err:
1768 cacfd1fd Iustin Pop
      msgs.append(str(err))
1769 746f7476 Iustin Pop
1770 a8083063 Iustin Pop
  if disk.children:
1771 a8083063 Iustin Pop
    for child in disk.children:
1772 c26a6bd2 Iustin Pop
      try:
1773 c26a6bd2 Iustin Pop
        BlockdevShutdown(child)
1774 c26a6bd2 Iustin Pop
      except RPCFail, err:
1775 c26a6bd2 Iustin Pop
        msgs.append(str(err))
1776 746f7476 Iustin Pop
1777 c26a6bd2 Iustin Pop
  if msgs:
1778 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
1779 a8083063 Iustin Pop
1780 a8083063 Iustin Pop
1781 821d1bd1 Iustin Pop
def BlockdevAddchildren(parent_cdev, new_cdevs):
1782 153d9724 Iustin Pop
  """Extend a mirrored block device.
1783 a8083063 Iustin Pop

1784 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1785 10c2650b Iustin Pop
  @param parent_cdev: the disk to which we should add children
1786 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1787 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should add
1788 c26a6bd2 Iustin Pop
  @rtype: None
1789 10c2650b Iustin Pop

1790 a8083063 Iustin Pop
  """
1791 bca2e7f4 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
1792 153d9724 Iustin Pop
  if parent_bdev is None:
1793 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
1794 153d9724 Iustin Pop
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1795 153d9724 Iustin Pop
  if new_bdevs.count(None) > 0:
1796 2cc6781a Iustin Pop
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
1797 153d9724 Iustin Pop
  parent_bdev.AddChildren(new_bdevs)
1798 a8083063 Iustin Pop
1799 a8083063 Iustin Pop
1800 821d1bd1 Iustin Pop
def BlockdevRemovechildren(parent_cdev, new_cdevs):
1801 153d9724 Iustin Pop
  """Shrink a mirrored block device.
1802 a8083063 Iustin Pop

1803 10c2650b Iustin Pop
  @type parent_cdev: L{objects.Disk}
1804 10c2650b Iustin Pop
  @param parent_cdev: the disk from which we should remove children
1805 10c2650b Iustin Pop
  @type new_cdevs: list of L{objects.Disk}
1806 10c2650b Iustin Pop
  @param new_cdevs: the list of children which we should remove
1807 c26a6bd2 Iustin Pop
  @rtype: None
1808 10c2650b Iustin Pop

1809 a8083063 Iustin Pop
  """
1810 153d9724 Iustin Pop
  parent_bdev = _RecursiveFindBD(parent_cdev)
1811 153d9724 Iustin Pop
  if parent_bdev is None:
1812 2cc6781a Iustin Pop
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
1813 e739bd57 Iustin Pop
  devs = []
1814 e739bd57 Iustin Pop
  for disk in new_cdevs:
1815 e739bd57 Iustin Pop
    rpath = disk.StaticDevPath()
1816 e739bd57 Iustin Pop
    if rpath is None:
1817 e739bd57 Iustin Pop
      bd = _RecursiveFindBD(disk)
1818 e739bd57 Iustin Pop
      if bd is None:
1819 2cc6781a Iustin Pop
        _Fail("Can't find device %s while removing children", disk)
1820 e739bd57 Iustin Pop
      else:
1821 e739bd57 Iustin Pop
        devs.append(bd.dev_path)
1822 e739bd57 Iustin Pop
    else:
1823 e51db2a6 Iustin Pop
      if not utils.IsNormAbsPath(rpath):
1824 e51db2a6 Iustin Pop
        _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
1825 e739bd57 Iustin Pop
      devs.append(rpath)
1826 e739bd57 Iustin Pop
  parent_bdev.RemoveChildren(devs)
1827 a8083063 Iustin Pop
1828 a8083063 Iustin Pop
1829 821d1bd1 Iustin Pop
def BlockdevGetmirrorstatus(disks):
1830 a8083063 Iustin Pop
  """Get the mirroring status of a list of devices.
1831 a8083063 Iustin Pop

1832 10c2650b Iustin Pop
  @type disks: list of L{objects.Disk}
1833 10c2650b Iustin Pop
  @param disks: the list of disks which we should query
1834 10c2650b Iustin Pop
  @rtype: disk
1835 c6a9dffa Michael Hanselmann
  @return: List of L{objects.BlockDevStatus}, one for each disk
1836 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: if any of the disks cannot be
1837 10c2650b Iustin Pop
      found
1838 a8083063 Iustin Pop

1839 a8083063 Iustin Pop
  """
1840 a8083063 Iustin Pop
  stats = []
1841 a8083063 Iustin Pop
  for dsk in disks:
1842 a8083063 Iustin Pop
    rbd = _RecursiveFindBD(dsk)
1843 a8083063 Iustin Pop
    if rbd is None:
1844 3efa9051 Iustin Pop
      _Fail("Can't find device %s", dsk)
1845 96acbc09 Michael Hanselmann
1846 36145b12 Michael Hanselmann
    stats.append(rbd.CombinedSyncStatus())
1847 96acbc09 Michael Hanselmann
1848 c26a6bd2 Iustin Pop
  return stats
1849 a8083063 Iustin Pop
1850 a8083063 Iustin Pop
1851 c6a9dffa Michael Hanselmann
def BlockdevGetmirrorstatusMulti(disks):
1852 c6a9dffa Michael Hanselmann
  """Get the mirroring status of a list of devices.
1853 c6a9dffa Michael Hanselmann

1854 c6a9dffa Michael Hanselmann
  @type disks: list of L{objects.Disk}
1855 c6a9dffa Michael Hanselmann
  @param disks: the list of disks which we should query
1856 c6a9dffa Michael Hanselmann
  @rtype: disk
1857 c6a9dffa Michael Hanselmann
  @return: List of tuples, (bool, status), one for each disk; bool denotes
1858 c6a9dffa Michael Hanselmann
    success/failure, status is L{objects.BlockDevStatus} on success, string
1859 c6a9dffa Michael Hanselmann
    otherwise
1860 c6a9dffa Michael Hanselmann

1861 c6a9dffa Michael Hanselmann
  """
1862 c6a9dffa Michael Hanselmann
  result = []
1863 c6a9dffa Michael Hanselmann
  for disk in disks:
1864 c6a9dffa Michael Hanselmann
    try:
1865 c6a9dffa Michael Hanselmann
      rbd = _RecursiveFindBD(disk)
1866 c6a9dffa Michael Hanselmann
      if rbd is None:
1867 c6a9dffa Michael Hanselmann
        result.append((False, "Can't find device %s" % disk))
1868 c6a9dffa Michael Hanselmann
        continue
1869 c6a9dffa Michael Hanselmann
1870 c6a9dffa Michael Hanselmann
      status = rbd.CombinedSyncStatus()
1871 c6a9dffa Michael Hanselmann
    except errors.BlockDeviceError, err:
1872 c6a9dffa Michael Hanselmann
      logging.exception("Error while getting disk status")
1873 c6a9dffa Michael Hanselmann
      result.append((False, str(err)))
1874 c6a9dffa Michael Hanselmann
    else:
1875 c6a9dffa Michael Hanselmann
      result.append((True, status))
1876 c6a9dffa Michael Hanselmann
1877 c6a9dffa Michael Hanselmann
  assert len(disks) == len(result)
1878 c6a9dffa Michael Hanselmann
1879 c6a9dffa Michael Hanselmann
  return result
1880 c6a9dffa Michael Hanselmann
1881 c6a9dffa Michael Hanselmann
1882 bca2e7f4 Iustin Pop
def _RecursiveFindBD(disk):
1883 a8083063 Iustin Pop
  """Check if a device is activated.
1884 a8083063 Iustin Pop

1885 5bbd3f7f Michael Hanselmann
  If so, return information about the real device.
1886 a8083063 Iustin Pop

1887 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1888 10c2650b Iustin Pop
  @param disk: the disk object we need to find
1889 a8083063 Iustin Pop

1890 10c2650b Iustin Pop
  @return: None if the device can't be found,
1891 10c2650b Iustin Pop
      otherwise the device instance
1892 a8083063 Iustin Pop

1893 a8083063 Iustin Pop
  """
1894 a8083063 Iustin Pop
  children = []
1895 a8083063 Iustin Pop
  if disk.children:
1896 a8083063 Iustin Pop
    for chdisk in disk.children:
1897 a8083063 Iustin Pop
      children.append(_RecursiveFindBD(chdisk))
1898 a8083063 Iustin Pop
1899 464f8daf Iustin Pop
  return bdev.FindDevice(disk.dev_type, disk.physical_id, children, disk.size)
1900 a8083063 Iustin Pop
1901 a8083063 Iustin Pop
1902 f2e07bb4 Michael Hanselmann
def _OpenRealBD(disk):
1903 f2e07bb4 Michael Hanselmann
  """Opens the underlying block device of a disk.
1904 f2e07bb4 Michael Hanselmann

1905 f2e07bb4 Michael Hanselmann
  @type disk: L{objects.Disk}
1906 f2e07bb4 Michael Hanselmann
  @param disk: the disk object we want to open
1907 f2e07bb4 Michael Hanselmann

1908 f2e07bb4 Michael Hanselmann
  """
1909 f2e07bb4 Michael Hanselmann
  real_disk = _RecursiveFindBD(disk)
1910 f2e07bb4 Michael Hanselmann
  if real_disk is None:
1911 f2e07bb4 Michael Hanselmann
    _Fail("Block device '%s' is not set up", disk)
1912 f2e07bb4 Michael Hanselmann
1913 f2e07bb4 Michael Hanselmann
  real_disk.Open()
1914 f2e07bb4 Michael Hanselmann
1915 f2e07bb4 Michael Hanselmann
  return real_disk
1916 f2e07bb4 Michael Hanselmann
1917 f2e07bb4 Michael Hanselmann
1918 821d1bd1 Iustin Pop
def BlockdevFind(disk):
1919 a8083063 Iustin Pop
  """Check if a device is activated.
1920 a8083063 Iustin Pop

1921 5bbd3f7f Michael Hanselmann
  If it is, return information about the real device.
1922 a8083063 Iustin Pop

1923 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
1924 10c2650b Iustin Pop
  @param disk: the disk to find
1925 96acbc09 Michael Hanselmann
  @rtype: None or objects.BlockDevStatus
1926 96acbc09 Michael Hanselmann
  @return: None if the disk cannot be found, otherwise a the current
1927 96acbc09 Michael Hanselmann
           information
1928 a8083063 Iustin Pop

1929 a8083063 Iustin Pop
  """
1930 23829f6f Iustin Pop
  try:
1931 23829f6f Iustin Pop
    rbd = _RecursiveFindBD(disk)
1932 23829f6f Iustin Pop
  except errors.BlockDeviceError, err:
1933 2cc6781a Iustin Pop
    _Fail("Failed to find device: %s", err, exc=True)
1934 96acbc09 Michael Hanselmann
1935 a8083063 Iustin Pop
  if rbd is None:
1936 c26a6bd2 Iustin Pop
    return None
1937 96acbc09 Michael Hanselmann
1938 96acbc09 Michael Hanselmann
  return rbd.GetSyncStatus()
1939 a8083063 Iustin Pop
1940 a8083063 Iustin Pop
1941 968a7623 Iustin Pop
def BlockdevGetsize(disks):
1942 968a7623 Iustin Pop
  """Computes the size of the given disks.
1943 968a7623 Iustin Pop

1944 968a7623 Iustin Pop
  If a disk is not found, returns None instead.
1945 968a7623 Iustin Pop

1946 968a7623 Iustin Pop
  @type disks: list of L{objects.Disk}
1947 968a7623 Iustin Pop
  @param disks: the list of disk to compute the size for
1948 968a7623 Iustin Pop
  @rtype: list
1949 968a7623 Iustin Pop
  @return: list with elements None if the disk cannot be found,
1950 968a7623 Iustin Pop
      otherwise the size
1951 968a7623 Iustin Pop

1952 968a7623 Iustin Pop
  """
1953 968a7623 Iustin Pop
  result = []
1954 968a7623 Iustin Pop
  for cf in disks:
1955 968a7623 Iustin Pop
    try:
1956 968a7623 Iustin Pop
      rbd = _RecursiveFindBD(cf)
1957 1122eb25 Iustin Pop
    except errors.BlockDeviceError:
1958 968a7623 Iustin Pop
      result.append(None)
1959 968a7623 Iustin Pop
      continue
1960 968a7623 Iustin Pop
    if rbd is None:
1961 968a7623 Iustin Pop
      result.append(None)
1962 968a7623 Iustin Pop
    else:
1963 968a7623 Iustin Pop
      result.append(rbd.GetActualSize())
1964 968a7623 Iustin Pop
  return result
1965 968a7623 Iustin Pop
1966 968a7623 Iustin Pop
1967 858f3d18 Iustin Pop
def BlockdevExport(disk, dest_node, dest_path, cluster_name):
1968 858f3d18 Iustin Pop
  """Export a block device to a remote node.
1969 858f3d18 Iustin Pop

1970 858f3d18 Iustin Pop
  @type disk: L{objects.Disk}
1971 858f3d18 Iustin Pop
  @param disk: the description of the disk to export
1972 858f3d18 Iustin Pop
  @type dest_node: str
1973 858f3d18 Iustin Pop
  @param dest_node: the destination node to export to
1974 858f3d18 Iustin Pop
  @type dest_path: str
1975 858f3d18 Iustin Pop
  @param dest_path: the destination path on the target node
1976 858f3d18 Iustin Pop
  @type cluster_name: str
1977 858f3d18 Iustin Pop
  @param cluster_name: the cluster name, needed for SSH hostalias
1978 858f3d18 Iustin Pop
  @rtype: None
1979 858f3d18 Iustin Pop

1980 858f3d18 Iustin Pop
  """
1981 f2e07bb4 Michael Hanselmann
  real_disk = _OpenRealBD(disk)
1982 858f3d18 Iustin Pop
1983 858f3d18 Iustin Pop
  # the block size on the read dd is 1MiB to match our units
1984 858f3d18 Iustin Pop
  expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
1985 858f3d18 Iustin Pop
                               "dd if=%s bs=1048576 count=%s",
1986 858f3d18 Iustin Pop
                               real_disk.dev_path, str(disk.size))
1987 858f3d18 Iustin Pop
1988 858f3d18 Iustin Pop
  # we set here a smaller block size as, due to ssh buffering, more
1989 858f3d18 Iustin Pop
  # than 64-128k will mostly ignored; we use nocreat to fail if the
1990 858f3d18 Iustin Pop
  # device is not already there or we pass a wrong path; we use
1991 858f3d18 Iustin Pop
  # notrunc to no attempt truncate on an LV device; we use oflag=dsync
1992 858f3d18 Iustin Pop
  # to not buffer too much memory; this means that at best, we flush
1993 858f3d18 Iustin Pop
  # every 64k, which will not be very fast
1994 858f3d18 Iustin Pop
  destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
1995 858f3d18 Iustin Pop
                                " oflag=dsync", dest_path)
1996 858f3d18 Iustin Pop
1997 858f3d18 Iustin Pop
  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
1998 858f3d18 Iustin Pop
                                                   constants.GANETI_RUNAS,
1999 858f3d18 Iustin Pop
                                                   destcmd)
2000 858f3d18 Iustin Pop
2001 858f3d18 Iustin Pop
  # all commands have been checked, so we're safe to combine them
2002 d0c8c01d Iustin Pop
  command = "|".join([expcmd, utils.ShellQuoteArgs(remotecmd)])
2003 858f3d18 Iustin Pop
2004 858f3d18 Iustin Pop
  result = utils.RunCmd(["bash", "-c", command])
2005 858f3d18 Iustin Pop
2006 858f3d18 Iustin Pop
  if result.failed:
2007 858f3d18 Iustin Pop
    _Fail("Disk copy command '%s' returned error: %s"
2008 858f3d18 Iustin Pop
          " output: %s", command, result.fail_reason, result.output)
2009 858f3d18 Iustin Pop
2010 858f3d18 Iustin Pop
2011 a8083063 Iustin Pop
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
2012 a8083063 Iustin Pop
  """Write a file to the filesystem.
2013 a8083063 Iustin Pop

2014 a8083063 Iustin Pop
  This allows the master to overwrite(!) a file. It will only perform
2015 a8083063 Iustin Pop
  the operation if the file belongs to a list of configuration files.
2016 a8083063 Iustin Pop

2017 10c2650b Iustin Pop
  @type file_name: str
2018 10c2650b Iustin Pop
  @param file_name: the target file name
2019 10c2650b Iustin Pop
  @type data: str
2020 10c2650b Iustin Pop
  @param data: the new contents of the file
2021 10c2650b Iustin Pop
  @type mode: int
2022 10c2650b Iustin Pop
  @param mode: the mode to give the file (can be None)
2023 9a914f7a René Nussbaumer
  @type uid: string
2024 9a914f7a René Nussbaumer
  @param uid: the owner of the file
2025 9a914f7a René Nussbaumer
  @type gid: string
2026 9a914f7a René Nussbaumer
  @param gid: the group of the file
2027 10c2650b Iustin Pop
  @type atime: float
2028 10c2650b Iustin Pop
  @param atime: the atime to set on the file (can be None)
2029 10c2650b Iustin Pop
  @type mtime: float
2030 10c2650b Iustin Pop
  @param mtime: the mtime to set on the file (can be None)
2031 c26a6bd2 Iustin Pop
  @rtype: None
2032 10c2650b Iustin Pop

2033 a8083063 Iustin Pop
  """
2034 a8083063 Iustin Pop
  if not os.path.isabs(file_name):
2035 2cc6781a Iustin Pop
    _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
2036 a8083063 Iustin Pop
2037 360b0dc2 Iustin Pop
  if file_name not in _ALLOWED_UPLOAD_FILES:
2038 2cc6781a Iustin Pop
    _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
2039 2cc6781a Iustin Pop
          file_name)
2040 a8083063 Iustin Pop
2041 12bce260 Michael Hanselmann
  raw_data = _Decompress(data)
2042 12bce260 Michael Hanselmann
2043 9a914f7a René Nussbaumer
  if not (isinstance(uid, basestring) and isinstance(gid, basestring)):
2044 9a914f7a René Nussbaumer
    _Fail("Invalid username/groupname type")
2045 9a914f7a René Nussbaumer
2046 9a914f7a René Nussbaumer
  getents = runtime.GetEnts()
2047 9a914f7a René Nussbaumer
  uid = getents.LookupUser(uid)
2048 9a914f7a René Nussbaumer
  gid = getents.LookupGroup(gid)
2049 9a914f7a René Nussbaumer
2050 8f065ae2 Iustin Pop
  utils.SafeWriteFile(file_name, None,
2051 8f065ae2 Iustin Pop
                      data=raw_data, mode=mode, uid=uid, gid=gid,
2052 8f065ae2 Iustin Pop
                      atime=atime, mtime=mtime)
2053 a8083063 Iustin Pop
2054 386b57af Iustin Pop
2055 b2f29800 René Nussbaumer
def RunOob(oob_program, command, node, timeout):
2056 b2f29800 René Nussbaumer
  """Executes oob_program with given command on given node.
2057 b2f29800 René Nussbaumer

2058 b2f29800 René Nussbaumer
  @param oob_program: The path to the executable oob_program
2059 b2f29800 René Nussbaumer
  @param command: The command to invoke on oob_program
2060 b2f29800 René Nussbaumer
  @param node: The node given as an argument to the program
2061 b2f29800 René Nussbaumer
  @param timeout: Timeout after which we kill the oob program
2062 b2f29800 René Nussbaumer

2063 b2f29800 René Nussbaumer
  @return: stdout
2064 b2f29800 René Nussbaumer
  @raise RPCFail: If execution fails for some reason
2065 b2f29800 René Nussbaumer

2066 b2f29800 René Nussbaumer
  """
2067 b2f29800 René Nussbaumer
  result = utils.RunCmd([oob_program, command, node], timeout=timeout)
2068 b2f29800 René Nussbaumer
2069 b2f29800 René Nussbaumer
  if result.failed:
2070 b2f29800 René Nussbaumer
    _Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
2071 b2f29800 René Nussbaumer
          result.fail_reason, result.output)
2072 b2f29800 René Nussbaumer
2073 b2f29800 René Nussbaumer
  return result.stdout
2074 b2f29800 René Nussbaumer
2075 b2f29800 René Nussbaumer
2076 03d1dba2 Michael Hanselmann
def WriteSsconfFiles(values):
2077 89b14f05 Iustin Pop
  """Update all ssconf files.
2078 89b14f05 Iustin Pop

2079 89b14f05 Iustin Pop
  Wrapper around the SimpleStore.WriteFiles.
2080 89b14f05 Iustin Pop

2081 89b14f05 Iustin Pop
  """
2082 89b14f05 Iustin Pop
  ssconf.SimpleStore().WriteFiles(values)
2083 6ddc95ec Michael Hanselmann
2084 6ddc95ec Michael Hanselmann
2085 a8083063 Iustin Pop
def _ErrnoOrStr(err):
2086 a8083063 Iustin Pop
  """Format an EnvironmentError exception.
2087 a8083063 Iustin Pop

2088 10c2650b Iustin Pop
  If the L{err} argument has an errno attribute, it will be looked up
2089 10c2650b Iustin Pop
  and converted into a textual C{E...} description. Otherwise the
2090 10c2650b Iustin Pop
  string representation of the error will be returned.
2091 10c2650b Iustin Pop

2092 10c2650b Iustin Pop
  @type err: L{EnvironmentError}
2093 10c2650b Iustin Pop
  @param err: the exception to format
2094 a8083063 Iustin Pop

2095 a8083063 Iustin Pop
  """
2096 d0c8c01d Iustin Pop
  if hasattr(err, "errno"):
2097 a8083063 Iustin Pop
    detail = errno.errorcode[err.errno]
2098 a8083063 Iustin Pop
  else:
2099 a8083063 Iustin Pop
    detail = str(err)
2100 a8083063 Iustin Pop
  return detail
2101 a8083063 Iustin Pop
2102 5d0fe286 Iustin Pop
2103 c19f9810 Iustin Pop
def _OSOndiskAPIVersion(os_dir):
2104 2f8598a5 Alexander Schreiber
  """Compute and return the API version of a given OS.
2105 a8083063 Iustin Pop

2106 c19f9810 Iustin Pop
  This function will try to read the API version of the OS residing in
2107 c19f9810 Iustin Pop
  the 'os_dir' directory.
2108 7c3d51d4 Guido Trotter

2109 10c2650b Iustin Pop
  @type os_dir: str
2110 c19f9810 Iustin Pop
  @param os_dir: the directory in which we should look for the OS
2111 8e70b181 Iustin Pop
  @rtype: tuple
2112 8e70b181 Iustin Pop
  @return: tuple (status, data) with status denoting the validity and
2113 8e70b181 Iustin Pop
      data holding either the vaid versions or an error message
2114 a8083063 Iustin Pop

2115 a8083063 Iustin Pop
  """
2116 e02b9114 Iustin Pop
  api_file = utils.PathJoin(os_dir, constants.OS_API_FILE)
2117 a8083063 Iustin Pop
2118 a8083063 Iustin Pop
  try:
2119 a8083063 Iustin Pop
    st = os.stat(api_file)
2120 a8083063 Iustin Pop
  except EnvironmentError, err:
2121 b6b45e0d Guido Trotter
    return False, ("Required file '%s' not found under path %s: %s" %
2122 b6b45e0d Guido Trotter
                   (constants.OS_API_FILE, os_dir, _ErrnoOrStr(err)))
2123 a8083063 Iustin Pop
2124 a8083063 Iustin Pop
  if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2125 b6b45e0d Guido Trotter
    return False, ("File '%s' in %s is not a regular file" %
2126 b6b45e0d Guido Trotter
                   (constants.OS_API_FILE, os_dir))
2127 a8083063 Iustin Pop
2128 a8083063 Iustin Pop
  try:
2129 3374afa9 Guido Trotter
    api_versions = utils.ReadFile(api_file).splitlines()
2130 a8083063 Iustin Pop
  except EnvironmentError, err:
2131 255dcebd Iustin Pop
    return False, ("Error while reading the API version file at %s: %s" %
2132 255dcebd Iustin Pop
                   (api_file, _ErrnoOrStr(err)))
2133 a8083063 Iustin Pop
2134 a8083063 Iustin Pop
  try:
2135 63b9b186 Guido Trotter
    api_versions = [int(version.strip()) for version in api_versions]
2136 a8083063 Iustin Pop
  except (TypeError, ValueError), err:
2137 255dcebd Iustin Pop
    return False, ("API version(s) can't be converted to integer: %s" %
2138 255dcebd Iustin Pop
                   str(err))
2139 a8083063 Iustin Pop
2140 255dcebd Iustin Pop
  return True, api_versions
2141 a8083063 Iustin Pop
2142 386b57af Iustin Pop
2143 7c3d51d4 Guido Trotter
def DiagnoseOS(top_dirs=None):
2144 a8083063 Iustin Pop
  """Compute the validity for all OSes.
2145 a8083063 Iustin Pop

2146 10c2650b Iustin Pop
  @type top_dirs: list
2147 10c2650b Iustin Pop
  @param top_dirs: the list of directories in which to
2148 10c2650b Iustin Pop
      search (if not given defaults to
2149 10c2650b Iustin Pop
      L{constants.OS_SEARCH_PATH})
2150 10c2650b Iustin Pop
  @rtype: list of L{objects.OS}
2151 bad78e66 Iustin Pop
  @return: a list of tuples (name, path, status, diagnose, variants,
2152 bad78e66 Iustin Pop
      parameters, api_version) for all (potential) OSes under all
2153 bad78e66 Iustin Pop
      search paths, where:
2154 255dcebd Iustin Pop
          - name is the (potential) OS name
2155 255dcebd Iustin Pop
          - path is the full path to the OS
2156 255dcebd Iustin Pop
          - status True/False is the validity of the OS
2157 255dcebd Iustin Pop
          - diagnose is the error message for an invalid OS, otherwise empty
2158 ba00557a Guido Trotter
          - variants is a list of supported OS variants, if any
2159 c7d04a6b Iustin Pop
          - parameters is a list of (name, help) parameters, if any
2160 bad78e66 Iustin Pop
          - api_version is a list of support OS API versions
2161 a8083063 Iustin Pop

2162 a8083063 Iustin Pop
  """
2163 7c3d51d4 Guido Trotter
  if top_dirs is None:
2164 7c3d51d4 Guido Trotter
    top_dirs = constants.OS_SEARCH_PATH
2165 a8083063 Iustin Pop
2166 a8083063 Iustin Pop
  result = []
2167 65fe4693 Iustin Pop
  for dir_name in top_dirs:
2168 65fe4693 Iustin Pop
    if os.path.isdir(dir_name):
2169 7c3d51d4 Guido Trotter
      try:
2170 65fe4693 Iustin Pop
        f_names = utils.ListVisibleFiles(dir_name)
2171 7c3d51d4 Guido Trotter
      except EnvironmentError, err:
2172 29921401 Iustin Pop
        logging.exception("Can't list the OS directory %s: %s", dir_name, err)
2173 7c3d51d4 Guido Trotter
        break
2174 7c3d51d4 Guido Trotter
      for name in f_names:
2175 e02b9114 Iustin Pop
        os_path = utils.PathJoin(dir_name, name)
2176 255dcebd Iustin Pop
        status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
2177 255dcebd Iustin Pop
        if status:
2178 255dcebd Iustin Pop
          diagnose = ""
2179 ba00557a Guido Trotter
          variants = os_inst.supported_variants
2180 c7d04a6b Iustin Pop
          parameters = os_inst.supported_parameters
2181 bad78e66 Iustin Pop
          api_versions = os_inst.api_versions
2182 255dcebd Iustin Pop
        else:
2183 255dcebd Iustin Pop
          diagnose = os_inst
2184 bad78e66 Iustin Pop
          variants = parameters = api_versions = []
2185 bad78e66 Iustin Pop
        result.append((name, os_path, status, diagnose, variants,
2186 bad78e66 Iustin Pop
                       parameters, api_versions))
2187 a8083063 Iustin Pop
2188 c26a6bd2 Iustin Pop
  return result
2189 a8083063 Iustin Pop
2190 a8083063 Iustin Pop
2191 255dcebd Iustin Pop
def _TryOSFromDisk(name, base_dir=None):
2192 a8083063 Iustin Pop
  """Create an OS instance from disk.
2193 a8083063 Iustin Pop

2194 a8083063 Iustin Pop
  This function will return an OS instance if the given name is a
2195 8e70b181 Iustin Pop
  valid OS name.
2196 a8083063 Iustin Pop

2197 8ee4dc80 Guido Trotter
  @type base_dir: string
2198 8ee4dc80 Guido Trotter
  @keyword base_dir: Base directory containing OS installations.
2199 8ee4dc80 Guido Trotter
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2200 255dcebd Iustin Pop
  @rtype: tuple
2201 255dcebd Iustin Pop
  @return: success and either the OS instance if we find a valid one,
2202 255dcebd Iustin Pop
      or error message
2203 7c3d51d4 Guido Trotter

2204 a8083063 Iustin Pop
  """
2205 56bcd3f4 Guido Trotter
  if base_dir is None:
2206 57c177af Iustin Pop
    os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir)
2207 c34c0cfd Iustin Pop
  else:
2208 f95c81bf Iustin Pop
    os_dir = utils.FindFile(name, [base_dir], os.path.isdir)
2209 f95c81bf Iustin Pop
2210 f95c81bf Iustin Pop
  if os_dir is None:
2211 5c0433d6 Iustin Pop
    return False, "Directory for OS %s not found in search path" % name
2212 a8083063 Iustin Pop
2213 c19f9810 Iustin Pop
  status, api_versions = _OSOndiskAPIVersion(os_dir)
2214 255dcebd Iustin Pop
  if not status:
2215 255dcebd Iustin Pop
    # push the error up
2216 255dcebd Iustin Pop
    return status, api_versions
2217 a8083063 Iustin Pop
2218 d1a7d66f Guido Trotter
  if not constants.OS_API_VERSIONS.intersection(api_versions):
2219 255dcebd Iustin Pop
    return False, ("API version mismatch for path '%s': found %s, want %s." %
2220 d1a7d66f Guido Trotter
                   (os_dir, api_versions, constants.OS_API_VERSIONS))
2221 a8083063 Iustin Pop
2222 35007011 Iustin Pop
  # OS Files dictionary, we will populate it with the absolute path
2223 35007011 Iustin Pop
  # names; if the value is True, then it is a required file, otherwise
2224 35007011 Iustin Pop
  # an optional one
2225 35007011 Iustin Pop
  os_files = dict.fromkeys(constants.OS_SCRIPTS, True)
2226 a8083063 Iustin Pop
2227 95075fba Guido Trotter
  if max(api_versions) >= constants.OS_API_V15:
2228 35007011 Iustin Pop
    os_files[constants.OS_VARIANTS_FILE] = False
2229 95075fba Guido Trotter
2230 c7d04a6b Iustin Pop
  if max(api_versions) >= constants.OS_API_V20:
2231 35007011 Iustin Pop
    os_files[constants.OS_PARAMETERS_FILE] = True
2232 c7d04a6b Iustin Pop
  else:
2233 c7d04a6b Iustin Pop
    del os_files[constants.OS_SCRIPT_VERIFY]
2234 c7d04a6b Iustin Pop
2235 35007011 Iustin Pop
  for (filename, required) in os_files.items():
2236 e02b9114 Iustin Pop
    os_files[filename] = utils.PathJoin(os_dir, filename)
2237 a8083063 Iustin Pop
2238 a8083063 Iustin Pop
    try:
2239 ea79fc15 Michael Hanselmann
      st = os.stat(os_files[filename])
2240 a8083063 Iustin Pop
    except EnvironmentError, err:
2241 35007011 Iustin Pop
      if err.errno == errno.ENOENT and not required:
2242 35007011 Iustin Pop
        del os_files[filename]
2243 35007011 Iustin Pop
        continue
2244 41ba4061 Guido Trotter
      return False, ("File '%s' under path '%s' is missing (%s)" %
2245 ea79fc15 Michael Hanselmann
                     (filename, os_dir, _ErrnoOrStr(err)))
2246 a8083063 Iustin Pop
2247 a8083063 Iustin Pop
    if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2248 41ba4061 Guido Trotter
      return False, ("File '%s' under path '%s' is not a regular file" %
2249 ea79fc15 Michael Hanselmann
                     (filename, os_dir))
2250 255dcebd Iustin Pop
2251 ea79fc15 Michael Hanselmann
    if filename in constants.OS_SCRIPTS:
2252 0757c107 Guido Trotter
      if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
2253 0757c107 Guido Trotter
        return False, ("File '%s' under path '%s' is not executable" %
2254 ea79fc15 Michael Hanselmann
                       (filename, os_dir))
2255 0757c107 Guido Trotter
2256 845da3e8 Iustin Pop
  variants = []
2257 95075fba Guido Trotter
  if constants.OS_VARIANTS_FILE in os_files:
2258 95075fba Guido Trotter
    variants_file = os_files[constants.OS_VARIANTS_FILE]
2259 95075fba Guido Trotter
    try:
2260 95075fba Guido Trotter
      variants = utils.ReadFile(variants_file).splitlines()
2261 95075fba Guido Trotter
    except EnvironmentError, err:
2262 35007011 Iustin Pop
      # we accept missing files, but not other errors
2263 35007011 Iustin Pop
      if err.errno != errno.ENOENT:
2264 35007011 Iustin Pop
        return False, ("Error while reading the OS variants file at %s: %s" %
2265 35007011 Iustin Pop
                       (variants_file, _ErrnoOrStr(err)))
2266 0757c107 Guido Trotter
2267 c7d04a6b Iustin Pop
  parameters = []
2268 c7d04a6b Iustin Pop
  if constants.OS_PARAMETERS_FILE in os_files:
2269 c7d04a6b Iustin Pop
    parameters_file = os_files[constants.OS_PARAMETERS_FILE]
2270 c7d04a6b Iustin Pop
    try:
2271 c7d04a6b Iustin Pop
      parameters = utils.ReadFile(parameters_file).splitlines()
2272 c7d04a6b Iustin Pop
    except EnvironmentError, err:
2273 c7d04a6b Iustin Pop
      return False, ("Error while reading the OS parameters file at %s: %s" %
2274 c7d04a6b Iustin Pop
                     (parameters_file, _ErrnoOrStr(err)))
2275 c7d04a6b Iustin Pop
    parameters = [v.split(None, 1) for v in parameters]
2276 c7d04a6b Iustin Pop
2277 8e70b181 Iustin Pop
  os_obj = objects.OS(name=name, path=os_dir,
2278 41ba4061 Guido Trotter
                      create_script=os_files[constants.OS_SCRIPT_CREATE],
2279 41ba4061 Guido Trotter
                      export_script=os_files[constants.OS_SCRIPT_EXPORT],
2280 41ba4061 Guido Trotter
                      import_script=os_files[constants.OS_SCRIPT_IMPORT],
2281 41ba4061 Guido Trotter
                      rename_script=os_files[constants.OS_SCRIPT_RENAME],
2282 40684c3a Iustin Pop
                      verify_script=os_files.get(constants.OS_SCRIPT_VERIFY,
2283 40684c3a Iustin Pop
                                                 None),
2284 95075fba Guido Trotter
                      supported_variants=variants,
2285 c7d04a6b Iustin Pop
                      supported_parameters=parameters,
2286 255dcebd Iustin Pop
                      api_versions=api_versions)
2287 255dcebd Iustin Pop
  return True, os_obj
2288 255dcebd Iustin Pop
2289 255dcebd Iustin Pop
2290 255dcebd Iustin Pop
def OSFromDisk(name, base_dir=None):
2291 255dcebd Iustin Pop
  """Create an OS instance from disk.
2292 255dcebd Iustin Pop

2293 255dcebd Iustin Pop
  This function will return an OS instance if the given name is a
2294 255dcebd Iustin Pop
  valid OS name. Otherwise, it will raise an appropriate
2295 255dcebd Iustin Pop
  L{RPCFail} exception, detailing why this is not a valid OS.
2296 255dcebd Iustin Pop

2297 255dcebd Iustin Pop
  This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
2298 255dcebd Iustin Pop
  an exception but returns true/false status data.
2299 255dcebd Iustin Pop

2300 255dcebd Iustin Pop
  @type base_dir: string
2301 255dcebd Iustin Pop
  @keyword base_dir: Base directory containing OS installations.
2302 255dcebd Iustin Pop
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2303 255dcebd Iustin Pop
  @rtype: L{objects.OS}
2304 255dcebd Iustin Pop
  @return: the OS instance if we find a valid one
2305 255dcebd Iustin Pop
  @raise RPCFail: if we don't find a valid OS
2306 255dcebd Iustin Pop

2307 255dcebd Iustin Pop
  """
2308 870dc44c Iustin Pop
  name_only = objects.OS.GetName(name)
2309 6ee7102a Guido Trotter
  status, payload = _TryOSFromDisk(name_only, base_dir)
2310 255dcebd Iustin Pop
2311 255dcebd Iustin Pop
  if not status:
2312 255dcebd Iustin Pop
    _Fail(payload)
2313 a8083063 Iustin Pop
2314 255dcebd Iustin Pop
  return payload
2315 a8083063 Iustin Pop
2316 a8083063 Iustin Pop
2317 a025e535 Vitaly Kuznetsov
def OSCoreEnv(os_name, inst_os, os_params, debug=0):
2318 efaa9b06 Iustin Pop
  """Calculate the basic environment for an os script.
2319 2266edb2 Guido Trotter

2320 a025e535 Vitaly Kuznetsov
  @type os_name: str
2321 a025e535 Vitaly Kuznetsov
  @param os_name: full operating system name (including variant)
2322 099c52ad Iustin Pop
  @type inst_os: L{objects.OS}
2323 099c52ad Iustin Pop
  @param inst_os: operating system for which the environment is being built
2324 1bdcbbab Iustin Pop
  @type os_params: dict
2325 1bdcbbab Iustin Pop
  @param os_params: the OS parameters
2326 2266edb2 Guido Trotter
  @type debug: integer
2327 10c2650b Iustin Pop
  @param debug: debug level (0 or 1, for OS Api 10)
2328 2266edb2 Guido Trotter
  @rtype: dict
2329 2266edb2 Guido Trotter
  @return: dict of environment variables
2330 10c2650b Iustin Pop
  @raise errors.BlockDeviceError: if the block device
2331 10c2650b Iustin Pop
      cannot be found
2332 2266edb2 Guido Trotter

2333 2266edb2 Guido Trotter
  """
2334 2266edb2 Guido Trotter
  result = {}
2335 099c52ad Iustin Pop
  api_version = \
2336 099c52ad Iustin Pop
    max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
2337 d0c8c01d Iustin Pop
  result["OS_API_VERSION"] = "%d" % api_version
2338 d0c8c01d Iustin Pop
  result["OS_NAME"] = inst_os.name
2339 d0c8c01d Iustin Pop
  result["DEBUG_LEVEL"] = "%d" % debug
2340 efaa9b06 Iustin Pop
2341 efaa9b06 Iustin Pop
  # OS variants
2342 35007011 Iustin Pop
  if api_version >= constants.OS_API_V15 and inst_os.supported_variants:
2343 870dc44c Iustin Pop
    variant = objects.OS.GetVariant(os_name)
2344 870dc44c Iustin Pop
    if not variant:
2345 099c52ad Iustin Pop
      variant = inst_os.supported_variants[0]
2346 35007011 Iustin Pop
  else:
2347 35007011 Iustin Pop
    variant = ""
2348 35007011 Iustin Pop
  result["OS_VARIANT"] = variant
2349 efaa9b06 Iustin Pop
2350 1bdcbbab Iustin Pop
  # OS params
2351 1bdcbbab Iustin Pop
  for pname, pvalue in os_params.items():
2352 d0c8c01d Iustin Pop
    result["OSP_%s" % pname.upper()] = pvalue
2353 1bdcbbab Iustin Pop
2354 efaa9b06 Iustin Pop
  return result
2355 efaa9b06 Iustin Pop
2356 efaa9b06 Iustin Pop
2357 efaa9b06 Iustin Pop
def OSEnvironment(instance, inst_os, debug=0):
2358 efaa9b06 Iustin Pop
  """Calculate the environment for an os script.
2359 efaa9b06 Iustin Pop

2360 efaa9b06 Iustin Pop
  @type instance: L{objects.Instance}
2361 efaa9b06 Iustin Pop
  @param instance: target instance for the os script run
2362 efaa9b06 Iustin Pop
  @type inst_os: L{objects.OS}
2363 efaa9b06 Iustin Pop
  @param inst_os: operating system for which the environment is being built
2364 efaa9b06 Iustin Pop
  @type debug: integer
2365 efaa9b06 Iustin Pop
  @param debug: debug level (0 or 1, for OS Api 10)
2366 efaa9b06 Iustin Pop
  @rtype: dict
2367 efaa9b06 Iustin Pop
  @return: dict of environment variables
2368 efaa9b06 Iustin Pop
  @raise errors.BlockDeviceError: if the block device
2369 efaa9b06 Iustin Pop
      cannot be found
2370 efaa9b06 Iustin Pop

2371 efaa9b06 Iustin Pop
  """
2372 a025e535 Vitaly Kuznetsov
  result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug)
2373 efaa9b06 Iustin Pop
2374 519719fd Marco Casavecchia
  for attr in ["name", "os", "uuid", "ctime", "mtime", "primary_node"]:
2375 f2165b8a Iustin Pop
    result["INSTANCE_%s" % attr.upper()] = str(getattr(instance, attr))
2376 f2165b8a Iustin Pop
2377 d0c8c01d Iustin Pop
  result["HYPERVISOR"] = instance.hypervisor
2378 d0c8c01d Iustin Pop
  result["DISK_COUNT"] = "%d" % len(instance.disks)
2379 d0c8c01d Iustin Pop
  result["NIC_COUNT"] = "%d" % len(instance.nics)
2380 d0c8c01d Iustin Pop
  result["INSTANCE_SECONDARY_NODES"] = \
2381 d0c8c01d Iustin Pop
      ("%s" % " ".join(instance.secondary_nodes))
2382 efaa9b06 Iustin Pop
2383 efaa9b06 Iustin Pop
  # Disks
2384 2266edb2 Guido Trotter
  for idx, disk in enumerate(instance.disks):
2385 f2e07bb4 Michael Hanselmann
    real_disk = _OpenRealBD(disk)
2386 d0c8c01d Iustin Pop
    result["DISK_%d_PATH" % idx] = real_disk.dev_path
2387 d0c8c01d Iustin Pop
    result["DISK_%d_ACCESS" % idx] = disk.mode
2388 2266edb2 Guido Trotter
    if constants.HV_DISK_TYPE in instance.hvparams:
2389 d0c8c01d Iustin Pop
      result["DISK_%d_FRONTEND_TYPE" % idx] = \
2390 2266edb2 Guido Trotter
        instance.hvparams[constants.HV_DISK_TYPE]
2391 2266edb2 Guido Trotter
    if disk.dev_type in constants.LDS_BLOCK:
2392 d0c8c01d Iustin Pop
      result["DISK_%d_BACKEND_TYPE" % idx] = "block"
2393 2266edb2 Guido Trotter
    elif disk.dev_type == constants.LD_FILE:
2394 d0c8c01d Iustin Pop
      result["DISK_%d_BACKEND_TYPE" % idx] = \
2395 d0c8c01d Iustin Pop
        "file:%s" % disk.physical_id[0]
2396 efaa9b06 Iustin Pop
2397 efaa9b06 Iustin Pop
  # NICs
2398 2266edb2 Guido Trotter
  for idx, nic in enumerate(instance.nics):
2399 d0c8c01d Iustin Pop
    result["NIC_%d_MAC" % idx] = nic.mac
2400 2266edb2 Guido Trotter
    if nic.ip:
2401 d0c8c01d Iustin Pop
      result["NIC_%d_IP" % idx] = nic.ip
2402 d0c8c01d Iustin Pop
    result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
2403 1ba9227f Guido Trotter
    if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
2404 d0c8c01d Iustin Pop
      result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
2405 1ba9227f Guido Trotter
    if nic.nicparams[constants.NIC_LINK]:
2406 d0c8c01d Iustin Pop
      result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
2407 2266edb2 Guido Trotter
    if constants.HV_NIC_TYPE in instance.hvparams:
2408 d0c8c01d Iustin Pop
      result["NIC_%d_FRONTEND_TYPE" % idx] = \
2409 2266edb2 Guido Trotter
        instance.hvparams[constants.HV_NIC_TYPE]
2410 2266edb2 Guido Trotter
2411 efaa9b06 Iustin Pop
  # HV/BE params
2412 67fc3042 Iustin Pop
  for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
2413 67fc3042 Iustin Pop
    for key, value in source.items():
2414 030b218a Iustin Pop
      result["INSTANCE_%s_%s" % (kind, key)] = str(value)
2415 67fc3042 Iustin Pop
2416 2266edb2 Guido Trotter
  return result
2417 a8083063 Iustin Pop
2418 f2e07bb4 Michael Hanselmann
2419 a59faf4b Iustin Pop
def BlockdevGrow(disk, amount, dryrun):
2420 594609c0 Iustin Pop
  """Grow a stack of block devices.
2421 594609c0 Iustin Pop

2422 594609c0 Iustin Pop
  This function is called recursively, with the childrens being the
2423 10c2650b Iustin Pop
  first ones to resize.
2424 594609c0 Iustin Pop

2425 10c2650b Iustin Pop
  @type disk: L{objects.Disk}
2426 10c2650b Iustin Pop
  @param disk: the disk to be grown
2427 a59faf4b Iustin Pop
  @type amount: integer
2428 a59faf4b Iustin Pop
  @param amount: the amount (in mebibytes) to grow with
2429 a59faf4b Iustin Pop
  @type dryrun: boolean
2430 a59faf4b Iustin Pop
  @param dryrun: whether to execute the operation in simulation mode
2431 a59faf4b Iustin Pop
      only, without actually increasing the size
2432 10c2650b Iustin Pop
  @rtype: (status, result)
2433 a59faf4b Iustin Pop
  @return: a tuple with the status of the operation (True/False), and
2434 a59faf4b Iustin Pop
      the errors message if status is False
2435 594609c0 Iustin Pop

2436 594609c0 Iustin Pop
  """
2437 594609c0 Iustin Pop
  r_dev = _RecursiveFindBD(disk)
2438 594609c0 Iustin Pop
  if r_dev is None:
2439 afdc3985 Iustin Pop
    _Fail("Cannot find block device %s", disk)
2440 594609c0 Iustin Pop
2441 594609c0 Iustin Pop
  try:
2442 a59faf4b Iustin Pop
    r_dev.Grow(amount, dryrun)
2443 594609c0 Iustin Pop
  except errors.BlockDeviceError, err:
2444 2cc6781a Iustin Pop
    _Fail("Failed to grow block device: %s", err, exc=True)
2445 594609c0 Iustin Pop
2446 594609c0 Iustin Pop
2447 821d1bd1 Iustin Pop
def BlockdevSnapshot(disk):
2448 a8083063 Iustin Pop
  """Create a snapshot copy of a block device.
2449 a8083063 Iustin Pop

2450 a8083063 Iustin Pop
  This function is called recursively, and the snapshot is actually created
2451 a8083063 Iustin Pop
  just for the leaf lvm backend device.
2452 a8083063 Iustin Pop

2453 e9e9263d Guido Trotter
  @type disk: L{objects.Disk}
2454 e9e9263d Guido Trotter
  @param disk: the disk to be snapshotted
2455 e9e9263d Guido Trotter
  @rtype: string
2456 800ac399 Iustin Pop
  @return: snapshot disk ID as (vg, lv)
2457 a8083063 Iustin Pop

2458 098c0958 Michael Hanselmann
  """
2459 433c63aa Iustin Pop
  if disk.dev_type == constants.LD_DRBD8:
2460 433c63aa Iustin Pop
    if not disk.children:
2461 433c63aa Iustin Pop
      _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
2462 433c63aa Iustin Pop
            disk.unique_id)
2463 433c63aa Iustin Pop
    return BlockdevSnapshot(disk.children[0])
2464 fe96220b Iustin Pop
  elif disk.dev_type == constants.LD_LV:
2465 a8083063 Iustin Pop
    r_dev = _RecursiveFindBD(disk)
2466 a8083063 Iustin Pop
    if r_dev is not None:
2467 433c63aa Iustin Pop
      # FIXME: choose a saner value for the snapshot size
2468 a8083063 Iustin Pop
      # let's stay on the safe side and ask for the full size, for now
2469 c26a6bd2 Iustin Pop
      return r_dev.Snapshot(disk.size)
2470 a8083063 Iustin Pop
    else:
2471 87812fd3 Iustin Pop
      _Fail("Cannot find block device %s", disk)
2472 a8083063 Iustin Pop
  else:
2473 87812fd3 Iustin Pop
    _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
2474 87812fd3 Iustin Pop
          disk.unique_id, disk.dev_type)
2475 a8083063 Iustin Pop
2476 a8083063 Iustin Pop
2477 a8083063 Iustin Pop
def FinalizeExport(instance, snap_disks):
2478 a8083063 Iustin Pop
  """Write out the export configuration information.
2479 a8083063 Iustin Pop

2480 10c2650b Iustin Pop
  @type instance: L{objects.Instance}
2481 10c2650b Iustin Pop
  @param instance: the instance which we export, used for
2482 10c2650b Iustin Pop
      saving configuration
2483 10c2650b Iustin Pop
  @type snap_disks: list of L{objects.Disk}
2484 10c2650b Iustin Pop
  @param snap_disks: list of snapshot block devices, which
2485 10c2650b Iustin Pop
      will be used to get the actual name of the dump file
2486 a8083063 Iustin Pop

2487 c26a6bd2 Iustin Pop
  @rtype: None
2488 a8083063 Iustin Pop

2489 098c0958 Michael Hanselmann
  """
2490 c4feafe8 Iustin Pop
  destdir = utils.PathJoin(constants.EXPORT_DIR, instance.name + ".new")
2491 c4feafe8 Iustin Pop
  finaldestdir = utils.PathJoin(constants.EXPORT_DIR, instance.name)
2492 a8083063 Iustin Pop
2493 a8083063 Iustin Pop
  config = objects.SerializableConfigParser()
2494 a8083063 Iustin Pop
2495 a8083063 Iustin Pop
  config.add_section(constants.INISECT_EXP)
2496 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "version", "0")
2497 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time()))
2498 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "source", instance.primary_node)
2499 d0c8c01d Iustin Pop
  config.set(constants.INISECT_EXP, "os", instance.os)
2500 775b8743 Michael Hanselmann
  config.set(constants.INISECT_EXP, "compression", "none")
2501 a8083063 Iustin Pop
2502 a8083063 Iustin Pop
  config.add_section(constants.INISECT_INS)
2503 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "name", instance.name)
2504 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "memory", "%d" %
2505 51de46bf Iustin Pop
             instance.beparams[constants.BE_MEMORY])
2506 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "vcpus", "%d" %
2507 51de46bf Iustin Pop
             instance.beparams[constants.BE_VCPUS])
2508 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "disk_template", instance.disk_template)
2509 d0c8c01d Iustin Pop
  config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
2510 fbb2c636 Michael Hanselmann
  config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags()))
2511 66f93869 Manuel Franceschini
2512 95268cc3 Iustin Pop
  nic_total = 0
2513 a8083063 Iustin Pop
  for nic_count, nic in enumerate(instance.nics):
2514 95268cc3 Iustin Pop
    nic_total += 1
2515 d0c8c01d Iustin Pop
    config.set(constants.INISECT_INS, "nic%d_mac" %
2516 d0c8c01d Iustin Pop
               nic_count, "%s" % nic.mac)
2517 d0c8c01d Iustin Pop
    config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip)
2518 6801eb5c Iustin Pop
    for param in constants.NICS_PARAMETER_TYPES:
2519 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
2520 d0c8c01d Iustin Pop
                 "%s" % nic.nicparams.get(param, None))
2521 a8083063 Iustin Pop
  # TODO: redundant: on load can read nics until it doesn't exist
2522 e687ec01 Michael Hanselmann
  config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total)
2523 a8083063 Iustin Pop
2524 726d7d68 Iustin Pop
  disk_total = 0
2525 a8083063 Iustin Pop
  for disk_count, disk in enumerate(snap_disks):
2526 19d7f90a Guido Trotter
    if disk:
2527 726d7d68 Iustin Pop
      disk_total += 1
2528 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
2529 d0c8c01d Iustin Pop
                 ("%s" % disk.iv_name))
2530 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
2531 d0c8c01d Iustin Pop
                 ("%s" % disk.physical_id[1]))
2532 d0c8c01d Iustin Pop
      config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
2533 d0c8c01d Iustin Pop
                 ("%d" % disk.size))
2534 d0c8c01d Iustin Pop
2535 e687ec01 Michael Hanselmann
  config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total)
2536 a8083063 Iustin Pop
2537 3c8954ad Iustin Pop
  # New-style hypervisor/backend parameters
2538 3c8954ad Iustin Pop
2539 3c8954ad Iustin Pop
  config.add_section(constants.INISECT_HYP)
2540 3c8954ad Iustin Pop
  for name, value in instance.hvparams.items():
2541 3c8954ad Iustin Pop
    if name not in constants.HVC_GLOBALS:
2542 3c8954ad Iustin Pop
      config.set(constants.INISECT_HYP, name, str(value))
2543 3c8954ad Iustin Pop
2544 3c8954ad Iustin Pop
  config.add_section(constants.INISECT_BEP)
2545 3c8954ad Iustin Pop
  for name, value in instance.beparams.items():
2546 3c8954ad Iustin Pop
    config.set(constants.INISECT_BEP, name, str(value))
2547 3c8954ad Iustin Pop
2548 535b49cb Iustin Pop
  config.add_section(constants.INISECT_OSP)
2549 535b49cb Iustin Pop
  for name, value in instance.osparams.items():
2550 535b49cb Iustin Pop
    config.set(constants.INISECT_OSP, name, str(value))
2551 535b49cb Iustin Pop
2552 c4feafe8 Iustin Pop
  utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE),
2553 726d7d68 Iustin Pop
                  data=config.Dumps())
2554 56569f4e Michael Hanselmann
  shutil.rmtree(finaldestdir, ignore_errors=True)
2555 a8083063 Iustin Pop
  shutil.move(destdir, finaldestdir)
2556 a8083063 Iustin Pop
2557 a8083063 Iustin Pop
2558 a8083063 Iustin Pop
def ExportInfo(dest):
2559 a8083063 Iustin Pop
  """Get export configuration information.
2560 a8083063 Iustin Pop

2561 10c2650b Iustin Pop
  @type dest: str
2562 10c2650b Iustin Pop
  @param dest: directory containing the export
2563 a8083063 Iustin Pop

2564 10c2650b Iustin Pop
  @rtype: L{objects.SerializableConfigParser}
2565 10c2650b Iustin Pop
  @return: a serializable config file containing the
2566 10c2650b Iustin Pop
      export info
2567 a8083063 Iustin Pop

2568 a8083063 Iustin Pop
  """
2569 c4feafe8 Iustin Pop
  cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE)
2570 a8083063 Iustin Pop
2571 a8083063 Iustin Pop
  config = objects.SerializableConfigParser()
2572 a8083063 Iustin Pop
  config.read(cff)
2573 a8083063 Iustin Pop
2574 a8083063 Iustin Pop
  if (not config.has_section(constants.INISECT_EXP) or
2575 a8083063 Iustin Pop
      not config.has_section(constants.INISECT_INS)):
2576 3eccac06 Iustin Pop
    _Fail("Export info file doesn't have the required fields")
2577 a8083063 Iustin Pop
2578 c26a6bd2 Iustin Pop
  return config.Dumps()
2579 a8083063 Iustin Pop
2580 a8083063 Iustin Pop
2581 a8083063 Iustin Pop
def ListExports():
2582 a8083063 Iustin Pop
  """Return a list of exports currently available on this machine.
2583 098c0958 Michael Hanselmann

2584 10c2650b Iustin Pop
  @rtype: list
2585 10c2650b Iustin Pop
  @return: list of the exports
2586 10c2650b Iustin Pop

2587 a8083063 Iustin Pop
  """
2588 a8083063 Iustin Pop
  if os.path.isdir(constants.EXPORT_DIR):
2589 b5b8309d Guido Trotter
    return sorted(utils.ListVisibleFiles(constants.EXPORT_DIR))
2590 a8083063 Iustin Pop
  else:
2591 afdc3985 Iustin Pop
    _Fail("No exports directory")
2592 a8083063 Iustin Pop
2593 a8083063 Iustin Pop
2594 a8083063 Iustin Pop
def RemoveExport(export):
2595 a8083063 Iustin Pop
  """Remove an existing export from the node.
2596 a8083063 Iustin Pop

2597 10c2650b Iustin Pop
  @type export: str
2598 10c2650b Iustin Pop
  @param export: the name of the export to remove
2599 c26a6bd2 Iustin Pop
  @rtype: None
2600 a8083063 Iustin Pop

2601 098c0958 Michael Hanselmann
  """
2602 c4feafe8 Iustin Pop
  target = utils.PathJoin(constants.EXPORT_DIR, export)
2603 a8083063 Iustin Pop
2604 35fbcd11 Iustin Pop
  try:
2605 35fbcd11 Iustin Pop
    shutil.rmtree(target)
2606 35fbcd11 Iustin Pop
  except EnvironmentError, err:
2607 35fbcd11 Iustin Pop
    _Fail("Error while removing the export: %s", err, exc=True)
2608 a8083063 Iustin Pop
2609 a8083063 Iustin Pop
2610 821d1bd1 Iustin Pop
def BlockdevRename(devlist):
2611 f3e513ad Iustin Pop
  """Rename a list of block devices.
2612 f3e513ad Iustin Pop

2613 10c2650b Iustin Pop
  @type devlist: list of tuples
2614 10c2650b Iustin Pop
  @param devlist: list of tuples of the form  (disk,
2615 10c2650b Iustin Pop
      new_logical_id, new_physical_id); disk is an
2616 10c2650b Iustin Pop
      L{objects.Disk} object describing the current disk,
2617 10c2650b Iustin Pop
      and new logical_id/physical_id is the name we
2618 10c2650b Iustin Pop
      rename it to
2619 10c2650b Iustin Pop
  @rtype: boolean
2620 10c2650b Iustin Pop
  @return: True if all renames succeeded, False otherwise
2621 f3e513ad Iustin Pop

2622 f3e513ad Iustin Pop
  """
2623 6b5e3f70 Iustin Pop
  msgs = []
2624 f3e513ad Iustin Pop
  result = True
2625 f3e513ad Iustin Pop
  for disk, unique_id in devlist:
2626 f3e513ad Iustin Pop
    dev = _RecursiveFindBD(disk)
2627 f3e513ad Iustin Pop
    if dev is None:
2628 6b5e3f70 Iustin Pop
      msgs.append("Can't find device %s in rename" % str(disk))
2629 f3e513ad Iustin Pop
      result = False
2630 f3e513ad Iustin Pop
      continue
2631 f3e513ad Iustin Pop
    try:
2632 3f78eef2 Iustin Pop
      old_rpath = dev.dev_path
2633 f3e513ad Iustin Pop
      dev.Rename(unique_id)
2634 3f78eef2 Iustin Pop
      new_rpath = dev.dev_path
2635 3f78eef2 Iustin Pop
      if old_rpath != new_rpath:
2636 3f78eef2 Iustin Pop
        DevCacheManager.RemoveCache(old_rpath)
2637 3f78eef2 Iustin Pop
        # FIXME: we should add the new cache information here, like:
2638 3f78eef2 Iustin Pop
        # DevCacheManager.UpdateCache(new_rpath, owner, ...)
2639 3f78eef2 Iustin Pop
        # but we don't have the owner here - maybe parse from existing
2640 3f78eef2 Iustin Pop
        # cache? for now, we only lose lvm data when we rename, which
2641 3f78eef2 Iustin Pop
        # is less critical than DRBD or MD
2642 f3e513ad Iustin Pop
    except errors.BlockDeviceError, err:
2643 6b5e3f70 Iustin Pop
      msgs.append("Can't rename device '%s' to '%s': %s" %
2644 6b5e3f70 Iustin Pop
                  (dev, unique_id, err))
2645 18682bca Iustin Pop
      logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
2646 f3e513ad Iustin Pop
      result = False
2647 afdc3985 Iustin Pop
  if not result:
2648 afdc3985 Iustin Pop
    _Fail("; ".join(msgs))
2649 f3e513ad Iustin Pop
2650 f3e513ad Iustin Pop
2651 4b97f902 Apollon Oikonomopoulos
def _TransformFileStorageDir(fs_dir):
2652 778b75bb Manuel Franceschini
  """Checks whether given file_storage_dir is valid.
2653 778b75bb Manuel Franceschini

2654 4b97f902 Apollon Oikonomopoulos
  Checks wheter the given fs_dir is within the cluster-wide default
2655 4b97f902 Apollon Oikonomopoulos
  file_storage_dir or the shared_file_storage_dir, which are stored in
2656 4b97f902 Apollon Oikonomopoulos
  SimpleStore. Only paths under those directories are allowed.
2657 778b75bb Manuel Franceschini

2658 4b97f902 Apollon Oikonomopoulos
  @type fs_dir: str
2659 4b97f902 Apollon Oikonomopoulos
  @param fs_dir: the path to check
2660 d61cbe76 Iustin Pop

2661 b1206984 Iustin Pop
  @return: the normalized path if valid, None otherwise
2662 778b75bb Manuel Franceschini

2663 778b75bb Manuel Franceschini
  """
2664 cb7c0198 Iustin Pop
  if not constants.ENABLE_FILE_STORAGE:
2665 cb7c0198 Iustin Pop
    _Fail("File storage disabled at configure time")
2666 c657dcc9 Michael Hanselmann
  cfg = _GetConfig()
2667 4b97f902 Apollon Oikonomopoulos
  fs_dir = os.path.normpath(fs_dir)
2668 4b97f902 Apollon Oikonomopoulos
  base_fstore = cfg.GetFileStorageDir()
2669 4b97f902 Apollon Oikonomopoulos
  base_shared = cfg.GetSharedFileStorageDir()
2670 cf00dba0 René Nussbaumer
  if not (utils.IsBelowDir(base_fstore, fs_dir) or
2671 cf00dba0 René Nussbaumer
          utils.IsBelowDir(base_shared, fs_dir)):
2672 b2b8bcce Iustin Pop
    _Fail("File storage directory '%s' is not under base file"
2673 4b97f902 Apollon Oikonomopoulos
          " storage directory '%s' or shared storage directory '%s'",
2674 4b97f902 Apollon Oikonomopoulos
          fs_dir, base_fstore, base_shared)
2675 4b97f902 Apollon Oikonomopoulos
  return fs_dir
2676 778b75bb Manuel Franceschini
2677 778b75bb Manuel Franceschini
2678 778b75bb Manuel Franceschini
def CreateFileStorageDir(file_storage_dir):
2679 778b75bb Manuel Franceschini
  """Create file storage directory.
2680 778b75bb Manuel Franceschini

2681 b1206984 Iustin Pop
  @type file_storage_dir: str
2682 b1206984 Iustin Pop
  @param file_storage_dir: directory to create
2683 778b75bb Manuel Franceschini

2684 b1206984 Iustin Pop
  @rtype: tuple
2685 b1206984 Iustin Pop
  @return: tuple with first element a boolean indicating wheter dir
2686 b1206984 Iustin Pop
      creation was successful or not
2687 778b75bb Manuel Franceschini

2688 778b75bb Manuel Franceschini
  """
2689 778b75bb Manuel Franceschini
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2690 b2b8bcce Iustin Pop
  if os.path.exists(file_storage_dir):
2691 b2b8bcce Iustin Pop
    if not os.path.isdir(file_storage_dir):
2692 b2b8bcce Iustin Pop
      _Fail("Specified storage dir '%s' is not a directory",
2693 b2b8bcce Iustin Pop
            file_storage_dir)
2694 778b75bb Manuel Franceschini
  else:
2695 b2b8bcce Iustin Pop
    try:
2696 b2b8bcce Iustin Pop
      os.makedirs(file_storage_dir, 0750)
2697 b2b8bcce Iustin Pop
    except OSError, err:
2698 b2b8bcce Iustin Pop
      _Fail("Cannot create file storage directory '%s': %s",
2699 b2b8bcce Iustin Pop
            file_storage_dir, err, exc=True)
2700 778b75bb Manuel Franceschini
2701 778b75bb Manuel Franceschini
2702 778b75bb Manuel Franceschini
def RemoveFileStorageDir(file_storage_dir):
2703 778b75bb Manuel Franceschini
  """Remove file storage directory.
2704 778b75bb Manuel Franceschini

2705 778b75bb Manuel Franceschini
  Remove it only if it's empty. If not log an error and return.
2706 778b75bb Manuel Franceschini

2707 10c2650b Iustin Pop
  @type file_storage_dir: str
2708 10c2650b Iustin Pop
  @param file_storage_dir: the directory we should cleanup
2709 10c2650b Iustin Pop
  @rtype: tuple (success,)
2710 10c2650b Iustin Pop
  @return: tuple of one element, C{success}, denoting
2711 5bbd3f7f Michael Hanselmann
      whether the operation was successful
2712 778b75bb Manuel Franceschini

2713 778b75bb Manuel Franceschini
  """
2714 778b75bb Manuel Franceschini
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2715 b2b8bcce Iustin Pop
  if os.path.exists(file_storage_dir):
2716 b2b8bcce Iustin Pop
    if not os.path.isdir(file_storage_dir):
2717 b2b8bcce Iustin Pop
      _Fail("Specified Storage directory '%s' is not a directory",
2718 b2b8bcce Iustin Pop
            file_storage_dir)
2719 afdc3985 Iustin Pop
    # deletes dir only if empty, otherwise we want to fail the rpc call
2720 b2b8bcce Iustin Pop
    try:
2721 b2b8bcce Iustin Pop
      os.rmdir(file_storage_dir)
2722 b2b8bcce Iustin Pop
    except OSError, err:
2723 b2b8bcce Iustin Pop
      _Fail("Cannot remove file storage directory '%s': %s",
2724 b2b8bcce Iustin Pop
            file_storage_dir, err)
2725 b2b8bcce Iustin Pop
2726 778b75bb Manuel Franceschini
2727 778b75bb Manuel Franceschini
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
2728 778b75bb Manuel Franceschini
  """Rename the file storage directory.
2729 778b75bb Manuel Franceschini

2730 10c2650b Iustin Pop
  @type old_file_storage_dir: str
2731 10c2650b Iustin Pop
  @param old_file_storage_dir: the current path
2732 10c2650b Iustin Pop
  @type new_file_storage_dir: str
2733 10c2650b Iustin Pop
  @param new_file_storage_dir: the name we should rename to
2734 10c2650b Iustin Pop
  @rtype: tuple (success,)
2735 10c2650b Iustin Pop
  @return: tuple of one element, C{success}, denoting
2736 10c2650b Iustin Pop
      whether the operation was successful
2737 778b75bb Manuel Franceschini

2738 778b75bb Manuel Franceschini
  """
2739 778b75bb Manuel Franceschini
  old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
2740 778b75bb Manuel Franceschini
  new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
2741 b2b8bcce Iustin Pop
  if not os.path.exists(new_file_storage_dir):
2742 b2b8bcce Iustin Pop
    if os.path.isdir(old_file_storage_dir):
2743 b2b8bcce Iustin Pop
      try:
2744 b2b8bcce Iustin Pop
        os.rename(old_file_storage_dir, new_file_storage_dir)
2745 b2b8bcce Iustin Pop
      except OSError, err:
2746 b2b8bcce Iustin Pop
        _Fail("Cannot rename '%s' to '%s': %s",
2747 b2b8bcce Iustin Pop
              old_file_storage_dir, new_file_storage_dir, err)
2748 778b75bb Manuel Franceschini
    else:
2749 b2b8bcce Iustin Pop
      _Fail("Specified storage dir '%s' is not a directory",
2750 b2b8bcce Iustin Pop
            old_file_storage_dir)
2751 b2b8bcce Iustin Pop
  else:
2752 b2b8bcce Iustin Pop
    if os.path.exists(old_file_storage_dir):
2753 b2b8bcce Iustin Pop
      _Fail("Cannot rename '%s' to '%s': both locations exist",
2754 b2b8bcce Iustin Pop
            old_file_storage_dir, new_file_storage_dir)
2755 778b75bb Manuel Franceschini
2756 778b75bb Manuel Franceschini
2757 c8457ce7 Iustin Pop
def _EnsureJobQueueFile(file_name):
2758 dc31eae3 Michael Hanselmann
  """Checks whether the given filename is in the queue directory.
2759 ca52cdeb Michael Hanselmann

2760 10c2650b Iustin Pop
  @type file_name: str
2761 10c2650b Iustin Pop
  @param file_name: the file name we should check
2762 c8457ce7 Iustin Pop
  @rtype: None
2763 c8457ce7 Iustin Pop
  @raises RPCFail: if the file is not valid
2764 10c2650b Iustin Pop

2765 ca52cdeb Michael Hanselmann
  """
2766 ca52cdeb Michael Hanselmann
  queue_dir = os.path.normpath(constants.QUEUE_DIR)
2767 dc31eae3 Michael Hanselmann
  result = (os.path.commonprefix([queue_dir, file_name]) == queue_dir)
2768 dc31eae3 Michael Hanselmann
2769 dc31eae3 Michael Hanselmann
  if not result:
2770 c8457ce7 Iustin Pop
    _Fail("Passed job queue file '%s' does not belong to"
2771 c8457ce7 Iustin Pop
          " the queue directory '%s'", file_name, queue_dir)
2772 dc31eae3 Michael Hanselmann
2773 dc31eae3 Michael Hanselmann
2774 dc31eae3 Michael Hanselmann
def JobQueueUpdate(file_name, content):
2775 dc31eae3 Michael Hanselmann
  """Updates a file in the queue directory.
2776 dc31eae3 Michael Hanselmann

2777 3865ca48 Michael Hanselmann
  This is just a wrapper over L{utils.io.WriteFile}, with proper
2778 10c2650b Iustin Pop
  checking.
2779 10c2650b Iustin Pop

2780 10c2650b Iustin Pop
  @type file_name: str
2781 10c2650b Iustin Pop
  @param file_name: the job file name
2782 10c2650b Iustin Pop
  @type content: str
2783 10c2650b Iustin Pop
  @param content: the new job contents
2784 10c2650b Iustin Pop
  @rtype: boolean
2785 10c2650b Iustin Pop
  @return: the success of the operation
2786 10c2650b Iustin Pop

2787 dc31eae3 Michael Hanselmann
  """
2788 c8457ce7 Iustin Pop
  _EnsureJobQueueFile(file_name)
2789 82b22e19 René Nussbaumer
  getents = runtime.GetEnts()
2790 ca52cdeb Michael Hanselmann
2791 ca52cdeb Michael Hanselmann
  # Write and replace the file atomically
2792 82b22e19 René Nussbaumer
  utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid,
2793 82b22e19 René Nussbaumer
                  gid=getents.masterd_gid)
2794 ca52cdeb Michael Hanselmann
2795 ca52cdeb Michael Hanselmann
2796 af5ebcb1 Michael Hanselmann
def JobQueueRename(old, new):
2797 af5ebcb1 Michael Hanselmann
  """Renames a job queue file.
2798 af5ebcb1 Michael Hanselmann

2799 c41eea6e Iustin Pop
  This is just a wrapper over os.rename with proper checking.
2800 10c2650b Iustin Pop

2801 10c2650b Iustin Pop
  @type old: str
2802 10c2650b Iustin Pop
  @param old: the old (actual) file name
2803 10c2650b Iustin Pop
  @type new: str
2804 10c2650b Iustin Pop
  @param new: the desired file name
2805 c8457ce7 Iustin Pop
  @rtype: tuple
2806 c8457ce7 Iustin Pop
  @return: the success of the operation and payload
2807 10c2650b Iustin Pop

2808 af5ebcb1 Michael Hanselmann
  """
2809 c8457ce7 Iustin Pop
  _EnsureJobQueueFile(old)
2810 c8457ce7 Iustin Pop
  _EnsureJobQueueFile(new)
2811 af5ebcb1 Michael Hanselmann
2812 8e5a705d René Nussbaumer
  getents = runtime.GetEnts()
2813 8e5a705d René Nussbaumer
2814 8e5a705d René Nussbaumer
  utils.RenameFile(old, new, mkdir=True, mkdir_mode=0700,
2815 8e5a705d René Nussbaumer
                   dir_uid=getents.masterd_uid, dir_gid=getents.masterd_gid)
2816 af5ebcb1 Michael Hanselmann
2817 af5ebcb1 Michael Hanselmann
2818 821d1bd1 Iustin Pop
def BlockdevClose(instance_name, disks):
2819 d61cbe76 Iustin Pop
  """Closes the given block devices.
2820 d61cbe76 Iustin Pop

2821 10c2650b Iustin Pop
  This means they will be switched to secondary mode (in case of
2822 10c2650b Iustin Pop
  DRBD).
2823 10c2650b Iustin Pop

2824 b2e7666a Iustin Pop
  @param instance_name: if the argument is not empty, the symlinks
2825 b2e7666a Iustin Pop
      of this instance will be removed
2826 10c2650b Iustin Pop
  @type disks: list of L{objects.Disk}
2827 10c2650b Iustin Pop
  @param disks: the list of disks to be closed
2828 10c2650b Iustin Pop
  @rtype: tuple (success, message)
2829 10c2650b Iustin Pop
  @return: a tuple of success and message, where success
2830 10c2650b Iustin Pop
      indicates the succes of the operation, and message
2831 10c2650b Iustin Pop
      which will contain the error details in case we
2832 10c2650b Iustin Pop
      failed
2833 d61cbe76 Iustin Pop

2834 d61cbe76 Iustin Pop
  """
2835 d61cbe76 Iustin Pop
  bdevs = []
2836 d61cbe76 Iustin Pop
  for cf in disks:
2837 d61cbe76 Iustin Pop
    rd = _RecursiveFindBD(cf)
2838 d61cbe76 Iustin Pop
    if rd is None:
2839 2cc6781a Iustin Pop
      _Fail("Can't find device %s", cf)
2840 d61cbe76 Iustin Pop
    bdevs.append(rd)
2841 d61cbe76 Iustin Pop
2842 d61cbe76 Iustin Pop
  msg = []
2843 d61cbe76 Iustin Pop
  for rd in bdevs:
2844 d61cbe76 Iustin Pop
    try:
2845 d61cbe76 Iustin Pop
      rd.Close()
2846 d61cbe76 Iustin Pop
    except errors.BlockDeviceError, err:
2847 d61cbe76 Iustin Pop
      msg.append(str(err))
2848 d61cbe76 Iustin Pop
  if msg:
2849 afdc3985 Iustin Pop
    _Fail("Can't make devices secondary: %s", ",".join(msg))
2850 d61cbe76 Iustin Pop
  else:
2851 b2e7666a Iustin Pop
    if instance_name:
2852 5282084b Iustin Pop
      _RemoveBlockDevLinks(instance_name, disks)
2853 d61cbe76 Iustin Pop
2854 d61cbe76 Iustin Pop
2855 6217e295 Iustin Pop
def ValidateHVParams(hvname, hvparams):
2856 6217e295 Iustin Pop
  """Validates the given hypervisor parameters.
2857 6217e295 Iustin Pop

2858 6217e295 Iustin Pop
  @type hvname: string
2859 6217e295 Iustin Pop
  @param hvname: the hypervisor name
2860 6217e295 Iustin Pop
  @type hvparams: dict
2861 6217e295 Iustin Pop
  @param hvparams: the hypervisor parameters to be validated
2862 c26a6bd2 Iustin Pop
  @rtype: None
2863 6217e295 Iustin Pop

2864 6217e295 Iustin Pop
  """
2865 6217e295 Iustin Pop
  try:
2866 6217e295 Iustin Pop
    hv_type = hypervisor.GetHypervisor(hvname)
2867 6217e295 Iustin Pop
    hv_type.ValidateParameters(hvparams)
2868 6217e295 Iustin Pop
  except errors.HypervisorError, err:
2869 afdc3985 Iustin Pop
    _Fail(str(err), log=False)
2870 6217e295 Iustin Pop
2871 6217e295 Iustin Pop
2872 acd9ff9e Iustin Pop
def _CheckOSPList(os_obj, parameters):
2873 acd9ff9e Iustin Pop
  """Check whether a list of parameters is supported by the OS.
2874 acd9ff9e Iustin Pop

2875 acd9ff9e Iustin Pop
  @type os_obj: L{objects.OS}
2876 acd9ff9e Iustin Pop
  @param os_obj: OS object to check
2877 acd9ff9e Iustin Pop
  @type parameters: list
2878 acd9ff9e Iustin Pop
  @param parameters: the list of parameters to check
2879 acd9ff9e Iustin Pop

2880 acd9ff9e Iustin Pop
  """
2881 acd9ff9e Iustin Pop
  supported = [v[0] for v in os_obj.supported_parameters]
2882 acd9ff9e Iustin Pop
  delta = frozenset(parameters).difference(supported)
2883 acd9ff9e Iustin Pop
  if delta:
2884 acd9ff9e Iustin Pop
    _Fail("The following parameters are not supported"
2885 acd9ff9e Iustin Pop
          " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
2886 acd9ff9e Iustin Pop
2887 acd9ff9e Iustin Pop
2888 acd9ff9e Iustin Pop
def ValidateOS(required, osname, checks, osparams):
2889 acd9ff9e Iustin Pop
  """Validate the given OS' parameters.
2890 acd9ff9e Iustin Pop

2891 acd9ff9e Iustin Pop
  @type required: boolean
2892 acd9ff9e Iustin Pop
  @param required: whether absence of the OS should translate into
2893 acd9ff9e Iustin Pop
      failure or not
2894 acd9ff9e Iustin Pop
  @type osname: string
2895 acd9ff9e Iustin Pop
  @param osname: the OS to be validated
2896 acd9ff9e Iustin Pop
  @type checks: list
2897 acd9ff9e Iustin Pop
  @param checks: list of the checks to run (currently only 'parameters')
2898 acd9ff9e Iustin Pop
  @type osparams: dict
2899 acd9ff9e Iustin Pop
  @param osparams: dictionary with OS parameters
2900 acd9ff9e Iustin Pop
  @rtype: boolean
2901 acd9ff9e Iustin Pop
  @return: True if the validation passed, or False if the OS was not
2902 acd9ff9e Iustin Pop
      found and L{required} was false
2903 acd9ff9e Iustin Pop

2904 acd9ff9e Iustin Pop
  """
2905 acd9ff9e Iustin Pop
  if not constants.OS_VALIDATE_CALLS.issuperset(checks):
2906 acd9ff9e Iustin Pop
    _Fail("Unknown checks required for OS %s: %s", osname,
2907 acd9ff9e Iustin Pop
          set(checks).difference(constants.OS_VALIDATE_CALLS))
2908 acd9ff9e Iustin Pop
2909 870dc44c Iustin Pop
  name_only = objects.OS.GetName(osname)
2910 acd9ff9e Iustin Pop
  status, tbv = _TryOSFromDisk(name_only, None)
2911 acd9ff9e Iustin Pop
2912 acd9ff9e Iustin Pop
  if not status:
2913 acd9ff9e Iustin Pop
    if required:
2914 acd9ff9e Iustin Pop
      _Fail(tbv)
2915 acd9ff9e Iustin Pop
    else:
2916 acd9ff9e Iustin Pop
      return False
2917 acd9ff9e Iustin Pop
2918 72db3fd7 Iustin Pop
  if max(tbv.api_versions) < constants.OS_API_V20:
2919 72db3fd7 Iustin Pop
    return True
2920 72db3fd7 Iustin Pop
2921 acd9ff9e Iustin Pop
  if constants.OS_VALIDATE_PARAMETERS in checks:
2922 acd9ff9e Iustin Pop
    _CheckOSPList(tbv, osparams.keys())
2923 acd9ff9e Iustin Pop
2924 a025e535 Vitaly Kuznetsov
  validate_env = OSCoreEnv(osname, tbv, osparams)
2925 acd9ff9e Iustin Pop
  result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env,
2926 896a03f6 Iustin Pop
                        cwd=tbv.path, reset_env=True)
2927 acd9ff9e Iustin Pop
  if result.failed:
2928 acd9ff9e Iustin Pop
    logging.error("os validate command '%s' returned error: %s output: %s",
2929 acd9ff9e Iustin Pop
                  result.cmd, result.fail_reason, result.output)
2930 acd9ff9e Iustin Pop
    _Fail("OS validation script failed (%s), output: %s",
2931 acd9ff9e Iustin Pop
          result.fail_reason, result.output, log=False)
2932 acd9ff9e Iustin Pop
2933 acd9ff9e Iustin Pop
  return True
2934 acd9ff9e Iustin Pop
2935 acd9ff9e Iustin Pop
2936 56aa9fd5 Iustin Pop
def DemoteFromMC():
2937 56aa9fd5 Iustin Pop
  """Demotes the current node from master candidate role.
2938 56aa9fd5 Iustin Pop

2939 56aa9fd5 Iustin Pop
  """
2940 56aa9fd5 Iustin Pop
  # try to ensure we're not the master by mistake
2941 56aa9fd5 Iustin Pop
  master, myself = ssconf.GetMasterAndMyself()
2942 56aa9fd5 Iustin Pop
  if master == myself:
2943 afdc3985 Iustin Pop
    _Fail("ssconf status shows I'm the master node, will not demote")
2944 f154a7a3 Michael Hanselmann
2945 f154a7a3 Michael Hanselmann
  result = utils.RunCmd([constants.DAEMON_UTIL, "check", constants.MASTERD])
2946 f154a7a3 Michael Hanselmann
  if not result.failed:
2947 afdc3985 Iustin Pop
    _Fail("The master daemon is running, will not demote")
2948 f154a7a3 Michael Hanselmann
2949 56aa9fd5 Iustin Pop
  try:
2950 9a5cb537 Iustin Pop
    if os.path.isfile(constants.CLUSTER_CONF_FILE):
2951 9a5cb537 Iustin Pop
      utils.CreateBackup(constants.CLUSTER_CONF_FILE)
2952 56aa9fd5 Iustin Pop
  except EnvironmentError, err:
2953 56aa9fd5 Iustin Pop
    if err.errno != errno.ENOENT:
2954 afdc3985 Iustin Pop
      _Fail("Error while backing up cluster file: %s", err, exc=True)
2955 f154a7a3 Michael Hanselmann
2956 56aa9fd5 Iustin Pop
  utils.RemoveFile(constants.CLUSTER_CONF_FILE)
2957 56aa9fd5 Iustin Pop
2958 56aa9fd5 Iustin Pop
2959 f942a838 Michael Hanselmann
def _GetX509Filenames(cryptodir, name):
2960 f942a838 Michael Hanselmann
  """Returns the full paths for the private key and certificate.
2961 f942a838 Michael Hanselmann

2962 f942a838 Michael Hanselmann
  """
2963 f942a838 Michael Hanselmann
  return (utils.PathJoin(cryptodir, name),
2964 f942a838 Michael Hanselmann
          utils.PathJoin(cryptodir, name, _X509_KEY_FILE),
2965 f942a838 Michael Hanselmann
          utils.PathJoin(cryptodir, name, _X509_CERT_FILE))
2966 f942a838 Michael Hanselmann
2967 f942a838 Michael Hanselmann
2968 f942a838 Michael Hanselmann
def CreateX509Certificate(validity, cryptodir=constants.CRYPTO_KEYS_DIR):
2969 f942a838 Michael Hanselmann
  """Creates a new X509 certificate for SSL/TLS.
2970 f942a838 Michael Hanselmann

2971 f942a838 Michael Hanselmann
  @type validity: int
2972 f942a838 Michael Hanselmann
  @param validity: Validity in seconds
2973 f942a838 Michael Hanselmann
  @rtype: tuple; (string, string)
2974 f942a838 Michael Hanselmann
  @return: Certificate name and public part
2975 f942a838 Michael Hanselmann

2976 f942a838 Michael Hanselmann
  """
2977 f942a838 Michael Hanselmann
  (key_pem, cert_pem) = \
2978 b705c7a6 Manuel Franceschini
    utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(),
2979 f942a838 Michael Hanselmann
                                     min(validity, _MAX_SSL_CERT_VALIDITY))
2980 f942a838 Michael Hanselmann
2981 f942a838 Michael Hanselmann
  cert_dir = tempfile.mkdtemp(dir=cryptodir,
2982 f942a838 Michael Hanselmann
                              prefix="x509-%s-" % utils.TimestampForFilename())
2983 f942a838 Michael Hanselmann
  try:
2984 f942a838 Michael Hanselmann
    name = os.path.basename(cert_dir)
2985 f942a838 Michael Hanselmann
    assert len(name) > 5
2986 f942a838 Michael Hanselmann
2987 f942a838 Michael Hanselmann
    (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
2988 f942a838 Michael Hanselmann
2989 f942a838 Michael Hanselmann
    utils.WriteFile(key_file, mode=0400, data=key_pem)
2990 f942a838 Michael Hanselmann
    utils.WriteFile(cert_file, mode=0400, data=cert_pem)
2991 f942a838 Michael Hanselmann
2992 f942a838 Michael Hanselmann
    # Never return private key as it shouldn't leave the node
2993 f942a838 Michael Hanselmann
    return (name, cert_pem)
2994 f942a838 Michael Hanselmann
  except Exception:
2995 f942a838 Michael Hanselmann
    shutil.rmtree(cert_dir, ignore_errors=True)
2996 f942a838 Michael Hanselmann
    raise
2997 f942a838 Michael Hanselmann
2998 f942a838 Michael Hanselmann
2999 f942a838 Michael Hanselmann
def RemoveX509Certificate(name, cryptodir=constants.CRYPTO_KEYS_DIR):
3000 f942a838 Michael Hanselmann
  """Removes a X509 certificate.
3001 f942a838 Michael Hanselmann

3002 f942a838 Michael Hanselmann
  @type name: string
3003 f942a838 Michael Hanselmann
  @param name: Certificate name
3004 f942a838 Michael Hanselmann

3005 f942a838 Michael Hanselmann
  """
3006 f942a838 Michael Hanselmann
  (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
3007 f942a838 Michael Hanselmann
3008 f942a838 Michael Hanselmann
  utils.RemoveFile(key_file)
3009 f942a838 Michael Hanselmann
  utils.RemoveFile(cert_file)
3010 f942a838 Michael Hanselmann
3011 f942a838 Michael Hanselmann
  try:
3012 f942a838 Michael Hanselmann
    os.rmdir(cert_dir)
3013 f942a838 Michael Hanselmann
  except EnvironmentError, err:
3014 f942a838 Michael Hanselmann
    _Fail("Cannot remove certificate directory '%s': %s",
3015 f942a838 Michael Hanselmann
          cert_dir, err)
3016 f942a838 Michael Hanselmann
3017 f942a838 Michael Hanselmann
3018 1651d116 Michael Hanselmann
def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
3019 1651d116 Michael Hanselmann
  """Returns the command for the requested input/output.
3020 1651d116 Michael Hanselmann

3021 1651d116 Michael Hanselmann
  @type instance: L{objects.Instance}
3022 1651d116 Michael Hanselmann
  @param instance: The instance object
3023 1651d116 Michael Hanselmann
  @param mode: Import/export mode
3024 1651d116 Michael Hanselmann
  @param ieio: Input/output type
3025 1651d116 Michael Hanselmann
  @param ieargs: Input/output arguments
3026 1651d116 Michael Hanselmann

3027 1651d116 Michael Hanselmann
  """
3028 1651d116 Michael Hanselmann
  assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)
3029 1651d116 Michael Hanselmann
3030 1651d116 Michael Hanselmann
  env = None
3031 1651d116 Michael Hanselmann
  prefix = None
3032 1651d116 Michael Hanselmann
  suffix = None
3033 2ad5550d Michael Hanselmann
  exp_size = None
3034 1651d116 Michael Hanselmann
3035 1651d116 Michael Hanselmann
  if ieio == constants.IEIO_FILE:
3036 1651d116 Michael Hanselmann
    (filename, ) = ieargs
3037 1651d116 Michael Hanselmann
3038 1651d116 Michael Hanselmann
    if not utils.IsNormAbsPath(filename):
3039 1651d116 Michael Hanselmann
      _Fail("Path '%s' is not normalized or absolute", filename)
3040 1651d116 Michael Hanselmann
3041 748c9884 René Nussbaumer
    real_filename = os.path.realpath(filename)
3042 748c9884 René Nussbaumer
    directory = os.path.dirname(real_filename)
3043 1651d116 Michael Hanselmann
3044 945859e0 René Nussbaumer
    if not utils.IsBelowDir(constants.EXPORT_DIR, real_filename):
3045 748c9884 René Nussbaumer
      _Fail("File '%s' is not under exports directory '%s': %s",
3046 748c9884 René Nussbaumer
            filename, constants.EXPORT_DIR, real_filename)
3047 1651d116 Michael Hanselmann
3048 1651d116 Michael Hanselmann
    # Create directory
3049 1651d116 Michael Hanselmann
    utils.Makedirs(directory, mode=0750)
3050 1651d116 Michael Hanselmann
3051 1651d116 Michael Hanselmann
    quoted_filename = utils.ShellQuote(filename)
3052 1651d116 Michael Hanselmann
3053 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
3054 1651d116 Michael Hanselmann
      suffix = "> %s" % quoted_filename
3055 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
3056 1651d116 Michael Hanselmann
      suffix = "< %s" % quoted_filename
3057 1651d116 Michael Hanselmann
3058 2ad5550d Michael Hanselmann
      # Retrieve file size
3059 2ad5550d Michael Hanselmann
      try:
3060 2ad5550d Michael Hanselmann
        st = os.stat(filename)
3061 2ad5550d Michael Hanselmann
      except EnvironmentError, err:
3062 2ad5550d Michael Hanselmann
        logging.error("Can't stat(2) %s: %s", filename, err)
3063 2ad5550d Michael Hanselmann
      else:
3064 2ad5550d Michael Hanselmann
        exp_size = utils.BytesToMebibyte(st.st_size)
3065 2ad5550d Michael Hanselmann
3066 1651d116 Michael Hanselmann
  elif ieio == constants.IEIO_RAW_DISK:
3067 1651d116 Michael Hanselmann
    (disk, ) = ieargs
3068 1651d116 Michael Hanselmann
3069 1651d116 Michael Hanselmann
    real_disk = _OpenRealBD(disk)
3070 1651d116 Michael Hanselmann
3071 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
3072 1651d116 Michael Hanselmann
      # we set here a smaller block size as, due to transport buffering, more
3073 1651d116 Michael Hanselmann
      # than 64-128k will mostly ignored; we use nocreat to fail if the device
3074 1651d116 Michael Hanselmann
      # is not already there or we pass a wrong path; we use notrunc to no
3075 1651d116 Michael Hanselmann
      # attempt truncate on an LV device; we use oflag=dsync to not buffer too
3076 1651d116 Michael Hanselmann
      # much memory; this means that at best, we flush every 64k, which will
3077 1651d116 Michael Hanselmann
      # not be very fast
3078 1651d116 Michael Hanselmann
      suffix = utils.BuildShellCmd(("| dd of=%s conv=nocreat,notrunc"
3079 1651d116 Michael Hanselmann
                                    " bs=%s oflag=dsync"),
3080 1651d116 Michael Hanselmann
                                    real_disk.dev_path,
3081 1651d116 Michael Hanselmann
                                    str(64 * 1024))
3082 1651d116 Michael Hanselmann
3083 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
3084 1651d116 Michael Hanselmann
      # the block size on the read dd is 1MiB to match our units
3085 1651d116 Michael Hanselmann
      prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
3086 1651d116 Michael Hanselmann
                                   real_disk.dev_path,
3087 1651d116 Michael Hanselmann
                                   str(1024 * 1024), # 1 MB
3088 1651d116 Michael Hanselmann
                                   str(disk.size))
3089 2ad5550d Michael Hanselmann
      exp_size = disk.size
3090 1651d116 Michael Hanselmann
3091 1651d116 Michael Hanselmann
  elif ieio == constants.IEIO_SCRIPT:
3092 1651d116 Michael Hanselmann
    (disk, disk_index, ) = ieargs
3093 1651d116 Michael Hanselmann
3094 1651d116 Michael Hanselmann
    assert isinstance(disk_index, (int, long))
3095 1651d116 Michael Hanselmann
3096 1651d116 Michael Hanselmann
    real_disk = _OpenRealBD(disk)
3097 1651d116 Michael Hanselmann
3098 1651d116 Michael Hanselmann
    inst_os = OSFromDisk(instance.os)
3099 1651d116 Michael Hanselmann
    env = OSEnvironment(instance, inst_os)
3100 1651d116 Michael Hanselmann
3101 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
3102 1651d116 Michael Hanselmann
      env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index]
3103 1651d116 Michael Hanselmann
      env["IMPORT_INDEX"] = str(disk_index)
3104 1651d116 Michael Hanselmann
      script = inst_os.import_script
3105 1651d116 Michael Hanselmann
3106 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
3107 1651d116 Michael Hanselmann
      env["EXPORT_DEVICE"] = real_disk.dev_path
3108 1651d116 Michael Hanselmann
      env["EXPORT_INDEX"] = str(disk_index)
3109 1651d116 Michael Hanselmann
      script = inst_os.export_script
3110 1651d116 Michael Hanselmann
3111 1651d116 Michael Hanselmann
    # TODO: Pass special environment only to script
3112 1651d116 Michael Hanselmann
    script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
3113 1651d116 Michael Hanselmann
3114 1651d116 Michael Hanselmann
    if mode == constants.IEM_IMPORT:
3115 1651d116 Michael Hanselmann
      suffix = "| %s" % script_cmd
3116 1651d116 Michael Hanselmann
3117 1651d116 Michael Hanselmann
    elif mode == constants.IEM_EXPORT:
3118 1651d116 Michael Hanselmann
      prefix = "%s |" % script_cmd
3119 1651d116 Michael Hanselmann
3120 2ad5550d Michael Hanselmann
    # Let script predict size
3121 2ad5550d Michael Hanselmann
    exp_size = constants.IE_CUSTOM_SIZE
3122 2ad5550d Michael Hanselmann
3123 1651d116 Michael Hanselmann
  else:
3124 1651d116 Michael Hanselmann
    _Fail("Invalid %s I/O mode %r", mode, ieio)
3125 1651d116 Michael Hanselmann
3126 2ad5550d Michael Hanselmann
  return (env, prefix, suffix, exp_size)
3127 1651d116 Michael Hanselmann
3128 1651d116 Michael Hanselmann
3129 1651d116 Michael Hanselmann
def _CreateImportExportStatusDir(prefix):
3130 1651d116 Michael Hanselmann
  """Creates status directory for import/export.
3131 1651d116 Michael Hanselmann

3132 1651d116 Michael Hanselmann
  """
3133 1651d116 Michael Hanselmann
  return tempfile.mkdtemp(dir=constants.IMPORT_EXPORT_DIR,
3134 1651d116 Michael Hanselmann
                          prefix=("%s-%s-" %
3135 1651d116 Michael Hanselmann
                                  (prefix, utils.TimestampForFilename())))
3136 1651d116 Michael Hanselmann
3137 1651d116 Michael Hanselmann
3138 6613661a Iustin Pop
def StartImportExportDaemon(mode, opts, host, port, instance, component,
3139 6613661a Iustin Pop
                            ieio, ieioargs):
3140 1651d116 Michael Hanselmann
  """Starts an import or export daemon.
3141 1651d116 Michael Hanselmann

3142 1651d116 Michael Hanselmann
  @param mode: Import/output mode
3143 eb630f50 Michael Hanselmann
  @type opts: L{objects.ImportExportOptions}
3144 eb630f50 Michael Hanselmann
  @param opts: Daemon options
3145 1651d116 Michael Hanselmann
  @type host: string
3146 1651d116 Michael Hanselmann
  @param host: Remote host for export (None for import)
3147 1651d116 Michael Hanselmann
  @type port: int
3148 1651d116 Michael Hanselmann
  @param port: Remote port for export (None for import)
3149 1651d116 Michael Hanselmann
  @type instance: L{objects.Instance}
3150 1651d116 Michael Hanselmann
  @param instance: Instance object
3151 6613661a Iustin Pop
  @type component: string
3152 6613661a Iustin Pop
  @param component: which part of the instance is transferred now,
3153 6613661a Iustin Pop
      e.g. 'disk/0'
3154 1651d116 Michael Hanselmann
  @param ieio: Input/output type
3155 1651d116 Michael Hanselmann
  @param ieioargs: Input/output arguments
3156 1651d116 Michael Hanselmann

3157 1651d116 Michael Hanselmann
  """
3158 1651d116 Michael Hanselmann
  if mode == constants.IEM_IMPORT:
3159 1651d116 Michael Hanselmann
    prefix = "import"
3160 1651d116 Michael Hanselmann
3161 1651d116 Michael Hanselmann
    if not (host is None and port is None):
3162 1651d116 Michael Hanselmann
      _Fail("Can not specify host or port on import")
3163 1651d116 Michael Hanselmann
3164 1651d116 Michael Hanselmann
  elif mode == constants.IEM_EXPORT:
3165 1651d116 Michael Hanselmann
    prefix = "export"
3166 1651d116 Michael Hanselmann
3167 1651d116 Michael Hanselmann
    if host is None or port is None:
3168 1651d116 Michael Hanselmann
      _Fail("Host and port must be specified for an export")
3169 1651d116 Michael Hanselmann
3170 1651d116 Michael Hanselmann
  else:
3171 1651d116 Michael Hanselmann
    _Fail("Invalid mode %r", mode)
3172 1651d116 Michael Hanselmann
3173 eb630f50 Michael Hanselmann
  if (opts.key_name is None) ^ (opts.ca_pem is None):
3174 1651d116 Michael Hanselmann
    _Fail("Cluster certificate can only be used for both key and CA")
3175 1651d116 Michael Hanselmann
3176 2ad5550d Michael Hanselmann
  (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \
3177 1651d116 Michael Hanselmann
    _GetImportExportIoCommand(instance, mode, ieio, ieioargs)
3178 1651d116 Michael Hanselmann
3179 eb630f50 Michael Hanselmann
  if opts.key_name is None:
3180 1651d116 Michael Hanselmann
    # Use server.pem
3181 1651d116 Michael Hanselmann
    key_path = constants.NODED_CERT_FILE
3182 1651d116 Michael Hanselmann
    cert_path = constants.NODED_CERT_FILE
3183 eb630f50 Michael Hanselmann
    assert opts.ca_pem is None
3184 1651d116 Michael Hanselmann
  else:
3185 1651d116 Michael Hanselmann
    (_, key_path, cert_path) = _GetX509Filenames(constants.CRYPTO_KEYS_DIR,
3186 eb630f50 Michael Hanselmann
                                                 opts.key_name)
3187 eb630f50 Michael Hanselmann
    assert opts.ca_pem is not None
3188 1651d116 Michael Hanselmann
3189 63bcea2a Michael Hanselmann
  for i in [key_path, cert_path]:
3190 dcaabc4f Michael Hanselmann
    if not os.path.exists(i):
3191 63bcea2a Michael Hanselmann
      _Fail("File '%s' does not exist" % i)
3192 63bcea2a Michael Hanselmann
3193 6613661a Iustin Pop
  status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
3194 1651d116 Michael Hanselmann
  try:
3195 1651d116 Michael Hanselmann
    status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE)
3196 1651d116 Michael Hanselmann
    pid_file = utils.PathJoin(status_dir, _IES_PID_FILE)
3197 63bcea2a Michael Hanselmann
    ca_file = utils.PathJoin(status_dir, _IES_CA_FILE)
3198 1651d116 Michael Hanselmann
3199 eb630f50 Michael Hanselmann
    if opts.ca_pem is None:
3200 1651d116 Michael Hanselmann
      # Use server.pem
3201 63bcea2a Michael Hanselmann
      ca = utils.ReadFile(constants.NODED_CERT_FILE)
3202 eb630f50 Michael Hanselmann
    else:
3203 eb630f50 Michael Hanselmann
      ca = opts.ca_pem
3204 63bcea2a Michael Hanselmann
3205 eb630f50 Michael Hanselmann
    # Write CA file
3206 63bcea2a Michael Hanselmann
    utils.WriteFile(ca_file, data=ca, mode=0400)
3207 1651d116 Michael Hanselmann
3208 1651d116 Michael Hanselmann
    cmd = [
3209 1651d116 Michael Hanselmann
      constants.IMPORT_EXPORT_DAEMON,
3210 1651d116 Michael Hanselmann
      status_file, mode,
3211 1651d116 Michael Hanselmann
      "--key=%s" % key_path,
3212 1651d116 Michael Hanselmann
      "--cert=%s" % cert_path,
3213 63bcea2a Michael Hanselmann
      "--ca=%s" % ca_file,
3214 1651d116 Michael Hanselmann
      ]
3215 1651d116 Michael Hanselmann
3216 1651d116 Michael Hanselmann
    if host:
3217 1651d116 Michael Hanselmann
      cmd.append("--host=%s" % host)
3218 1651d116 Michael Hanselmann
3219 1651d116 Michael Hanselmann
    if port:
3220 1651d116 Michael Hanselmann
      cmd.append("--port=%s" % port)
3221 1651d116 Michael Hanselmann
3222 855d2fc7 Michael Hanselmann
    if opts.ipv6:
3223 855d2fc7 Michael Hanselmann
      cmd.append("--ipv6")
3224 855d2fc7 Michael Hanselmann
    else:
3225 855d2fc7 Michael Hanselmann
      cmd.append("--ipv4")
3226 855d2fc7 Michael Hanselmann
3227 a5310c2a Michael Hanselmann
    if opts.compress:
3228 a5310c2a Michael Hanselmann
      cmd.append("--compress=%s" % opts.compress)
3229 a5310c2a Michael Hanselmann
3230 af1d39b1 Michael Hanselmann
    if opts.magic:
3231 af1d39b1 Michael Hanselmann
      cmd.append("--magic=%s" % opts.magic)
3232 af1d39b1 Michael Hanselmann
3233 2ad5550d Michael Hanselmann
    if exp_size is not None:
3234 2ad5550d Michael Hanselmann
      cmd.append("--expected-size=%s" % exp_size)
3235 2ad5550d Michael Hanselmann
3236 1651d116 Michael Hanselmann
    if cmd_prefix:
3237 1651d116 Michael Hanselmann
      cmd.append("--cmd-prefix=%s" % cmd_prefix)
3238 1651d116 Michael Hanselmann
3239 1651d116 Michael Hanselmann
    if cmd_suffix:
3240 1651d116 Michael Hanselmann
      cmd.append("--cmd-suffix=%s" % cmd_suffix)
3241 1651d116 Michael Hanselmann
3242 4478301b Michael Hanselmann
    if mode == constants.IEM_EXPORT:
3243 4478301b Michael Hanselmann
      # Retry connection a few times when connecting to remote peer
3244 4478301b Michael Hanselmann
      cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
3245 4478301b Michael Hanselmann
      cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
3246 4478301b Michael Hanselmann
    elif opts.connect_timeout is not None:
3247 4478301b Michael Hanselmann
      assert mode == constants.IEM_IMPORT
3248 4478301b Michael Hanselmann
      # Overall timeout for establishing connection while listening
3249 4478301b Michael Hanselmann
      cmd.append("--connect-timeout=%s" % opts.connect_timeout)
3250 4478301b Michael Hanselmann
3251 6aa7a354 Iustin Pop
    logfile = _InstanceLogName(prefix, instance.os, instance.name, component)
3252 1651d116 Michael Hanselmann
3253 1651d116 Michael Hanselmann
    # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
3254 1651d116 Michael Hanselmann
    # support for receiving a file descriptor for output
3255 1651d116 Michael Hanselmann
    utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file,
3256 1651d116 Michael Hanselmann
                      output=logfile)
3257 1651d116 Michael Hanselmann
3258 1651d116 Michael Hanselmann
    # The import/export name is simply the status directory name
3259 1651d116 Michael Hanselmann
    return os.path.basename(status_dir)
3260 1651d116 Michael Hanselmann
3261 1651d116 Michael Hanselmann
  except Exception:
3262 1651d116 Michael Hanselmann
    shutil.rmtree(status_dir, ignore_errors=True)
3263 1651d116 Michael Hanselmann
    raise
3264 1651d116 Michael Hanselmann
3265 1651d116 Michael Hanselmann
3266 1651d116 Michael Hanselmann
def GetImportExportStatus(names):
3267 1651d116 Michael Hanselmann
  """Returns import/export daemon status.
3268 1651d116 Michael Hanselmann

3269 1651d116 Michael Hanselmann
  @type names: sequence
3270 1651d116 Michael Hanselmann
  @param names: List of names
3271 1651d116 Michael Hanselmann
  @rtype: List of dicts
3272 1651d116 Michael Hanselmann
  @return: Returns a list of the state of each named import/export or None if a
3273 1651d116 Michael Hanselmann
           status couldn't be read
3274 1651d116 Michael Hanselmann

3275 1651d116 Michael Hanselmann
  """
3276 1651d116 Michael Hanselmann
  result = []
3277 1651d116 Michael Hanselmann
3278 1651d116 Michael Hanselmann
  for name in names:
3279 1651d116 Michael Hanselmann
    status_file = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name,
3280 1651d116 Michael Hanselmann
                                 _IES_STATUS_FILE)
3281 1651d116 Michael Hanselmann
3282 1651d116 Michael Hanselmann
    try:
3283 1651d116 Michael Hanselmann
      data = utils.ReadFile(status_file)
3284 1651d116 Michael Hanselmann
    except EnvironmentError, err:
3285 1651d116 Michael Hanselmann
      if err.errno != errno.ENOENT:
3286 1651d116 Michael Hanselmann
        raise
3287 1651d116 Michael Hanselmann
      data = None
3288 1651d116 Michael Hanselmann
3289 1651d116 Michael Hanselmann
    if not data:
3290 1651d116 Michael Hanselmann
      result.append(None)
3291 1651d116 Michael Hanselmann
      continue
3292 1651d116 Michael Hanselmann
3293 1651d116 Michael Hanselmann
    result.append(serializer.LoadJson(data))
3294 1651d116 Michael Hanselmann
3295 1651d116 Michael Hanselmann
  return result
3296 1651d116 Michael Hanselmann
3297 1651d116 Michael Hanselmann
3298 f81c4737 Michael Hanselmann
def AbortImportExport(name):
3299 f81c4737 Michael Hanselmann
  """Sends SIGTERM to a running import/export daemon.
3300 f81c4737 Michael Hanselmann

3301 f81c4737 Michael Hanselmann
  """
3302 f81c4737 Michael Hanselmann
  logging.info("Abort import/export %s", name)
3303 f81c4737 Michael Hanselmann
3304 f81c4737 Michael Hanselmann
  status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name)
3305 f81c4737 Michael Hanselmann
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3306 f81c4737 Michael Hanselmann
3307 f81c4737 Michael Hanselmann
  if pid:
3308 f81c4737 Michael Hanselmann
    logging.info("Import/export %s is running with PID %s, sending SIGTERM",
3309 f81c4737 Michael Hanselmann
                 name, pid)
3310 560cbec1 Michael Hanselmann
    utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM)
3311 f81c4737 Michael Hanselmann
3312 f81c4737 Michael Hanselmann
3313 1651d116 Michael Hanselmann
def CleanupImportExport(name):
3314 1651d116 Michael Hanselmann
  """Cleanup after an import or export.
3315 1651d116 Michael Hanselmann

3316 1651d116 Michael Hanselmann
  If the import/export daemon is still running it's killed. Afterwards the
3317 1651d116 Michael Hanselmann
  whole status directory is removed.
3318 1651d116 Michael Hanselmann

3319 1651d116 Michael Hanselmann
  """
3320 1651d116 Michael Hanselmann
  logging.info("Finalizing import/export %s", name)
3321 1651d116 Michael Hanselmann
3322 1651d116 Michael Hanselmann
  status_dir = utils.PathJoin(constants.IMPORT_EXPORT_DIR, name)
3323 1651d116 Michael Hanselmann
3324 debed9ae Michael Hanselmann
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3325 1651d116 Michael Hanselmann
3326 1651d116 Michael Hanselmann
  if pid:
3327 1651d116 Michael Hanselmann
    logging.info("Import/export %s is still running with PID %s",
3328 1651d116 Michael Hanselmann
                 name, pid)
3329 1651d116 Michael Hanselmann
    utils.KillProcess(pid, waitpid=False)
3330 1651d116 Michael Hanselmann
3331 1651d116 Michael Hanselmann
  shutil.rmtree(status_dir, ignore_errors=True)
3332 1651d116 Michael Hanselmann
3333 1651d116 Michael Hanselmann
3334 6b93ec9d Iustin Pop
def _FindDisks(nodes_ip, disks):
3335 6b93ec9d Iustin Pop
  """Sets the physical ID on disks and returns the block devices.
3336 6b93ec9d Iustin Pop

3337 6b93ec9d Iustin Pop
  """
3338 6b93ec9d Iustin Pop
  # set the correct physical ID
3339 b705c7a6 Manuel Franceschini
  my_name = netutils.Hostname.GetSysName()
3340 6b93ec9d Iustin Pop
  for cf in disks:
3341 6b93ec9d Iustin Pop
    cf.SetPhysicalID(my_name, nodes_ip)
3342 6b93ec9d Iustin Pop
3343 6b93ec9d Iustin Pop
  bdevs = []
3344 6b93ec9d Iustin Pop
3345 6b93ec9d Iustin Pop
  for cf in disks:
3346 6b93ec9d Iustin Pop
    rd = _RecursiveFindBD(cf)
3347 6b93ec9d Iustin Pop
    if rd is None:
3348 5a533f8a Iustin Pop
      _Fail("Can't find device %s", cf)
3349 6b93ec9d Iustin Pop
    bdevs.append(rd)
3350 5a533f8a Iustin Pop
  return bdevs
3351 6b93ec9d Iustin Pop
3352 6b93ec9d Iustin Pop
3353 6b93ec9d Iustin Pop
def DrbdDisconnectNet(nodes_ip, disks):
3354 6b93ec9d Iustin Pop
  """Disconnects the network on a list of drbd devices.
3355 6b93ec9d Iustin Pop

3356 6b93ec9d Iustin Pop
  """
3357 5a533f8a Iustin Pop
  bdevs = _FindDisks(nodes_ip, disks)
3358 6b93ec9d Iustin Pop
3359 6b93ec9d Iustin Pop
  # disconnect disks
3360 6b93ec9d Iustin Pop
  for rd in bdevs:
3361 6b93ec9d Iustin Pop
    try:
3362 6b93ec9d Iustin Pop
      rd.DisconnectNet()
3363 6b93ec9d Iustin Pop
    except errors.BlockDeviceError, err:
3364 2cc6781a Iustin Pop
      _Fail("Can't change network configuration to standalone mode: %s",
3365 2cc6781a Iustin Pop
            err, exc=True)
3366 6b93ec9d Iustin Pop
3367 6b93ec9d Iustin Pop
3368 6b93ec9d Iustin Pop
def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster):
3369 6b93ec9d Iustin Pop
  """Attaches the network on a list of drbd devices.
3370 6b93ec9d Iustin Pop

3371 6b93ec9d Iustin Pop
  """
3372 5a533f8a Iustin Pop
  bdevs = _FindDisks(nodes_ip, disks)
3373 6b93ec9d Iustin Pop
3374 6b93ec9d Iustin Pop
  if multimaster:
3375 53c776b5 Iustin Pop
    for idx, rd in enumerate(bdevs):
3376 6b93ec9d Iustin Pop
      try:
3377 53c776b5 Iustin Pop
        _SymlinkBlockDev(instance_name, rd.dev_path, idx)
3378 6b93ec9d Iustin Pop
      except EnvironmentError, err:
3379 2cc6781a Iustin Pop
        _Fail("Can't create symlink: %s", err)
3380 6b93ec9d Iustin Pop
  # reconnect disks, switch to new master configuration and if
3381 6b93ec9d Iustin Pop
  # needed primary mode
3382 6b93ec9d Iustin Pop
  for rd in bdevs:
3383 6b93ec9d Iustin Pop
    try:
3384 6b93ec9d Iustin Pop
      rd.AttachNet(multimaster)
3385 6b93ec9d Iustin Pop
    except errors.BlockDeviceError, err:
3386 2cc6781a Iustin Pop
      _Fail("Can't change network configuration: %s", err)
3387 3c0cdc83 Michael Hanselmann
3388 6b93ec9d Iustin Pop
  # wait until the disks are connected; we need to retry the re-attach
3389 6b93ec9d Iustin Pop
  # if the device becomes standalone, as this might happen if the one
3390 6b93ec9d Iustin Pop
  # node disconnects and reconnects in a different mode before the
3391 6b93ec9d Iustin Pop
  # other node reconnects; in this case, one or both of the nodes will
3392 6b93ec9d Iustin Pop
  # decide it has wrong configuration and switch to standalone
3393 3c0cdc83 Michael Hanselmann
3394 3c0cdc83 Michael Hanselmann
  def _Attach():
3395 6b93ec9d Iustin Pop
    all_connected = True
3396 3c0cdc83 Michael Hanselmann
3397 6b93ec9d Iustin Pop
    for rd in bdevs:
3398 6b93ec9d Iustin Pop
      stats = rd.GetProcStatus()
3399 3c0cdc83 Michael Hanselmann
3400 3c0cdc83 Michael Hanselmann
      all_connected = (all_connected and
3401 3c0cdc83 Michael Hanselmann
                       (stats.is_connected or stats.is_in_resync))
3402 3c0cdc83 Michael Hanselmann
3403 6b93ec9d Iustin Pop
      if stats.is_standalone:
3404 6b93ec9d Iustin Pop
        # peer had different config info and this node became
3405 6b93ec9d Iustin Pop
        # standalone, even though this should not happen with the
3406 6b93ec9d Iustin Pop
        # new staged way of changing disk configs
3407 6b93ec9d Iustin Pop
        try:
3408 c738375b Iustin Pop
          rd.AttachNet(multimaster)
3409 6b93ec9d Iustin Pop
        except errors.BlockDeviceError, err:
3410 2cc6781a Iustin Pop
          _Fail("Can't change network configuration: %s", err)
3411 3c0cdc83 Michael Hanselmann
3412 3c0cdc83 Michael Hanselmann
    if not all_connected:
3413 3c0cdc83 Michael Hanselmann
      raise utils.RetryAgain()
3414 3c0cdc83 Michael Hanselmann
3415 3c0cdc83 Michael Hanselmann
  try:
3416 3c0cdc83 Michael Hanselmann
    # Start with a delay of 100 miliseconds and go up to 5 seconds
3417 3c0cdc83 Michael Hanselmann
    utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60)
3418 3c0cdc83 Michael Hanselmann
  except utils.RetryTimeout:
3419 afdc3985 Iustin Pop
    _Fail("Timeout in disk reconnecting")
3420 3c0cdc83 Michael Hanselmann
3421 6b93ec9d Iustin Pop
  if multimaster:
3422 6b93ec9d Iustin Pop
    # change to primary mode
3423 6b93ec9d Iustin Pop
    for rd in bdevs:
3424 d3da87b8 Iustin Pop
      try:
3425 d3da87b8 Iustin Pop
        rd.Open()
3426 d3da87b8 Iustin Pop
      except errors.BlockDeviceError, err:
3427 2cc6781a Iustin Pop
        _Fail("Can't change to primary mode: %s", err)
3428 6b93ec9d Iustin Pop
3429 6b93ec9d Iustin Pop
3430 6b93ec9d Iustin Pop
def DrbdWaitSync(nodes_ip, disks):
3431 6b93ec9d Iustin Pop
  """Wait until DRBDs have synchronized.
3432 6b93ec9d Iustin Pop

3433 6b93ec9d Iustin Pop
  """
3434 db8667b7 Iustin Pop
  def _helper(rd):
3435 db8667b7 Iustin Pop
    stats = rd.GetProcStatus()
3436 db8667b7 Iustin Pop
    if not (stats.is_connected or stats.is_in_resync):
3437 db8667b7 Iustin Pop
      raise utils.RetryAgain()
3438 db8667b7 Iustin Pop
    return stats
3439 db8667b7 Iustin Pop
3440 5a533f8a Iustin Pop
  bdevs = _FindDisks(nodes_ip, disks)
3441 6b93ec9d Iustin Pop
3442 6b93ec9d Iustin Pop
  min_resync = 100
3443 6b93ec9d Iustin Pop
  alldone = True
3444 6b93ec9d Iustin Pop
  for rd in bdevs:
3445 db8667b7 Iustin Pop
    try:
3446 db8667b7 Iustin Pop
      # poll each second for 15 seconds
3447 db8667b7 Iustin Pop
      stats = utils.Retry(_helper, 1, 15, args=[rd])
3448 db8667b7 Iustin Pop
    except utils.RetryTimeout:
3449 db8667b7 Iustin Pop
      stats = rd.GetProcStatus()
3450 db8667b7 Iustin Pop
      # last check
3451 db8667b7 Iustin Pop
      if not (stats.is_connected or stats.is_in_resync):
3452 db8667b7 Iustin Pop
        _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
3453 6b93ec9d Iustin Pop
    alldone = alldone and (not stats.is_in_resync)
3454 6b93ec9d Iustin Pop
    if stats.sync_percent is not None:
3455 6b93ec9d Iustin Pop
      min_resync = min(min_resync, stats.sync_percent)
3456 afdc3985 Iustin Pop
3457 c26a6bd2 Iustin Pop
  return (alldone, min_resync)
3458 6b93ec9d Iustin Pop
3459 6b93ec9d Iustin Pop
3460 c46b9782 Luca Bigliardi
def GetDrbdUsermodeHelper():
3461 c46b9782 Luca Bigliardi
  """Returns DRBD usermode helper currently configured.
3462 c46b9782 Luca Bigliardi

3463 c46b9782 Luca Bigliardi
  """
3464 c46b9782 Luca Bigliardi
  try:
3465 c46b9782 Luca Bigliardi
    return bdev.BaseDRBD.GetUsermodeHelper()
3466 c46b9782 Luca Bigliardi
  except errors.BlockDeviceError, err:
3467 c46b9782 Luca Bigliardi
    _Fail(str(err))
3468 c46b9782 Luca Bigliardi
3469 c46b9782 Luca Bigliardi
3470 f5118ade Iustin Pop
def PowercycleNode(hypervisor_type):
3471 f5118ade Iustin Pop
  """Hard-powercycle the node.
3472 f5118ade Iustin Pop

3473 f5118ade Iustin Pop
  Because we need to return first, and schedule the powercycle in the
3474 f5118ade Iustin Pop
  background, we won't be able to report failures nicely.
3475 f5118ade Iustin Pop

3476 f5118ade Iustin Pop
  """
3477 f5118ade Iustin Pop
  hyper = hypervisor.GetHypervisor(hypervisor_type)
3478 f5118ade Iustin Pop
  try:
3479 f5118ade Iustin Pop
    pid = os.fork()
3480 29921401 Iustin Pop
  except OSError:
3481 f5118ade Iustin Pop
    # if we can't fork, we'll pretend that we're in the child process
3482 f5118ade Iustin Pop
    pid = 0
3483 f5118ade Iustin Pop
  if pid > 0:
3484 c26a6bd2 Iustin Pop
    return "Reboot scheduled in 5 seconds"
3485 1af6ac0f Luca Bigliardi
  # ensure the child is running on ram
3486 1af6ac0f Luca Bigliardi
  try:
3487 1af6ac0f Luca Bigliardi
    utils.Mlockall()
3488 b459a848 Andrea Spadaccini
  except Exception: # pylint: disable=W0703
3489 1af6ac0f Luca Bigliardi
    pass
3490 f5118ade Iustin Pop
  time.sleep(5)
3491 f5118ade Iustin Pop
  hyper.PowercycleNode()
3492 f5118ade Iustin Pop
3493 f5118ade Iustin Pop
3494 a8083063 Iustin Pop
class HooksRunner(object):
3495 a8083063 Iustin Pop
  """Hook runner.
3496 a8083063 Iustin Pop

3497 10c2650b Iustin Pop
  This class is instantiated on the node side (ganeti-noded) and not
3498 10c2650b Iustin Pop
  on the master side.
3499 a8083063 Iustin Pop

3500 a8083063 Iustin Pop
  """
3501 a8083063 Iustin Pop
  def __init__(self, hooks_base_dir=None):
3502 a8083063 Iustin Pop
    """Constructor for hooks runner.
3503 a8083063 Iustin Pop

3504 10c2650b Iustin Pop
    @type hooks_base_dir: str or None
3505 10c2650b Iustin Pop
    @param hooks_base_dir: if not None, this overrides the
3506 10c2650b Iustin Pop
        L{constants.HOOKS_BASE_DIR} (useful for unittests)
3507 a8083063 Iustin Pop

3508 a8083063 Iustin Pop
    """
3509 a8083063 Iustin Pop
    if hooks_base_dir is None:
3510 a8083063 Iustin Pop
      hooks_base_dir = constants.HOOKS_BASE_DIR
3511 fe267188 Iustin Pop
    # yeah, _BASE_DIR is not valid for attributes, we use it like a
3512 fe267188 Iustin Pop
    # constant
3513 b459a848 Andrea Spadaccini
    self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
3514 a8083063 Iustin Pop
3515 0fa481f5 Andrea Spadaccini
  def RunLocalHooks(self, node_list, hpath, phase, env):
3516 0fa481f5 Andrea Spadaccini
    """Check that the hooks will be run only locally and then run them.
3517 0fa481f5 Andrea Spadaccini

3518 0fa481f5 Andrea Spadaccini
    """
3519 0fa481f5 Andrea Spadaccini
    assert len(node_list) == 1
3520 0fa481f5 Andrea Spadaccini
    node = node_list[0]
3521 0fa481f5 Andrea Spadaccini
    _, myself = ssconf.GetMasterAndMyself()
3522 0fa481f5 Andrea Spadaccini
    assert node == myself
3523 0fa481f5 Andrea Spadaccini
3524 0fa481f5 Andrea Spadaccini
    results = self.RunHooks(hpath, phase, env)
3525 0fa481f5 Andrea Spadaccini
3526 0fa481f5 Andrea Spadaccini
    # Return values in the form expected by HooksMaster
3527 0fa481f5 Andrea Spadaccini
    return {node: (None, False, results)}
3528 0fa481f5 Andrea Spadaccini
3529 a8083063 Iustin Pop
  def RunHooks(self, hpath, phase, env):
3530 a8083063 Iustin Pop
    """Run the scripts in the hooks directory.
3531 a8083063 Iustin Pop

3532 10c2650b Iustin Pop
    @type hpath: str
3533 10c2650b Iustin Pop
    @param hpath: the path to the hooks directory which
3534 10c2650b Iustin Pop
        holds the scripts
3535 10c2650b Iustin Pop
    @type phase: str
3536 10c2650b Iustin Pop
    @param phase: either L{constants.HOOKS_PHASE_PRE} or
3537 10c2650b Iustin Pop
        L{constants.HOOKS_PHASE_POST}
3538 10c2650b Iustin Pop
    @type env: dict
3539 10c2650b Iustin Pop
    @param env: dictionary with the environment for the hook
3540 10c2650b Iustin Pop
    @rtype: list
3541 10c2650b Iustin Pop
    @return: list of 3-element tuples:
3542 10c2650b Iustin Pop
      - script path
3543 10c2650b Iustin Pop
      - script result, either L{constants.HKR_SUCCESS} or
3544 10c2650b Iustin Pop
        L{constants.HKR_FAIL}
3545 10c2650b Iustin Pop
      - output of the script
3546 10c2650b Iustin Pop

3547 10c2650b Iustin Pop
    @raise errors.ProgrammerError: for invalid input
3548 10c2650b Iustin Pop
        parameters
3549 a8083063 Iustin Pop

3550 a8083063 Iustin Pop
    """
3551 a8083063 Iustin Pop
    if phase == constants.HOOKS_PHASE_PRE:
3552 a8083063 Iustin Pop
      suffix = "pre"
3553 a8083063 Iustin Pop
    elif phase == constants.HOOKS_PHASE_POST:
3554 a8083063 Iustin Pop
      suffix = "post"
3555 a8083063 Iustin Pop
    else:
3556 3fb4f740 Iustin Pop
      _Fail("Unknown hooks phase '%s'", phase)
3557 3fb4f740 Iustin Pop
3558 a8083063 Iustin Pop
    subdir = "%s-%s.d" % (hpath, suffix)
3559 0411c011 Iustin Pop
    dir_name = utils.PathJoin(self._BASE_DIR, subdir)
3560 6bb65e3a Guido Trotter
3561 6bb65e3a Guido Trotter
    results = []
3562 a9b7e346 Iustin Pop
3563 a9b7e346 Iustin Pop
    if not os.path.isdir(dir_name):
3564 a9b7e346 Iustin Pop
      # for non-existing/non-dirs, we simply exit instead of logging a
3565 a9b7e346 Iustin Pop
      # warning at every operation
3566 a9b7e346 Iustin Pop
      return results
3567 a9b7e346 Iustin Pop
3568 a9b7e346 Iustin Pop
    runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
3569 a9b7e346 Iustin Pop
3570 6bb65e3a Guido Trotter
    for (relname, relstatus, runresult)  in runparts_results:
3571 6bb65e3a Guido Trotter
      if relstatus == constants.RUNPARTS_SKIP:
3572 a8083063 Iustin Pop
        rrval = constants.HKR_SKIP
3573 a8083063 Iustin Pop
        output = ""
3574 6bb65e3a Guido Trotter
      elif relstatus == constants.RUNPARTS_ERR:
3575 6bb65e3a Guido Trotter
        rrval = constants.HKR_FAIL
3576 6bb65e3a Guido Trotter
        output = "Hook script execution error: %s" % runresult
3577 6bb65e3a Guido Trotter
      elif relstatus == constants.RUNPARTS_RUN:
3578 6bb65e3a Guido Trotter
        if runresult.failed:
3579 a8083063 Iustin Pop
          rrval = constants.HKR_FAIL
3580 a8083063 Iustin Pop
        else:
3581 6bb65e3a Guido Trotter
          rrval = constants.HKR_SUCCESS
3582 6bb65e3a Guido Trotter
        output = utils.SafeEncode(runresult.output.strip())
3583 6bb65e3a Guido Trotter
      results.append(("%s/%s" % (subdir, relname), rrval, output))
3584 6bb65e3a Guido Trotter
3585 6bb65e3a Guido Trotter
    return results
3586 3f78eef2 Iustin Pop
3587 3f78eef2 Iustin Pop
3588 8d528b7c Iustin Pop
class IAllocatorRunner(object):
3589 8d528b7c Iustin Pop
  """IAllocator runner.
3590 8d528b7c Iustin Pop

3591 8d528b7c Iustin Pop
  This class is instantiated on the node side (ganeti-noded) and not on
3592 8d528b7c Iustin Pop
  the master side.
3593 8d528b7c Iustin Pop

3594 8d528b7c Iustin Pop
  """
3595 7e950d31 Iustin Pop
  @staticmethod
3596 7e950d31 Iustin Pop
  def Run(name, idata):
3597 8d528b7c Iustin Pop
    """Run an iallocator script.
3598 8d528b7c Iustin Pop

3599 10c2650b Iustin Pop
    @type name: str
3600 10c2650b Iustin Pop
    @param name: the iallocator script name
3601 10c2650b Iustin Pop
    @type idata: str
3602 10c2650b Iustin Pop
    @param idata: the allocator input data
3603 10c2650b Iustin Pop

3604 10c2650b Iustin Pop
    @rtype: tuple
3605 87f5c298 Iustin Pop
    @return: two element tuple of:
3606 87f5c298 Iustin Pop
       - status
3607 87f5c298 Iustin Pop
       - either error message or stdout of allocator (for success)
3608 8d528b7c Iustin Pop

3609 8d528b7c Iustin Pop
    """
3610 8d528b7c Iustin Pop
    alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
3611 8d528b7c Iustin Pop
                                  os.path.isfile)
3612 8d528b7c Iustin Pop
    if alloc_script is None:
3613 87f5c298 Iustin Pop
      _Fail("iallocator module '%s' not found in the search path", name)
3614 8d528b7c Iustin Pop
3615 8d528b7c Iustin Pop
    fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
3616 8d528b7c Iustin Pop
    try:
3617 8d528b7c Iustin Pop
      os.write(fd, idata)
3618 8d528b7c Iustin Pop
      os.close(fd)
3619 8d528b7c Iustin Pop
      result = utils.RunCmd([alloc_script, fin_name])
3620 8d528b7c Iustin Pop
      if result.failed:
3621 87f5c298 Iustin Pop
        _Fail("iallocator module '%s' failed: %s, output '%s'",
3622 87f5c298 Iustin Pop
              name, result.fail_reason, result.output)
3623 8d528b7c Iustin Pop
    finally:
3624 8d528b7c Iustin Pop
      os.unlink(fin_name)
3625 8d528b7c Iustin Pop
3626 c26a6bd2 Iustin Pop
    return result.stdout
3627 8d528b7c Iustin Pop
3628 8d528b7c Iustin Pop
3629 3f78eef2 Iustin Pop
class DevCacheManager(object):
3630 c99a3cc0 Manuel Franceschini
  """Simple class for managing a cache of block device information.
3631 3f78eef2 Iustin Pop

3632 3f78eef2 Iustin Pop
  """
3633 3f78eef2 Iustin Pop
  _DEV_PREFIX = "/dev/"
3634 3f78eef2 Iustin Pop
  _ROOT_DIR = constants.BDEV_CACHE_DIR
3635 3f78eef2 Iustin Pop
3636 3f78eef2 Iustin Pop
  @classmethod
3637 3f78eef2 Iustin Pop
  def _ConvertPath(cls, dev_path):
3638 3f78eef2 Iustin Pop
    """Converts a /dev/name path to the cache file name.
3639 3f78eef2 Iustin Pop

3640 3f78eef2 Iustin Pop
    This replaces slashes with underscores and strips the /dev
3641 10c2650b Iustin Pop
    prefix. It then returns the full path to the cache file.
3642 10c2650b Iustin Pop

3643 10c2650b Iustin Pop
    @type dev_path: str
3644 10c2650b Iustin Pop
    @param dev_path: the C{/dev/} path name
3645 10c2650b Iustin Pop
    @rtype: str
3646 10c2650b Iustin Pop
    @return: the converted path name
3647 3f78eef2 Iustin Pop

3648 3f78eef2 Iustin Pop
    """
3649 3f78eef2 Iustin Pop
    if dev_path.startswith(cls._DEV_PREFIX):
3650 3f78eef2 Iustin Pop
      dev_path = dev_path[len(cls._DEV_PREFIX):]
3651 3f78eef2 Iustin Pop
    dev_path = dev_path.replace("/", "_")
3652 0411c011 Iustin Pop
    fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
3653 3f78eef2 Iustin Pop
    return fpath
3654 3f78eef2 Iustin Pop
3655 3f78eef2 Iustin Pop
  @classmethod
3656 3f78eef2 Iustin Pop
  def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
3657 3f78eef2 Iustin Pop
    """Updates the cache information for a given device.
3658 3f78eef2 Iustin Pop

3659 10c2650b Iustin Pop
    @type dev_path: str
3660 10c2650b Iustin Pop
    @param dev_path: the pathname of the device
3661 10c2650b Iustin Pop
    @type owner: str
3662 10c2650b Iustin Pop
    @param owner: the owner (instance name) of the device
3663 10c2650b Iustin Pop
    @type on_primary: bool
3664 10c2650b Iustin Pop
    @param on_primary: whether this is the primary
3665 10c2650b Iustin Pop
        node nor not
3666 10c2650b Iustin Pop
    @type iv_name: str
3667 10c2650b Iustin Pop
    @param iv_name: the instance-visible name of the
3668 c41eea6e Iustin Pop
        device, as in objects.Disk.iv_name
3669 10c2650b Iustin Pop

3670 10c2650b Iustin Pop
    @rtype: None
3671 10c2650b Iustin Pop

3672 3f78eef2 Iustin Pop
    """
3673 cf5a8306 Iustin Pop
    if dev_path is None:
3674 18682bca Iustin Pop
      logging.error("DevCacheManager.UpdateCache got a None dev_path")
3675 cf5a8306 Iustin Pop
      return
3676 3f78eef2 Iustin Pop
    fpath = cls._ConvertPath(dev_path)
3677 3f78eef2 Iustin Pop
    if on_primary:
3678 3f78eef2 Iustin Pop
      state = "primary"
3679 3f78eef2 Iustin Pop
    else:
3680 3f78eef2 Iustin Pop
      state = "secondary"
3681 3f78eef2 Iustin Pop
    if iv_name is None:
3682 3f78eef2 Iustin Pop
      iv_name = "not_visible"
3683 3f78eef2 Iustin Pop
    fdata = "%s %s %s\n" % (str(owner), state, iv_name)
3684 3f78eef2 Iustin Pop
    try:
3685 3f78eef2 Iustin Pop
      utils.WriteFile(fpath, data=fdata)
3686 3f78eef2 Iustin Pop
    except EnvironmentError, err:
3687 29921401 Iustin Pop
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
3688 3f78eef2 Iustin Pop
3689 3f78eef2 Iustin Pop
  @classmethod
3690 3f78eef2 Iustin Pop
  def RemoveCache(cls, dev_path):
3691 3f78eef2 Iustin Pop
    """Remove data for a dev_path.
3692 3f78eef2 Iustin Pop

3693 3865ca48 Michael Hanselmann
    This is just a wrapper over L{utils.io.RemoveFile} with a converted
3694 10c2650b Iustin Pop
    path name and logging.
3695 10c2650b Iustin Pop

3696 10c2650b Iustin Pop
    @type dev_path: str
3697 10c2650b Iustin Pop
    @param dev_path: the pathname of the device
3698 10c2650b Iustin Pop

3699 10c2650b Iustin Pop
    @rtype: None
3700 10c2650b Iustin Pop

3701 3f78eef2 Iustin Pop
    """
3702 cf5a8306 Iustin Pop
    if dev_path is None:
3703 18682bca Iustin Pop
      logging.error("DevCacheManager.RemoveCache got a None dev_path")
3704 cf5a8306 Iustin Pop
      return
3705 3f78eef2 Iustin Pop
    fpath = cls._ConvertPath(dev_path)
3706 3f78eef2 Iustin Pop
    try:
3707 3f78eef2 Iustin Pop
      utils.RemoveFile(fpath)
3708 3f78eef2 Iustin Pop
    except EnvironmentError, err:
3709 29921401 Iustin Pop
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)