Statistics
| Branch: | Tag: | Revision:

root / lib / backend.py @ 63b9b186

History | View | Annotate | Download (79.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
class RPCFail(Exception):
50
  """Class denoting RPC failure.
51

52
  Its argument is the error message.
53

54
  """
55

    
56
def _Fail(msg, *args, **kwargs):
57
  """Log an error and the raise an RPCFail exception.
58

59
  This exception is then handled specially in the ganeti daemon and
60
  turned into a 'failed' return type. As such, this function is a
61
  useful shortcut for logging the error and returning it to the master
62
  daemon.
63

64
  @type msg: string
65
  @param msg: the text of the exception
66
  @raise RPCFail
67

68
  """
69
  if args:
70
    msg = msg % args
71
  if "log" not in kwargs or kwargs["log"]: # if we should log this error
72
    if "exc" in kwargs and kwargs["exc"]:
73
      logging.exception(msg)
74
    else:
75
      logging.error(msg)
76
  raise RPCFail(msg)
77

    
78

    
79
def _GetConfig():
80
  """Simple wrapper to return a SimpleStore.
81

82
  @rtype: L{ssconf.SimpleStore}
83
  @return: a SimpleStore instance
84

85
  """
86
  return ssconf.SimpleStore()
87

    
88

    
89
def _GetSshRunner(cluster_name):
90
  """Simple wrapper to return an SshRunner.
91

92
  @type cluster_name: str
93
  @param cluster_name: the cluster name, which is needed
94
      by the SshRunner constructor
95
  @rtype: L{ssh.SshRunner}
96
  @return: an SshRunner instance
97

98
  """
99
  return ssh.SshRunner(cluster_name)
100

    
101

    
102
def _Decompress(data):
103
  """Unpacks data compressed by the RPC client.
104

105
  @type data: list or tuple
106
  @param data: Data sent by RPC client
107
  @rtype: str
108
  @return: Decompressed data
109

110
  """
111
  assert isinstance(data, (list, tuple))
112
  assert len(data) == 2
113
  (encoding, content) = data
114
  if encoding == constants.RPC_ENCODING_NONE:
115
    return content
116
  elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
117
    return zlib.decompress(base64.b64decode(content))
118
  else:
119
    raise AssertionError("Unknown data encoding")
120

    
121

    
122
def _CleanDirectory(path, exclude=None):
123
  """Removes all regular files in a directory.
124

125
  @type path: str
126
  @param path: the directory to clean
127
  @type exclude: list
128
  @param exclude: list of files to be excluded, defaults
129
      to the empty list
130

131
  """
132
  if not os.path.isdir(path):
133
    return
134
  if exclude is None:
135
    exclude = []
136
  else:
137
    # Normalize excluded paths
138
    exclude = [os.path.normpath(i) for i in exclude]
139

    
140
  for rel_name in utils.ListVisibleFiles(path):
141
    full_name = os.path.normpath(os.path.join(path, rel_name))
142
    if full_name in exclude:
143
      continue
144
    if os.path.isfile(full_name) and not os.path.islink(full_name):
145
      utils.RemoveFile(full_name)
146

    
147

    
148
def JobQueuePurge():
149
  """Removes job queue files and archived jobs.
150

151
  @rtype: tuple
152
  @return: True, None
153

154
  """
155
  _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE])
156
  _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
157

    
158

    
159
def GetMasterInfo():
160
  """Returns master information.
161

162
  This is an utility function to compute master information, either
163
  for consumption here or from the node daemon.
164

165
  @rtype: tuple
166
  @return: master_netdev, master_ip, master_name
167
  @raise RPCFail: in case of errors
168

169
  """
170
  try:
171
    cfg = _GetConfig()
172
    master_netdev = cfg.GetMasterNetdev()
173
    master_ip = cfg.GetMasterIP()
174
    master_node = cfg.GetMasterNode()
175
  except errors.ConfigurationError, err:
176
    _Fail("Cluster configuration incomplete: %s", err, exc=True)
177
  return master_netdev, master_ip, master_node
178

    
179

    
180
def StartMaster(start_daemons):
181
  """Activate local node as master node.
182

183
  The function will always try activate the IP address of the master
184
  (unless someone else has it). It will also start the master daemons,
185
  based on the start_daemons parameter.
186

187
  @type start_daemons: boolean
188
  @param start_daemons: whether to also start the master
189
      daemons (ganeti-masterd and ganeti-rapi)
190
  @rtype: None
191

192
  """
193
  # GetMasterInfo will raise an exception if not able to return data
194
  master_netdev, master_ip, _ = GetMasterInfo()
195

    
196
  err_msgs = []
197
  if utils.TcpPing(master_ip, constants.DEFAULT_NODED_PORT):
198
    if utils.OwnIpAddress(master_ip):
199
      # we already have the ip:
200
      logging.debug("Master IP already configured, doing nothing")
201
    else:
202
      msg = "Someone else has the master ip, not activating"
203
      logging.error(msg)
204
      err_msgs.append(msg)
205
  else:
206
    result = utils.RunCmd(["ip", "address", "add", "%s/32" % master_ip,
207
                           "dev", master_netdev, "label",
208
                           "%s:0" % master_netdev])
209
    if result.failed:
210
      msg = "Can't activate master IP: %s" % result.output
211
      logging.error(msg)
212
      err_msgs.append(msg)
213

    
214
    result = utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_netdev,
215
                           "-s", master_ip, master_ip])
216
    # we'll ignore the exit code of arping
217

    
218
  # and now start the master and rapi daemons
219
  if start_daemons:
220
    for daemon in 'ganeti-masterd', 'ganeti-rapi':
221
      result = utils.RunCmd([daemon])
222
      if result.failed:
223
        msg = "Can't start daemon %s: %s" % (daemon, result.output)
224
        logging.error(msg)
225
        err_msgs.append(msg)
226

    
227
  if err_msgs:
228
    _Fail("; ".join(err_msgs))
229

    
230

    
231
def StopMaster(stop_daemons):
232
  """Deactivate this node as master.
233

234
  The function will always try to deactivate the IP address of the
235
  master. It will also stop the master daemons depending on the
236
  stop_daemons parameter.
237

238
  @type stop_daemons: boolean
239
  @param stop_daemons: whether to also stop the master daemons
240
      (ganeti-masterd and ganeti-rapi)
241
  @rtype: None
242

243
  """
244
  # TODO: log and report back to the caller the error failures; we
245
  # need to decide in which case we fail the RPC for this
246

    
247
  # GetMasterInfo will raise an exception if not able to return data
248
  master_netdev, master_ip, _ = GetMasterInfo()
249

    
250
  result = utils.RunCmd(["ip", "address", "del", "%s/32" % master_ip,
251
                         "dev", master_netdev])
252
  if result.failed:
253
    logging.error("Can't remove the master IP, error: %s", result.output)
254
    # but otherwise ignore the failure
255

    
256
  if stop_daemons:
257
    # stop/kill the rapi and the master daemon
258
    for daemon in constants.RAPI_PID, constants.MASTERD_PID:
259
      utils.KillProcess(utils.ReadPidFile(utils.DaemonPidFileName(daemon)))
260

    
261

    
262
def AddNode(dsa, dsapub, rsa, rsapub, sshkey, sshpub):
263
  """Joins this node to the cluster.
264

265
  This does the following:
266
      - updates the hostkeys of the machine (rsa and dsa)
267
      - adds the ssh private key to the user
268
      - adds the ssh public key to the users' authorized_keys file
269

270
  @type dsa: str
271
  @param dsa: the DSA private key to write
272
  @type dsapub: str
273
  @param dsapub: the DSA public key to write
274
  @type rsa: str
275
  @param rsa: the RSA private key to write
276
  @type rsapub: str
277
  @param rsapub: the RSA public key to write
278
  @type sshkey: str
279
  @param sshkey: the SSH private key to write
280
  @type sshpub: str
281
  @param sshpub: the SSH public key to write
282
  @rtype: boolean
283
  @return: the success of the operation
284

285
  """
286
  sshd_keys =  [(constants.SSH_HOST_RSA_PRIV, rsa, 0600),
287
                (constants.SSH_HOST_RSA_PUB, rsapub, 0644),
288
                (constants.SSH_HOST_DSA_PRIV, dsa, 0600),
289
                (constants.SSH_HOST_DSA_PUB, dsapub, 0644)]
290
  for name, content, mode in sshd_keys:
291
    utils.WriteFile(name, data=content, mode=mode)
292

    
293
  try:
294
    priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS,
295
                                                    mkdir=True)
296
  except errors.OpExecError, err:
297
    _Fail("Error while processing user ssh files: %s", err, exc=True)
298

    
299
  for name, content in [(priv_key, sshkey), (pub_key, sshpub)]:
300
    utils.WriteFile(name, data=content, mode=0600)
301

    
302
  utils.AddAuthorizedKey(auth_keys, sshpub)
303

    
304
  utils.RunCmd([constants.SSH_INITD_SCRIPT, "restart"])
305

    
306

    
307
def LeaveCluster():
308
  """Cleans up and remove the current node.
309

310
  This function cleans up and prepares the current node to be removed
311
  from the cluster.
312

313
  If processing is successful, then it raises an
314
  L{errors.QuitGanetiException} which is used as a special case to
315
  shutdown the node daemon.
316

317
  """
318
  _CleanDirectory(constants.DATA_DIR)
319
  JobQueuePurge()
320

    
321
  try:
322
    priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
323

    
324
    f = open(pub_key, 'r')
325
    try:
326
      utils.RemoveAuthorizedKey(auth_keys, f.read(8192))
327
    finally:
328
      f.close()
329

    
330
    utils.RemoveFile(priv_key)
331
    utils.RemoveFile(pub_key)
332
  except errors.OpExecError:
333
    logging.exception("Error while processing ssh files")
334

    
335
  # Raise a custom exception (handled in ganeti-noded)
336
  raise errors.QuitGanetiException(True, 'Shutdown scheduled')
337

    
338

    
339
def GetNodeInfo(vgname, hypervisor_type):
340
  """Gives back a hash with different informations about the node.
341

342
  @type vgname: C{string}
343
  @param vgname: the name of the volume group to ask for disk space information
344
  @type hypervisor_type: C{str}
345
  @param hypervisor_type: the name of the hypervisor to ask for
346
      memory information
347
  @rtype: C{dict}
348
  @return: dictionary with the following keys:
349
      - vg_size is the size of the configured volume group in MiB
350
      - vg_free is the free size of the volume group in MiB
351
      - memory_dom0 is the memory allocated for domain0 in MiB
352
      - memory_free is the currently available (free) ram in MiB
353
      - memory_total is the total number of ram in MiB
354

355
  """
356
  outputarray = {}
357
  vginfo = _GetVGInfo(vgname)
358
  outputarray['vg_size'] = vginfo['vg_size']
359
  outputarray['vg_free'] = vginfo['vg_free']
360

    
361
  hyper = hypervisor.GetHypervisor(hypervisor_type)
362
  hyp_info = hyper.GetNodeInfo()
363
  if hyp_info is not None:
364
    outputarray.update(hyp_info)
365

    
366
  f = open("/proc/sys/kernel/random/boot_id", 'r')
367
  try:
368
    outputarray["bootid"] = f.read(128).rstrip("\n")
369
  finally:
370
    f.close()
371

    
372
  return outputarray
373

    
374

    
375
def VerifyNode(what, cluster_name):
376
  """Verify the status of the local node.
377

378
  Based on the input L{what} parameter, various checks are done on the
379
  local node.
380

381
  If the I{filelist} key is present, this list of
382
  files is checksummed and the file/checksum pairs are returned.
383

384
  If the I{nodelist} key is present, we check that we have
385
  connectivity via ssh with the target nodes (and check the hostname
386
  report).
387

388
  If the I{node-net-test} key is present, we check that we have
389
  connectivity to the given nodes via both primary IP and, if
390
  applicable, secondary IPs.
391

392
  @type what: C{dict}
393
  @param what: a dictionary of things to check:
394
      - filelist: list of files for which to compute checksums
395
      - nodelist: list of nodes we should check ssh communication with
396
      - node-net-test: list of nodes we should check node daemon port
397
        connectivity with
398
      - hypervisor: list with hypervisors to run the verify for
399
  @rtype: dict
400
  @return: a dictionary with the same keys as the input dict, and
401
      values representing the result of the checks
402

403
  """
