Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ c89622cd

History | View | Annotate | Download (148 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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,C0302
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
# C0302: This module has become too big and should be split up
38

    
39

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

    
55
from ganeti import errors
56
from ganeti import http
57
from ganeti import utils
58
from ganeti import ssh
59
from ganeti import hypervisor
60
from ganeti import constants
61
from ganeti.storage import bdev
62
from ganeti.storage import drbd
63
from ganeti.storage import filestorage
64
from ganeti import objects
65
from ganeti import ssconf
66
from ganeti import serializer
67
from ganeti import netutils
68
from ganeti import runtime
69
from ganeti import compat
70
from ganeti import pathutils
71
from ganeti import vcluster
72
from ganeti import ht
73
from ganeti.storage.base import BlockDev
74
from ganeti.storage.drbd import DRBD8
75
from ganeti import hooksmaster
76

    
77

    
78
_BOOT_ID_PATH = "/proc/sys/kernel/random/boot_id"
79
_ALLOWED_CLEAN_DIRS = compat.UniqueFrozenset([
80
  pathutils.DATA_DIR,
81
  pathutils.JOB_QUEUE_ARCHIVE_DIR,
82
  pathutils.QUEUE_DIR,
83
  pathutils.CRYPTO_KEYS_DIR,
84
  ])
85
_MAX_SSL_CERT_VALIDITY = 7 * 24 * 60 * 60
86
_X509_KEY_FILE = "key"
87
_X509_CERT_FILE = "cert"
88
_IES_STATUS_FILE = "status"
89
_IES_PID_FILE = "pid"
90
_IES_CA_FILE = "ca"
91

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

    
95
# Actions for the master setup script
96
_MASTER_START = "start"
97
_MASTER_STOP = "stop"
98

    
99
#: Maximum file permissions for restricted command directory and executables
100
_RCMD_MAX_MODE = (stat.S_IRWXU |
101
                  stat.S_IRGRP | stat.S_IXGRP |
102
                  stat.S_IROTH | stat.S_IXOTH)
103

    
104
#: Delay before returning an error for restricted commands
105
_RCMD_INVALID_DELAY = 10
106

    
107
#: How long to wait to acquire lock for restricted commands (shorter than
108
#: L{_RCMD_INVALID_DELAY}) to reduce blockage of noded forks when many
109
#: command requests arrive
110
_RCMD_LOCK_TIMEOUT = _RCMD_INVALID_DELAY * 0.8
111

    
112

    
113
class RPCFail(Exception):
114
  """Class denoting RPC failure.
115

116
  Its argument is the error message.
117

118
  """
119

    
120

    
121
def _GetInstReasonFilename(instance_name):
122
  """Path of the file containing the reason of the instance status change.
123

124
  @type instance_name: string
125
  @param instance_name: The name of the instance
126
  @rtype: string
127
  @return: The path of the file
128

129
  """
130
  return utils.PathJoin(pathutils.INSTANCE_REASON_DIR, instance_name)
131

    
132

    
133
def _StoreInstReasonTrail(instance_name, trail):
134
  """Serialize a reason trail related to an instance change of state to file.
135

136
  The exact location of the file depends on the name of the instance and on
137
  the configuration of the Ganeti cluster defined at deploy time.
138

139
  @type instance_name: string
140
  @param instance_name: The name of the instance
141

142
  @type trail: list of reasons
143
  @param trail: reason trail
144

145
  @rtype: None
146

147
  """
148
  json = serializer.DumpJson(trail)
149
  filename = _GetInstReasonFilename(instance_name)
150
  utils.WriteFile(filename, data=json)
151

    
152

    
153
def _Fail(msg, *args, **kwargs):
154
  """Log an error and the raise an RPCFail exception.
155

156
  This exception is then handled specially in the ganeti daemon and
157
  turned into a 'failed' return type. As such, this function is a
158
  useful shortcut for logging the error and returning it to the master
159
  daemon.
160

161
  @type msg: string
162
  @param msg: the text of the exception
163
  @raise RPCFail
164

165
  """
166
  if args:
167
    msg = msg % args
168
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
169
    if "exc" in kwargs and kwargs["exc"]:
170
      logging.exception(msg)
171
    else:
172
      logging.error(msg)
173
  raise RPCFail(msg)
174

    
175

    
176
def _GetConfig():
177
  """Simple wrapper to return a SimpleStore.
178

179
  @rtype: L{ssconf.SimpleStore}
180
  @return: a SimpleStore instance
181

182
  """
183
  return ssconf.SimpleStore()
184

    
185

    
186
def _GetSshRunner(cluster_name):
187
  """Simple wrapper to return an SshRunner.
188

189
  @type cluster_name: str
190
  @param cluster_name: the cluster name, which is needed
191
      by the SshRunner constructor
192
  @rtype: L{ssh.SshRunner}
193
  @return: an SshRunner instance
194

195
  """
196
  return ssh.SshRunner(cluster_name)
197

    
198

    
199
def _Decompress(data):
200
  """Unpacks data compressed by the RPC client.
201

202
  @type data: list or tuple
203
  @param data: Data sent by RPC client
204
  @rtype: str
205
  @return: Decompressed data
206

207
  """
208
  assert isinstance(data, (list, tuple))
209
  assert len(data) == 2
210
  (encoding, content) = data
211
  if encoding == constants.RPC_ENCODING_NONE:
212
    return content
213
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
214
    return zlib.decompress(base64.b64decode(content))
215
  else:
216
    raise AssertionError("Unknown data encoding")
217

    
218

    
219
def _CleanDirectory(path, exclude=None):
220
  """Removes all regular files in a directory.
221

222
  @type path: str
223
  @param path: the directory to clean
224
  @type exclude: list
225
  @param exclude: list of files to be excluded, defaults
226
      to the empty list
227

228
  """
229
  if path not in _ALLOWED_CLEAN_DIRS:
230
    _Fail("Path passed to _CleanDirectory not in allowed clean targets: '%s'",
231
          path)
232

    
233
  if not os.path.isdir(path):
234
    return
235
  if exclude is None:
236
    exclude = []
237
  else:
238
    # Normalize excluded paths
239
    exclude = [os.path.normpath(i) for i in exclude]
240

    
241
  for rel_name in utils.ListVisibleFiles(path):
242
    full_name = utils.PathJoin(path, rel_name)
243
    if full_name in exclude:
244
      continue
245
    if os.path.isfile(full_name) and not os.path.islink(full_name):
246
      utils.RemoveFile(full_name)
247

    
248

    
249
def _BuildUploadFileList():
250
  """Build the list of allowed upload files.
251

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

254
  """
255
  allowed_files = set([
256
    pathutils.CLUSTER_CONF_FILE,
257
    pathutils.ETC_HOSTS,
258
    pathutils.SSH_KNOWN_HOSTS_FILE,
259
    pathutils.VNC_PASSWORD_FILE,
260
    pathutils.RAPI_CERT_FILE,
261
    pathutils.SPICE_CERT_FILE,
262
    pathutils.SPICE_CACERT_FILE,
263
    pathutils.RAPI_USERS_FILE,
264
    pathutils.CONFD_HMAC_KEY,
265
    pathutils.CLUSTER_DOMAIN_SECRET_FILE,
266
    ])
267

    
268
  for hv_name in constants.HYPER_TYPES:
269
    hv_class = hypervisor.GetHypervisorClass(hv_name)
270
    allowed_files.update(hv_class.GetAncillaryFiles()[0])
271

    
272
  assert pathutils.FILE_STORAGE_PATHS_FILE not in allowed_files, \
273
    "Allowed file storage paths should never be uploaded via RPC"
274

    
275
  return frozenset(allowed_files)
276

    
277

    
278
_ALLOWED_UPLOAD_FILES = _BuildUploadFileList()
279

    
280

    
281
def JobQueuePurge():
282
  """Removes job queue files and archived jobs.
283

284
  @rtype: tuple
285
  @return: True, None
286

287
  """
288
  _CleanDirectory(pathutils.QUEUE_DIR, exclude=[pathutils.JOB_QUEUE_LOCK_FILE])
289
  _CleanDirectory(pathutils.JOB_QUEUE_ARCHIVE_DIR)
290

    
291

    
292
def GetMasterNodeName():
293
  """Returns the master node name.
294

295
  @rtype: string
296
  @return: name of the master node
297
  @raise RPCFail: in case of errors
298

299
  """
300
  try:
301
    return _GetConfig().GetMasterNode()
302
  except errors.ConfigurationError, err:
303
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
304

    
305

    
306
def RunLocalHooks(hook_opcode, hooks_path, env_builder_fn):
307
  """Decorator that runs hooks before and after the decorated function.
308

309
  @type hook_opcode: string
310
  @param hook_opcode: opcode of the hook
311
  @type hooks_path: string
312
  @param hooks_path: path of the hooks
313
  @type env_builder_fn: function
314
  @param env_builder_fn: function that returns a dictionary containing the
315
    environment variables for the hooks. Will get all the parameters of the
316
    decorated function.
317
  @raise RPCFail: in case of pre-hook failure
318

319
  """
320
  def decorator(fn):
321
    def wrapper(*args, **kwargs):
322
      _, myself = ssconf.GetMasterAndMyself()
323
      nodes = ([myself], [myself])  # these hooks run locally
324

    
325
      env_fn = compat.partial(env_builder_fn, *args, **kwargs)
326

    
327
      cfg = _GetConfig()
328
      hr = HooksRunner()
329
      hm = hooksmaster.HooksMaster(hook_opcode, hooks_path, nodes,
330
                                   hr.RunLocalHooks, None, env_fn, None,
331
                                   logging.warning, cfg.GetClusterName(),
332
                                   cfg.GetMasterNode())
333
      hm.RunPhase(constants.HOOKS_PHASE_PRE)
334
      result = fn(*args, **kwargs)
335
      hm.RunPhase(constants.HOOKS_PHASE_POST)
336

    
337
      return result
338
    return wrapper
339
  return decorator
340

    
341

    
342
def _BuildMasterIpEnv(master_params, use_external_mip_script=None):
343
  """Builds environment variables for master IP hooks.
344

345
  @type master_params: L{objects.MasterNetworkParameters}
346
  @param master_params: network parameters of the master
347
  @type use_external_mip_script: boolean
348
  @param use_external_mip_script: whether to use an external master IP
349
    address setup script (unused, but necessary per the implementation of the
350
    _RunLocalHooks decorator)
351

352
  """
353
  # pylint: disable=W0613
354
  ver = netutils.IPAddress.GetVersionFromAddressFamily(master_params.ip_family)
355
  env = {
356
    "MASTER_NETDEV": master_params.netdev,
357
    "MASTER_IP": master_params.ip,
358
    "MASTER_NETMASK": str(master_params.netmask),
359
    "CLUSTER_IP_VERSION": str(ver),
360
  }
361

    
362
  return env
363

    
364

    
365
def _RunMasterSetupScript(master_params, action, use_external_mip_script):
366
  """Execute the master IP address setup script.
367

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

379
  """
380
  env = _BuildMasterIpEnv(master_params)
381

    
382
  if use_external_mip_script:
383
    setup_script = pathutils.EXTERNAL_MASTER_SETUP_SCRIPT
384
  else:
385
    setup_script = pathutils.DEFAULT_MASTER_SETUP_SCRIPT
386

    
387
  result = utils.RunCmd([setup_script, action], env=env, reset_env=True)
388

    
389
  if result.failed:
390
    _Fail("Failed to %s the master IP. Script return value: %s, output: '%s'" %
391
          (action, result.exit_code, result.output), log=True)
392

    
393

    
394
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNUP, "master-ip-turnup",
395
               _BuildMasterIpEnv)
396
def ActivateMasterIp(master_params, use_external_mip_script):
397
  """Activate the IP address of the master daemon.
398

399
  @type master_params: L{objects.MasterNetworkParameters}
400
  @param master_params: network parameters of the master
401
  @type use_external_mip_script: boolean
402
  @param use_external_mip_script: whether to use an external master IP
403
    address setup script
404
  @raise RPCFail: in case of errors during the IP startup
405

406
  """
407
  _RunMasterSetupScript(master_params, _MASTER_START,
408
                        use_external_mip_script)
409

    
410

    
411
def StartMasterDaemons(no_voting):
412
  """Activate local node as master node.
413

414
  The function will start the master daemons (ganeti-masterd and ganeti-rapi).
415

416
  @type no_voting: boolean
417
  @param no_voting: whether to start ganeti-masterd without a node vote
418
      but still non-interactively
419
  @rtype: None
420

421
  """
422

    
423
  if no_voting:
424
    masterd_args = "--no-voting --yes-do-it"
425
  else:
426
    masterd_args = ""
427

    
428
  env = {
429
    "EXTRA_MASTERD_ARGS": masterd_args,
430
    }
431

    
432
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "start-master"], env=env)
433
  if result.failed:
434
    msg = "Can't start Ganeti master: %s" % result.output
435
    logging.error(msg)
436
    _Fail(msg)
437

    
438

    
439
@RunLocalHooks(constants.FAKE_OP_MASTER_TURNDOWN, "master-ip-turndown",
440
               _BuildMasterIpEnv)
441
def DeactivateMasterIp(master_params, use_external_mip_script):
442
  """Deactivate the master IP on this node.
443

444
  @type master_params: L{objects.MasterNetworkParameters}
445
  @param master_params: network parameters of the master
446
  @type use_external_mip_script: boolean
447
  @param use_external_mip_script: whether to use an external master IP
448
    address setup script
449
  @raise RPCFail: in case of errors during the IP turndown
450

451
  """
452
  _RunMasterSetupScript(master_params, _MASTER_STOP,
453
                        use_external_mip_script)
454

    
455

    
456
def StopMasterDaemons():
457
  """Stop the master daemons on this node.
458

459
  Stop the master daemons (ganeti-masterd and ganeti-rapi) on this node.
460

461
  @rtype: None
462

463
  """
464
  # TODO: log and report back to the caller the error failures; we
465
  # need to decide in which case we fail the RPC for this
466

    
467
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-master"])
468
  if result.failed:
469
    logging.error("Could not stop Ganeti master, command %s had exitcode %s"
470
                  " and error %s",
471
                  result.cmd, result.exit_code, result.output)
472

    
473

    
474
def ChangeMasterNetmask(old_netmask, netmask, master_ip, master_netdev):
475
  """Change the netmask of the master IP.
476

477
  @param old_netmask: the old value of the netmask
478
  @param netmask: the new value of the netmask
479
  @param master_ip: the master IP
480
  @param master_netdev: the master network device
481

482
  """
483
  if old_netmask == netmask:
484
    return
485

    
486
  if not netutils.IPAddress.Own(master_ip):
487
    _Fail("The master IP address is not up, not attempting to change its"
488
          " netmask")
489

    
490
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "add",
491
                         "%s/%s" % (master_ip, netmask),
492
                         "dev", master_netdev, "label",
493
                         "%s:0" % master_netdev])
494
  if result.failed:
495
    _Fail("Could not set the new netmask on the master IP address")
496

    
497
  result = utils.RunCmd([constants.IP_COMMAND_PATH, "address", "del",
498
                         "%s/%s" % (master_ip, old_netmask),
499
                         "dev", master_netdev, "label",
500
                         "%s:0" % master_netdev])
501
  if result.failed:
502
    _Fail("Could not bring down the master IP address with the old netmask")
503

    
504

    
505
def EtcHostsModify(mode, host, ip):
506
  """Modify a host entry in /etc/hosts.
507

508
  @param mode: The mode to operate. Either add or remove entry
509
  @param host: The host to operate on
510
  @param ip: The ip associated with the entry
511

512
  """
513
  if mode == constants.ETC_HOSTS_ADD:
514
    if not ip:
515
      RPCFail("Mode 'add' needs 'ip' parameter, but parameter not"
516
              " present")
517
    utils.AddHostToEtcHosts(host, ip)
518
  elif mode == constants.ETC_HOSTS_REMOVE:
519
    if ip:
520
      RPCFail("Mode 'remove' does not allow 'ip' parameter, but"
521
              " parameter is present")
522
    utils.RemoveHostFromEtcHosts(host)
523
  else:
524
    RPCFail("Mode not supported")
525

    
526

    
527
def LeaveCluster(modify_ssh_setup):
528
  """Cleans up and remove the current node.
529

530
  This function cleans up and prepares the current node to be removed
531
  from the cluster.
532

533
  If processing is successful, then it raises an
534
  L{errors.QuitGanetiException} which is used as a special case to
535
  shutdown the node daemon.
536

537
  @param modify_ssh_setup: boolean
538

539
  """
540
  _CleanDirectory(pathutils.DATA_DIR)
541
  _CleanDirectory(pathutils.CRYPTO_KEYS_DIR)
542
  JobQueuePurge()
543

    
544
  if modify_ssh_setup:
545
    try:
546
      priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.SSH_LOGIN_USER)
547

    
548
      utils.RemoveAuthorizedKey(auth_keys, utils.ReadFile(pub_key))
549

    
550
      utils.RemoveFile(priv_key)
551
      utils.RemoveFile(pub_key)
552
    except errors.OpExecError:
553
      logging.exception("Error while processing ssh files")
554

    
555
  try:
556
    utils.RemoveFile(pathutils.CONFD_HMAC_KEY)
557
    utils.RemoveFile(pathutils.RAPI_CERT_FILE)
558
    utils.RemoveFile(pathutils.SPICE_CERT_FILE)
559
    utils.RemoveFile(pathutils.SPICE_CACERT_FILE)
560
    utils.RemoveFile(pathutils.NODED_CERT_FILE)
561
  except: # pylint: disable=W0702
562
    logging.exception("Error while removing cluster secrets")
563

    
564
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop", constants.CONFD])
565
  if result.failed:
566
    logging.error("Command %s failed with exitcode %s and error %s",
567
                  result.cmd, result.exit_code, result.output)
568

    
569
  # Raise a custom exception (handled in ganeti-noded)
570
  raise errors.QuitGanetiException(True, "Shutdown scheduled")
571

    
572

    
573
def _CheckStorageParams(params, num_params):
574
  """Performs sanity checks for storage parameters.
575

576
  @type params: list
577
  @param params: list of storage parameters
578
  @type num_params: int
579
  @param num_params: expected number of parameters
580

581
  """
582
  if params is None:
583
    raise errors.ProgrammerError("No storage parameters for storage"
584
                                 " reporting is provided.")
585
  if not isinstance(params, list):
586
    raise errors.ProgrammerError("The storage parameters are not of type"
587
                                 " list: '%s'" % params)
588
  if not len(params) == num_params:
589
    raise errors.ProgrammerError("Did not receive the expected number of"
590
                                 "storage parameters: expected %s,"
591
                                 " received '%s'" % (num_params, len(params)))
592

    
593

    
594
def _CheckLvmStorageParams(params):
595
  """Performs sanity check for the 'exclusive storage' flag.
596

597
  @see: C{_CheckStorageParams}
598

599
  """
600
  _CheckStorageParams(params, 1)
601
  excl_stor = params[0]
602
  if not isinstance(params[0], bool):
603
    raise errors.ProgrammerError("Exclusive storage parameter is not"
604
                                 " boolean: '%s'." % excl_stor)
605
  return excl_stor
606

    
607

    
608
def _GetLvmVgSpaceInfo(name, params):
609
  """Wrapper around C{_GetVgInfo} which checks the storage parameters.
610

611
  @type name: string
612
  @param name: name of the volume group
613
  @type params: list
614
  @param params: list of storage parameters, which in this case should be
615
    containing only one for exclusive storage
616

617
  """
618
  excl_stor = _CheckLvmStorageParams(params)
619
  return _GetVgInfo(name, excl_stor)
620

    
621

    
622
def _GetVgInfo(
623
    name, excl_stor, info_fn=bdev.LogicalVolume.GetVGInfo):
624
  """Retrieves information about a LVM volume group.
625

626
  """
627
  # TODO: GetVGInfo supports returning information for multiple VGs at once
628
  vginfo = info_fn([name], excl_stor)
629
  if vginfo:
630
    vg_free = int(round(vginfo[0][0], 0))
631
    vg_size = int(round(vginfo[0][1], 0))
632
  else:
633
    vg_free = None
634
    vg_size = None
635

    
636
  return {
637
    "type": constants.ST_LVM_VG,
638
    "name": name,
639
    "storage_free": vg_free,
640
    "storage_size": vg_size,
641
    }
642

    
643

    
644
def _GetLvmPvSpaceInfo(name, params):
645
  """Wrapper around C{_GetVgSpindlesInfo} with sanity checks.
646

647
  @see: C{_GetLvmVgSpaceInfo}
648

649
  """
650
  excl_stor = _CheckLvmStorageParams(params)
651
  return _GetVgSpindlesInfo(name, excl_stor)
652

    
653

    
654
def _GetVgSpindlesInfo(
655
    name, excl_stor, info_fn=bdev.LogicalVolume.GetVgSpindlesInfo):
656
  """Retrieves information about spindles in an LVM volume group.
657

658
  @type name: string
659
  @param name: VG name
660
  @type excl_stor: bool
661
  @param excl_stor: exclusive storage
662
  @rtype: dict
663
  @return: dictionary whose keys are "name", "vg_free", "vg_size" for VG name,
664
      free spindles, total spindles respectively
665

666
  """
667
  if excl_stor:
668
    (vg_free, vg_size) = info_fn(name)
669
  else:
670
    vg_free = 0
671
    vg_size = 0
672
  return {
673
    "type": constants.ST_LVM_PV,
674
    "name": name,
675
    "storage_free": vg_free,
676
    "storage_size": vg_size,
677
    }
678

    
679

    
680
def _GetHvInfo(name, hvparams, get_hv_fn=hypervisor.GetHypervisor):
681
  """Retrieves node information from a hypervisor.
682

683
  The information returned depends on the hypervisor. Common items:
684

685
    - vg_size is the size of the configured volume group in MiB
686
    - vg_free is the free size of the volume group in MiB
687
    - memory_dom0 is the memory allocated for domain0 in MiB
688
    - memory_free is the currently available (free) ram in MiB
689
    - memory_total is the total number of ram in MiB
690
    - hv_version: the hypervisor version, if available
691

692
  @type hvparams: dict of string
693
  @param hvparams: the hypervisor's hvparams
694

695
  """
