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:
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 class SimpleConfigWriter(SimpleConfigReader):
79 """Simple class to write configuration file.
82 def SetMasterNode(self, node):
83 """Change master node.
86 self._config_data["cluster"]["master_node"] = node
89 """Writes configuration file.
91 Warning: Doesn't take care of locking or synchronizing with other
95 utils.WriteFile(self._file_name,
96 data=serializer.Dump(self._config_data),
100 def GetMasterAndMyself(ss=None):
101 """Get the master node and my own hostname.
103 This can be either used for a 'soft' check (compared to CheckMaster,
104 which exits) or just for computing both at the same time.
106 The function does not handle any errors, these should be handled in
107 the caller (errors.ConfigurationError, errors.ResolverError).
112 return ss.GetMasterNode(), utils.HostInfo().name
114 def CheckMaster(debug, ss=None):
115 """Checks the node setup.
117 If this is the master, the function will return. Otherwise it will
118 exit with an exit code based on the node status.
122 master_name, myself = GetMasterAndMyself(ss)
123 except errors.ConfigurationError, err:
124 print "Cluster configuration incomplete: '%s'" % str(err)
125 sys.exit(constants.EXIT_NODESETUP_ERROR)
126 except errors.ResolverError, err:
127 sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
128 sys.exit(constants.EXIT_NODESETUP_ERROR)
130 if myself != master_name:
132 sys.stderr.write("Not master, exiting.\n")
133 sys.exit(constants.EXIT_NOTMASTER)