New RPC to get size and spindles of disks
[ganeti-local] / lib / jstore.py
index 26be6ca..324f91e 100644 (file)
@@ -28,6 +28,7 @@ from ganeti import constants
 from ganeti import errors
 from ganeti import runtime
 from ganeti import utils
+from ganeti import pathutils
 
 
 JOBS_PER_ARCHIVE_DIRECTORY = 10000
@@ -41,12 +42,19 @@ def _ReadNumericFile(file_name):
 
   """
   try:
-    return int(utils.ReadFile(file_name))
+    contents = utils.ReadFile(file_name)
   except EnvironmentError, err:
     if err.errno in (errno.ENOENT, ):
       return None
     raise
 
+  try:
+    return int(contents)
+  except (ValueError, TypeError), err:
+    # Couldn't convert to int
+    raise errors.JobQueueError("Content of file '%s' is not numeric: %s" %
+                               (file_name, err))
+
 
 def ReadSerial():
   """Read the serial file.
@@ -54,7 +62,7 @@ def ReadSerial():
   The queue should be locked while this function is called.
 
   """
-  return _ReadNumericFile(constants.JOB_QUEUE_SERIAL_FILE)
+  return _ReadNumericFile(pathutils.JOB_QUEUE_SERIAL_FILE)
 
 
 def ReadVersion():
@@ -63,7 +71,7 @@ def ReadVersion():
   The queue should be locked while this function is called.
 
   """
-  return _ReadNumericFile(constants.JOB_QUEUE_VERSION_FILE)
+  return _ReadNumericFile(pathutils.JOB_QUEUE_VERSION_FILE)
 
 
 def InitAndVerifyQueue(must_lock):
@@ -81,7 +89,7 @@ def InitAndVerifyQueue(must_lock):
   getents = runtime.GetEnts()
 
   # Lock queue
-  queue_lock = utils.FileLock.Open(constants.JOB_QUEUE_LOCK_FILE)
+  queue_lock = utils.FileLock.Open(pathutils.JOB_QUEUE_LOCK_FILE)
   try:
     # The queue needs to be locked in exclusive mode to write to the serial and
     # version files.
@@ -102,8 +110,9 @@ def InitAndVerifyQueue(must_lock):
       version = ReadVersion()
       if version is None:
         # Write new version file
-        utils.WriteFile(constants.JOB_QUEUE_VERSION_FILE,
-                        uid=getents.masterd_uid, gid=getents.masterd_gid,
+        utils.WriteFile(pathutils.JOB_QUEUE_VERSION_FILE,
+                        uid=getents.masterd_uid, gid=getents.daemons_gid,
+                        mode=constants.JOB_QUEUE_FILES_PERMS,
                         data="%s\n" % constants.JOB_QUEUE_VERSION)
 
         # Read again
@@ -116,8 +125,9 @@ def InitAndVerifyQueue(must_lock):
       serial = ReadSerial()
       if serial is None:
         # Write new serial file
-        utils.WriteFile(constants.JOB_QUEUE_SERIAL_FILE,
-                        uid=getents.masterd_uid, gid=getents.masterd_gid,
+        utils.WriteFile(pathutils.JOB_QUEUE_SERIAL_FILE,
+                        uid=getents.masterd_uid, gid=getents.daemons_gid,
+                        mode=constants.JOB_QUEUE_FILES_PERMS,
                         data="%s\n" % 0)
 
         # Read again
@@ -150,7 +160,7 @@ def CheckDrainFlag():
   @return: True if the job queue is marked drained
 
   """
-  return os.path.exists(constants.JOB_QUEUE_DRAIN_FILE)
+  return os.path.exists(pathutils.JOB_QUEUE_DRAIN_FILE)
 
 
 def SetDrainFlag(drain_flag):
@@ -165,10 +175,11 @@ def SetDrainFlag(drain_flag):
   getents = runtime.GetEnts()
 
   if drain_flag:
-    utils.WriteFile(constants.JOB_QUEUE_DRAIN_FILE, data="",
-                    uid=getents.masterd_uid, gid=getents.masterd_gid)
+    utils.WriteFile(pathutils.JOB_QUEUE_DRAIN_FILE, data="",
+                    uid=getents.masterd_uid, gid=getents.daemons_gid,
+                    mode=constants.JOB_QUEUE_FILES_PERMS)
   else:
-    utils.RemoveFile(constants.JOB_QUEUE_DRAIN_FILE)
+    utils.RemoveFile(pathutils.JOB_QUEUE_DRAIN_FILE)
 
   assert (not drain_flag) ^ CheckDrainFlag()