Statistics
| Branch: | Tag: | Revision:

root / lib / ssconf.py @ 58b311ca

History | View | Annotate | Download (3.9 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 856c67e1 Michael Hanselmann
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
"""Global Configuration data for Ganeti.
23 a8083063 Iustin Pop

24 a8083063 Iustin Pop
This module provides the interface to a special case of cluster
25 a8083063 Iustin Pop
configuration data, which is mostly static and available to all nodes.
26 a8083063 Iustin Pop

27 a8083063 Iustin Pop
"""
28 a8083063 Iustin Pop
29 a8083063 Iustin Pop
import socket
30 5675cd1f Iustin Pop
import sys
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
from ganeti import errors
33 a8083063 Iustin Pop
from ganeti import constants
34 41a57aab Michael Hanselmann
from ganeti import utils
35 856c67e1 Michael Hanselmann
from ganeti import serializer
36 a8083063 Iustin Pop
37 a8083063 Iustin Pop
38 02f99608 Oleksiy Mishchenko
class SimpleConfigReader(object):
39 856c67e1 Michael Hanselmann
  """Simple class to read configuration file.
40 856c67e1 Michael Hanselmann

41 856c67e1 Michael Hanselmann
  """
42 856c67e1 Michael Hanselmann
  def __init__(self, file_name=constants.CLUSTER_CONF_FILE):
43 856c67e1 Michael Hanselmann
    """Initializes this class.
44 856c67e1 Michael Hanselmann

45 856c67e1 Michael Hanselmann
    @type file_name: string
46 856c67e1 Michael Hanselmann
    @param file_name: Configuration file path
47 856c67e1 Michael Hanselmann

48 856c67e1 Michael Hanselmann
    """
49 856c67e1 Michael Hanselmann
    self._file_name = file_name
50 856c67e1 Michael Hanselmann
    self._config_data = serializer.Load(utils.ReadFile(file_name))
51 856c67e1 Michael Hanselmann
    # TODO: Error handling
52 856c67e1 Michael Hanselmann
53 856c67e1 Michael Hanselmann
  def GetClusterName(self):
54 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["cluster_name"]
55 856c67e1 Michael Hanselmann
56 856c67e1 Michael Hanselmann
  def GetHostKey(self):
57 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["rsahostkeypub"]
58 856c67e1 Michael Hanselmann
59 856c67e1 Michael Hanselmann
  def GetMasterNode(self):
60 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["master_node"]
61 856c67e1 Michael Hanselmann
62 856c67e1 Michael Hanselmann
  def GetMasterIP(self):
63 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["master_ip"]
64 856c67e1 Michael Hanselmann
65 856c67e1 Michael Hanselmann
  def GetMasterNetdev(self):
66 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["master_netdev"]
67 856c67e1 Michael Hanselmann
68 856c67e1 Michael Hanselmann
  def GetFileStorageDir(self):
69 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["file_storage_dir"]
70 856c67e1 Michael Hanselmann
71 856c67e1 Michael Hanselmann
  def GetHypervisorType(self):
72 856c67e1 Michael Hanselmann
    return self._config_data["cluster"]["hypervisor"]
73 856c67e1 Michael Hanselmann
74 856c67e1 Michael Hanselmann
  def GetNodeList(self):
75 856c67e1 Michael Hanselmann
    return self._config_data["nodes"].keys()
76 856c67e1 Michael Hanselmann
77 02f99608 Oleksiy Mishchenko
  @classmethod
78 02f99608 Oleksiy Mishchenko
  def FromDict(cls, val, cfg_file=constants.CLUSTER_CONF_FILE):
79 02f99608 Oleksiy Mishchenko
    """Alternative construction from a dictionary.
80 02f99608 Oleksiy Mishchenko

81 02f99608 Oleksiy Mishchenko
    """
82 02f99608 Oleksiy Mishchenko
    obj = SimpleConfigReader.__new__(cls)
83 02f99608 Oleksiy Mishchenko
    obj._config_data = val
84 02f99608 Oleksiy Mishchenko
    obj._file_name = cfg_file
85 02f99608 Oleksiy Mishchenko
    return obj
86 02f99608 Oleksiy Mishchenko
87 856c67e1 Michael Hanselmann
88 856c67e1 Michael Hanselmann
class SimpleConfigWriter(SimpleConfigReader):
89 856c67e1 Michael Hanselmann
  """Simple class to write configuration file.
90 856c67e1 Michael Hanselmann

91 856c67e1 Michael Hanselmann
  """
92 856c67e1 Michael Hanselmann
  def SetMasterNode(self, node):
93 856c67e1 Michael Hanselmann
    """Change master node.
94 856c67e1 Michael Hanselmann

95 856c67e1 Michael Hanselmann
    """
96 856c67e1 Michael Hanselmann
    self._config_data["cluster"]["master_node"] = node
97 856c67e1 Michael Hanselmann
98 856c67e1 Michael Hanselmann
  def Save(self):
99 856c67e1 Michael Hanselmann
    """Writes configuration file.
100 856c67e1 Michael Hanselmann

101 856c67e1 Michael Hanselmann
    Warning: Doesn't take care of locking or synchronizing with other
102 856c67e1 Michael Hanselmann
    processes.
103 856c67e1 Michael Hanselmann

104 856c67e1 Michael Hanselmann
    """
105 856c67e1 Michael Hanselmann
    utils.WriteFile(self._file_name,
106 856c67e1 Michael Hanselmann
                    data=serializer.Dump(self._config_data),
107 856c67e1 Michael Hanselmann
                    mode=0600)
108 856c67e1 Michael Hanselmann
109 856c67e1 Michael Hanselmann
110 b33e986b Iustin Pop
def GetMasterAndMyself(ss=None):
111 b33e986b Iustin Pop
  """Get the master node and my own hostname.
112 b33e986b Iustin Pop

113 b33e986b Iustin Pop
  This can be either used for a 'soft' check (compared to CheckMaster,
114 b33e986b Iustin Pop
  which exits) or just for computing both at the same time.
115 b33e986b Iustin Pop

116 b33e986b Iustin Pop
  The function does not handle any errors, these should be handled in
117 b33e986b Iustin Pop
  the caller (errors.ConfigurationError, errors.ResolverError).
118 b33e986b Iustin Pop

119 b33e986b Iustin Pop
  """
120 b33e986b Iustin Pop
  if ss is None:
121 06dc5b44 Iustin Pop
    ss = SimpleConfigReader()
122 b33e986b Iustin Pop
  return ss.GetMasterNode(), utils.HostInfo().name
123 b33e986b Iustin Pop
124 06dc5b44 Iustin Pop
125 b33e986b Iustin Pop
def CheckMaster(debug, ss=None):
126 5675cd1f Iustin Pop
  """Checks the node setup.
127 5675cd1f Iustin Pop

128 5675cd1f Iustin Pop
  If this is the master, the function will return. Otherwise it will
129 5675cd1f Iustin Pop
  exit with an exit code based on the node status.
130 5675cd1f Iustin Pop

131 5675cd1f Iustin Pop
  """
132 5675cd1f Iustin Pop
  try:
133 b33e986b Iustin Pop
    master_name, myself = GetMasterAndMyself(ss)
134 5675cd1f Iustin Pop
  except errors.ConfigurationError, err:
135 5675cd1f Iustin Pop
    print "Cluster configuration incomplete: '%s'" % str(err)
136 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
137 5675cd1f Iustin Pop
  except errors.ResolverError, err:
138 5675cd1f Iustin Pop
    sys.stderr.write("Cannot resolve my own name (%s)\n" % err.args[0])
139 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NODESETUP_ERROR)
140 5675cd1f Iustin Pop
141 b33e986b Iustin Pop
  if myself != master_name:
142 5675cd1f Iustin Pop
    if debug:
143 5675cd1f Iustin Pop
      sys.stderr.write("Not master, exiting.\n")
144 5675cd1f Iustin Pop
    sys.exit(constants.EXIT_NOTMASTER)