696
  return get_hv_fn(name).GetNodeInfo(hvparams=hvparams)
697

    
698

    
699
def _GetHvInfoAll(hv_specs, get_hv_fn=hypervisor.GetHypervisor):
700
  """Retrieves node information for all hypervisors.
701

702
  See C{_GetHvInfo} for information on the output.
703

704
  @type hv_specs: list of pairs (string, dict of strings)
705
  @param hv_specs: list of pairs of a hypervisor's name and its hvparams
706

707
  """
708
  if hv_specs is None:
709
    return None
710

    
711
  result = []
712
  for hvname, hvparams in hv_specs:
713
    result.append(_GetHvInfo(hvname, hvparams, get_hv_fn))
714
  return result
715

    
716

    
717
def _GetNamedNodeInfo(names, fn):
718
  """Calls C{fn} for all names in C{names} and returns a dictionary.
719

720
  @rtype: None or dict
721

722
  """
723
  if names is None:
724
    return None
725
  else:
726
    return map(fn, names)
727

    
728

    
729
def GetNodeInfo(storage_units, hv_specs):
730
  """Gives back a hash with different information about the node.
731

732
  @type storage_units: list of tuples (string, string, list)
733
  @param storage_units: List of tuples (storage unit, identifier, parameters) to
734
    ask for disk space information. In case of lvm-vg, the identifier is
735
    the VG name. The parameters can contain additional, storage-type-specific
736
    parameters, for example exclusive storage for lvm storage.
737
  @type hv_specs: list of pairs (string, dict of strings)
738
  @param hv_specs: list of pairs of a hypervisor's name and its hvparams
739
  @rtype: tuple; (string, None/dict, None/dict)
740
  @return: Tuple containing boot ID, volume group information and hypervisor
741
    information
742

743
  """
744
  bootid = utils.ReadFile(_BOOT_ID_PATH, size=128).rstrip("\n")
745
  storage_info = _GetNamedNodeInfo(
746
    storage_units,
747
    (lambda (storage_type, storage_key, storage_params):
748
        _ApplyStorageInfoFunction(storage_type, storage_key, storage_params)))
749
  hv_info = _GetHvInfoAll(hv_specs)
750
  return (bootid, storage_info, hv_info)
751

    
752

    
753
def _GetFileStorageSpaceInfo(path, params):
754
  """Wrapper around filestorage.GetSpaceInfo.
755

756
  The purpose of this wrapper is to call filestorage.GetFileStorageSpaceInfo
757
  and ignore the *args parameter to not leak it into the filestorage
758
  module's code.
759

760
  @see: C{filestorage.GetFileStorageSpaceInfo} for description of the
761
    parameters.
762

763
  """
764
  _CheckStorageParams(params, 0)
765
  return filestorage.GetFileStorageSpaceInfo(path)
766

    
767

    
768
# FIXME: implement storage reporting for all missing storage types.
769
_STORAGE_TYPE_INFO_FN = {
770
  constants.ST_BLOCK: None,
771
  constants.ST_DISKLESS: None,
772
  constants.ST_EXT: None,
773
  constants.ST_FILE: _GetFileStorageSpaceInfo,
774
  constants.ST_LVM_PV: _GetLvmPvSpaceInfo,
775
  constants.ST_LVM_VG: _GetLvmVgSpaceInfo,
776
  constants.ST_SHARED_FILE: None,
777
  constants.ST_RADOS: None,
778
}
779

    
780

    
781
def _ApplyStorageInfoFunction(storage_type, storage_key, *args):
782
  """Looks up and applies the correct function to calculate free and total
783
  storage for the given storage type.
784

785
  @type storage_type: string
786
  @param storage_type: the storage type for which the storage shall be reported.
787
  @type storage_key: string
788
  @param storage_key: identifier of a storage unit, e.g. the volume group name
789
    of an LVM storage unit
790
  @type args: any
791
  @param args: various parameters that can be used for storage reporting. These
792
    parameters and their semantics vary from storage type to storage type and
793
    are just propagated in this function.
794
  @return: the results of the application of the storage space function (see
795
    _STORAGE_TYPE_INFO_FN) if storage space reporting is implemented for that
796
    storage type
797
  @raises NotImplementedError: for storage types who don't support space
798
    reporting yet
799
  """
800
  fn = _STORAGE_TYPE_INFO_FN[storage_type]
801
  if fn is not None:
802
    return fn(storage_key, *args)
803
  else:
804
    raise NotImplementedError
805

    
806

    
807
def _CheckExclusivePvs(pvi_list):
808
  """Check that PVs are not shared among LVs
809

810
  @type pvi_list: list of L{objects.LvmPvInfo} objects
811
  @param pvi_list: information about the PVs
812

813
  @rtype: list of tuples (string, list of strings)
814
  @return: offending volumes, as tuples: (pv_name, [lv1_name, lv2_name...])
815

816
  """
817
  res = []
818
  for pvi in pvi_list:
819
    if len(pvi.lv_list) > 1:
820
      res.append((pvi.name, pvi.lv_list))
821
  return res
822

    
823

    
824
def _VerifyHypervisors(what, vm_capable, result, all_hvparams,
825
                       get_hv_fn=hypervisor.GetHypervisor):
826
  """Verifies the hypervisor. Appends the results to the 'results' list.
827

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

840
  """
841
  if not vm_capable:
842
    return
843

    
844
  if constants.NV_HYPERVISOR in what:
845
    result[constants.NV_HYPERVISOR] = {}
846
    for hv_name in what[constants.NV_HYPERVISOR]:
847
      hvparams = all_hvparams[hv_name]
848
      try:
849
        val = get_hv_fn(hv_name).Verify(hvparams=hvparams)
850
      except errors.HypervisorError, err:
851
        val = "Error while checking hypervisor: %s" % str(err)
852
      result[constants.NV_HYPERVISOR][hv_name] = val
853

    
854

    
855
def _VerifyHvparams(what, vm_capable, result,
856
                    get_hv_fn=hypervisor.GetHypervisor):
857
  """Verifies the hvparams. Appends the results to the 'results' list.
858

859
  @type what: C{dict}
860
  @param what: a dictionary of things to check
861
  @type vm_capable: boolean
862
  @param vm_capable: whether or not this node is vm capable
863
  @type result: dict
864
  @param result: dictionary of verification results; results of the
865
    verifications in this function will be added here
866
  @type get_hv_fn: function
867
  @param get_hv_fn: function to retrieve the hypervisor, to improve testability
868

869
  """
870
  if not vm_capable:
871
    return
872

    
873
  if constants.NV_HVPARAMS in what:
874
    result[constants.NV_HVPARAMS] = []
875
    for source, hv_name, hvparms in what[constants.NV_HVPARAMS]:
876
      try:
877
        logging.info("Validating hv %s, %s", hv_name, hvparms)
878
        get_hv_fn(hv_name).ValidateParameters(hvparms)
879
      except errors.HypervisorError, err:
880
        result[constants.NV_HVPARAMS].append((source, hv_name, str(err)))
881

    
882

    
883
def _VerifyInstanceList(what, vm_capable, result, all_hvparams):
884
  """Verifies the instance list.
885

886
  @type what: C{dict}
887
  @param what: a dictionary of things to check
888
  @type vm_capable: boolean
889
  @param vm_capable: whether or not this node is vm capable
890
  @type result: dict
891
  @param result: dictionary of verification results; results of the
892
    verifications in this function will be added here
893
  @type all_hvparams: dict of dict of string
894
  @param all_hvparams: dictionary mapping hypervisor names to hvparams
895

896
  """
897
  if constants.NV_INSTANCELIST in what and vm_capable:
898
    # GetInstanceList can fail
899
    try:
900
      val = GetInstanceList(what[constants.NV_INSTANCELIST],
901
                            all_hvparams=all_hvparams)
902
    except RPCFail, err:
903
      val = str(err)
904
    result[constants.NV_INSTANCELIST] = val
905

    
906

    
907
def _VerifyNodeInfo(what, vm_capable, result, all_hvparams):
908
  """Verifies the node info.
909

910
  @type what: C{dict}
911
  @param what: a dictionary of things to check
912
  @type vm_capable: boolean
913
  @param vm_capable: whether or not this node is vm capable
914
  @type result: dict
915
  @param result: dictionary of verification results; results of the
916
    verifications in this function will be added here
917
  @type all_hvparams: dict of dict of string
918
  @param all_hvparams: dictionary mapping hypervisor names to hvparams
919

920
  """
921
  if constants.NV_HVINFO in what and vm_capable:
922
    hvname = what[constants.NV_HVINFO]
923
    hyper = hypervisor.GetHypervisor(hvname)
924
    hvparams = all_hvparams[hvname]
925
    result[constants.NV_HVINFO] = hyper.GetNodeInfo(hvparams=hvparams)
926

    
927

    
928
def _VerifyClientCertificate(cert_file=pathutils.NODED_CLIENT_CERT_FILE):
929
  """Verify the existance and validity of the client SSL certificate.
930

931
  """
932
  create_cert_cmd = "gnt-cluster renew-crypto --new-node-certificates"
933
  if not os.path.exists(cert_file):
934
    return (constants.CV_ERROR,
935
            "The client certificate does not exist. Run '%s' to create"
936
            " client certificates for all nodes." % create_cert_cmd)
937

    
938
  (errcode, msg) = utils.VerifyCertificate(cert_file)
939
  if errcode is not None:
940
    return (errcode, msg)
941
  else:
942
    # if everything is fine, we return the digest to be compared to the config
943
    return (None, utils.GetCertificateDigest(cert_filename=cert_file))
944

    
945

    
946
def VerifyNode(what, cluster_name, all_hvparams, node_groups, groups_cfg):
947
  """Verify the status of the local node.
948

949
  Based on the input L{what} parameter, various checks are done on the
950
  local node.
951

952
  If the I{filelist} key is present, this list of
953
  files is checksummed and the file/checksum pairs are returned.
954

955
  If the I{nodelist} key is present, we check that we have
956
  connectivity via ssh with the target nodes (and check the hostname
957
  report).
958

959
  If the I{node-net-test} key is present, we check that we have
960
  connectivity to the given nodes via both primary IP and, if
961
  applicable, secondary IPs.
962

963
  @type what: C{dict}
964
  @param what: a dictionary of things to check:
965
      - filelist: list of files for which to compute checksums
966
      - nodelist: list of nodes we should check ssh communication with
967
      - node-net-test: list of nodes we should check node daemon port
968
        connectivity with
969
      - hypervisor: list with hypervisors to run the verify for
970
  @type cluster_name: string
971
  @param cluster_name: the cluster's name
972
  @type all_hvparams: dict of dict of strings
973
  @param all_hvparams: a dictionary mapping hypervisor names to hvparams
974
  @type node_groups: a dict of strings
975
  @param node_groups: node _names_ mapped to their group uuids (it's enough to
976
      have only those nodes that are in `what["nodelist"]`)
977
  @type groups_cfg: a dict of dict of strings
978
  @param groups_cfg: a dictionary mapping group uuids to their configuration
979
  @rtype: dict
980
  @return: a dictionary with the same keys as the input dict, and
981
      values representing the result of the checks
982

983
  """
984
  result = {}
985
  my_name = netutils.Hostname.GetSysName()
986
  port = netutils.GetDaemonPort(constants.NODED)
987
  vm_capable = my_name not in what.get(constants.NV_VMNODES, [])
988

    
989
  _VerifyHypervisors(what, vm_capable, result, all_hvparams)
990
  _VerifyHvparams(what, vm_capable, result)
991

    
992
  if constants.NV_FILELIST in what:
993
    fingerprints = utils.FingerprintFiles(map(vcluster.LocalizeVirtualPath,
994
                                              what[constants.NV_FILELIST]))
995
    result[constants.NV_FILELIST] = \
996
      dict((vcluster.MakeVirtualPath(key), value)
997
           for (key, value) in fingerprints.items())
998

    
999
  if constants.NV_CLIENT_CERT in what:
1000
    result[constants.NV_CLIENT_CERT] = _VerifyClientCertificate()
1001

    
1002
  if constants.NV_NODELIST in what:
1003
    (nodes, bynode) = what[constants.NV_NODELIST]
1004

    
1005
    # Add nodes from other groups (different for each node)
1006
    try:
1007
      nodes.extend(bynode[my_name])
1008
    except KeyError:
1009
      pass
1010

    
1011
    # Use a random order
1012
    random.shuffle(nodes)
1013

    
1014
    # Try to contact all nodes
1015
    val = {}
1016
    for node in nodes:
1017
      params = groups_cfg.get(node_groups.get(node))
1018
      ssh_port = params["ndparams"].get(constants.ND_SSH_PORT)
1019
      logging.debug("Ssh port %s (None = default) for node %s",
1020
                    str(ssh_port), node)
1021
      success, message = _GetSshRunner(cluster_name). \
1022
                            VerifyNodeHostname(node, ssh_port)
1023
      if not success:
1024
        val[node] = message
1025

    
1026
    result[constants.NV_NODELIST] = val
1027

    
1028
  if constants.NV_NODENETTEST in what:
1029
    result[constants.NV_NODENETTEST] = tmp = {}
1030
    my_pip = my_sip = None
1031
    for name, pip, sip in what[constants.NV_NODENETTEST]:
1032
      if name == my_name:
1033
        my_pip = pip
1034
        my_sip = sip
1035
        break
1036
    if not my_pip:
1037
      tmp[my_name] = ("Can't find my own primary/secondary IP"
1038
                      " in the node list")
1039
    else:
1040
      for name, pip, sip in what[constants.NV_NODENETTEST]:
1041
        fail = []
1042
        if not netutils.TcpPing(pip, port, source=my_pip):
1043
          fail.append("primary")
1044
        if sip != pip:
1045
          if not netutils.TcpPing(sip, port, source=my_sip):
1046
            fail.append("secondary")
1047
        if fail:
1048
          tmp[name] = ("failure using the %s interface(s)" %
1049
                       " and ".join(fail))
1050

    
1051
  if constants.NV_MASTERIP in what:
1052
    # FIXME: add checks on incoming data structures (here and in the
1053
    # rest of the function)
1054
    master_name, master_ip = what[constants.NV_MASTERIP]
1055
    if master_name == my_name:
1056
      source = constants.IP4_ADDRESS_LOCALHOST
1057
    else:
1058
      source = None
1059
    result[constants.NV_MASTERIP] = netutils.TcpPing(master_ip, port,
1060
                                                     source=source)
1061

    
1062
  if constants.NV_USERSCRIPTS in what:
1063
    result[constants.NV_USERSCRIPTS] = \
1064
      [script for script in what[constants.NV_USERSCRIPTS]
1065
       if not utils.IsExecutable(script)]
1066

    
1067
  if constants.NV_OOB_PATHS in what:
1068
    result[constants.NV_OOB_PATHS] = tmp = []
1069
    for path in what[constants.NV_OOB_PATHS]:
1070
      try:
1071
        st = os.stat(path)
1072
      except OSError, err:
1073
        tmp.append("error stating out of band helper: %s" % err)
1074
      else:
1075
        if stat.S_ISREG(st.st_mode):
1076
          if stat.S_IMODE(st.st_mode) & stat.S_IXUSR:
1077
            tmp.append(None)
1078
          else:
1079
            tmp.append("out of band helper %s is not executable" % path)
1080
        else:
1081
          tmp.append("out of band helper %s is not a file" % path)
1082

    
1083
  if constants.NV_LVLIST in what and vm_capable:
1084
    try:
1085
      val = GetVolumeList(utils.ListVolumeGroups().keys())
1086
    except RPCFail, err:
1087
      val = str(err)
1088
    result[constants.NV_LVLIST] = val
1089

    
1090
  _VerifyInstanceList(what, vm_capable, result, all_hvparams)
1091

    
1092
  if constants.NV_VGLIST in what and vm_capable:
1093
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
1094

    
1095
  if constants.NV_PVLIST in what and vm_capable:
1096
    check_exclusive_pvs = constants.NV_EXCLUSIVEPVS in what
1097
    val = bdev.LogicalVolume.GetPVInfo(what[constants.NV_PVLIST],
1098
                                       filter_allocatable=False,
1099
                                       include_lvs=check_exclusive_pvs)
1100
    if check_exclusive_pvs:
1101
      result[constants.NV_EXCLUSIVEPVS] = _CheckExclusivePvs(val)
1102
      for pvi in val:
1103
        # Avoid sending useless data on the wire
1104
        pvi.lv_list = []
1105
    result[constants.NV_PVLIST] = map(objects.LvmPvInfo.ToDict, val)
1106

    
1107
  if constants.NV_VERSION in what:
1108
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
1109
                                    constants.RELEASE_VERSION)
1110

    
1111
  _VerifyNodeInfo(what, vm_capable, result, all_hvparams)
1112

    
1113
  if constants.NV_DRBDVERSION in what and vm_capable:
1114
    try:
1115
      drbd_version = DRBD8.GetProcInfo().GetVersionString()
1116
    except errors.BlockDeviceError, err:
1117
      logging.warning("Can't get DRBD version", exc_info=True)
1118
      drbd_version = str(err)
1119
    result[constants.NV_DRBDVERSION] = drbd_version
1120

    
1121
  if constants.NV_DRBDLIST in what and vm_capable:
1122
    try:
1123
      used_minors = drbd.DRBD8.GetUsedDevs()
1124
    except errors.BlockDeviceError, err:
1125
      logging.warning("Can't get used minors list", exc_info=True)
1126
      used_minors = str(err)
1127
    result[constants.NV_DRBDLIST] = used_minors
1128

    
1129
  if constants.NV_DRBDHELPER in what and vm_capable:
1130
    status = True
1131
    try:
1132
      payload = drbd.DRBD8.GetUsermodeHelper()
1133
    except errors.BlockDeviceError, err:
1134
      logging.error("Can't get DRBD usermode helper: %s", str(err))
1135
      status = False
1136
      payload = str(err)
1137
    result[constants.NV_DRBDHELPER] = (status, payload)
1138

    
1139
  if constants.NV_NODESETUP in what:
1140
    result[constants.NV_NODESETUP] = tmpr = []
1141
    if not os.path.isdir("/sys/block") or not os.path.isdir("/sys/class/net"):
1142
      tmpr.append("The sysfs filesytem doesn't seem to be mounted"
1143
                  " under /sys, missing required directories /sys/block"
1144
                  " and /sys/class/net")
1145
    if (not os.path.isdir("/proc/sys") or
1146
        not os.path.isfile("/proc/sysrq-trigger")):
1147
      tmpr.append("The procfs filesystem doesn't seem to be mounted"
1148
                  " under /proc, missing required directory /proc/sys and"
1149
                  " the file /proc/sysrq-trigger")
1150

    
1151
  if constants.NV_TIME in what:
1152
    result[constants.NV_TIME] = utils.SplitTime(time.time())
1153

    
1154
  if constants.NV_OSLIST in what and vm_capable:
1155
    result[constants.NV_OSLIST] = DiagnoseOS()
1156

    
1157
  if constants.NV_BRIDGES in what and vm_capable:
1158
    result[constants.NV_BRIDGES] = [bridge
1159
                                    for bridge in what[constants.NV_BRIDGES]
1160
                                    if not utils.BridgeExists(bridge)]
1161

    
1162
  if what.get(constants.NV_ACCEPTED_STORAGE_PATHS) == my_name:
1163
    result[constants.NV_ACCEPTED_STORAGE_PATHS] = \
1164
        filestorage.ComputeWrongFileStoragePaths()
1165

    
1166
  if what.get(constants.NV_FILE_STORAGE_PATH):
1167
    pathresult = filestorage.CheckFileStoragePath(
1168
        what[constants.NV_FILE_STORAGE_PATH])
1169
    if pathresult:
1170
      result[constants.NV_FILE_STORAGE_PATH] = pathresult
1171

    
1172
  if what.get(constants.NV_SHARED_FILE_STORAGE_PATH):
1173
    pathresult = filestorage.CheckFileStoragePath(
1174
        what[constants.NV_SHARED_FILE_STORAGE_PATH])
1175
    if pathresult:
1176
      result[constants.NV_SHARED_FILE_STORAGE_PATH] = pathresult
1177

    
1178
  return result
1179

    
1180

    
1181
def GetCryptoTokens(token_requests):
1182
  """Perform actions on the node's cryptographic tokens.
1183

1184
  Token types can be 'ssl' or 'ssh'. So far only some actions are implemented
1185
  for 'ssl'. Action 'get' returns the digest of the public client ssl
1186
  certificate. Action 'create' creates a new client certificate and private key
1187
  and also returns the digest of the certificate. The third parameter of a
1188
  token request are optional parameters for the actions, so far only the
1189
  filename is supported.
1190

1191
  @type token_requests: list of tuples of (string, string, dict), where the
1192
    first string is in constants.CRYPTO_TYPES, the second in
1193
    constants.CRYPTO_ACTIONS. The third parameter is a dictionary of string
1194
    to string.
1195
  @param token_requests: list of requests of cryptographic tokens and actions
1196
    to perform on them. The actions come with a dictionary of options.
1197
  @rtype: list of tuples (string, string)
1198
  @return: list of tuples of the token type and the public crypto token
1199

1200
  """
1201
  getents = runtime.GetEnts()
1202
  _VALID_CERT_FILES = [pathutils.NODED_CERT_FILE,
1203
                       pathutils.NODED_CLIENT_CERT_FILE,
1204
                       pathutils.NODED_CLIENT_CERT_FILE_TMP]
1205
  _DEFAULT_CERT_FILE = pathutils.NODED_CLIENT_CERT_FILE
1206
  tokens = []
1207
  for (token_type, action, options) in token_requests:
1208
    if token_type not in constants.CRYPTO_TYPES:
1209
      raise errors.ProgrammerError("Token type '%s' not supported." %
1210
                                   token_type)
1211
    if action not in constants.CRYPTO_ACTIONS:
1212
      raise errors.ProgrammerError("Action '%s' is not supported." %
1213
                                   action)
