Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ 6b405598

History | View | Annotate | Download (80.4 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007 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

    
25
import os
26
import os.path
27
import shutil
28
import time
29
import stat
30
import errno
31
import re
32
import subprocess
33
import random
34
import logging
35
import tempfile
36
import zlib
37
import base64
38

    
39
from ganeti import errors
40
from ganeti import utils
41
from ganeti import ssh
42
from ganeti import hypervisor
43
from ganeti import constants
44
from ganeti import bdev
45
from ganeti import objects
46
from ganeti import ssconf
47

    
48

    
49
def _GetConfig():
50
  """Simple wrapper to return a SimpleStore.
51

52
  @rtype: L{ssconf.SimpleStore}
53
  @return: a SimpleStore instance
54

55
  """
56
  return ssconf.SimpleStore()
57

    
58

    
59
def _GetSshRunner(cluster_name):
60
  """Simple wrapper to return an SshRunner.
61

62
  @type cluster_name: str
63
  @param cluster_name: the cluster name, which is needed
64
      by the SshRunner constructor
65
  @rtype: L{ssh.SshRunner}
66
  @return: an SshRunner instance
67

68
  """
69
  return ssh.SshRunner(cluster_name)
70

    
71

    
72
def _Decompress(data):
73
  """Unpacks data compressed by the RPC client.
74

75
  @type data: list or tuple
76
  @param data: Data sent by RPC client
77
  @rtype: str
78
  @return: Decompressed data
79

80
  """
81
  assert isinstance(data, (list, tuple))
82
  assert len(data) == 2
83
  (encoding, content) = data
84
  if encoding == constants.RPC_ENCODING_NONE:
85
    return content
86
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
87
    return zlib.decompress(base64.b64decode(content))
88
  else:
89
    raise AssertionError("Unknown data encoding")
90

    
91

    
92
def _CleanDirectory(path, exclude=None):
93
  """Removes all regular files in a directory.
94

95
  @type path: str
96
  @param path: the directory to clean
97
  @type exclude: list
98
  @param exclude: list of files to be excluded, defaults
99
      to the empty list
100

101
  """
102
  if not os.path.isdir(path):
103
    return
104
  if exclude is None:
105
    exclude = []
106
  else:
107
    # Normalize excluded paths
108
    exclude = [os.path.normpath(i) for i in exclude]
109

    
110
  for rel_name in utils.ListVisibleFiles(path):
111
    full_name = os.path.normpath(os.path.join(path, rel_name))
112
    if full_name in exclude:
113
      continue
114
    if os.path.isfile(full_name) and not os.path.islink(full_name):
115
      utils.RemoveFile(full_name)
116

    
117

    
118
def JobQueuePurge():
119
  """Removes job queue files and archived jobs.
120

121
  @rtype: None
122

123
  """
124
  _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE])
125
  _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
126

    
127

    
128
def GetMasterInfo():
129
  """Returns master information.
130

131
  This is an utility function to compute master information, either
132
  for consumption here or from the node daemon.
133

134
  @rtype: tuple
135
  @return: (master_netdev, master_ip, master_name) if we have a good
136
      configuration, otherwise (None, None, None)
137

138
  """
139
  try:
140
    cfg = _GetConfig()
141
    master_netdev = cfg.GetMasterNetdev()
142
    master_ip = cfg.GetMasterIP()
143
    master_node = cfg.GetMasterNode()
144
  except errors.ConfigurationError, err:
145
    logging.exception("Cluster configuration incomplete")
146
    return (None, None, None)
147
  return (master_netdev, master_ip, master_node)
148

    
149

    
150
def StartMaster(start_daemons):
151
  """Activate local node as master node.
152

153
  The function will always try activate the IP address of the master
154
  (unless someone else has it). It will also start the master daemons,
155
  based on the start_daemons parameter.
156

157
  @type start_daemons: boolean
158
  @param start_daemons: whther to also start the master
159
      daemons (ganeti-masterd and ganeti-rapi)
160
  @rtype: None
161

162
  """
163
  ok = True
164
  master_netdev, master_ip, _ = GetMasterInfo()
165
  if not master_netdev:
166
    return False
167

    
168
  if utils.TcpPing(master_ip, constants.DEFAULT_NODED_PORT):
169
    if utils.OwnIpAddress(master_ip):
170
      # we already have the ip:
171
      logging.debug("Already started")
172
    else:
173
      logging.error("Someone else has the master ip, not activating")
174
      ok = False
175
  else:
176
    result = utils.RunCmd(["ip", "address", "add", "%s/32" % master_ip,
177
                           "dev", master_netdev, "label",
178
                           "%s:0" % master_netdev])
179
    if result.failed:
180
      logging.error("Can't activate master IP: %s", result.output)
181
      ok = False
182

    
183
    result = utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_netdev,
184
                           "-s", master_ip, master_ip])
185
    # we'll ignore the exit code of arping
186

    
187
  # and now start the master and rapi daemons
188
  if start_daemons:
189
    for daemon in 'ganeti-masterd', 'ganeti-rapi':
190
      result = utils.RunCmd([daemon])
191
      if result.failed:
192
        logging.error("Can't start daemon %s: %s", daemon, result.output)
193
        ok = False
194
  return ok
195

    
196

    
197
def StopMaster(stop_daemons):
198
  """Deactivate this node as master.
199

200
  The function will always try to deactivate the IP address of the
201
  master. It will also stop the master daemons depending on the
202
  stop_daemons parameter.
203

204
  @type stop_daemons: boolean
205
  @param stop_daemons: whether to also stop the master daemons
206
      (ganeti-masterd and ganeti-rapi)
207
  @rtype: None
208

209
  """
210
  master_netdev, master_ip, _ = GetMasterInfo()
211
  if not master_netdev:
212
    return False
213

    
214
  result = utils.RunCmd(["ip", "address", "del", "%s/32" % master_ip,
215
                         "dev", master_netdev])
216
  if result.failed:
217
    logging.error("Can't remove the master IP, error: %s", result.output)
218
    # but otherwise ignore the failure
219

    
220
  if stop_daemons:
221
    # stop/kill the rapi and the master daemon
222
    for daemon in constants.RAPI_PID, constants.MASTERD_PID:
223
      utils.KillProcess(utils.ReadPidFile(utils.DaemonPidFileName(daemon)))
224

    
225
  return True
226

    
227

    
228
def AddNode(dsa, dsapub, rsa, rsapub, sshkey, sshpub):
229
  """Joins this node to the cluster.
230

231
  This does the following:
232
      - updates the hostkeys of the machine (rsa and dsa)
233
      - adds the ssh private key to the user
234
      - adds the ssh public key to the users' authorized_keys file
235

236
  @type dsa: str
237
  @param dsa: the DSA private key to write
238
  @type dsapub: str
239
  @param dsapub: the DSA public key to write
240
  @type rsa: str
241
  @param rsa: the RSA private key to write
242
  @type rsapub: str
243
  @param rsapub: the RSA public key to write
244
  @type sshkey: str
245
  @param sshkey: the SSH private key to write
246
  @type sshpub: str
247
  @param sshpub: the SSH public key to write
248
  @rtype: boolean
249
  @return: the success of the operation
250

251
  """
252
  sshd_keys =  [(constants.SSH_HOST_RSA_PRIV, rsa, 0600),
253
                (constants.SSH_HOST_RSA_PUB, rsapub, 0644),
254
                (constants.SSH_HOST_DSA_PRIV, dsa, 0600),
255
                (constants.SSH_HOST_DSA_PUB, dsapub, 0644)]
256
  for name, content, mode in sshd_keys:
257
    utils.WriteFile(name, data=content, mode=mode)
258

    
259
  try:
260
    priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS,
261
                                                    mkdir=True)
262
  except errors.OpExecError, err:
263
    msg = "Error while processing user ssh files"
264
    logging.exception(msg)
265
    return (False, "%s: %s" % (msg, err))
266

    
267
  for name, content in [(priv_key, sshkey), (pub_key, sshpub)]:
268
    utils.WriteFile(name, data=content, mode=0600)
269

    
270
  utils.AddAuthorizedKey(auth_keys, sshpub)
271

    
272
  utils.RunCmd([constants.SSH_INITD_SCRIPT, "restart"])
273

    
274
  return (True, "Node added successfully")
275

    
276

    
277
def LeaveCluster():
278
  """Cleans up and remove the current node.
279

280
  This function cleans up and prepares the current node to be removed
281
  from the cluster.
282

283
  If processing is successful, then it raises an
284
  L{errors.QuitGanetiException} which is used as a special case to
285
  shutdown the node daemon.
286

287
  """
288
  _CleanDirectory(constants.DATA_DIR)
289
  JobQueuePurge()
290

    
291
  try:
292
    priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
293
  except errors.OpExecError:
294
    logging.exception("Error while processing ssh files")
295
    return
296

    
297
  f = open(pub_key, 'r')
298
  try:
299
    utils.RemoveAuthorizedKey(auth_keys, f.read(8192))
300
  finally:
301
    f.close()
302

    
303
  utils.RemoveFile(priv_key)
304
  utils.RemoveFile(pub_key)
305

    
306
  # Return a reassuring string to the caller, and quit
307
  raise errors.QuitGanetiException(False, 'Shutdown scheduled')
308

    
309

    
310
def GetNodeInfo(vgname, hypervisor_type):
311
  """Gives back a hash with different informations about the node.
312

313
  @type vgname: C{string}
314
  @param vgname: the name of the volume group to ask for disk space information
315
  @type hypervisor_type: C{str}
316
  @param hypervisor_type: the name of the hypervisor to ask for
317
      memory information
318
  @rtype: C{dict}
319
  @return: dictionary with the following keys:
320
      - vg_size is the size of the configured volume group in MiB
321
      - vg_free is the free size of the volume group in MiB
322
      - memory_dom0 is the memory allocated for domain0 in MiB
323
      - memory_free is the currently available (free) ram in MiB
324
      - memory_total is the total number of ram in MiB
325

326
  """
327
  outputarray = {}
328
  vginfo = _GetVGInfo(vgname)
329
  outputarray['vg_size'] = vginfo['vg_size']
330
  outputarray['vg_free'] = vginfo['vg_free']
331

    
332
  hyper = hypervisor.GetHypervisor(hypervisor_type)
333
  hyp_info = hyper.GetNodeInfo()
334
  if hyp_info is not None:
335
    outputarray.update(hyp_info)
336

    
337
  f = open("/proc/sys/kernel/random/boot_id", 'r')
338
  try:
339
    outputarray["bootid"] = f.read(128).rstrip("\n")
340
  finally:
341
    f.close()
342

    
343
  return outputarray
344

    
345

    
346
def VerifyNode(what, cluster_name):
347
  """Verify the status of the local node.
348

349
  Based on the input L{what} parameter, various checks are done on the
350
  local node.
351

352
  If the I{filelist} key is present, this list of
353
  files is checksummed and the file/checksum pairs are returned.
354

355
  If the I{nodelist} key is present, we check that we have
356
  connectivity via ssh with the target nodes (and check the hostname
357
  report).
358

359
  If the I{node-net-test} key is present, we check that we have
360
  connectivity to the given nodes via both primary IP and, if
361
  applicable, secondary IPs.
362

363
  @type what: C{dict}
364
  @param what: a dictionary of things to check:
365
      - filelist: list of files for which to compute checksums
366
      - nodelist: list of nodes we should check ssh communication with
367
      - node-net-test: list of nodes we should check node daemon port
368
        connectivity with
369
      - hypervisor: list with hypervisors to run the verify for
370
  @rtype: dict
371
  @return: a dictionary with the same keys as the input dict, and
372
      values representing the result of the checks
373

374
  """
375
  result = {}
376

    
377
  if constants.NV_HYPERVISOR in what:
378
    result[constants.NV_HYPERVISOR] = tmp = {}
