Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ 59726e15

History | View | Annotate | Download (121 kB)

1
#
2
#
3

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

    
21

    
22
"""Functions used by the node daemon
23

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

29
"""
30

    
31
# pylint: disable=E1103
32

    
33
# E1103: %s %r has no %r member (but some types could not be
34
# inferred), because the _TryOSFromDisk returns either (True, os_obj)
35
# or (False, "string") which confuses pylint
36

    
37

    
38
import os
39
import os.path
40
import shutil
41
import time
42
import stat
43
import errno
44
import re
45
import random
46
import logging
47
import tempfile
48
import zlib
49
import base64
50
import signal
51

    
52
from ganeti import errors
53
from ganeti import utils
54
from ganeti import ssh
55
from ganeti import hypervisor
56
from ganeti import constants
57
from ganeti import bdev
58
from ganeti import objects
59
from ganeti import ssconf
60
from ganeti import serializer
61
from ganeti import netutils
62
from ganeti import runtime
63
from ganeti import mcpu
64
from ganeti import compat
65
from ganeti import pathutils
66
from ganeti import vcluster
67
from ganeti import ht
68

    
69

    
70
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
71
_ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([
72
  pathutils.DATA_DIR,
73
  pathutils.JOB_QUEUE_ARCHIVE_DIR,
74
  pathutils.QUEUE_DIR,
75
  pathutils.CRYPTO_KEYS_DIR,
76
  ])
77
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
78
_X509_KEY_FILE = "key"
79
_X509_CERT_FILE = "cert"
80
_IES_STATUS_FILE = "status"
81
_IES_PID_FILE = "pid"
82
_IES_CA_FILE = "ca"
83

    
84
#: Valid LVS output line regex
85
_LVSLINE_REGEX = re.compile("^ *([^|]+)\|([^|]+)\|([0-9.]+)\|([^|]{6,})\|?$")
86

    
87
# Actions for the master setup script
88
_MASTER_START = "start"
89
_MASTER_STOP = "stop"
90

    
91
#: Maximum file permissions for remote command directory and executables
92
_RCMD_MAX_MODE = (stat.S_IRWXU |
93
                  stat.S_IRGRP | stat.S_IXGRP |
94
                  stat.S_IROTH | stat.S_IXOTH)
95

    
96
#: Delay before returning an error for remote commands
97
_RCMD_INVALID_DELAY = 10
98

    
99
#: How long to wait to acquire lock for remote commands (shorter than
100
#: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
101
#: command requests arrive
102
_RCMD_LOCK_TIMEOUT = _RCMD_INVALID_DELAY * 0.8
103

    
104

    
105
class RPCFail(Exception):
106
  """Class denoting RPC failure.
107

108
  Its argument is the error message.
109

110
  """
111

    
112

    
113
def _Fail(msg, *args, **kwargs):
114
  """Log an error and the raise an RPCFail exception.
115

116
  This exception is then handled specially in the ganeti daemon and
117
  turned into a 'failed' return type. As such, this function is a
118
  useful shortcut for logging the error and returning it to the master
119
  daemon.
120

121
  @type msg: string
122
  @param msg: the text of the exception
123
  @raise RPCFail
124

125
  """
126
  if args:
127
    msg = msg % args
128
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
129
    if "exc" in kwargs and kwargs["exc"]:
130
      logging.exception(msg)
131
    else:
132
      logging.error(msg)
133
  raise RPCFail(msg)
134

    
135

    
136
def _GetConfig():
137
  """Simple wrapper to return a SimpleStore.
138

139
  @rtype: L{ssconf.SimpleStore}
140
  @return: a SimpleStore instance
141

142
  """
143
  return ssconf.SimpleStore()
144

    
145

    
146
def _GetSshRunner(cluster_name):
147
  """Simple wrapper to return an SshRunner.
148

149
  @type cluster_name: str
150
  @param cluster_name: the cluster name, which is needed
151
      by the SshRunner constructor
152
  @rtype: L{ssh.SshRunner}
153
  @return: an SshRunner instance
154

155
  """
156
  return ssh.SshRunner(cluster_name)
157

    
158

    
159
def _Decompress(data):
160
  """Unpacks data compressed by the RPC client.
161

162
  @type data: list or tuple
163
  @param data: Data sent by RPC client
164
  @rtype: str
165
  @return: Decompressed data
166

167
  """
168
  assert isinstance(data, (list, tuple))
169
  assert len(data) == 2
170
  (encoding, content) = data
171
  if encoding == constants.RPC_ENCODING_NONE:
172
    return content
173
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
174
    return zlib.decompress(base64.b64decode(content))
175
  else:
176
    raise AssertionError("Unknown data encoding")
177

    
178

    
179
def _CleanDirectory(path, exclude=None):
180
  """Removes all regular files in a directory.
181

182
  @type path: str
183
  @param path: the directory to clean
184
  @type exclude: list
185
  @param exclude: list of files to be excluded, defaults
186
      to the empty list
187

188
  """
189
  if path not in _ALLOWED_CLEAN_DIRS:
190
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
191
          path)
192

    
193
  if not os.path.isdir(path):
194
    return
195
  if exclude is None:
196
    exclude = []
197
  else:
198
    # Normalize excluded paths
199
    exclude = [os.path.normpath(i) for i in exclude]
200

    
201
  for rel_name in utils.ListVisibleFiles(path):
202
    full_name = utils.PathJoin(path, rel_name)
203
    if full_name in exclude:
204
      continue
205
    if os.path.isfile(full_name) and not os.path.islink(full_name):
206
      utils.RemoveFile(full_name)
207

    
208

    
209
def _BuildUploadFileList():
210
  """Build the list of allowed upload files.
211

212
  This is abstracted so that it's built only once at module import time.
213

214
  """
215
  allowed_files = set([
216
    pathutils.CLUSTER_CONF_FILE,
217
    pathutils.ETC_HOSTS,
218
    pathutils.SSH_KNOWN_HOSTS_FILE,
219
    pathutils.VNC_PASSWORD_FILE,
220
    pathutils.RAPI_CERT_FILE,
221
    pathutils.SPICE_CERT_FILE,
222
    pathutils.SPICE_CACERT_FILE,
223
    pathutils.RAPI_USERS_FILE,
224
    pathutils.CONFD_HMAC_KEY,
225
    pathutils.CLUSTER_DOMAIN_SECRET_FILE,
226
    ])
227

    
228
  for hv_name in constants.HYPER_TYPES:
229
    hv_class = hypervisor.GetHypervisorClass(hv_name)
230
    allowed_files.update(hv_class.GetAncillaryFiles()[0])
231

    
232
  assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \
233
    "Allowed file storage paths should never be uploaded via RPC"
234

    
235
  return frozenset(allowed_files)
236

    
237

    
238
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
239

    
240

    
241
def JobQueuePurge():
242
  """Removes job queue files and archived jobs.
243

244
  @rtype: tuple
245
  @return: True, None
246

247
  """
248
  _CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE])
249
  _CleanDirectory(pathutils.JOB_QUEUE_ARCHIVE_DIR)
250

    
251

    
252
def GetMasterInfo():
253
  """Returns master information.
254

255
  This is an utility function to compute master information, either
256
  for consumption here or from the node daemon.
257

258
  @rtype: tuple
259
  @return: master_netdev, master_ip, master_name, primary_ip_family,
260
    master_netmask
261
  @raise RPCFail: in case of errors
262

263
  """
264
  try:
265
    cfg = _GetConfig()
266
    master_netdev = cfg.GetMasterNetdev()
267
    master_ip = cfg.GetMasterIP()
268
    master_netmask = cfg.GetMasterNetmask()
269
    master_node = cfg.GetMasterNode()
270
    primary_ip_family = cfg.GetPrimaryIPFamily()
271
  except errors.ConfigurationError, err:
272
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
273
  return (master_netdev, master_ip, master_node, primary_ip_family,
274
          master_netmask)
275

    
276

    
277
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
278
  """Decorator that runs hooks before and after the decorated function.
279

280
  @type hook_opcode: string
281
  @param hook_opcode: opcode of the hook
282
  @type hooks_path: string
283
  @param hooks_path: path of the hooks
284
  @type env_builder_fn: function
285
  @param env_builder_fn: function that returns a dictionary containing the
286
    environment variables for the hooks. Will get all the parameters of the
287
    decorated function.
288
  @raise RPCFail: in case of pre-hook failure
289

290
  """
291
  def decorator(fn):
292
    def wrapper(*args, **kwargs):
293
      _, myself = ssconf.GetMasterAndMyself()
294
      nodes = ([myself], [myself])  # these hooks run locally
295

    
296
      env_fn = compat.partial(env_builder_fn, *args, **kwargs)
297

    
298
      cfg = _GetConfig()
299
      hr = HooksRunner()
300
      hm = mcpu.HooksMaster(hook_opcode, hooks_path, nodes, hr.RunLocalHooks,
301
                            None, env_fn, logging.warning, cfg.GetClusterName(),
302
                            cfg.GetMasterNode())
303

    
304
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
305
      result = fn(*args, **kwargs)
306
      hm.RunPhase(constants.HOOKS_PHASE_POST)
307

    
308
      return result
309
    return wrapper
310
  return decorator
311

    
312

    
313
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
314
  """Builds environment variables for master IP hooks.
315

316
  @type master_params: L{objects.MasterNetworkParameters}
317
  @param master_params: network parameters of the master
318
  @type use_external_mip_script: boolean
319
  @param use_external_mip_script: whether to use an external master IP
320
    address setup script (unused, but necessary per the implementation of the
321
    _RunLocalHooks decorator)
322

323
  """
324
  # pylint: disable=W0613
325
  ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
326
  env = {
327
    "MASTER_NETDEV": master_params.netdev,
328
    "MASTER_IP": master_params.ip,
329
    "MASTER_NETMASK": str(master_params.netmask),
330
    "CLUSTER_IP_VERSION": str(ver),
331
  }
332

    
333
  return env
334

    
335

    
336
def _RunMasterSetupScript(master_params, action, use_external_mip_script):
337
  """Execute the master IP address setup script.
338

339
  @type master_params: L{objects.MasterNetworkParameters}
340
  @param master_params: network parameters of the master
341
  @type action: string
342
  @param action: action to pass to the script. Must be one of
343
    L{backend._MASTER_START} or L{backend._MASTER_STOP}
344
  @type use_external_mip_script: boolean
345
  @param use_external_mip_script: whether to use an external master IP
346
    address setup script
347
  @raise backend.RPCFail: if there are errors during the execution of the
348
    script
349

350
  """
351
  env = _BuildMasterIpEnv(master_params)
352

    
353
  if use_external_mip_script:
354
    setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT
355
  else:
356
    setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT
357

    
358
  result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
359

    
360
  if result.failed:
361
    _Fail("Failed to %s the master IP. Script return value: %s" %
362
          (action, result.exit_code), log=True)
363

    
364

    
365
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
366
               _BuildMasterIpEnv)
367
def ActivateMasterIp(master_params, use_external_mip_script):
368
  """Activate the IP address of the master daemon.
369

370
  @type master_params: L{objects.MasterNetworkParameters}
371
  @param master_params: network parameters of the master
372
  @type use_external_mip_script: boolean
373
  @param use_external_mip_script: whether to use an external master IP
374
    address setup script
375
  @raise RPCFail: in case of errors during the IP startup
376

377
  """
378
  _RunMasterSetupScript(master_params, _MASTER_START,
379
                        use_external_mip_script)
380

    
381

    
382
def StartMasterDaemons(no_voting):
383
  """Activate local node as master node.
384

385
  The function will start the master daemons (ganeti-masterd and ganeti-rapi).
386

387
  @type no_voting: boolean
388
  @param no_voting: whether to start ganeti-masterd without a node vote
389
      but still non-interactively
390
  @rtype: None
391

392
  """
393

    
394
  if no_voting:
395
    masterd_args = "--no-voting --yes-do-it"
396
  else:
397
    masterd_args = ""
398

    
399
  env = {
400
    "EXTRA_MASTERD_ARGS": masterd_args,
401
    }
402

    
403
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
404
  if result.failed:
405
    msg = "Can't start Ganeti master: %s" % result.output
406
    logging.error(msg)
407
    _Fail(msg)
408

    
409

    
410
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
411
               _BuildMasterIpEnv)
412
def DeactivateMasterIp(master_params, use_external_mip_script):
413
  """Deactivate the master IP on this node.
414

415
  @type master_params: L{objects.MasterNetworkParameters}
416
  @param master_params: network parameters of the master
417
  @type use_external_mip_script: boolean
418
  @param use_external_mip_script: whether to use an external master IP
419
    address setup script
420
  @raise RPCFail: in case of errors during the IP turndown
421

422
  """
423
  _RunMasterSetupScript(master_params, _MASTER_STOP,
424
                        use_external_mip_script)
425

    
426

    
427
def StopMasterDaemons():
428
  """Stop the master daemons on this node.
429

430
  Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
431

432
  @rtype: None
433

434
  """
435
  # TODO: log and report back to the caller the error failures; we
436
  # need to decide in which case we fail the RPC for this
437

    
438
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
439
  if result.failed:
440
    logging.error("Could not stop Ganeti master, command %s had exitcode %s"
441
                  " and error %s",
442
                  result.cmd, result.exit_code, result.output)
443

    
444

    
445
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
446
  """Change the netmask of the master IP.
447

448
  @param old_netmask: the old value of the netmask
449
  @param netmask: the new value of the netmask
450
  @param master_ip: the master IP
451
  @param master_netdev: the master network device
452

453
  """
454
  if old_netmask == netmask:
455
    return
456

    
457
  if not netutils.IPAddress.Own(master_ip):
458
    _Fail("The master IP address is not up, not attempting to change its"
459
          " netmask")
460

    
461
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
462
                         "%s/%s" % (master_ip, netmask),
463
                         "dev", master_netdev, "label",
464
                         "%s:0" % master_netdev])
465
  if result.failed:
466
    _Fail("Could not set the new netmask on the master IP address")
467

    
468
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
469
                         "%s/%s" % (master_ip, old_netmask),
470
                         "dev", master_netdev, "label",
471
                         "%s:0" % master_netdev])
472
  if result.failed:
473
    _Fail("Could not bring down the master IP address with the old netmask")
474

    
475

    
476
def EtcHostsModify(mode, host, ip):
477
  """Modify a host entry in /etc/hosts.
478

479
  @param mode: The mode to operate. Either add or remove entry
480
  @param host: The host to operate on
481
  @param ip: The ip associated with the entry
482

483
  """
484
  if mode == constants.ETC_HOSTS_ADD:
485
    if not ip:
486
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
487
              " present")
488
    utils.AddHostToEtcHosts(host, ip)
489
  elif mode == constants.ETC_HOSTS_REMOVE:
490
    if ip:
491
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
492
              " parameter is present")
493
    utils.RemoveHostFromEtcHosts(host)
494
  else:
495
    RPCFail("Mode not supported")
496

    
497

    
498
def LeaveCluster(modify_ssh_setup):
499
  """Cleans up and remove the current node.
500

501
  This function cleans up and prepares the current node to be removed
502
  from the cluster.
503

504
  If processing is successful, then it raises an
505
  L{errors.QuitGanetiException} which is used as a special case to
506
  shutdown the node daemon.
507

508
  @param modify_ssh_setup: boolean
509

510
  """
511
  _CleanDirectory(pathutils.DATA_DIR)
512
  _CleanDirectory(pathutils.CRYPTO_KEYS_DIR)
513
  JobQueuePurge()
514

    
515
  if modify_ssh_setup:
516
    try:
517
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
518

    
519
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
520

    
521
      utils.RemoveFile(priv_key)
522
      utils.RemoveFile(pub_key)
523
    except errors.OpExecError:
524
      logging.exception("Error while processing ssh files")
525

    
526
  try:
527
    utils.RemoveFile(pathutils.CONFD_HMAC_KEY)
528
    utils.RemoveFile(pathutils.RAPI_CERT_FILE)
529
    utils.RemoveFile(pathutils.SPICE_CERT_FILE)
530
    utils.RemoveFile(pathutils.SPICE_CACERT_FILE)
531
    utils.RemoveFile(pathutils.NODED_CERT_FILE)
532
  except: # pylint: disable=W0702
533
    logging.exception("Error while removing cluster secrets")
534

    
535
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop", constants.CONFD])
536
  if result.failed:
537
    logging.error("Command %s failed with exitcode %s and error %s",
538
                  result.cmd, result.exit_code, result.output)
539

    
540
  # Raise a custom exception (handled in ganeti-noded)
541
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
542

    
543

    
544
def _GetVgInfo(name):
545
  """Retrieves information about a LVM volume group.
546

547
  """
548
  # TODO: GetVGInfo supports returning information for multiple VGs at once
549
  vginfo = bdev.LogicalVolume.GetVGInfo([name])
550
  if vginfo:
551
    vg_free = int(round(vginfo[0][0], 0))
552
    vg_size = int(round(vginfo[0][1], 0))
553
  else:
554
    vg_free = None
555
    vg_size = None
556

    
557
  return {
558
    "name": name,
559
    "vg_free": vg_free,
560
    "vg_size": vg_size,
561
    }
562

    
563

    
564
def _GetHvInfo(name):
565
  """Retrieves node information from a hypervisor.
566

567
  The information returned depends on the hypervisor. Common items:
568

569
    - vg_size is the size of the configured volume group in MiB
570
    - vg_free is the free size of the volume group in MiB
571
    - memory_dom0 is the memory allocated for domain0 in MiB
572
    - memory_free is the currently available (free) ram in MiB
573
    - memory_total is the total number of ram in MiB
574
    - hv_version: the hypervisor version, if available
575

576
  """