404
  result = {}
405

    
406
  if constants.NV_HYPERVISOR in what:
407
    result[constants.NV_HYPERVISOR] = tmp = {}
408
    for hv_name in what[constants.NV_HYPERVISOR]:
409
      tmp[hv_name] = hypervisor.GetHypervisor(hv_name).Verify()
410

    
411
  if constants.NV_FILELIST in what:
412
    result[constants.NV_FILELIST] = utils.FingerprintFiles(
413
      what[constants.NV_FILELIST])
414

    
415
  if constants.NV_NODELIST in what:
416
    result[constants.NV_NODELIST] = tmp = {}
417
    random.shuffle(what[constants.NV_NODELIST])
418
    for node in what[constants.NV_NODELIST]:
419
      success, message = _GetSshRunner(cluster_name).VerifyNodeHostname(node)
420
      if not success:
421
        tmp[node] = message
422

    
423
  if constants.NV_NODENETTEST in what:
424
    result[constants.NV_NODENETTEST] = tmp = {}
425
    my_name = utils.HostInfo().name
426
    my_pip = my_sip = None
427
    for name, pip, sip in what[constants.NV_NODENETTEST]:
428
      if name == my_name:
429
        my_pip = pip
430
        my_sip = sip
431
        break
432
    if not my_pip:
433
      tmp[my_name] = ("Can't find my own primary/secondary IP"
434
                      " in the node list")
435
    else:
436
      port = utils.GetNodeDaemonPort()
437
      for name, pip, sip in what[constants.NV_NODENETTEST]:
438
        fail = []
439
        if not utils.TcpPing(pip, port, source=my_pip):
440
          fail.append("primary")
441
        if sip != pip:
442
          if not utils.TcpPing(sip, port, source=my_sip):
443
            fail.append("secondary")
444
        if fail:
445
          tmp[name] = ("failure using the %s interface(s)" %
446
                       " and ".join(fail))
447

    
448
  if constants.NV_LVLIST in what:
449
    result[constants.NV_LVLIST] = GetVolumeList(what[constants.NV_LVLIST])
450

    
451
  if constants.NV_INSTANCELIST in what:
452
    result[constants.NV_INSTANCELIST] = GetInstanceList(
453
      what[constants.NV_INSTANCELIST])
454

    
455
  if constants.NV_VGLIST in what:
456
    result[constants.NV_VGLIST] = utils.ListVolumeGroups()
457

    
458
  if constants.NV_VERSION in what:
459
    result[constants.NV_VERSION] = (constants.PROTOCOL_VERSION,
460
                                    constants.RELEASE_VERSION)
461

    
462
  if constants.NV_HVINFO in what:
463
    hyper = hypervisor.GetHypervisor(what[constants.NV_HVINFO])
464
    result[constants.NV_HVINFO] = hyper.GetNodeInfo()
465

    
466
  if constants.NV_DRBDLIST in what:
467
    try:
468
      used_minors = bdev.DRBD8.GetUsedDevs().keys()
469
    except errors.BlockDeviceError, err:
470
      logging.warning("Can't get used minors list", exc_info=True)
471
      used_minors = str(err)
472
    result[constants.NV_DRBDLIST] = used_minors
473

    
474
  return result
475

    
476

    
477
def GetVolumeList(vg_name):
478
  """Compute list of logical volumes and their size.
479

480
  @type vg_name: str
481
  @param vg_name: the volume group whose LVs we should list
482
  @rtype: dict
483
  @return:
484
      dictionary of all partions (key) with value being a tuple of
485
      their size (in MiB), inactive and online status::
486

487
        {'test1': ('20.06', True, True)}
488

489
      in case of errors, a string is returned with the error
490
      details.
491

492
  """
493
  lvs = {}
494
  sep = '|'
495
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
496
                         "--separator=%s" % sep,
497
                         "-olv_name,lv_size,lv_attr", vg_name])
498
  if result.failed:
499
    _Fail("Failed to list logical volumes, lvs output: %s", result.output)
500

    
501
  valid_line_re = re.compile("^ *([^|]+)\|([0-9.]+)\|([^|]{6})\|?$")
502
  for line in result.stdout.splitlines():
503
    line = line.strip()
504
    match = valid_line_re.match(line)
505
    if not match:
506
      logging.error("Invalid line returned from lvs output: '%s'", line)
507
      continue
508
    name, size, attr = match.groups()
509
    inactive = attr[4] == '-'
510
    online = attr[5] == 'o'
511
    lvs[name] = (size, inactive, online)
512

    
513
  return lvs
514

    
515

    
516
def ListVolumeGroups():
517
  """List the volume groups and their size.
518

519
  @rtype: dict
520
  @return: dictionary with keys volume name and values the
521
      size of the volume
522

523
  """
524
  return utils.ListVolumeGroups()
525

    
526

    
527
def NodeVolumes():
528
  """List all volumes on this node.
529

530
  @rtype: list
531
  @return:
532
    A list of dictionaries, each having four keys:
533
      - name: the logical volume name,
534
      - size: the size of the logical volume
535
      - dev: the physical device on which the LV lives
536
      - vg: the volume group to which it belongs
537

538
    In case of errors, we return an empty list and log the
539
    error.
540

541
    Note that since a logical volume can live on multiple physical
542
    volumes, the resulting list might include a logical volume
543
    multiple times.
544

545
  """
546
  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
547
                         "--separator=|",
548
                         "--options=lv_name,lv_size,devices,vg_name"])
549
  if result.failed:
550
    _Fail("Failed to list logical volumes, lvs output: %s",
551
          result.output)
552

    
553
  def parse_dev(dev):
554
    if '(' in dev:
555
      return dev.split('(')[0]
556
    else:
557
      return dev
558

    
559
  def map_line(line):
560
    return {
561
      'name': line[0].strip(),
562
      'size': line[1].strip(),
563
      'dev': parse_dev(line[2].strip()),
564
      'vg': line[3].strip(),
565
    }
566

    
567
  return [map_line(line.split('|')) for line in result.stdout.splitlines()
568
          if line.count('|') >= 3]
569

    
570

    
571
def BridgesExist(bridges_list):
572
  """Check if a list of bridges exist on the current node.
573

574
  @rtype: boolean
575
  @return: C{True} if all of them exist, C{False} otherwise
576

577
  """
578
  missing = []
579
  for bridge in bridges_list:
580
    if not utils.BridgeExists(bridge):
581
      missing.append(bridge)
582

    
583
  if missing:
584
    _Fail("Missing bridges %s", ", ".join(missing))
585

    
586

    
587
def GetInstanceList(hypervisor_list):
588
  """Provides a list of instances.
589

590
  @type hypervisor_list: list
591
  @param hypervisor_list: the list of hypervisors to query information
592

593
  @rtype: list
594
  @return: a list of all running instances on the current node
595
    - instance1.example.com
596
    - instance2.example.com
597

598
  """
599
  results = []
600
  for hname in hypervisor_list:
601
    try:
602
      names = hypervisor.GetHypervisor(hname).ListInstances()
603
      results.extend(names)
604
    except errors.HypervisorError, err:
605
      _Fail("Error enumerating instances (hypervisor %s): %s",
606
            hname, err, exc=True)
607

    
608
  return results
609

    
610

    
611
def GetInstanceInfo(instance, hname):
612
  """Gives back the informations about an instance as a dictionary.
613

614
  @type instance: string
615
  @param instance: the instance name
616
  @type hname: string
617
  @param hname: the hypervisor type of the instance
618

619
  @rtype: dict
620
  @return: dictionary with the following keys:
621
      - memory: memory size of instance (int)
622
      - state: xen state of instance (string)
623
      - time: cpu time of instance (float)
624

625
  """
626
  output = {}
627

    
628
  iinfo = hypervisor.GetHypervisor(hname).GetInstanceInfo(instance)
629
  if iinfo is not None:
630
    output['memory'] = iinfo[2]
631
    output['state'] = iinfo[4]
632
    output['time'] = iinfo[5]
633

    
634
  return output
635

    
636

    
637
def GetInstanceMigratable(instance):
638
  """Gives whether an instance can be migrated.
639

640
  @type instance: L{objects.Instance}
641
  @param instance: object representing the instance to be checked.
642

643
  @rtype: tuple
644
  @return: tuple of (result, description) where:
645
      - result: whether the instance can be migrated or not
646
      - description: a description of the issue, if relevant
647

648
  """
649
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
650
  iname = instance.name
651
  if iname not in hyper.ListInstances():
652
    _Fail("Instance %s is not running", iname)
653

    
654
  for idx in range(len(instance.disks)):
655
    link_name = _GetBlockDevSymlinkPath(iname, idx)
656
    if not os.path.islink(link_name):
657
      _Fail("Instance %s was not restarted since ganeti 1.2.5", iname)
658

    
659

    
660
def GetAllInstancesInfo(hypervisor_list):
661
  """Gather data about all instances.
662

663
  This is the equivalent of L{GetInstanceInfo}, except that it
664
  computes data for all instances at once, thus being faster if one
665
  needs data about more than one instance.
666

667
  @type hypervisor_list: list
668
  @param hypervisor_list: list of hypervisors to query for instance data
669

670
  @rtype: dict
671
  @return: dictionary of instance: data, with data having the following keys:
672
      - memory: memory size of instance (int)
673
      - state: xen state of instance (string)
674
      - time: cpu time of instance (float)
675
      - vcpus: the number of vcpus
676

677
  """
678
  output = {}
679

    
680
  for hname in hypervisor_list:
681
    iinfo = hypervisor.GetHypervisor(hname).GetAllInstancesInfo()
682
    if iinfo:
683
      for name, _, memory, vcpus, state, times in iinfo:
684
        value = {
685
          'memory': memory,
686
          'vcpus': vcpus,
687
          'state': state,
688
          'time': times,
689
          }
690
        if name in output:
691
          # we only check static parameters, like memory and vcpus,
692
          # and not state and time which can change between the
693
          # invocations of the different hypervisors
694
          for key in 'memory', 'vcpus':
695
            if value[key] != output[name][key]:
696
              _Fail("Instance %s is running twice"
697
                    " with different parameters", name)
698
        output[name] = value
699

    
700
  return output
701

    
702

    
703
def InstanceOsAdd(instance, reinstall):
704
  """Add an OS to an instance.
705

706
  @type instance: L{objects.Instance}
707
  @param instance: Instance whose OS is to be installed
708
  @type reinstall: boolean
709
  @param reinstall: whether this is an instance reinstall
710
  @rtype: None
711

712
  """
713
  inst_os = OSFromDisk(instance.os)
714

    
715
  create_env = OSEnvironment(instance)
716
  if reinstall:
717
    create_env['INSTANCE_REINSTALL'] = "1"
718

    
719
  logfile = "%s/add-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
720
                                     instance.name, int(time.time()))
721

    
722
  result = utils.RunCmd([inst_os.create_script], env=create_env,
723
                        cwd=inst_os.path, output=logfile,)
724
  if result.failed:
725
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
726
                  " output: %s", result.cmd, result.fail_reason, logfile,
727
                  result.output)
728
    lines = [utils.SafeEncode(val)
729
             for val in utils.TailFile(logfile, lines=20)]