1214
    if token_type == constants.CRYPTO_TYPE_SSL_DIGEST:
1215
      if action == constants.CRYPTO_ACTION_CREATE:
1216

    
1217
        # extract file name from options
1218
        cert_filename = None
1219
        if options:
1220
          cert_filename = options.get(constants.CRYPTO_OPTION_CERT_FILE)
1221
        if not cert_filename:
1222
          cert_filename = _DEFAULT_CERT_FILE
1223
        # For security reason, we don't allow arbitrary filenames
1224
        if not cert_filename in _VALID_CERT_FILES:
1225
          raise errors.ProgrammerError(
1226
            "The certificate file name path '%s' is not allowed." %
1227
            cert_filename)
1228

    
1229
        # extract serial number from options
1230
        serial_no = None
1231
        if options:
1232
          try:
1233
            serial_no = int(options[constants.CRYPTO_OPTION_SERIAL_NO])
1234
          except ValueError:
1235
            raise errors.ProgrammerError(
1236
              "The given serial number is not an intenger: %s." %
1237
              options.get(constants.CRYPTO_OPTION_SERIAL_NO))
1238
          except KeyError:
1239
            raise errors.ProgrammerError("No serial number was provided.")
1240

    
1241
        if not serial_no:
1242
          raise errors.ProgrammerError(
1243
            "Cannot create an SSL certificate without a serial no.")
1244

    
1245
        utils.GenerateNewSslCert(
1246
          True, cert_filename, serial_no,
1247
          "Create new client SSL certificate in %s." % cert_filename,
1248
          uid=getents.masterd_uid, gid=getents.masterd_gid)
1249
        tokens.append((token_type,
1250
                       utils.GetCertificateDigest(
1251
                         cert_filename=cert_filename)))
1252
      elif action == constants.CRYPTO_ACTION_GET:
1253
        tokens.append((token_type,
1254
                       utils.GetCertificateDigest()))
1255
  return tokens
1256

    
1257

    
1258
def GetBlockDevSizes(devices):
1259
  """Return the size of the given block devices
1260

1261
  @type devices: list
1262
  @param devices: list of block device nodes to query
1263
  @rtype: dict
1264
  @return:
1265
    dictionary of all block devices under /dev (key). The value is their
1266
    size in MiB.
1267

1268
    {'/dev/disk/by-uuid/123456-12321231-312312-312': 124}
1269

1270
  """
1271
  DEV_PREFIX = "/dev/"
1272
  blockdevs = {}
1273

    
1274
  for devpath in devices:
1275
    if not utils.IsBelowDir(DEV_PREFIX, devpath):
1276
      continue
1277

    
1278
    try:
1279
      st = os.stat(devpath)
1280
    except EnvironmentError, err:
1281
      logging.warning("Error stat()'ing device %s: %s", devpath, str(err))
1282
      continue
1283

    
1284
    if stat.S_ISBLK(st.st_mode):
1285
      result = utils.RunCmd(["blockdev", "--getsize64", devpath])
1286
      if result.failed:
1287
        # We don't want to fail, just do not list this device as available
1288
        logging.warning("Cannot get size for block device %s", devpath)
1289
        continue
1290

    
1291
      size = int(result.stdout) / (1024 * 1024)
1292
      blockdevs[devpath] = size
1293
  return blockdevs
1294

    
1295

    
1296
def GetVolumeList(vg_names):
1297
  """Compute list of logical volumes and their size.
1298

1299
  @type vg_names: list
1300
  @param vg_names: the volume groups whose LVs we should list, or
1301
      empty for all volume groups
1302
  @rtype: dict
1303
  @return:
1304
      dictionary of all partions (key) with value being a tuple of
1305
      their size (in MiB), inactive and online status::
1306

1307
        {'xenvg/test1': ('20.06', True, True)}
1308

1309
      in case of errors, a string is returned with the error
1310
      details.
1311

1312
  """
1313
  lvs = {}
1314
  sep = "|"
1315
  if not vg_names:
1316
    vg_names = []
1317
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
1318
                         "--separator=%s" % sep,
1319
                         "-ovg_name,lv_name,lv_size,lv_attr"] + vg_names)
1320
  if result.failed:
1321
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
1322

    
1323
  for line in result.stdout.splitlines():
1324
    line = line.strip()
1325
    match = _LVSLINE_REGEX.match(line)
1326
    if not match:
1327
      logging.error("Invalid line returned from lvs output: '%s'", line)
1328
      continue
1329
    vg_name, name, size, attr = match.groups()
1330
    inactive = attr[4] == "-"
1331
    online = attr[5] == "o"
1332
    virtual = attr[0] == "v"
1333
    if virtual:
1334
      # we don't want to report such volumes as existing, since they
1335
      # don't really hold data
1336
      continue
1337
    lvs[vg_name + "/" + name] = (size, inactive, online)
1338

    
1339
  return lvs
1340

    
1341

    
1342
def ListVolumeGroups():
1343
  """List the volume groups and their size.
1344

1345
  @rtype: dict
1346
  @return: dictionary with keys volume name and values the
1347
      size of the volume
1348

1349
  """
1350
  return utils.ListVolumeGroups()
1351

    
1352

    
1353
def NodeVolumes():
1354
  """List all volumes on this node.
1355

1356
  @rtype: list
1357
  @return:
1358
    A list of dictionaries, each having four keys:
1359
      - name: the logical volume name,
1360
      - size: the size of the logical volume
1361
      - dev: the physical device on which the LV lives
1362
      - vg: the volume group to which it belongs
1363

1364
    In case of errors, we return an empty list and log the
1365
    error.
1366

1367
    Note that since a logical volume can live on multiple physical
1368
    volumes, the resulting list might include a logical volume
1369
    multiple times.
1370

1371
  """
1372
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
1373
                         "--separator=|",
1374
                         "--options=lv_name,lv_size,devices,vg_name"])
1375
  if result.failed:
1376
    _Fail("Failed to list logical volumes, lvs output: %s",
1377
          result.output)
1378

    
1379
  def parse_dev(dev):
1380
    return dev.split("(")[0]
1381

    
1382
  def handle_dev(dev):
1383
    return [parse_dev(x) for x in dev.split(",")]
1384

    
1385
  def map_line(line):
1386
    line = [v.strip() for v in line]
1387
    return [{"name": line[0], "size": line[1],
1388
             "dev": dev, "vg": line[3]} for dev in handle_dev(line[2])]
1389

    
1390
  all_devs = []
1391
  for line in result.stdout.splitlines():
1392
    if line.count("|") >= 3:
1393
      all_devs.extend(map_line(line.split("|")))
1394
    else:
1395
      logging.warning("Strange line in the output from lvs: '%s'", line)
1396
  return all_devs
1397

    
1398

    
1399
def BridgesExist(bridges_list):
1400
  """Check if a list of bridges exist on the current node.
1401

1402
  @rtype: boolean
1403
  @return: C{True} if all of them exist, C{False} otherwise
1404

1405
  """
1406
  missing = []
1407
  for bridge in bridges_list:
1408
    if not utils.BridgeExists(bridge):
1409
      missing.append(bridge)
1410

    
1411
  if missing:
1412
    _Fail("Missing bridges %s", utils.CommaJoin(missing))
1413

    
1414

    
1415
def GetInstanceListForHypervisor(hname, hvparams=None,
1416
                                 get_hv_fn=hypervisor.GetHypervisor):
1417
  """Provides a list of instances of the given hypervisor.
1418

1419
  @type hname: string
1420
  @param hname: name of the hypervisor
1421
  @type hvparams: dict of strings
1422
  @param hvparams: hypervisor parameters for the given hypervisor
1423
  @type get_hv_fn: function
1424
  @param get_hv_fn: function that returns a hypervisor for the given hypervisor
1425
    name; optional parameter to increase testability
1426

1427
  @rtype: list
1428
  @return: a list of all running instances on the current node
1429
    - instance1.example.com
1430
    - instance2.example.com
1431

1432
  """
1433
  results = []
1434
  try:
1435
    hv = get_hv_fn(hname)
1436
    names = hv.ListInstances(hvparams=hvparams)
1437
    results.extend(names)
1438
  except errors.HypervisorError, err:
1439
    _Fail("Error enumerating instances (hypervisor %s): %s",
1440
          hname, err, exc=True)
1441
  return results
1442

    
1443

    
1444
def GetInstanceList(hypervisor_list, all_hvparams=None,
1445
                    get_hv_fn=hypervisor.GetHypervisor):
1446
  """Provides a list of instances.
1447

1448
  @type hypervisor_list: list
1449
  @param hypervisor_list: the list of hypervisors to query information
1450
  @type all_hvparams: dict of dict of strings
1451
  @param all_hvparams: a dictionary mapping hypervisor types to respective
1452
    cluster-wide hypervisor parameters
1453
  @type get_hv_fn: function
1454
  @param get_hv_fn: function that returns a hypervisor for the given hypervisor
1455
    name; optional parameter to increase testability
1456

1457
  @rtype: list
1458
  @return: a list of all running instances on the current node
1459
    - instance1.example.com
1460
    - instance2.example.com
1461

1462
  """
1463
  results = []
1464
  for hname in hypervisor_list:
1465
    hvparams = all_hvparams[hname]
1466
    results.extend(GetInstanceListForHypervisor(hname, hvparams=hvparams,
1467
                                                get_hv_fn=get_hv_fn))
1468
  return results
1469

    
1470

    
1471
def GetInstanceInfo(instance, hname, hvparams=None):
1472
  """Gives back the information about an instance as a dictionary.
1473

1474
  @type instance: string
1475
  @param instance: the instance name
1476
  @type hname: string
1477
  @param hname: the hypervisor type of the instance
1478
  @type hvparams: dict of strings
1479
  @param hvparams: the instance's hvparams
1480

1481
  @rtype: dict
1482
  @return: dictionary with the following keys:
1483
      - memory: memory size of instance (int)
1484
      - state: state of instance (HvInstanceState)
1485
      - time: cpu time of instance (float)
1486
      - vcpus: the number of vcpus (int)
1487

1488
  """
1489
  output = {}
1490

    
1491
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance,
1492
                                                          hvparams=hvparams)
1493
  if iinfo is not None:
1494
    output["memory"] = iinfo[2]
1495
    output["vcpus"] = iinfo[3]
1496
    output["state"] = iinfo[4]
1497
    output["time"] = iinfo[5]
1498

    
1499
  return output
1500

    
1501

    
1502
def GetInstanceMigratable(instance):
1503
  """Computes whether an instance can be migrated.
1504

1505
  @type instance: L{objects.Instance}
1506
  @param instance: object representing the instance to be checked.
1507

1508
  @rtype: tuple
1509
  @return: tuple of (result, description) where:
1510
      - result: whether the instance can be migrated or not
1511
      - description: a description of the issue, if relevant
1512

1513
  """
1514
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1515
  iname = instance.name
1516
  if iname not in hyper.ListInstances(instance.hvparams):
1517
    _Fail("Instance %s is not running", iname)
1518

    
1519
  for idx in range(len(instance.disks)):
1520
    link_name = _GetBlockDevSymlinkPath(iname, idx)
1521
    if not os.path.islink(link_name):
1522
      logging.warning("Instance %s is missing symlink %s for disk %d",
1523
                      iname, link_name, idx)
1524

    
1525

    
1526
def GetAllInstancesInfo(hypervisor_list, all_hvparams):
1527
  """Gather data about all instances.
1528

1529
  This is the equivalent of L{GetInstanceInfo}, except that it
1530
  computes data for all instances at once, thus being faster if one
1531
  needs data about more than one instance.
1532

1533
  @type hypervisor_list: list
1534
  @param hypervisor_list: list of hypervisors to query for instance data
1535
  @type all_hvparams: dict of dict of strings
1536
  @param all_hvparams: mapping of hypervisor names to hvparams
1537

1538
  @rtype: dict
1539
  @return: dictionary of instance: data, with data having the following keys:
1540
      - memory: memory size of instance (int)
1541
      - state: xen state of instance (string)
1542
      - time: cpu time of instance (float)
1543
      - vcpus: the number of vcpus
1544

1545
  """
1546
  output = {}
1547
  for hname in hypervisor_list:
1548
    hvparams = all_hvparams[hname]
1549
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo(hvparams)
1550
    if iinfo:
1551
      for name, _, memory, vcpus, state, times in iinfo:
1552
        value = {
1553
          "memory": memory,
1554
          "vcpus": vcpus,
1555
          "state": state,
1556
          "time": times,
1557
          }
1558
        if name in output:
1559
          # we only check static parameters, like memory and vcpus,
1560
          # and not state and time which can change between the
1561
          # invocations of the different hypervisors
1562
          for key in "memory", "vcpus":
1563
            if value[key] != output[name][key]:
1564
              _Fail("Instance %s is running twice"
1565
                    " with different parameters", name)
1566
        output[name] = value
1567

    
1568
  return output
1569

    
1570

    
1571
def GetInstanceConsoleInfo(instance_param_dict,
1572
                           get_hv_fn=hypervisor.GetHypervisor):
1573
  """Gather data about the console access of a set of instances of this node.
1574

1575
  This function assumes that the caller already knows which instances are on
1576
  this node, by calling a function such as L{GetAllInstancesInfo} or
1577
  L{GetInstanceList}.
1578

1579
  For every instance, a large amount of configuration data needs to be
1580
  provided to the hypervisor interface in order to receive the console
1581
  information. Whether this could or should be cut down can be discussed.
1582
  The information is provided in a dictionary indexed by instance name,
1583
  allowing any number of instance queries to be done.
1584

1585
  @type instance_param_dict: dict of string to tuple of dictionaries, where the
1586
    dictionaries represent: L{objects.Instance}, L{objects.Node},
1587
    L{objects.NodeGroup}, HvParams, BeParams
1588
  @param instance_param_dict: mapping of instance name to parameters necessary
1589
    for console information retrieval
1590

1591
  @rtype: dict
1592
  @return: dictionary of instance: data, with data having the following keys:
1593
      - instance: instance name
1594
      - kind: console kind
1595
      - message: used with kind == CONS_MESSAGE, indicates console to be
1596
                 unavailable, supplies error message
1597
      - host: host to connect to
1598
      - port: port to use
1599
      - user: user for login
1600
      - command: the command, broken into parts as an array
1601
      - display: unknown, potentially unused?
1602

1603
  """
1604

    
1605
  output = {}
1606
  for inst_name in instance_param_dict:
1607
    instance = instance_param_dict[inst_name]["instance"]
1608
    pnode = instance_param_dict[inst_name]["node"]
1609
    group = instance_param_dict[inst_name]["group"]
1610
    hvparams = instance_param_dict[inst_name]["hvParams"]
1611
    beparams = instance_param_dict[inst_name]["beParams"]
1612

    
1613
    instance = objects.Instance.FromDict(instance)
1614
    pnode = objects.Node.FromDict(pnode)
1615
    group = objects.NodeGroup.FromDict(group)
1616

    
1617
    h = get_hv_fn(instance.hypervisor)
1618
    output[inst_name] = h.GetInstanceConsole(instance, pnode, group,
1619
                                             hvparams, beparams).ToDict()
1620

    
1621
  return output
1622

    
1623

    
1624
def _InstanceLogName(kind, os_name, instance, component):
1625
  """Compute the OS log filename for a given instance and operation.
1626

1627
  The instance name and os name are passed in as strings since not all
1628
  operations have these as part of an instance object.
1629

1630
  @type kind: string
1631
  @param kind: the operation type (e.g. add, import, etc.)
1632
  @type os_name: string
1633
  @param os_name: the os name
1634
  @type instance: string
1635
  @param instance: the name of the instance being imported/added/etc.
1636
  @type component: string or None
1637
  @param component: the name of the component of the instance being
1638
      transferred
1639

1640
  """
1641
  # TODO: Use tempfile.mkstemp to create unique filename
1642
  if component:
1643
    assert "/" not in component
1644
    c_msg = "-%s" % component
1645
  else:
1646
    c_msg = ""
1647
  base = ("%s-%s-%s%s-%s.log" %
1648
          (kind, os_name, instance, c_msg, utils.TimestampForFilename()))
1649
  return utils.PathJoin(pathutils.LOG_OS_DIR, base)
1650

    
1651

    
1652
def InstanceOsAdd(instance, reinstall, debug):
1653
  """Add an OS to an instance.
1654

1655
  @type instance: L{objects.Instance}
1656
  @param instance: Instance whose OS is to be installed
1657
  @type reinstall: boolean
1658
  @param reinstall: whether this is an instance reinstall
1659
  @type debug: integer
1660
  @param debug: debug level, passed to the OS scripts
1661
  @rtype: None
1662

1663
  """
1664
  inst_os = OSFromDisk(instance.os)
1665

    
1666
  create_env = OSEnvironment(instance, inst_os, debug)
1667
  if reinstall:
1668
    create_env["INSTANCE_REINSTALL"] = "1"
1669

    
1670
  logfile = _InstanceLogName("add", instance.os, instance.name, None)
1671

    
1672
  result = utils.RunCmd([inst_os.create_script], env=create_env,
1673
                        cwd=inst_os.path, output=logfile, reset_env=True)
1674
  if result.failed:
1675
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
1676
                  " output: %s", result.cmd, result.fail_reason, logfile,
1677
                  result.output)
1678
    lines = [utils.SafeEncode(val)
1679
             for val in utils.TailFile(logfile, lines=20)]
1680
    _Fail("OS create script failed (%s), last lines in the"
1681
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1682

    
1683

    
1684
def RunRenameInstance(instance, old_name, debug):
1685
  """Run the OS rename script for an instance.
1686

1687
  @type instance: L{objects.Instance}
1688
  @param instance: Instance whose OS is to be installed
1689
  @type old_name: string
1690
  @param old_name: previous instance name
1691
  @type debug: integer
1692
  @param debug: debug level, passed to the OS scripts
1693
  @rtype: boolean
1694
  @return: the success of the operation
1695

1696
  """
1697
  inst_os = OSFromDisk(instance.os)
1698

    
1699
  rename_env = OSEnvironment(instance, inst_os, debug)
1700
  rename_env["OLD_INSTANCE_NAME"] = old_name
1701

    
1702
  logfile = _InstanceLogName("rename", instance.os,
1703
                             "%s-%s" % (old_name, instance.name), None)
1704

    
1705
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
1706
                        cwd=inst_os.path, output=logfile, reset_env=True)
1707

    
1708
  if result.failed:
1709
    logging.error("os create command '%s' returned error: %s output: %s",
1710
                  result.cmd, result.fail_reason, result.output)
1711
    lines = [utils.SafeEncode(val)
1712
             for val in utils.TailFile(logfile, lines=20)]
1713
    _Fail("OS rename script failed (%s), last lines in the"
1714
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
1715

    
1716

    
1717
def _GetBlockDevSymlinkPath(instance_name, idx, _dir=None):
1718
  """Returns symlink path for block device.
1719

1720
  """
1721
  if _dir is None:
1722
    _dir = pathutils.DISK_LINKS_DIR
1723

    
1724
  return utils.PathJoin(_dir,
1725
                        ("%s%s%s" %
1726
                         (instance_name, constants.DISK_SEPARATOR, idx)))
1727

    
1728

    
1729
def _SymlinkBlockDev(instance_name, device_path, idx):
1730
  """Set up symlinks to a instance's block device.
1731

1732
  This is an auxiliary function run when an instance is start (on the primary
1733
  node) or when an instance is migrated (on the target node).
1734

1735

1736
  @param instance_name: the name of the target instance
1737
  @param device_path: path of the physical block device, on the node
1738
  @param idx: the disk index
1739
  @return: absolute path to the disk's symlink
1740

1741
  """
1742
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1743
  try:
1744
    os.symlink(device_path, link_name)
1745
  except OSError, err:
1746
    if err.errno == errno.EEXIST:
1747
      if (not os.path.islink(link_name) or
1748
          os.readlink(link_name) != device_path):
1749
        os.remove(link_name)
1750
        os.symlink(device_path, link_name)
1751
    else:
1752
      raise
1753

    
1754
  return link_name
1755

    
1756

    
1757
def _RemoveBlockDevLinks(instance_name, disks):
1758
  """Remove the block device symlinks belonging to the given instance.
1759

1760
  """
1761
  for idx, _ in enumerate(disks):
1762
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
1763
    if os.path.islink(link_name):
1764
      try:
1765
        os.remove(link_name)
1766
      except OSError:
1767
        logging.exception("Can't remove symlink '%s'", link_name)
1768

    
1769

    
1770
def _CalculateDeviceURI(instance, disk, device):
1771
  """Get the URI for the device.
1772

1773
  @type instance: L{objects.Instance}
1774
  @param instance: the instance which disk belongs to
1775
  @type disk: L{objects.Disk}
1776
  @param disk: the target disk object
1777
  @type device: L{bdev.BlockDev}
1778
  @param device: the corresponding BlockDevice
1779
  @rtype: string
1780
  @return: the device uri if any else None
1781

1782
  """
1783
  access_mode = disk.params.get(constants.LDP_ACCESS,
1784
                                constants.DISK_KERNELSPACE)
1785
  if access_mode == constants.DISK_USERSPACE:
1786
    # This can raise errors.BlockDeviceError
1787
    return device.GetUserspaceAccessUri(instance.hypervisor)
1788
  else:
1789
    return None
1790

    
1791

    
1792
def _GatherAndLinkBlockDevs(instance):
1793
  """Set up an instance's block device(s).
1794

1795
  This is run on the primary node at instance startup. The block
1796
  devices must be already assembled.
1797

1798
  @type instance: L{objects.Instance}
1799
  @param instance: the instance whose disks we should assemble
1800
  @rtype: list
1801
  @return: list of (disk_object, link_name, drive_uri)
1802

1803
  """
1804
  block_devices = []
1805
  for idx, disk in enumerate(instance.disks):
1806
    device = _RecursiveFindBD(disk)
1807
    if device is None:
1808
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
1809
                                    str(disk))
1810
    device.Open()
1811
    try:
1812
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
1813
    except OSError, e:
1814
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
1815
                                    e.strerror)