577
  return hypervisor.GetHypervisor(name).GetNodeInfo()
578

    
579

    
580
def _GetNamedNodeInfo(names, fn):
581
  """Calls C{fn} for all names in C{names} and returns a dictionary.
582

583
  @rtype: None or dict
584

585
  """
586
  if names is None:
587
    return None
588
  else:
589
    return map(fn, names)
590

    
591

    
592
def GetNodeInfo(vg_names, hv_names):
593
  """Gives back a hash with different information about the node.
594

595
  @type vg_names: list of string
596
  @param vg_names: Names of the volume groups to ask for disk space information
597
  @type hv_names: list of string
598
  @param hv_names: Names of the hypervisors to ask for node information
599
  @rtype: tuple; (string, None/dict, None/dict)
600
  @return: Tuple containing boot ID, volume group information and hypervisor
601
    information
602

603
  """
604
  bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
605
  vg_info = _GetNamedNodeInfo(vg_names, _GetVgInfo)
606
  hv_info = _GetNamedNodeInfo(hv_names, _GetHvInfo)
607

    
608
  return (bootid, vg_info, hv_info)
609

    
610

    
611
def VerifyNode(what, cluster_name):
612
  """Verify the status of the local node.
613

614
  Based on the input L{what} parameter, various checks are done on the
615
  local node.
616

617
  If the I{filelist} key is present, this list of
618
  files is checksummed and the file/checksum pairs are returned.
619

620
  If the I{nodelist} key is present, we check that we have
621
  connectivity via ssh with the target nodes (and check the hostname
622
  report).
623

624
  If the I{node-net-test} key is present, we check that we have
625
  connectivity to the given nodes via both primary IP and, if
626
  applicable, secondary IPs.
627

628
  @type what: C{dict}
629
  @param what: a dictionary of things to check:
630
      - filelist: list of files for which to compute checksums
631
      - nodelist: list of nodes we should check ssh communication with
632
      - node-net-test: list of nodes we should check node daemon port
633
        connectivity with
634
      - hypervisor: list with hypervisors to run the verify for
635
  @rtype: dict
636
  @return: a dictionary with the same keys as the input dict, and
637
      values representing the result of the checks
638

639
  """
640
  result = {}
641
  my_name = netutils.Hostname.GetSysName()
642
  port = netutils.GetDaemonPort(constants.NODED)
643
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
644

    
645
  if constants.NV_HYPERVISOR in what and vm_capable:
646
    result[constants.NV_HYPERVISOR] = tmp = {}
647
    for hv_name in what[constants.NV_HYPERVISOR]:
648
      try:
649
        val = hypervisor.GetHypervisor(hv_name).Verify()
650
      except errors.HypervisorError, err:
651
        val = "Error while checking hypervisor: %s" % str(err)
652
      tmp[hv_name] = val
653

    
654
  if constants.NV_HVPARAMS in what and vm_capable:
655
    result[constants.NV_HVPARAMS] = tmp = []
656
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
657
      try:
658
        logging.info("Validating hv %s, %s", hv_name, hvparms)
659
        hypervisor.GetHypervisor(hv_name).ValidateParameters(hvparms)
660
      except errors.HypervisorError, err:
661
        tmp.append((source, hv_name, str(err)))
662

    
663
  if constants.NV_FILELIST in what:
664
    fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
665
                                              what[constants.NV_FILELIST]))
666
    result[constants.NV_FILELIST] = \
667
      dict((vcluster.MakeVirtualPath(key), value)
668
           for (key, value) in fingerprints.items())
669

    
670
  if constants.NV_NODELIST in what:
671
    (nodes, bynode) = what[constants.NV_NODELIST]
672

    
673
    # Add nodes from other groups (different for each node)
674
    try:
675
      nodes.extend(bynode[my_name])
676
    except KeyError:
677
      pass
678

    
679
    # Use a random order
680
    random.shuffle(nodes)
681

    
682
    # Try to contact all nodes
683
    val = {}
684
    for node in nodes:
685
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
686
      if not success:
687
        val[node] = message
688

    
689
    result[constants.NV_NODELIST] = val
690

    
691
  if constants.NV_NODENETTEST in what:
692
    result[constants.NV_NODENETTEST] = tmp = {}
693
    my_pip = my_sip = None
694
    for name, pip, sip in what[constants.NV_NODENETTEST]:
695
      if name == my_name:
696
        my_pip = pip
697
        my_sip = sip
698
        break
699
    if not my_pip:
700
      tmp[my_name] = ("Can't find my own primary/secondary IP"
701
                      " in the node list")
702
    else:
703
      for name, pip, sip in what[constants.NV_NODENETTEST]:
704
        fail = []
705
        if not netutils.TcpPing(pip, port, source=my_pip):
706
          fail.append("primary")
707
        if sip != pip:
708
          if not netutils.TcpPing(sip, port, source=my_sip):
709
            fail.append("secondary")
710
        if fail:
711
          tmp[name] = ("failure using the %s interface(s)" %
712
                       " and ".join(fail))
713

    
714
  if constants.NV_MASTERIP in what:
715
    # FIXME: add checks on incoming data structures (here and in the
716
    # rest of the function)
717
    master_name, master_ip = what[constants.NV_MASTERIP]
718
    if master_name == my_name:
719
      source = constants.IP4_ADDRESS_LOCALHOST
720
    else:
721
      source = None
722
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
723
                                                     source=source)
724

    
725
  if constants.NV_USERSCRIPTS in what:
726
    result[constants.NV_USERSCRIPTS] = \
727
      [script for script in what[constants.NV_USERSCRIPTS]
728
       if not utils.IsExecutable(script)]
729

    
730
  if constants.NV_OOB_PATHS in what:
731
    result[constants.NV_OOB_PATHS] = tmp = []
732
    for path in what[constants.NV_OOB_PATHS]:
733
      try:
734
        st = os.stat(path)
735
      except OSError, err:
736
        tmp.append("error stating out of band helper: %s" % err)
737
      else:
738
        if stat.S_ISREG(st.st_mode):
739
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
740
            tmp.append(None)
741
          else:
742
            tmp.append("out of band helper %s is not executable" % path)
743
        else:
744
          tmp.append("out of band helper %s is not a file" % path)
745

    
746
  if constants.NV_LVLIST in what and vm_capable:
747
    try:
748
      val = GetVolumeList(utils.ListVolumeGroups().keys())
749
    except RPCFail, err:
750
      val = str(err)
751
    result[constants.NV_LVLIST] = val
752

    
753
  if constants.NV_INSTANCELIST in what and vm_capable:
754
    # GetInstanceList can fail
755
    try:
756
      val = GetInstanceList(what[constants.NV_INSTANCELIST])
757
    except RPCFail, err:
758
      val = str(err)
759
    result[constants.NV_INSTANCELIST] = val
760

    
761
  if constants.NV_VGLIST in what and vm_capable:
762
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
763

    
764
  if constants.NV_PVLIST in what and vm_capable:
765
    val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
766
                                       filter_allocatable=False)
767
    result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
768

    
769
  if constants.NV_VERSION in what:
770
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
771
                                    constants.RELEASE_VERSION)
772

    
773
  if constants.NV_HVINFO in what and vm_capable:
774
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
775
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
776

    
777
  if constants.NV_DRBDLIST in what and vm_capable:
778
    try:
779
      used_minors = bdev.DRBD8.GetUsedDevs().keys()
780
    except errors.BlockDeviceError, err:
781
      logging.warning("Can't get used minors list", exc_info=True)
782
      used_minors = str(err)
783
    result[constants.NV_DRBDLIST] = used_minors
784

    
785
  if constants.NV_DRBDHELPER in what and vm_capable:
786
    status = True
787
    try:
788
      payload = bdev.BaseDRBD.GetUsermodeHelper()
789
    except errors.BlockDeviceError, err:
790
      logging.error("Can't get DRBD usermode helper: %s", str(err))
791
      status = False
792
      payload = str(err)
793
    result[constants.NV_DRBDHELPER] = (status, payload)
794

    
795
  if constants.NV_NODESETUP in what:
796
    result[constants.NV_NODESETUP] = tmpr = []
797
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
798
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
799
                  " under /sys, missing required directories /sys/block"
800
                  " and /sys/class/net")
801
    if (not os.path.isdir("/proc/sys") or
802
        not os.path.isfile("/proc/sysrq-trigger")):
803
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
804
                  " under /proc, missing required directory /proc/sys and"
805
                  " the file /proc/sysrq-trigger")
806

    
807
  if constants.NV_TIME in what:
808
    result[constants.NV_TIME] = utils.SplitTime(time.time())
809

    
810
  if constants.NV_OSLIST in what and vm_capable:
811
    result[constants.NV_OSLIST] = DiagnoseOS()
812

    
813
  if constants.NV_BRIDGES in what and vm_capable:
814
    result[constants.NV_BRIDGES] = [bridge
815
                                    for bridge in what[constants.NV_BRIDGES]
816
                                    if not utils.BridgeExists(bridge)]
817

    
818
  if what.get(constants.NV_FILE_STORAGE_PATHS) == my_name:
819
    result[constants.NV_FILE_STORAGE_PATHS] = \
820
      bdev.ComputeWrongFileStoragePaths()
821

    
822
  return result
823

    
824

    
825
def GetBlockDevSizes(devices):
826
  """Return the size of the given block devices
827

828
  @type devices: list
829
  @param devices: list of block device nodes to query
830
  @rtype: dict
831
  @return:
832
    dictionary of all block devices under /dev (key). The value is their
833
    size in MiB.
834

835
    {'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
836

837
  """
838
  DEV_PREFIX = "/dev/"
839
  blockdevs = {}
840

    
841
  for devpath in devices:
842
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
843
      continue
844

    
845
    try:
846
      st = os.stat(devpath)
847
    except EnvironmentError, err:
848
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
849
      continue
850

    
851
    if stat.S_ISBLK(st.st_mode):
852
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
853
      if result.failed:
854
        # We don't want to fail, just do not list this device as available
855
        logging.warning("Cannot get size for block device %s", devpath)
856
        continue
857

    
858
      size = int(result.stdout) / (1024 * 1024)
859
      blockdevs[devpath] = size
860
  return blockdevs
861

    
862

    
863
def GetVolumeList(vg_names):
864
  """Compute list of logical volumes and their size.
865

866
  @type vg_names: list
867
  @param vg_names: the volume groups whose LVs we should list, or
868
      empty for all volume groups
869
  @rtype: dict
870
  @return:
871
      dictionary of all partions (key) with value being a tuple of
872
      their size (in MiB), inactive and online status::
873

874
        {'xenvg/test1': ('20.06', True, True)}
875

876
      in case of errors, a string is returned with the error
877
      details.
878

879
  """
880
  lvs = {}
881
  sep = "|"
882
  if not vg_names:
883
    vg_names = []
884
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
885
                         "--separator=%s" % sep,
886
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
887
  if result.failed:
888
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
889

    
890
  for line in result.stdout.splitlines():
891
    line = line.strip()
892
    match = _LVSLINE_REGEX.match(line)
893
    if not match:
894
      logging.error("Invalid line returned from lvs output: '%s'", line)
895
      continue
896
    vg_name, name, size, attr = match.groups()
897
    inactive = attr[4] == "-"
898
    online = attr[5] == "o"
899
    virtual = attr[0] == "v"
900
    if virtual:
901
      # we don't want to report such volumes as existing, since they
902
      # don't really hold data
903
      continue
904
    lvs[vg_name + "/" + name] = (size, inactive, online)
905

    
906
  return lvs
907

    
908

    
909
def ListVolumeGroups():
910
  """List the volume groups and their size.
911

912
  @rtype: dict
913
  @return: dictionary with keys volume name and values the
914
      size of the volume
915

916
  """
917
  return utils.ListVolumeGroups()
918

    
919

    
920
def NodeVolumes():
921
  """List all volumes on this node.
922

923
  @rtype: list
924
  @return:
925
    A list of dictionaries, each having four keys:
926
      - name: the logical volume name,
927
      - size: the size of the logical volume
928
      - dev: the physical device on which the LV lives
929
      - vg: the volume group to which it belongs
930

931
    In case of errors, we return an empty list and log the
932
    error.
933

934
    Note that since a logical volume can live on multiple physical
935
    volumes, the resulting list might include a logical volume
936
    multiple times.
937

938
  """
939
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
940
                         "--separator=|",
941
                         "--options=lv_name,lv_size,devices,vg_name"])
942
  if result.failed:
943
    _Fail("Failed to list logical volumes, lvs output: %s",
944
          result.output)
945

    
946
  def parse_dev(dev):
947
    return dev.split("(")[0]
948

    
949
  def handle_dev(dev):
950
    return [parse_dev(x) for x in dev.split(",")]
951

    
952
  def map_line(line):
953
    line = [v.strip() for v in line]