730
    _Fail("OS create script failed (%s), last lines in the"
731
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
732

    
733

    
734
def RunRenameInstance(instance, old_name):
735
  """Run the OS rename script for an instance.
736

737
  @type instance: L{objects.Instance}
738
  @param instance: Instance whose OS is to be installed
739
  @type old_name: string
740
  @param old_name: previous instance name
741
  @rtype: boolean
742
  @return: the success of the operation
743

744
  """
745
  inst_os = OSFromDisk(instance.os)
746

    
747
  rename_env = OSEnvironment(instance)
748
  rename_env['OLD_INSTANCE_NAME'] = old_name
749

    
750
  logfile = "%s/rename-%s-%s-%s-%d.log" % (constants.LOG_OS_DIR, instance.os,
751
                                           old_name,
752
                                           instance.name, int(time.time()))
753

    
754
  result = utils.RunCmd([inst_os.rename_script], env=rename_env,
755
                        cwd=inst_os.path, output=logfile)
756

    
757
  if result.failed:
758
    logging.error("os create command '%s' returned error: %s output: %s",
759
                  result.cmd, result.fail_reason, result.output)
760
    lines = [utils.SafeEncode(val)
761
             for val in utils.TailFile(logfile, lines=20)]
762
    _Fail("OS rename script failed (%s), last lines in the"
763
          " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
764

    
765

    
766
def _GetVGInfo(vg_name):
767
  """Get informations about the volume group.
768

769
  @type vg_name: str
770
  @param vg_name: the volume group which we query
771
  @rtype: dict
772
  @return:
773
    A dictionary with the following keys:
774
      - C{vg_size} is the total size of the volume group in MiB
775
      - C{vg_free} is the free size of the volume group in MiB
776
      - C{pv_count} are the number of physical disks in that VG
777

778
    If an error occurs during gathering of data, we return the same dict
779
    with keys all set to None.
780

781
  """
782
  retdic = dict.fromkeys(["vg_size", "vg_free", "pv_count"])
783

    
784
  retval = utils.RunCmd(["vgs", "-ovg_size,vg_free,pv_count", "--noheadings",
785
                         "--nosuffix", "--units=m", "--separator=:", vg_name])
786

    
787
  if retval.failed:
788
    logging.error("volume group %s not present", vg_name)
789
    return retdic
790
  valarr = retval.stdout.strip().rstrip(':').split(':')
791
  if len(valarr) == 3:
792
    try:
793
      retdic = {
794
        "vg_size": int(round(float(valarr[0]), 0)),
795
        "vg_free": int(round(float(valarr[1]), 0)),
796
        "pv_count": int(valarr[2]),
797
        }
798
    except ValueError, err:
799
      logging.exception("Fail to parse vgs output: %s", err)
800
  else:
801
    logging.error("vgs output has the wrong number of fields (expected"
802
                  " three): %s", str(valarr))
803
  return retdic
804

    
805

    
806
def _GetBlockDevSymlinkPath(instance_name, idx):
807
  return os.path.join(constants.DISK_LINKS_DIR,
808
                      "%s:%d" % (instance_name, idx))
809

    
810

    
811
def _SymlinkBlockDev(instance_name, device_path, idx):
812
  """Set up symlinks to a instance's block device.
813

814
  This is an auxiliary function run when an instance is start (on the primary
815
  node) or when an instance is migrated (on the target node).
816

817

818
  @param instance_name: the name of the target instance
819
  @param device_path: path of the physical block device, on the node
820
  @param idx: the disk index
821
  @return: absolute path to the disk's symlink
822

823
  """
824
  link_name = _GetBlockDevSymlinkPath(instance_name, idx)
825
  try:
826
    os.symlink(device_path, link_name)
827
  except OSError, err:
828
    if err.errno == errno.EEXIST:
829
      if (not os.path.islink(link_name) or
830
          os.readlink(link_name) != device_path):
831
        os.remove(link_name)
832
        os.symlink(device_path, link_name)
833
    else:
834
      raise
835

    
836
  return link_name
837

    
838

    
839
def _RemoveBlockDevLinks(instance_name, disks):
840
  """Remove the block device symlinks belonging to the given instance.
841

842
  """
843
  for idx, _ in enumerate(disks):
844
    link_name = _GetBlockDevSymlinkPath(instance_name, idx)
845
    if os.path.islink(link_name):
846
      try:
847
        os.remove(link_name)
848
      except OSError:
849
        logging.exception("Can't remove symlink '%s'", link_name)
850

    
851

    
852
def _GatherAndLinkBlockDevs(instance):
853
  """Set up an instance's block device(s).
854

855
  This is run on the primary node at instance startup. The block
856
  devices must be already assembled.
857

858
  @type instance: L{objects.Instance}
859
  @param instance: the instance whose disks we shoul assemble
860
  @rtype: list
861
  @return: list of (disk_object, device_path)
862

863
  """
864
  block_devices = []
865
  for idx, disk in enumerate(instance.disks):
866
    device = _RecursiveFindBD(disk)
867
    if device is None:
868
      raise errors.BlockDeviceError("Block device '%s' is not set up." %
869
                                    str(disk))
870
    device.Open()
871
    try:
872
      link_name = _SymlinkBlockDev(instance.name, device.dev_path, idx)
873
    except OSError, e:
874
      raise errors.BlockDeviceError("Cannot create block device symlink: %s" %
875
                                    e.strerror)
876

    
877
    block_devices.append((disk, link_name))
878

    
879
  return block_devices
880

    
881

    
882
def StartInstance(instance):
883
  """Start an instance.
884

885
  @type instance: L{objects.Instance}
886
  @param instance: the instance object
887
  @rtype: None
888

889
  """
890
  running_instances = GetInstanceList([instance.hypervisor])
891

    
892
  if instance.name in running_instances:
893
    logging.info("Instance %s already running, not starting", instance.name)
894
    return
895

    
896
  try:
897
    block_devices = _GatherAndLinkBlockDevs(instance)
898
    hyper = hypervisor.GetHypervisor(instance.hypervisor)
899
    hyper.StartInstance(instance, block_devices)
900
  except errors.BlockDeviceError, err:
901
    _Fail("Block device error: %s", err, exc=True)
902
  except errors.HypervisorError, err:
903
    _RemoveBlockDevLinks(instance.name, instance.disks)
904
    _Fail("Hypervisor error: %s", err, exc=True)
905

    
906

    
907
def InstanceShutdown(instance):
908
  """Shut an instance down.
909

910
  @note: this functions uses polling with a hardcoded timeout.
911

912
  @type instance: L{objects.Instance}
913
  @param instance: the instance object
914
  @rtype: None
915

916
  """
917
  hv_name = instance.hypervisor
918
  running_instances = GetInstanceList([hv_name])
919
  iname = instance.name
920

    
921
  if iname not in running_instances:
922
    logging.info("Instance %s not running, doing nothing", iname)
923
    return
924

    
925
  hyper = hypervisor.GetHypervisor(hv_name)
926
  try:
927
    hyper.StopInstance(instance)
928
  except errors.HypervisorError, err:
929
    _Fail("Failed to stop instance %s: %s", iname, err)
930

    
931
  # test every 10secs for 2min
932

    
933
  time.sleep(1)
934
  for dummy in range(11):
935
    if instance.name not in GetInstanceList([hv_name]):
936
      break
937
    time.sleep(10)
938
  else:
939
    # the shutdown did not succeed
940
    logging.error("Shutdown of '%s' unsuccessful, using destroy", iname)
941

    
942
    try:
943
      hyper.StopInstance(instance, force=True)
944
    except errors.HypervisorError, err:
945
      _Fail("Failed to force stop instance %s: %s", iname, err)
946

    
947
    time.sleep(1)
948
    if instance.name in GetInstanceList([hv_name]):
949
      _Fail("Could not shutdown instance %s even by destroy", iname)
950

    
951
  _RemoveBlockDevLinks(iname, instance.disks)
952

    
953

    
954
def InstanceReboot(instance, reboot_type):
955
  """Reboot an instance.
956

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

971
  """
972
  running_instances = GetInstanceList([instance.hypervisor])
973

    
974
  if instance.name not in running_instances:
975
    _Fail("Cannot reboot instance %s that is not running", instance.name)
976

    
977
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
978
  if reboot_type == constants.INSTANCE_REBOOT_SOFT:
979
    try:
980
      hyper.RebootInstance(instance)
981
    except errors.HypervisorError, err:
982
      _Fail("Failed to soft reboot instance %s: %s", instance.name, err)
983
  elif reboot_type == constants.INSTANCE_REBOOT_HARD:
984
    try:
985
      InstanceShutdown(instance)
986
      return StartInstance(instance)
987
    except errors.HypervisorError, err:
988
      _Fail("Failed to hard reboot instance %s: %s", instance.name, err)
989
  else:
990
    _Fail("Invalid reboot_type received: %s", reboot_type)
991

    
992

    
993
def MigrationInfo(instance):
994
  """Gather information about an instance to be migrated.
995

996
  @type instance: L{objects.Instance}
997
  @param instance: the instance definition
998

999
  """
1000
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1001
  try:
1002
    info = hyper.MigrationInfo(instance)
1003
  except errors.HypervisorError, err:
1004
    _Fail("Failed to fetch migration information: %s", err, exc=True)
1005
  return info
1006

    
1007

    
1008
def AcceptInstance(instance, info, target):
1009
  """Prepare the node to accept an instance.
1010

1011
  @type instance: L{objects.Instance}
1012
  @param instance: the instance definition
1013
  @type info: string/data (opaque)
1014
  @param info: migration information, from the source node
1015
  @type target: string
1016
  @param target: target host (usually ip), on this node
1017

1018
  """
1019
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1020
  try:
1021
    hyper.AcceptInstance(instance, info, target)
1022
  except errors.HypervisorError, err:
1023
    _Fail("Failed to accept instance: %s", err, exc=True)
1024

    
1025

    
1026
def FinalizeMigration(instance, info, success):
1027
  """Finalize any preparation to accept an instance.
1028

1029
  @type instance: L{objects.Instance}
1030
  @param instance: the instance definition
1031
  @type info: string/data (opaque)
1032
  @param info: migration information, from the source node
1033
  @type success: boolean
1034
  @param success: whether the migration was a success or a failure
1035

1036
  """
1037
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1038
  try:
1039
    hyper.FinalizeMigration(instance, info, success)
1040
  except errors.HypervisorError, err:
1041
    _Fail("Failed to finalize migration: %s", err, exc=True)
1042

    
1043

    
1044
def MigrateInstance(instance, target, live):
1045
  """Migrates an instance to another node.
1046

1047
  @type instance: L{objects.Instance}
1048
  @param instance: the instance definition
1049
  @type target: string
1050
  @param target: the target node name
1051
  @type live: boolean
1052
  @param live: whether the migration should be done live or not (the
1053
      interpretation of this parameter is left to the hypervisor)
1054
  @rtype: tuple
1055
  @return: a tuple of (success, msg) where:
1056
      - succes is a boolean denoting the success/failure of the operation
1057
      - msg is a string with details in case of failure
1058

1059
  """
1060
  hyper = hypervisor.GetHypervisor(instance.hypervisor)
1061

    
1062
  try:
1063
    hyper.MigrateInstance(instance.name, target, live)
1064
  except errors.HypervisorError, err:
1065
    _Fail("Failed to migrate instance: %s", err, exc=True)
1066

    
1067

    
1068
def BlockdevCreate(disk, size, owner, on_primary, info):
1069
  """Creates a block device for an instance.
1070

1071
  @type disk: L{objects.Disk}
1072
  @param disk: the object describing the disk we should create
1073
  @type size: int
1074
  @param size: the size of the physical underlying device, in MiB
1075
  @type owner: str
1076
  @param owner: the name of the instance for which disk is created,
1077
      used for device cache data
1078
  @type on_primary: boolean
1079
  @param on_primary:  indicates if it is the primary node or not
1080
  @type info: string
1081
  @param info: string that will be sent to the physical device
1082
      creation, used for example to set (LVM) tags on LVs
1083

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

1088
  """
1089
  clist = []
1090
  if disk.children:
1091
    for child in disk.children:
1092
      try:
1093
        crdev = _RecursiveAssembleBD(child, owner, on_primary)
1094
      except errors.BlockDeviceError, err:
1095
        _Fail("Can't assemble device %s: %s", child, err)
1096
      if on_primary or disk.AssembleOnSecondary():
1097
        # we need the children open in case the device itself has to
1098
        # be assembled
1099
        try:
1100
          crdev.Open()
1101
        except errors.BlockDeviceError, err:
1102
          _Fail("Can't make child '%s' read-write: %s", child, err)
1103
      clist.append(crdev)
1104

    
1105
  try:
1106
    device = bdev.Create(disk.dev_type, disk.physical_id, clist, disk.size)
1107
  except errors.BlockDeviceError, err:
1108
    _Fail("Can't create block device: %s", err)
1109

    
1110
  if on_primary or disk.AssembleOnSecondary():
1111
    try:
1112
      device.Assemble()
1113
    except errors.BlockDeviceError, err:
1114
      _Fail("Can't assemble device after creation, unusual event: %s", err)
1115
    device.SetSyncSpeed(constants.SYNC_SPEED)
1116
    if on_primary or disk.OpenOnSecondary():
1117
      try:
1118
        device.Open(force=True)
1119
      except errors.BlockDeviceError, err:
1120
        _Fail("Can't make device r/w after creation, unusual event: %s", err)
1121
    DevCacheManager.UpdateCache(device.dev_path, owner,
1122
                                on_primary, disk.iv_name)
1123

    
1124
  device.SetInfo(info)
1125

    
1126
  return device.unique_id
1127

    
1128

    
1129
def BlockdevRemove(disk):
1130
  """Remove a block device.
1131

1132
  @note: This is intended to be called recursively.
1133

1134
  @type disk: L{objects.Disk}
1135
  @param disk: the disk object we should remove
1136
  @rtype: boolean
1137
  @return: the success of the operation
1138

1139
  """
1140
  msgs = []
1141
  try:
1142
    rdev = _RecursiveFindBD(disk)
1143
  except errors.BlockDeviceError, err:
1144
    # probably can't attach
1145
    logging.info("Can't attach to device %s in remove", disk)
1146
    rdev = None
1147
  if rdev is not None:
1148
    r_path = rdev.dev_path
1149
    try:
1150
      rdev.Remove()
1151
    except errors.BlockDeviceError, err:
1152
      msgs.append(str(err))
1153
    if not msgs:
1154
      DevCacheManager.RemoveCache(r_path)
1155

    
1156
  if disk.children:
1157
    for child in disk.children:
1158
      try:
1159
        BlockdevRemove(child)
1160
      except RPCFail, err:
1161
        msgs.append(str(err))
1162

    
1163
  if msgs:
1164
    _Fail("; ".join(msgs))
1165

    
1166

    
1167
def _RecursiveAssembleBD(disk, owner, as_primary):
1168
  """Activate a block device for an instance.
1169

1170
  This is run on the primary and secondary nodes for an instance.
1171

1172
  @note: this function is called recursively.
1173

1174
  @type disk: L{objects.Disk}
1175
  @param disk: the disk we try to assemble
1176
  @type owner: str
1177
  @param owner: the name of the instance which owns the disk
1178
  @type as_primary: boolean
1179
  @param as_primary: if we should make the block device
1180
      read/write
1181

1182
  @return: the assembled device or None (in case no device
1183
      was assembled)
1184
  @raise errors.BlockDeviceError: in case there is an error
1185
      during the activation of the children or the device
1186
      itself
1187

1188
  """
1189
  children = []
1190
  if disk.children:
1191
    mcn = disk.ChildrenNeeded()
1192
    if mcn == -1:
1193
      mcn = 0 # max number of Nones allowed
1194
    else:
1195
      mcn = len(disk.children) - mcn # max number of Nones
1196
    for chld_disk in disk.children:
1197
      try:
1198
        cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
1199
      except errors.BlockDeviceError, err:
1200
        if children.count(None) >= mcn:
1201
          raise
1202
        cdev = None
1203
        logging.error("Error in child activation (but continuing): %s",
1204
                      str(err))
1205
      children.append(cdev)
1206

    
1207
  if as_primary or disk.AssembleOnSecondary():
1208
    r_dev = bdev.Assemble(disk.dev_type, disk.physical_id, children, disk.size)
1209
    r_dev.SetSyncSpeed(constants.SYNC_SPEED)
1210
    result = r_dev
1211
    if as_primary or disk.OpenOnSecondary():
1212
      r_dev.Open()
1213
    DevCacheManager.UpdateCache(r_dev.dev_path, owner,
1214
                                as_primary, disk.iv_name)
1215

    
1216
  else:
1217
    result = True
1218
  return result
1219

    
1220

    
1221
def BlockdevAssemble(disk, owner, as_primary):
1222
  """Activate a block device for an instance.
1223

1224
  This is a wrapper over _RecursiveAssembleBD.
1225

1226
  @rtype: str or boolean
1227
  @return: a C{/dev/...} path for primary nodes, and
1228
      C{True} for secondary nodes
1229

1230
  """
1231
  try:
1232
    result = _RecursiveAssembleBD(disk, owner, as_primary)
1233
    if isinstance(result, bdev.BlockDev):
1234
      result = result.dev_path
1235
  except errors.BlockDeviceError, err:
1236
    _Fail("Error while assembling disk: %s", err, exc=True)
1237

    
1238
  return result
1239

    
1240

    
1241
def BlockdevShutdown(disk):
1242
  """Shut down a block device.
1243

1244
  First, if the device is assembled (Attach() is successfull), then
1245
  the device is shutdown. Then the children of the device are
1246
  shutdown.
1247

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

1252
  @type disk: L{objects.Disk}
1253
  @param disk: the description of the disk we should
1254
      shutdown
1255
  @rtype: None
1256

1257
  """
1258
  msgs = []
1259
  r_dev = _RecursiveFindBD(disk)
1260
  if r_dev is not None:
1261
    r_path = r_dev.dev_path
1262
    try:
1263
      r_dev.Shutdown()
1264
      DevCacheManager.RemoveCache(r_path)
1265
    except errors.BlockDeviceError, err:
1266
      msgs.append(str(err))
1267

    
1268
  if disk.children:
1269
    for child in disk.children:
1270
      try:
1271
        BlockdevShutdown(child)
1272
      except RPCFail, err:
1273
        msgs.append(str(err))
1274

    
1275
  if msgs:
1276
    _Fail("; ".join(msgs))
1277

    
1278

    
1279
def BlockdevAddchildren(parent_cdev, new_cdevs):
1280
  """Extend a mirrored block device.
1281

1282
  @type parent_cdev: L{objects.Disk}
1283
  @param parent_cdev: the disk to which we should add children
1284
  @type new_cdevs: list of L{objects.Disk}
1285
  @param new_cdevs: the list of children which we should add
1286
  @rtype: None
1287

1288
  """
1289
  parent_bdev = _RecursiveFindBD(parent_cdev)
1290
  if parent_bdev is None:
1291
    _Fail("Can't find parent device '%s' in add children", parent_cdev)
1292
  new_bdevs = [_RecursiveFindBD(disk) for disk in new_cdevs]
1293
  if new_bdevs.count(None) > 0:
1294
    _Fail("Can't find new device(s) to add: %s:%s", new_bdevs, new_cdevs)
1295
  parent_bdev.AddChildren(new_bdevs)
1296

    
1297

    
1298
def BlockdevRemovechildren(parent_cdev, new_cdevs):
1299
  """Shrink a mirrored block device.
1300

1301
  @type parent_cdev: L{objects.Disk}
1302
  @param parent_cdev: the disk from which we should remove children
1303
  @type new_cdevs: list of L{objects.Disk}
1304
  @param new_cdevs: the list of children which we should remove
1305
  @rtype: None
1306

1307
  """
1308
  parent_bdev = _RecursiveFindBD(parent_cdev)
1309
  if parent_bdev is None:
1310
    _Fail("Can't find parent device '%s' in remove children", parent_cdev)
1311
  devs = []
1312
  for disk in new_cdevs:
1313
    rpath = disk.StaticDevPath()
1314
    if rpath is None:
1315
      bd = _RecursiveFindBD(disk)
1316
      if bd is None:
1317
        _Fail("Can't find device %s while removing children", disk)
1318
      else:
1319
        devs.append(bd.dev_path)
1320
    else:
1321
      devs.append(rpath)
1322
  parent_bdev.RemoveChildren(devs)
1323

    
1324

    
1325
def BlockdevGetmirrorstatus(disks):
1326
  """Get the mirroring status of a list of devices.
1327

1328
  @type disks: list of L{objects.Disk}
1329
  @param disks: the list of disks which we should query
1330
  @rtype: disk
1331
  @return:
1332
      a list of (mirror_done, estimated_time) tuples, which
1333
      are the result of L{bdev.BlockDev.CombinedSyncStatus}
1334
  @raise errors.BlockDeviceError: if any of the disks cannot be
1335
      found
1336

1337
  """
1338
  stats = []
1339
  for dsk in disks:
1340
    rbd = _RecursiveFindBD(dsk)
1341
    if rbd is None:
1342
      _Fail("Can't find device %s", dsk)
1343
    stats.append(rbd.CombinedSyncStatus())
1344
  return stats
1345

    
1346

    
1347
def _RecursiveFindBD(disk):
1348
  """Check if a device is activated.
1349

1350
  If so, return informations about the real device.
1351

1352
  @type disk: L{objects.Disk}
1353
  @param disk: the disk object we need to find
1354

1355
  @return: None if the device can't be found,
1356
      otherwise the device instance
1357

1358
  """
1359
  children = []
1360
  if disk.children:
1361
    for chdisk in disk.children:
1362
      children.append(_RecursiveFindBD(chdisk))
1363

    
1364
  return bdev.FindDevice(disk.dev_type, disk.physical_id, children, disk.size)
1365

    
1366

    
1367
def BlockdevFind(disk):
1368
  """Check if a device is activated.
1369

1370
  If it is, return informations about the real device.
1371

1372
  @type disk: L{objects.Disk}
1373
  @param disk: the disk to find
1374
  @rtype: None or tuple
1375
  @return: None if the disk cannot be found, otherwise a
1376
      tuple (device_path, major, minor, sync_percent,
1377
      estimated_time, is_degraded)
1378

1379
  """
1380
  try:
1381
    rbd = _RecursiveFindBD(disk)
1382
  except errors.BlockDeviceError, err:
1383
    _Fail("Failed to find device: %s", err, exc=True)
1384
  if rbd is None:
1385
    return None
1386
  return (rbd.dev_path, rbd.major, rbd.minor) + rbd.GetSyncStatus()
1387

    
1388

    
1389
def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
1390
  """Write a file to the filesystem.
1391

1392
  This allows the master to overwrite(!) a file. It will only perform
1393
  the operation if the file belongs to a list of configuration files.
1394

1395
  @type file_name: str
1396
  @param file_name: the target file name
1397
  @type data: str
1398
  @param data: the new contents of the file
1399
  @type mode: int
1400
  @param mode: the mode to give the file (can be None)
1401
  @type uid: int
1402
  @param uid: the owner of the file (can be -1 for default)
1403
  @type gid: int
1404
  @param gid: the group of the file (can be -1 for default)
1405
  @type atime: float
1406
  @param atime: the atime to set on the file (can be None)
1407
  @type mtime: float
1408
  @param mtime: the mtime to set on the file (can be None)
1409
  @rtype: None
1410

1411
  """
1412
  if not os.path.isabs(file_name):
1413
    _Fail("Filename passed to UploadFile is not absolute: '%s'", file_name)
1414

    
1415
  allowed_files = set([
1416
    constants.CLUSTER_CONF_FILE,
1417
    constants.ETC_HOSTS,
1418
    constants.SSH_KNOWN_HOSTS_FILE,
1419
    constants.VNC_PASSWORD_FILE,
1420
    constants.RAPI_CERT_FILE,
1421
    constants.RAPI_USERS_FILE,
1422
    ])
1423

    
1424
  for hv_name in constants.HYPER_TYPES:
1425
    hv_class = hypervisor.GetHypervisor(hv_name)
1426
    allowed_files.update(hv_class.GetAncillaryFiles())
1427

    
1428
  if file_name not in allowed_files:
1429
    _Fail("Filename passed to UploadFile not in allowed upload targets: '%s'",
1430
          file_name)
1431

    
1432
  raw_data = _Decompress(data)
1433

    
1434
  utils.WriteFile(file_name, data=raw_data, mode=mode, uid=uid, gid=gid,
1435
                  atime=atime, mtime=mtime)
1436

    
1437

    
1438
def WriteSsconfFiles(values):
1439
  """Update all ssconf files.
1440

1441
  Wrapper around the SimpleStore.WriteFiles.
1442

1443
  """
1444
  ssconf.SimpleStore().WriteFiles(values)
1445

    
1446

    
1447
def _ErrnoOrStr(err):
1448
  """Format an EnvironmentError exception.
1449

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

1454
  @type err: L{EnvironmentError}
1455
  @param err: the exception to format
1456

1457
  """
1458
  if hasattr(err, 'errno'):
1459
    detail = errno.errorcode[err.errno]
1460
  else:
1461
    detail = str(err)
1462
  return detail
1463

    
1464

    
1465
def _OSOndiskAPIVersion(name, os_dir):
1466
  """Compute and return the API version of a given OS.
1467

1468
  This function will try to read the API version of the OS given by
1469
  the 'name' parameter and residing in the 'os_dir' directory.
1470

1471
  @type name: str
1472
  @param name: the OS name we should look for
1473
  @type os_dir: str
1474
  @param os_dir: the directory inwhich we should look for the OS
1475
  @rtype: tuple
1476
  @return: tuple (status, data) with status denoting the validity and
1477
      data holding either the vaid versions or an error message
1478

1479
  """
1480
  api_file = os.path.sep.join([os_dir, "ganeti_api_version"])
1481

    
1482
  try:
1483
    st = os.stat(api_file)
1484
  except EnvironmentError, err:
1485
    return False, ("Required file 'ganeti_api_version' file not"
1486
                   " found under path %s: %s" % (os_dir, _ErrnoOrStr(err)))
1487

    
1488
  if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
1489
    return False, ("File 'ganeti_api_version' file at %s is not"
1490
                   " a regular file" % os_dir)
1491

    
1492
  try:
1493
    api_versions = utils.ReadFile(api_file).splitlines()
1494
  except EnvironmentError, err:
1495
    return False, ("Error while reading the API version file at %s: %s" %
1496
                   (api_file, _ErrnoOrStr(err)))
1497

    
1498
  try:
1499
    api_versions = [int(version.strip()) for version in api_versions]
1500
  except (TypeError, ValueError), err:
1501
    return False, ("API version(s) can't be converted to integer: %s" %
1502
                   str(err))
1503

    
1504
  return True, api_versions
1505

    
1506

    
1507
def DiagnoseOS(top_dirs=None):
1508
  """Compute the validity for all OSes.
1509

1510
  @type top_dirs: list
1511
  @param top_dirs: the list of directories in which to
1512
      search (if not given defaults to
1513
      L{constants.OS_SEARCH_PATH})
1514
  @rtype: list of L{objects.OS}
1515
  @return: a list of tuples (name, path, status, diagnose)
1516
      for all (potential) OSes under all search paths, where:
1517
          - name is the (potential) OS name
1518
          - path is the full path to the OS
1519
          - status True/False is the validity of the OS
1520
          - diagnose is the error message for an invalid OS, otherwise empty
1521

1522
  """
1523
  if top_dirs is None:
1524
    top_dirs = constants.OS_SEARCH_PATH
1525

    
1526
  result = []
1527
  for dir_name in top_dirs:
1528
    if os.path.isdir(dir_name):
1529
      try:
1530
        f_names = utils.ListVisibleFiles(dir_name)
1531
      except EnvironmentError, err:
1532
        logging.exception("Can't list the OS directory %s: %s", dir_name, err)
1533
        break
1534
      for name in f_names:
1535
        os_path = os.path.sep.join([dir_name, name])
1536
        status, os_inst = _TryOSFromDisk(name, base_dir=dir_name)
1537
        if status:
1538
          diagnose = ""
1539
        else:
1540
          diagnose = os_inst
1541
        result.append((name, os_path, status, diagnose))
1542

    
1543
  return result
1544

    
1545

    
1546
def _TryOSFromDisk(name, base_dir=None):
1547
  """Create an OS instance from disk.
1548

1549
  This function will return an OS instance if the given name is a
1550
  valid OS name.
1551

1552
  @type base_dir: string
1553
  @keyword base_dir: Base directory containing OS installations.
1554
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
1555
  @rtype: tuple
1556
  @return: success and either the OS instance if we find a valid one,
1557
      or error message
1558

1559
  """
1560
  if base_dir is None:
1561
    os_dir = utils.FindFile(name, constants.OS_SEARCH_PATH, os.path.isdir)
1562
    if os_dir is None:
1563
      return False, "Directory for OS %s not found in search path" % name
1564
  else:
1565
    os_dir = os.path.sep.join([base_dir, name])
1566

    
1567
  status, api_versions = _OSOndiskAPIVersion(name, os_dir)
1568
  if not status:
1569
    # push the error up
1570
    return status, api_versions
1571

    
1572
  if constants.OS_API_VERSION not in api_versions:
1573
    return False, ("API version mismatch for path '%s': found %s, want %s." %
1574
                   (os_dir, api_versions, constants.OS_API_VERSION))
1575

    
1576
  # OS Scripts dictionary, we will populate it with the actual script names
1577
  os_scripts = dict.fromkeys(constants.OS_SCRIPTS)
1578

    
1579
  for script in os_scripts:
1580
    os_scripts[script] = os.path.sep.join([os_dir, script])
1581

    
1582
    try:
1583
      st = os.stat(os_scripts[script])
1584
    except EnvironmentError, err:
1585
      return False, ("Script '%s' under path '%s' is missing (%s)" %
1586
                     (script, os_dir, _ErrnoOrStr(err)))
1587

    
1588
    if stat.S_IMODE(st.st_mode) & stat.S_IXUSR != stat.S_IXUSR:
1589
      return False, ("Script '%s' under path '%s' is not executable" %
1590
                     (script, os_dir))
1591

    
1592
    if not stat.S_ISREG(stat.S_IFMT(st.st_mode)):
1593
      return False, ("Script '%s' under path '%s' is not a regular file" %
1594
                     (script, os_dir))
1595

    
1596
  os_obj = objects.OS(name=name, path=os_dir,
1597
                      create_script=os_scripts[constants.OS_SCRIPT_CREATE],
1598
                      export_script=os_scripts[constants.OS_SCRIPT_EXPORT],
1599
                      import_script=os_scripts[constants.OS_SCRIPT_IMPORT],
1600
                      rename_script=os_scripts[constants.OS_SCRIPT_RENAME],
1601
                      api_versions=api_versions)
1602
  return True, os_obj
1603

    
1604

    
1605
def OSFromDisk(name, base_dir=None):
1606
  """Create an OS instance from disk.
1607

1608
  This function will return an OS instance if the given name is a
1609
  valid OS name. Otherwise, it will raise an appropriate
1610
  L{RPCFail} exception, detailing why this is not a valid OS.
1611

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

1615
  @type base_dir: string
1616
  @keyword base_dir: Base directory containing OS installations.
1617
                     Defaults to a search in all the OS_SEARCH_PATH dirs.
1618
  @rtype: L{objects.OS}
1619
  @return: the OS instance if we find a valid one
1620
  @raise RPCFail: if we don't find a valid OS
1621

1622
  """
1623
  status, payload = _TryOSFromDisk(name, base_dir)
1624

    
1625
  if not status:
1626
    _Fail(payload)
1627

    
1628
  return payload
1629

    
1630

    
1631
def OSEnvironment(instance, debug=0):
1632
  """Calculate the environment for an os script.
1633

1634
  @type instance: L{objects.Instance}
1635
  @param instance: target instance for the os script run
1636
  @type debug: integer
1637
  @param debug: debug level (0 or 1, for OS Api 10)
1638
  @rtype: dict
1639
  @return: dict of environment variables
1640
  @raise errors.BlockDeviceError: if the block device
1641
      cannot be found
1642

1643
  """
1644
  result = {}
1645
  result['OS_API_VERSION'] = '%d' % constants.OS_API_VERSION
1646
  result['INSTANCE_NAME'] = instance.name
1647
  result['INSTANCE_OS'] = instance.os
1648
  result['HYPERVISOR'] = instance.hypervisor
1649
  result['DISK_COUNT'] = '%d' % len(instance.disks)
1650
  result['NIC_COUNT'] = '%d' % len(instance.nics)
1651
  result['DEBUG_LEVEL'] = '%d' % debug
1652
  for idx, disk in enumerate(instance.disks):
1653
    real_disk = _RecursiveFindBD(disk)
1654
    if real_disk is None:
1655
      raise errors.BlockDeviceError("Block device '%s' is not set up" %
1656
                                    str(disk))
1657
    real_disk.Open()
1658
    result['DISK_%d_PATH' % idx] = real_disk.dev_path
1659
    result['DISK_%d_ACCESS' % idx] = disk.mode
1660
    if constants.HV_DISK_TYPE in instance.hvparams:
1661
      result['DISK_%d_FRONTEND_TYPE' % idx] = \
1662
        instance.hvparams[constants.HV_DISK_TYPE]
1663
    if disk.dev_type in constants.LDS_BLOCK:
1664
      result['DISK_%d_BACKEND_TYPE' % idx] = 'block'
1665
    elif disk.dev_type == constants.LD_FILE:
1666
      result['DISK_%d_BACKEND_TYPE' % idx] = \
1667
        'file:%s' % disk.physical_id[0]
1668
  for idx, nic in enumerate(instance.nics):
1669
    result['NIC_%d_MAC' % idx] = nic.mac
1670
    if nic.ip:
1671
      result['NIC_%d_IP' % idx] = nic.ip
1672
    result['NIC_%d_MODE' % idx] = nic.nicparams[constants.NIC_MODE]
1673
    if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
1674
      result['NIC_%d_BRIDGE' % idx] = nic.nicparams[constants.NIC_LINK]
1675
    if nic.nicparams[constants.NIC_LINK]:
1676
      result['NIC_%d_LINK' % idx] = nic.nicparams[constants.NIC_LINK]
1677
    if constants.HV_NIC_TYPE in instance.hvparams:
1678
      result['NIC_%d_FRONTEND_TYPE' % idx] = \
1679
        instance.hvparams[constants.HV_NIC_TYPE]
1680

    
1681
  for source, kind in [(instance.beparams, "BE"), (instance.hvparams, "HV")]:
1682
    for key, value in source.items():
1683
      result["INSTANCE_%s_%s" % (kind, key)] = str(value)
1684

    
1685
  return result
1686

    
1687
def BlockdevGrow(disk, amount):
1688
  """Grow a stack of block devices.
1689

1690
  This function is called recursively, with the childrens being the
1691
  first ones to resize.
1692

1693
  @type disk: L{objects.Disk}
1694
  @param disk: the disk to be grown
1695
  @rtype: (status, result)
1696
  @return: a tuple with the status of the operation
1697
      (True/False), and the errors message if status
1698
      is False
1699

1700
  """
1701
  r_dev = _RecursiveFindBD(disk)
1702
  if r_dev is None:
1703
    _Fail("Cannot find block device %s", disk)
1704

    
1705
  try:
1706
    r_dev.Grow(amount)
1707
  except errors.BlockDeviceError, err:
1708
    _Fail("Failed to grow block device: %s", err, exc=True)
1709

    
1710

    
1711
def BlockdevSnapshot(disk):
1712
  """Create a snapshot copy of a block device.
1713

1714
  This function is called recursively, and the snapshot is actually created
1715
  just for the leaf lvm backend device.
1716

1717
  @type disk: L{objects.Disk}
1718
  @param disk: the disk to be snapshotted
1719
  @rtype: string
1720
  @return: snapshot disk path
1721

1722
  """
1723
  if disk.children:
1724
    if len(disk.children) == 1:
1725
      # only one child, let's recurse on it
1726
      return BlockdevSnapshot(disk.children[0])
1727
    else:
1728
      # more than one child, choose one that matches
1729
      for child in disk.children:
1730
        if child.size == disk.size:
1731
          # return implies breaking the loop
1732
          return BlockdevSnapshot(child)
1733
  elif disk.dev_type == constants.LD_LV:
1734
    r_dev = _RecursiveFindBD(disk)
1735
    if r_dev is not None:
1736
      # let's stay on the safe side and ask for the full size, for now
1737
      return r_dev.Snapshot(disk.size)
1738
    else:
1739
      _Fail("Cannot find block device %s", disk)
1740
  else:
1741
    _Fail("Cannot snapshot non-lvm block device '%s' of type '%s'",
1742
          disk.unique_id, disk.dev_type)
1743

    
1744

    
1745
def ExportSnapshot(disk, dest_node, instance, cluster_name, idx):
1746
  """Export a block device snapshot to a remote node.
1747

1748
  @type disk: L{objects.Disk}
1749
  @param disk: the description of the disk to export
1750
  @type dest_node: str
1751
  @param dest_node: the destination node to export to
1752
  @type instance: L{objects.Instance}
1753
  @param instance: the instance object to whom the disk belongs
1754
  @type cluster_name: str
1755
  @param cluster_name: the cluster name, needed for SSH hostalias
1756
  @type idx: int
1757
  @param idx: the index of the disk in the instance's disk list,
1758
      used to export to the OS scripts environment
1759
  @rtype: None
1760

1761
  """
1762
  export_env = OSEnvironment(instance)
1763

    
1764
  inst_os = OSFromDisk(instance.os)
1765
  export_script = inst_os.export_script
1766

    
1767
  logfile = "%s/exp-%s-%s-%s.log" % (constants.LOG_OS_DIR, inst_os.name,
1768
                                     instance.name, int(time.time()))
1769
  if not os.path.exists(constants.LOG_OS_DIR):
1770
    os.mkdir(constants.LOG_OS_DIR, 0750)
1771
  real_disk = _RecursiveFindBD(disk)
1772
  if real_disk is None:
1773
    _Fail("Block device '%s' is not set up", disk)
1774

    
1775
  real_disk.Open()
1776

    
1777
  export_env['EXPORT_DEVICE'] = real_disk.dev_path
1778
  export_env['EXPORT_INDEX'] = str(idx)
1779

    
1780
  destdir = os.path.join(constants.EXPORT_DIR, instance.name + ".new")
1781
  destfile = disk.physical_id[1]
1782

    
1783
  # the target command is built out of three individual commands,
1784
  # which are joined by pipes; we check each individual command for
1785
  # valid parameters
1786
  expcmd = utils.BuildShellCmd("cd %s; %s 2>%s", inst_os.path,
1787
                               export_script, logfile)
1788

    
1789
  comprcmd = "gzip"
1790

    
1791
  destcmd = utils.BuildShellCmd("mkdir -p %s && cat > %s/%s",
1792
                                destdir, destdir, destfile)
1793
  remotecmd = _GetSshRunner(cluster_name).BuildCmd(dest_node,
1794
                                                   constants.GANETI_RUNAS,
1795
                                                   destcmd)
1796

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

    
1800
  result = utils.RunCmd(command, env=export_env)
1801

    
1802
  if result.failed:
1803
    _Fail("OS snapshot export command '%s' returned error: %s"
1804
          " output: %s", command, result.fail_reason, result.output)
1805

    
1806

    
1807
def FinalizeExport(instance, snap_disks):
1808
  """Write out the export configuration information.
1809

1810
  @type instance: L{objects.Instance}
1811
  @param instance: the instance which we export, used for
1812
      saving configuration
1813
  @type snap_disks: list of L{objects.Disk}
1814
  @param snap_disks: list of snapshot block devices, which
1815
      will be used to get the actual name of the dump file
1816

1817
  @rtype: None
1818

1819
  """
1820
  destdir = os.path.join(constants.EXPORT_DIR, instance.name + ".new")
1821
  finaldestdir = os.path.join(constants.EXPORT_DIR, instance.name)
1822

    
1823
  config = objects.SerializableConfigParser()
1824

    
1825
  config.add_section(constants.INISECT_EXP)
1826
  config.set(constants.INISECT_EXP, 'version', '0')
1827
  config.set(constants.INISECT_EXP, 'timestamp', '%d' % int(time.time()))
1828
  config.set(constants.INISECT_EXP, 'source', instance.primary_node)
1829
  config.set(constants.INISECT_EXP, 'os', instance.os)
1830
  config.set(constants.INISECT_EXP, 'compression', 'gzip')
1831

    
1832
  config.add_section(constants.INISECT_INS)
1833
  config.set(constants.INISECT_INS, 'name', instance.name)
1834
  config.set(constants.INISECT_INS, 'memory', '%d' %
1835
             instance.beparams[constants.BE_MEMORY])
1836
  config.set(constants.INISECT_INS, 'vcpus', '%d' %
1837
             instance.beparams[constants.BE_VCPUS])
1838
  config.set(constants.INISECT_INS, 'disk_template', instance.disk_template)
1839

    
1840
  nic_total = 0
1841
  for nic_count, nic in enumerate(instance.nics):
1842
    nic_total += 1
1843
    config.set(constants.INISECT_INS, 'nic%d_mac' %
1844
               nic_count, '%s' % nic.mac)
1845
    config.set(constants.INISECT_INS, 'nic%d_ip' % nic_count, '%s' % nic.ip)
1846
    config.set(constants.INISECT_INS, 'nic%d_bridge' % nic_count,
1847
               '%s' % nic.bridge)
1848
  # TODO: redundant: on load can read nics until it doesn't exist
1849
  config.set(constants.INISECT_INS, 'nic_count' , '%d' % nic_total)
1850

    
1851
  disk_total = 0
1852
  for disk_count, disk in enumerate(snap_disks):
1853
    if disk:
1854
      disk_total += 1
1855
      config.set(constants.INISECT_INS, 'disk%d_ivname' % disk_count,
1856
                 ('%s' % disk.iv_name))
1857
      config.set(constants.INISECT_INS, 'disk%d_dump' % disk_count,
1858
                 ('%s' % disk.physical_id[1]))
1859
      config.set(constants.INISECT_INS, 'disk%d_size' % disk_count,
1860
                 ('%d' % disk.size))
1861

    
1862
  config.set(constants.INISECT_INS, 'disk_count' , '%d' % disk_total)
1863

    
1864
  utils.WriteFile(os.path.join(destdir, constants.EXPORT_CONF_FILE),
1865
                  data=config.Dumps())
1866
  shutil.rmtree(finaldestdir, True)
1867
  shutil.move(destdir, finaldestdir)
1868

    
1869

    
1870
def ExportInfo(dest):
1871
  """Get export configuration information.
1872

1873
  @type dest: str
1874
  @param dest: directory containing the export
1875

1876
  @rtype: L{objects.SerializableConfigParser}
1877
  @return: a serializable config file containing the
1878
      export info
1879

1880
  """
1881
  cff = os.path.join(dest, constants.EXPORT_CONF_FILE)
1882

    
1883
  config = objects.SerializableConfigParser()
1884
  config.read(cff)
1885

    
1886
  if (not config.has_section(constants.INISECT_EXP) or
1887
      not config.has_section(constants.INISECT_INS)):
1888
    _Fail("Export info file doesn't have the required fields")
1889

    
1890
  return config.Dumps()
1891

    
1892

    
1893
def ImportOSIntoInstance(instance, src_node, src_images, cluster_name):
1894
  """Import an os image into an instance.
1895

1896
  @type instance: L{objects.Instance}
1897
  @param instance: instance to import the disks into
1898
  @type src_node: string
1899
  @param src_node: source node for the disk images
1900
  @type src_images: list of string
1901
  @param src_images: absolute paths of the disk images
1902
  @rtype: list of boolean
1903
  @return: each boolean represent the success of importing the n-th disk
1904

1905
  """
1906
  import_env = OSEnvironment(instance)
1907
  inst_os = OSFromDisk(instance.os)
1908
  import_script = inst_os.import_script
1909

    
1910
  logfile = "%s/import-%s-%s-%s.log" % (constants.LOG_OS_DIR, instance.os,
1911
                                        instance.name, int(time.time()))
1912
  if not os.path.exists(constants.LOG_OS_DIR):
1913
    os.mkdir(constants.LOG_OS_DIR, 0750)
1914

    
1915
  comprcmd = "gunzip"
1916
  impcmd = utils.BuildShellCmd("(cd %s; %s >%s 2>&1)", inst_os.path,
1917
                               import_script, logfile)
1918

    
1919
  final_result = []
1920
  for idx, image in enumerate(src_images):
1921
    if image:
1922
      destcmd = utils.BuildShellCmd('cat %s', image)
1923
      remotecmd = _GetSshRunner(cluster_name).BuildCmd(src_node,
1924
                                                       constants.GANETI_RUNAS,
1925
                                                       destcmd)
1926
      command = '|'.join([utils.ShellQuoteArgs(remotecmd), comprcmd, impcmd])
1927
      import_env['IMPORT_DEVICE'] = import_env['DISK_%d_PATH' % idx]
1928
      import_env['IMPORT_INDEX'] = str(idx)
1929
      result = utils.RunCmd(command, env=import_env)
1930
      if result.failed:
1931
        logging.error("Disk import command '%s' returned error: %s"
1932
                      " output: %s", command, result.fail_reason,
1933
                      result.output)
1934
        final_result.append("error importing disk %d: %s, %s" %
1935
                            (idx, result.fail_reason, result.output[-100]))
1936

    
1937
  if final_result:
1938
    _Fail("; ".join(final_result), log=False)
1939

    
1940

    
1941
def ListExports():
1942
  """Return a list of exports currently available on this machine.
1943

1944
  @rtype: list
1945
  @return: list of the exports
1946

1947
  """
1948
  if os.path.isdir(constants.EXPORT_DIR):
1949
    return utils.ListVisibleFiles(constants.EXPORT_DIR)
1950
  else:
1951
    _Fail("No exports directory")
1952

    
1953

    
1954
def RemoveExport(export):
1955
  """Remove an existing export from the node.
1956

1957
  @type export: str
1958
  @param export: the name of the export to remove
1959
  @rtype: None
1960

1961
  """
1962
  target = os.path.join(constants.EXPORT_DIR, export)
1963

    
1964
  try:
1965
    shutil.rmtree(target)
1966
  except EnvironmentError, err:
1967
    _Fail("Error while removing the export: %s", err, exc=True)
1968

    
1969

    
1970
def BlockdevRename(devlist):
1971
  """Rename a list of block devices.
1972

1973
  @type devlist: list of tuples
1974
  @param devlist: list of tuples of the form  (disk,
1975
      new_logical_id, new_physical_id); disk is an
1976
      L{objects.Disk} object describing the current disk,
1977
      and new logical_id/physical_id is the name we
1978
      rename it to
1979
  @rtype: boolean
1980
  @return: True if all renames succeeded, False otherwise
1981

1982
  """
1983
  msgs = []
1984
  result = True
1985
  for disk, unique_id in devlist:
1986
    dev = _RecursiveFindBD(disk)
1987
    if dev is None:
1988
      msgs.append("Can't find device %s in rename" % str(disk))
1989
      result = False
1990
      continue
1991
    try:
1992
      old_rpath = dev.dev_path
1993
      dev.Rename(unique_id)
1994
      new_rpath = dev.dev_path
1995
      if old_rpath != new_rpath:
1996
        DevCacheManager.RemoveCache(old_rpath)
1997
        # FIXME: we should add the new cache information here, like:
1998
        # DevCacheManager.UpdateCache(new_rpath, owner, ...)
1999
        # but we don't have the owner here - maybe parse from existing
2000
        # cache? for now, we only lose lvm data when we rename, which
2001
        # is less critical than DRBD or MD
2002
    except errors.BlockDeviceError, err:
2003
      msgs.append("Can't rename device '%s' to '%s': %s" %
2004
                  (dev, unique_id, err))
2005
      logging.exception("Can't rename device '%s' to '%s'", dev, unique_id)
2006
      result = False
2007
  if not result:
2008
    _Fail("; ".join(msgs))
2009

    
2010

    
2011
def _TransformFileStorageDir(file_storage_dir):
2012
  """Checks whether given file_storage_dir is valid.
2013

2014
  Checks wheter the given file_storage_dir is within the cluster-wide
2015
  default file_storage_dir stored in SimpleStore. Only paths under that
2016
  directory are allowed.
2017

2018
  @type file_storage_dir: str
2019
  @param file_storage_dir: the path to check
2020

2021
  @return: the normalized path if valid, None otherwise
2022

2023
  """
2024
  cfg = _GetConfig()
2025
  file_storage_dir = os.path.normpath(file_storage_dir)
2026
  base_file_storage_dir = cfg.GetFileStorageDir()
2027
  if (not os.path.commonprefix([file_storage_dir, base_file_storage_dir]) ==
2028
      base_file_storage_dir):
2029
    _Fail("File storage directory '%s' is not under base file"
2030
          " storage directory '%s'", file_storage_dir, base_file_storage_dir)
2031
  return file_storage_dir
2032

    
2033

    
2034
def CreateFileStorageDir(file_storage_dir):
2035
  """Create file storage directory.
2036

2037
  @type file_storage_dir: str
2038
  @param file_storage_dir: directory to create
2039

2040
  @rtype: tuple
2041
  @return: tuple with first element a boolean indicating wheter dir
2042
      creation was successful or not
2043

2044
  """
2045
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2046
  if os.path.exists(file_storage_dir):
2047
    if not os.path.isdir(file_storage_dir):
2048
      _Fail("Specified storage dir '%s' is not a directory",
2049
            file_storage_dir)
2050
  else:
2051
    try:
2052
      os.makedirs(file_storage_dir, 0750)
2053
    except OSError, err:
2054
      _Fail("Cannot create file storage directory '%s': %s",
2055
            file_storage_dir, err, exc=True)
2056

    
2057

    
2058
def RemoveFileStorageDir(file_storage_dir):
2059
  """Remove file storage directory.
2060

2061
  Remove it only if it's empty. If not log an error and return.
2062

2063
  @type file_storage_dir: str
2064
  @param file_storage_dir: the directory we should cleanup
2065
  @rtype: tuple (success,)
2066
  @return: tuple of one element, C{success}, denoting
2067
      whether the operation was successfull
2068

2069
  """
2070
  file_storage_dir = _TransformFileStorageDir(file_storage_dir)
2071
  if os.path.exists(file_storage_dir):
2072
    if not os.path.isdir(file_storage_dir):
2073
      _Fail("Specified Storage directory '%s' is not a directory",
2074
            file_storage_dir)
2075
    # deletes dir only if empty, otherwise we want to fail the rpc call
2076
    try:
2077
      os.rmdir(file_storage_dir)
2078
    except OSError, err:
2079
      _Fail("Cannot remove file storage directory '%s': %s",
2080
            file_storage_dir, err)
2081

    
2082

    
2083
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir):
2084
  """Rename the file storage directory.
2085

2086
  @type old_file_storage_dir: str
2087
  @param old_file_storage_dir: the current path
2088
  @type new_file_storage_dir: str
2089
  @param new_file_storage_dir: the name we should rename to
2090
  @rtype: tuple (success,)
2091
  @return: tuple of one element, C{success}, denoting
2092
      whether the operation was successful
2093

2094
  """
2095
  old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir)