379
    for hv_name in what[constants.NV_HYPERVISOR]:
380
      tmp[hv_name] = hypervisor.GetHypervisor(hv_name).Verify()
381

    
382
  if constants.NV_FILELIST in what:
383
    result[constants.NV_FILELIST] = utils.FingerprintFiles(
384
      what[constants.NV_FILELIST])
385

    
386
  if constants.NV_NODELIST in what:
387
    result[constants.NV_NODELIST] = tmp = {}
388
    random.shuffle(what[constants.NV_NODELIST])
389
    for node in what[constants.NV_NODELIST]:
390
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
391
      if not success:
392
        tmp[node] = message
393

    
394
  if constants.NV_NODENETTEST in what:
395
    result[constants.NV_NODENETTEST] = tmp = {}
396
    my_name = utils.HostInfo().name
397
    my_pip = my_sip = None
398
    for name, pip, sip in what[constants.NV_NODENETTEST]:
399
      if name == my_name:
400
        my_pip = pip
401
        my_sip = sip
402
        break
403
    if not my_pip:
404
      tmp[my_name] = ("Can't find my own primary/secondary IP"
405
                      " in the node list")
406
    else:
407
      port = utils.GetNodeDaemonPort()
408
      for name, pip, sip in what[constants.NV_NODENETTEST]:
409
        fail = []
410
        if not utils.TcpPing(pip, port, source=my_pip):
411
          fail.append("primary")
412
        if sip != pip:
413
          if not utils.TcpPing(sip, port, source=my_sip):
414
            fail.append("secondary")
415
        if fail:
416
          tmp[name] = ("failure using the %s interface(s)" %
417
                       " and ".join(fail))
418

    
419
  if constants.NV_LVLIST in what:
420
    result[constants.NV_LVLIST] = GetVolumeList(what[constants.NV_LVLIST])
421

    
422
  if constants.NV_INSTANCELIST in what:
423
    result[constants.NV_INSTANCELIST] = GetInstanceList(
424
      what[constants.NV_INSTANCELIST])
425

    
426
  if constants.NV_VGLIST in what:
427
    result[constants.NV_VGLIST] = ListVolumeGroups()
428

    
429
  if constants.NV_VERSION in what:
430
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
431
                                    constants.RELEASE_VERSION)
432

    
433
  if constants.NV_HVINFO in what:
434
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
435
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
436

    
437
  if constants.NV_DRBDLIST in what:
438
    try:
439
      used_minors = bdev.DRBD8.GetUsedDevs().keys()
440
    except errors.BlockDeviceError, err:
441
      logging.warning("Can't get used minors list", exc_info=True)
442
      used_minors = str(err)
443
    result[constants.NV_DRBDLIST] = used_minors
444

    
445
  return result
446

    
447

    
448
def GetVolumeList(vg_name):
449
  """Compute list of logical volumes and their size.
450

451
  @type vg_name: str
452
  @param vg_name: the volume group whose LVs we should list
453
  @rtype: dict
454
  @return:
455
      dictionary of all partions (key) with value being a tuple of
456
      their size (in MiB), inactive and online status::
457

458
        {'test1': ('20.06', True, True)}
459

460
      in case of errors, a string is returned with the error
461
      details.
462

463
  """
464
  lvs = {}
465
  sep = '|'
466
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
467
                         "--separator=%s" % sep,
468
                         "-olv_name,lv_size,lv_attr", vg_name])
469
  if result.failed:
470
    logging.error("Failed to list logical volumes, lvs output: %s",
471
                  result.output)
472
    return result.output
473

    
474
  valid_line_re = re.compile("^ *([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
475
  for line in result.stdout.splitlines():
476
    line = line.strip()
477
    match = valid_line_re.match(line)
478
    if not match:
479
      logging.error("Invalid line returned from lvs output: '%s'", line)
480
      continue
481
    name, size, attr = match.groups()
482
    inactive = attr[4] == '-'
483
    online = attr[5] == 'o'
484
    lvs[name] = (size, inactive, online)
485

    
486
  return lvs
487

    
488

    
489
def ListVolumeGroups():
490
  """List the volume groups and their size.
491

492
  @rtype: dict
493
  @return: dictionary with keys volume name and values the
494
      size of the volume
495

496
  """
497
  return utils.ListVolumeGroups()
498

    
499

    
500
def NodeVolumes():
501
  """List all volumes on this node.
502

503
  @rtype: list
504
  @return:
505
    A list of dictionaries, each having four keys:
506
      - name: the logical volume name,
507
      - size: the size of the logical volume
508
      - dev: the physical device on which the LV lives
509
      - vg: the volume group to which it belongs
510

511
    In case of errors, we return an empty list and log the
512
    error.
513

514
    Note that since a logical volume can live on multiple physical
515
    volumes, the resulting list might include a logical volume
516
    multiple times.
517

518
  """
519
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
520
                         "--separator=|",
521
                         "--options=lv_name,lv_size,devices,vg_name"])
522
  if result.failed:
523
    logging.error("Failed to list logical volumes, lvs output: %s",
524
                  result.output)
525
    return []
526

    
527
  def parse_dev(dev):
528
    if '(' in dev:
529
      return dev.split('(')[0]
530
    else:
531
      return dev
532

    
533
  def map_line(line):
534
    return {
535
      'name': line[0].strip(),
536
      'size': line[1].strip(),
537
      'dev': parse_dev(line[2].strip()),
538
      'vg': line[3].strip(),
539
    }
540

    
541
  return [map_line(line.split('|')) for line in result.stdout.splitlines()
542
          if line.count('|') >= 3]
543

    
544

    
545
def BridgesExist(bridges_list):
546
  """Check if a list of bridges exist on the current node.
547

548
  @rtype: boolean
549
  @return: C{True} if all of them exist, C{False} otherwise
550

551
  """
552
  for bridge in bridges_list:
553
    if not utils.BridgeExists(bridge):
554
      return False
555

    
556
  return True
557

    
558

    
559
def GetInstanceList(hypervisor_list):
560
  """Provides a list of instances.
561

562
  @type hypervisor_list: list
563
  @param hypervisor_list: the list of hypervisors to query information
564

565
  @rtype: list
566
  @return: a list of all running instances on the current node
567
    - instance1.example.com
568
    - instance2.example.com
569

570
  """
571
  results = []
572
  for hname in hypervisor_list:
573
    try:
574
      names = hypervisor.GetHypervisor(hname).ListInstances()
575
      results.extend(names)
576
    except errors.HypervisorError, err:
577
      logging.exception("Error enumerating instances for hypevisor %s", hname)
578
      raise
579

    
580
  return results
581

    
582

    
583
def GetInstanceInfo(instance, hname):
584
  """Gives back the informations about an instance as a dictionary.
585

586
  @type instance: string
587
  @param instance: the instance name
588
  @type hname: string
589
  @param hname: the hypervisor type of the instance
590

591
  @rtype: dict
592
  @return: dictionary with the following keys:
593
      - memory: memory size of instance (int)
594
      - state: xen state of instance (string)
595
      - time: cpu time of instance (float)
596

597
  """
598
  output = {}
599

    
600
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
601
  if iinfo is not None:
602
    output['memory'] = iinfo[2]
603
    output['state'] = iinfo[4]
604
    output['time'] = iinfo[5]
605

    
606
  return output
607

    
608

    
609
def GetInstanceMigratable(instance):
610
  """Gives whether an instance can be migrated.
611

612
  @type instance: L{objects.Instance}
613
  @param instance: object representing the instance to be checked.
614

615
  @rtype: tuple
616
  @return: tuple of (result, description) where:
617
      - result: whether the instance can be migrated or not
618
      - description: a description of the issue, if relevant
619

620
  """
621
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
622
  if instance.name not in hyper.ListInstances():
623
    return (False, 'not running')
624

    
625
  for idx in range(len(instance.disks)):
626
    link_name = _GetBlockDevSymlinkPath(instance.name, idx)
627
    if not os.path.islink(link_name):
628
      return (False, 'not restarted since ganeti 1.2.5')
629

    
630
  return (True, '')
631

    
632

    
633
def GetAllInstancesInfo(hypervisor_list):
634
  """Gather data about all instances.
635

636
  This is the equivalent of L{GetInstanceInfo}, except that it
637
  computes data for all instances at once, thus being faster if one
638
  needs data about more than one instance.
639

640
  @type hypervisor_list: list
641
  @param hypervisor_list: list of hypervisors to query for instance data
642

643
  @rtype: dict
644
  @return: dictionary of instance: data, with data having the following keys:
645
      - memory: memory size of instance (int)
646
      - state: xen state of instance (string)
647
      - time: cpu time of instance (float)
648
      - vcpus: the number of vcpus
649

650
  """
651
  output = {}
652

    
653
  for hname in hypervisor_list:
654
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
655
    if iinfo:
656
      for name, inst_id, memory, vcpus, state, times in iinfo:
657
        value = {
658
          'memory': memory,
659
          'vcpus': vcpus,
660
          'state': state,
661
          'time': times,
662
          }
663
        if name in output:
664
          # we only check static parameters, like memory and vcpus,
665
          # and not state and time which can change between the
666
          # invocations of the different hypervisors
667
          for key in 'memory', 'vcpus':
668
            if value[key] != output[name][key]:
669
              raise errors.HypervisorError("Instance %s is running twice"
670
                                           " with different parameters" % name)
671
        output[name] = value
672

    
673
  return output
674

    
675

    
676
def InstanceOsAdd(instance):
677
  """Add an OS to an instance.
678

679
  @type instance: L{objects.Instance}
680
  @param instance: Instance whose OS is to be installed
681
  @rtype: boolean
682
  @return: the success of the operation
683

684
  """
685
  try:
686
    inst_os = OSFromDisk(instance.os)
687
  except errors.InvalidOS, err:
688
    os_name, os_dir, os_err = err.args
689
    if os_dir is None:
690
      return (False, "Can't find OS '%s': %s" % (os_name, os_err))
691
    else:
692
      return (False, "Error parsing OS '%s' in directory %s: %s" %
693
              (os_name, os_dir, os_err))
694

    
695
  create_env = OSEnvironment(instance)
696

    
697
  logfile = "%s/add-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
698
                                     instance.name, int(time.time()))
699

    
700
  result = utils.RunCmd([inst_os.create_script], env=create_env,
701
                        cwd=inst_os.path, output=logfile,)
702
  if result.failed:
703
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
704
                  " output: %s", result.cmd, result.fail_reason, logfile,
705
                  result.output)
706
    lines = [utils.SafeEncode(val)
707
             for val in utils.TailFile(logfile, lines=20)]
708
    return (False, "OS create script failed (%s), last lines in the"
709
            " log file:\n%s" % (result.fail_reason, "\n".join(lines)))
710

    
711
  return (True, "Successfully installed")
712

    
713

    
714
def RunRenameInstance(instance, old_name):
715
  """Run the OS rename script for an instance.
716

717
  @type instance: L{objects.Instance}
718
  @param instance: Instance whose OS is to be installed
719
  @type old_name: string
720
  @param old_name: previous instance name
721
  @rtype: boolean
722
  @return: the success of the operation
723

724
  """
725
  inst_os = OSFromDisk(instance.os)
726

    
727
  rename_env = OSEnvironment(instance)
728
  rename_env['OLD_INSTANCE_NAME'] = old_name
729

    
730
  logfile = "%s/rename-%s-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
731
                                           old_name,
732
                                           instance.name, int(time.time()))
733

    
734
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
735
                        cwd=inst_os.path, output=logfile)
736

    
737
  if result.failed:
738
    logging.error("os create command '%s' returned error: %s output: %s",
739
                  result.cmd, result.fail_reason, result.output)
740
    lines = [utils.SafeEncode(val)
741
             for val in utils.TailFile(logfile, lines=20)]
742
    return (False, "OS rename script failed (%s), last lines in the"
743
            " log file:\n%s" % (result.fail_reason, "\n".join(lines)))