954
    return [{"name": line[0], "size": line[1],
955
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
956

    
957
  all_devs = []
958
  for line in result.stdout.splitlines():
959
    if line.count("|") >= 3:
960
      all_devs.extend(map_line(line.split("|")))
961
    else:
962
      logging.warning("Strange line in the output from lvs: '%s'", line)
963
  return all_devs
964

    
965

    
966
def BridgesExist(bridges_list):
967
  """Check if a list of bridges exist on the current node.
968

969
  @rtype: boolean
970
  @return: C{True} if all of them exist, C{False} otherwise
971

972
  """
973
  missing = []
974
  for bridge in bridges_list:
975
    if not utils.BridgeExists(bridge):
976
      missing.append(bridge)
977

    
978
  if missing:
979
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
980

    
981

    
982
def GetInstanceList(hypervisor_list):
983
  """Provides a list of instances.
984

985
  @type hypervisor_list: list
986
  @param hypervisor_list: the list of hypervisors to query information
987

988
  @rtype: list
989
  @return: a list of all running instances on the current node
990
    - instance1.example.com
991
    - instance2.example.com
992

993
  """
994
  results = []
995
  for hname in hypervisor_list:
996
    try:
997
      names = hypervisor.GetHypervisor(hname).ListInstances()
998
      results.extend(names)
999
    except errors.HypervisorError, err:
1000
      _Fail("Error enumerating instances (hypervisor %s): %s",
1001
            hname, err, exc=True)
1002

    
1003
  return results
1004

    
1005

    
1006
def GetInstanceInfo(instance, hname):
1007
  """Gives back the information about an instance as a dictionary.
1008

1009
  @type instance: string
1010
  @param instance: the instance name
1011
  @type hname: string
1012
  @param hname: the hypervisor type of the instance
1013

1014
  @rtype: dict
1015
  @return: dictionary with the following keys:
1016
      - memory: memory size of instance (int)
1017
      - state: xen state of instance (string)
1018
      - time: cpu time of instance (float)
1019
      - vcpus: the number of vcpus (int)
1020

1021
  """
1022
  output = {}
1023

    
1024
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
1025
  if iinfo is not None:
1026
    output["memory"] = iinfo[2]
1027
    output["vcpus"] = iinfo[3]
1028
    output["state"] = iinfo[4]
1029
    output["time"] = iinfo[5]
1030

    
1031
  return output
1032

    
1033

    
1034
def GetInstanceMigratable(instance):
1035
  """Gives whether an instance can be migrated.
1036

1037
  @type instance: L{objects.Instance}
1038
  @param instance: object representing the instance to be checked.
1039

1040
  @rtype: tuple
1041
  @return: tuple of (result, description) where:
1042
      - result: whether the instance can be migrated or not
1043
      - description: a description of the issue, if relevant
1044

1045
  """
1046
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1047
  iname = instance.name
1048
  if iname not in hyper.ListInstances():
1049
    _Fail("Instance %s is not running", iname)
1050

    
1051
  for idx in range(len(instance.disks)):
1052
    link_name = _GetBlockDevSymlinkPath(iname, idx)
1053
    if not os.path.islink(link_name):
1054
      logging.warning("Instance %s is missing symlink %s for disk %d",
1055
                      iname, link_name, idx)
1056

    
1057

    
1058
def GetAllInstancesInfo(hypervisor_list):
1059
  """Gather data about all instances.
1060

1061
  This is the equivalent of L{GetInstanceInfo}, except that it
1062
  computes data for all instances at once, thus being faster if one
1063
  needs data about more than one instance.
1064

1065
  @type hypervisor_list: list
1066
  @param hypervisor_list: list of hypervisors to query for instance data
1067

1068
  @rtype: dict
1069
  @return: dictionary of instance: data, with data having the following keys:
1070
      - memory: memory size of instance (int)
1071
      - state: xen state of instance (string)
1072
      - time: cpu time of instance (float)
1073
      - vcpus: the number of vcpus
1074

1075
  """
1076
  output = {}
1077

    
1078
  for hname in hypervisor_list:
1079
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
1080
    if iinfo:
1081
      for name, _, memory, vcpus, state, times in iinfo:
1082
        value = {
1083
          "memory": memory,
1084
          "vcpus": vcpus,
1085
          "state": state,
1086
          "time": times,
1087
          }
1088
        if name in output:
1089
          # we only check static parameters, like memory and vcpus,
1090
          # and not state and time which can change between the
1091
          # invocations of the different hypervisors
1092
          for key in "memory", "vcpus":
1093
            if value[key] != output[name][key]:
1094
              _Fail("Instance %s is running twice"
1095
                    " with different parameters", name)
1096
        output[name] = value
1097

    
1098
  return output
1099

    
1100

    
1101
def _InstanceLogName(kind, os_name, instance, component):
1102
  """Compute the OS log filename for a given instance and operation.
1103

1104
  The instance name and os name are passed in as strings since not all
1105
  operations have these as part of an instance object.
1106

1107
  @type kind: string
1108
  @param kind: the operation type (e.g. add, import, etc.)
1109
  @type os_name: string
1110
  @param os_name: the os name
1111
  @type instance: string
1112
  @param instance: the name of the instance being imported/added/etc.
1113
  @type component: string or None
1114
  @param component: the name of the component of the instance being
1115
      transferred
1116

1117
  """
1118
  # TODO: Use tempfile.mkstemp to create unique filename
1119
  if component:
1120
    assert "/" not in component
1121
    c_msg = "-%s" % component
1122
  else:
1123
    c_msg = ""
1124
  base = ("%s-%s-%s%s-%s.log" %
1125
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
1126
  return utils.PathJoin(pathutils.LOG_OS_DIR, base)
1127

    
1128

    
1129
def InstanceOsAdd(instance, reinstall, debug):
1130
  """Add an OS to an instance.
1131

1132
  @type instance: L{objects.Instance}
1133
  @param instance: Instance whose OS is to be installed
1134
  @type reinstall: boolean
1135
  @param reinstall: whether this is an instance reinstall
1136
  @type debug: integer
1137
  @param debug: debug level, passed to the OS scripts
1138
  @rtype: None
1139

1140
  """
1141
  inst_os = OSFromDisk(instance.os)
1142

    
1143
  create_env = OSEnvironment(instance, inst_os, debug)
1144
  if reinstall:
1145
    create_env["INSTANCE_REINSTALL"] = "1"
1146

    
1147
  logfile = _InstanceLogName("add", instance.os, instance.name, None)
1148

    
1149
  result = utils.RunCmd([inst_os.create_script], env=create_env,
1150
                        cwd=inst_os.path, output=logfile, reset_env=True)
1151
  if result.failed:
1152
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
1153
                  " output: %s", result.cmd, result.fail_reason, logfile,
1154
                  result.output)
1155
    lines = [utils.SafeEncode(val)
1156
             for val in utils.TailFile(logfile, lines=20)]
1157
    _Fail("OS create script failed (%s), last lines in the"
1158
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1159

    
1160

    
1161
def RunRenameInstance(instance, old_name, debug):
1162
  """Run the OS rename script for an instance.
1163

1164
  @type instance: L{objects.Instance}
1165
  @param instance: Instance whose OS is to be installed
1166
  @type old_name: string
1167
  @param old_name: previous instance name
1168
  @type debug: integer
1169
  @param debug: debug level, passed to the OS scripts
1170
  @rtype: boolean
1171
  @return: the success of the operation
1172

1173
  """
1174
  inst_os = OSFromDisk(instance.os)
1175

    
1176
  rename_env = OSEnvironment(instance, inst_os, debug)
1177
  rename_env["OLD_INSTANCE_NAME"] = old_name
1178

    
1179
  logfile = _InstanceLogName("rename", instance.os,
1180
                             "%s-%s" % (old_name, instance.name), None)
1181

    
1182
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1183
                        cwd=inst_os.path, output=logfile, reset_env=True)
1184

    
1185
  if result.failed:
1186
    logging.error("os create command '%s' returned error: %s output: %s",
1187
                  result.cmd, result.fail_reason, result.output)
1188
    lines = [utils.SafeEncode(val)
1189
             for val in utils.TailFile(logfile, lines=20)]
1190
    _Fail("OS rename script failed (%s), last lines in the"
1191
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1192

    
1193

    
1194
def _GetBlockDevSymlinkPath(instance_name, idx):
1195
  return utils.PathJoin(pathutils.DISK_LINKS_DIR, "%s%s%d" %
1196
                        (instance_name, constants.DISK_SEPARATOR, idx))
1197

    
1198

    
1199
def _SymlinkBlockDev(instance_name, device_path, idx):
1200
  """Set up symlinks to a instance's block device.
1201

1202
  This is an auxiliary function run when an instance is start (on the primary
1203
  node) or when an instance is migrated (on the target node).
1204

1205

1206
  @param instance_name: the name of the target instance
1207
  @param device_path: path of the physical block device, on the node
1208
  @param idx: the disk index
1209
  @return: absolute path to the disk's symlink
1210

1211
  """
1212
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1213
  try:
1214
    os.symlink(device_path, link_name)
1215
  except OSError, err:
1216
    if err.errno == errno.EEXIST:
1217
      if (not os.path.islink(link_name) or
1218
          os.readlink(link_name) != device_path):
1219
        os.remove(link_name)
1220
        os.symlink(device_path, link_name)
1221
    else:
1222
      raise
1223

    
1224
  return link_name
1225

    
1226

    
1227
def _RemoveBlockDevLinks(instance_name, disks):
1228
  """Remove the block device symlinks belonging to the given instance.
1229

1230
  """
1231
  for idx, _ in enumerate(disks):
1232
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1233
    if os.path.islink(link_name):
1234
      try:
1235
        os.remove(link_name)
1236
      except OSError:
1237
        logging.exception("Can't remove symlink '%s'", link_name)
1238

    
1239

    
1240
def _GatherAndLinkBlockDevs(instance):
1241
  """Set up an instance's block device(s).
1242

1243
  This is run on the primary node at instance startup. The block
1244
  devices must be already assembled.
1245

1246
  @type instance: L{objects.Instance}
1247
  @param instance: the instance whose disks we shoul assemble
1248
  @rtype: list
1249
  @return: list of (disk_object, device_path)
1250

1251
  """
1252
  block_devices = []
1253
  for idx, disk in enumerate(instance.disks):
1254
    device = _RecursiveFindBD(disk)
1255
    if device is None:
1256
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1257
                                    str(disk))
1258
    device.Open()
1259
    try:
1260
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1261
    except OSError, e:
1262
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1263
                                    e.strerror)
1264

    
1265
    block_devices.append((disk, link_name))
1266

    
1267
  return block_devices
1268

    
1269

    
1270
def StartInstance(instance, startup_paused):
1271
  """Start an instance.
1272

1273
  @type instance: L{objects.Instance}
1274
  @param instance: the instance object
1275
  @type startup_paused: bool
1276
  @param instance: pause instance at startup?
1277
  @rtype: None
1278

1279
  """
1280
  running_instances = GetInstanceList([instance.hypervisor])
1281

    
1282
  if instance.name in running_instances:
1283
    logging.info("Instance %s already running, not starting", instance.name)
1284
    return
1285

    
1286
  try:
1287
    block_devices = _GatherAndLinkBlockDevs(instance)
1288
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1289
    hyper.StartInstance(instance, block_devices, startup_paused)
1290
  except errors.BlockDeviceError, err:
1291
    _Fail("Block device error: %s", err, exc=True)
1292
  except errors.HypervisorError, err:
1293
    _RemoveBlockDevLinks(instance.name, instance.disks)
1294
    _Fail("Hypervisor error: %s", err, exc=True)
1295

    
1296

    
1297
def InstanceShutdown(instance, timeout):
1298
  """Shut an instance down.
1299

1300
  @note: this functions uses polling with a hardcoded timeout.
1301

1302
  @type instance: L{objects.Instance}
1303
  @param instance: the instance object
1304
  @type timeout: integer
1305
  @param timeout: maximum timeout for soft shutdown
1306
  @rtype: None
1307

1308
  """
1309
  hv_name = instance.hypervisor
1310
  hyper = hypervisor.GetHypervisor(hv_name)
1311
  iname = instance.name
1312

    
1313
  if instance.name not in hyper.ListInstances():
1314
    logging.info("Instance %s not running, doing nothing", iname)
1315
    return
1316

    
1317
  class _TryShutdown:
1318
    def __init__(self):
1319
      self.tried_once = False
1320

    
1321
    def __call__(self):
1322
      if iname not in hyper.ListInstances():
1323
        return
1324

    
1325
      try:
1326
        hyper.StopInstance(instance, retry=self.tried_once)
1327
      except errors.HypervisorError, err:
1328
        if iname not in hyper.ListInstances():
1329
          # if the instance is no longer existing, consider this a
1330
          # success and go to cleanup
1331
          return
1332

    
1333
        _Fail("Failed to stop instance %s: %s", iname, err)
1334

    
1335
      self.tried_once = True
1336

    
1337
      raise utils.RetryAgain()
1338

    
1339
  try:
1340
    utils.Retry(_TryShutdown(), 5, timeout)
1341
  except utils.RetryTimeout:
1342
    # the shutdown did not succeed
1343
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1344

    
1345
    try:
1346
      hyper.StopInstance(instance, force=True)
1347
    except errors.HypervisorError, err:
1348
      if iname in hyper.ListInstances():
1349
        # only raise an error if the instance still exists, otherwise
1350
        # the error could simply be "instance ... unknown"!
1351
        _Fail("Failed to force stop instance %s: %s", iname, err)
1352

    
1353
    time.sleep(1)
1354

    
1355
    if iname in hyper.ListInstances():
1356
      _Fail("Could not shutdown instance %s even by destroy", iname)
1357

    
1358
  try:
1359
    hyper.CleanupInstance(instance.name)
1360
  except errors.HypervisorError, err:
1361
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1362

    
1363
  _RemoveBlockDevLinks(iname, instance.disks)
1364

    
1365

    
1366
def InstanceReboot(instance, reboot_type, shutdown_timeout):
1367
  """Reboot an instance.
1368

1369
  @type instance: L{objects.Instance}
1370
  @param instance: the instance object to reboot
1371
  @type reboot_type: str
1372
  @param reboot_type: the type of reboot, one the following
1373
    constants:
1374
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1375
        instance OS, do not recreate the VM
1376
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1377
        restart the VM (at the hypervisor level)
1378
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1379
        not accepted here, since that mode is handled differently, in
1380
        cmdlib, and translates into full stop and start of the
1381
        instance (instead of a call_instance_reboot RPC)
1382
  @type shutdown_timeout: integer
1383
  @param shutdown_timeout: maximum timeout for soft shutdown
1384
  @rtype: None
1385

1386
  """
1387
  running_instances = GetInstanceList([instance.hypervisor])
1388

    
1389
  if instance.name not in running_instances:
1390
    _Fail("Cannot reboot instance %s that is not running", instance.name)
1391

    
1392
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1393
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1394
    try:
1395
      hyper.RebootInstance(instance)
1396
    except errors.HypervisorError, err:
1397
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1398
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1399
    try:
1400
      InstanceShutdown(instance, shutdown_timeout)
1401
      return StartInstance(instance, False)
1402
    except errors.HypervisorError, err:
1403
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1404
  else:
1405
    _Fail("Invalid reboot_type received: %s", reboot_type)
1406

    
1407

    
1408
def InstanceBalloonMemory(instance, memory):
1409
  """Resize an instance's memory.
1410

1411
  @type instance: L{objects.Instance}
1412
  @param instance: the instance object
1413
  @type memory: int
1414
  @param memory: new memory amount in MB
1415
  @rtype: None
1416

1417
  """
1418
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1419
  running = hyper.ListInstances()
1420
  if instance.name not in running:
1421
    logging.info("Instance %s is not running, cannot balloon", instance.name)
1422
    return
1423
  try:
1424
    hyper.BalloonInstanceMemory(instance, memory)
1425
  except errors.HypervisorError, err:
1426
    _Fail("Failed to balloon instance memory: %s", err, exc=True)
1427

    
1428

    
1429
def MigrationInfo(instance):
1430
  """Gather information about an instance to be migrated.
1431

1432
  @type instance: L{objects.Instance}
1433
  @param instance: the instance definition
1434

1435
  """
1436
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1437
  try:
1438
    info = hyper.MigrationInfo(instance)
1439
  except errors.HypervisorError, err:
1440
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1441
  return info
1442

    
1443

    
1444
def AcceptInstance(instance, info, target):
1445
  """Prepare the node to accept an instance.
1446

1447
  @type instance: L{objects.Instance}
1448
  @param instance: the instance definition
1449
  @type info: string/data (opaque)
1450
  @param info: migration information, from the source node
1451
  @type target: string
1452
  @param target: target host (usually ip), on this node
1453

1454
  """
1455
  # TODO: why is this required only for DTS_EXT_MIRROR?
1456
  if instance.disk_template in constants.DTS_EXT_MIRROR:
1457
    # Create the symlinks, as the disks are not active
1458
    # in any way
1459
    try:
1460
      _GatherAndLinkBlockDevs(instance)
1461
    except errors.BlockDeviceError, err:
1462
      _Fail("Block device error: %s", err, exc=True)
1463

    
1464
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1465
  try:
1466
    hyper.AcceptInstance(instance, info, target)
1467
  except errors.HypervisorError, err:
1468
    if instance.disk_template in constants.DTS_EXT_MIRROR:
1469
      _RemoveBlockDevLinks(instance.name, instance.disks)
1470
    _Fail("Failed to accept instance: %s", err, exc=True)
1471

    
1472

    
1473
def FinalizeMigrationDst(instance, info, success):
1474
  """Finalize any preparation to accept an instance.
1475

1476
  @type instance: L{objects.Instance}
1477
  @param instance: the instance definition
1478
  @type info: string/data (opaque)
1479
  @param info: migration information, from the source node
1480
  @type success: boolean
1481
  @param success: whether the migration was a success or a failure
1482

1483
  """
1484
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1485
  try:
1486
    hyper.FinalizeMigrationDst(instance, info, success)
1487
  except errors.HypervisorError, err:
1488
    _Fail("Failed to finalize migration on the target node: %s", err, exc=True)
1489

    
1490

    
1491
def MigrateInstance(instance, target, live):
1492
  """Migrates an instance to another node.
1493

1494
  @type instance: L{objects.Instance}
1495
  @param instance: the instance definition
1496
  @type target: string
1497
  @param target: the target node name
1498
  @type live: boolean
1499
  @param live: whether the migration should be done live or not (the
1500
      interpretation of this parameter is left to the hypervisor)
1501
  @raise RPCFail: if migration fails for some reason
1502

1503
  """
1504
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1505

    
1506
  try:
1507
    hyper.MigrateInstance(instance, target, live)
1508
  except errors.HypervisorError, err:
1509
    _Fail("Failed to migrate instance: %s", err, exc=True)
1510

    
1511

    
1512
def FinalizeMigrationSource(instance, success, live):
1513
  """Finalize the instance migration on the source node.
1514

1515
  @type instance: L{objects.Instance}
1516
  @param instance: the instance definition of the migrated instance
1517
  @type success: bool
1518
  @param success: whether the migration succeeded or not
1519
  @type live: bool
1520
  @param live: whether the user requested a live migration or not
1521
  @raise RPCFail: If the execution fails for some reason
1522

1523
  """
1524
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1525

    
1526
  try:
1527
    hyper.FinalizeMigrationSource(instance, success, live)
1528
  except Exception, err:  # pylint: disable=W0703
1529
    _Fail("Failed to finalize the migration on the source node: %s", err,
1530
          exc=True)
1531

    
1532

    
1533
def GetMigrationStatus(instance):
1534
  """Get the migration status
1535

1536
  @type instance: L{objects.Instance}
1537
  @param instance: the instance that is being migrated
1538
  @rtype: L{objects.MigrationStatus}
1539
  @return: the status of the current migration (one of
1540
           L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
1541
           progress info that can be retrieved from the hypervisor
1542
  @raise RPCFail: If the migration status cannot be retrieved
1543

1544
  """
1545
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1546
  try:
1547
    return hyper.GetMigrationStatus(instance)
1548
  except Exception, err:  # pylint: disable=W0703
1549
    _Fail("Failed to get migration status: %s", err, exc=True)
1550

    
1551

    
1552
def BlockdevCreate(disk, size, owner, on_primary, info):
1553
  """Creates a block device for an instance.
1554

1555
  @type disk: L{objects.Disk}
1556
  @param disk: the object describing the disk we should create
1557
  @type size: int
1558
  @param size: the size of the physical underlying device, in MiB
1559
  @type owner: str
1560
  @param owner: the name of the instance for which disk is created,
1561
      used for device cache data
1562
  @type on_primary: boolean
1563
  @param on_primary:  indicates if it is the primary node or not
1564
  @type info: string
1565
  @param info: string that will be sent to the physical device
1566
      creation, used for example to set (LVM) tags on LVs
1567

1568
  @return: the new unique_id of the device (this can sometime be
1569
      computed only after creation), or None. On secondary nodes,
1570
      it's not required to return anything.
1571

1572
  """
1573
  # TODO: remove the obsolete "size" argument
1574
  # pylint: disable=W0613
1575
  clist = []
1576
  if disk.children:
1577
    for child in disk.children:
1578
      try:
1579
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1580
      except errors.BlockDeviceError, err:
1581
        _Fail("Can't assemble device %s: %s", child, err)
1582
      if on_primary or disk.AssembleOnSecondary():
1583
        # we need the children open in case the device itself has to
1584
        # be assembled
1585
        try:
1586
          # pylint: disable=E1103
1587
          crdev.Open()
1588
        except errors.BlockDeviceError, err:
1589
          _Fail("Can't make child '%s' read-write: %s", child, err)
1590
      clist.append(crdev)
1591

    
1592
  try:
1593
    device = bdev.Create(disk, clist)
1594
  except errors.BlockDeviceError, err:
1595
    _Fail("Can't create block device: %s", err)
1596

    
1597
  if on_primary or disk.AssembleOnSecondary():
1598
    try:
1599
      device.Assemble()
1600
    except errors.BlockDeviceError, err:
1601
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1602
    if on_primary or disk.OpenOnSecondary():
1603
      try:
1604
        device.Open(force=True)
1605
      except errors.BlockDeviceError, err:
1606
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1607
    DevCacheManager.UpdateCache(device.dev_path, owner,
1608
                                on_primary, disk.iv_name)
1609

    
1610
  device.SetInfo(info)
1611

    
1612
  return device.unique_id
1613

    
1614

    
1615
def _WipeDevice(path, offset, size):
1616
  """This function actually wipes the device.
1617

1618
  @param path: The path to the device to wipe
1619
  @param offset: The offset in MiB in the file
1620
  @param size: The size in MiB to write
1621

1622
  """
1623
  # Internal sizes are always in Mebibytes; if the following "dd" command
1624
  # should use a different block size the offset and size given to this
1625
  # function must be adjusted accordingly before being passed to "dd".
1626
  block_size = 1024 * 1024
1627

    
1628
  cmd = [constants.DD_CMD, "if=/dev/zero", "seek=%d" % offset,
1629
         "bs=%s" % block_size, "oflag=direct", "of=%s" % path,
1630
         "count=%d" % size]
1631
  result = utils.RunCmd(cmd)
1632

    
1633
  if result.failed:
1634
    _Fail("Wipe command '%s' exited with error: %s; output: %s", result.cmd,
1635
          result.fail_reason, result.output)
1636

    
1637

    
1638
def BlockdevWipe(disk, offset, size):
1639
  """Wipes a block device.
1640

1641
  @type disk: L{objects.Disk}
1642
  @param disk: the disk object we want to wipe
1643
  @type offset: int
1644
  @param offset: The offset in MiB in the file
1645
  @type size: int
1646
  @param size: The size in MiB to write
1647

1648
  """
1649
  try:
1650
    rdev = _RecursiveFindBD(disk)
1651
  except errors.BlockDeviceError:
1652
    rdev = None
1653

    
1654
  if not rdev:
1655
    _Fail("Cannot execute wipe for device %s: device not found", disk.iv_name)
1656

    
1657
  # Do cross verify some of the parameters
1658
  if offset < 0:
1659
    _Fail("Negative offset")
1660
  if size < 0:
1661
    _Fail("Negative size")
1662
  if offset > rdev.size:
1663
    _Fail("Offset is bigger than device size")
1664
  if (offset + size) > rdev.size:
1665
    _Fail("The provided offset and size to wipe is bigger than device size")
1666

    
1667
  _WipeDevice(rdev.dev_path, offset, size)
1668

    
1669

    
1670
def BlockdevPauseResumeSync(disks, pause):
1671
  """Pause or resume the sync of the block device.
1672

1673
  @type disks: list of L{objects.Disk}
1674
  @param disks: the disks object we want to pause/resume
1675
  @type pause: bool
1676
  @param pause: Wheater to pause or resume
1677

1678
  """
1679
  success = []
1680
  for disk in disks:
1681
    try:
1682
      rdev = _RecursiveFindBD(disk)
1683
    except errors.BlockDeviceError:
1684
      rdev = None
1685

    
1686
    if not rdev:
1687
      success.append((False, ("Cannot change sync for device %s:"
1688
                              " device not found" % disk.iv_name)))
1689
      continue
1690

    
1691
    result = rdev.PauseResumeSync(pause)
1692

    
1693
    if result:
1694
      success.append((result, None))
1695
    else:
1696
      if pause:
1697
        msg = "Pause"
1698
      else:
1699
        msg = "Resume"
1700
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
1701

    
1702
  return success
1703

    
1704

    
1705
def BlockdevRemove(disk):
1706
  """Remove a block device.
1707

1708
  @note: This is intended to be called recursively.
1709

1710
  @type disk: L{objects.Disk}
1711
  @param disk: the disk object we should remove
1712
  @rtype: boolean
1713
  @return: the success of the operation
1714

1715
  """
1716
  msgs = []
1717
  try:
1718
    rdev = _RecursiveFindBD(disk)
1719
  except errors.BlockDeviceError, err:
1720
    # probably can't attach
1721
    logging.info("Can't attach to device %s in remove", disk)
1722
    rdev = None
1723
  if rdev is not None:
1724
    r_path = rdev.dev_path
1725
    try:
1726
      rdev.Remove()
1727
    except errors.BlockDeviceError, err:
1728
      msgs.append(str(err))
1729
    if not msgs:
1730
      DevCacheManager.RemoveCache(r_path)
1731

    
1732
  if disk.children:
1733
    for child in disk.children:
1734
      try:
1735
        BlockdevRemove(child)
1736
      except RPCFail, err:
1737
        msgs.append(str(err))
1738

    
1739
  if msgs:
1740
    _Fail("; ".join(msgs))
1741

    
1742

    
1743
def _RecursiveAssembleBD(disk, owner, as_primary):
1744
  """Activate a block device for an instance.
1745

1746
  This is run on the primary and secondary nodes for an instance.
1747

1748
  @note: this function is called recursively.
1749

1750
  @type disk: L{objects.Disk}
1751
  @param disk: the disk we try to assemble
1752
  @type owner: str
1753
  @param owner: the name of the instance which owns the disk
1754
  @type as_primary: boolean
1755
  @param as_primary: if we should make the block device
1756
      read/write
1757

1758
  @return: the assembled device or None (in case no device
1759
      was assembled)
1760
  @raise errors.BlockDeviceError: in case there is an error
1761
      during the activation of the children or the device
1762
      itself
1763

1764
  """
1765
  children = []
1766
  if disk.children:
1767
    mcn = disk.ChildrenNeeded()
1768
    if mcn == -1:
1769
      mcn = 0 # max number of Nones allowed
1770
    else:
1771
      mcn = len(disk.children) - mcn # max number of Nones
1772
    for chld_disk in disk.children:
1773
      try:
1774
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1775
      except errors.BlockDeviceError, err:
1776
        if children.count(None) >= mcn:
1777
          raise
1778
        cdev = None
1779
        logging.error("Error in child activation (but continuing): %s",
1780
                      str(err))
1781
      children.append(cdev)
1782

    
1783
  if as_primary or disk.AssembleOnSecondary():
1784
    r_dev = bdev.Assemble(disk, children)
1785
    result = r_dev
1786
    if as_primary or disk.OpenOnSecondary():
1787
      r_dev.Open()
1788
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1789
                                as_primary, disk.iv_name)
