root / lib / ssconf.py @ d9f311d7
History | View | Annotate | Download (5.8 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007 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 |
|
31 |
from ganeti import errors |
32 |
from ganeti import constants |
33 |
from ganeti import utils |
34 |
|
35 |
|
36 |
class SimpleStore: |
37 |
"""Interface to static cluster data.
|
38 |
|
39 |
This is different that the config.ConfigWriter class in that it
|
40 |
holds data that is (mostly) constant after the cluster
|
41 |
initialization. Its purpose is to allow limited customization of
|
42 |
things which would otherwise normally live in constants.py. Note
|
43 |
that this data cannot live in ConfigWriter as that is available only
|
44 |
on the master node, and our data must be readable by both the master
|
45 |
and the nodes.
|
46 |
|
47 |
Other particularities of the datastore:
|
48 |
- keys are restricted to predefined values
|
49 |
- values are small (<4k)
|
50 |
- some keys are handled specially (read from the system)
|
51 |
|
52 |
"""
|
53 |
_SS_FILEPREFIX = "ssconf_"
|
54 |
SS_HYPERVISOR = "hypervisor"
|
55 |
SS_NODED_PASS = "node_pass"
|
56 |
SS_MASTER_NODE = "master_node"
|
57 |
SS_MASTER_IP = "master_ip"
|
58 |
SS_MASTER_NETDEV = "master_netdev"
|
59 |
SS_CLUSTER_NAME = "cluster_name"
|
60 |
SS_FILE_STORAGE_DIR = "file_storage_dir"
|
61 |
SS_CONFIG_VERSION = "config_version"
|
62 |
_VALID_KEYS = (SS_HYPERVISOR, SS_NODED_PASS, SS_MASTER_NODE, SS_MASTER_IP, |
63 |
SS_MASTER_NETDEV, SS_CLUSTER_NAME, SS_FILE_STORAGE_DIR, |
64 |
SS_CONFIG_VERSION) |
65 |
_MAX_SIZE = 4096
|
66 |
|
67 |
def __init__(self, cfg_location=None): |
68 |
if cfg_location is None: |
69 |
self._cfg_dir = constants.DATA_DIR
|
70 |
else:
|
71 |
self._cfg_dir = cfg_location
|
72 |
|
73 |
def KeyToFilename(self, key): |
74 |
"""Convert a given key into filename.
|
75 |
|
76 |
"""
|
77 |
if key not in self._VALID_KEYS: |
78 |
raise errors.ProgrammerError("Invalid key requested from SSConf: '%s'" |
79 |
% str(key))
|
80 |
|
81 |
filename = self._cfg_dir + '/' + self._SS_FILEPREFIX + key |
82 |
return filename
|
83 |
|
84 |
def _ReadFile(self, key): |
85 |
"""Generic routine to read keys.
|
86 |
|
87 |
This will read the file which holds the value requested. Errors
|
88 |
will be changed into ConfigurationErrors.
|
89 |
|
90 |
"""
|
91 |
filename = self.KeyToFilename(key)
|
92 |
try:
|
93 |
fh = file(filename, 'r') |
94 |
try:
|
95 |
data = fh.readline(self._MAX_SIZE)
|
96 |
data = data.rstrip('\n')
|
97 |
finally:
|
98 |
fh.close() |
99 |
except EnvironmentError, err: |
100 |
raise errors.ConfigurationError("Can't read from the ssconf file:" |
101 |
" '%s'" % str(err)) |
102 |
return data
|
103 |
|
104 |
def GetNodeDaemonPort(self): |
105 |
"""Get the node daemon port for this cluster.
|
106 |
|
107 |
Note that this routine does not read a ganeti-specific file, but
|
108 |
instead uses socket.getservbyname to allow pre-customization of
|
109 |
this parameter outside of ganeti.
|
110 |
|
111 |
"""
|
112 |
try:
|
113 |
port = socket.getservbyname("ganeti-noded", "tcp") |
114 |
except socket.error:
|
115 |
port = constants.DEFAULT_NODED_PORT |
116 |
|
117 |
return port
|
118 |
|
119 |
def GetHypervisorType(self): |
120 |
"""Get the hypervisor type for this cluster.
|
121 |
|
122 |
"""
|
123 |
return self._ReadFile(self.SS_HYPERVISOR) |
124 |
|
125 |
def GetNodeDaemonPassword(self): |
126 |
"""Get the node password for this cluster.
|
127 |
|
128 |
"""
|
129 |
return self._ReadFile(self.SS_NODED_PASS) |
130 |
|
131 |
def GetMasterNode(self): |
132 |
"""Get the hostname of the master node for this cluster.
|
133 |
|
134 |
"""
|
135 |
return self._ReadFile(self.SS_MASTER_NODE) |
136 |
|
137 |
def GetMasterIP(self): |
138 |
"""Get the IP of the master node for this cluster.
|
139 |
|
140 |
"""
|
141 |
return self._ReadFile(self.SS_MASTER_IP) |
142 |
|
143 |
def GetMasterNetdev(self): |
144 |
"""Get the netdev to which we'll add the master ip.
|
145 |
|
146 |
"""
|
147 |
return self._ReadFile(self.SS_MASTER_NETDEV) |
148 |
|
149 |
def GetClusterName(self): |
150 |
"""Get the cluster name.
|
151 |
|
152 |
"""
|
153 |
return self._ReadFile(self.SS_CLUSTER_NAME) |
154 |
|
155 |
def GetFileStorageDir(self): |
156 |
"""Get the file storage dir.
|
157 |
|
158 |
"""
|
159 |
return self._ReadFile(self.SS_FILE_STORAGE_DIR) |
160 |
|
161 |
def GetConfigVersion(self): |
162 |
"""Get the configuration version.
|
163 |
|
164 |
"""
|
165 |
value = self._ReadFile(self.SS_CONFIG_VERSION) |
166 |
try:
|
167 |
return int(value) |
168 |
except (ValueError, TypeError), err: |
169 |
raise errors.ConfigurationError("Failed to convert config version %s to" |
170 |
" int: '%s'" % (value, str(err))) |
171 |
|
172 |
def GetFileList(self): |
173 |
"""Return the list of all config files.
|
174 |
|
175 |
This is used for computing node replication data.
|
176 |
|
177 |
"""
|
178 |
return [self.KeyToFilename(key) for key in self._VALID_KEYS] |
179 |
|
180 |
|
181 |
class WritableSimpleStore(SimpleStore): |
182 |
"""This is a read/write interface to SimpleStore, which is used rarely, when
|
183 |
values need to be changed. Since WriteFile handles updates in an atomic way
|
184 |
it should be fine to use two WritableSimpleStore at the same time, but in
|
185 |
the future we might want to put additional protection for this class.
|
186 |
|
187 |
A WritableSimpleStore cannot be used to update system-dependent values.
|
188 |
|
189 |
"""
|
190 |
|
191 |
def SetKey(self, key, value): |
192 |
"""Set the value of a key.
|
193 |
|
194 |
This should be used only when adding a node to a cluster, or in other
|
195 |
infrequent operations such as cluster-rename or master-failover.
|
196 |
|
197 |
"""
|
198 |
file_name = self.KeyToFilename(key)
|
199 |
utils.WriteFile(file_name, data="%s\n" % str(value), |
200 |
uid=0, gid=0, mode=0400) |
201 |
|