744

    
745
  return (True, "Rename successful")
746

    
747

    
748
def _GetVGInfo(vg_name):
749
  """Get informations about the volume group.
750

751
  @type vg_name: str
752
  @param vg_name: the volume group which we query
753
  @rtype: dict
754
  @return:
755
    A dictionary with the following keys:
756
      - C{vg_size} is the total size of the volume group in MiB
757
      - C{vg_free} is the free size of the volume group in MiB
758
      - C{pv_count} are the number of physical disks in that VG
759

760
    If an error occurs during gathering of data, we return the same dict
761
    with keys all set to None.
762

763
  """
764
  retdic = dict.fromkeys(["vg_size", "vg_free", "pv_count"])
765

    
766
  retval = utils.RunCmd(["vgs", "-ovg_size,vg_free,pv_count", "--noheadings",
767
                         "--nosuffix", "--units=m", "--separator=:", vg_name])
768

    
769
  if retval.failed:
770
    logging.error("volume group %s not present", vg_name)
771
    return retdic
772
  valarr = retval.stdout.strip().rstrip(':').split(':')
773
  if len(valarr) == 3:
774
    try:
775
      retdic = {
776
        "vg_size": int(round(float(valarr[0]), 0)),
777
        "vg_free": int(round(float(valarr[1]), 0)),
778
        "pv_count": int(valarr[2]),
779
        }
780
    except ValueError, err:
781
      logging.exception("Fail to parse vgs output")
782
  else:
783
    logging.error("vgs output has the wrong number of fields (expected"
784
                  " three): %s", str(valarr))
785
  return retdic
786

    
787

    
788
def _GetBlockDevSymlinkPath(instance_name, idx):
789
  return os.path.join(constants.DISK_LINKS_DIR,
790
                      "%s:%d" % (instance_name, idx))
791

    
792

    
793
def _SymlinkBlockDev(instance_name, device_path, idx):
794
  """Set up symlinks to a instance's block device.
795

796
  This is an auxiliary function run when an instance is start (on the primary
797
  node) or when an instance is migrated (on the target node).
798

799

800
  @param instance_name: the name of the target instance
801
  @param device_path: path of the physical block device, on the node
802
  @param idx: the disk index
803
  @return: absolute path to the disk's symlink
804

805
  """
806
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
807
  try:
808
    os.symlink(device_path, link_name)
809
  except OSError, err:
810
    if err.errno == errno.EEXIST:
811
      if (not os.path.islink(link_name) or
812
          os.readlink(link_name) != device_path):
813
        os.remove(link_name)
814
        os.symlink(device_path, link_name)
815
    else:
816
      raise
817

    
818
  return link_name
819

    
820

    
821
def _RemoveBlockDevLinks(instance_name, disks):
822
  """Remove the block device symlinks belonging to the given instance.
823

824
  """
825
  for idx, disk in enumerate(disks):
826
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
827
    if os.path.islink(link_name):
828
      try:
829
        os.remove(link_name)
830
      except OSError:
831
        logging.exception("Can't remove symlink '%s'", link_name)
832

    
833

    
834
def _GatherAndLinkBlockDevs(instance):
835
  """Set up an instance's block device(s).
836

837
  This is run on the primary node at instance startup. The block
838
  devices must be already assembled.
839

840
  @type instance: L{objects.Instance}
841
  @param instance: the instance whose disks we shoul assemble
842
  @rtype: list
843
  @return: list of (disk_object, device_path)
844

845
  """
846
  block_devices = []
847
  for idx, disk in enumerate(instance.disks):
848
    device = _RecursiveFindBD(disk)
849
    if device is None:
850
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
851
                                    str(disk))
852
    device.Open()
853
    try:
854
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
855
    except OSError, e:
856
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
857
                                    e.strerror)
858

    
859
    block_devices.append((disk, link_name))
860

    
861
  return block_devices
862

    
863

    
864
def StartInstance(instance):
865
  """Start an instance.
866

867
  @type instance: L{objects.Instance}
868
  @param instance: the instance object
869
  @rtype: boolean
870
  @return: whether the startup was successful or not
871

872
  """
873
  running_instances = GetInstanceList([instance.hypervisor])
874

    
875
  if instance.name in running_instances:
876
    return (True, "Already running")
877

    
878
  try:
879
    block_devices = _GatherAndLinkBlockDevs(instance)
880
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
881
    hyper.StartInstance(instance, block_devices)
882
  except errors.BlockDeviceError, err:
883
    logging.exception("Failed to start instance")
884
    return (False, "Block device error: %s" % str(err))
885
  except errors.HypervisorError, err:
886
    logging.exception("Failed to start instance")
887
    _RemoveBlockDevLinks(instance.name, instance.disks)
888
    return (False, "Hypervisor error: %s" % str(err))
889

    
890
  return (True, "Instance started successfully")
891

    
892

    
893
def InstanceShutdown(instance):
894
  """Shut an instance down.
895

896
  @note: this functions uses polling with a hardcoded timeout.
897

898
  @type instance: L{objects.Instance}
899
  @param instance: the instance object
900
  @rtype: boolean
901
  @return: whether the startup was successful or not
902

903
  """
904
  hv_name = instance.hypervisor
905
  running_instances = GetInstanceList([hv_name])
906

    
907
  if instance.name not in running_instances:
908
    return (True, "Instance already stopped")
909

    
910
  hyper = hypervisor.GetHypervisor(hv_name)
911
  try:
912
    hyper.StopInstance(instance)
913
  except errors.HypervisorError, err:
914
    msg = "Failed to stop instance %s: %s" % (instance.name, err)
915
    logging.error(msg)
916
    return (False, msg)
917

    
918
  # test every 10secs for 2min
919

    
920
  time.sleep(1)
921
  for dummy in range(11):
922
    if instance.name not in GetInstanceList([hv_name]):
923
      break
924
    time.sleep(10)
925
  else:
926
    # the shutdown did not succeed
927
    logging.error("Shutdown of '%s' unsuccessful, using destroy",
928
                  instance.name)
929

    
930
    try:
931
      hyper.StopInstance(instance, force=True)
932
    except errors.HypervisorError, err:
933
      msg = "Failed to force stop instance %s: %s" % (instance.name, err)
934
      logging.error(msg)
935
      return (False, msg)
936

    
937
    time.sleep(1)
938
    if instance.name in GetInstanceList([hv_name]):
939
      msg = ("Could not shutdown instance %s even by destroy" %
940
             instance.name)
941
      logging.error(msg)
942
      return (False, msg)
943

    
944
  _RemoveBlockDevLinks(instance.name, instance.disks)
945

    
946
  return (True, "Instance has been shutdown successfully")
947

    
948

    
949
def InstanceReboot(instance, reboot_type):
950
  """Reboot an instance.
951

952
  @type instance: L{objects.Instance}
953
  @param instance: the instance object to reboot
954
  @type reboot_type: str
955
  @param reboot_type: the type of reboot, one the following
956
    constants:
957
      - L{constants.INSTANCE_REBOOT_SOFT}: only reboot the
958
        instance OS, do not recreate the VM
959
      - L{constants.INSTANCE_REBOOT_HARD}: tear down and
960
        restart the VM (at the hypervisor level)
961
      - the other reboot type (L{constants.INSTANCE_REBOOT_HARD})
962
        is not accepted here, since that mode is handled
963
        differently
964
  @rtype: boolean
965
  @return: the success of the operation
966

967
  """
968
  running_instances = GetInstanceList([instance.hypervisor])
969

    
970
  if instance.name not in running_instances:
971
    msg = "Cannot reboot instance %s that is not running" % instance.name
972
    logging.error(msg)
973
    return (False, msg)
974

    
975
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
976
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
977
    try:
978
      hyper.RebootInstance(instance)
979
    except errors.HypervisorError, err:
980
      msg = "Failed to soft reboot instance %s: %s" % (instance.name, err)
981
      logging.error(msg)
982
      return (False, msg)
983
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
984
    try:
985
      stop_result = InstanceShutdown(instance)
986
      if not stop_result[0]:
987
        return stop_result
988
      return StartInstance(instance)
989
    except errors.HypervisorError, err:
990
      msg = "Failed to hard reboot instance %s: %s" % (instance.name, err)
991
      logging.error(msg)
992
      return (False, msg)
993
  else:
994
    return (False, "Invalid reboot_type received: %s" % (reboot_type,))
995

    
996
  return (True, "Reboot successful")
997

    
998

    
999
def MigrationInfo(instance):
1000
  """Gather information about an instance to be migrated.
1001

1002
  @type instance: L{objects.Instance}
1003
  @param instance: the instance definition
1004

1005
  """
1006
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1007
  try:
1008
    info = hyper.MigrationInfo(instance)
1009
  except errors.HypervisorError, err:
1010
    msg = "Failed to fetch migration information"
1011
    logging.exception(msg)
1012
    return (False, '%s: %s' % (msg, err))
1013
  return (True, info)
1014

    
1015

    
1016
def AcceptInstance(instance, info, target):
1017
  """Prepare the node to accept an instance.
1018

1019
  @type instance: L{objects.Instance}
1020
  @param instance: the instance definition
1021
  @type info: string/data (opaque)
1022
  @param info: migration information, from the source node
1023
  @type target: string
1024
  @param target: target host (usually ip), on this node
1025

1026
  """
1027
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1028
  try:
1029
    hyper.AcceptInstance(instance, info, target)
1030
  except errors.HypervisorError, err:
1031
    msg = "Failed to accept instance"
1032
    logging.exception(msg)
1033
    return (False, '%s: %s' % (msg, err))
1034
  return (True, "Accept successfull")
1035

    
1036

    
1037
def FinalizeMigration(instance, info, success):
1038
  """Finalize any preparation to accept an instance.
1039

1040
  @type instance: L{objects.Instance}
1041
  @param instance: the instance definition
1042
  @type info: string/data (opaque)
1043
  @param info: migration information, from the source node
1044
  @type success: boolean
1045
  @param success: whether the migration was a success or a failure
1046

1047
  """
1048
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1049
  try:
1050
    hyper.FinalizeMigration(instance, info, success)
1051
  except errors.HypervisorError, err:
1052
    msg = "Failed to finalize migration"
1053
    logging.exception(msg)
1054
    return (False, '%s: %s' % (msg, err))
1055
  return (True, "Migration Finalized")
1056

    
1057

    
1058
def MigrateInstance(instance, target, live):
1059
  """Migrates an instance to another node.
1060

1061
  @type instance: L{objects.Instance}
1062
  @param instance: the instance definition
1063
  @type target: string
1064
  @param target: the target node name
1065
  @type live: boolean
1066
  @param live: whether the migration should be done live or not (the
1067
      interpretation of this parameter is left to the hypervisor)
1068
  @rtype: tuple
1069
  @return: a tuple of (success, msg) where:
1070
      - succes is a boolean denoting the success/failure of the operation
1071
      - msg is a string with details in case of failure
1072

1073
  """
1074
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1075

    
1076
  try:
1077
    hyper.MigrateInstance(instance.name, target, live)
1078
  except errors.HypervisorError, err:
1079
    msg = "Failed to migrate instance"
1080
    logging.exception(msg)
1081
    return (False, "%s: %s" % (msg, err))
1082
  return (True, "Migration successfull")
1083

    
1084

    
1085
def BlockdevCreate(disk, size, owner, on_primary, info):
1086
  """Creates a block device for an instance.
1087

1088
  @type disk: L{objects.Disk}
1089
  @param disk: the object describing the disk we should create
1090
  @type size: int
1091
  @param size: the size of the physical underlying device, in MiB
1092
  @type owner: str
1093
  @param owner: the name of the instance for which disk is created,
1094
      used for device cache data
1095
  @type on_primary: boolean
1096
  @param on_primary:  indicates if it is the primary node or not
1097
  @type info: string
1098
  @param info: string that will be sent to the physical device
1099
      creation, used for example to set (LVM) tags on LVs
1100

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

1105
  """
