Get rid of ssconf
[ganeti-local] / lib / ssconf.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Global Configuration data for Ganeti.
23
24 This module provides the interface to a special case of cluster
25 configuration data, which is mostly static and available to all nodes.
26
27 """
28
29 import socket
30 import sys
31
32 from ganeti import errors
33 from ganeti import constants
34 from ganeti import utils
35 from ganeti import serializer
36
37
38 class SimpleConfigReader:
39   """Simple class to read configuration file.
40
41   """
42   def __init__(self, file_name=constants.CLUSTER_CONF_FILE):
43     """Initializes this class.
44
45     @type file_name: string
46     @param file_name: Configuration file path
47
48     """
49     self._file_name = file_name
50     self._config_data = serializer.Load(utils.ReadFile(file_name))
51     # TODO: Error handling
52
53   def GetClusterName(self):
54     return self._config_data["cluster"]["cluster_name"]
55
56   def GetHostKey(self):
57     return self._config_data["cluster"]["rsahostkeypub"]
58
59   def GetMasterNode(self):
60     return self._config_data["cluster"]["master_node"]
61
62   def GetMasterIP(self):
63     return self._config_data["cluster"]["master_ip"]
64
65   def GetMasterNetdev(self):
66     return self._config_data["cluster"]["master_netdev"]
67
68   def GetFileStorageDir(self):
69     return self._config_data["cluster"]["file_storage_dir"]
70
71   def GetHypervisorType(self):
72     return self._config_data["cluster"]["hypervisor"]
73
74   def GetNodeList(self):
75     return self._config_data["nodes"].keys()
76
77
78 class SimpleConfigWriter(SimpleConfigReader):
79   """Simple class to write configuration file.
80
81   """
82   def SetMasterNode(self, node):
83     """Change master node.
84
85     """
86     self._config_data["cluster"]["master_node"] = node
87
88   def Save(self):
89     """Writes configuration file.
90
91     Warning: Doesn't take care of locking or synchronizing with other
92     processes.
93
94     """
95     utils.WriteFile(self._file_name,
96                     data=serializer.Dump(self._config_data),
97                     mode=0600)
98
99
100 def GetMasterAndMyself(ss=None):
101   """Get the master node and my own hostname.
102
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.
105
106   The function does not handle any errors, these should be handled in
107   the caller (errors.ConfigurationError, errors.ResolverError).
108
109   """
110   if ss is None:
111     ss = SimpleStore()
112   return ss.GetMasterNode(), utils.HostInfo().name
113
114 def CheckMaster(debug, ss=None):
115   """Checks the node setup.
116
117   If this is the master, the function will return. Otherwise it will
118   exit with an exit code based on the node status.
119
120   """
121   try:
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)
129
130   if myself != master_name:
131     if debug:
132       sys.stderr.write("Not master, exiting.\n")
133     sys.exit(constants.EXIT_NOTMASTER)