1816
    uri = _CalculateDeviceURI(instance, disk, device)
1817

    
1818
    block_devices.append((disk, link_name, uri))
1819

    
1820
  return block_devices
1821

    
1822

    
1823
def StartInstance(instance, startup_paused, reason, store_reason=True):
1824
  """Start an instance.
1825

1826
  @type instance: L{objects.Instance}
1827
  @param instance: the instance object
1828
  @type startup_paused: bool
1829
  @param instance: pause instance at startup?
1830
  @type reason: list of reasons
1831
  @param reason: the reason trail for this startup
1832
  @type store_reason: boolean
1833
  @param store_reason: whether to store the shutdown reason trail on file
1834
  @rtype: None
1835

1836
  """
1837
  running_instances = GetInstanceListForHypervisor(instance.hypervisor,
1838
                                                   instance.hvparams)
1839

    
1840
  if instance.name in running_instances:
1841
    logging.info("Instance %s already running, not starting", instance.name)
1842
    return
1843

    
1844
  try:
1845
    block_devices = _GatherAndLinkBlockDevs(instance)
1846
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
1847
    hyper.StartInstance(instance, block_devices, startup_paused)
1848
    if store_reason:
1849
      _StoreInstReasonTrail(instance.name, reason)
1850
  except errors.BlockDeviceError, err:
1851
    _Fail("Block device error: %s", err, exc=True)
1852
  except errors.HypervisorError, err:
1853
    _RemoveBlockDevLinks(instance.name, instance.disks)
1854
    _Fail("Hypervisor error: %s", err, exc=True)
1855

    
1856

    
1857
def InstanceShutdown(instance, timeout, reason, store_reason=True):
1858
  """Shut an instance down.
1859

1860
  @note: this functions uses polling with a hardcoded timeout.
1861

1862
  @type instance: L{objects.Instance}
1863
  @param instance: the instance object
1864
  @type timeout: integer
1865
  @param timeout: maximum timeout for soft shutdown
1866
  @type reason: list of reasons
1867
  @param reason: the reason trail for this shutdown
1868
  @type store_reason: boolean
1869
  @param store_reason: whether to store the shutdown reason trail on file
1870
  @rtype: None
1871

1872
  """
1873
  hv_name = instance.hypervisor
1874
  hyper = hypervisor.GetHypervisor(hv_name)
1875
  iname = instance.name
1876

    
1877
  if instance.name not in hyper.ListInstances(instance.hvparams):
1878
    logging.info("Instance %s not running, doing nothing", iname)
1879
    return
1880

    
1881
  class _TryShutdown:
1882
    def __init__(self):
1883
      self.tried_once = False
1884

    
1885
    def __call__(self):
1886
      if iname not in hyper.ListInstances(instance.hvparams):
1887
        return
1888

    
1889
      try:
1890
        hyper.StopInstance(instance, retry=self.tried_once, timeout=timeout)
1891
        if store_reason:
1892
          _StoreInstReasonTrail(instance.name, reason)
1893
      except errors.HypervisorError, err:
1894
        if iname not in hyper.ListInstances(instance.hvparams):
1895
          # if the instance is no longer existing, consider this a
1896
          # success and go to cleanup
1897
          return
1898

    
1899
        _Fail("Failed to stop instance %s: %s", iname, err)
1900

    
1901
      self.tried_once = True
1902

    
1903
      raise utils.RetryAgain()
1904

    
1905
  try:
1906
    utils.Retry(_TryShutdown(), 5, timeout)
1907
  except utils.RetryTimeout:
1908
    # the shutdown did not succeed
1909
    logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
1910

    
1911
    try:
1912
      hyper.StopInstance(instance, force=True)
1913
    except errors.HypervisorError, err:
1914
      if iname in hyper.ListInstances(instance.hvparams):
1915
        # only raise an error if the instance still exists, otherwise
1916
        # the error could simply be "instance ... unknown"!
1917
        _Fail("Failed to force stop instance %s: %s", iname, err)
1918

    
1919
    time.sleep(1)
1920

    
1921
    if iname in hyper.ListInstances(instance.hvparams):
1922
      _Fail("Could not shutdown instance %s even by destroy", iname)
1923

    
1924
  try:
1925
    hyper.CleanupInstance(instance.name)
1926
  except errors.HypervisorError, err:
1927
    logging.warning("Failed to execute post-shutdown cleanup step: %s", err)
1928

    
1929
  _RemoveBlockDevLinks(iname, instance.disks)
1930

    
1931

    
1932
def InstanceReboot(instance, reboot_type, shutdown_timeout, reason):
1933
  """Reboot an instance.
1934

1935
  @type instance: L{objects.Instance}
1936
  @param instance: the instance object to reboot
1937
  @type reboot_type: str
1938
  @param reboot_type: the type of reboot, one the following
1939
    constants:
1940
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
1941
        instance OS, do not recreate the VM
1942
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
1943
        restart the VM (at the hypervisor level)
1944
      - the other reboot type (L{constants.INSTANCE_REBOOT_FULL}) is
1945
        not accepted here, since that mode is handled differently, in
1946
        cmdlib, and translates into full stop and start of the
1947
        instance (instead of a call_instance_reboot RPC)
1948
  @type shutdown_timeout: integer
1949
  @param shutdown_timeout: maximum timeout for soft shutdown
1950
  @type reason: list of reasons
1951
  @param reason: the reason trail for this reboot
1952
  @rtype: None
1953

1954
  """
1955
  running_instances = GetInstanceListForHypervisor(instance.hypervisor,
1956
                                                   instance.hvparams)
1957

    
1958
  if instance.name not in running_instances:
1959
    _Fail("Cannot reboot instance %s that is not running", instance.name)
1960

    
1961
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1962
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
1963
    try:
1964
      hyper.RebootInstance(instance)
1965
    except errors.HypervisorError, err:
1966
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
1967
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
1968
    try:
1969
      InstanceShutdown(instance, shutdown_timeout, reason, store_reason=False)
1970
      result = StartInstance(instance, False, reason, store_reason=False)
1971
      _StoreInstReasonTrail(instance.name, reason)
1972
      return result
1973
    except errors.HypervisorError, err:
1974
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
1975
  else:
1976
    _Fail("Invalid reboot_type received: %s", reboot_type)
1977

    
1978

    
1979
def InstanceBalloonMemory(instance, memory):
1980
  """Resize an instance's memory.
1981

1982
  @type instance: L{objects.Instance}
1983
  @param instance: the instance object
1984
  @type memory: int
1985
  @param memory: new memory amount in MB
1986
  @rtype: None
1987

1988
  """
1989
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1990
  running = hyper.ListInstances(instance.hvparams)
1991
  if instance.name not in running:
1992
    logging.info("Instance %s is not running, cannot balloon", instance.name)
1993
    return
1994
  try:
1995
    hyper.BalloonInstanceMemory(instance, memory)
1996
  except errors.HypervisorError, err:
1997
    _Fail("Failed to balloon instance memory: %s", err, exc=True)
1998

    
1999

    
2000
def MigrationInfo(instance):
2001
  """Gather information about an instance to be migrated.
2002

2003
  @type instance: L{objects.Instance}
2004
  @param instance: the instance definition
2005

2006
  """
2007
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2008
  try:
2009
    info = hyper.MigrationInfo(instance)
2010
  except errors.HypervisorError, err:
2011
    _Fail("Failed to fetch migration information: %s", err, exc=True)
2012
  return info
2013

    
2014

    
2015
def AcceptInstance(instance, info, target):
2016
  """Prepare the node to accept an instance.
2017

2018
  @type instance: L{objects.Instance}
2019
  @param instance: the instance definition
2020
  @type info: string/data (opaque)
2021
  @param info: migration information, from the source node
2022
  @type target: string
2023
  @param target: target host (usually ip), on this node
2024

2025
  """
2026
  # TODO: why is this required only for DTS_EXT_MIRROR?
2027
  if instance.disk_template in constants.DTS_EXT_MIRROR:
2028
    # Create the symlinks, as the disks are not active
2029
    # in any way
2030
    try:
2031
      _GatherAndLinkBlockDevs(instance)
2032
    except errors.BlockDeviceError, err:
2033
      _Fail("Block device error: %s", err, exc=True)
2034

    
2035
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2036
  try:
2037
    hyper.AcceptInstance(instance, info, target)
2038
  except errors.HypervisorError, err:
2039
    if instance.disk_template in constants.DTS_EXT_MIRROR:
2040
      _RemoveBlockDevLinks(instance.name, instance.disks)
2041
    _Fail("Failed to accept instance: %s", err, exc=True)
2042

    
2043

    
2044
def FinalizeMigrationDst(instance, info, success):
2045
  """Finalize any preparation to accept an instance.
2046

2047
  @type instance: L{objects.Instance}
2048
  @param instance: the instance definition
2049
  @type info: string/data (opaque)
2050
  @param info: migration information, from the source node
2051
  @type success: boolean
2052
  @param success: whether the migration was a success or a failure
2053

2054
  """
2055
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2056
  try:
2057
    hyper.FinalizeMigrationDst(instance, info, success)
2058
  except errors.HypervisorError, err:
2059
    _Fail("Failed to finalize migration on the target node: %s", err, exc=True)
2060

    
2061

    
2062
def MigrateInstance(cluster_name, instance, target, live):
2063
  """Migrates an instance to another node.
2064

2065
  @type cluster_name: string
2066
  @param cluster_name: name of the cluster
2067
  @type instance: L{objects.Instance}
2068
  @param instance: the instance definition
2069
  @type target: string
2070
  @param target: the target node name
2071
  @type live: boolean
2072
  @param live: whether the migration should be done live or not (the
2073
      interpretation of this parameter is left to the hypervisor)
2074
  @raise RPCFail: if migration fails for some reason
2075

2076
  """
2077
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2078

    
2079
  try:
2080
    hyper.MigrateInstance(cluster_name, instance, target, live)
2081
  except errors.HypervisorError, err:
2082
    _Fail("Failed to migrate instance: %s", err, exc=True)
2083

    
2084

    
2085
def FinalizeMigrationSource(instance, success, live):
2086
  """Finalize the instance migration on the source node.
2087

2088
  @type instance: L{objects.Instance}
2089
  @param instance: the instance definition of the migrated instance
2090
  @type success: bool
2091
  @param success: whether the migration succeeded or not
2092
  @type live: bool
2093
  @param live: whether the user requested a live migration or not
2094
  @raise RPCFail: If the execution fails for some reason
2095

2096
  """
2097
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2098

    
2099
  try:
2100
    hyper.FinalizeMigrationSource(instance, success, live)
2101
  except Exception, err:  # pylint: disable=W0703
2102
    _Fail("Failed to finalize the migration on the source node: %s", err,
2103
          exc=True)
2104

    
2105

    
2106
def GetMigrationStatus(instance):
2107
  """Get the migration status
2108

2109
  @type instance: L{objects.Instance}
2110
  @param instance: the instance that is being migrated
2111
  @rtype: L{objects.MigrationStatus}
2112
  @return: the status of the current migration (one of
2113
           L{constants.HV_MIGRATION_VALID_STATUSES}), plus any additional
2114
           progress info that can be retrieved from the hypervisor
2115
  @raise RPCFail: If the migration status cannot be retrieved
2116

2117
  """
2118
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2119
  try:
2120
    return hyper.GetMigrationStatus(instance)
2121
  except Exception, err:  # pylint: disable=W0703
2122
    _Fail("Failed to get migration status: %s", err, exc=True)
2123

    
2124

    
2125
def HotplugDevice(instance, action, dev_type, device, extra, seq):
2126
  """Hotplug a device
2127

2128
  Hotplug is currently supported only for KVM Hypervisor.
2129
  @type instance: L{objects.Instance}
2130
  @param instance: the instance to which we hotplug a device
2131
  @type action: string
2132
  @param action: the hotplug action to perform
2133
  @type dev_type: string
2134
  @param dev_type: the device type to hotplug
2135
  @type device: either L{objects.NIC} or L{objects.Disk}
2136
  @param device: the device object to hotplug
2137
  @type extra: string
2138
  @param extra: extra info used by hotplug code (e.g. disk link)
2139
  @type seq: int
2140
  @param seq: the index of the device from master perspective
2141
  @raise RPCFail: in case instance does not have KVM hypervisor
2142

2143
  """
2144
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2145
  try:
2146
    hyper.VerifyHotplugSupport(instance, action, dev_type)
2147
  except errors.HotplugError, err:
2148
    _Fail("Hotplug is not supported: %s", err)
2149

    
2150
  if action == constants.HOTPLUG_ACTION_ADD:
2151
    fn = hyper.HotAddDevice
2152
  elif action == constants.HOTPLUG_ACTION_REMOVE:
2153
    fn = hyper.HotDelDevice
2154
  elif action == constants.HOTPLUG_ACTION_MODIFY:
2155
    fn = hyper.HotModDevice
2156
  else:
2157
    assert action in constants.HOTPLUG_ALL_ACTIONS
2158

    
2159
  return fn(instance, dev_type, device, extra, seq)
2160

    
2161

    
2162
def HotplugSupported(instance):
2163
  """Checks if hotplug is generally supported.
2164

2165
  """
2166
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
2167
  try:
2168
    hyper.HotplugSupported(instance)
2169
  except errors.HotplugError, err:
2170
    _Fail("Hotplug is not supported: %s", err)
2171

    
2172

    
2173
def BlockdevCreate(disk, size, owner, on_primary, info, excl_stor):
2174
  """Creates a block device for an instance.
2175

2176
  @type disk: L{objects.Disk}
2177
  @param disk: the object describing the disk we should create
2178
  @type size: int
2179
  @param size: the size of the physical underlying device, in MiB
2180
  @type owner: str
2181
  @param owner: the name of the instance for which disk is created,
2182
      used for device cache data
2183
  @type on_primary: boolean
2184
  @param on_primary:  indicates if it is the primary node or not
2185
  @type info: string
2186
  @param info: string that will be sent to the physical device
2187
      creation, used for example to set (LVM) tags on LVs
2188
  @type excl_stor: boolean
2189
  @param excl_stor: Whether exclusive_storage is active
2190

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

2195
  """
2196
  # TODO: remove the obsolete "size" argument
2197
  # pylint: disable=W0613
2198
  clist = []
2199
  if disk.children:
2200
    for child in disk.children:
2201
      try:
2202
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
2203
      except errors.BlockDeviceError, err:
2204
        _Fail("Can't assemble device %s: %s", child, err)
2205
      if on_primary or disk.AssembleOnSecondary():
2206
        # we need the children open in case the device itself has to
2207
        # be assembled
2208
        try:
2209
          # pylint: disable=E1103
2210
          crdev.Open()
2211
        except errors.BlockDeviceError, err:
2212
          _Fail("Can't make child '%s' read-write: %s", child, err)
2213
      clist.append(crdev)
2214

    
2215
  try:
2216
    device = bdev.Create(disk, clist, excl_stor)
2217
  except errors.BlockDeviceError, err:
2218
    _Fail("Can't create block device: %s", err)
2219

    
2220
  if on_primary or disk.AssembleOnSecondary():
2221
    try:
2222
      device.Assemble()
2223
    except errors.BlockDeviceError, err:
2224
      _Fail("Can't assemble device after creation, unusual event: %s", err)
2225
    if on_primary or disk.OpenOnSecondary():
2226
      try:
2227
        device.Open(force=True)
2228
      except errors.BlockDeviceError, err:
2229
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
2230
    DevCacheManager.UpdateCache(device.dev_path, owner,
2231
                                on_primary, disk.iv_name)
2232

    
2233
  device.SetInfo(info)
2234

    
2235
  return device.unique_id
2236

    
2237

    
2238
def _DumpDevice(source_path, target_path, offset, size):
2239
  """This function images/wipes the device using a local file.
2240

2241
  @type source_path: string
2242
  @param source_path: path of the image or data source (e.g., "/dev/zero")
2243

2244
  @type target_path: string
2245
  @param target_path: path of the device to image/wipe
2246

2247
  @type offset: int
2248
  @param offset: offset in MiB in the output file
2249

2250
  @type size: int
2251
  @param size: maximum size in MiB to write (data source might be smaller)
2252

2253
  @return: None
2254
  @raise RPCFail: in case of failure
2255

2256
  """
2257
  # Internal sizes are always in Mebibytes; if the following "dd" command
2258
  # should use a different block size the offset and size given to this
2259
  # function must be adjusted accordingly before being passed to "dd".
2260
  block_size = 1024 * 1024
2261

    
2262
  cmd = [constants.DD_CMD, "if=%s" % source_path, "seek=%d" % offset,
2263
         "bs=%s" % block_size, "oflag=direct", "of=%s" % target_path,
2264
         "count=%d" % size]
2265
  result = utils.RunCmd(cmd)
2266

    
2267
  if result.failed:
2268
    _Fail("Dump command '%s' exited with error: %s; output: %s", result.cmd,
2269
          result.fail_reason, result.output)
2270

    
2271

    
2272
def _DownloadAndDumpDevice(source_url, target_path, size):
2273
  """This function images a device using a downloaded image file.
2274

2275
  @type source_url: string
2276
  @param source_url: URL of image to dump to disk
2277

2278
  @type target_path: string
2279
  @param target_path: path of the device to image
2280

2281
  @type size: int
2282
  @param size: maximum size in MiB to write (data source might be smaller)
2283

2284
  @rtype: NoneType
2285
  @return: None
2286
  @raise RPCFail: in case of download or write failures
2287

2288
  """
2289
  class DDParams(object):
2290
    def __init__(self, current_size, total_size):
2291
      self.current_size = current_size
2292
      self.total_size = total_size
2293
      self.image_size_error = False
2294

    
2295
  def dd_write(ddparams, out):
2296
    if ddparams.current_size < ddparams.total_size:
2297
      ddparams.current_size += len(out)
2298
      target_file.write(out)
2299
    else:
2300
      ddparams.image_size_error = True
2301
      return -1
2302

    
2303
  target_file = open(target_path, "w")
2304
  ddparams = DDParams(0, 1024 * 1024 * size)
2305

    
2306
  curl = pycurl.Curl()
2307
  curl.setopt(pycurl.VERBOSE, True)
2308
  curl.setopt(pycurl.NOSIGNAL, True)
2309
  curl.setopt(pycurl.USERAGENT, http.HTTP_GANETI_VERSION)
2310
  curl.setopt(pycurl.URL, source_url)
2311
  curl.setopt(pycurl.WRITEFUNCTION, lambda out: dd_write(ddparams, out))
2312

    
2313
  try:
2314
    curl.perform()
2315
  except pycurl.error:
2316
    if ddparams.image_size_error:
2317
      _Fail("Disk image larger than the disk")
2318
    else:
2319
      raise
2320

    
2321
  target_file.close()
2322

    
2323

    
2324
def BlockdevWipe(disk, offset, size):
2325
  """Wipes a block device.
2326

2327
  @type disk: L{objects.Disk}
2328
  @param disk: the disk object we want to wipe
2329
  @type offset: int
2330
  @param offset: The offset in MiB in the file
2331
  @type size: int
2332
  @param size: The size in MiB to write
2333

2334
  """
2335
  try:
2336
    rdev = _RecursiveFindBD(disk)
2337
  except errors.BlockDeviceError:
2338
    rdev = None
2339

    
2340
  if not rdev:
2341
    _Fail("Cannot wipe device %s: device not found", disk.iv_name)
2342
  if offset < 0:
2343
    _Fail("Negative offset")
2344
  if size < 0:
2345
    _Fail("Negative size")
2346
  if offset > rdev.size:
2347
    _Fail("Wipe offset is bigger than device size")
2348
  if (offset + size) > rdev.size:
2349
    _Fail("Wipe offset and size are bigger than device size")
2350

    
2351
  _DumpDevice("/dev/zero", rdev.dev_path, offset, size)
2352

    
2353

    
2354

    
2355
def BlockdevPauseResumeSync(disks, pause):
2356
  """Pause or resume the sync of the block device.
2357

2358
  @type disks: list of L{objects.Disk}
2359
  @param disks: the disks object we want to pause/resume
2360
  @type pause: bool
2361
  @param pause: Wheater to pause or resume
2362

2363
  """
2364
  success = []
2365
  for disk in disks:
2366
    try:
2367
      rdev = _RecursiveFindBD(disk)
2368
    except errors.BlockDeviceError:
2369
      rdev = None
2370

    
2371
    if not rdev:
2372
      success.append((False, ("Cannot change sync for device %s:"
2373
                              " device not found" % disk.iv_name)))
2374
      continue
2375

    
2376
    result = rdev.PauseResumeSync(pause)
2377

    
2378
    if result:
2379
      success.append((result, None))
2380
    else:
2381
      if pause:
2382
        msg = "Pause"
2383
      else:
2384
        msg = "Resume"
2385
      success.append((result, "%s for device %s failed" % (msg, disk.iv_name)))
2386

    
2387
  return success
2388

    
2389

    
2390
def BlockdevRemove(disk):
2391
  """Remove a block device.
2392

2393
  @note: This is intended to be called recursively.
2394

2395
  @type disk: L{objects.Disk}
2396
  @param disk: the disk object we should remove
2397
  @rtype: boolean
2398
  @return: the success of the operation
2399

2400
  """
2401
  msgs = []
2402
  try:
2403
    rdev = _RecursiveFindBD(disk)
2404
  except errors.BlockDeviceError, err:
2405
    # probably can't attach
2406
    logging.info("Can't attach to device %s in remove", disk)
2407
    rdev = None
2408
  if rdev is not None:
2409
    r_path = rdev.dev_path
2410

    
2411
    def _TryRemove():
2412
      try:
2413
        rdev.Remove()
2414
        return []
2415
      except errors.BlockDeviceError, err:
2416
        return [str(err)]
2417

    
2418
    msgs.extend(utils.SimpleRetry([], _TryRemove,
2419
                                  constants.DISK_REMOVE_RETRY_INTERVAL,
2420
                                  constants.DISK_REMOVE_RETRY_TIMEOUT))
2421

    
2422
    if not msgs:
2423
      DevCacheManager.RemoveCache(r_path)
2424

    
2425
  if disk.children:
