4 # Copyright (C) 2006, 2007, 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Global Configuration data for Ganeti.
24 This module provides the interface to a special case of cluster
25 configuration data, which is mostly static and available to all nodes.
32 from ganeti import errors
33 from ganeti import constants
34 from ganeti import utils
35 from ganeti import serializer
38 class SimpleConfigReader(object):
39 """Simple class to read configuration file.
42 def __init__(self, file_name=constants.CLUSTER_CONF_FILE):
43 """Initializes this class.
45 @type file_name: string
46 @param file_name: Configuration file path
49 self._file_name = file_name
50 self._config_data = serializer.Load(utils.ReadFile(file_name))
51 # TODO: Error handling
53 def GetClusterName(self):
54 return self._config_data["cluster"]["cluster_name"]
57 return self._config_data["cluster"]["rsahostkeypub"]
59 def GetMasterNode(self):
60 return self._config_data["cluster"]["master_node"]
62 def GetMasterIP(self):
63 return self._config_data["cluster"]["master_ip"]
65 def GetMasterNetdev(self):
66 return self._config_data["cluster"]["master_netdev"]
68 def GetFileStorageDir(self):
69 return self._config_data["cluster"]["file_storage_dir"]
71 def GetHypervisorType(self):
72 return self._config_data["cluster"]["hypervisor"]
74 def GetNodeList(self):
75 return self._config_data["nodes"].keys()
78 def FromDict(cls, val, cfg_file=constants.CLUSTER_CONF_FILE):
79 """Alternative construction from a dictionary.
82 obj = SimpleConfigReader.__new__(cls)
83 obj._config_data = val
84 obj._file_name = cfg_file
88 class SimpleConfigWriter(SimpleConfigReader):
89 """Simple class to write configuration file.
92 def SetMasterNode(self, node):
93 """Change master node.
96 self._config_data["cluster"]["master_node"] = node
99 """Writes configuration file.
101 Warning: Doesn't take care of locking or synchronizing with other
105 utils.WriteFile(self._file_name,
106 data=serializer.Dump(self._config_data),
110 def GetMasterAndMyself(ss=None):
111 """Get the master node and my own hostname.
113 This can be either used for a 'soft' check (compared to CheckMaster,
114 which exits) or just for computing both at the same time.
116 The function does not handle any errors, these should be handled in
117 the caller (errors.ConfigurationError, errors.ResolverError).
121 ss = SimpleConfigReader()
122 return ss.GetMasterNode(), utils.HostInfo().name
125 def CheckMaster(debug, ss=None):
126 """Checks the node setup.
128 If this is the master, the function will return. Otherwise it will
129 exit with an exit code based on the node status.
133 master_name, myself = GetMasterAndMyself(ss)
134 except errors.ConfigurationError, err:
135 print "Cluster configuration incomplete: '%s'" % str(err)
136 sys.exit(constants.EXIT_NODESETUP_ERROR)
137 except errors.ResolverError, err:
138 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
139 sys.exit(constants.EXIT_NODESETUP_ERROR)
141 if myself != master_name:
143 sys.stderr.write("Not master, exiting.\n")
144 sys.exit(constants.EXIT_NOTMASTER)