1106
  clist = []
1107
  if disk.children:
1108
    for child in disk.children:
1109
      try:
1110
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1111
      except errors.BlockDeviceError, err:
1112
        errmsg = "Can't assemble device %s: %s" % (child, err)
1113
        logging.error(errmsg)
1114
        return False, errmsg
1115
      if on_primary or disk.AssembleOnSecondary():
1116
        # we need the children open in case the device itself has to
1117
        # be assembled
1118
        try:
1119
          crdev.Open()
1120
        except errors.BlockDeviceError, err:
1121
          errmsg = "Can't make child '%s' read-write: %s" % (child, err)
1122
          logging.error(errmsg)
1123
          return False, errmsg
1124
      clist.append(crdev)
1125

    
1126
  try:
1127
    device = bdev.Create(disk.dev_type, disk.physical_id, clist, size)
1128
  except errors.BlockDeviceError, err:
1129
    return False, "Can't create block device: %s" % str(err)
1130

    
1131
  if on_primary or disk.AssembleOnSecondary():
1132
    try:
1133
      device.Assemble()
1134
    except errors.BlockDeviceError, err:
1135
      errmsg = ("Can't assemble device after creation, very"
1136
                " unusual event: %s" % str(err))
1137
      logging.error(errmsg)
1138
      return False, errmsg
1139
    device.SetSyncSpeed(constants.SYNC_SPEED)
1140
    if on_primary or disk.OpenOnSecondary():
1141
      try:
1142
        device.Open(force=True)
1143
      except errors.BlockDeviceError, err:
1144
        errmsg = ("Can't make device r/w after creation, very"
1145
                  " unusual event: %s" % str(err))
1146
        logging.error(errmsg)
1147
        return False, errmsg
1148
    DevCacheManager.UpdateCache(device.dev_path, owner,
1149
                                on_primary, disk.iv_name)
1150

    
1151
  device.SetInfo(info)
1152

    
1153
  physical_id = device.unique_id
1154
  return True, physical_id
1155

    
1156

    
1157
def BlockdevRemove(disk):
1158
  """Remove a block device.
1159

1160
  @note: This is intended to be called recursively.
1161

1162
  @type disk: L{objects.Disk}
1163
  @param disk: the disk object we should remove
1164
  @rtype: boolean
1165
  @return: the success of the operation
1166

1167
  """
1168
  msgs = []
1169
  result = True
1170
  try:
1171
    rdev = _RecursiveFindBD(disk)
1172
  except errors.BlockDeviceError, err:
1173
    # probably can't attach
1174
    logging.info("Can't attach to device %s in remove", disk)
1175
    rdev = None
1176
  if rdev is not None:
1177
    r_path = rdev.dev_path
1178
    try:
1179
      rdev.Remove()
1180
    except errors.BlockDeviceError, err:
1181
      msgs.append(str(err))
1182
      result = False
1183
    if result:
1184
      DevCacheManager.RemoveCache(r_path)
1185

    
1186
  if disk.children:
1187
    for child in disk.children:
1188
      c_status, c_msg = BlockdevRemove(child)
1189
      result = result and c_status
1190
      if c_msg: # not an empty message
1191
        msgs.append(c_msg)
1192

    
1193
  return (result, "; ".join(msgs))
1194

    
1195

    
1196
def _RecursiveAssembleBD(disk, owner, as_primary):
1197
  """Activate a block device for an instance.
1198

1199
  This is run on the primary and secondary nodes for an instance.
1200

1201
  @note: this function is called recursively.
1202

1203
  @type disk: L{objects.Disk}
1204
  @param disk: the disk we try to assemble
1205
  @type owner: str
1206
  @param owner: the name of the instance which owns the disk
1207
  @type as_primary: boolean
1208
  @param as_primary: if we should make the block device
1209
      read/write
1210

1211
  @return: the assembled device or None (in case no device
1212
      was assembled)
1213
  @raise errors.BlockDeviceError: in case there is an error
1214
      during the activation of the children or the device
1215
      itself
1216

1217
  """
1218
  children = []
1219
  if disk.children:
1220
    mcn = disk.ChildrenNeeded()
1221
    if mcn == -1:
1222
      mcn = 0 # max number of Nones allowed
1223
    else:
1224
      mcn = len(disk.children) - mcn # max number of Nones
1225
    for chld_disk in disk.children:
1226
      try:
1227
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1228
      except errors.BlockDeviceError, err:
1229
        if children.count(None) >= mcn:
1230
          raise
1231
        cdev = None
1232
        logging.error("Error in child activation (but continuing): %s",
1233
                      str(err))
1234
      children.append(cdev)
1235

    
1236
  if as_primary or disk.AssembleOnSecondary():
1237
    r_dev = bdev.Assemble(disk.dev_type, disk.physical_id, children)
1238
    r_dev.SetSyncSpeed(constants.SYNC_SPEED)
1239
    result = r_dev
1240
    if as_primary or disk.OpenOnSecondary():
1241
      r_dev.Open()
1242
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1243
                                as_primary, disk.iv_name)
1244

    
1245
  else:
1246
    result = True
1247
  return result
1248

    
1249

    
1250
def BlockdevAssemble(disk, owner, as_primary):
1251
  """Activate a block device for an instance.
1252

1253
  This is a wrapper over _RecursiveAssembleBD.
1254

1255
  @rtype: str or boolean
1256
  @return: a C{/dev/...} path for primary nodes, and
1257
      C{True} for secondary nodes
1258

1259
  """
1260
  status = True
1261
  result = "no error information"
1262
  try:
1263
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1264
    if isinstance(result, bdev.BlockDev):
1265
      result = result.dev_path
1266
  except errors.BlockDeviceError, err:
1267
    result = "Error while assembling disk: %s" % str(err)
1268
    status = False
1269
  return (status, result)
1270

    
1271

    
1272
def BlockdevShutdown(disk):
1273
  """Shut down a block device.
1274

1275
  First, if the device is assembled (Attach() is successfull), then
1276
  the device is shutdown. Then the children of the device are
1277
  shutdown.
1278

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

1283
  @type disk: L{objects.Disk}
1284
  @param disk: the description of the disk we should
1285
      shutdown
1286
  @rtype: boolean
1287
  @return: the success of the operation
1288

1289
  """
1290
  msgs = []
1291
  result = True
1292
  r_dev = _RecursiveFindBD(disk)
1293
  if r_dev is not None:
1294
    r_path = r_dev.dev_path
1295
    try:
1296
      r_dev.Shutdown()
1297
      DevCacheManager.RemoveCache(r_path)
1298
    except errors.BlockDeviceError, err:
1299
      msgs.append(str(err))
1300
      result = False
1301

    
1302
  if disk.children:
1303
    for child in disk.children:
1304
      c_status, c_msg = BlockdevShutdown(child)
1305
      result = result and c_status
1306
      if c_msg: # not an empty message
1307
        msgs.append(c_msg)
1308

    
1309
  return (result, "; ".join(msgs))
1310

    
1311

    
1312
def BlockdevAddchildren(parent_cdev, new_cdevs):
1313
  """Extend a mirrored block device.
1314

1315
  @type parent_cdev: L{objects.Disk}
1316
  @param parent_cdev: the disk to which we should add children
1317
  @type new_cdevs: list of L{objects.Disk}
1318
  @param new_cdevs: the list of children which we should add
1319
  @rtype: boolean
1320
  @return: the success of the operation
1321

1322
  """
1323
  parent_bdev = _RecursiveFindBD(parent_cdev)
1324
  if parent_bdev is None:
1325
    logging.error("Can't find parent device")
1326
    return False
1327
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1328
  if new_bdevs.count(None) > 0:
1329
    logging.error("Can't find new device(s) to add: %s:%s",
1330
                  new_bdevs, new_cdevs)
1331
    return False
1332
  parent_bdev.AddChildren(new_bdevs)
1333
  return True
1334

    
1335

    
1336
def BlockdevRemovechildren(parent_cdev, new_cdevs):
1337
  """Shrink a mirrored block device.
1338

1339
  @type parent_cdev: L{objects.Disk}
1340
  @param parent_cdev: the disk from which we should remove children
1341
  @type new_cdevs: list of L{objects.Disk}
1342
  @param new_cdevs: the list of children which we should remove
1343
  @rtype: boolean
1344
  @return: the success of the operation
1345

1346
  """
1347
  parent_bdev = _RecursiveFindBD(parent_cdev)
1348
  if parent_bdev is None:
1349
    logging.error("Can't find parent in remove children: %s", parent_cdev)
1350
    return False
1351
  devs = []
1352
  for disk in new_cdevs:
1353
    rpath = disk.StaticDevPath()
1354
    if rpath is None:
1355
      bd = _RecursiveFindBD(disk)
1356
      if bd is None:
1357
        logging.error("Can't find dynamic device %s while removing children",
1358
                      disk)
1359
        return False
1360
      else:
1361
        devs.append(bd.dev_path)
1362
    else:
1363
      devs.append(rpath)
1364
  parent_bdev.RemoveChildren(devs)
1365
  return True
1366

    
1367

    
1368
def BlockdevGetmirrorstatus(disks):
1369
  """Get the mirroring status of a list of devices.
1370

1371
  @type disks: list of L{objects.Disk}
1372
  @param disks: the list of disks which we should query
1373
  @rtype: disk
1374
  @return:
1375
      a list of (mirror_done, estimated_time) tuples, which
1376
      are the result of L{bdev.BlockDev.CombinedSyncStatus}
1377
  @raise errors.BlockDeviceError: if any of the disks cannot be
1378
      found
1379

1380
  """
1381
  stats = []
1382
  for dsk in disks:
1383
    rbd = _RecursiveFindBD(dsk)
1384
    if rbd is None:
1385
      raise errors.BlockDeviceError("Can't find device %s" % str(dsk))
1386
    stats.append(rbd.CombinedSyncStatus())
1387
  return stats
1388

    
1389

    
1390
def _RecursiveFindBD(disk):
1391
  """Check if a device is activated.
1392

1393
  If so, return informations about the real device.
1394

1395
  @type disk: L{objects.Disk}
1396
  @param disk: the disk object we need to find
1397

1398
  @return: None if the device can't be found,
1399
      otherwise the device instance
1400

1401
  """
1402
  children = []
1403
  if disk.children:
1404
    for chdisk in disk.children:
1405
      children.append(_RecursiveFindBD(chdisk))
1406

    
1407
  return bdev.FindDevice(disk.dev_type, disk.physical_id, children)
1408

    
1409

    
1410
def BlockdevFind(disk):
1411
  """Check if a device is activated.
1412

1413
  If it is, return informations about the real device.
1414

1415
  @type disk: L{objects.Disk}
1416
  @param disk: the disk to find
1417
  @rtype: None or tuple
1418
  @return: None if the disk cannot be found, otherwise a
1419
      tuple (device_path, major, minor, sync_percent,
1420
      estimated_time, is_degraded)
1421

1422
  """
1423
  try:
1424
    rbd = _RecursiveFindBD(disk)
1425
  except errors.BlockDeviceError, err:
1426
    return (False, str(err))
1427
  if rbd is None:
1428
    return (True, None)
1429
  return (True, (rbd.dev_path, rbd.major, rbd.minor) + rbd.GetSyncStatus())
1430

    
1431

    
1432
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
1433
  """Write a file to the filesystem.
1434

1435
  This allows the master to overwrite(!) a file. It will only perform
1436
  the operation if the file belongs to a list of configuration files.
1437

1438
  @type file_name: str
1439
  @param file_name: the target file name
1440
  @type data: str
1441
  @param data: the new contents of the file
1442
  @type mode: int
1443
  @param mode: the mode to give the file (can be None)
1444
  @type uid: int
1445
  @param uid: the owner of the file (can be -1 for default)
1446
  @type gid: int
1447
  @param gid: the group of the file (can be -1 for default)
1448
  @type atime: float
1449
  @param atime: the atime to set on the file (can be None)
1450
  @type mtime: float
1451
  @param mtime: the mtime to set on the file (can be None)
1452
  @rtype: boolean
1453
  @return: the success of the operation; errors are logged
1454
      in the node daemon log
1455

1456
  """
