-#!/usr/bin/python
+#
#
# Copyright (C) 2006, 2007 Google Inc.
"""
-import os
-import tempfile
-import errno
import socket
+import sys
from ganeti import errors
from ganeti import constants
+from ganeti import utils
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_"
SS_MASTER_IP = "master_ip"
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_MASTER_NETDEV, SS_CLUSTER_NAME, SS_FILE_STORAGE_DIR,
+ SS_CONFIG_VERSION)
_MAX_SIZE = 4096
def __init__(self, cfg_location=None):
self._cfg_dir = constants.DATA_DIR
else:
self._cfg_dir = cfg_location
- self._cache = {}
def KeyToFilename(self, key):
"""Convert a given key into filename.
will be changed into ConfigurationErrors.
"""
- if key in self._cache:
- return self._cache[key]
filename = self.KeyToFilename(key)
try:
fh = file(filename, 'r')
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):
"""
return self._ReadFile(self.SS_CLUSTER_NAME)
- def SetKey(self, key, value):
- """Set the value of a key.
+ def GetFileStorageDir(self):
+ """Get the file storage dir.
+
+ """
+ return self._ReadFile(self.SS_FILE_STORAGE_DIR)
- This should be used only when adding a node to a cluster.
+ def GetConfigVersion(self):
+ """Get the configuration version.
"""
- file_name = self.KeyToFilename(key)
- dir_name, small_name = os.path.split(file_name)
- fd, new_name = tempfile.mkstemp('.new', small_name, dir_name)
- # here we need to make sure we remove the temp file, if any error
- # leaves it in place
+ value = self._ReadFile(self.SS_CONFIG_VERSION)
try:
- os.chown(new_name, 0, 0)
- os.chmod(new_name, 0400)
- os.write(fd, "%s\n" % str(value))
- os.fsync(fd)
- os.rename(new_name, file_name)
- self._cache[key] = value
- finally:
- os.close(fd)
- try:
- os.unlink(new_name)
- except OSError, err:
- if err.errno != errno.ENOENT:
- raise
+ 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)
+
+
+def GetMasterAndMyself(ss=None):
+ """Get the master node and my own hostname.
+
+ This can be either used for a 'soft' check (compared to CheckMaster,
+ which exits) or just for computing both at the same time.
+
+ The function does not handle any errors, these should be handled in
+ the caller (errors.ConfigurationError, errors.ResolverError).
+
+ """
+ if ss is None:
+ ss = SimpleStore()
+ return ss.GetMasterNode(), utils.HostInfo().name
+
+def CheckMaster(debug, ss=None):
+ """Checks the node setup.
+
+ If this is the master, the function will return. Otherwise it will
+ exit with an exit code based on the node status.
+
+ """
+ try:
+ master_name, myself = GetMasterAndMyself(ss)
+ except errors.ConfigurationError, err:
+ print "Cluster configuration incomplete: '%s'" % str(err)
+ sys.exit(constants.EXIT_NODESETUP_ERROR)
+ except errors.ResolverError, err:
+ sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
+ sys.exit(constants.EXIT_NODESETUP_ERROR)
+
+ if myself != master_name:
+ if debug:
+ sys.stderr.write("Not master, exiting.\n")
+ sys.exit(constants.EXIT_NOTMASTER)