2426
    for child in disk.children:
2427
      try:
2428
        BlockdevRemove(child)
2429
      except RPCFail, err:
2430
        msgs.append(str(err))
2431

    
2432
  if msgs:
2433
    _Fail("; ".join(msgs))
2434

    
2435

    
2436
def _RecursiveAssembleBD(disk, owner, as_primary):
2437
  """Activate a block device for an instance.
2438

2439
  This is run on the primary and secondary nodes for an instance.
2440

2441
  @note: this function is called recursively.
2442

2443
  @type disk: L{objects.Disk}
2444
  @param disk: the disk we try to assemble
2445
  @type owner: str
2446
  @param owner: the name of the instance which owns the disk
2447
  @type as_primary: boolean
2448
  @param as_primary: if we should make the block device
2449
      read/write
2450

2451
  @return: the assembled device or None (in case no device
2452
      was assembled)
2453
  @raise errors.BlockDeviceError: in case there is an error
2454
      during the activation of the children or the device
2455
      itself
2456

2457
  """
2458
  children = []
2459
  if disk.children:
2460
    mcn = disk.ChildrenNeeded()
2461
    if mcn == -1:
2462
      mcn = 0 # max number of Nones allowed
2463
    else:
2464
      mcn = len(disk.children) - mcn # max number of Nones
2465
    for chld_disk in disk.children:
2466
      try:
2467
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
2468
      except errors.BlockDeviceError, err:
2469
        if children.count(None) >= mcn:
2470
          raise
2471
        cdev = None
2472
        logging.error("Error in child activation (but continuing): %s",
2473
                      str(err))
2474
      children.append(cdev)
2475

    
2476
  if as_primary or disk.AssembleOnSecondary():
2477
    r_dev = bdev.Assemble(disk, children)
2478
    result = r_dev
2479
    if as_primary or disk.OpenOnSecondary():
2480
      r_dev.Open()
2481
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
2482
                                as_primary, disk.iv_name)
2483

    
2484
  else:
2485
    result = True
2486
  return result
2487

    
2488

    
2489
def BlockdevAssemble(disk, owner, as_primary, idx):
2490
  """Activate a block device for an instance.
2491

2492
  This is a wrapper over _RecursiveAssembleBD.
2493

2494
  @rtype: str or boolean
2495
  @return: a tuple with the C{/dev/...} path and the created symlink
2496
      for primary nodes, and (C{True}, C{True}) for secondary nodes
2497

2498
  """
2499
  try:
2500
    result = _RecursiveAssembleBD(disk, owner, as_primary)
2501
    if isinstance(result, BlockDev):
2502
      # pylint: disable=E1103
2503
      dev_path = result.dev_path
2504
      link_name = None
2505
      if as_primary:
2506
        link_name = _SymlinkBlockDev(owner, dev_path, idx)
2507
    elif result:
2508
      return result, result
2509
    else:
2510
      _Fail("Unexpected result from _RecursiveAssembleBD")
2511
  except errors.BlockDeviceError, err:
2512
    _Fail("Error while assembling disk: %s", err, exc=True)
2513
  except OSError, err:
2514
    _Fail("Error while symlinking disk: %s", err, exc=True)
2515

    
2516
  return dev_path, link_name
2517

    
2518

    
2519
def BlockdevShutdown(disk):
2520
  """Shut down a block device.
2521

2522
  First, if the device is assembled (Attach() is successful), then
2523
  the device is shutdown. Then the children of the device are
2524
  shutdown.
2525

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

2530
  @type disk: L{objects.Disk}
2531
  @param disk: the description of the disk we should
2532
      shutdown
2533
  @rtype: None
2534

2535
  """
2536
  msgs = []
2537
  r_dev = _RecursiveFindBD(disk)
2538
  if r_dev is not None:
2539
    r_path = r_dev.dev_path
2540
    try:
2541
      r_dev.Shutdown()
2542
      DevCacheManager.RemoveCache(r_path)
2543
    except errors.BlockDeviceError, err:
2544
      msgs.append(str(err))
2545

    
2546
  if disk.children:
2547
    for child in disk.children:
2548
      try:
2549
        BlockdevShutdown(child)
2550
      except RPCFail, err:
2551
        msgs.append(str(err))
2552

    
2553
  if msgs:
2554
    _Fail("; ".join(msgs))
2555

    
2556

    
2557
def BlockdevAddchildren(parent_cdev, new_cdevs):
2558
  """Extend a mirrored block device.
2559

2560
  @type parent_cdev: L{objects.Disk}
2561
  @param parent_cdev: the disk to which we should add children
2562
  @type new_cdevs: list of L{objects.Disk}
2563
  @param new_cdevs: the list of children which we should add
2564
  @rtype: None
2565

2566
  """
2567
  parent_bdev = _RecursiveFindBD(parent_cdev)
2568
  if parent_bdev is None:
2569
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
2570
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
2571
  if new_bdevs.count(None) > 0:
2572
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
2573
  parent_bdev.AddChildren(new_bdevs)
2574

    
2575

    
2576
def BlockdevRemovechildren(parent_cdev, new_cdevs):
2577
  """Shrink a mirrored block device.
2578

2579
  @type parent_cdev: L{objects.Disk}
2580
  @param parent_cdev: the disk from which we should remove children
2581
  @type new_cdevs: list of L{objects.Disk}
2582
  @param new_cdevs: the list of children which we should remove
2583
  @rtype: None
2584

2585
  """
2586
  parent_bdev = _RecursiveFindBD(parent_cdev)
2587
  if parent_bdev is None:
2588
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
2589
  devs = []
2590
  for disk in new_cdevs:
2591
    rpath = disk.StaticDevPath()
2592
    if rpath is None:
2593
      bd = _RecursiveFindBD(disk)
2594
      if bd is None:
2595
        _Fail("Can't find device %s while removing children", disk)
2596
      else:
2597
        devs.append(bd.dev_path)
2598
    else:
2599
      if not utils.IsNormAbsPath(rpath):
2600
        _Fail("Strange path returned from StaticDevPath: '%s'", rpath)
2601
      devs.append(rpath)
2602
  parent_bdev.RemoveChildren(devs)
2603

    
2604

    
2605
def BlockdevGetmirrorstatus(disks):
2606
  """Get the mirroring status of a list of devices.
2607

2608
  @type disks: list of L{objects.Disk}
2609
  @param disks: the list of disks which we should query
2610
  @rtype: disk
2611
  @return: List of L{objects.BlockDevStatus}, one for each disk
2612
  @raise errors.BlockDeviceError: if any of the disks cannot be
2613
      found
2614

2615
  """
2616
  stats = []
2617
  for dsk in disks:
2618
    rbd = _RecursiveFindBD(dsk)
2619
    if rbd is None:
2620
      _Fail("Can't find device %s", dsk)
2621

    
2622
    stats.append(rbd.CombinedSyncStatus())
2623

    
2624
  return stats
2625

    
2626

    
2627
def BlockdevGetmirrorstatusMulti(disks):
2628
  """Get the mirroring status of a list of devices.
2629

2630
  @type disks: list of L{objects.Disk}
2631
  @param disks: the list of disks which we should query
2632
  @rtype: disk
2633
  @return: List of tuples, (bool, status), one for each disk; bool denotes
2634
    success/failure, status is L{objects.BlockDevStatus} on success, string
2635
    otherwise
2636

2637
  """
2638
  result = []
2639
  for disk in disks:
2640
    try:
2641
      rbd = _RecursiveFindBD(disk)
2642
      if rbd is None:
2643
        result.append((False, "Can't find device %s" % disk))
2644
        continue
2645

    
2646
      status = rbd.CombinedSyncStatus()
2647
    except errors.BlockDeviceError, err:
2648
      logging.exception("Error while getting disk status")
2649
      result.append((False, str(err)))
2650
    else:
2651
      result.append((True, status))
2652

    
2653
  assert len(disks) == len(result)
2654

    
2655
  return result
2656

    
2657

    
2658
def _RecursiveFindBD(disk):
2659
  """Check if a device is activated.
2660

2661
  If so, return information about the real device.
2662

2663
  @type disk: L{objects.Disk}
2664
  @param disk: the disk object we need to find
2665

2666
  @return: None if the device can't be found,
2667
      otherwise the device instance
2668

2669
  """
2670
  children = []
2671
  if disk.children:
2672
    for chdisk in disk.children:
2673
      children.append(_RecursiveFindBD(chdisk))
2674

    
2675
  return bdev.FindDevice(disk, children)
2676

    
2677

    
2678
def _OpenRealBD(disk):
2679
  """Opens the underlying block device of a disk.
2680

2681
  @type disk: L{objects.Disk}
2682
  @param disk: the disk object we want to open
2683

2684
  """
2685
  real_disk = _RecursiveFindBD(disk)
2686
  if real_disk is None:
2687
    _Fail("Block device '%s' is not set up", disk)
2688

    
2689
  real_disk.Open()
2690

    
2691
  return real_disk
2692

    
2693

    
2694
def BlockdevFind(disk):
2695
  """Check if a device is activated.
2696

2697
  If it is, return information about the real device.
2698

2699
  @type disk: L{objects.Disk}
2700
  @param disk: the disk to find
2701
  @rtype: None or objects.BlockDevStatus
2702
  @return: None if the disk cannot be found, otherwise a the current
2703
           information
2704

2705
  """
2706
  try:
2707
    rbd = _RecursiveFindBD(disk)
2708
  except errors.BlockDeviceError, err:
2709
    _Fail("Failed to find device: %s", err, exc=True)
2710

    
2711
  if rbd is None:
2712
    return None
2713

    
2714
  return rbd.GetSyncStatus()
2715

    
2716

    
2717
def BlockdevGetdimensions(disks):
2718
  """Computes the size of the given disks.
2719

2720
  If a disk is not found, returns None instead.
2721

2722
  @type disks: list of L{objects.Disk}
2723
  @param disks: the list of disk to compute the size for
2724
  @rtype: list
2725
  @return: list with elements None if the disk cannot be found,
2726
      otherwise the pair (size, spindles), where spindles is None if the
2727
      device doesn't support that
2728

2729
  """
2730
  result = []
2731
  for cf in disks:
2732
    try:
2733
      rbd = _RecursiveFindBD(cf)
2734
    except errors.BlockDeviceError:
2735
      result.append(None)
2736
      continue
2737
    if rbd is None:
2738
      result.append(None)
2739
    else:
2740
      result.append(rbd.GetActualDimensions())
2741
  return result
2742

    
2743

    
2744
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
2745
  """Write a file to the filesystem.
2746

2747
  This allows the master to overwrite(!) a file. It will only perform
2748
  the operation if the file belongs to a list of configuration files.
2749

2750
  @type file_name: str
2751
  @param file_name: the target file name
2752
  @type data: str
2753
  @param data: the new contents of the file
2754
  @type mode: int
2755
  @param mode: the mode to give the file (can be None)
2756
  @type uid: string
2757
  @param uid: the owner of the file
2758
  @type gid: string
2759
  @param gid: the group of the file
2760
  @type atime: float
2761
  @param atime: the atime to set on the file (can be None)
2762
  @type mtime: float
2763
  @param mtime: the mtime to set on the file (can be None)
2764
  @rtype: None
2765

2766
  """
2767
  file_name = vcluster.LocalizeVirtualPath(file_name)
2768

    
2769
  if not os.path.isabs(file_name):
2770
    _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
2771

    
2772
  if file_name not in _ALLOWED_UPLOAD_FILES:
2773
    _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
2774
          file_name)
2775

    
2776
  raw_data = _Decompress(data)
2777

    
2778
  if not (isinstance(uid, basestring) and isinstance(gid, basestring)):
2779
    _Fail("Invalid username/groupname type")
2780

    
2781
  getents = runtime.GetEnts()
2782
  uid = getents.LookupUser(uid)
2783
  gid = getents.LookupGroup(gid)
2784

    
2785
  utils.SafeWriteFile(file_name, None,
2786
                      data=raw_data, mode=mode, uid=uid, gid=gid,
2787
                      atime=atime, mtime=mtime)
2788

    
2789

    
2790
def RunOob(oob_program, command, node, timeout):
2791
  """Executes oob_program with given command on given node.
2792

2793
  @param oob_program: The path to the executable oob_program
2794
  @param command: The command to invoke on oob_program
2795
  @param node: The node given as an argument to the program
2796
  @param timeout: Timeout after which we kill the oob program
2797

2798
  @return: stdout
2799
  @raise RPCFail: If execution fails for some reason
2800

2801
  """
2802
  result = utils.RunCmd([oob_program, command, node], timeout=timeout)
2803

    
2804
  if result.failed:
2805
    _Fail("'%s' failed with reason '%s'; output: %s", result.cmd,
2806
          result.fail_reason, result.output)
2807

    
2808
  return result.stdout
2809

    
2810

    
2811
def _OSOndiskAPIVersion(os_dir):
2812
  """Compute and return the API version of a given OS.
2813

2814
  This function will try to read the API version of the OS residing in
2815
  the 'os_dir' directory.
2816

2817
  @type os_dir: str
2818
  @param os_dir: the directory in which we should look for the OS
2819
  @rtype: tuple
2820
  @return: tuple (status, data) with status denoting the validity and
2821
      data holding either the vaid versions or an error message
2822

2823
  """
2824
  api_file = utils.PathJoin(os_dir, constants.OS_API_FILE)
2825

    
2826
  try:
2827
    st = os.stat(api_file)
2828
  except EnvironmentError, err:
2829
    return False, ("Required file '%s' not found under path %s: %s" %
2830
                   (constants.OS_API_FILE, os_dir, utils.ErrnoOrStr(err)))
2831

    
2832
  if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2833
    return False, ("File '%s' in %s is not a regular file" %
2834
                   (constants.OS_API_FILE, os_dir))
2835

    
2836
  try:
2837
    api_versions = utils.ReadFile(api_file).splitlines()
2838
  except EnvironmentError, err:
2839
    return False, ("Error while reading the API version file at %s: %s" %
2840
                   (api_file, utils.ErrnoOrStr(err)))
2841

    
2842
  try:
2843
    api_versions = [int(version.strip()) for version in api_versions]
2844
  except (TypeError, ValueError), err:
2845
    return False, ("API version(s) can't be converted to integer: %s" %
2846
                   str(err))
2847

    
2848
  return True, api_versions
2849

    
2850

    
2851
def DiagnoseOS(top_dirs=None):
2852
  """Compute the validity for all OSes.
2853

2854
  @type top_dirs: list
2855
  @param top_dirs: the list of directories in which to
2856
      search (if not given defaults to
2857
      L{pathutils.OS_SEARCH_PATH})
2858
  @rtype: list of L{objects.OS}
2859
  @return: a list of tuples (name, path, status, diagnose, variants,
2860
      parameters, api_version) for all (potential) OSes under all
2861
      search paths, where:
2862
          - name is the (potential) OS name
2863
          - path is the full path to the OS
2864
          - status True/False is the validity of the OS
2865
          - diagnose is the error message for an invalid OS, otherwise empty
2866
          - variants is a list of supported OS variants, if any
2867
          - parameters is a list of (name, help) parameters, if any
2868
          - api_version is a list of support OS API versions
2869

2870
  """
2871
  if top_dirs is None:
2872
    top_dirs = pathutils.OS_SEARCH_PATH
2873

    
2874
  result = []
2875
  for dir_name in top_dirs:
2876
    if os.path.isdir(dir_name):
2877
      try:
2878
        f_names = utils.ListVisibleFiles(dir_name)
2879
      except EnvironmentError, err:
2880
        logging.exception("Can't list the OS directory %s: %s", dir_name, err)
2881
        break
2882
      for name in f_names:
2883
        os_path = utils.PathJoin(dir_name, name)
2884
        status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
2885
        if status:
2886
          diagnose = ""
2887
          variants = os_inst.supported_variants
2888
          parameters = os_inst.supported_parameters
2889
          api_versions = os_inst.api_versions
2890
        else:
2891
          diagnose = os_inst
2892
          variants = parameters = api_versions = []
2893
        result.append((name, os_path, status, diagnose, variants,
2894
                       parameters, api_versions))
2895

    
2896
  return result
2897

    
2898

    
2899
def _TryOSFromDisk(name, base_dir=None):
2900
  """Create an OS instance from disk.
2901

2902
  This function will return an OS instance if the given name is a
2903
  valid OS name.
2904

2905
  @type base_dir: string
2906
  @keyword base_dir: Base directory containing OS installations.
2907
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
2908
  @rtype: tuple
2909
  @return: success and either the OS instance if we find a valid one,
2910
      or error message
2911

2912
  """
2913
  if base_dir is None:
2914
    os_dir = utils.FindFile(name, pathutils.OS_SEARCH_PATH, os.path.isdir)
2915
  else:
2916
    os_dir = utils.FindFile(name, [base_dir], os.path.isdir)
2917

    
2918
  if os_dir is None:
2919
    return False, "Directory for OS %s not found in search path" % name
2920

    
2921
  status, api_versions = _OSOndiskAPIVersion(os_dir)
2922
  if not status:
2923
    # push the error up
2924
    return status, api_versions
2925

    
2926
  if not constants.OS_API_VERSIONS.intersection(api_versions):
2927
    return False, ("API version mismatch for path '%s': found %s, want %s." %
2928
                   (os_dir, api_versions, constants.OS_API_VERSIONS))
2929

    
2930
  # OS Files dictionary, we will populate it with the absolute path
2931
  # names; if the value is True, then it is a required file, otherwise
2932
  # an optional one
2933
  os_files = dict.fromkeys(constants.OS_SCRIPTS, True)
2934

    
2935
  if max(api_versions) >= constants.OS_API_V15:
2936
    os_files[constants.OS_VARIANTS_FILE] = False
2937

    
2938
  if max(api_versions) >= constants.OS_API_V20:
2939
    os_files[constants.OS_PARAMETERS_FILE] = True
2940
  else:
2941
    del os_files[constants.OS_SCRIPT_VERIFY]
2942

    
2943
  for (filename, required) in os_files.items():
2944
    os_files[filename] = utils.PathJoin(os_dir, filename)
2945

    
2946
    try:
2947
      st = os.stat(os_files[filename])
2948
    except EnvironmentError, err:
2949
      if err.errno == errno.ENOENT and not required:
2950
        del os_files[filename]
2951
        continue
2952
      return False, ("File '%s' under path '%s' is missing (%s)" %
2953
                     (filename, os_dir, utils.ErrnoOrStr(err)))
2954

    
2955
    if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
2956
      return False, ("File '%s' under path '%s' is not a regular file" %
2957
                     (filename, os_dir))
2958

    
2959
    if filename in constants.OS_SCRIPTS:
2960
      if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
2961
        return False, ("File '%s' under path '%s' is not executable" %
2962
                       (filename, os_dir))
2963

    
2964
  variants = []
2965
  if constants.OS_VARIANTS_FILE in os_files:
2966
    variants_file = os_files[constants.OS_VARIANTS_FILE]
2967
    try:
2968
      variants = \
2969
        utils.FilterEmptyLinesAndComments(utils.ReadFile(variants_file))
2970
    except EnvironmentError, err:
2971
      # we accept missing files, but not other errors
2972
      if err.errno != errno.ENOENT:
2973
        return False, ("Error while reading the OS variants file at %s: %s" %
2974
                       (variants_file, utils.ErrnoOrStr(err)))
2975

    
2976
  parameters = []
2977
  if constants.OS_PARAMETERS_FILE in os_files:
2978
    parameters_file = os_files[constants.OS_PARAMETERS_FILE]
2979
    try:
2980
      parameters = utils.ReadFile(parameters_file).splitlines()
2981
    except EnvironmentError, err:
2982
      return False, ("Error while reading the OS parameters file at %s: %s" %
2983
                     (parameters_file, utils.ErrnoOrStr(err)))
2984
    parameters = [v.split(None, 1) for v in parameters]
2985

    
2986
  os_obj = objects.OS(name=name, path=os_dir,
2987
                      create_script=os_files[constants.OS_SCRIPT_CREATE],
2988
                      export_script=os_files[constants.OS_SCRIPT_EXPORT],
2989
                      import_script=os_files[constants.OS_SCRIPT_IMPORT],
2990
                      rename_script=os_files[constants.OS_SCRIPT_RENAME],
2991
                      verify_script=os_files.get(constants.OS_SCRIPT_VERIFY,
2992
                                                 None),
2993
                      supported_variants=variants,
2994
                      supported_parameters=parameters,
2995
                      api_versions=api_versions)
2996
  return True, os_obj
2997

    
2998

    
2999
def OSFromDisk(name, base_dir=None):
3000
  """Create an OS instance from disk.
3001

3002
  This function will return an OS instance if the given name is a
3003
  valid OS name. Otherwise, it will raise an appropriate
3004
  L{RPCFail} exception, detailing why this is not a valid OS.
3005

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

3009
  @type base_dir: string
3010
  @keyword base_dir: Base directory containing OS installations.
3011
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
3012
  @rtype: L{objects.OS}
3013
  @return: the OS instance if we find a valid one
3014
  @raise RPCFail: if we don't find a valid OS
3015

3016
  """
3017
  name_only = objects.OS.GetName(name)
3018
  status, payload = _TryOSFromDisk(name_only, base_dir)
3019

    
3020
  if not status:
3021
    _Fail(payload)
3022

    
3023
  return payload
3024

    
3025

    
3026
def OSCoreEnv(os_name, inst_os, os_params, debug=0):
3027
  """Calculate the basic environment for an os script.
3028

3029
  @type os_name: str
3030
  @param os_name: full operating system name (including variant)
3031
  @type inst_os: L{objects.OS}
3032
  @param inst_os: operating system for which the environment is being built
3033
  @type os_params: dict
3034
  @param os_params: the OS parameters
3035
  @type debug: integer
3036
  @param debug: debug level (0 or 1, for OS Api 10)
3037
  @rtype: dict
3038
  @return: dict of environment variables
3039
  @raise errors.BlockDeviceError: if the block device
3040
      cannot be found
3041

3042
  """
3043
  result = {}
3044
  api_version = \
3045
    max(constants.OS_API_VERSIONS.intersection(inst_os.api_versions))