1457
  if not os.path.isabs(file_name):
1458
    logging.error("Filename passed to UploadFile is not absolute: '%s'",
1459
                  file_name)
1460
    return False
1461

    
1462
  allowed_files = [
1463
    constants.CLUSTER_CONF_FILE,
1464
    constants.ETC_HOSTS,
1465
    constants.SSH_KNOWN_HOSTS_FILE,
1466
    constants.VNC_PASSWORD_FILE,
1467
    ]
1468

    
1469
  if file_name not in allowed_files:
1470
    logging.error("Filename passed to UploadFile not in allowed"
1471
                 " upload targets: '%s'", file_name)
1472
    return False
1473

    
1474
  raw_data = _Decompress(data)
1475

    
1476
  utils.WriteFile(file_name, data=raw_data, mode=mode, uid=uid, gid=gid,
1477
                  atime=atime, mtime=mtime)
1478
  return True
1479

    
1480

    
1481
def WriteSsconfFiles(values):
1482
  """Update all ssconf files.
1483

1484
  Wrapper around the SimpleStore.WriteFiles.
1485

1486
  """
1487
  ssconf.SimpleStore().WriteFiles(values)
1488

    
1489

    
1490
def _ErrnoOrStr(err):
1491
  """Format an EnvironmentError exception.
1492

1493
  If the L{err} argument has an errno attribute, it will be looked up
1494
  and converted into a textual C{E...} description. Otherwise the
1495
  string representation of the error will be returned.
1496

1497
  @type err: L{EnvironmentError}
1498
  @param err: the exception to format
1499

1500
  """
1501
  if hasattr(err, 'errno'):
1502
    detail = errno.errorcode[err.errno]
1503
  else:
1504
    detail = str(err)
1505
  return detail
1506

    
1507

    
1508
def _OSOndiskVersion(name, os_dir):
1509
  """Compute and return the API version of a given OS.
1510

1511
  This function will try to read the API version of the OS given by
1512
  the 'name' parameter and residing in the 'os_dir' directory.
1513

1514
  @type name: str
1515
  @param name: the OS name we should look for
1516
  @type os_dir: str
1517
  @param os_dir: the directory inwhich we should look for the OS
1518
  @rtype: int or None
1519
  @return:
1520
      Either an integer denoting the version or None in the
1521
      case when this is not a valid OS name.
1522
  @raise errors.InvalidOS: if the OS cannot be found
1523

1524
  """
1525
  api_file = os.path.sep.join([os_dir, "ganeti_api_version"])
1526

    
1527
  try:
1528
    st = os.stat(api_file)
1529
  except EnvironmentError, err:
1530
    raise errors.InvalidOS(name, os_dir, "'ganeti_api_version' file not"
1531
                           " found (%s)" % _ErrnoOrStr(err))
1532

    
1533
  if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
1534
    raise errors.InvalidOS(name, os_dir, "'ganeti_api_version' file is not"
1535
                           " a regular file")
1536

    
1537
  try:
1538
    f = open(api_file)
1539
    try:
1540
      api_versions = f.readlines()
1541
    finally:
1542
      f.close()
1543
  except EnvironmentError, err:
1544
    raise errors.InvalidOS(name, os_dir, "error while reading the"
1545
                           " API version (%s)" % _ErrnoOrStr(err))
1546

    
1547
  api_versions = [version.strip() for version in api_versions]
1548
  try:
1549
    api_versions = [int(version) for version in api_versions]
1550
  except (TypeError, ValueError), err:
1551
    raise errors.InvalidOS(name, os_dir,
1552
                           "API version is not integer (%s)" % str(err))
1553

    
1554
  return api_versions
1555

    
1556

    
1557
def DiagnoseOS(top_dirs=None):
1558
  """Compute the validity for all OSes.
1559

1560
  @type top_dirs: list
1561
  @param top_dirs: the list of directories in which to
1562
      search (if not given defaults to
1563
      L{constants.OS_SEARCH_PATH})
1564
  @rtype: list of L{objects.OS}
1565
  @return: an OS object for each name in all the given
1566
      directories
1567

1568
  """
1569
  if top_dirs is None:
1570
    top_dirs = constants.OS_SEARCH_PATH
1571

    
1572
  result = []
1573
  for dir_name in top_dirs:
1574
    if os.path.isdir(dir_name):
1575
      try:
1576
        f_names = utils.ListVisibleFiles(dir_name)
1577
      except EnvironmentError, err:
1578
        logging.exception("Can't list the OS directory %s", dir_name)
1579
        break
1580
      for name in f_names:
1581
        try:
1582
          os_inst = OSFromDisk(name, base_dir=dir_name)
1583
          result.append(os_inst)
1584
        except errors.InvalidOS, err:
1585
          result.append(objects.OS.FromInvalidOS(err))
1586

    
1587
  return result
1588

    
1589

    
1590
def OSFromDisk(name, base_dir=None):
1591
  """Create an OS instance from disk.
1592

1593
  This function will return an OS instance if the given name is a
1594
  valid OS name. Otherwise, it will raise an appropriate
1595
  L{errors.InvalidOS} exception, detailing why this is not a valid OS.
1596

1597
  @type base_dir: string
1598
  @keyword base_dir: Base directory containing OS installations.
1599
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
1600
  @rtype: L{objects.OS}
1601
  @return: the OS instance if we find a valid one
1602
  @raise errors.InvalidOS: if we don't find a valid OS
1603

1604
  """
1605
  if base_dir is None:
1606
    os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir)
1607
    if os_dir is None:
1608
      raise errors.InvalidOS(name, None, "OS dir not found in search path")
1609
  else:
1610
    os_dir = os.path.sep.join([base_dir, name])
1611

    
1612
  api_versions = _OSOndiskVersion(name, os_dir)
1613

    
1614
  if constants.OS_API_VERSION not in api_versions:
1615
    raise errors.InvalidOS(name, os_dir, "API version mismatch"
1616
                           " (found %s want %s)"
1617
                           % (api_versions, constants.OS_API_VERSION))
1618

    
1619
  # OS Scripts dictionary, we will populate it with the actual script names
1620
  os_scripts = dict.fromkeys(constants.OS_SCRIPTS)
1621

    
1622
  for script in os_scripts:
1623
    os_scripts[script] = os.path.sep.join([os_dir, script])
1624

    
1625
    try:
1626
      st = os.stat(os_scripts[script])
1627
    except EnvironmentError, err:
1628
      raise errors.InvalidOS(name, os_dir, "'%s' script missing (%s)" %
1629
                             (script, _ErrnoOrStr(err)))
1630

    
1631
    if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
1632
      raise errors.InvalidOS(name, os_dir, "'%s' script not executable" %
1633
                             script)
1634

    
1635
    if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
1636
      raise errors.InvalidOS(name, os_dir, "'%s' is not a regular file" %
1637
                             script)
1638

    
1639

    
1640
  return objects.OS(name=name, path=os_dir, status=constants.OS_VALID_STATUS,
1641
                    create_script=os_scripts[constants.OS_SCRIPT_CREATE],
1642
                    export_script=os_scripts[constants.OS_SCRIPT_EXPORT],
1643
                    import_script=os_scripts[constants.OS_SCRIPT_IMPORT],
1644
                    rename_script=os_scripts[constants.OS_SCRIPT_RENAME],
1645
                    api_versions=api_versions)
1646

    
1647
def OSEnvironment(instance, debug=0):
1648
  """Calculate the environment for an os script.
1649

1650
  @type instance: L{objects.Instance}
1651
  @param instance: target instance for the os script run
1652
  @type debug: integer
1653
  @param debug: debug level (0 or 1, for OS Api 10)
1654
  @rtype: dict
1655
  @return: dict of environment variables
1656
  @raise errors.BlockDeviceError: if the block device
1657
      cannot be found
1658

1659
  """
1660
  result = {}
1661
  result['OS_API_VERSION'] = '%d' % constants.OS_API_VERSION
1662
  result['INSTANCE_NAME'] = instance.name
1663
  result['INSTANCE_OS'] = instance.os
1664
  result['HYPERVISOR'] = instance.hypervisor
1665
  result['DISK_COUNT'] = '%d' % len(instance.disks)
1666
  result['NIC_COUNT'] = '%d' % len(instance.nics)
1667
  result['DEBUG_LEVEL'] = '%d' % debug
1668
  for idx, disk in enumerate(instance.disks):
1669
    real_disk = _RecursiveFindBD(disk)
1670
    if real_disk is None:
1671
      raise errors.BlockDeviceError("Block device '%s' is not set up" %
1672
                                    str(disk))
1673
    real_disk.Open()
1674
    result['DISK_%d_PATH' % idx] = real_disk.dev_path
1675
    result['DISK_%d_ACCESS' % idx] = disk.mode
1676
    if constants.HV_DISK_TYPE in instance.hvparams:
1677
      result['DISK_%d_FRONTEND_TYPE' % idx] = \
1678
        instance.hvparams[constants.HV_DISK_TYPE]
1679
    if disk.dev_type in constants.LDS_BLOCK:
1680
      result['DISK_%d_BACKEND_TYPE' % idx] = 'block'
1681
    elif disk.dev_type == constants.LD_FILE:
1682
      result['DISK_%d_BACKEND_TYPE' % idx] = \
1683
        'file:%s' % disk.physical_id[0]
1684
  for idx, nic in enumerate(instance.nics):
1685
    result['NIC_%d_MAC' % idx] = nic.mac
1686
    if nic.ip:
1687
      result['NIC_%d_IP' % idx] = nic.ip
1688
    result['NIC_%d_BRIDGE' % idx] = nic.bridge
1689
    if constants.HV_NIC_TYPE in instance.hvparams:
1690
      result['NIC_%d_FRONTEND_TYPE' % idx] = \
1691
        instance.hvparams[constants.HV_NIC_TYPE]
1692

    
1693
  return result
1694

    
1695
def BlockdevGrow(disk, amount):
1696
  """Grow a stack of block devices.
1697

1698
  This function is called recursively, with the childrens being the
1699
  first ones to resize.
1700

1701
  @type disk: L{objects.Disk}
1702
  @param disk: the disk to be grown
1703
  @rtype: (status, result)
1704
  @return: a tuple with the status of the operation
1705
      (True/False), and the errors message if status
1706
      is False
1707

1708
  """
1709
  r_dev = _RecursiveFindBD(disk)
1710
  if r_dev is None:
1711
    return False, "Cannot find block device %s" % (disk,)
1712

    
1713
  try:
1714
    r_dev.Grow(amount)
1715
  except errors.BlockDeviceError, err:
1716
    return False, str(err)
1717

    
1718
  return True, None
1719

    
1720

    
1721
def BlockdevSnapshot(disk):
1722
  """Create a snapshot copy of a block device.
1723

1724
  This function is called recursively, and the snapshot is actually created
1725
  just for the leaf lvm backend device.
1726

1727
  @type disk: L{objects.Disk}
1728
  @param disk: the disk to be snapshotted
1729
  @rtype: string
1730
  @return: snapshot disk path
1731

1732
  """
1733
  if disk.children:
1734
    if len(disk.children) == 1:
1735
      # only one child, let's recurse on it
1736
      return BlockdevSnapshot(disk.children[0])
1737
    else:
1738
      # more than one child, choose one that matches
1739
      for child in disk.children:
1740
        if child.size == disk.size:
1741
          # return implies breaking the loop
1742
          return BlockdevSnapshot(child)
