Add custom logging setup for daemons
[ganeti-local] / lib / ssconf.py
index b64cf69..d73a267 100644 (file)
@@ -47,9 +47,7 @@ class SimpleStore:
   Other particularities of the datastore:
     - keys are restricted to predefined values
     - values are small (<4k)
-    - since the data is practically static, read keys are cached in memory
-    - some keys are handled specially (read from the system, so
-      we can't update them)
+    - some keys are handled specially (read from the system)
 
   """
   _SS_FILEPREFIX = "ssconf_"
@@ -60,8 +58,10 @@ class SimpleStore:
   SS_MASTER_NETDEV = "master_netdev"
   SS_CLUSTER_NAME = "cluster_name"
   SS_FILE_STORAGE_DIR = "file_storage_dir"
+  SS_CONFIG_VERSION = "config_version"
   _VALID_KEYS = (SS_HYPERVISOR, SS_NODED_PASS, SS_MASTER_NODE, SS_MASTER_IP,
-                 SS_MASTER_NETDEV, SS_CLUSTER_NAME, SS_FILE_STORAGE_DIR)
+                 SS_MASTER_NETDEV, SS_CLUSTER_NAME, SS_FILE_STORAGE_DIR,
+                 SS_CONFIG_VERSION)
   _MAX_SIZE = 4096
 
   def __init__(self, cfg_location=None):
@@ -69,7 +69,6 @@ class SimpleStore:
       self._cfg_dir = constants.DATA_DIR
     else:
       self._cfg_dir = cfg_location
-    self._cache = {}
 
   def KeyToFilename(self, key):
     """Convert a given key into filename.
@@ -89,8 +88,6 @@ class SimpleStore:
     will be changed into ConfigurationErrors.
 
     """
-    if key in self._cache:
-      return self._cache[key]
     filename = self.KeyToFilename(key)
     try:
       fh = file(filename, 'r')
@@ -102,7 +99,6 @@ class SimpleStore:
     except EnvironmentError, err:
       raise errors.ConfigurationError("Can't read from the ssconf file:"
                                       " '%s'" % str(err))
-    self._cache[key] = data
     return data
 
   def GetNodeDaemonPort(self):
@@ -162,21 +158,44 @@ class SimpleStore:
     """
     return self._ReadFile(self.SS_FILE_STORAGE_DIR)
 
-  def SetKey(self, key, value):
-    """Set the value of a key.
-
-    This should be used only when adding a node to a cluster.
+  def GetConfigVersion(self):
+    """Get the configuration version.
 
     """
-    file_name = self.KeyToFilename(key)
-    utils.WriteFile(file_name, data="%s\n" % str(value),
-                    uid=0, gid=0, mode=0400)
-    self._cache[key] = value
+    value = self._ReadFile(self.SS_CONFIG_VERSION)
+    try:
+      return int(value)
+    except (ValueError, TypeError), err:
+      raise errors.ConfigurationError("Failed to convert config version %s to"
+                                      " int: '%s'" % (value, str(err)))
 
   def GetFileList(self):
-    """Return the lis of all config files.
+    """Return the list of all config files.
 
     This is used for computing node replication data.
 
     """
     return [self.KeyToFilename(key) for key in self._VALID_KEYS]
+
+
+class WritableSimpleStore(SimpleStore):
+  """This is a read/write interface to SimpleStore, which is used rarely, when
+  values need to be changed. Since WriteFile handles updates in an atomic way
+  it should be fine to use two WritableSimpleStore at the same time, but in
+  the future we might want to put additional protection for this class.
+
+  A WritableSimpleStore cannot be used to update system-dependent values.
+
+  """
+
+  def SetKey(self, key, value):
+    """Set the value of a key.
+
+    This should be used only when adding a node to a cluster, or in other
+    infrequent operations such as cluster-rename or master-failover.
+
+    """
+    file_name = self.KeyToFilename(key)
+    utils.WriteFile(file_name, data="%s\n" % str(value),
+                    uid=0, gid=0, mode=0400)
+