2096
  new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir)
2097
  if not os.path.exists(new_file_storage_dir):
2098
    if os.path.isdir(old_file_storage_dir):
2099
      try:
2100
        os.rename(old_file_storage_dir, new_file_storage_dir)
2101
      except OSError, err:
2102
        _Fail("Cannot rename '%s' to '%s': %s",
2103
              old_file_storage_dir, new_file_storage_dir, err)
2104
    else:
2105
      _Fail("Specified storage dir '%s' is not a directory",
2106
            old_file_storage_dir)
2107
  else:
2108
    if os.path.exists(old_file_storage_dir):
2109
      _Fail("Cannot rename '%s' to '%s': both locations exist",
2110
            old_file_storage_dir, new_file_storage_dir)
2111

    
2112

    
2113
def _EnsureJobQueueFile(file_name):
2114
  """Checks whether the given filename is in the queue directory.
2115

2116
  @type file_name: str
2117
  @param file_name: the file name we should check
2118
  @rtype: None
2119
  @raises RPCFail: if the file is not valid
2120

2121
  """
2122
  queue_dir = os.path.normpath(constants.QUEUE_DIR)
2123
  result = (os.path.commonprefix([queue_dir, file_name]) == queue_dir)
2124

    
2125
  if not result:
2126
    _Fail("Passed job queue file '%s' does not belong to"
2127
          " the queue directory '%s'", file_name, queue_dir)
