errors: Document arguments to QueryFilterParseError
[ganeti-local] / lib / watcher / state.py
index ec659b8..3e8c463 100644 (file)
@@ -28,7 +28,6 @@ import time
 import logging
 
 from ganeti import utils
-from ganeti import constants
 from ganeti import serializer
 from ganeti import errors
 
@@ -87,7 +86,7 @@ class WatcherState(object):
         self._data = {}
       else:
         self._data = serializer.Load(state_data)
-    except Exception, msg: # pylint: disable-msg=W0703
+    except Exception, msg: # pylint: disable=W0703
       # Ignore errors while loading the file and treat it as empty
       self._data = {}
       logging.warning(("Invalid state file. Using defaults."
@@ -100,7 +99,7 @@ class WatcherState(object):
 
     self._orig_data = serializer.Dump(self._data)
 
-  def Save(self):
+  def Save(self, filename):
     """Save state to file, then unlock and close it.
 
     """
@@ -109,15 +108,15 @@ class WatcherState(object):
     serialized_form = serializer.Dump(self._data)
     if self._orig_data == serialized_form:
       logging.debug("Data didn't change, just touching status file")
-      os.utime(constants.WATCHER_STATEFILE, None)
+      os.utime(filename, None)
       return
 
     # We need to make sure the file is locked before renaming it, otherwise
     # starting ganeti-watcher again at the same time will create a conflict.
-    fd = utils.WriteFile(constants.WATCHER_STATEFILE,
+    fd = utils.WriteFile(filename,
                          data=serialized_form,
                          prewrite=utils.LockFile, close=False)
-    self.statefile = os.fdopen(fd, 'w+')
+    self.statefile = os.fdopen(fd, "w+")
 
   def Close(self):
     """Unlock configuration file and close it.
@@ -147,22 +146,19 @@ class WatcherState(object):
 
     ndata = self._data["node"]
 
-    if name not in ndata:
-      ndata[name] = {}
+    ndata.setdefault(name, {})[KEY_BOOT_ID] = bootid
 
-    ndata[name][KEY_BOOT_ID] = bootid
-
-  def NumberOfRestartAttempts(self, instance):
+  def NumberOfRestartAttempts(self, instance_name):
     """Returns number of previous restart attempts.
 
-    @type instance: L{Instance}
-    @param instance: the instance to look up
+    @type instance_name: string
+    @param instance_name: the name of the instance to look up
 
     """
     idata = self._data["instance"]
 
-    if instance.name in idata:
-      return idata[instance.name][KEY_RESTART_COUNT]
+    if instance_name in idata:
+      return idata[instance_name][KEY_RESTART_COUNT]
 
     return 0
 
@@ -174,11 +170,12 @@ class WatcherState(object):
 
     """
     idict = self._data["instance"]
+
     # First, delete obsolete instances
     obsolete_instances = set(idict).difference(instances)
     for inst in obsolete_instances:
       logging.debug("Forgetting obsolete instance %s", inst)
-      del idict[inst]
+      idict.pop(inst, None)
 
     # Second, delete expired records
     earliest = time.time() - RETRY_EXPIRATION
@@ -186,36 +183,31 @@ class WatcherState(object):
                          if idict[i][KEY_RESTART_WHEN] < earliest]
     for inst in expired_instances:
       logging.debug("Expiring record for instance %s", inst)
-      del idict[inst]
+      idict.pop(inst, None)
 
-  def RecordRestartAttempt(self, instance):
+  def RecordRestartAttempt(self, instance_name):
     """Record a restart attempt.
 
-    @type instance: L{Instance}
-    @param instance: the instance being restarted
+    @type instance_name: string
+    @param instance_name: the name of the instance being restarted
 
     """
     idata = self._data["instance"]
 
-    if instance.name not in idata:
-      inst = idata[instance.name] = {}
-    else:
-      inst = idata[instance.name]
-
+    inst = idata.setdefault(instance_name, {})
     inst[KEY_RESTART_WHEN] = time.time()
     inst[KEY_RESTART_COUNT] = inst.get(KEY_RESTART_COUNT, 0) + 1
 
-  def RemoveInstance(self, instance):
+  def RemoveInstance(self, instance_name):
     """Update state to reflect that a machine is running.
 
     This method removes the record for a named instance (as we only
     track down instances).
 
-    @type instance: L{Instance}
-    @param instance: the instance to remove from books
+    @type instance_name: string
+    @param instance_name: the name of the instance to remove from books
 
     """
     idata = self._data["instance"]
 
-    if instance.name in idata:
-      del idata[instance.name]
+    idata.pop(instance_name, None)