+ _SS_FILEPREFIX = "ssconf_"
+ _VALID_KEYS = (
+ constants.SS_CLUSTER_NAME,
+ constants.SS_CLUSTER_TAGS,
+ constants.SS_FILE_STORAGE_DIR,
+ constants.SS_MASTER_CANDIDATES,
+ constants.SS_MASTER_CANDIDATES_IPS,
+ constants.SS_MASTER_IP,
+ constants.SS_MASTER_NETDEV,
+ constants.SS_MASTER_NODE,
+ constants.SS_NODE_LIST,
+ constants.SS_NODE_PRIMARY_IPS,
+ constants.SS_NODE_SECONDARY_IPS,
+ constants.SS_OFFLINE_NODES,
+ constants.SS_ONLINE_NODES,
+ constants.SS_INSTANCE_LIST,
+ constants.SS_RELEASE_VERSION,
+ )
+ _MAX_SIZE = 131072
+
+ def __init__(self, cfg_location=None):
+ if cfg_location is None:
+ self._cfg_dir = constants.DATA_DIR
+ else:
+ self._cfg_dir = cfg_location
+
+ def KeyToFilename(self, key):
+ """Convert a given key into filename.
+
+ """
+ if key not in self._VALID_KEYS:
+ raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'"
+ % str(key))
+
+ filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key
+ return filename
+
+ def _ReadFile(self, key):
+ """Generic routine to read keys.
+
+ This will read the file which holds the value requested. Errors
+ will be changed into ConfigurationErrors.
+
+ """
+ filename = self.KeyToFilename(key)
+ try:
+ data = utils.ReadFile(filename, size=self._MAX_SIZE)
+ except EnvironmentError, err:
+ raise errors.ConfigurationError("Can't read from the ssconf file:"
+ " '%s'" % str(err))
+ data = data.rstrip('\n')
+ return data
+
+ def WriteFiles(self, values):
+ """Writes ssconf files used by external scripts.
+
+ @type values: dict
+ @param values: Dictionary of (name, value)
+
+ """
+ ssconf_lock = utils.FileLock(constants.SSCONF_LOCK_FILE)
+
+ # Get lock while writing files
+ ssconf_lock.Exclusive(blocking=True, timeout=SSCONF_LOCK_TIMEOUT)
+ try:
+ for name, value in values.iteritems():
+ if value and not value.endswith("\n"):
+ value += "\n"
+ utils.WriteFile(self.KeyToFilename(name), data=value, mode=0444)
+ finally:
+ ssconf_lock.Unlock()
+
+ def GetFileList(self):
+ """Return the list of all config files.
+
+ This is used for computing node replication data.