2128

    
2129

    
2130
def JobQueueUpdate(file_name, content):
2131
  """Updates a file in the queue directory.
2132

2133
  This is just a wrapper over L{utils.WriteFile}, with proper
2134
  checking.
2135

2136
  @type file_name: str
2137
  @param file_name: the job file name
2138
  @type content: str
2139
  @param content: the new job contents
2140
  @rtype: boolean
2141
  @return: the success of the operation
2142

2143
  """
2144
  _EnsureJobQueueFile(file_name)
2145

    
2146
  # Write and replace the file atomically
2147
  utils.WriteFile(file_name, data=_Decompress(content))
2148

    
2149

    
2150
def JobQueueRename(old, new):
2151
  """Renames a job queue file.
2152

2153
  This is just a wrapper over os.rename with proper checking.
2154

2155
  @type old: str
2156
  @param old: the old (actual) file name
2157
  @type new: str
2158
  @param new: the desired file name
2159
  @rtype: tuple
2160
  @return: the success of the operation and payload
2161

2162
  """
2163
  _EnsureJobQueueFile(old)
2164
  _EnsureJobQueueFile(new)
2165

    
2166
  utils.RenameFile(old, new, mkdir=True)
2167

    
2168

    
2169
def JobQueueSetDrainFlag(drain_flag):
2170
  """Set the drain flag for the queue.
2171

2172
  This will set or unset the queue drain flag.
2173

2174
  @type drain_flag: boolean
2175
  @param drain_flag: if True, will set the drain flag, otherwise reset it.
2176
  @rtype: truple
2177
  @return: always True, None
2178
  @warning: the function always returns True
2179

2180
  """