1743
  elif disk.dev_type == constants.LD_LV:
1744
    r_dev = _RecursiveFindBD(disk)
1745
    if r_dev is not None:
1746
      # let's stay on the safe side and ask for the full size, for now
1747
      return r_dev.Snapshot(disk.size)
1748
    else:
1749
      return None
1750
  else:
1751
    raise errors.ProgrammerError("Cannot snapshot non-lvm block device"
1752
                                 " '%s' of type '%s'" %
1753
                                 (disk.unique_id, disk.dev_type))
1754

    
1755

    
1756
def ExportSnapshot(disk, dest_node, instance, cluster_name, idx):
1757
  """Export a block device snapshot to a remote node.
1758

1759
  @type disk: L{objects.Disk}
1760
  @param disk: the description of the disk to export
1761
  @type dest_node: str
1762
  @param dest_node: the destination node to export to
1763
  @type instance: L{objects.Instance}
1764
  @param instance: the instance object to whom the disk belongs
1765
  @type cluster_name: str
1766
  @param cluster_name: the cluster name, needed for SSH hostalias
1767
  @type idx: int
1768
  @param idx: the index of the disk in the instance's disk list,
1769
      used to export to the OS scripts environment
1770
  @rtype: boolean
1771
  @return: the success of the operation
1772

1773
  """
1774
  export_env = OSEnvironment(instance)
1775

    
1776
  inst_os = OSFromDisk(instance.os)
1777
  export_script = inst_os.export_script
1778

    
1779
  logfile = "%s/exp-%s-%s-%s.log" % (constants.LOG_OS_DIR, inst_os.name,
1780
                                     instance.name, int(time.time()))
1781
  if not os.path.exists(constants.LOG_OS_DIR):
1782
    os.mkdir(constants.LOG_OS_DIR, 0750)
1783
  real_disk = _RecursiveFindBD(disk)
1784
  if real_disk is None:
1785
    raise errors.BlockDeviceError("Block device '%s' is not set up" %
1786
                                  str(disk))
1787
  real_disk.Open()
1788

    
1789
  export_env['EXPORT_DEVICE'] = real_disk.dev_path
1790
  export_env['EXPORT_INDEX'] = str(idx)
1791

    
1792
  destdir = os.path.join(constants.EXPORT_DIR, instance.name + ".new")
1793
  destfile = disk.physical_id[1]
1794

    
1795
  # the target command is built out of three individual commands,
1796
  # which are joined by pipes; we check each individual command for
1797
  # valid parameters
1798
  expcmd = utils.BuildShellCmd("cd %s; %s 2>%s", inst_os.path,
1799
                               export_script, logfile)
1800

    
1801
  comprcmd = "gzip"
1802

    
1803
  destcmd = utils.BuildShellCmd("mkdir -p %s && cat > %s/%s",
1804
                                destdir, destdir, destfile)
1805
  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
1806
                                                   constants.GANETI_RUNAS,
1807
                                                   destcmd)
1808

    
1809
  # all commands have been checked, so we're safe to combine them
1810
  command = '|'.join([expcmd, comprcmd, utils.ShellQuoteArgs(remotecmd)])
1811

    
1812
  result = utils.RunCmd(command, env=export_env)
1813

    
1814
  if result.failed:
1815
    logging.error("os snapshot export command '%s' returned error: %s"
1816
                  " output: %s", command, result.fail_reason, result.output)
1817
    return False
1818

    
1819
  return True
1820

    
1821

    
1822
def FinalizeExport(instance, snap_disks):
1823
  """Write out the export configuration information.
1824

1825
  @type instance: L{objects.Instance}
1826
  @param instance: the instance which we export, used for
1827
      saving configuration
1828
  @type snap_disks: list of L{objects.Disk}
1829
  @param snap_disks: list of snapshot block devices, which
1830
      will be used to get the actual name of the dump file
1831

1832
  @rtype: boolean
1833
  @return: the success of the operation
1834

1835
  """
1836
  destdir = os.path.join(constants.EXPORT_DIR, instance.name + ".new")
1837
  finaldestdir = os.path.join(constants.EXPORT_DIR, instance.name)
1838

    
1839
  config = objects.SerializableConfigParser()
1840

    
1841
  config.add_section(constants.INISECT_EXP)
1842
  config.set(constants.INISECT_EXP, 'version', '0')
1843
  config.set(constants.INISECT_EXP, 'timestamp', '%d' % int(time.time()))
1844
  config.set(constants.INISECT_EXP, 'source', instance.primary_node)
1845
  config.set(constants.INISECT_EXP, 'os', instance.os)
1846
  config.set(constants.INISECT_EXP, 'compression', 'gzip')
1847

    
1848
  config.add_section(constants.INISECT_INS)
1849
  config.set(constants.INISECT_INS, 'name', instance.name)
1850
  config.set(constants.INISECT_INS, 'memory', '%d' %
1851
             instance.beparams[constants.BE_MEMORY])
1852
  config.set(constants.INISECT_INS, 'vcpus', '%d' %
1853
             instance.beparams[constants.BE_VCPUS])
1854
  config.set(constants.INISECT_INS, 'disk_template', instance.disk_template)
1855

    
1856
  nic_total = 0
1857
  for nic_count, nic in enumerate(instance.nics):
1858
    nic_total += 1
1859
    config.set(constants.INISECT_INS, 'nic%d_mac' %
1860
               nic_count, '%s' % nic.mac)
1861
    config.set(constants.INISECT_INS, 'nic%d_ip' % nic_count, '%s' % nic.ip)
1862
    config.set(constants.INISECT_INS, 'nic%d_bridge' % nic_count,
1863
               '%s' % nic.bridge)
1864
  # TODO: redundant: on load can read nics until it doesn't exist
1865
  config.set(constants.INISECT_INS, 'nic_count' , '%d' % nic_total)
1866

    
1867
  disk_total = 0
1868
  for disk_count, disk in enumerate(snap_disks):
1869
    if disk:
1870
      disk_total += 1
1871
      config.set(constants.INISECT_INS, 'disk%d_ivname' % disk_count,
1872
                 ('%s' % disk.iv_name))
1873
      config.set(constants.INISECT_INS, 'disk%d_dump' % disk_count,
1874
                 ('%s' % disk.physical_id[1]))
1875
      config.set(constants.INISECT_INS, 'disk%d_size' % disk_count,
1876
                 ('%d' % disk.size))
1877

    
1878
  config.set(constants.INISECT_INS, 'disk_count' , '%d' % disk_total)
1879

    
1880
  utils.WriteFile(os.path.join(destdir, constants.EXPORT_CONF_FILE),
1881
                  data=config.Dumps())
1882
  shutil.rmtree(finaldestdir, True)
1883
  shutil.move(destdir, finaldestdir)
1884

    
1885
  return True
1886

    
1887

    
1888
def ExportInfo(dest):
1889
  """Get export configuration information.
1890

1891
  @type dest: str
1892
  @param dest: directory containing the export
1893

1894
  @rtype: L{objects.SerializableConfigParser}
1895
  @return: a serializable config file containing the
1896
      export info
1897

1898
  """
1899
  cff = os.path.join(dest, constants.EXPORT_CONF_FILE)
1900

    
1901
  config = objects.SerializableConfigParser()
1902
  config.read(cff)
1903

    
1904
  if (not config.has_section(constants.INISECT_EXP) or
1905
      not config.has_section(constants.INISECT_INS)):
1906
    return None
1907

    
1908
  return config
1909

    
1910

    
1911
def ImportOSIntoInstance(instance, src_node, src_images, cluster_name):
1912
  """Import an os image into an instance.
1913

1914
  @type instance: L{objects.Instance}
1915
  @param instance: instance to import the disks into
1916
  @type src_node: string
1917
  @param src_node: source node for the disk images
1918
  @type src_images: list of string
1919
  @param src_images: absolute paths of the disk images
1920
  @rtype: list of boolean
1921
  @return: each boolean represent the success of importing the n-th disk
1922

1923
  """
1924
  import_env = OSEnvironment(instance)
1925
  inst_os = OSFromDisk(instance.os)
1926
  import_script = inst_os.import_script
1927

    
1928
  logfile = "%s/import-%s-%s-%s.log" % (constants.LOG_OS_DIR, instance.os,
1929
                                        instance.name, int(time.time()))
1930
  if not os.path.exists(constants.LOG_OS_DIR):
1931
    os.mkdir(constants.LOG_OS_DIR, 0750)
1932

    
1933
  comprcmd = "gunzip"
1934
  impcmd = utils.BuildShellCmd("(cd %s; %s >%s 2>&1)", inst_os.path,
1935
                               import_script, logfile)
1936

    
1937
  final_result = []
1938
  for idx, image in enumerate(src_images):
1939
    if image:
1940
      destcmd = utils.BuildShellCmd('cat %s', image)
1941
      remotecmd = _GetSshRunner(cluster_name).BuildCmd(src_node,
1942
                                                       constants.GANETI_RUNAS,
1943
                                                       destcmd)
1944
      command = '|'.join([utils.ShellQuoteArgs(remotecmd), comprcmd, impcmd])
1945
      import_env['IMPORT_DEVICE'] = import_env['DISK_%d_PATH' % idx]
1946
      import_env['IMPORT_INDEX'] = str(idx)
1947
      result = utils.RunCmd(command, env=import_env)
1948
      if result.failed:
1949
        logging.error("Disk import command '%s' returned error: %s"
1950
                      " output: %s", command, result.fail_reason,
1951
                      result.output)
1952
        final_result.append(False)
1953
      else:
1954
        final_result.append(True)
1955
    else:
1956
      final_result.append(True)
1957

    
1958
  return final_result
1959

    
1960

    
1961
def ListExports():
1962
  """Return a list of exports currently available on this machine.
1963

1964
  @rtype: list
1965
  @return: list of the exports
1966

1967
  """
1968
  if os.path.isdir(constants.EXPORT_DIR):
1969
    return utils.ListVisibleFiles(constants.EXPORT_DIR)
1970
  else:
1971
    return []
1972

    
1973

    
1974
def RemoveExport(export):
1975
  """Remove an existing export from the node.
1976

1977
  @type export: str
1978
  @param export: the name of the export to remove
1979
  @rtype: boolean
1980
  @return: the success of the operation
1981

1982
  """
1983
  target = os.path.join(constants.EXPORT_DIR, export)
1984

    
1985
  shutil.rmtree(target)
1986
  # TODO: catch some of the relevant exceptions and provide a pretty
1987
  # error message if rmtree fails.
1988

    
1989
  return True
1990

    
1991

    
1992
def BlockdevRename(devlist):
1993
  """Rename a list of block devices.
1994

1995
  @type devlist: list of tuples
1996
  @param devlist: list of tuples of the form  (disk,
1997
      new_logical_id, new_physical_id); disk is an
1998
      L{objects.Disk} object describing the current disk,
1999
      and new logical_id/physical_id is the name we
2000
      rename it to
2001
  @rtype: boolean
2002
  @return: True if all renames succeeded, False otherwise
2003

2004
  """
2005
  result = True
2006
  for disk, unique_id in devlist:
2007
    dev = _RecursiveFindBD(disk)
2008
    if dev is None:
2009
      result = False
2010
      continue
2011
    try:
2012
      old_rpath = dev.dev_path
2013
      dev.Rename(unique_id)
2014
      new_rpath = dev.dev_path
2015
      if old_rpath != new_rpath:
2016
        DevCacheManager.RemoveCache(old_rpath)
2017
        # FIXME: we should add the new cache information here, like:
2018
        # DevCacheManager.UpdateCache(new_rpath, owner, ...)
2019
        # but we don't have the owner here - maybe parse from existing
2020
        # cache? for now, we only lose lvm data when we rename, which
2021
        # is less critical than DRBD or MD
2022
    except errors.BlockDeviceError, err:
2023
      logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