3046
  result["OS_API_VERSION"] = "%d" % api_version
3047
  result["OS_NAME"] = inst_os.name
3048
  result["DEBUG_LEVEL"] = "%d" % debug
3049

    
3050
  # OS variants
3051
  if api_version >= constants.OS_API_V15 and inst_os.supported_variants:
3052
    variant = objects.OS.GetVariant(os_name)
3053
    if not variant:
3054
      variant = inst_os.supported_variants[0]
3055
  else:
3056
    variant = ""
3057
  result["OS_VARIANT"] = variant
3058

    
3059
  # OS params
3060
  for pname, pvalue in os_params.items():
3061
    result["OSP_%s" % pname.upper()] = pvalue
3062

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

    
3068
  return result
3069

    
3070

    
3071
def OSEnvironment(instance, inst_os, debug=0):
3072
  """Calculate the environment for an os script.
3073

3074
  @type instance: L{objects.Instance}
3075
  @param instance: target instance for the os script run
3076
  @type inst_os: L{objects.OS}
3077
  @param inst_os: operating system for which the environment is being built
3078
  @type debug: integer
3079
  @param debug: debug level (0 or 1, for OS Api 10)
3080
  @rtype: dict
3081
  @return: dict of environment variables
3082
  @raise errors.BlockDeviceError: if the block device
3083
      cannot be found
3084

3085
  """
3086
  result = OSCoreEnv(instance.os, inst_os, instance.osparams, debug=debug)
3087

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

    
3091
  result["HYPERVISOR"] = instance.hypervisor
3092
  result["DISK_COUNT"] = "%d" % len(instance.disks)
3093
  result["NIC_COUNT"] = "%d" % len(instance.nics)
3094
  result["INSTANCE_SECONDARY_NODES"] = \
3095
      ("%s" % " ".join(instance.secondary_nodes))
3096

    
3097
  # Disks
3098
  for idx, disk in enumerate(instance.disks):
3099
    real_disk = _OpenRealBD(disk)
3100
    result["DISK_%d_PATH" % idx] = real_disk.dev_path
3101
    result["DISK_%d_ACCESS" % idx] = disk.mode
3102
    result["DISK_%d_UUID" % idx] = disk.uuid
3103
    if disk.name:
3104
      result["DISK_%d_NAME" % idx] = disk.name
3105
    if constants.HV_DISK_TYPE in instance.hvparams:
3106
      result["DISK_%d_FRONTEND_TYPE" % idx] = \
3107
        instance.hvparams[constants.HV_DISK_TYPE]
3108
    if disk.dev_type in constants.DTS_BLOCK:
3109
      result["DISK_%d_BACKEND_TYPE" % idx] = "block"
3110
    elif disk.dev_type in constants.DTS_FILEBASED:
3111
      result["DISK_%d_BACKEND_TYPE" % idx] = \
3112
        "file:%s" % disk.logical_id[0]
3113

    
3114
  # NICs
3115
  for idx, nic in enumerate(instance.nics):
3116
    result["NIC_%d_MAC" % idx] = nic.mac
3117
    result["NIC_%d_UUID" % idx] = nic.uuid
3118
    if nic.name:
3119
      result["NIC_%d_NAME" % idx] = nic.name
3120
    if nic.ip:
3121
      result["NIC_%d_IP" % idx] = nic.ip
3122
    result["NIC_%d_MODE" % idx] = nic.nicparams[constants.NIC_MODE]
3123
    if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
3124
      result["NIC_%d_BRIDGE" % idx] = nic.nicparams[constants.NIC_LINK]
3125
    if nic.nicparams[constants.NIC_LINK]:
3126
      result["NIC_%d_LINK" % idx] = nic.nicparams[constants.NIC_LINK]
3127
    if nic.netinfo:
3128
      nobj = objects.Network.FromDict(nic.netinfo)
3129
      result.update(nobj.HooksDict("NIC_%d_" % idx))
3130
    if constants.HV_NIC_TYPE in instance.hvparams:
3131
      result["NIC_%d_FRONTEND_TYPE" % idx] = \
3132
        instance.hvparams[constants.HV_NIC_TYPE]
3133

    
3134
  # HV/BE params
3135
  for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
3136
    for key, value in source.items():
3137
      result["INSTANCE_%s_%s" % (kind, key)] = str(value)
3138

    
3139
  return result
3140

    
3141

    
3142
def DiagnoseExtStorage(top_dirs=None):
3143
  """Compute the validity for all ExtStorage Providers.
3144

3145
  @type top_dirs: list
3146
  @param top_dirs: the list of directories in which to
3147
      search (if not given defaults to
3148
      L{pathutils.ES_SEARCH_PATH})
3149
  @rtype: list of L{objects.ExtStorage}
3150
  @return: a list of tuples (name, path, status, diagnose, parameters)
3151
      for all (potential) ExtStorage Providers under all
3152
      search paths, where:
3153
          - name is the (potential) ExtStorage Provider
3154
          - path is the full path to the ExtStorage Provider
3155
          - status True/False is the validity of the ExtStorage Provider
3156
          - diagnose is the error message for an invalid ExtStorage Provider,
3157
            otherwise empty
3158
          - parameters is a list of (name, help) parameters, if any
3159

3160
  """
3161
  if top_dirs is None:
3162
    top_dirs = pathutils.ES_SEARCH_PATH
3163

    
3164
  result = []
3165
  for dir_name in top_dirs:
3166
    if os.path.isdir(dir_name):
3167
      try:
3168
        f_names = utils.ListVisibleFiles(dir_name)
3169
      except EnvironmentError, err:
3170
        logging.exception("Can't list the ExtStorage directory %s: %s",
3171
                          dir_name, err)
3172
        break
3173
      for name in f_names:
3174
        es_path = utils.PathJoin(dir_name, name)
3175
        status, es_inst = bdev.ExtStorageFromDisk(name, base_dir=dir_name)
3176
        if status:
3177
          diagnose = ""
3178
          parameters = es_inst.supported_parameters
3179
        else:
3180
          diagnose = es_inst
3181
          parameters = []
3182
        result.append((name, es_path, status, diagnose, parameters))
3183

    
3184
  return result
3185

    
3186

    
3187
def BlockdevGrow(disk, amount, dryrun, backingstore, excl_stor):
3188
  """Grow a stack of block devices.
3189

3190
  This function is called recursively, with the childrens being the
3191
  first ones to resize.
3192

3193
  @type disk: L{objects.Disk}
3194
  @param disk: the disk to be grown
3195
  @type amount: integer
3196
  @param amount: the amount (in mebibytes) to grow with
3197
  @type dryrun: boolean
3198
  @param dryrun: whether to execute the operation in simulation mode
3199
      only, without actually increasing the size
3200
  @param backingstore: whether to execute the operation on backing storage
3201
      only, or on "logical" storage only; e.g. DRBD is logical storage,
3202
      whereas LVM, file, RBD are backing storage
3203
  @rtype: (status, result)
3204
  @type excl_stor: boolean
3205
  @param excl_stor: Whether exclusive_storage is active
3206
  @return: a tuple with the status of the operation (True/False), and
3207
      the errors message if status is False
3208

3209
  """
3210
  r_dev = _RecursiveFindBD(disk)
3211
  if r_dev is None:
3212
    _Fail("Cannot find block device %s", disk)
3213

    
3214
  try:
3215
    r_dev.Grow(amount, dryrun, backingstore, excl_stor)
3216
  except errors.BlockDeviceError, err:
3217
    _Fail("Failed to grow block device: %s", err, exc=True)
3218

    
3219

    
3220
def BlockdevSnapshot(disk):
3221
  """Create a snapshot copy of a block device.
3222

3223
  This function is called recursively, and the snapshot is actually created
3224
  just for the leaf lvm backend device.
3225

3226
  @type disk: L{objects.Disk}
3227
  @param disk: the disk to be snapshotted
3228
  @rtype: string
3229
  @return: snapshot disk ID as (vg, lv)
3230

3231
  """
3232
  if disk.dev_type == constants.DT_DRBD8:
3233
    if not disk.children:
3234
      _Fail("DRBD device '%s' without backing storage cannot be snapshotted",
3235
            disk.unique_id)
3236
    return BlockdevSnapshot(disk.children[0])
3237
  elif disk.dev_type == constants.DT_PLAIN:
3238
    r_dev = _RecursiveFindBD(disk)
3239
    if r_dev is not None:
3240
      # FIXME: choose a saner value for the snapshot size
3241
      # let's stay on the safe side and ask for the full size, for now
3242
      return r_dev.Snapshot(disk.size)
3243
    else:
3244
      _Fail("Cannot find block device %s", disk)
3245
  else:
3246
    _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
3247
          disk.unique_id, disk.dev_type)
3248

    
3249

    
3250
def BlockdevSetInfo(disk, info):
3251
  """Sets 'metadata' information on block devices.
3252

3253
  This function sets 'info' metadata on block devices. Initial
3254
  information is set at device creation; this function should be used
3255
  for example after renames.
3256

3257
  @type disk: L{objects.Disk}
3258
  @param disk: the disk to be grown
3259
  @type info: string
3260
  @param info: new 'info' metadata
3261
  @rtype: (status, result)
3262
  @return: a tuple with the status of the operation (True/False), and
3263
      the errors message if status is False
3264

3265
  """
3266
  r_dev = _RecursiveFindBD(disk)
3267
  if r_dev is None:
3268
    _Fail("Cannot find block device %s", disk)
3269

    
3270
  try:
3271
    r_dev.SetInfo(info)
3272
  except errors.BlockDeviceError, err:
3273
    _Fail("Failed to set information on block device: %s", err, exc=True)
3274

    
3275

    
3276
def FinalizeExport(instance, snap_disks):
3277
  """Write out the export configuration information.
3278

3279
  @type instance: L{objects.Instance}
3280
  @param instance: the instance which we export, used for
3281
      saving configuration
3282
  @type snap_disks: list of L{objects.Disk}
3283
  @param snap_disks: list of snapshot block devices, which
3284
      will be used to get the actual name of the dump file
3285

3286
  @rtype: None
3287

3288
  """
3289
  destdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name + ".new")
3290
  finaldestdir = utils.PathJoin(pathutils.EXPORT_DIR, instance.name)
3291

    
3292
  config = objects.SerializableConfigParser()
3293

    
3294
  config.add_section(constants.INISECT_EXP)
3295
  config.set(constants.INISECT_EXP, "version", "0")
3296
  config.set(constants.INISECT_EXP, "timestamp", "%d" % int(time.time()))
3297
  config.set(constants.INISECT_EXP, "source", instance.primary_node)
3298
  config.set(constants.INISECT_EXP, "os", instance.os)
3299
  config.set(constants.INISECT_EXP, "compression", "none")
3300

    
3301
  config.add_section(constants.INISECT_INS)
3302
  config.set(constants.INISECT_INS, "name", instance.name)
3303
  config.set(constants.INISECT_INS, "maxmem", "%d" %
3304
             instance.beparams[constants.BE_MAXMEM])
3305
  config.set(constants.INISECT_INS, "minmem", "%d" %
3306
             instance.beparams[constants.BE_MINMEM])
3307
  # "memory" is deprecated, but useful for exporting to old ganeti versions
3308
  config.set(constants.INISECT_INS, "memory", "%d" %
3309
             instance.beparams[constants.BE_MAXMEM])
3310
  config.set(constants.INISECT_INS, "vcpus", "%d" %
3311
             instance.beparams[constants.BE_VCPUS])
3312
  config.set(constants.INISECT_INS, "disk_template", instance.disk_template)
3313
  config.set(constants.INISECT_INS, "hypervisor", instance.hypervisor)
3314
  config.set(constants.INISECT_INS, "tags", " ".join(instance.GetTags()))
3315

    
3316
  nic_total = 0
3317
  for nic_count, nic in enumerate(instance.nics):
3318
    nic_total += 1
3319
    config.set(constants.INISECT_INS, "nic%d_mac" %
3320
               nic_count, "%s" % nic.mac)
3321
    config.set(constants.INISECT_INS, "nic%d_ip" % nic_count, "%s" % nic.ip)
3322
    config.set(constants.INISECT_INS, "nic%d_network" % nic_count,
3323
               "%s" % nic.network)
3324
    config.set(constants.INISECT_INS, "nic%d_name" % nic_count,
3325
               "%s" % nic.name)
3326
    for param in constants.NICS_PARAMETER_TYPES:
3327
      config.set(constants.INISECT_INS, "nic%d_%s" % (nic_count, param),
3328
                 "%s" % nic.nicparams.get(param, None))
3329
  # TODO: redundant: on load can read nics until it doesn't exist
3330
  config.set(constants.INISECT_INS, "nic_count", "%d" % nic_total)
3331

    
3332
  disk_total = 0
3333
  for disk_count, disk in enumerate(snap_disks):
3334
    if disk:
3335
      disk_total += 1
3336
      config.set(constants.INISECT_INS, "disk%d_ivname" % disk_count,
3337
                 ("%s" % disk.iv_name))
3338
      config.set(constants.INISECT_INS, "disk%d_dump" % disk_count,
3339
                 ("%s" % disk.logical_id[1]))
3340
      config.set(constants.INISECT_INS, "disk%d_size" % disk_count,
3341
                 ("%d" % disk.size))
3342
      config.set(constants.INISECT_INS, "disk%d_name" % disk_count,
3343
                 "%s" % disk.name)
3344

    
3345
  config.set(constants.INISECT_INS, "disk_count", "%d" % disk_total)
3346

    
3347
  # New-style hypervisor/backend parameters
3348

    
3349
  config.add_section(constants.INISECT_HYP)
3350
  for name, value in instance.hvparams.items():
3351
    if name not in constants.HVC_GLOBALS:
3352
      config.set(constants.INISECT_HYP, name, str(value))
3353

    
3354
  config.add_section(constants.INISECT_BEP)
3355
  for name, value in instance.beparams.items():
3356
    config.set(constants.INISECT_BEP, name, str(value))
3357

    
3358
  config.add_section(constants.INISECT_OSP)
3359
  for name, value in instance.osparams.items():
3360
    config.set(constants.INISECT_OSP, name, str(value))
3361

    
3362
  config.add_section(constants.INISECT_OSP_PRIVATE)
3363
  for name, value in instance.osparams_private.items():
3364
    config.set(constants.INISECT_OSP_PRIVATE, name, str(value.Get()))
3365

    
3366
  utils.WriteFile(utils.PathJoin(destdir, constants.EXPORT_CONF_FILE),
3367
                  data=config.Dumps())
3368
  shutil.rmtree(finaldestdir, ignore_errors=True)
3369
  shutil.move(destdir, finaldestdir)
3370

    
3371

    
3372
def ExportInfo(dest):
3373
  """Get export configuration information.
3374

3375
  @type dest: str
3376
  @param dest: directory containing the export
3377

3378
  @rtype: L{objects.SerializableConfigParser}
3379
  @return: a serializable config file containing the
3380
      export info
3381

3382
  """
3383
  cff = utils.PathJoin(dest, constants.EXPORT_CONF_FILE)
3384

    
3385
  config = objects.SerializableConfigParser()
3386
  config.read(cff)
3387

    
3388
  if (not config.has_section(constants.INISECT_EXP) or
3389
      not config.has_section(constants.INISECT_INS)):
3390
    _Fail("Export info file doesn't have the required fields")
3391

    
3392
  return config.Dumps()
3393

    
3394

    
3395
def ListExports():
3396
  """Return a list of exports currently available on this machine.
3397

3398
  @rtype: list
3399
  @return: list of the exports
3400

3401
  """
3402
  if os.path.isdir(pathutils.EXPORT_DIR):
3403
    return sorted(utils.ListVisibleFiles(pathutils.EXPORT_DIR))
3404
  else:
3405
    _Fail("No exports directory")
3406

    
3407

    
3408
def RemoveExport(export):
3409
  """Remove an existing export from the node.
3410

3411
  @type export: str
3412
  @param export: the name of the export to remove
3413
  @rtype: None
3414

3415
  """
3416
  target = utils.PathJoin(pathutils.EXPORT_DIR, export)
3417

    
3418
  try:
3419
    shutil.rmtree(target)
3420
  except EnvironmentError, err:
3421
    _Fail("Error while removing the export: %s", err, exc=True)
3422

    
3423

    
3424
def BlockdevRename(devlist):
3425
  """Rename a list of block devices.
3426

3427
  @type devlist: list of tuples
3428
  @param devlist: list of tuples of the form  (disk, new_unique_id); disk is
3429
      an L{objects.Disk} object describing the current disk, and new
3430
      unique_id is the name we rename it to
3431
  @rtype: boolean
3432
  @return: True if all renames succeeded, False otherwise
3433

3434
  """
3435
  msgs = []
3436
  result = True
3437
  for disk, unique_id in devlist:
3438
    dev = _RecursiveFindBD(disk)
3439
    if dev is None:
3440
      msgs.append("Can't find device %s in rename" % str(disk))
3441
      result = False
3442
      continue
3443
    try:
3444
      old_rpath = dev.dev_path
3445
      dev.Rename(unique_id)
3446
      new_rpath = dev.dev_path
3447
      if old_rpath != new_rpath:
3448
        DevCacheManager.RemoveCache(old_rpath)
3449
        # FIXME: we should add the new cache information here, like:
3450
        # DevCacheManager.UpdateCache(new_rpath, owner, ...)
3451
        # but we don't have the owner here - maybe parse from existing
3452
        # cache? for now, we only lose lvm data when we rename, which
3453
        # is less critical than DRBD or MD
3454
    except errors.BlockDeviceError, err:
3455
      msgs.append("Can't rename device '%s' to '%s': %s" %
3456
                  (dev, unique_id, err))
3457
      logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
3458
      result = False
3459
  if not result:
3460
    _Fail("; ".join(msgs))
3461

    
3462

    
3463
def _TransformFileStorageDir(fs_dir):
3464
  """Checks whether given file_storage_dir is valid.
3465

3466
  Checks wheter the given fs_dir is within the cluster-wide default
3467
  file_storage_dir or the shared_file_storage_dir, which are stored in
3468
  SimpleStore. Only paths under those directories are allowed.
3469

3470
  @type fs_dir: str
3471
  @param fs_dir: the path to check
3472

3473
  @return: the normalized path if valid, None otherwise
3474

3475
  """
3476
  filestorage.CheckFileStoragePath(fs_dir)
3477

    
3478
  return os.path.normpath(fs_dir)
3479

    
3480

    
3481
def CreateFileStorageDir(file_storage_dir):
3482
  """Create file storage directory.
3483

3484
  @type file_storage_dir: str
3485
  @param file_storage_dir: directory to create
3486

3487
  @rtype: tuple
3488
  @return: tuple with first element a boolean indicating wheter dir
3489
      creation was successful or not
3490

3491
  """
3492
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
3493
  if os.path.exists(file_storage_dir):
3494
    if not os.path.isdir(file_storage_dir):
3495
      _Fail("Specified storage dir '%s' is not a directory",
3496
            file_storage_dir)
3497
  else:
3498
    try:
3499
      os.makedirs(file_storage_dir, 0750)
3500
    except OSError, err:
3501
      _Fail("Cannot create file storage directory '%s': %s",
3502
            file_storage_dir, err, exc=True)
3503

    
3504

    
3505
def RemoveFileStorageDir(file_storage_dir):
3506
  """Remove file storage directory.
3507

3508
  Remove it only if it's empty. If not log an error and return.
3509

3510
  @type file_storage_dir: str
3511
  @param file_storage_dir: the directory we should cleanup
3512
  @rtype: tuple (success,)
3513
  @return: tuple of one element, C{success}, denoting
3514
      whether the operation was successful
3515

3516
  """
3517
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
3518
  if os.path.exists(file_storage_dir):
3519
    if not os.path.isdir(file_storage_dir):
3520
      _Fail("Specified Storage directory '%s' is not a directory",
3521
            file_storage_dir)
3522
    # deletes dir only if empty, otherwise we want to fail the rpc call
3523
    try:
3524
      os.rmdir(file_storage_dir)
3525
    except OSError, err:
3526
      _Fail("Cannot remove file storage directory '%s': %s",
3527
            file_storage_dir, err)
3528

    
3529

    
3530
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
3531
  """Rename the file storage directory.
3532

3533
  @type old_file_storage_dir: str
3534
  @param old_file_storage_dir: the current path
3535
  @type new_file_storage_dir: str
3536
  @param new_file_storage_dir: the name we should rename to
3537
  @rtype: tuple (success,)
3538
  @return: tuple of one element, C{success}, denoting
3539
      whether the operation was successful
3540

3541
  """
3542
  old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
3543
  new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
3544
  if not os.path.exists(new_file_storage_dir):
3545
    if os.path.isdir(old_file_storage_dir):
3546
      try:
3547
        os.rename(old_file_storage_dir, new_file_storage_dir)
3548
      except OSError, err:
3549
        _Fail("Cannot rename '%s' to '%s': %s",
3550
              old_file_storage_dir, new_file_storage_dir, err)
3551
    else:
3552
      _Fail("Specified storage dir '%s' is not a directory",
3553
            old_file_storage_dir)
3554
  else:
3555
    if os.path.exists(old_file_storage_dir):
3556
      _Fail("Cannot rename '%s' to '%s': both locations exist",
3557
            old_file_storage_dir, new_file_storage_dir)
3558

    
3559

    
3560
def _EnsureJobQueueFile(file_name):
3561
  """Checks whether the given filename is in the queue directory.
3562

3563
  @type file_name: str
3564
  @param file_name: the file name we should check
3565
  @rtype: None
3566
  @raises RPCFail: if the file is not valid
3567

3568
  """
3569
  if not utils.IsBelowDir(pathutils.QUEUE_DIR, file_name):
3570
    _Fail("Passed job queue file '%s' does not belong to"
3571
          " the queue directory '%s'", file_name, pathutils.QUEUE_DIR)