1790

    
1791
  else:
1792
    result = True
1793
  return result
1794

    
1795

    
1796
def BlockdevAssemble(disk, owner, as_primary, idx):
1797
  """Activate a block device for an instance.
1798

1799
  This is a wrapper over _RecursiveAssembleBD.
1800

1801
  @rtype: str or boolean
1802
  @return: a C{/dev/...} path for primary nodes, and
1803
      C{True} for secondary nodes
1804

1805
  """
1806
  try:
1807
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1808
    if isinstance(result, bdev.BlockDev):
1809
      # pylint: disable=E1103
1810
      result = result.dev_path
1811
      if as_primary:
1812
        _SymlinkBlockDev(owner, result, idx)
1813
  except errors.BlockDeviceError, err:
1814
    _Fail("Error while assembling disk: %s", err, exc=True)
1815
  except OSError, err:
1816
    _Fail("Error while symlinking disk: %s", err, exc=True)
1817

    
1818
  return result
1819

    
1820

    
1821
def BlockdevShutdown(disk):
1822
  """Shut down a block device.
1823

1824
  First, if the device is assembled (Attach() is successful), then
1825
  the device is shutdown. Then the children of the device are
1826
  shutdown.
1827

1828
  This function is called recursively. Note that we don't cache the
1829
  children or such, as oppossed to assemble, shutdown of different
1830
  devices doesn't require that the upper device was active.
1831

1832
  @type disk: L{objects.Disk}
1833
  @param disk: the description of the disk we should
1834
      shutdown
1835
  @rtype: None
1836

1837
  """
1838
  msgs = []
1839
  r_dev = _RecursiveFindBD(disk)
1840
  if r_dev is not None:
1841
    r_path = r_dev.dev_path
1842
    try:
1843
      r_dev.Shutdown()
1844
      DevCacheManager.RemoveCache(r_path)
1845
    except errors.BlockDeviceError, err:
1846
      msgs.append(str(err))
1847

    
1848
  if disk.children:
1849
    for child in disk.children:
1850
      try:
1851
        BlockdevShutdown(child)
1852
      except RPCFail, err:
1853
        msgs.append(str(err))
1854

    
1855
  if msgs:
1856
    _Fail("; ".join(msgs))
1857

    
1858

    
1859
def BlockdevAddchildren(parent_cdev, new_cdevs):
1860
  """Extend a mirrored block device.
1861

1862
  @type parent_cdev: L{objects.Disk}
1863
  @param parent_cdev: the disk to which we should add children
1864
  @type new_cdevs: list of L{objects.Disk}
1865
  @param new_cdevs: the list of children which we should add
1866
  @rtype: None
1867

1868
  """
1869
  parent_bdev = _RecursiveFindBD(parent_cdev)
1870
  if parent_bdev is None:
1871
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
1872
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1873
  if new_bdevs.count(None) > 0:
1874
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
1875
  parent_bdev.AddChildren(new_bdevs)
1876

    
1877

    
1878
def BlockdevRemovechildren(parent_cdev, new_cdevs):
1879
  """Shrink a mirrored block device.
1880

1881
  @type parent_cdev: L{objects.Disk}
1882
  @param parent_cdev: the disk from which we should remove children
1883
  @type new_cdevs: list of L{objects.Disk}
1884
  @param new_cdevs: the list of children which we should remove
1885
  @rtype: None
1886

1887
  """
1888
  parent_bdev = _RecursiveFindBD(parent_cdev)
1889
  if parent_bdev is None:
1890
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
1891
  devs = []
1892
  for disk in new_cdevs:
1893
    rpath = disk.StaticDevPath()
1894
    if rpath is None:
1895
      bd = _RecursiveFindBD(disk)
1896
      if bd is None:
1897
        _Fail("Can't find device %s while removing children", disk)
1898
      else:
1899
        devs.append(bd.dev_path)
1900
    else:
1901
      if not utils.IsNormAbsPath(rpath):
1902
        _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
1903
      devs.append(rpath)
1904
  parent_bdev.RemoveChildren(devs)
1905

    
1906

    
1907
def BlockdevGetmirrorstatus(disks):
1908
  """Get the mirroring status of a list of devices.
1909

1910
  @type disks: list of L{objects.Disk}
1911
  @param disks: the list of disks which we should query
1912
  @rtype: disk
1913
  @return: List of L{objects.BlockDevStatus}, one for each disk
1914
  @raise errors.BlockDeviceError: if any of the disks cannot be
1915
      found
1916

1917
  """
1918
  stats = []
1919
  for dsk in disks:
1920
    rbd = _RecursiveFindBD(dsk)
1921
    if rbd is None:
1922
      _Fail("Can't find device %s", dsk)
1923

    
1924
    stats.append(rbd.CombinedSyncStatus())
1925

    
1926
  return stats
1927

    
1928

    
1929
def BlockdevGetmirrorstatusMulti(disks):
1930
  """Get the mirroring status of a list of devices.
1931

1932
  @type disks: list of L{objects.Disk}
1933
  @param disks: the list of disks which we should query
1934
  @rtype: disk
1935
  @return: List of tuples, (bool, status), one for each disk; bool denotes
1936
    success/failure, status is L{objects.BlockDevStatus} on success, string
1937
    otherwise
1938

1939
  """
1940
  result = []
1941
  for disk in disks:
1942
    try:
1943
      rbd = _RecursiveFindBD(disk)
1944
      if rbd is None:
1945
        result.append((False, "Can't find device %s" % disk))
1946
        continue
1947

    
1948
      status = rbd.CombinedSyncStatus()
1949
    except errors.BlockDeviceError, err:
1950
      logging.exception("Error while getting disk status")
1951
      result.append((False, str(err)))
1952
    else:
1953
      result.append((True, status))
1954

    
1955
  assert len(disks) == len(result)
1956

    
1957
  return result
1958

    
1959

    
1960
def _RecursiveFindBD(disk):
1961
  """Check if a device is activated.
1962

1963
  If so, return information about the real device.
1964

1965
  @type disk: L{objects.Disk}
1966
  @param disk: the disk object we need to find
1967

1968
  @return: None if the device can't be found,
1969
      otherwise the device instance
1970

1971
  """
1972
  children = []
1973
  if disk.children:
1974
    for chdisk in disk.children:
1975
      children.append(_RecursiveFindBD(chdisk))
1976

    
1977
  return bdev.FindDevice(disk, children)
1978

    
1979

    
1980
def _OpenRealBD(disk):
1981
  """Opens the underlying block device of a disk.
1982

1983
  @type disk: L{objects.Disk}
1984
  @param disk: the disk object we want to open
1985

1986
  """
1987
  real_disk = _RecursiveFindBD(disk)
1988
  if real_disk is None:
1989
    _Fail("Block device '%s' is not set up", disk)
1990

    
1991
  real_disk.Open()
1992

    
1993
  return real_disk
1994

    
1995

    
1996
def BlockdevFind(disk):
1997
  """Check if a device is activated.
1998

1999
  If it is, return information about the real device.
2000

2001
  @type disk: L{objects.Disk}
2002
  @param disk: the disk to find
2003
  @rtype: None or objects.BlockDevStatus
2004
  @return: None if the disk cannot be found, otherwise a the current
2005
           information
2006

2007
  """
2008
  try:
2009
    rbd = _RecursiveFindBD(disk)
2010
  except errors.BlockDeviceError, err:
2011
    _Fail("Failed to find device: %s", err, exc=True)
2012

    
2013
  if rbd is None:
2014
    return None
2015

    
2016
  return rbd.GetSyncStatus()
2017

    
2018

    
2019
def BlockdevGetsize(disks):
2020
  """Computes the size of the given disks.
2021

2022
  If a disk is not found, returns None instead.
2023

2024
  @type disks: list of L{objects.Disk}
2025
  @param disks: the list of disk to compute the size for
2026
  @rtype: list
2027
  @return: list with elements None if the disk cannot be found,
2028
      otherwise the size
2029

2030
  """
2031
  result = []
2032
  for cf in disks:
2033
    try:
2034
      rbd = _RecursiveFindBD(cf)
2035
    except errors.BlockDeviceError:
2036
      result.append(None)
2037
      continue
2038
    if rbd is None:
2039
      result.append(None)
2040
    else:
2041
      result.append(rbd.GetActualSize())
2042
  return result
2043

    
2044

    
2045
def BlockdevExport(disk, dest_node, dest_path, cluster_name):
2046
  """Export a block device to a remote node.
2047

2048
  @type disk: L{objects.Disk}
2049
  @param disk: the description of the disk to export
2050
  @type dest_node: str
2051
  @param dest_node: the destination node to export to
2052
  @type dest_path: str
2053
  @param dest_path: the destination path on the target node
2054
  @type cluster_name: str
2055
  @param cluster_name: the cluster name, needed for SSH hostalias
2056
  @rtype: None
2057

2058
  """
2059
  real_disk = _OpenRealBD(disk)
2060

    
2061
  # the block size on the read dd is 1MiB to match our units
2062
  expcmd = utils.BuildShellCmd("set -e; set -o pipefail; "
2063
                               "dd if=%s bs=1048576 count=%s",
2064
                               real_disk.dev_path, str(disk.size))
2065

    
2066
  # we set here a smaller block size as, due to ssh buffering, more
2067
  # than 64-128k will mostly ignored; we use nocreat to fail if the
2068
  # device is not already there or we pass a wrong path; we use
2069
  # notrunc to no attempt truncate on an LV device; we use oflag=dsync
2070
  # to not buffer too much memory; this means that at best, we flush
2071
  # every 64k, which will not be very fast
2072
  destcmd = utils.BuildShellCmd("dd of=%s conv=nocreat,notrunc bs=65536"
2073
                                " oflag=dsync", dest_path)
2074

    
2075
  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
2076
                                                   constants.SSH_LOGIN_USER,
2077
                                                   destcmd)
2078

    
2079
  # all commands have been checked, so we're safe to combine them
2080
  command = "|".join([expcmd, utils.ShellQuoteArgs(remotecmd)])
2081

    
2082
  result = utils.RunCmd(["bash", "-c", command])
2083

    
2084
  if result.failed:
2085
    _Fail("Disk copy command '%s' returned error: %s"
2086
          " output: %s", command, result.fail_reason, result.output)
2087

    
2088

    
2089
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
2090
  """Write a file to the filesystem.
2091

2092
  This allows the master to overwrite(!) a file. It will only perform
2093
  the operation if the file belongs to a list of configuration files.
2094

2095
  @type file_name: str
2096
  @param file_name: the target file name
2097
  @type data: str
2098
  @param data: the new contents of the file
2099
  @type mode: int
2100
  @param mode: the mode to give the file (can be None)
2101
  @type uid: string
2102
  @param uid: the owner of the file
2103
  @type gid: string
2104
  @param gid: the group of the file
2105
  @type atime: float
2106
  @param atime: the atime to set on the file (can be None)
2107
  @type mtime: float
2108
  @param mtime: the mtime to set on the file (can be None)
2109
  @rtype: None
2110

2111
  """
2112
  file_name = vcluster.LocalizeVirtualPath(file_name)
2113

    
2114
  if not os.path.isabs(file_name):
2115
    _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
2116

    
2117
  if file_name not in _ALLOWED_UPLOAD_FILES:
2118
    _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
2119
          file_name)
2120

    
2121
  raw_data = _Decompress(data)