2181
  if drain_flag:
2182
    utils.WriteFile(constants.JOB_QUEUE_DRAIN_FILE, data="", close=True)
2183
  else:
2184
    utils.RemoveFile(constants.JOB_QUEUE_DRAIN_FILE)
2185

    
2186

    
2187
def BlockdevClose(instance_name, disks):
2188
  """Closes the given block devices.
2189

2190
  This means they will be switched to secondary mode (in case of
2191
  DRBD).
2192

2193
  @param instance_name: if the argument is not empty, the symlinks
2194
      of this instance will be removed
2195
  @type disks: list of L{objects.Disk}
2196
  @param disks: the list of disks to be closed
2197
  @rtype: tuple (success, message)
2198
  @return: a tuple of success and message, where success
2199
      indicates the succes of the operation, and message
2200
      which will contain the error details in case we
2201
      failed
2202

2203
  """
2204
  bdevs = []
2205
  for cf in disks:
2206
    rd = _RecursiveFindBD(cf)
2207
    if rd is None:
2208
      _Fail("Can't find device %s", cf)
2209
    bdevs.append(rd)
2210

    
2211
  msg = []
2212
  for rd in bdevs:
2213
    try:
2214
      rd.Close()
2215
    except errors.BlockDeviceError, err:
2216
      msg.append(str(err))