3572

    
3573

    
3574
def JobQueueUpdate(file_name, content):
3575
  """Updates a file in the queue directory.
3576

3577
  This is just a wrapper over L{utils.io.WriteFile}, with proper
3578
  checking.
3579

3580
  @type file_name: str
3581
  @param file_name: the job file name
3582
  @type content: str
3583
  @param content: the new job contents
3584
  @rtype: boolean
3585
  @return: the success of the operation
3586

3587
  """
3588
  file_name = vcluster.LocalizeVirtualPath(file_name)
3589

    
3590
  _EnsureJobQueueFile(file_name)
3591
  getents = runtime.GetEnts()
3592

    
3593
  # Write and replace the file atomically
3594
  utils.WriteFile(file_name, data=_Decompress(content), uid=getents.masterd_uid,
3595
                  gid=getents.daemons_gid, mode=constants.JOB_QUEUE_FILES_PERMS)
3596

    
3597

    
3598
def JobQueueRename(old, new):
3599
  """Renames a job queue file.
3600

3601
  This is just a wrapper over os.rename with proper checking.
3602

3603
  @type old: str
3604
  @param old: the old (actual) file name
3605
  @type new: str
3606
  @param new: the desired file name
3607
  @rtype: tuple
3608
  @return: the success of the operation and payload
3609

3610
  """
3611
  old = vcluster.LocalizeVirtualPath(old)
3612
  new = vcluster.LocalizeVirtualPath(new)
3613

    
3614
  _EnsureJobQueueFile(old)
3615
  _EnsureJobQueueFile(new)
3616

    
3617
  getents = runtime.GetEnts()
3618

    
3619
  utils.RenameFile(old, new, mkdir=True, mkdir_mode=0750,
3620
                   dir_uid=getents.masterd_uid, dir_gid=getents.daemons_gid)
3621

    
3622

    
3623
def BlockdevClose(instance_name, disks):
3624
  """Closes the given block devices.
3625

3626
  This means they will be switched to secondary mode (in case of
3627
  DRBD).
3628

3629
  @param instance_name: if the argument is not empty, the symlinks
3630
      of this instance will be removed
3631
  @type disks: list of L{objects.Disk}
3632
  @param disks: the list of disks to be closed
3633
  @rtype: tuple (success, message)
3634
  @return: a tuple of success and message, where success
3635
      indicates the succes of the operation, and message
3636
      which will contain the error details in case we
3637
      failed
3638

3639
  """
3640
  bdevs = []
3641
  for cf in disks:
3642
    rd = _RecursiveFindBD(cf)
3643
    if rd is None:
3644
      _Fail("Can't find device %s", cf)
3645
    bdevs.append(rd)
3646

    
3647
  msg = []
3648
  for rd in bdevs:
3649
    try:
3650
      rd.Close()
3651
    except errors.BlockDeviceError, err:
3652
      msg.append(str(err))
3653
  if msg:
3654
    _Fail("Can't make devices secondary: %s", ",".join(msg))
3655
  else:
3656
    if instance_name:
3657
      _RemoveBlockDevLinks(instance_name, disks)
3658

    
3659

    
3660
def ValidateHVParams(hvname, hvparams):
3661
  """Validates the given hypervisor parameters.
3662

3663
  @type hvname: string
3664
  @param hvname: the hypervisor name
3665
  @type hvparams: dict
3666
  @param hvparams: the hypervisor parameters to be validated
3667
  @rtype: None
3668

3669
  """
3670
  try:
3671
    hv_type = hypervisor.GetHypervisor(hvname)
3672
    hv_type.ValidateParameters(hvparams)
3673
  except errors.HypervisorError, err:
3674
    _Fail(str(err), log=False)
3675

    
3676

    
3677
def _CheckOSPList(os_obj, parameters):
3678
  """Check whether a list of parameters is supported by the OS.
3679

3680
  @type os_obj: L{objects.OS}
3681
  @param os_obj: OS object to check
3682
  @type parameters: list
3683
  @param parameters: the list of parameters to check
3684

3685
  """
3686
  supported = [v[0] for v in os_obj.supported_parameters]
3687
  delta = frozenset(parameters).difference(supported)
3688
  if delta:
3689
    _Fail("The following parameters are not supported"
3690
          " by the OS %s: %s" % (os_obj.name, utils.CommaJoin(delta)))
3691

    
3692

    
3693
def ValidateOS(required, osname, checks, osparams):
3694
  """Validate the given OS parameters.
3695

3696
  @type required: boolean
3697
  @param required: whether absence of the OS should translate into
3698
      failure or not
3699
  @type osname: string
3700
  @param osname: the OS to be validated
3701
  @type checks: list
3702
  @param checks: list of the checks to run (currently only 'parameters')
3703
  @type osparams: dict
3704
  @param osparams: dictionary with OS parameters, some of which may be
3705
                   private.
3706
  @rtype: boolean
3707
  @return: True if the validation passed, or False if the OS was not
3708
      found and L{required} was false
3709

3710
  """
3711
  if not constants.OS_VALIDATE_CALLS.issuperset(checks):
3712
    _Fail("Unknown checks required for OS %s: %s", osname,
3713
          set(checks).difference(constants.OS_VALIDATE_CALLS))
3714

    
3715
  name_only = objects.OS.GetName(osname)
3716
  status, tbv = _TryOSFromDisk(name_only, None)
3717

    
3718
  if not status:
3719
    if required:
3720
      _Fail(tbv)
3721
    else:
3722
      return False
3723

    
3724
  if max(tbv.api_versions) < constants.OS_API_V20:
3725
    return True
3726

    
3727
  if constants.OS_VALIDATE_PARAMETERS in checks:
3728
    _CheckOSPList(tbv, osparams.keys())
3729

    
3730
  validate_env = OSCoreEnv(osname, tbv, osparams)
3731
  result = utils.RunCmd([tbv.verify_script] + checks, env=validate_env,
3732
                        cwd=tbv.path, reset_env=True)
3733
  if result.failed:
3734
    logging.error("os validate command '%s' returned error: %s output: %s",
3735
                  result.cmd, result.fail_reason, result.output)
3736
    _Fail("OS validation script failed (%s), output: %s",
3737
          result.fail_reason, result.output, log=False)
3738

    
3739
  return True
3740

    
3741

    
3742
def DemoteFromMC():
3743
  """Demotes the current node from master candidate role.
3744

3745
  """
3746
  # try to ensure we're not the master by mistake
3747
  master, myself = ssconf.GetMasterAndMyself()
3748
  if master == myself:
3749
    _Fail("ssconf status shows I'm the master node, will not demote")
3750

    
3751
  result = utils.RunCmd([pathutils.DAEMON_UTIL, "check", constants.MASTERD])
3752
  if not result.failed:
3753
    _Fail("The master daemon is running, will not demote")
3754

    
3755
  try:
3756
    if os.path.isfile(pathutils.CLUSTER_CONF_FILE):
3757
      utils.CreateBackup(pathutils.CLUSTER_CONF_FILE)
3758
  except EnvironmentError, err:
3759
    if err.errno != errno.ENOENT:
3760
      _Fail("Error while backing up cluster file: %s", err, exc=True)
3761

    
3762
  utils.RemoveFile(pathutils.CLUSTER_CONF_FILE)
3763

    
3764

    
3765
def _GetX509Filenames(cryptodir, name):
3766
  """Returns the full paths for the private key and certificate.
3767

3768
  """
3769
  return (utils.PathJoin(cryptodir, name),
3770
          utils.PathJoin(cryptodir, name, _X509_KEY_FILE),
3771
          utils.PathJoin(cryptodir, name, _X509_CERT_FILE))
3772

    
3773

    
3774
def CreateX509Certificate(validity, cryptodir=pathutils.CRYPTO_KEYS_DIR):
3775
  """Creates a new X509 certificate for SSL/TLS.
3776

3777
  @type validity: int
3778
  @param validity: Validity in seconds
3779
  @rtype: tuple; (string, string)
3780
  @return: Certificate name and public part
3781

3782
  """
3783
  (key_pem, cert_pem) = \
3784
    utils.GenerateSelfSignedX509Cert(netutils.Hostname.GetSysName(),
3785
                                     min(validity, _MAX_SSL_CERT_VALIDITY), 1)
3786

    
3787
  cert_dir = tempfile.mkdtemp(dir=cryptodir,
3788
                              prefix="x509-%s-" % utils.TimestampForFilename())
3789
  try:
3790
    name = os.path.basename(cert_dir)
3791
    assert len(name) > 5
3792

    
3793
    (_, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
3794

    
3795
    utils.WriteFile(key_file, mode=0400, data=key_pem)
3796
    utils.WriteFile(cert_file, mode=0400, data=cert_pem)
3797

    
3798
    # Never return private key as it shouldn't leave the node
3799
    return (name, cert_pem)
3800
  except Exception:
3801
    shutil.rmtree(cert_dir, ignore_errors=True)
3802
    raise
3803

    
3804

    
3805
def RemoveX509Certificate(name, cryptodir=pathutils.CRYPTO_KEYS_DIR):
3806
  """Removes a X509 certificate.
3807

3808
  @type name: string
3809
  @param name: Certificate name
3810

3811
  """
3812
  (cert_dir, key_file, cert_file) = _GetX509Filenames(cryptodir, name)
3813

    
3814
  utils.RemoveFile(key_file)
3815
  utils.RemoveFile(cert_file)
3816

    
3817
  try:
3818
    os.rmdir(cert_dir)
3819
  except EnvironmentError, err:
3820
    _Fail("Cannot remove certificate directory '%s': %s",
3821
          cert_dir, err)
3822

    
3823

    
3824
def _GetImportExportIoCommand(instance, mode, ieio, ieargs):
3825
  """Returns the command for the requested input/output.
3826

3827
  @type instance: L{objects.Instance}
3828
  @param instance: The instance object
3829
  @param mode: Import/export mode
3830
  @param ieio: Input/output type
3831
  @param ieargs: Input/output arguments
3832

3833
  """
3834
  assert mode in (constants.IEM_IMPORT, constants.IEM_EXPORT)
3835

    
3836
  env = None
3837
  prefix = None
3838
  suffix = None
3839
  exp_size = None
3840

    
3841
  if ieio == constants.IEIO_FILE:
3842
    (filename, ) = ieargs
3843

    
3844
    if not utils.IsNormAbsPath(filename):
3845
      _Fail("Path '%s' is not normalized or absolute", filename)
3846

    
3847
    real_filename = os.path.realpath(filename)
3848
    directory = os.path.dirname(real_filename)
3849

    
3850
    if not utils.IsBelowDir(pathutils.EXPORT_DIR, real_filename):
3851
      _Fail("File '%s' is not under exports directory '%s': %s",
3852
            filename, pathutils.EXPORT_DIR, real_filename)
3853

    
3854
    # Create directory
3855
    utils.Makedirs(directory, mode=0750)
3856

    
3857
    quoted_filename = utils.ShellQuote(filename)
3858

    
3859
    if mode == constants.IEM_IMPORT:
3860
      suffix = "> %s" % quoted_filename
3861
    elif mode == constants.IEM_EXPORT:
3862
      suffix = "< %s" % quoted_filename
3863

    
3864
      # Retrieve file size
3865
      try:
3866
        st = os.stat(filename)
3867
      except EnvironmentError, err:
3868
        logging.error("Can't stat(2) %s: %s", filename, err)
3869
      else:
3870
        exp_size = utils.BytesToMebibyte(st.st_size)
3871

    
3872
  elif ieio == constants.IEIO_RAW_DISK:
3873
    (disk, ) = ieargs
3874

    
3875
    real_disk = _OpenRealBD(disk)
3876

    
3877
    if mode == constants.IEM_IMPORT:
3878
      # we use nocreat to fail if the device is not already there or we pass a
3879
      # wrong path; we use notrunc to no attempt truncate on an LV device
3880
      suffix = utils.BuildShellCmd("| dd of=%s conv=nocreat,notrunc bs=%s",
3881
                                   real_disk.dev_path,
3882
                                   str(1024 * 1024)) # 1 MB
3883

    
3884
    elif mode == constants.IEM_EXPORT:
3885
      # the block size on the read dd is 1MiB to match our units
3886
      prefix = utils.BuildShellCmd("dd if=%s bs=%s count=%s |",
3887
                                   real_disk.dev_path,
3888
                                   str(1024 * 1024), # 1 MB
3889
                                   str(disk.size))
3890
      exp_size = disk.size
3891

    
3892
  elif ieio == constants.IEIO_SCRIPT:
3893
    (disk, disk_index, ) = ieargs
3894

    
3895
    assert isinstance(disk_index, (int, long))
3896

    
3897
    inst_os = OSFromDisk(instance.os)
3898
    env = OSEnvironment(instance, inst_os)
3899

    
3900
    if mode == constants.IEM_IMPORT:
3901
      env["IMPORT_DEVICE"] = env["DISK_%d_PATH" % disk_index]
3902
      env["IMPORT_INDEX"] = str(disk_index)
3903
      script = inst_os.import_script
3904

    
3905
    elif mode == constants.IEM_EXPORT:
3906
      real_disk = _OpenRealBD(disk)
3907
      env["EXPORT_DEVICE"] = real_disk.dev_path
3908
      env["EXPORT_INDEX"] = str(disk_index)
3909
      script = inst_os.export_script
3910

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

    
3914
    if mode == constants.IEM_IMPORT:
3915
      suffix = "| %s" % script_cmd
3916

    
3917
    elif mode == constants.IEM_EXPORT:
3918
      prefix = "%s |" % script_cmd
3919

    
3920
    # Let script predict size
3921
    exp_size = constants.IE_CUSTOM_SIZE
3922

    
3923
  else:
3924
    _Fail("Invalid %s I/O mode %r", mode, ieio)
3925

    
3926
  return (env, prefix, suffix, exp_size)
3927

    
3928

    
3929
def _CreateImportExportStatusDir(prefix):
3930
  """Creates status directory for import/export.
3931

3932
  """
3933
  return tempfile.mkdtemp(dir=pathutils.IMPORT_EXPORT_DIR,
3934
                          prefix=("%s-%s-" %
3935
                                  (prefix, utils.TimestampForFilename())))
3936

    
3937

    
3938
def StartImportExportDaemon(mode, opts, host, port, instance, component,
3939
                            ieio, ieioargs):
3940
  """Starts an import or export daemon.
3941

3942
  @param mode: Import/output mode
3943
  @type opts: L{objects.ImportExportOptions}
3944
  @param opts: Daemon options
3945
  @type host: string
3946
  @param host: Remote host for export (None for import)
3947
  @type port: int
3948
  @param port: Remote port for export (None for import)
3949
  @type instance: L{objects.Instance}
3950
  @param instance: Instance object
3951
  @type component: string
3952
  @param component: which part of the instance is transferred now,
3953
      e.g. 'disk/0'
3954
  @param ieio: Input/output type
3955
  @param ieioargs: Input/output arguments
3956

3957
  """
3958
  if mode == constants.IEM_IMPORT:
3959
    prefix = "import"
3960

    
3961
    if not (host is None and port is None):
3962
      _Fail("Can not specify host or port on import")
3963

    
3964
  elif mode == constants.IEM_EXPORT:
3965
    prefix = "export"
3966

    
3967
    if host is None or port is None:
3968
      _Fail("Host and port must be specified for an export")
3969

    
3970
  else:
3971
    _Fail("Invalid mode %r", mode)
3972

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

    
3976
  (cmd_env, cmd_prefix, cmd_suffix, exp_size) = \
3977
    _GetImportExportIoCommand(instance, mode, ieio, ieioargs)
3978

    
3979
  if opts.key_name is None:
3980
    # Use server.pem
3981
    key_path = pathutils.NODED_CERT_FILE
3982
    cert_path = pathutils.NODED_CERT_FILE
3983
    assert opts.ca_pem is None
3984
  else:
3985
    (_, key_path, cert_path) = _GetX509Filenames(pathutils.CRYPTO_KEYS_DIR,
3986
                                                 opts.key_name)
3987
    assert opts.ca_pem is not None
3988

    
3989
  for i in [key_path, cert_path]:
3990
    if not os.path.exists(i):
3991
      _Fail("File '%s' does not exist" % i)
3992

    
3993
  status_dir = _CreateImportExportStatusDir("%s-%s" % (prefix, component))
3994
  try:
3995
    status_file = utils.PathJoin(status_dir, _IES_STATUS_FILE)
3996
    pid_file = utils.PathJoin(status_dir, _IES_PID_FILE)
3997
    ca_file = utils.PathJoin(status_dir, _IES_CA_FILE)
3998

    
3999
    if opts.ca_pem is None:
4000
      # Use server.pem
4001
      ca = utils.ReadFile(pathutils.NODED_CERT_FILE)
4002
    else:
4003
      ca = opts.ca_pem
4004

    
4005
    # Write CA file
4006
    utils.WriteFile(ca_file, data=ca, mode=0400)
4007

    
4008
    cmd = [
4009
      pathutils.IMPORT_EXPORT_DAEMON,
4010
      status_file, mode,
4011
      "--key=%s" % key_path,
4012
      "--cert=%s" % cert_path,
4013
      "--ca=%s" % ca_file,
4014
      ]
4015

    
4016
    if host:
4017
      cmd.append("--host=%s" % host)
4018

    
4019
    if port:
4020
      cmd.append("--port=%s" % port)
4021

    
4022
    if opts.ipv6:
4023
      cmd.append("--ipv6")
4024
    else:
4025
      cmd.append("--ipv4")
4026

    
4027
    if opts.compress:
4028
      cmd.append("--compress=%s" % opts.compress)
4029

    
4030
    if opts.magic:
4031
      cmd.append("--magic=%s" % opts.magic)
4032

    
4033
    if exp_size is not None:
4034
      cmd.append("--expected-size=%s" % exp_size)
4035

    
4036
    if cmd_prefix:
4037
      cmd.append("--cmd-prefix=%s" % cmd_prefix)
4038

    
4039
    if cmd_suffix:
4040
      cmd.append("--cmd-suffix=%s" % cmd_suffix)
4041

    
4042
    if mode == constants.IEM_EXPORT:
4043
      # Retry connection a few times when connecting to remote peer
4044
      cmd.append("--connect-retries=%s" % constants.RIE_CONNECT_RETRIES)
4045
      cmd.append("--connect-timeout=%s" % constants.RIE_CONNECT_ATTEMPT_TIMEOUT)
4046
    elif opts.connect_timeout is not None:
4047
      assert mode == constants.IEM_IMPORT
4048
      # Overall timeout for establishing connection while listening
4049
      cmd.append("--connect-timeout=%s" % opts.connect_timeout)
4050

    
4051
    logfile = _InstanceLogName(prefix, instance.os, instance.name, component)
4052

    
4053
    # TODO: Once _InstanceLogName uses tempfile.mkstemp, StartDaemon has
4054
    # support for receiving a file descriptor for output
4055
    utils.StartDaemon(cmd, env=cmd_env, pidfile=pid_file,
4056
                      output=logfile)
4057

    
4058
    # The import/export name is simply the status directory name
4059
    return os.path.basename(status_dir)
4060

    
4061
  except Exception:
4062
    shutil.rmtree(status_dir, ignore_errors=True)
4063
    raise
4064

    
4065

    
4066
def GetImportExportStatus(names):
4067
  """Returns import/export daemon status.
4068

4069
  @type names: sequence
4070
  @param names: List of names
4071
  @rtype: List of dicts
4072
  @return: Returns a list of the state of each named import/export or None if a
4073
           status couldn't be read
4074

4075
  """
4076
  result = []
4077

    
4078
  for name in names:
4079
    status_file = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name,
4080
                                 _IES_STATUS_FILE)
4081

    
4082
    try:
4083
      data = utils.ReadFile(status_file)
4084
    except EnvironmentError, err:
4085
      if err.errno != errno.ENOENT:
4086
        raise
4087
      data = None
4088

    
4089
    if not data:
4090
      result.append(None)
4091
      continue
4092

    
4093
    result.append(serializer.LoadJson(data))
4094

    
4095
  return result
4096

    
4097

    
4098
def AbortImportExport(name):
4099
  """Sends SIGTERM to a running import/export daemon.
4100

4101
  """
4102
  logging.info("Abort import/export %s", name)
4103

    
4104
  status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name)
4105
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
4106

    
4107
  if pid:
4108
    logging.info("Import/export %s is running with PID %s, sending SIGTERM",
4109
                 name, pid)
4110
    utils.IgnoreProcessNotFound(os.kill, pid, signal.SIGTERM)
4111

    
4112

    
4113
def CleanupImportExport(name):
4114
  """Cleanup after an import or export.
4115

4116
  If the import/export daemon is still running it's killed. Afterwards the
4117
  whole status directory is removed.
4118

4119
  """
4120
  logging.info("Finalizing import/export %s", name)
4121

    
4122
  status_dir = utils.PathJoin(pathutils.IMPORT_EXPORT_DIR, name)
4123

    
4124
  pid = utils.ReadLockedPidFile(utils.PathJoin(status_dir, _IES_PID_FILE))
4125

    
4126
  if pid:
4127
    logging.info("Import/export %s is still running with PID %s",
4128
                 name, pid)
4129
    utils.KillProcess(pid, waitpid=False)
4130

    
4131
  shutil.rmtree(status_dir, ignore_errors=True)
4132

    
4133

    
4134
def _FindDisks(disks):
4135
  """Finds attached L{BlockDev}s for the given disks.
4136

4137
  @type disks: list of L{objects.Disk}
4138
  @param disks: the disk objects we need to find
4139

4140
  @return: list of L{BlockDev} objects or C{None} if a given disk
4141
           was not found or was no attached.
4142

4143
  """
4144
  bdevs = []
4145

    
4146
  for disk in disks:
4147
    rd = _RecursiveFindBD(disk)
4148
    if rd is None:
4149
      _Fail("Can't find device %s", disk)
4150
    bdevs.append(rd)
4151
  return bdevs
4152

    
4153

    
4154
def DrbdDisconnectNet(disks):
4155
  """Disconnects the network on a list of drbd devices.
4156

4157
  """
4158
  bdevs = _FindDisks(disks)
4159

    
4160
  # disconnect disks
4161
  for rd in bdevs:
4162
    try:
4163
      rd.DisconnectNet()
4164
    except errors.BlockDeviceError, err:
4165
      _Fail("Can't change network configuration to standalone mode: %s",
4166
            err, exc=True)
4167

    
4168

    
4169
def DrbdAttachNet(disks, instance_name, multimaster):
4170
  """Attaches the network on a list of drbd devices.
4171

4172
  """
4173
  bdevs = _FindDisks(disks)
4174

    
4175
  if multimaster:
4176
    for idx, rd in enumerate(bdevs):
4177
      try:
4178
        _SymlinkBlockDev(instance_name, rd.dev_path, idx)
4179
      except EnvironmentError, err:
4180
        _Fail("Can't create symlink: %s", err)
4181
  # reconnect disks, switch to new master configuration and if
4182
  # needed primary mode
4183
  for rd in bdevs:
4184
    try:
4185
      rd.AttachNet(multimaster)
4186
    except errors.BlockDeviceError, err:
4187
      _Fail("Can't change network configuration: %s", err)
4188

    
4189
  # wait until the disks are connected; we need to retry the re-attach
4190
  # if the device becomes standalone, as this might happen if the one
4191
  # node disconnects and reconnects in a different mode before the
4192
  # other node reconnects; in this case, one or both of the nodes will
4193
  # decide it has wrong configuration and switch to standalone
4194

    
4195
  def _Attach():
4196
    all_connected = True
4197

    
4198
    for rd in bdevs:
4199
      stats = rd.GetProcStatus()
4200

    
4201
      if multimaster:
4202
        # In the multimaster case we have to wait explicitly until
4203
        # the resource is Connected and UpToDate/UpToDate, because
4204
        # we promote *both nodes* to primary directly afterwards.
4205
        # Being in resync is not enough, since there is a race during which we
4206
        # may promote a node with an Outdated disk to primary, effectively
4207
        # tearing down the connection.
4208
        all_connected = (all_connected and
4209
                         stats.is_connected and
4210
                         stats.is_disk_uptodate and
4211
                         stats.peer_disk_uptodate)
4212
      else:
4213
        all_connected = (all_connected and
4214
                         (stats.is_connected or stats.is_in_resync))
4215

    
4216
      if stats.is_standalone:
4217
        # peer had different config info and this node became
4218
        # standalone, even though this should not happen with the
4219
        # new staged way of changing disk configs
4220
        try:
4221
          rd.AttachNet(multimaster)
4222
        except errors.BlockDeviceError, err:
4223
          _Fail("Can't change network configuration: %s", err)
4224

    
4225
    if not all_connected:
4226
      raise utils.RetryAgain()
4227

    
4228
  try:
4229
    # Start with a delay of 100 miliseconds and go up to 5 seconds
4230
    utils.Retry(_Attach, (0.1, 1.5, 5.0), 2 * 60)
4231
  except utils.RetryTimeout:
4232
    _Fail("Timeout in disk reconnecting")
4233

    
4234
  if multimaster:
4235
    # change to primary mode
4236
    for rd in bdevs:
4237
      try:
4238
        rd.Open()
4239
      except errors.BlockDeviceError, err:
4240
        _Fail("Can't change to primary mode: %s", err)
4241

    
4242

    
4243
def DrbdWaitSync(disks):
4244
  """Wait until DRBDs have synchronized.
4245

4246
  """
4247
  def _helper(rd):
4248
    stats = rd.GetProcStatus()
4249
    if not (stats.is_connected or stats.is_in_resync):
4250
      raise utils.RetryAgain()
4251
    return stats
4252

    
4253
  bdevs = _FindDisks(disks)
4254

    
4255
  min_resync = 100
4256
  alldone = True
4257
  for rd in bdevs:
4258
    try:
4259
      # poll each second for 15 seconds
4260
      stats = utils.Retry(_helper, 1, 15, args=[rd])
4261
    except utils.RetryTimeout:
4262
      stats = rd.GetProcStatus()
4263
      # last check
4264
      if not (stats.is_connected or stats.is_in_resync):
4265
        _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
4266
    alldone = alldone and (not stats.is_in_resync)
4267
    if stats.sync_percent is not None:
4268
      min_resync = min(min_resync, stats.sync_percent)
4269

    
4270
  return (alldone, min_resync)
4271

    
4272

    
4273
def DrbdNeedsActivation(disks):
4274
  """Checks which of the passed disks needs activation and returns their UUIDs.
4275

4276
  """
4277
  faulty_disks = []
4278

    
4279
  for disk in disks:
4280
    rd = _RecursiveFindBD(disk)
4281
    if rd is None:
4282
      faulty_disks.append(disk)
4283
      continue
4284

    
4285
    stats = rd.GetProcStatus()
4286
    if stats.is_standalone or stats.is_diskless:
4287
      faulty_disks.append(disk)
4288

    
4289
  return [disk.uuid for disk in faulty_disks]
4290

    
4291

    
4292
def GetDrbdUsermodeHelper():
4293
  """Returns DRBD usermode helper currently configured.
4294

4295
  """
4296
  try:
4297
    return drbd.DRBD8.GetUsermodeHelper()
4298
  except errors.BlockDeviceError, err:
4299
    _Fail(str(err))
4300

    
4301

    
4302
def PowercycleNode(hypervisor_type, hvparams=None):
4303
  """Hard-powercycle the node.
4304

4305
  Because we need to return first, and schedule the powercycle in the
4306
  background, we won't be able to report failures nicely.
4307

4308
  """
4309
  hyper = hypervisor.GetHypervisor(hypervisor_type)
4310
  try:
4311
    pid = os.fork()
4312
  except OSError:
4313
    # if we can't fork, we'll pretend that we're in the child process
4314
    pid = 0
4315
  if pid > 0:
4316
    return "Reboot scheduled in 5 seconds"
4317
  # ensure the child is running on ram
4318
  try:
4319
    utils.Mlockall()
4320
  except Exception: # pylint: disable=W0703
4321
    pass
4322
  time.sleep(5)
4323
  hyper.PowercycleNode(hvparams=hvparams)
4324

    
4325

    
4326
def _VerifyRestrictedCmdName(cmd):
4327
  """Verifies a restricted command name.
4328

4329
  @type cmd: string
4330
  @param cmd: Command name
4331
  @rtype: tuple; (boolean, string or None)
4332
  @return: The tuple's first element is the status; if C{False}, the second
4333
    element is an error message string, otherwise it's C{None}
4334

4335
  """
4336
  if not cmd.strip():
4337
    return (False, "Missing command name")
4338

    
4339
  if os.path.basename(cmd) != cmd:
4340
    return (False, "Invalid command name")
4341

    
4342
  if not constants.EXT_PLUGIN_MASK.match(cmd):
4343
    return (False, "Command name contains forbidden characters")
4344

    
4345
  return (True, None)
4346

    
4347

    
4348
def _CommonRestrictedCmdCheck(path, owner):
4349
  """Common checks for restricted command file system directories and files.
4350

4351
  @type path: string
4352
  @param path: Path to check
4353
  @param owner: C{None} or tuple containing UID and GID
4354
  @rtype: tuple; (boolean, string or C{os.stat} result)
4355
  @return: The tuple's first element is the status; if C{False}, the second
4356
    element is an error message string, otherwise it's the result of C{os.stat}
4357

4358
  """
4359
  if owner is None:
4360
    # Default to root as owner
4361
    owner = (0, 0)
4362

    
4363
  try:
4364
    st = os.stat(path)
4365
  except EnvironmentError, err:
4366
    return (False, "Can't stat(2) '%s': %s" % (path, err))
4367

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

    
4371
  if (st.st_uid, st.st_gid) != owner:
4372
    (owner_uid, owner_gid) = owner
4373
    return (False, "'%s' is not owned by %s:%s" % (path, owner_uid, owner_gid))
4374

    
4375
  return (True, st)
4376

    
4377

    
4378
def _VerifyRestrictedCmdDirectory(path, _owner=None):
4379
  """Verifies restricted command directory.
4380

4381
  @type path: string
4382
  @param path: Path to check
4383
  @rtype: tuple; (boolean, string or None)
4384
  @return: The tuple's first element is the status; if C{False}, the second
4385
    element is an error message string, otherwise it's C{None}
4386

4387
  """
4388
  (status, value) = _CommonRestrictedCmdCheck(path, _owner)
4389

    
4390
  if not status:
4391
    return (False, value)
4392

    
4393
  if not stat.S_ISDIR(value.st_mode):
4394
    return (False, "Path '%s' is not a directory" % path)
4395

    
4396
  return (True, None)
4397

    
4398

    
4399
def _VerifyRestrictedCmd(path, cmd, _owner=None):
4400
  """Verifies a whole restricted command and returns its executable filename.
4401

4402
  @type path: string
4403
  @param path: Directory containing restricted commands
4404
  @type cmd: string
4405
  @param cmd: Command name
4406
  @rtype: tuple; (boolean, string)
4407
  @return: The tuple's first element is the status; if C{False}, the second
4408
    element is an error message string, otherwise the second element is the
4409
    absolute path to the executable
4410

4411
  """
4412
  executable = utils.PathJoin(path, cmd)
4413

    
4414
  (status, msg) = _CommonRestrictedCmdCheck(executable, _owner)
4415

    
4416
  if not status:
4417
    return (False, msg)
4418

    
4419
  if not utils.IsExecutable(executable):
4420
    return (False, "access(2) thinks '%s' can't be executed" % executable)
4421

    
4422
  return (True, executable)
4423

    
4424

    
4425
def _PrepareRestrictedCmd(path, cmd,
4426
                          _verify_dir=_VerifyRestrictedCmdDirectory,
4427
                          _verify_name=_VerifyRestrictedCmdName,
4428
                          _verify_cmd=_VerifyRestrictedCmd):
4429
  """Performs a number of tests on a restricted command.
4430

4431
  @type path: string
4432
  @param path: Directory containing restricted commands
4433
  @type cmd: string
4434
  @param cmd: Command name
4435
  @return: Same as L{_VerifyRestrictedCmd}
4436

4437
  """
4438
  # Verify the directory first
4439
  (status, msg) = _verify_dir(path)
4440
  if status:
4441
    # Check command if everything was alright
4442
    (status, msg) = _verify_name(cmd)
4443

    
4444
  if not status:
4445
    return (False, msg)
4446

    
4447
  # Check actual executable
4448
  return _verify_cmd(path, cmd)
4449

    
4450

    
4451
def RunRestrictedCmd(cmd,
4452
                     _lock_timeout=_RCMD_LOCK_TIMEOUT,
4453
                     _lock_file=pathutils.RESTRICTED_COMMANDS_LOCK_FILE,
4454
                     _path=pathutils.RESTRICTED_COMMANDS_DIR,
4455
                     _sleep_fn=time.sleep,
4456
                     _prepare_fn=_PrepareRestrictedCmd,
4457
                     _runcmd_fn=utils.RunCmd,
4458
                     _enabled=constants.ENABLE_RESTRICTED_COMMANDS):
4459
  """Executes a restricted command after performing strict tests.
4460

4461
  @type cmd: string
4462
  @param cmd: Command name
4463
  @rtype: string
4464
  @return: Command output
4465
  @raise RPCFail: In case of an error
4466

4467
  """
4468
  logging.info("Preparing to run restricted command '%s'", cmd)
4469

    
4470
  if not _enabled:
4471
    _Fail("Restricted commands disabled at configure time")
4472

    
4473
  lock = None
4474
  try:
4475
    cmdresult = None
4476
    try:
4477
      lock = utils.FileLock.Open(_lock_file)
4478
      lock.Exclusive(blocking=True, timeout=_lock_timeout)
4479

    
4480
      (status, value) = _prepare_fn(_path, cmd)
4481

    
4482
      if status:
4483
        cmdresult = _runcmd_fn([value], env={}, reset_env=True,
4484
                               postfork_fn=lambda _: lock.Unlock())
4485
      else:
4486
        logging.error(value)
4487
    except Exception: # pylint: disable=W0703
4488
      # Keep original error in log
4489
      logging.exception("Caught exception")
4490

    
4491
    if cmdresult is None:
4492
      logging.info("Sleeping for %0.1f seconds before returning",
4493
                   _RCMD_INVALID_DELAY)
4494
      _sleep_fn(_RCMD_INVALID_DELAY)
4495

    
4496
      # Do not include original error message in returned error
4497
      _Fail("Executing command '%s' failed" % cmd)
4498
    elif cmdresult.failed or cmdresult.fail_reason:
4499
      _Fail("Restricted command '%s' failed: %s; output: %s",
4500
            cmd, cmdresult.fail_reason, cmdresult.output)
4501
    else:
4502
      return cmdresult.output
4503
  finally:
4504
    if lock is not None:
4505
      # Release lock at last
4506
      lock.Close()
4507
      lock = None
4508

    
4509

    
4510
def SetWatcherPause(until, _filename=pathutils.WATCHER_PAUSEFILE):
4511
  """Creates or removes the watcher pause file.
4512

4513
  @type until: None or number
4514
  @param until: Unix timestamp saying until when the watcher shouldn't run
4515

4516
  """
4517
  if until is None:
4518
    logging.info("Received request to no longer pause watcher")
4519
    utils.RemoveFile(_filename)
4520
  else:
4521
    logging.info("Received request to pause watcher until %s", until)
4522

    
4523
    if not ht.TNumber(until):
4524
      _Fail("Duration must be numeric")
4525

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

    
4528

    
4529
def ConfigureOVS(ovs_name, ovs_link):
4530
  """Creates a OpenvSwitch on the node.
4531

4532
  This function sets up a OpenvSwitch on the node with given name nad
4533
  connects it via a given eth device.
4534

4535
  @type ovs_name: string
4536
  @param ovs_name: Name of the OpenvSwitch to create.
4537
  @type ovs_link: None or string
4538
  @param ovs_link: Ethernet device for outside connection (can be missing)
4539

4540
  """
4541
  # Initialize the OpenvSwitch
4542
  result = utils.RunCmd(["ovs-vsctl", "add-br", ovs_name])
4543
  if result.failed:
4544
    _Fail("Failed to create openvswitch. Script return value: %s, output: '%s'"
4545
          % (result.exit_code, result.output), log=True)
4546

    
4547
  # And connect it to a physical interface, if given
4548
  if ovs_link:
4549
    result = utils.RunCmd(["ovs-vsctl", "add-port", ovs_name, ovs_link])
4550
    if result.failed:
4551
      _Fail("Failed to connect openvswitch to  interface %s. Script return"
4552
            " value: %s, output: '%s'" % (ovs_link, result.exit_code,
4553
            result.output), log=True)
4554

    
4555

    
4556
class HooksRunner(object):
4557
  """Hook runner.
4558

4559
  This class is instantiated on the node side (ganeti-noded) and not
4560
  on the master side.
4561

4562
  """
4563
  def __init__(self, hooks_base_dir=None):
4564
    """Constructor for hooks runner.
4565

4566
    @type hooks_base_dir: str or None
4567
    @param hooks_base_dir: if not None, this overrides the
4568
        L{pathutils.HOOKS_BASE_DIR} (useful for unittests)
4569

4570
    """
4571
    if hooks_base_dir is None:
4572
      hooks_base_dir = pathutils.HOOKS_BASE_DIR
4573
    # yeah, _BASE_DIR is not valid for attributes, we use it like a
4574
    # constant
4575
    self._BASE_DIR = hooks_base_dir # pylint: disable=C0103
4576

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

4580
    """
4581
    assert len(node_list) == 1
4582
    node = node_list[0]
4583
    _, myself = ssconf.GetMasterAndMyself()
4584
    assert node == myself
4585

    
4586
    results = self.RunHooks(hpath, phase, env)
4587

    
4588
    # Return values in the form expected by HooksMaster
4589
    return {node: (None, False, results)}
4590

    
4591
  def RunHooks(self, hpath, phase, env):
4592
    """Run the scripts in the hooks directory.
4593

4594
    @type hpath: str
4595
    @param hpath: the path to the hooks directory which
4596
        holds the scripts
4597
    @type phase: str
4598
    @param phase: either L{constants.HOOKS_PHASE_PRE} or
4599
        L{constants.HOOKS_PHASE_POST}
4600
    @type env: dict
4601
    @param env: dictionary with the environment for the hook
4602
    @rtype: list
4603
    @return: list of 3-element tuples:
4604
      - script path
4605
      - script result, either L{constants.HKR_SUCCESS} or
4606
        L{constants.HKR_FAIL}
4607
      - output of the script
4608

4609
    @raise errors.ProgrammerError: for invalid input
4610
        parameters
4611

4612
    """
4613
    if phase == constants.HOOKS_PHASE_PRE:
4614
      suffix = "pre"
4615
    elif phase == constants.HOOKS_PHASE_POST:
4616
      suffix = "post"
4617
    else:
4618
      _Fail("Unknown hooks phase '%s'", phase)
4619

    
4620
    subdir = "%s-%s.d" % (hpath, suffix)
4621
    dir_name = utils.PathJoin(self._BASE_DIR, subdir)
4622

    
4623
    results = []
4624

    
4625
    if not os.path.isdir(dir_name):
4626
      # for non-existing/non-dirs, we simply exit instead of logging a
4627
      # warning at every operation
4628
      return results
4629

    
4630
    runparts_results = utils.RunParts(dir_name, env=env, reset_env=True)
4631

    
4632
    for (relname, relstatus, runresult) in runparts_results:
4633
      if relstatus == constants.RUNPARTS_SKIP:
4634
        rrval = constants.HKR_SKIP
4635
        output = ""
4636
      elif relstatus == constants.RUNPARTS_ERR:
4637
        rrval = constants.HKR_FAIL
4638
        output = "Hook script execution error: %s" % runresult
4639
      elif relstatus == constants.RUNPARTS_RUN:
4640
        if runresult.failed:
4641
          rrval = constants.HKR_FAIL
4642
        else:
4643
          rrval = constants.HKR_SUCCESS
4644
        output = utils.SafeEncode(runresult.output.strip())
4645
      results.append(("%s/%s" % (subdir, relname), rrval, output))
4646

    
4647
    return results
4648

    
4649

    
4650
class IAllocatorRunner(object):
4651
  """IAllocator runner.
4652

4653
  This class is instantiated on the node side (ganeti-noded) and not on
4654
  the master side.
4655

4656
  """
4657
  @staticmethod
4658
  def Run(name, idata, ial_params):
4659
    """Run an iallocator script.
4660

4661
    @type name: str
4662
    @param name: the iallocator script name
4663
    @type idata: str
4664
    @param idata: the allocator input data
4665
    @type ial_params: list
4666
    @param ial_params: the iallocator parameters
4667

4668
    @rtype: tuple
4669
    @return: two element tuple of:
4670
       - status
4671
       - either error message or stdout of allocator (for success)
4672

4673
    """
4674
    alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
4675
                                  os.path.isfile)
4676
    if alloc_script is None:
4677
      _Fail("iallocator module '%s' not found in the search path", name)
4678

    
4679
    fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
4680
    try:
4681
      os.write(fd, idata)
4682
      os.close(fd)
4683
      result = utils.RunCmd([alloc_script, fin_name] + ial_params)
4684
      if result.failed:
4685
        _Fail("iallocator module '%s' failed: %s, output '%s'",
4686
              name, result.fail_reason, result.output)
4687
    finally:
4688
      os.unlink(fin_name)
4689

    
4690
    return result.stdout
4691

    
4692

    
4693
class DevCacheManager(object):
4694
  """Simple class for managing a cache of block device information.
4695

4696
  """
4697
  _DEV_PREFIX = "/dev/"
4698
  _ROOT_DIR = pathutils.BDEV_CACHE_DIR
4699

    
4700
  @classmethod
4701
  def _ConvertPath(cls, dev_path):
4702
    """Converts a /dev/name path to the cache file name.
4703

4704
    This replaces slashes with underscores and strips the /dev
4705
    prefix. It then returns the full path to the cache file.
4706

4707
    @type dev_path: str
4708
    @param dev_path: the C{/dev/} path name
4709
    @rtype: str
4710
    @return: the converted path name
4711

4712
    """
4713
    if dev_path.startswith(cls._DEV_PREFIX):
4714
      dev_path = dev_path[len(cls._DEV_PREFIX):]
4715
    dev_path = dev_path.replace("/", "_")
4716
    fpath = utils.PathJoin(cls._ROOT_DIR, "bdev_%s" % dev_path)
4717
    return fpath
4718

    
4719
  @classmethod
4720
  def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
4721
    """Updates the cache information for a given device.
4722

4723
    @type dev_path: str
4724
    @param dev_path: the pathname of the device
4725
    @type owner: str
4726
    @param owner: the owner (instance name) of the device
4727
    @type on_primary: bool
4728
    @param on_primary: whether this is the primary
4729
        node nor not
4730
    @type iv_name: str
4731
    @param iv_name: the instance-visible name of the
4732
        device, as in objects.Disk.iv_name
4733

4734
    @rtype: None
4735

4736
    """
4737
    if dev_path is None:
4738
      logging.error("DevCacheManager.UpdateCache got a None dev_path")
4739
      return
4740
    fpath = cls._ConvertPath(dev_path)
4741
    if on_primary:
4742
      state = "primary"
4743
    else:
4744
      state = "secondary"
4745
    if iv_name is None:
4746
      iv_name = "not_visible"
4747
    fdata = "%s %s %s\n" % (str(owner), state, iv_name)
4748
    try:
4749
      utils.WriteFile(fpath, data=fdata)
4750
    except EnvironmentError, err:
4751
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
4752

    
4753
  @classmethod
4754
  def RemoveCache(cls, dev_path):
4755
    """Remove data for a dev_path.
4756

4757
    This is just a wrapper over L{utils.io.RemoveFile} with a converted
4758
    path name and logging.
4759

4760
    @type dev_path: str
4761
    @param dev_path: the pathname of the device
4762

4763
    @rtype: None
4764

4765
    """
4766
    if dev_path is None:
4767
      logging.error("DevCacheManager.RemoveCache got a None dev_path")
4768
      return
4769
    fpath = cls._ConvertPath(dev_path)
4770
    try:
4771
      utils.RemoveFile(fpath)
4772
    except EnvironmentError, err:
4773
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)