2122

    
2123
  if not (isinstance(uid, basestring) and isinstance(gid, basestring)):
2124
    _Fail("Invalid username/groupname type")
2125

    
2126
  getents = runtime.GetEnts()
2127
  uid = getents.LookupUser(uid)
2128
  gid = getents.LookupGroup(gid)
2129

    
2130
  utils.SafeWriteFile(file_name, None,
2131
                      data=raw_data, mode=mode, uid=uid, gid=gid,
2132
                      atime=atime, mtime=mtime)
2133

    
2134

    
2135
def RunOob(oob_program, command, node, timeout):
2136
  """Executes oob_program with given command on given node.
2137

2138
  @param oob_program: The path to the executable oob_program
2139
  @param command: The command to invoke on oob_program
2140
  @param node: The node given as an argument to the program
2141
  @param timeout: Timeout after which we kill the oob program
2142

2143
  @return: stdout
2144
  @raise RPCFail: If execution fails for some reason
2145

2146
  """
2147
  result = utils.RunCmd([oob_program, command, node], timeout=timeout)
2148

    
2149
  if result.failed:
2150
    _Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
2151
          result.fail_reason, result.output)
2152

    
2153
  return result.stdout
2154

    
2155

    
2156
def _OSOndiskAPIVersion(os_dir):
2157
  """Compute and return the API version of a given OS.
2158

2159
  This function will try to read the API version of the OS residing in
2160
  the 'os_dir' directory.
2161

2162
  @type os_dir: str
2163
  @param os_dir: the directory in which we should look for the OS
2164
  @rtype: tuple
2165
  @return: tuple (status, data) with status denoting the validity and
2166
      data holding either the vaid versions or an error message
2167

2168
  """
2169
  api_file = utils.PathJoin(os_dir, constants.OS_API_FILE)
2170

    
2171
  try:
2172
    st = os.stat(api_file)
2173
  except EnvironmentError, err:
2174
    return False, ("Required file '%s' not found under path %s: %s" %
2175
                   (constants.OS_API_FILE, os_dir, utils.ErrnoOrStr(err)))
2176

    
2177
  if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2178
    return False, ("File '%s' in %s is not a regular file" %
2179
                   (constants.OS_API_FILE, os_dir))
2180

    
2181
  try:
2182
    api_versions = utils.ReadFile(api_file).splitlines()
2183
  except EnvironmentError, err:
2184
    return False, ("Error while reading the API version file at %s: %s" %
2185
                   (api_file, utils.ErrnoOrStr(err)))
2186

    
2187
  try:
2188
    api_versions = [int(version.strip()) for version in api_versions]
2189
  except (TypeError, ValueError), err:
2190
    return False, ("API version(s) can't be converted to integer: %s" %
2191
                   str(err))
2192

    
2193
  return True, api_versions
2194

    
2195

    
2196
def DiagnoseOS(top_dirs=None):
2197
  """Compute the validity for all OSes.
2198

2199
  @type top_dirs: list
2200
  @param top_dirs: the list of directories in which to
2201
      search (if not given defaults to
2202
      L{pathutils.OS_SEARCH_PATH})
2203
  @rtype: list of L{objects.OS}
2204
  @return: a list of tuples (name, path, status, diagnose, variants,
2205
      parameters, api_version) for all (potential) OSes under all
2206
      search paths, where:
2207
          - name is the (potential) OS name
2208
          - path is the full path to the OS
2209
          - status True/False is the validity of the OS
2210
          - diagnose is the error message for an invalid OS, otherwise empty
2211
          - variants is a list of supported OS variants, if any
2212
          - parameters is a list of (name, help) parameters, if any
2213
          - api_version is a list of support OS API versions
2214

2215
  """
2216
  if top_dirs is None:
2217
    top_dirs = pathutils.OS_SEARCH_PATH
2218

    
2219
  result = []
2220
  for dir_name in top_dirs:
2221
    if os.path.isdir(dir_name):
2222
      try:
2223
        f_names = utils.ListVisibleFiles(dir_name)
2224
      except EnvironmentError, err:
2225
        logging.exception("Can't list the OS directory %s: %s", dir_name, err)
2226
        break
2227
      for name in f_names:
2228
        os_path = utils.PathJoin(dir_name, name)
2229
        status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
2230
        if status:
2231
          diagnose = ""
2232
          variants = os_inst.supported_variants
2233
          parameters = os_inst.supported_parameters
2234
          api_versions = os_inst.api_versions
2235
        else:
2236
          diagnose = os_inst
2237
          variants = parameters = api_versions = []
2238
        result.append((name, os_path, status, diagnose, variants,
2239
                       parameters, api_versions))
2240

    
2241
  return result
2242

    
2243

    
2244
def _TryOSFromDisk(name, base_dir=None):
2245
  """Create an OS instance from disk.
2246

2247
  This function will return an OS instance if the given name is a
2248
  valid OS name.
2249

2250
  @type base_dir: string
2251
  @keyword base_dir: Base directory containing OS installations.
2252
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2253
  @rtype: tuple
2254
  @return: success and either the OS instance if we find a valid one,
2255
      or error message
2256

2257
  """
2258
  if base_dir is None:
2259
    os_dir = utils.FindFile(name, pathutils.OS_SEARCH_PATH, os.path.isdir)
2260
  else:
2261
    os_dir = utils.FindFile(name, [base_dir], os.path.isdir)
2262

    
2263
  if os_dir is None:
2264
    return False, "Directory for OS %s not found in search path" % name
2265

    
2266
  status, api_versions = _OSOndiskAPIVersion(os_dir)
2267
  if not status:
2268
    # push the error up
2269
    return status, api_versions
2270

    
2271
  if not constants.OS_API_VERSIONS.intersection(api_versions):
2272
    return False, ("API version mismatch for path '%s': found %s, want %s." %
2273
                   (os_dir, api_versions, constants.OS_API_VERSIONS))
2274

    
2275
  # OS Files dictionary, we will populate it with the absolute path
2276
  # names; if the value is True, then it is a required file, otherwise
2277
  # an optional one
2278
  os_files = dict.fromkeys(constants.OS_SCRIPTS, True)
2279

    
2280
  if max(api_versions) >= constants.OS_API_V15:
2281
    os_files[constants.OS_VARIANTS_FILE] = False
2282

    
2283
  if max(api_versions) >= constants.OS_API_V20:
2284
    os_files[constants.OS_PARAMETERS_FILE] = True
2285
  else:
2286
    del os_files[constants.OS_SCRIPT_VERIFY]
2287

    
2288
  for (filename, required) in os_files.items():
2289
    os_files[filename] = utils.PathJoin(os_dir, filename)
2290

    
2291
    try:
2292
      st = os.stat(os_files[filename])
2293
    except EnvironmentError, err:
2294
      if err.errno == errno.ENOENT and not required:
2295
        del os_files[filename]
2296
        continue
2297
      return False, ("File '%s' under path '%s' is missing (%s)" %
2298
                     (filename, os_dir, utils.ErrnoOrStr(err)))
2299

    
2300
    if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2301
      return False, ("File '%s' under path '%s' is not a regular file" %
2302
                     (filename, os_dir))
2303

    
2304
    if filename in constants.OS_SCRIPTS:
2305
      if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
2306
        return False, ("File '%s' under path '%s' is not executable" %
2307
                       (filename, os_dir))
2308

    
2309
  variants = []
2310
  if constants.OS_VARIANTS_FILE in os_files:
2311
    variants_file = os_files[constants.OS_VARIANTS_FILE]
2312
    try:
2313
      variants = \
2314
        utils.FilterEmptyLinesAndComments(utils.ReadFile(variants_file))
2315
    except EnvironmentError, err:
2316
      # we accept missing files, but not other errors
2317
      if err.errno != errno.ENOENT:
2318
        return False, ("Error while reading the OS variants file at %s: %s" %
2319
                       (variants_file, utils.ErrnoOrStr(err)))
2320

    
2321
  parameters = []
2322
  if constants.OS_PARAMETERS_FILE in os_files:
2323
    parameters_file = os_files[constants.OS_PARAMETERS_FILE]
2324
    try:
2325
      parameters = utils.ReadFile(parameters_file).splitlines()
2326
    except EnvironmentError, err:
2327
      return False, ("Error while reading the OS parameters file at %s: %s" %
2328
                     (parameters_file, utils.ErrnoOrStr(err)))
2329
    parameters = [v.split(None, 1) for v in parameters]
2330

    
2331
  os_obj = objects.OS(name=name, path=os_dir,
2332
                      create_script=os_files[constants.OS_SCRIPT_CREATE],
2333
                      export_script=os_files[constants.OS_SCRIPT_EXPORT],
2334
                      import_script=os_files[constants.OS_SCRIPT_IMPORT],
2335
                      rename_script=os_files[constants.OS_SCRIPT_RENAME],
2336
                      verify_script=os_files.get(constants.OS_SCRIPT_VERIFY,
2337
                                                 None),
2338
                      supported_variants=variants,
2339
                      supported_parameters=parameters,
2340
                      api_versions=api_versions)
2341
  return True, os_obj
2342

    
2343

    
2344
def OSFromDisk(name, base_dir=None):
2345
  """Create an OS instance from disk.
2346

2347
  This function will return an OS instance if the given name is a
2348
  valid OS name. Otherwise, it will raise an appropriate
2349
  L{RPCFail} exception, detailing why this is not a valid OS.
2350

2351
  This is just a wrapper over L{_TryOSFromDisk}, which doesn't raise
2352
  an exception but returns true/false status data.
2353

2354
  @type base_dir: string
2355
  @keyword base_dir: Base directory containing OS installations.
2356
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2357
  @rtype: L{objects.OS}
2358
  @return: the OS instance if we find a valid one
2359
  @raise RPCFail: if we don't find a valid OS
2360

2361
  """
2362
  name_only = objects.OS.GetName(name)
2363
  status, payload = _TryOSFromDisk(name_only, base_dir)
2364

    
2365
  if not status:
2366
    _Fail(payload)
2367

    
2368
  return payload
2369

    
2370

    
2371
def OSCoreEnv(os_name, inst_os, os_params, debug=0):
2372
  """Calculate the basic environment for an os script.
2373

2374
  @type os_name: str
2375
  @param os_name: full operating system name (including variant)
2376
  @type inst_os: L{objects.OS}
2377
  @param inst_os: operating system for which the environment is being built
2378
  @type os_params: dict
2379
  @param os_params: the OS parameters
2380
  @type debug: integer
2381
  @param debug: debug level (0 or 1, for OS Api 10)
2382
  @rtype: dict
2383
  @return: dict of environment variables
2384
  @raise errors.BlockDeviceError: if the block device
2385
      cannot be found
2386

2387
  """
2388
  result = {}
2389
  api_version = \
2390
    max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
2391
  result["OS_API_VERSION"] = "%d" % api_version
2392
  result["OS_NAME"] = inst_os.name
2393
  result["DEBUG_LEVEL"] = "%d" % debug
2394

    
2395
  # OS variants
2396
  if api_version >= constants.OS_API_V15 and inst_os.supported_variants:
2397
    variant = objects.OS.GetVariant(os_name)
2398
    if not variant:
2399
      variant = inst_os.supported_variants[0]
2400
  else:
2401
    variant = ""
2402
  result["OS_VARIANT"] = variant
2403

    
2404
  # OS params
2405
  for pname, pvalue in os_params.items():
2406
    result["OSP_%s" % pname.upper()] = pvalue
2407

    
2408
  # Set a default path otherwise programs called by OS scripts (or
2409
  # even hooks called from OS scripts) might break, and we don't want
2410
  # to have each script require setting a PATH variable
2411
  result["PATH"] = constants.HOOKS_PATH
2412

    
2413
  return result
2414

    
2415

    
2416
def OSEnvironment(instance, inst_os, debug=0):
2417
  """Calculate the environment for an os script.
2418

2419
  @type instance: L{objects.Instance}
2420
  @param instance: target instance for the os script run
2421
  @type inst_os: L{objects.OS}
2422
  @param inst_os: operating system for which the environment is being built
2423
  @type debug: integer
2424
  @param debug: debug level (0 or 1, for OS Api 10)
2425
  @rtype: dict
2426
  @return: dict of environment variables
2427
  @raise errors.BlockDeviceError: if the block device
2428
      cannot be found
2429

2430
  """
2431
  result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug)
2432

    
2433
  for attr in ["name", "os", "uuid", "ctime", "mtime", "primary_node"]:
2434
    result["INSTANCE_%s" % attr.upper()] = str(getattr(instance, attr))
2435

    
2436
  result["HYPERVISOR"] = instance.hypervisor
2437
  result["DISK_COUNT"] = "%d" % len(instance.disks)
2438
  result["NIC_COUNT"] = "%d" % len(instance.nics)
2439
  result["INSTANCE_SECONDARY_NODES"] = \
2440
      ("%s" % " ".join(instance.secondary_nodes))
2441

    
2442
  # Disks
2443
  for idx, disk in enumerate(instance.disks):
2444
    real_disk = _OpenRealBD(disk)
2445
    result["DISK_%d_PATH" % idx] = real_disk.dev_path
2446
    result["DISK_%d_ACCESS" % idx] = disk.mode
2447
    if constants.HV_DISK_TYPE in instance.hvparams:
2448
      result["DISK_%d_FRONTEND_TYPE" % idx] = \
2449
        instance.hvparams[constants.HV_DISK_TYPE]
2450
    if disk.dev_type in constants.LDS_BLOCK:
2451
      result["DISK_%d_BACKEND_TYPE" % idx] = "block"
2452
    elif disk.dev_type == constants.LD_FILE:
2453
      result["DISK_%d_BACKEND_TYPE" % idx] = \
2454
        "file:%s" % disk.physical_id[0]
2455

    
2456
  # NICs
2457
  for idx, nic in enumerate(instance.nics):
2458
    result["NIC_%d_MAC" % idx] = nic.mac
2459
    if nic.ip:
2460
      result["NIC_%d_IP" % idx] = nic.ip
2461
    result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
2462
    if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
2463
      result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
2464
    if nic.nicparams[constants.NIC_LINK]:
2465
      result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
2466
    if nic.network:
2467
      result["NIC_%d_NETWORK" % idx] = nic.network
2468
    if constants.HV_NIC_TYPE in instance.hvparams:
2469
      result["NIC_%d_FRONTEND_TYPE" % idx] = \
2470
        instance.hvparams[constants.HV_NIC_TYPE]
2471

    
2472
  # HV/BE params
2473
  for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
2474
    for key, value in source.items():
2475
      result["INSTANCE_%s_%s" % (kind, key)] = str(value)
2476

    
2477
  return result
2478

    
2479

    
2480
def BlockdevGrow(disk, amount, dryrun, backingstore):
2481
  """Grow a stack of block devices.
2482

2483
  This function is called recursively, with the childrens being the
2484
  first ones to resize.
2485

2486
  @type disk: L{objects.Disk}
2487
  @param disk: the disk to be grown
2488
  @type amount: integer
2489
  @param amount: the amount (in mebibytes) to grow with
2490
  @type dryrun: boolean
2491
  @param dryrun: whether to execute the operation in simulation mode
2492
      only, without actually increasing the size
2493
  @param backingstore: whether to execute the operation on backing storage
2494
      only, or on "logical" storage only; e.g. DRBD is logical storage,
2495
      whereas LVM, file, RBD are backing storage
2496
  @rtype: (status, result)
2497
  @return: a tuple with the status of the operation (True/False), and
2498
      the errors message if status is False
2499

2500
  """
2501
  r_dev = _RecursiveFindBD(disk)
2502
  if r_dev is None:
2503
    _Fail("Cannot find block device %s", disk)
2504

    
2505
  try:
2506
    r_dev.Grow(amount, dryrun, backingstore)
2507
  except errors.BlockDeviceError, err:
2508
    _Fail("Failed to grow block device: %s", err, exc=True)
2509

    
2510

    
2511
def BlockdevSnapshot(disk):
2512
  """Create a snapshot copy of a block device.
2513

2514
  This function is called recursively, and the snapshot is actually created
2515
  just for the leaf lvm backend device.
2516

2517
  @type disk: L{objects.Disk}
2518
  @param disk: the disk to be snapshotted
2519
  @rtype: string
2520
  @return: snapshot disk ID as (vg, lv)
2521

2522
  """
2523
  if disk.dev_type == constants.LD_DRBD8:
2524
    if not disk.children:
2525
      _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
2526
            disk.unique_id)
2527
    return BlockdevSnapshot(disk.children[0])
2528
  elif disk.dev_type == constants.LD_LV:
2529
    r_dev = _RecursiveFindBD(disk)
2530
    if r_dev is not None:
2531
      # FIXME: choose a saner value for the snapshot size
2532
      # let's stay on the safe side and ask for the full size, for now
2533
      return r_dev.Snapshot(disk.size)
2534
    else:
2535
      _Fail("Cannot find block device %s", disk)
2536
  else:
2537
    _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
2538
          disk.unique_id, disk.dev_type)
2539

    
2540

    
2541
def BlockdevSetInfo(disk, info):
2542
  """Sets 'metadata' information on block devices.
2543

2544
  This function sets 'info' metadata on block devices. Initial
2545
  information is set at device creation; this function should be used
2546
  for example after renames.
2547

2548
  @type disk: L{objects.Disk}
2549
  @param disk: the disk to be grown
2550
  @type info: string
2551
  @param info: new 'info' metadata
2552
  @rtype: (status, result)
2553
  @return: a tuple with the status of the operation (True/False), and
2554
      the errors message if status is False
2555

2556
  """