2024
      result = False
2025
  return result
2026

    
2027

    
2028
def _TransformFileStorageDir(file_storage_dir):
2029
  """Checks whether given file_storage_dir is valid.
2030

2031
  Checks wheter the given file_storage_dir is within the cluster-wide
2032
  default file_storage_dir stored in SimpleStore. Only paths under that
2033
  directory are allowed.
2034

2035
  @type file_storage_dir: str
2036
  @param file_storage_dir: the path to check
2037

2038
  @return: the normalized path if valid, None otherwise
2039

2040
  """
2041
  cfg = _GetConfig()
2042
  file_storage_dir = os.path.normpath(file_storage_dir)
2043
  base_file_storage_dir = cfg.GetFileStorageDir()
2044
  if (not os.path.commonprefix([file_storage_dir, base_file_storage_dir]) ==
2045
      base_file_storage_dir):
2046
    logging.error("file storage directory '%s' is not under base file"
2047
                  " storage directory '%s'",
2048
                  file_storage_dir, base_file_storage_dir)
2049
    return None
2050
  return file_storage_dir
2051

    
2052

    
2053
def CreateFileStorageDir(file_storage_dir):
2054
  """Create file storage directory.
2055

2056
  @type file_storage_dir: str
2057
  @param file_storage_dir: directory to create
2058

2059
  @rtype: tuple
2060
  @return: tuple with first element a boolean indicating wheter dir
2061
      creation was successful or not
2062

2063
  """
2064
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2065
  result = True,
2066
  if not file_storage_dir:
2067
    result = False,
2068
  else:
2069
    if os.path.exists(file_storage_dir):
2070
      if not os.path.isdir(file_storage_dir):
2071
        logging.error("'%s' is not a directory", file_storage_dir)
2072
        result = False,
2073
    else:
2074
      try:
2075
        os.makedirs(file_storage_dir, 0750)
2076
      except OSError, err:
2077
        logging.error("Cannot create file storage directory '%s': %s",
2078
                      file_storage_dir, err)
2079
        result = False,
2080
  return result
2081

    
2082

    
2083
def RemoveFileStorageDir(file_storage_dir):
2084
  """Remove file storage directory.
2085

2086
  Remove it only if it's empty. If not log an error and return.
2087

2088
  @type file_storage_dir: str
2089
  @param file_storage_dir: the directory we should cleanup
2090
  @rtype: tuple (success,)
2091
  @return: tuple of one element, C{success}, denoting
2092
      whether the operation was successfull
2093

2094
  """
2095
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2096
  result = True,
2097
  if not file_storage_dir:
2098
    result = False,
2099
  else:
2100
    if os.path.exists(file_storage_dir):
2101
      if not os.path.isdir(file_storage_dir):
2102
        logging.error("'%s' is not a directory", file_storage_dir)
2103
        result = False,
2104
      # deletes dir only if empty, otherwise we want to return False
2105
      try:
2106
        os.rmdir(file_storage_dir)
2107
      except OSError, err:
2108
        logging.exception("Cannot remove file storage directory '%s'",
2109
                          file_storage_dir)
2110
        result = False,
2111
  return result
2112

    
2113

    
2114
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
2115
  """Rename the file storage directory.
2116

2117
  @type old_file_storage_dir: str
2118
  @param old_file_storage_dir: the current path
2119
  @type new_file_storage_dir: str
2120
  @param new_file_storage_dir: the name we should rename to
2121
  @rtype: tuple (success,)
2122
  @return: tuple of one element, C{success}, denoting
2123
      whether the operation was successful
2124

2125
  """
2126
  old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
2127
  new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
2128
  result = True,
2129
  if not old_file_storage_dir or not new_file_storage_dir:
2130
    result = False,
2131
  else:
2132
    if not os.path.exists(new_file_storage_dir):
2133
      if os.path.isdir(old_file_storage_dir):
2134
        try:
2135
          os.rename(old_file_storage_dir, new_file_storage_dir)
2136
        except OSError, err:
2137
          logging.exception("Cannot rename '%s' to '%s'",
2138
                            old_file_storage_dir, new_file_storage_dir)
2139
          result =  False,
2140
      else:
2141
        logging.error("'%s' is not a directory", old_file_storage_dir)
2142
        result = False,
2143
    else:
2144
      if os.path.exists(old_file_storage_dir):
2145
        logging.error("Cannot rename '%s' to '%s'. Both locations exist.",
2146
                      old_file_storage_dir, new_file_storage_dir)
2147
        result = False,
2148
  return result
2149

    
2150

    
2151
def _IsJobQueueFile(file_name):
2152
  """Checks whether the given filename is in the queue directory.
2153

2154
  @type file_name: str
2155
  @param file_name: the file name we should check
2156
  @rtype: boolean
2157
  @return: whether the file is under the queue directory
2158

2159
  """
2160
  queue_dir = os.path.normpath(constants.QUEUE_DIR)
2161
  result = (os.path.commonprefix([queue_dir, file_name]) == queue_dir)
2162

    
2163
  if not result:
2164
    logging.error("'%s' is not a file in the queue directory",
2165
                  file_name)
2166

    
2167
  return result
2168

    
2169

    
2170
def JobQueueUpdate(file_name, content):
2171
  """Updates a file in the queue directory.
2172

2173
  This is just a wrapper over L{utils.WriteFile}, with proper
2174
  checking.
2175

2176
  @type file_name: str
2177
  @param file_name: the job file name
2178
  @type content: str
2179
  @param content: the new job contents
2180
  @rtype: boolean
2181
  @return: the success of the operation
2182

2183
  """
2184
  if not _IsJobQueueFile(file_name):
2185
    return False
2186

    
2187
  # Write and replace the file atomically
2188
  utils.WriteFile(file_name, data=_Decompress(content))
2189

    
2190
  return True
2191

    
2192

    
2193
def JobQueueRename(old, new):
2194
  """Renames a job queue file.
2195

2196
  This is just a wrapper over os.rename with proper checking.
2197

2198
  @type old: str
2199
  @param old: the old (actual) file name
2200
  @type new: str
2201
  @param new: the desired file name
2202
  @rtype: boolean
2203
  @return: the success of the operation
2204

2205
  """
2206
  if not (_IsJobQueueFile(old) and _IsJobQueueFile(new)):
2207
    return False
2208

    
2209
  utils.RenameFile(old, new, mkdir=True)
2210

    
2211
  return True
2212

    
2213

    
2214
def JobQueueSetDrainFlag(drain_flag):
2215
  """Set the drain flag for the queue.
2216

2217
  This will set or unset the queue drain flag.
2218

2219
  @type drain_flag: boolean
2220
  @param drain_flag: if True, will set the drain flag, otherwise reset it.
2221
  @rtype: boolean
2222
  @return: always True
2223
  @warning: the function always returns True
2224

2225
  """
2226
  if drain_flag:
2227
    utils.WriteFile(constants.JOB_QUEUE_DRAIN_FILE, data="", close=True)
2228
  else:
2229
    utils.RemoveFile(constants.JOB_QUEUE_DRAIN_FILE)
2230

    
2231
  return True
2232

    
2233

    
2234
def BlockdevClose(instance_name, disks):
2235
  """Closes the given block devices.
2236

2237
  This means they will be switched to secondary mode (in case of
2238
  DRBD).
2239

2240
  @param instance_name: if the argument is not empty, the symlinks
2241
      of this instance will be removed
2242
  @type disks: list of L{objects.Disk}
2243
  @param disks: the list of disks to be closed
2244
  @rtype: tuple (success, message)
2245
  @return: a tuple of success and message, where success
2246
      indicates the succes of the operation, and message
2247
      which will contain the error details in case we
2248
      failed
2249

2250
  """
2251
  bdevs = []
2252
  for cf in disks:
2253
    rd = _RecursiveFindBD(cf)
2254
    if rd is None:
2255
      return (False, "Can't find device %s" % cf)
2256
    bdevs.append(rd)
2257

    
2258
  msg = []
2259
  for rd in bdevs:
2260
    try:
2261
      rd.Close()
2262
    except errors.BlockDeviceError, err:
2263
      msg.append(str(err))
2264
  if msg:
2265
    return (False, "Can't make devices secondary: %s" % ",".join(msg))
2266
  else:
2267
    if instance_name:
2268
      _RemoveBlockDevLinks(instance_name, disks)
2269
    return (True, "All devices secondary")
2270

    
2271

    
2272
def ValidateHVParams(hvname, hvparams):
2273
  """Validates the given hypervisor parameters.
2274

2275
  @type hvname: string
2276
  @param hvname: the hypervisor name
2277
  @type hvparams: dict
2278
  @param hvparams: the hypervisor parameters to be validated
2279
  @rtype: tuple (success, message)
2280
  @return: a tuple of success and message, where success
2281
      indicates the succes of the operation, and message
2282
      which will contain the error details in case we
2283
      failed
2284

2285
  """
2286
  try:
2287
    hv_type = hypervisor.GetHypervisor(hvname)
2288
    hv_type.ValidateParameters(hvparams)
2289
    return (True, "Validation passed")
2290
  except errors.HypervisorError, err:
2291
    return (False, str(err))
2292

    
2293

    
2294
def DemoteFromMC():
2295
  """Demotes the current node from master candidate role.
2296

2297
  """
2298
  # try to ensure we're not the master by mistake
2299
  master, myself = ssconf.GetMasterAndMyself()
2300
  if master == myself:
2301
    return (False, "ssconf status shows I'm the master node, will not demote")
2302
  pid_file = utils.DaemonPidFileName(constants.MASTERD_PID)
2303
  if utils.IsProcessAlive(utils.ReadPidFile(pid_file)):
2304
    return (False, "The master daemon is running, will not demote")
2305
  try:
2306
    utils.CreateBackup(constants.CLUSTER_CONF_FILE)
2307
  except EnvironmentError, err:
2308
    if err.errno != errno.ENOENT:
2309
      return (False, "Error while backing up cluster file: %s" % str(err))
2310
  utils.RemoveFile(constants.CLUSTER_CONF_FILE)
2311
  return (True, "Done")
2312

    
2313

    
2314
def _FindDisks(nodes_ip, disks):
2315
  """Sets the physical ID on disks and returns the block devices.
2316

2317
  """
2318
  # set the correct physical ID
2319
  my_name = utils.HostInfo().name
2320
  for cf in disks:
2321
    cf.SetPhysicalID(my_name, nodes_ip)
2322

    
2323
  bdevs = []
2324

    
2325
  for cf in disks:
2326
    rd = _RecursiveFindBD(cf)
2327
    if rd is None:
2328
      return (False, "Can't find device %s" % cf)
2329
    bdevs.append(rd)
2330
  return (True, bdevs)
2331

    
2332

    
2333
def DrbdDisconnectNet(nodes_ip, disks):
2334
  """Disconnects the network on a list of drbd devices.
2335

2336
  """
2337
  status, bdevs = _FindDisks(nodes_ip, disks)
2338
  if not status:
2339
    return status, bdevs
2340

    
2341
  # disconnect disks
2342
  for rd in bdevs:
2343
    try:
2344
      rd.DisconnectNet()
2345
    except errors.BlockDeviceError, err:
2346
      logging.exception("Failed to go into standalone mode")
2347
      return (False, "Can't change network configuration: %s" % str(err))
2348
  return (True, "All disks are now disconnected")
2349

    
2350

    
2351
def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster):
2352
  """Attaches the network on a list of drbd devices.
2353

2354
  """
2355
  status, bdevs = _FindDisks(nodes_ip, disks)
2356
  if not status:
2357
    return status, bdevs
2358

    
2359
  if multimaster:
2360
    for idx, rd in enumerate(bdevs):
2361
      try:
2362
        _SymlinkBlockDev(instance_name, rd.dev_path, idx)
2363
      except EnvironmentError, err:
2364
        return (False, "Can't create symlink: %s" % str(err))
