Add support for command aliases
[ganeti-local] / lib / backend.py
index f85ecba..1cea6e3 100644 (file)
@@ -122,7 +122,6 @@ def LeaveCluster():
       if os.path.isfile(full_name) and not os.path.islink(full_name):
         utils.RemoveFile(full_name)
 
-
   try:
     priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
   except errors.OpExecError, err:
@@ -211,19 +210,31 @@ def GetVolumeList(vg_name):
   """Compute list of logical volumes and their size.
 
   Returns:
-    dictionary of all partions (key) with their size:
-    test1: 20.06MiB
+    dictionary of all partions (key) with their size (in MiB), inactive
+    and online status:
+    {'test1': ('20.06', True, True)}
 
   """
-  result = utils.RunCmd(["lvs", "--noheadings", "--units=m",
-                         "-oname,size", vg_name])
+  lvs = {}
+  sep = '|'
+  result = utils.RunCmd(["lvs", "--noheadings", "--units=m", "--nosuffix",
+                         "--separator=%s" % sep,
+                         "-olv_name,lv_size,lv_attr", vg_name])
   if result.failed:
     logger.Error("Failed to list logical volumes, lvs output: %s" %
                  result.output)
-    return {}
+    return result.output
+
+  for line in result.stdout.splitlines():
+    line = line.strip().rstrip(sep)
+    name, size, attr = line.split(sep)
+    if len(attr) != 6:
+      attr = '------'
+    inactive = attr[4] == '-'
+    online = attr[5] == 'o'
+    lvs[name] = (size, inactive, online)
 
-  lvlist = [line.split() for line in result.output.splitlines()]
-  return dict(lvlist)
+  return lvs
 
 
 def ListVolumeGroups():
@@ -262,7 +273,7 @@ def NodeVolumes():
       'vg': line[3].strip(),
     }
 
-  return [map_line(line.split('|')) for line in result.output.splitlines()]
+  return [map_line(line.split('|')) for line in result.stdout.splitlines()]
 
 
 def BridgesExist(bridges_list):
@@ -400,11 +411,10 @@ def AddOSToInstance(instance, os_disk, swap_disk):
                                 logfile)
 
   result = utils.RunCmd(command)
-
   if result.failed:
-    logger.Error("os create command '%s' returned error: %s"
+    logger.Error("os create command '%s' returned error: %s, logfile: %s,"
                  " output: %s" %
-                 (command, result.fail_reason, result.output))
+                 (command, result.fail_reason, logfile, result.output))
     return False
 
   return True
@@ -481,20 +491,32 @@ def _GetVGInfo(vg_name):
     vg_free is the free size of the volume group in MiB
     pv_count are the number of physical disks in that vg
 
+  If an error occurs during gathering of data, we return the same dict
+  with keys all set to None.
+
   """
+  retdic = dict.fromkeys(["vg_size", "vg_free", "pv_count"])
+
   retval = utils.RunCmd(["vgs", "-ovg_size,vg_free,pv_count", "--noheadings",
                          "--nosuffix", "--units=m", "--separator=:", vg_name])
 
   if retval.failed:
     errmsg = "volume group %s not present" % vg_name
     logger.Error(errmsg)
-    raise errors.LVMError(errmsg)
-  valarr = retval.stdout.strip().split(':')
-  retdic = {
-    "vg_size": int(round(float(valarr[0]), 0)),
-    "vg_free": int(round(float(valarr[1]), 0)),
-    "pv_count": int(valarr[2]),
-    }
+    return retdic
+  valarr = retval.stdout.strip().rstrip(':').split(':')
+  if len(valarr) == 3:
+    try:
+      retdic = {
+        "vg_size": int(round(float(valarr[0]), 0)),
+        "vg_free": int(round(float(valarr[1]), 0)),
+        "pv_count": int(valarr[2]),
+        }
+    except ValueError, err:
+      logger.Error("Fail to parse vgs output: %s" % str(err))
+  else:
+    logger.Error("vgs output has the wrong number of fields (expected"
+                 " three): %s" % str(valarr))
   return retdic
 
 
@@ -661,9 +683,10 @@ def CreateBlockDevice(disk, size, owner, on_primary, info):
                      (disk, size))
   if on_primary or disk.AssembleOnSecondary():
     if not device.Assemble():
-      raise errors.BlockDeviceError("Can't assemble device after creation,"
-                                    " very unusual event - check the node"
-                                    " daemon logs")
+      errorstring = "Can't assemble device after creation"
+      logger.Error(errorstring)
+      raise errors.BlockDeviceError("%s, very unusual event - check the node"
+                                    " daemon logs" % errorstring)
     device.SetSyncSpeed(constants.SYNC_SPEED)
     if on_primary or disk.OpenOnSecondary():
       device.Open(force=True)
@@ -731,7 +754,7 @@ def _RecursiveAssembleBD(disk, owner, as_primary):
       try:
         cdev = _RecursiveAssembleBD(chld_disk, owner, as_primary)
       except errors.BlockDeviceError, err:
-        if children.count(None) > mcn:
+        if children.count(None) >= mcn:
           raise
         cdev = None
         logger.Debug("Error in child activation: %s" % str(err))
@@ -895,8 +918,7 @@ def FindBlockDevice(disk):
   rbd = _RecursiveFindBD(disk)
   if rbd is None:
     return rbd
-  sync_p, est_t, is_degr = rbd.GetSyncStatus()
-  return rbd.dev_path, rbd.major, rbd.minor, sync_p, est_t, is_degr
+  return (rbd.dev_path, rbd.major, rbd.minor) + rbd.GetSyncStatus()
 
 
 def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
@@ -911,8 +933,11 @@ def UploadFile(file_name, data, mode, uid, gid, atime, mtime):
                  file_name)
     return False
 
-  allowed_files = [constants.CLUSTER_CONF_FILE, "/etc/hosts",
-                   constants.SSH_KNOWN_HOSTS_FILE]
+  allowed_files = [
+    constants.CLUSTER_CONF_FILE,
+    constants.ETC_HOSTS,
+    constants.SSH_KNOWN_HOSTS_FILE,
+    ]
   allowed_files.extend(ssconf.SimpleStore().GetFileList())
   if file_name not in allowed_files:
     logger.Error("Filename passed to UploadFile not in allowed"
@@ -1136,7 +1161,7 @@ def SnapshotBlockDevice(disk):
       return None
   else:
     raise errors.ProgrammerError("Cannot snapshot non-lvm block device"
-                                 "'%s' of type '%s'" %
+                                 " '%s' of type '%s'" %
                                  (disk.unique_id, disk.dev_type))
 
 
@@ -1445,7 +1470,7 @@ class HooksRunner(object):
       fdstdin = open("/dev/null", "r")
       child = subprocess.Popen([script], stdin=fdstdin, stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT, close_fds=True,
-                               shell=False, cwd="/",env=env)
+                               shell=False, cwd="/", env=env)
       output = ""
       try:
         output = child.stdout.read(4096)