2557
  r_dev = _RecursiveFindBD(disk)
2558
  if r_dev is None:
2559
    _Fail("Cannot find block device %s", disk)
2560

    
2561
  try:
2562
    r_dev.SetInfo(info)
2563
  except errors.BlockDeviceError, err:
2564
    _Fail("Failed to set information on block device: %s", err, exc=True)
2565

    
2566

    
2567
def FinalizeExport(instance, snap_disks):
2568
  """Write out the export configuration information.
2569

2570
  @type instance: L{objects.Instance}
2571
  @param instance: the instance which we export, used for
2572
      saving configuration
2573
  @type snap_disks: list of L{objects.Disk}
2574
  @param snap_disks: list of snapshot block devices, which
2575
      will be used to get the actual name of the dump file
2576

2577
  @rtype: None
2578

2579
  """
2580
  destdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name + ".new")
2581
  finaldestdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name)
2582

    
2583
  config = objects.SerializableConfigParser()
2584

    
2585
  config.add_section(constants.INISECT_EXP)
2586
  config.set(constants.INISECT_EXP, "version", "0")
2587
  config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time()))
2588
  config.set(constants.INISECT_EXP, "source", instance.primary_node)
2589
  config.set(constants.INISECT_EXP, "os", instance.os)
2590
  config.set(constants.INISECT_EXP, "compression", "none")
2591

    
2592
  config.add_section(constants.INISECT_INS)
2593
  config.set(constants.INISECT_INS, "name", instance.name)
2594
  config.set(constants.INISECT_INS, "maxmem", "%d" %
2595
             instance.beparams[constants.BE_MAXMEM])
2596
  config.set(constants.INISECT_INS, "minmem", "%d" %
2597
             instance.beparams[constants.BE_MINMEM])
2598
  # "memory" is deprecated, but useful for exporting to old ganeti versions
2599
  config.set(constants.INISECT_INS, "memory", "%d" %
2600
             instance.beparams[constants.BE_MAXMEM])
2601
  config.set(constants.INISECT_INS, "vcpus", "%d" %
2602
             instance.beparams[constants.BE_VCPUS])
2603
  config.set(constants.INISECT_INS, "disk_template", instance.disk_template)
2604
  config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
2605
  config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags()))
2606

    
2607
  nic_total = 0
2608
  for nic_count, nic in enumerate(instance.nics):
2609
    nic_total += 1
2610
    config.set(constants.INISECT_INS, "nic%d_mac" %
2611
               nic_count, "%s" % nic.mac)
2612
    config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip)
2613
    config.set(constants.INISECT_INS, "nic%d_network" % nic_count,
2614
               "%s" % nic.network)
2615
    for param in constants.NICS_PARAMETER_TYPES:
2616
      config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
2617
                 "%s" % nic.nicparams.get(param, None))
2618
  # TODO: redundant: on load can read nics until it doesn't exist
2619
  config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total)
2620

    
2621
  disk_total = 0
2622
  for disk_count, disk in enumerate(snap_disks):
2623
    if disk:
2624
      disk_total += 1
2625
      config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
2626
                 ("%s" % disk.iv_name))
2627
      config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
2628
                 ("%s" % disk.physical_id[1]))
2629
      config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
2630
                 ("%d" % disk.size))
2631

    
2632
  config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total)
2633

    
2634
  # New-style hypervisor/backend parameters
2635

    
2636
  config.add_section(constants.INISECT_HYP)
2637
  for name, value in instance.hvparams.items():
2638
    if name not in constants.HVC_GLOBALS:
2639
      config.set(constants.INISECT_HYP, name, str(value))
2640

    
2641
  config.add_section(constants.INISECT_BEP)
2642
  for name, value in instance.beparams.items():
2643
    config.set(constants.INISECT_BEP, name, str(value))
2644

    
2645
  config.add_section(constants.INISECT_OSP)
2646
  for name, value in instance.osparams.items():
2647
    config.set(constants.INISECT_OSP, name, str(value))
2648

    
2649
  utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE),
2650
                  data=config.Dumps())
2651
  shutil.rmtree(finaldestdir, ignore_errors=True)
2652
  shutil.move(destdir, finaldestdir)
2653

    
2654

    
2655
def ExportInfo(dest):
2656
  """Get export configuration information.
2657

2658
  @type dest: str
2659
  @param dest: directory containing the export
2660

2661
  @rtype: L{objects.SerializableConfigParser}
2662
  @return: a serializable config file containing the
2663
      export info
2664

2665
  """
2666
  cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE)
2667

    
2668
  config = objects.SerializableConfigParser()
2669
  config.read(cff)
2670

    
2671
  if (not config.has_section(constants.INISECT_EXP) or
2672
      not config.has_section(constants.INISECT_INS)):
2673
    _Fail("Export info file doesn't have the required fields")
2674

    
2675
  return config.Dumps()
2676

    
2677

    
2678
def ListExports():
2679
  """Return a list of exports currently available on this machine.
2680

2681
  @rtype: list
2682
  @return: list of the exports
2683

2684
  """
2685
  if os.path.isdir(pathutils.EXPORT_DIR):
2686
    return sorted(utils.ListVisibleFiles(pathutils.EXPORT_DIR))
2687
  else:
2688
    _Fail("No exports directory")
2689

    
2690

    
2691
def RemoveExport(export):
2692
  """Remove an existing export from the node.
2693

2694
  @type export: str
2695
  @param export: the name of the export to remove
2696
  @rtype: None
2697

2698
  """
2699
  target = utils.PathJoin(pathutils.EXPORT_DIR, export)
2700

    
2701
  try:
2702
    shutil.rmtree(target)
2703
  except EnvironmentError, err:
2704
    _Fail("Error while removing the export: %s", err, exc=True)
2705

    
2706

    
2707
def BlockdevRename(devlist):
2708
  """Rename a list of block devices.
2709

2710
  @type devlist: list of tuples
2711
  @param devlist: list of tuples of the form  (disk,
2712
      new_logical_id, new_physical_id); disk is an
2713
      L{objects.Disk} object describing the current disk,
2714
      and new logical_id/physical_id is the name we
2715
      rename it to
2716
  @rtype: boolean
2717
  @return: True if all renames succeeded, False otherwise
2718

2719
  """
2720
  msgs = []
2721
  result = True
2722
  for disk, unique_id in devlist:
2723
    dev = _RecursiveFindBD(disk)
2724
    if dev is None:
2725
      msgs.append("Can't find device %s in rename" % str(disk))
2726
      result = False
2727
      continue
2728
    try:
2729
      old_rpath = dev.dev_path
2730
      dev.Rename(unique_id)
2731
      new_rpath = dev.dev_path
2732
      if old_rpath != new_rpath:
2733
        DevCacheManager.RemoveCache(old_rpath)
2734
        # FIXME: we should add the new cache information here, like:
2735
        # DevCacheManager.UpdateCache(new_rpath, owner, ...)
2736
        # but we don't have the owner here - maybe parse from existing
2737
        # cache? for now, we only lose lvm data when we rename, which
2738
        # is less critical than DRBD or MD
2739
    except errors.BlockDeviceError, err:
2740
      msgs.append("Can't rename device '%s' to '%s': %s" %
2741
                  (dev, unique_id, err))
2742
      logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
2743
      result = False
2744
  if not result:
2745
    _Fail("; ".join(msgs))
2746

    
2747

    
2748
def _TransformFileStorageDir(fs_dir):
2749
  """Checks whether given file_storage_dir is valid.
2750

2751
  Checks wheter the given fs_dir is within the cluster-wide default
2752
  file_storage_dir or the shared_file_storage_dir, which are stored in
2753
  SimpleStore. Only paths under those directories are allowed.
2754

2755
  @type fs_dir: str
2756
  @param fs_dir: the path to check
2757

2758
  @return: the normalized path if valid, None otherwise
2759

2760
  """
2761
  if not (constants.ENABLE_FILE_STORAGE or
2762
          constants.ENABLE_SHARED_FILE_STORAGE):
2763
    _Fail("File storage disabled at configure time")
2764

    
2765
  bdev.CheckFileStoragePath(fs_dir)
2766

    
2767
  return os.path.normpath(fs_dir)
2768

    
2769

    
2770
def CreateFileStorageDir(file_storage_dir):
2771
  """Create file storage directory.
2772

2773
  @type file_storage_dir: str
2774
  @param file_storage_dir: directory to create
2775

2776
  @rtype: tuple
2777
  @return: tuple with first element a boolean indicating wheter dir
2778
      creation was successful or not
2779

2780
  """
2781
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2782
  if os.path.exists(file_storage_dir):
2783
    if not os.path.isdir(file_storage_dir):
2784
      _Fail("Specified storage dir '%s' is not a directory",
2785
            file_storage_dir)
2786
  else:
2787
    try:
2788
      os.makedirs(file_storage_dir, 0750)
2789
    except OSError, err:
2790
      _Fail("Cannot create file storage directory '%s': %s",
2791
            file_storage_dir, err, exc=True)
2792

    
2793

    
2794
def RemoveFileStorageDir(file_storage_dir):
2795
  """Remove file storage directory.
2796

2797
  Remove it only if it's empty. If not log an error and return.
2798

2799
  @type file_storage_dir: str
2800
  @param file_storage_dir: the directory we should cleanup
2801
  @rtype: tuple (success,)
2802
  @return: tuple of one element, C{success}, denoting
2803
      whether the operation was successful
2804

2805
  """
2806
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2807
  if os.path.exists(file_storage_dir):
2808
    if not os.path.isdir(file_storage_dir):
2809
      _Fail("Specified Storage directory '%s' is not a directory",
2810
            file_storage_dir)
2811
    # deletes dir only if empty, otherwise we want to fail the rpc call
2812
    try:
2813
      os.rmdir(file_storage_dir)
2814
    except OSError, err:
2815
      _Fail("Cannot remove file storage directory '%s': %s",
2816
            file_storage_dir, err)
2817

    
2818

    
2819
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
2820
  """Rename the file storage directory.
2821

2822
  @type old_file_storage_dir: str
2823
  @param old_file_storage_dir: the current path
2824
  @type new_file_storage_dir: str
2825
  @param new_file_storage_dir: the name we should rename to
2826
  @rtype: tuple (success,)
2827
  @return: tuple of one element, C{success}, denoting
2828
      whether the operation was successful
2829

2830
  """
2831
  old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
2832
  new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
2833
  if not os.path.exists(new_file_storage_dir):
2834
    if os.path.isdir(old_file_storage_dir):
2835
      try:
2836
        os.rename(old_file_storage_dir, new_file_storage_dir)
2837
      except OSError, err:
2838
        _Fail("Cannot rename '%s' to '%s': %s",
2839
              old_file_storage_dir, new_file_storage_dir, err)
2840
    else:
2841
      _Fail("Specified storage dir '%s' is not a directory",
2842
            old_file_storage_dir)
2843
  else:
2844
    if os.path.exists(old_file_storage_dir):
2845
      _Fail("Cannot rename '%s' to '%s': both locations exist",
2846
            old_file_storage_dir, new_file_storage_dir)
2847

    
2848

    
2849
def _EnsureJobQueueFile(file_name):
2850
  """Checks whether the given filename is in the queue directory.
2851

2852
  @type file_name: str
2853
  @param file_name: the file name we should check
2854
  @rtype: None
2855
  @raises RPCFail: if the file is not valid
2856

2857
  """
2858
  if not utils.IsBelowDir(pathutils.QUEUE_DIR, file_name):
2859
    _Fail("Passed job queue file '%s' does not belong to"
2860
          " the queue directory '%s'", file_name, pathutils.QUEUE_DIR)
2861

    
2862

    
2863
def JobQueueUpdate(file_name, content):
2864
  """Updates a file in the queue directory.
2865

2866
  This is just a wrapper over L{utils.io.WriteFile}, with proper
2867
  checking.
2868

2869
  @type file_name: str
2870
  @param file_name: the job file name
2871
  @type content: str
2872
  @param content: the new job contents
2873
  @rtype: boolean
2874
  @return: the success of the operation
2875

2876
  """
2877
  file_name = vcluster.LocalizeVirtualPath(file_name)
2878

    
2879
  _EnsureJobQueueFile(file_name)
2880
  getents = runtime.GetEnts()
2881

    
2882
  # Write and replace the file atomically
2883
  utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid,
2884
                  gid=getents.masterd_gid)
2885

    
2886

    
2887
def JobQueueRename(old, new):
2888
  """Renames a job queue file.
2889

2890
  This is just a wrapper over os.rename with proper checking.
2891

2892
  @type old: str
2893
  @param old: the old (actual) file name
2894
  @type new: str
2895
  @param new: the desired file name
2896
  @rtype: tuple
2897
  @return: the success of the operation and payload
2898

2899
  """
2900
  old = vcluster.LocalizeVirtualPath(old)
2901
  new = vcluster.LocalizeVirtualPath(new)
2902

    
2903
  _EnsureJobQueueFile(old)
2904
  _EnsureJobQueueFile(new)
2905

    
2906
  getents = runtime.GetEnts()
2907

    
2908
  utils.RenameFile(old, new, mkdir=True, mkdir_mode=0700,
2909
                   dir_uid=getents.masterd_uid, dir_gid=getents.masterd_gid)
2910

    
2911

    
2912
def BlockdevClose(instance_name, disks):
2913
  """Closes the given block devices.
2914

2915
  This means they will be switched to secondary mode (in case of
2916
  DRBD).
2917

2918
  @param instance_name: if the argument is not empty, the symlinks
2919
      of this instance will be removed
2920
  @type disks: list of L{objects.Disk}
2921
  @param disks: the list of disks to be closed
2922
  @rtype: tuple (success, message)
2923
  @return: a tuple of success and message, where success
2924
      indicates the succes of the operation, and message
2925
      which will contain the error details in case we
2926
      failed
2927

2928
  """
2929
  bdevs = []
2930
  for cf in disks:
2931
    rd = _RecursiveFindBD(cf)
2932
    if rd is None:
2933
      _Fail("Can't find device %s", cf)
2934
    bdevs.append(rd)
2935

    
2936
  msg = []
2937
  for rd in bdevs:
2938
    try:
2939
      rd.Close()
2940
    except errors.BlockDeviceError, err:
2941
      msg.append(str(err))
2942
  if msg:
2943
    _Fail("Can't make devices secondary: %s", ",".join(msg))
2944
  else:
2945
    if instance_name:
2946
      _RemoveBlockDevLinks(instance_name, disks)
2947

    
2948

    
2949
def ValidateHVParams(hvname, hvparams):
2950
  """Validates the given hypervisor parameters.
2951

2952
  @type hvname: string
2953
  @param hvname: the hypervisor name
2954
  @type hvparams: dict
2955
  @param hvparams: the hypervisor parameters to be validated
2956
  @rtype: None
2957

2958
  """
2959
  try:
2960
    hv_type = hypervisor.GetHypervisor(hvname)
2961
    hv_type.ValidateParameters(hvparams)
2962
  except errors.HypervisorError, err:
2963
    _Fail(str(err), log=False)
2964

    
2965

    
2966
def _CheckOSPList(os_obj, parameters):
2967
  """Check whether a list of parameters is supported by the OS.
2968

2969
  @type os_obj: L{objects.OS}
2970
  @param os_obj: OS object to check
2971
  @type parameters: list
2972
  @param parameters: the list of parameters to check
2973

2974
  """
2975
  supported = [v[0] for v in os_obj.supported_parameters]
2976
  delta = frozenset(parameters).difference(supported)
2977
  if delta:
2978
    _Fail("The following parameters are not supported"
2979
          " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
2980

    
2981

    
2982
def ValidateOS(required, osname, checks, osparams):
2983
  """Validate the given OS' parameters.
2984

2985
  @type required: boolean
2986
  @param required: whether absence of the OS should translate into
2987
      failure or not
2988
  @type osname: string
2989
  @param osname: the OS to be validated
2990
  @type checks: list
2991
  @param checks: list of the checks to run (currently only 'parameters')
2992
  @type osparams: dict
2993
  @param osparams: dictionary with OS parameters
2994
  @rtype: boolean
2995
  @return: True if the validation passed, or False if the OS was not
2996
      found and L{required} was false
2997

2998
  """
2999
  if not constants.OS_VALIDATE_CALLS.issuperset(checks):
3000
    _Fail("Unknown checks required for OS %s: %s", osname,
3001
          set(checks).difference(constants.OS_VALIDATE_CALLS))
3002

    
3003
  name_only = objects.OS.GetName(osname)
3004
  status, tbv = _TryOSFromDisk(name_only, None)
3005

    
3006
  if not status:
3007
    if required:
3008
      _Fail(tbv)
3009
    else:
3010
      return False
3011

    
3012
  if max(tbv.api_versions) < constants.OS_API_V20:
3013
    return True
3014

    
3015
  if constants.OS_VALIDATE_PARAMETERS in checks:
3016
    _CheckOSPList(tbv, osparams.keys())
3017

    
3018
  validate_env = OSCoreEnv(osname, tbv, osparams)
3019
  result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env,
3020
                        cwd=tbv.path, reset_env=True)
3021
  if result.failed:
3022
    logging.error("os validate command '%s' returned error: %s output: %s",
3023
                  result.cmd, result.fail_reason, result.output)
3024
    _Fail("OS validation script failed (%s), output: %s",
3025
          result.fail_reason, result.output, log=False)
3026

    
3027
  return True
3028

    
3029

    
3030
def DemoteFromMC():
3031
  """Demotes the current node from master candidate role.
3032

3033
  """