2365
  # reconnect disks, switch to new master configuration and if
2366
  # needed primary mode
2367
  for rd in bdevs:
2368
    try:
2369
      rd.AttachNet(multimaster)
2370
    except errors.BlockDeviceError, err:
2371
      return (False, "Can't change network configuration: %s" % str(err))
2372
  # wait until the disks are connected; we need to retry the re-attach
2373
  # if the device becomes standalone, as this might happen if the one
2374
  # node disconnects and reconnects in a different mode before the
2375
  # other node reconnects; in this case, one or both of the nodes will
2376
  # decide it has wrong configuration and switch to standalone
2377
  RECONNECT_TIMEOUT = 2 * 60
2378
  sleep_time = 0.100 # start with 100 miliseconds
2379
  timeout_limit = time.time() + RECONNECT_TIMEOUT
2380
  while time.time() < timeout_limit:
2381
    all_connected = True
2382
    for rd in bdevs:
2383
      stats = rd.GetProcStatus()
2384
      if not (stats.is_connected or stats.is_in_resync):
2385
        all_connected = False
2386
      if stats.is_standalone:
2387
        # peer had different config info and this node became
2388
        # standalone, even though this should not happen with the
2389
        # new staged way of changing disk configs
2390
        try:
2391
          rd.ReAttachNet(multimaster)
2392
        except errors.BlockDeviceError, err:
2393
          return (False, "Can't change network configuration: %s" % str(err))
2394
    if all_connected:
2395
      break
2396
    time.sleep(sleep_time)
2397
    sleep_time = min(5, sleep_time * 1.5)
2398
  if not all_connected:
2399
    return (False, "Timeout in disk reconnecting")
2400
  if multimaster:
2401
    # change to primary mode
2402
    for rd in bdevs:
2403
      try:
2404
        rd.Open()
2405
      except errors.BlockDeviceError, err:
2406
        return (False, "Can't change to primary mode: %s" % str(err))
2407
  if multimaster:
2408
    msg = "multi-master and primary"
2409
  else:
2410
    msg = "single-master"
2411
  return (True, "Disks are now configured as %s" % msg)
2412

    
2413

    
2414
def DrbdWaitSync(nodes_ip, disks):
2415
  """Wait until DRBDs have synchronized.
2416

2417
  """
2418
  status, bdevs = _FindDisks(nodes_ip, disks)
2419
  if not status:
2420
    return status, bdevs
2421

    
2422
  min_resync = 100
2423
  alldone = True
2424
  failure = False
2425
  for rd in bdevs:
2426
    stats = rd.GetProcStatus()
2427
    if not (stats.is_connected or stats.is_in_resync):
2428
      failure = True
2429
      break
2430
    alldone = alldone and (not stats.is_in_resync)
2431
    if stats.sync_percent is not None:
2432
      min_resync = min(min_resync, stats.sync_percent)
2433
  return (not failure, (alldone, min_resync))
2434

    
2435

    
2436
class HooksRunner(object):
2437
  """Hook runner.
2438

2439
  This class is instantiated on the node side (ganeti-noded) and not
2440
  on the master side.
2441

2442
  """
2443
  RE_MASK = re.compile("^[a-zA-Z0-9_-]+$")
2444

    
2445
  def __init__(self, hooks_base_dir=None):
2446
    """Constructor for hooks runner.
2447

2448
    @type hooks_base_dir: str or None
2449
    @param hooks_base_dir: if not None, this overrides the
2450
        L{constants.HOOKS_BASE_DIR} (useful for unittests)
2451

2452
    """
2453
    if hooks_base_dir is None:
2454
      hooks_base_dir = constants.HOOKS_BASE_DIR
2455
    self._BASE_DIR = hooks_base_dir
2456

    
2457
  @staticmethod
2458
  def ExecHook(script, env):
2459
    """Exec one hook script.
2460

2461
    @type script: str
2462
    @param script: the full path to the script
2463
    @type env: dict
2464
    @param env: the environment with which to exec the script
2465
    @rtype: tuple (success, message)
2466
    @return: a tuple of success and message, where success
2467
        indicates the succes of the operation, and message
2468
        which will contain the error details in case we
2469
        failed
2470

2471
    """
2472
    # exec the process using subprocess and log the output
2473
    fdstdin = None
2474
    try:
2475
      fdstdin = open("/dev/null", "r")
2476
      child = subprocess.Popen([script], stdin=fdstdin, stdout=subprocess.PIPE,
2477
                               stderr=subprocess.STDOUT, close_fds=True,
2478
                               shell=False, cwd="/", env=env)
2479
      output = ""
2480
      try:
2481
        output = child.stdout.read(4096)
2482
        child.stdout.close()
2483
      except EnvironmentError, err:
2484
        output += "Hook script error: %s" % str(err)
2485

    
2486
      while True:
2487
        try:
2488
          result = child.wait()
2489
          break
2490
        except EnvironmentError, err:
2491
          if err.errno == errno.EINTR:
2492
            continue
2493
          raise
2494
    finally:
2495
      # try not to leak fds
2496
      for fd in (fdstdin, ):
2497
        if fd is not None:
2498
          try:
2499
            fd.close()
2500
          except EnvironmentError, err:
2501
            # just log the error
2502
            #logging.exception("Error while closing fd %s", fd)
2503
            pass
2504

    
2505
    return result == 0, utils.SafeEncode(output.strip())
2506

    
2507
  def RunHooks(self, hpath, phase, env):
2508
    """Run the scripts in the hooks directory.
2509

2510
    @type hpath: str
2511
    @param hpath: the path to the hooks directory which
2512
        holds the scripts
2513
    @type phase: str
2514
    @param phase: either L{constants.HOOKS_PHASE_PRE} or
2515
        L{constants.HOOKS_PHASE_POST}
2516
    @type env: dict
2517
    @param env: dictionary with the environment for the hook
2518
    @rtype: list
2519
    @return: list of 3-element tuples:
2520
      - script path
2521
      - script result, either L{constants.HKR_SUCCESS} or
2522
        L{constants.HKR_FAIL}
2523
      - output of the script
2524

2525
    @raise errors.ProgrammerError: for invalid input
2526
        parameters
2527

2528
    """
2529
    if phase == constants.HOOKS_PHASE_PRE:
2530
      suffix = "pre"
2531
    elif phase == constants.HOOKS_PHASE_POST:
2532
      suffix = "post"
2533
    else:
2534
      raise errors.ProgrammerError("Unknown hooks phase: '%s'" % phase)
2535
    rr = []
2536

    
2537
    subdir = "%s-%s.d" % (hpath, suffix)
2538
    dir_name = "%s/%s" % (self._BASE_DIR, subdir)
2539
    try:
2540
      dir_contents = utils.ListVisibleFiles(dir_name)
2541
    except OSError, err:
2542
      # FIXME: must log output in case of failures
2543
      return rr
2544

    
2545
    # we use the standard python sort order,
2546
    # so 00name is the recommended naming scheme
2547
    dir_contents.sort()
2548
    for relname in dir_contents:
2549
      fname = os.path.join(dir_name, relname)
2550
      if not (os.path.isfile(fname) and os.access(fname, os.X_OK) and
2551
          self.RE_MASK.match(relname) is not None):
2552
        rrval = constants.HKR_SKIP
2553
        output = ""
2554
      else:
2555
        result, output = self.ExecHook(fname, env)
2556
        if not result:
2557
          rrval = constants.HKR_FAIL
2558
        else:
2559
          rrval = constants.HKR_SUCCESS
2560
      rr.append(("%s/%s" % (subdir, relname), rrval, output))
2561

    
2562
    return rr
2563

    
2564

    
2565
class IAllocatorRunner(object):
2566
  """IAllocator runner.
2567

2568
  This class is instantiated on the node side (ganeti-noded) and not on
2569
  the master side.
2570

2571
  """
2572
  def Run(self, name, idata):
2573
    """Run an iallocator script.
2574

2575
    @type name: str
2576
    @param name: the iallocator script name
2577
    @type idata: str
2578
    @param idata: the allocator input data
2579

2580
    @rtype: tuple
2581
    @return: four element tuple of:
2582
       - run status (one of the IARUN_ constants)
2583
       - stdout
2584
       - stderr
2585
       - fail reason (as from L{utils.RunResult})
2586

2587
    """
2588
    alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
2589
                                  os.path.isfile)
2590
    if alloc_script is None:
2591
      return (constants.IARUN_NOTFOUND, None, None, None)
2592

    
2593
    fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
2594
    try:
2595
      os.write(fd, idata)
2596
      os.close(fd)
2597
      result = utils.RunCmd([alloc_script, fin_name])
2598
      if result.failed:
2599
        return (constants.IARUN_FAILURE, result.stdout, result.stderr,
2600
                result.fail_reason)
2601
    finally:
2602
      os.unlink(fin_name)
2603

    
2604
    return (constants.IARUN_SUCCESS, result.stdout, result.stderr, None)
2605

    
2606

    
2607
class DevCacheManager(object):
2608
  """Simple class for managing a cache of block device information.
2609

2610
  """
2611
  _DEV_PREFIX = "/dev/"
2612
  _ROOT_DIR = constants.BDEV_CACHE_DIR
2613

    
2614
  @classmethod
2615
  def _ConvertPath(cls, dev_path):
2616
    """Converts a /dev/name path to the cache file name.
2617

2618
    This replaces slashes with underscores and strips the /dev
2619
    prefix. It then returns the full path to the cache file.
2620

2621
    @type dev_path: str
2622
    @param dev_path: the C{/dev/} path name
2623
    @rtype: str
2624
    @return: the converted path name
2625

2626
    """
2627
    if dev_path.startswith(cls._DEV_PREFIX):
2628
      dev_path = dev_path[len(cls._DEV_PREFIX):]
2629
    dev_path = dev_path.replace("/", "_")
2630
    fpath = "%s/bdev_%s" % (cls._ROOT_DIR, dev_path)
2631
    return fpath
2632

    
2633
  @classmethod
2634
  def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
2635
    """Updates the cache information for a given device.
2636

2637
    @type dev_path: str
2638
    @param dev_path: the pathname of the device
2639
    @type owner: str
2640
    @param owner: the owner (instance name) of the device
2641
    @type on_primary: bool
2642
    @param on_primary: whether this is the primary
2643
        node nor not
2644
    @type iv_name: str
2645
    @param iv_name: the instance-visible name of the
2646
        device, as in objects.Disk.iv_name
2647

2648
    @rtype: None
2649

2650
    """
2651
    if dev_path is None:
2652
      logging.error("DevCacheManager.UpdateCache got a None dev_path")
2653
      return
2654
    fpath = cls._ConvertPath(dev_path)
2655
    if on_primary:
2656
      state = "primary"
2657
    else:
2658
      state = "secondary"
2659
    if iv_name is None:
2660
      iv_name = "not_visible"
2661
    fdata = "%s %s %s\n" % (str(owner), state, iv_name)
2662
    try:
2663
      utils.WriteFile(fpath, data=fdata)
2664
    except EnvironmentError, err:
2665
      logging.exception("Can't update bdev cache for %s", dev_path)
2666

    
2667
  @classmethod
2668
  def RemoveCache(cls, dev_path):
2669
    """Remove data for a dev_path.
2670

2671
    This is just a wrapper over L{utils.RemoveFile} with a converted
2672
    path name and logging.
2673

2674
    @type dev_path: str
2675
    @param dev_path: the pathname of the device
2676

2677
    @rtype: None
2678

2679
    """
2680
    if dev_path is None:
2681
      logging.error("DevCacheManager.RemoveCache got a None dev_path")
2682
      return
2683
    fpath = cls._ConvertPath(dev_path)
2684
    try:
2685
      utils.RemoveFile(fpath)
2686
    except EnvironmentError, err:
2687
      logging.exception("Can't update bdev cache for %s", dev_path)