2217
  if msg:
2218
    _Fail("Can't make devices secondary: %s", ",".join(msg))
2219
  else:
2220
    if instance_name:
2221
      _RemoveBlockDevLinks(instance_name, disks)
2222

    
2223

    
2224
def ValidateHVParams(hvname, hvparams):
2225
  """Validates the given hypervisor parameters.
2226

2227
  @type hvname: string
2228
  @param hvname: the hypervisor name
2229
  @type hvparams: dict
2230
  @param hvparams: the hypervisor parameters to be validated
2231
  @rtype: None
2232

2233
  """
2234
  try:
2235
    hv_type = hypervisor.GetHypervisor(hvname)
2236
    hv_type.ValidateParameters(hvparams)
2237
  except errors.HypervisorError, err:
2238
    _Fail(str(err), log=False)
2239

    
2240

    
2241
def DemoteFromMC():
2242
  """Demotes the current node from master candidate role.
2243

2244
  """
2245
  # try to ensure we're not the master by mistake
2246
  master, myself = ssconf.GetMasterAndMyself()
2247
  if master == myself:
2248
    _Fail("ssconf status shows I'm the master node, will not demote")
2249
  pid_file = utils.DaemonPidFileName(constants.MASTERD_PID)
2250
  if utils.IsProcessAlive(utils.ReadPidFile(pid_file)):
2251
    _Fail("The master daemon is running, will not demote")
2252
  try:
2253
    utils.CreateBackup(constants.CLUSTER_CONF_FILE)
2254
  except EnvironmentError, err:
2255
    if err.errno != errno.ENOENT:
2256
      _Fail("Error while backing up cluster file: %s", err, exc=True)
2257
  utils.RemoveFile(constants.CLUSTER_CONF_FILE)
2258

    
2259

    
2260
def _FindDisks(nodes_ip, disks):
2261
  """Sets the physical ID on disks and returns the block devices.
2262

2263
  """
2264
  # set the correct physical ID
2265
  my_name = utils.HostInfo().name
2266
  for cf in disks:
2267
    cf.SetPhysicalID(my_name, nodes_ip)
2268

    
2269
  bdevs = []
2270

    
2271
  for cf in disks:
2272
    rd = _RecursiveFindBD(cf)
2273
    if rd is None:
2274
      _Fail("Can't find device %s", cf)
2275
    bdevs.append(rd)
2276
  return bdevs
2277

    
2278

    
2279
def DrbdDisconnectNet(nodes_ip, disks):
2280
  """Disconnects the network on a list of drbd devices.
2281

2282
  """
2283
  bdevs = _FindDisks(nodes_ip, disks)
2284

    
2285
  # disconnect disks
2286
  for rd in bdevs:
2287
    try:
2288
      rd.DisconnectNet()
2289
    except errors.BlockDeviceError, err:
2290
      _Fail("Can't change network configuration to standalone mode: %s",
2291
            err, exc=True)
2292

    
2293

    
2294
def DrbdAttachNet(nodes_ip, disks, instance_name, multimaster):
2295
  """Attaches the network on a list of drbd devices.
2296

2297
  """
2298
  bdevs = _FindDisks(nodes_ip, disks)
2299

    
2300
  if multimaster:
2301
    for idx, rd in enumerate(bdevs):
2302
      try:
2303
        _SymlinkBlockDev(instance_name, rd.dev_path, idx)
2304
      except EnvironmentError, err:
2305
        _Fail("Can't create symlink: %s", err)
2306
  # reconnect disks, switch to new master configuration and if
2307
  # needed primary mode
2308
  for rd in bdevs:
2309
    try:
2310
      rd.AttachNet(multimaster)