3034
  # try to ensure we're not the master by mistake
3035
  master, myself = ssconf.GetMasterAndMyself()
3036
  if master == myself:
3037
    _Fail("ssconf status shows I'm the master node, will not demote")
3038

    
3039
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "check", constants.MASTERD])
3040
  if not result.failed:
3041
    _Fail("The master daemon is running, will not demote")
3042

    
3043
  try:
3044
    if os.path.isfile(pathutils.CLUSTER_CONF_FILE):
3045
      utils.CreateBackup(pathutils.CLUSTER_CONF_FILE)
3046
  except EnvironmentError, err:
3047
    if err.errno != errno.ENOENT:
3048
      _Fail("Error while backing up cluster file: %s", err, exc=True)
3049

    
3050
  utils.RemoveFile(pathutils.CLUSTER_CONF_FILE)
3051

    
3052

    
3053
def _GetX509Filenames(cryptodir, name):
3054
  """Returns the full paths for the private key and certificate.
3055

3056
  """
3057
  return (utils.PathJoin(cryptodir, name),
3058
          utils.PathJoin(cryptodir, name, _X509_KEY_FILE),
3059
          utils.PathJoin(cryptodir, name, _X509_CERT_FILE))
3060

    
3061

    
3062
def CreateX509Certificate(validity, cryptodir=pathutils.CRYPTO_KEYS_DIR):
3063
  """Creates a new X509 certificate for SSL/TLS.
3064

3065
  @type validity: int
3066
  @param validity: Validity in seconds
3067
  @rtype: tuple; (string, string)
3068
  @return: Certificate name and public part
3069

3070
  """
3071
  (key_pem, cert_pem) = \
3072
    utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(),
3073
                                     min(validity, _MAX_SSL_CERT_VALIDITY))
3074

    
3075
  cert_dir = tempfile.mkdtemp(dir=cryptodir,
3076
                              prefix="x509-%s-" % utils.TimestampForFilename())
3077
  try:
3078
    name = os.path.basename(cert_dir)
3079
    assert len(name) > 5
3080

    
3081
    (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
3082

    
3083
    utils.WriteFile(key_file, mode=0400, data=key_pem)
3084
    utils.WriteFile(cert_file, mode=0400, data=cert_pem)
3085

    
3086
    # Never return private key as it shouldn't leave the node
3087
    return (name, cert_pem)
3088
  except Exception:
3089
    shutil.rmtree(cert_dir, ignore_errors=True)
3090
    raise
3091

    
3092

    
3093
def RemoveX509Certificate(name, cryptodir=pathutils.CRYPTO_KEYS_DIR):
3094
  """Removes a X509 certificate.
3095

3096
  @type name: string
3097
  @param name: Certificate name
3098

3099
  """
3100
  (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
3101

    
3102
  utils.RemoveFile(key_file)
3103
  utils.RemoveFile(cert_file)
3104

    
3105
  try:
3106
    os.rmdir(cert_dir)
3107
  except EnvironmentError, err:
3108
    _Fail("Cannot remove certificate directory '%s': %s",
3109
          cert_dir, err)
3110

    
3111

    
3112
def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
3113
  """Returns the command for the requested input/output.
3114

3115
  @type instance: L{objects.Instance}
3116
  @param instance: The instance object
3117
  @param mode: Import/export mode
3118
  @param ieio: Input/output type
3119
  @param ieargs: Input/output arguments
3120

3121
  """
3122
  assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)
3123

    
3124
  env = None
3125
  prefix = None
3126
  suffix = None
3127
  exp_size = None
3128

    
3129
  if ieio == constants.IEIO_FILE:
3130
    (filename, ) = ieargs
3131

    
3132
    if not utils.IsNormAbsPath(filename):
3133
      _Fail("Path '%s' is not normalized or absolute", filename)
3134

    
3135
    real_filename = os.path.realpath(filename)
3136
    directory = os.path.dirname(real_filename)
3137

    
3138
    if not utils.IsBelowDir(pathutils.EXPORT_DIR, real_filename):
3139
      _Fail("File '%s' is not under exports directory '%s': %s",
3140
            filename, pathutils.EXPORT_DIR, real_filename)
3141

    
3142
    # Create directory
3143
    utils.Makedirs(directory, mode=0750)
3144

    
3145
    quoted_filename = utils.ShellQuote(filename)
3146

    
3147
    if mode == constants.IEM_IMPORT:
3148
      suffix = "> %s" % quoted_filename
3149
    elif mode == constants.IEM_EXPORT:
3150
      suffix = "< %s" % quoted_filename
3151

    
3152
      # Retrieve file size
3153
      try:
3154
        st = os.stat(filename)
3155
      except EnvironmentError, err:
3156
        logging.error("Can't stat(2) %s: %s", filename, err)
3157
      else:
3158
        exp_size = utils.BytesToMebibyte(st.st_size)
3159

    
3160
  elif ieio == constants.IEIO_RAW_DISK:
3161
    (disk, ) = ieargs
3162

    
3163
    real_disk = _OpenRealBD(disk)
3164

    
3165
    if mode == constants.IEM_IMPORT:
3166
      # we set here a smaller block size as, due to transport buffering, more
3167
      # than 64-128k will mostly ignored; we use nocreat to fail if the device
3168
      # is not already there or we pass a wrong path; we use notrunc to no
3169
      # attempt truncate on an LV device; we use oflag=dsync to not buffer too
3170
      # much memory; this means that at best, we flush every 64k, which will
3171
      # not be very fast
3172
      suffix = utils.BuildShellCmd(("| dd of=%s conv=nocreat,notrunc"
3173
                                    " bs=%s oflag=dsync"),
3174
                                    real_disk.dev_path,
3175
                                    str(64 * 1024))
3176

    
3177
    elif mode == constants.IEM_EXPORT:
3178
      # the block size on the read dd is 1MiB to match our units
3179
      prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
3180
                                   real_disk.dev_path,
3181
                                   str(1024 * 1024), # 1 MB
3182
                                   str(disk.size))
3183
      exp_size = disk.size
3184

    
3185
  elif ieio == constants.IEIO_SCRIPT:
3186
    (disk, disk_index, ) = ieargs
3187

    
3188
    assert isinstance(disk_index, (int, long))
3189

    
3190
    real_disk = _OpenRealBD(disk)
3191

    
3192
    inst_os = OSFromDisk(instance.os)
3193
    env = OSEnvironment(instance, inst_os)
3194

    
3195
    if mode == constants.IEM_IMPORT:
3196
      env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index]
3197
      env["IMPORT_INDEX"] = str(disk_index)
3198
      script = inst_os.import_script
3199

    
3200
    elif mode == constants.IEM_EXPORT:
3201
      env["EXPORT_DEVICE"] = real_disk.dev_path
3202
      env["EXPORT_INDEX"] = str(disk_index)
3203
      script = inst_os.export_script
3204

    
3205
    # TODO: Pass special environment only to script
3206
    script_cmd = utils.BuildShellCmd("( cd %s && %s; )", inst_os.path, script)
3207

    
3208
    if mode == constants.IEM_IMPORT:
3209
      suffix = "| %s" % script_cmd
3210

    
3211
    elif mode == constants.IEM_EXPORT:
3212
      prefix = "%s |" % script_cmd
3213

    
3214
    # Let script predict size
3215
    exp_size = constants.IE_CUSTOM_SIZE
3216

    
3217
  else:
3218
    _Fail("Invalid %s I/O mode %r", mode, ieio)
3219

    
3220
  return (env, prefix, suffix, exp_size)
3221

    
3222

    
3223
def _CreateImportExportStatusDir(prefix):
3224
  """Creates status directory for import/export.
3225

3226
  """
3227
  return tempfile.mkdtemp(dir=pathutils.IMPORT_EXPORT_DIR,
3228
                          prefix=("%s-%s-" %
3229
                                  (prefix, utils.TimestampForFilename())))
3230

    
3231

    
3232
def StartImportExportDaemon(mode, opts, host, port, instance, component,
3233
                            ieio, ieioargs):
3234
  """Starts an import or export daemon.
3235

3236
  @param mode: Import/output mode
3237
  @type opts: L{objects.ImportExportOptions}
3238
  @param opts: Daemon options
3239
  @type host: string
3240
  @param host: Remote host for export (None for import)
3241
  @type port: int
3242
  @param port: Remote port for export (None for import)
3243
  @type instance: L{objects.Instance}
3244
  @param instance: Instance object
3245
  @type component: string
3246
  @param component: which part of the instance is transferred now,
3247
      e.g. 'disk/0'
3248
  @param ieio: Input/output type
3249
  @param ieioargs: Input/output arguments
3250

3251
  """
3252
  if mode == constants.IEM_IMPORT:
3253
    prefix = "import"
3254

    
3255
    if not (host is None and port is None):
3256
      _Fail("Can not specify host or port on import")
3257

    
3258
  elif mode == constants.IEM_EXPORT:
3259
    prefix = "export"
3260

    
3261
    if host is None or port is None:
3262
      _Fail("Host and port must be specified for an export")
3263

    
3264
  else:
3265
    _Fail("Invalid mode %r", mode)
3266

    
3267
  if (opts.key_name is None) ^ (opts.ca_pem is None):
3268
    _Fail("Cluster certificate can only be used for both key and CA")
3269

    
3270
  (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \
3271
    _GetImportExportIoCommand(instance, mode, ieio, ieioargs)
3272

    
3273
  if opts.key_name is None:
3274
    # Use server.pem
3275
    key_path = pathutils.NODED_CERT_FILE
3276
    cert_path = pathutils.NODED_CERT_FILE
3277
    assert opts.ca_pem is None
3278
  else:
3279
    (_, key_path, cert_path) = _GetX509Filenames(pathutils.CRYPTO_KEYS_DIR,
3280
                                                 opts.key_name)
3281
    assert opts.ca_pem is not None
3282

    
3283
  for i in [key_path, cert_path]:
3284
    if not os.path.exists(i):
3285
      _Fail("File '%s' does not exist" % i)
3286

    
3287
  status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
3288
  try:
3289
    status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE)
3290
    pid_file = utils.PathJoin(status_dir, _IES_PID_FILE)
3291
    ca_file = utils.PathJoin(status_dir, _IES_CA_FILE)
3292

    
3293
    if opts.ca_pem is None:
3294
      # Use server.pem
3295
      ca = utils.ReadFile(pathutils.NODED_CERT_FILE)
3296
    else:
3297
      ca = opts.ca_pem
3298

    
3299
    # Write CA file
3300
    utils.WriteFile(ca_file, data=ca, mode=0400)
3301

    
3302
    cmd = [
3303
      pathutils.IMPORT_EXPORT_DAEMON,
3304
      status_file, mode,
3305
      "--key=%s" % key_path,
3306
      "--cert=%s" % cert_path,
3307
      "--ca=%s" % ca_file,
3308
      ]
3309

    
3310
    if host:
3311
      cmd.append("--host=%s" % host)
3312

    
3313
    if port:
3314
      cmd.append("--port=%s" % port)
3315

    
3316
    if opts.ipv6:
3317
      cmd.append("--ipv6")
3318
    else:
3319
      cmd.append("--ipv4")
3320

    
3321
    if opts.compress:
3322
      cmd.append("--compress=%s" % opts.compress)
3323

    
3324
    if opts.magic:
3325
      cmd.append("--magic=%s" % opts.magic)
3326

    
3327
    if exp_size is not None:
3328
      cmd.append("--expected-size=%s" % exp_size)
3329

    
3330
    if cmd_prefix:
3331
      cmd.append("--cmd-prefix=%s" % cmd_prefix)
3332

    
3333
    if cmd_suffix:
3334
      cmd.append("--cmd-suffix=%s" % cmd_suffix)
3335

    
3336
    if mode == constants.IEM_EXPORT:
3337
      # Retry connection a few times when connecting to remote peer
3338
      cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
3339
      cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
3340
    elif opts.connect_timeout is not None:
3341
      assert mode == constants.IEM_IMPORT
3342
      # Overall timeout for establishing connection while listening
3343
      cmd.append("--connect-timeout=%s" % opts.connect_timeout)
3344

    
3345
    logfile = _InstanceLogName(prefix, instance.os, instance.name, component)
3346

    
3347
    # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
3348
    # support for receiving a file descriptor for output
3349
    utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file,
3350
                      output=logfile)
3351

    
3352
    # The import/export name is simply the status directory name
3353
    return os.path.basename(status_dir)
3354

    
3355
  except Exception:
3356
    shutil.rmtree(status_dir, ignore_errors=True)
3357
    raise
3358

    
3359

    
3360
def GetImportExportStatus(names):
3361
  """Returns import/export daemon status.
3362

3363
  @type names: sequence
3364
  @param names: List of names
3365
  @rtype: List of dicts
3366
  @return: Returns a list of the state of each named import/export or None if a
3367
           status couldn't be read
3368

3369
  """
3370
  result = []
3371

    
3372
  for name in names:
3373
    status_file = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name,
3374
                                 _IES_STATUS_FILE)
3375

    
3376
    try:
3377
      data = utils.ReadFile(status_file)
3378
    except EnvironmentError, err:
3379
      if err.errno != errno.ENOENT:
3380
        raise
3381
      data = None
3382

    
3383
    if not data:
3384
      result.append(None)
3385
      continue
3386

    
3387
    result.append(serializer.LoadJson(data))
3388

    
3389
  return result
3390

    
3391

    
3392
def AbortImportExport(name):
3393
  """Sends SIGTERM to a running import/export daemon.
3394

3395
  """
3396
  logging.info("Abort import/export %s", name)
3397

    
3398
  status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name)
3399
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3400

    
3401
  if pid:
3402
    logging.info("Import/export %s is running with PID %s, sending SIGTERM",
3403
                 name, pid)
3404
    utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM)
3405

    
3406

    
3407
def CleanupImportExport(name):
3408
  """Cleanup after an import or export.
3409

3410
  If the import/export daemon is still running it's killed. Afterwards the
3411
  whole status directory is removed.
3412

3413
  """
3414
  logging.info("Finalizing import/export %s", name)
3415

    
3416
  status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name)
3417

    
3418
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
3419

    
3420
  if pid:
3421
    logging.info("Import/export %s is still running with PID %s",
3422
                 name, pid)
3423
    utils.KillProcess(pid, waitpid=False)
3424

    
3425
  shutil.rmtree(status_dir, ignore_errors=True)
3426

    
3427

    
3428
def _FindDisks(nodes_ip, disks):
3429
  """Sets the physical ID on disks and returns the block devices.
3430

3431
  """
3432
  # set the correct physical ID
3433
  my_name = netutils.Hostname.GetSysName()
3434
  for cf in disks:
3435
    cf.SetPhysicalID(my_name, nodes_ip)
3436

    
3437
  bdevs = []
3438

    
3439
  for cf in disks:
3440
    rd = _RecursiveFindBD(cf)
3441
    if rd is None:
3442
      _Fail("Can't find device %s", cf)
3443
    bdevs.append(rd)
3444
  return bdevs
3445

    
3446

    
3447
def DrbdDisconnectNet(nodes_ip, disks):
3448
  """Disconnects the network on a list of drbd devices.
3449

3450
  """
3451
  bdevs = _FindDisks(nodes_ip, disks)
3452

    
3453
  # disconnect disks
3454
  for rd in bdevs:
3455
    try:
3456
      rd.DisconnectNet()
3457
    except errors.BlockDeviceError, err:
3458
      _Fail("Can't change network configuration to standalone mode: %s",
3459
            err, exc=True)
3460

    
3461

    
3462
def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster):
3463
  """Attaches the network on a list of drbd devices.
3464

3465
  """
3466
  bdevs = _FindDisks(nodes_ip, disks)
3467

    
3468
  if multimaster:
3469
    for idx, rd in enumerate(bdevs):
3470
      try:
3471
        _SymlinkBlockDev(instance_name, rd.dev_path, idx)
3472
      except EnvironmentError, err:
3473
        _Fail("Can't create symlink: %s", err)
3474
  # reconnect disks, switch to new master configuration and if
3475
  # needed primary mode
3476
  for rd in bdevs:
3477
    try:
3478
      rd.AttachNet(multimaster)
3479
    except errors.BlockDeviceError, err:
3480
      _Fail("Can't change network configuration: %s", err)
3481

    
3482
  # wait until the disks are connected; we need to retry the re-attach
3483
  # if the device becomes standalone, as this might happen if the one
3484
  # node disconnects and reconnects in a different mode before the
3485
  # other node reconnects; in this case, one or both of the nodes will
3486
  # decide it has wrong configuration and switch to standalone
3487

    
3488
  def _Attach():
3489
    all_connected = True
3490

    
3491
    for rd in bdevs:
3492
      stats = rd.GetProcStatus()
3493

    
3494
      all_connected = (all_connected and
3495
                       (stats.is_connected or stats.is_in_resync))
3496

    
3497
      if stats.is_standalone:
3498
        # peer had different config info and this node became
3499
        # standalone, even though this should not happen with the
3500
        # new staged way of changing disk configs
3501
        try:
3502
          rd.AttachNet(multimaster)
3503
        except errors.BlockDeviceError, err:
3504
          _Fail("Can't change network configuration: %s", err)
3505

    
3506
    if not all_connected:
3507
      raise utils.RetryAgain()
3508

    
3509
  try:
3510
    # Start with a delay of 100 miliseconds and go up to 5 seconds
3511
    utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60)
3512
  except utils.RetryTimeout:
3513
    _Fail("Timeout in disk reconnecting")
3514

    
3515
  if multimaster:
3516
    # change to primary mode
3517
    for rd in bdevs:
3518
      try:
3519
        rd.Open()
3520
      except errors.BlockDeviceError, err:
3521
        _Fail("Can't change to primary mode: %s", err)
3522

    
3523

    
3524
def DrbdWaitSync(nodes_ip, disks):
3525
  """Wait until DRBDs have synchronized.
3526

3527
  """
3528
  def _helper(rd):
3529
    stats = rd.GetProcStatus()
3530
    if not (stats.is_connected or stats.is_in_resync):
3531
      raise utils.RetryAgain()
3532
    return stats
3533

    
3534
  bdevs = _FindDisks(nodes_ip, disks)
3535

    
3536
  min_resync = 100
3537
  alldone = True
3538
  for rd in bdevs:
3539
    try:
3540
      # poll each second for 15 seconds
3541
      stats = utils.Retry(_helper, 1, 15, args=[rd])
3542
    except utils.RetryTimeout:
3543
      stats = rd.GetProcStatus()
3544
      # last check
3545
      if not (stats.is_connected or stats.is_in_resync):
3546
        _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
3547
    alldone = alldone and (not stats.is_in_resync)
3548
    if stats.sync_percent is not None:
3549
      min_resync = min(min_resync, stats.sync_percent)
3550

    
3551
  return (alldone, min_resync)
3552

    
3553

    
3554
def GetDrbdUsermodeHelper():
3555
  """Returns DRBD usermode helper currently configured.
3556

3557
  """
3558
  try:
3559
    return bdev.BaseDRBD.GetUsermodeHelper()
3560
  except errors.BlockDeviceError, err:
3561
    _Fail(str(err))
3562

    
3563

    
3564
def PowercycleNode(hypervisor_type):
3565
  """Hard-powercycle the node.
3566

3567
  Because we need to return first, and schedule the powercycle in the
3568
  background, we won't be able to report failures nicely.
3569

3570
  """
3571
  hyper = hypervisor.GetHypervisor(hypervisor_type)
3572
  try:
3573
    pid = os.fork()
3574
  except OSError:
3575
    # if we can't fork, we'll pretend that we're in the child process
3576
    pid = 0
3577
  if pid > 0:
3578
    return "Reboot scheduled in 5 seconds"
3579
  # ensure the child is running on ram
3580
  try:
3581
    utils.Mlockall()
3582
  except Exception: # pylint: disable=W0703
3583
    pass
3584
  time.sleep(5)
3585
  hyper.PowercycleNode()
3586

    
3587

    
3588
def _VerifyRestrictedCmdName(cmd):
3589
  """Verifies a remote command name.
3590

3591
  @type cmd: string
3592
  @param cmd: Command name
3593
  @rtype: tuple; (boolean, string or None)
3594
  @return: The tuple's first element is the status; if C{False}, the second
3595
    element is an error message string, otherwise it's C{None}
3596

3597
  """
3598
  if not cmd.strip():
3599
    return (False, "Missing command name")
3600

    
3601
  if os.path.basename(cmd) != cmd:
3602
    return (False, "Invalid command name")
3603

    
3604
  if not constants.EXT_PLUGIN_MASK.match(cmd):
3605
    return (False, "Command name contains forbidden characters")
3606

    
3607
  return (True, None)
3608

    
3609

    
3610
def _CommonRestrictedCmdCheck(path, owner):
3611
  """Common checks for remote command file system directories and files.
3612

3613
  @type path: string
3614
  @param path: Path to check
3615
  @param owner: C{None} or tuple containing UID and GID
3616
  @rtype: tuple; (boolean, string or C{os.stat} result)
3617
  @return: The tuple's first element is the status; if C{False}, the second
3618
    element is an error message string, otherwise it's the result of C{os.stat}
3619

3620
  """
3621
  if owner is None:
3622
    # Default to root as owner
3623
    owner = (0, 0)
3624

    
3625
  try:
3626
    st = os.stat(path)
3627
  except EnvironmentError, err:
3628
    return (False, "Can't stat(2) '%s': %s" % (path, err))
3629

    
3630
  if stat.S_IMODE(st.st_mode) & (~_RCMD_MAX_MODE):
3631
    return (False, "Permissions on '%s' are too permissive" % path)
3632

    
3633
  if (st.st_uid, st.st_gid) != owner:
3634
    (owner_uid, owner_gid) = owner
3635
    return (False, "'%s' is not owned by %s:%s" % (path, owner_uid, owner_gid))
3636

    
3637
  return (True, st)
3638

    
3639

    
3640
def _VerifyRestrictedCmdDirectory(path, _owner=None):
3641
  """Verifies remote command directory.
3642

3643
  @type path: string
3644
  @param path: Path to check
3645
  @rtype: tuple; (boolean, string or None)
3646
  @return: The tuple's first element is the status; if C{False}, the second
3647
    element is an error message string, otherwise it's C{None}
3648

3649
  """
3650
  (status, value) = _CommonRestrictedCmdCheck(path, _owner)
3651

    
3652
  if not status:
3653
    return (False, value)
3654

    
3655
  if not stat.S_ISDIR(value.st_mode):
3656
    return (False, "Path '%s' is not a directory" % path)
3657

    
3658
  return (True, None)
3659

    
3660

    
3661
def _VerifyRestrictedCmd(path, cmd, _owner=None):
3662
  """Verifies a whole remote command and returns its executable filename.
3663

3664
  @type path: string
3665
  @param path: Directory containing remote commands
3666
  @type cmd: string
3667
  @param cmd: Command name
3668
  @rtype: tuple; (boolean, string)
3669
  @return: The tuple's first element is the status; if C{False}, the second
3670
    element is an error message string, otherwise the second element is the
3671
    absolute path to the executable
3672

3673
  """
3674
  executable = utils.PathJoin(path, cmd)
3675

    
3676
  (status, msg) = _CommonRestrictedCmdCheck(executable, _owner)
3677

    
3678
  if not status:
3679
    return (False, msg)
3680

    
3681
  if not utils.IsExecutable(executable):
3682
    return (False, "access(2) thinks '%s' can't be executed" % executable)
3683

    
3684
  return (True, executable)
3685

    
3686

    
3687
def _PrepareRestrictedCmd(path, cmd,
3688
                          _verify_dir=_VerifyRestrictedCmdDirectory,
3689
                          _verify_name=_VerifyRestrictedCmdName,
3690
                          _verify_cmd=_VerifyRestrictedCmd):
3691
  """Performs a number of tests on a remote command.
3692

3693
  @type path: string
3694
  @param path: Directory containing remote commands
3695
  @type cmd: string
3696
  @param cmd: Command name
3697
  @return: Same as L{_VerifyRestrictedCmd}
3698

3699
  """
3700
  # Verify the directory first
3701
  (status, msg) = _verify_dir(path)
3702
  if status:
3703
    # Check command if everything was alright
3704
    (status, msg) = _verify_name(cmd)
3705

    
3706
  if not status:
3707
    return (False, msg)
3708

    
3709
  # Check actual executable
3710
  return _verify_cmd(path, cmd)
3711

    
3712

    
3713
def RunRestrictedCmd(cmd,
3714
                     _lock_timeout=_RCMD_LOCK_TIMEOUT,
3715
                     _lock_file=pathutils.RESTRICTED_COMMANDS_LOCK_FILE,
3716
                     _path=pathutils.RESTRICTED_COMMANDS_DIR,
3717
                     _sleep_fn=time.sleep,
3718
                     _prepare_fn=_PrepareRestrictedCmd,
3719
                     _runcmd_fn=utils.RunCmd,
3720
                     _enabled=constants.ENABLE_RESTRICTED_COMMANDS):
3721
  """Executes a remote command after performing strict tests.
3722

3723
  @type cmd: string
3724
  @param cmd: Command name
3725
  @rtype: string
3726
  @return: Command output
3727
  @raise RPCFail: In case of an error
3728

3729
  """
3730
  logging.info("Preparing to run remote command '%s'", cmd)
3731

    
3732
  if not _enabled:
3733
    _Fail("Remote commands disabled at configure time")
3734

    
3735
  lock = None
3736
  try:
3737
    cmdresult = None
3738
    try:
3739
      lock = utils.FileLock.Open(_lock_file)
3740
      lock.Exclusive(blocking=True, timeout=_lock_timeout)
3741

    
3742
      (status, value) = _prepare_fn(_path, cmd)
3743

    
3744
      if status:
3745
        cmdresult = _runcmd_fn([value], env={}, reset_env=True,
3746
                               postfork_fn=lambda _: lock.Unlock())
3747
      else:
3748
        logging.error(value)
3749
    except Exception: # pylint: disable=W0703
3750
      # Keep original error in log
3751
      logging.exception("Caught exception")
3752

    
3753
    if cmdresult is None:
3754
      logging.info("Sleeping for %0.1f seconds before returning",
3755
                   _RCMD_INVALID_DELAY)
3756
      _sleep_fn(_RCMD_INVALID_DELAY)
3757

    
3758
      # Do not include original error message in returned error
3759
      _Fail("Executing command '%s' failed" % cmd)
3760
    elif cmdresult.failed or cmdresult.fail_reason:
3761
      _Fail("Remote command '%s' failed: %s; output: %s",
3762
            cmd, cmdresult.fail_reason, cmdresult.output)
3763
    else:
3764
      return cmdresult.output
3765
  finally:
3766
    if lock is not None:
3767
      # Release lock at last
3768
      lock.Close()
3769
      lock = None
3770

    
3771

    
3772
def SetWatcherPause(until, _filename=pathutils.WATCHER_PAUSEFILE):
3773
  """Creates or removes the watcher pause file.
3774

3775
  @type until: None or number
3776
  @param until: Unix timestamp saying until when the watcher shouldn't run
3777

3778
  """
3779
  if until is None:
3780
    logging.info("Received request to no longer pause watcher")
3781
    utils.RemoveFile(_filename)
3782
  else:
3783
    logging.info("Received request to pause watcher until %s", until)
3784

    
3785
    if not ht.TNumber(until):
3786
      _Fail("Duration must be numeric")
3787

    
3788
    utils.WriteFile(_filename, data="%d\n" % (until, ), mode=0644)
3789

    
3790

    
3791
class HooksRunner(object):
3792
  """Hook runner.
3793

3794
  This class is instantiated on the node side (ganeti-noded) and not
3795
  on the master side.
3796

3797
  """
3798
  def __init__(self, hooks_base_dir=None):
3799
    """Constructor for hooks runner.
3800

3801
    @type hooks_base_dir: str or None
3802
    @param hooks_base_dir: if not None, this overrides the
3803
        L{pathutils.HOOKS_BASE_DIR} (useful for unittests)
3804

3805
    """
3806
    if hooks_base_dir is None:
3807
      hooks_base_dir = pathutils.HOOKS_BASE_DIR
3808
    # yeah, _BASE_DIR is not valid for attributes, we use it like a
3809
    # constant
3810
    self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
3811

    
3812
  def RunLocalHooks(self, node_list, hpath, phase, env):
3813
    """Check that the hooks will be run only locally and then run them.
3814

3815
    """
3816
    assert len(node_list) == 1
3817
    node = node_list[0]
3818
    _, myself = ssconf.GetMasterAndMyself()
3819
    assert node == myself
3820

    
3821
    results = self.RunHooks(hpath, phase, env)
3822

    
3823
    # Return values in the form expected by HooksMaster
3824
    return {node: (None, False, results)}
3825

    
3826
  def RunHooks(self, hpath, phase, env):
3827
    """Run the scripts in the hooks directory.
3828

3829
    @type hpath: str
3830
    @param hpath: the path to the hooks directory which
3831
        holds the scripts
3832
    @type phase: str
3833
    @param phase: either L{constants.HOOKS_PHASE_PRE} or
3834
        L{constants.HOOKS_PHASE_POST}
3835
    @type env: dict
3836
    @param env: dictionary with the environment for the hook
3837
    @rtype: list
3838
    @return: list of 3-element tuples:
3839
      - script path
3840
      - script result, either L{constants.HKR_SUCCESS} or
3841
        L{constants.HKR_FAIL}
3842
      - output of the script
3843

3844
    @raise errors.ProgrammerError: for invalid input
3845
        parameters
3846

3847
    """
3848
    if phase == constants.HOOKS_PHASE_PRE:
3849
      suffix = "pre"
3850
    elif phase == constants.HOOKS_PHASE_POST:
3851
      suffix = "post"
3852
    else:
3853
      _Fail("Unknown hooks phase '%s'", phase)
3854

    
3855
    subdir = "%s-%s.d" % (hpath, suffix)
3856
    dir_name = utils.PathJoin(self._BASE_DIR, subdir)
3857

    
3858
    results = []
3859

    
3860
    if not os.path.isdir(dir_name):
3861
      # for non-existing/non-dirs, we simply exit instead of logging a
3862
      # warning at every operation
3863
      return results
3864

    
3865
    runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
3866

    
3867
    for (relname, relstatus, runresult) in runparts_results:
3868
      if relstatus == constants.RUNPARTS_SKIP:
3869
        rrval = constants.HKR_SKIP
3870
        output = ""
3871
      elif relstatus == constants.RUNPARTS_ERR:
3872
        rrval = constants.HKR_FAIL
3873
        output = "Hook script execution error: %s" % runresult
3874
      elif relstatus == constants.RUNPARTS_RUN:
3875
        if runresult.failed:
3876
          rrval = constants.HKR_FAIL
3877
        else:
3878
          rrval = constants.HKR_SUCCESS
3879
        output = utils.SafeEncode(runresult.output.strip())
3880
      results.append(("%s/%s" % (subdir, relname), rrval, output))
3881

    
3882
    return results
3883

    
3884

    
3885
class IAllocatorRunner(object):
3886
  """IAllocator runner.
3887

3888
  This class is instantiated on the node side (ganeti-noded) and not on
3889
  the master side.
3890

3891
  """
3892
  @staticmethod
3893
  def Run(name, idata):
3894
    """Run an iallocator script.
3895

3896
    @type name: str
3897
    @param name: the iallocator script name
3898
    @type idata: str
3899
    @param idata: the allocator input data
3900

3901
    @rtype: tuple
3902
    @return: two element tuple of:
3903
       - status
3904
       - either error message or stdout of allocator (for success)
3905

3906
    """
3907
    alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
3908
                                  os.path.isfile)
3909
    if alloc_script is None:
3910
      _Fail("iallocator module '%s' not found in the search path", name)
3911

    
3912
    fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
3913
    try:
3914
      os.write(fd, idata)
3915
      os.close(fd)
3916
      result = utils.RunCmd([alloc_script, fin_name])
3917
      if result.failed:
3918
        _Fail("iallocator module '%s' failed: %s, output '%s'",
3919
              name, result.fail_reason, result.output)
3920
    finally:
3921
      os.unlink(fin_name)
3922

    
3923
    return result.stdout
3924

    
3925

    
3926
class DevCacheManager(object):
3927
  """Simple class for managing a cache of block device information.
3928

3929
  """
3930
  _DEV_PREFIX = "/dev/"
3931
  _ROOT_DIR = pathutils.BDEV_CACHE_DIR
3932

    
3933
  @classmethod
3934
  def _ConvertPath(cls, dev_path):
3935
    """Converts a /dev/name path to the cache file name.
3936

3937
    This replaces slashes with underscores and strips the /dev
3938
    prefix. It then returns the full path to the cache file.
3939

3940
    @type dev_path: str
3941
    @param dev_path: the C{/dev/} path name
3942
    @rtype: str
3943
    @return: the converted path name
3944

3945
    """
3946
    if dev_path.startswith(cls._DEV_PREFIX):
3947
      dev_path = dev_path[len(cls._DEV_PREFIX):]
3948
    dev_path = dev_path.replace("/", "_")
3949
    fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
3950
    return fpath
3951

    
3952
  @classmethod
3953
  def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
3954
    """Updates the cache information for a given device.
3955

3956
    @type dev_path: str
3957
    @param dev_path: the pathname of the device
3958
    @type owner: str
3959
    @param owner: the owner (instance name) of the device
3960
    @type on_primary: bool
3961
    @param on_primary: whether this is the primary
3962
        node nor not
3963
    @type iv_name: str
3964
    @param iv_name: the instance-visible name of the
3965
        device, as in objects.Disk.iv_name
3966

3967
    @rtype: None
3968

3969
    """
3970
    if dev_path is None:
3971
      logging.error("DevCacheManager.UpdateCache got a None dev_path")
3972
      return
3973
    fpath = cls._ConvertPath(dev_path)
3974
    if on_primary:
3975
      state = "primary"
3976
    else:
3977
      state = "secondary"
3978
    if iv_name is None:
3979
      iv_name = "not_visible"
3980
    fdata = "%s %s %s\n" % (str(owner), state, iv_name)
3981
    try:
3982
      utils.WriteFile(fpath, data=fdata)
3983
    except EnvironmentError, err:
3984
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
3985

    
3986
  @classmethod
3987
  def RemoveCache(cls, dev_path):
3988
    """Remove data for a dev_path.
3989

3990
    This is just a wrapper over L{utils.io.RemoveFile} with a converted
3991
    path name and logging.
3992

3993
    @type dev_path: str
3994
    @param dev_path: the pathname of the device
3995

3996
    @rtype: None
3997

3998
    """
3999
    if dev_path is None:
4000
      logging.error("DevCacheManager.RemoveCache got a None dev_path")
4001
      return
4002
    fpath = cls._ConvertPath(dev_path)
4003
    try:
4004
      utils.RemoveFile(fpath)
4005
    except EnvironmentError, err:
4006
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)