2311
    except errors.BlockDeviceError, err:
2312
      _Fail("Can't change network configuration: %s", err)
2313
  # wait until the disks are connected; we need to retry the re-attach
2314
  # if the device becomes standalone, as this might happen if the one
2315
  # node disconnects and reconnects in a different mode before the
2316
  # other node reconnects; in this case, one or both of the nodes will
2317
  # decide it has wrong configuration and switch to standalone
2318
  RECONNECT_TIMEOUT = 2 * 60
2319
  sleep_time = 0.100 # start with 100 miliseconds
2320
  timeout_limit = time.time() + RECONNECT_TIMEOUT
2321
  while time.time() < timeout_limit:
2322
    all_connected = True
2323
    for rd in bdevs:
2324
      stats = rd.GetProcStatus()
2325
      if not (stats.is_connected or stats.is_in_resync):
2326
        all_connected = False
2327
      if stats.is_standalone:
2328
        # peer had different config info and this node became
2329
        # standalone, even though this should not happen with the
2330
        # new staged way of changing disk configs
2331
        try:
2332
          rd.AttachNet(multimaster)
2333
        except errors.BlockDeviceError, err:
2334
          _Fail("Can't change network configuration: %s", err)
2335
    if all_connected:
2336
      break
2337
    time.sleep(sleep_time)
2338
    sleep_time = min(5, sleep_time * 1.5)
2339
  if not all_connected:
2340
    _Fail("Timeout in disk reconnecting")
2341
  if multimaster:
2342
    # change to primary mode
2343
    for rd in bdevs:
2344
      try:
2345
        rd.Open()
2346
      except errors.BlockDeviceError, err:
2347
        _Fail("Can't change to primary mode: %s", err)
2348

    
2349

    
2350
def DrbdWaitSync(nodes_ip, disks):
2351
  """Wait until DRBDs have synchronized.
2352

2353
  """
2354
  bdevs = _FindDisks(nodes_ip, disks)
2355

    
2356
  min_resync = 100
2357
  alldone = True
2358
  for rd in bdevs:
2359
    stats = rd.GetProcStatus()
2360
    if not (stats.is_connected or stats.is_in_resync):
2361
      _Fail("DRBD device %s is not in sync: stats=%s", rd, stats)
2362
    alldone = alldone and (not stats.is_in_resync)
2363
    if stats.sync_percent is not None:
2364
      min_resync = min(min_resync, stats.sync_percent)
2365

    
2366
  return (alldone, min_resync)
2367

    
2368

    
2369
def PowercycleNode(hypervisor_type):
2370
  """Hard-powercycle the node.
2371

2372
  Because we need to return first, and schedule the powercycle in the
2373
  background, we won't be able to report failures nicely.
2374

2375
  """
2376
  hyper = hypervisor.GetHypervisor(hypervisor_type)
2377
  try:
2378
    pid = os.fork()
2379
  except OSError:
2380
    # if we can't fork, we'll pretend that we're in the child process
2381
    pid = 0
2382
  if pid > 0:
2383
    return "Reboot scheduled in 5 seconds"
2384
  time.sleep(5)
2385
  hyper.PowercycleNode()
2386

    
2387

    
2388
class HooksRunner(object):
2389
  """Hook runner.
2390

2391
  This class is instantiated on the node side (ganeti-noded) and not
2392
  on the master side.
2393

2394
  """
2395
  RE_MASK = re.compile("^[a-zA-Z0-9_-]+$")
2396

    
2397
  def __init__(self, hooks_base_dir=None):
2398
    """Constructor for hooks runner.
2399

2400
    @type hooks_base_dir: str or None
2401
    @param hooks_base_dir: if not None, this overrides the
2402
        L{constants.HOOKS_BASE_DIR} (useful for unittests)
2403

2404
    """
2405
    if hooks_base_dir is None:
2406
      hooks_base_dir = constants.HOOKS_BASE_DIR
2407
    self._BASE_DIR = hooks_base_dir
2408

    
2409
  @staticmethod
2410
  def ExecHook(script, env):
2411
    """Exec one hook script.
2412

2413
    @type script: str
2414
    @param script: the full path to the script
2415
    @type env: dict
2416
    @param env: the environment with which to exec the script
2417
    @rtype: tuple (success, message)
2418
    @return: a tuple of success and message, where success
2419
        indicates the succes of the operation, and message
2420
        which will contain the error details in case we
2421
        failed
2422

2423
    """
2424
    # exec the process using subprocess and log the output
2425
    fdstdin = None
2426
    try:
2427
      fdstdin = open("/dev/null", "r")
2428
      child = subprocess.Popen([script], stdin=fdstdin, stdout=subprocess.PIPE,
2429
                               stderr=subprocess.STDOUT, close_fds=True,
2430
                               shell=False, cwd="/", env=env)
2431
      output = ""
2432
      try:
2433
        output = child.stdout.read(4096)
2434
        child.stdout.close()
2435
      except EnvironmentError, err:
2436
        output += "Hook script error: %s" % str(err)
2437

    
2438
      while True:
2439
        try:
2440
          result = child.wait()
2441
          break
2442
        except EnvironmentError, err:
2443
          if err.errno == errno.EINTR:
2444
            continue
2445
          raise
2446
    finally:
2447
      # try not to leak fds
2448
      for fd in (fdstdin, ):
2449
        if fd is not None:
2450
          try:
2451
            fd.close()
2452
          except EnvironmentError, err:
2453
            # just log the error
2454
            #logging.exception("Error while closing fd %s", fd)
2455
            pass
2456

    
2457
    return result == 0, utils.SafeEncode(output.strip())
2458

    
2459
  def RunHooks(self, hpath, phase, env):
2460
    """Run the scripts in the hooks directory.
2461

2462
    @type hpath: str
2463
    @param hpath: the path to the hooks directory which
2464
        holds the scripts
2465
    @type phase: str
2466
    @param phase: either L{constants.HOOKS_PHASE_PRE} or
2467
        L{constants.HOOKS_PHASE_POST}
2468
    @type env: dict
2469
    @param env: dictionary with the environment for the hook
2470
    @rtype: list
2471
    @return: list of 3-element tuples:
2472
      - script path
2473
      - script result, either L{constants.HKR_SUCCESS} or
2474
        L{constants.HKR_FAIL}
2475
      - output of the script
2476

2477
    @raise errors.ProgrammerError: for invalid input
2478
        parameters
2479

2480
    """
2481
    if phase == constants.HOOKS_PHASE_PRE:
2482
      suffix = "pre"
2483
    elif phase == constants.HOOKS_PHASE_POST:
2484
      suffix = "post"
2485
    else:
2486
      _Fail("Unknown hooks phase '%s'", phase)
2487

    
2488
    rr = []
2489

    
2490
    subdir = "%s-%s.d" % (hpath, suffix)
2491
    dir_name = "%s/%s" % (self._BASE_DIR, subdir)
2492
    try:
2493
      dir_contents = utils.ListVisibleFiles(dir_name)
2494
    except OSError:
2495
      # FIXME: must log output in case of failures
2496
      return rr
2497

    
2498
    # we use the standard python sort order,
2499
    # so 00name is the recommended naming scheme
2500
    dir_contents.sort()
2501
    for relname in dir_contents:
2502
      fname = os.path.join(dir_name, relname)
2503
      if not (os.path.isfile(fname) and os.access(fname, os.X_OK) and
2504
          self.RE_MASK.match(relname) is not None):
2505
        rrval = constants.HKR_SKIP
2506
        output = ""
2507
      else:
2508
        result, output = self.ExecHook(fname, env)
2509
        if not result:
2510
          rrval = constants.HKR_FAIL
2511
        else:
2512
          rrval = constants.HKR_SUCCESS
2513
      rr.append(("%s/%s" % (subdir, relname), rrval, output))
2514

    
2515
    return rr
2516

    
2517

    
2518
class IAllocatorRunner(object):
2519
  """IAllocator runner.
2520

2521
  This class is instantiated on the node side (ganeti-noded) and not on
2522
  the master side.
2523

2524
  """
2525
  def Run(self, name, idata):
2526
    """Run an iallocator script.
2527

2528
    @type name: str
2529
    @param name: the iallocator script name
2530
    @type idata: str
2531
    @param idata: the allocator input data
2532

2533
    @rtype: tuple
2534
    @return: two element tuple of:
2535
       - status
2536
       - either error message or stdout of allocator (for success)
2537

2538
    """
2539
    alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
2540
                                  os.path.isfile)
2541
    if alloc_script is None:
2542
      _Fail("iallocator module '%s' not found in the search path", name)
2543

    
2544
    fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
2545
    try:
2546
      os.write(fd, idata)
2547
      os.close(fd)
2548
      result = utils.RunCmd([alloc_script, fin_name])
2549
      if result.failed:
2550
        _Fail("iallocator module '%s' failed: %s, output '%s'",
2551
              name, result.fail_reason, result.output)
2552
    finally:
2553
      os.unlink(fin_name)
2554

    
2555
    return result.stdout
2556

    
2557

    
2558
class DevCacheManager(object):
2559
  """Simple class for managing a cache of block device information.
2560

2561
  """
2562
  _DEV_PREFIX = "/dev/"
2563
  _ROOT_DIR = constants.BDEV_CACHE_DIR
2564

    
2565
  @classmethod
2566
  def _ConvertPath(cls, dev_path):
2567
    """Converts a /dev/name path to the cache file name.
2568

2569
    This replaces slashes with underscores and strips the /dev
2570
    prefix. It then returns the full path to the cache file.
2571

2572
    @type dev_path: str
2573
    @param dev_path: the C{/dev/} path name
2574
    @rtype: str
2575
    @return: the converted path name
2576

2577
    """
2578
    if dev_path.startswith(cls._DEV_PREFIX):
2579
      dev_path = dev_path[len(cls._DEV_PREFIX):]
2580
    dev_path = dev_path.replace("/", "_")
2581
    fpath = "%s/bdev_%s" % (cls._ROOT_DIR, dev_path)
2582
    return fpath
2583

    
2584
  @classmethod
2585
  def UpdateCache(cls, dev_path, owner, on_primary, iv_name):
2586
    """Updates the cache information for a given device.
2587

2588
    @type dev_path: str
2589
    @param dev_path: the pathname of the device
2590
    @type owner: str
2591
    @param owner: the owner (instance name) of the device
2592
    @type on_primary: bool
2593
    @param on_primary: whether this is the primary
2594
        node nor not
2595
    @type iv_name: str
2596
    @param iv_name: the instance-visible name of the
2597
        device, as in objects.Disk.iv_name
2598

2599
    @rtype: None
2600

2601
    """
2602
    if dev_path is None:
2603
      logging.error("DevCacheManager.UpdateCache got a None dev_path")
2604
      return
2605
    fpath = cls._ConvertPath(dev_path)
2606
    if on_primary:
2607
      state = "primary"
2608
    else:
2609
      state = "secondary"
2610
    if iv_name is None:
2611
      iv_name = "not_visible"
2612
    fdata = "%s %s %s\n" % (str(owner), state, iv_name)
2613
    try:
2614
      utils.WriteFile(fpath, data=fdata)
2615
    except EnvironmentError, err:
2616
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)
2617

    
2618
  @classmethod
2619
  def RemoveCache(cls, dev_path):
2620
    """Remove data for a dev_path.
2621

2622
    This is just a wrapper over L{utils.RemoveFile} with a converted
2623
    path name and logging.
2624

2625
    @type dev_path: str
2626
    @param dev_path: the pathname of the device
2627

2628
    @rtype: None
2629

2630
    """
2631
    if dev_path is None:
2632
      logging.error("DevCacheManager.RemoveCache got a None dev_path")
2633
      return
2634
    fpath = cls._ConvertPath(dev_path)
2635
    try:
2636
      utils.RemoveFile(fpath)
2637
    except EnvironmentError, err:
2638
      logging.exception("Can't update bdev cache for %s: %s", dev_path, err)