4 # Copyright (C) 2006, 2007 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 """Ganeti master script
24 Exit codes, for both start and stop:
25 - 0: master setup successful
26 - 1: some generic error (this exit code can also be thrown by exceptions)
27 - 11: node is not master, nothing to do
28 - 12: node setup incomplete, cannot start
29 - 13: node should be master, but someone has the ip address already
31 Only exit codes 0 and 11 represent an ok state. Code 1 was left for
32 generic errors as other python code can cause exit with code 1.
40 from optparse import OptionParser
42 from ganeti import logger
43 from ganeti import constants
44 from ganeti import errors
45 from ganeti import ssconf
46 from ganeti import utils
50 EXIT_NOTMASTER = constants.EXIT_NOTMASTER
51 EXIT_NODESETUP_ERROR = 12
52 EXIT_DUPLICATE_IP = 13
57 """Parse the command line options.
60 (options, args) as from OptionParser.parse_args()
63 parser = OptionParser(description="Ganeti master",
65 version="%%prog (ganeti) %s" %
66 constants.RELEASE_VERSION)
68 parser.add_option("-d", "--debug", dest="debug",
69 help="Enable some debug messages",
70 default=False, action="store_true")
71 options, args = parser.parse_args()
73 if len(args) != 1 or args[0] not in ("start", "stop"):
74 sys.stderr.write("Usage: %s [-d] start|stop\n" % sys.argv[0])
75 sys.exit(EXIT_ARGS_ERROR)
80 def CheckNodeSetup(debug):
81 """Checks the node setup.
83 If the node setup if ok, this function will return the tuple
84 (master_hostname, master_netdev, master_ip). Otherwise the return
88 for fname in (constants.SSL_CERT_FILE,):
89 if not os.path.isfile(fname):
91 sys.stderr.write("Missing config file %s.\n" % fname)
94 ss = ssconf.SimpleStore()
95 port = ss.GetNodeDaemonPort()
96 pwdata = ss.GetNodeDaemonPassword()
97 master_name = ss.GetMasterNode()
98 master_netdev = ss.GetMasterNetdev()
99 master_ip = ss.GetMasterIP()
100 except errors.ConfigurationError, err:
102 sys.stderr.write("Cluster configuration incomplete: '%s'\n" % str(err))
104 return (master_name, master_netdev, master_ip)
107 def StartMaster(master_netdev, master_ip, debug):
108 """Starts the master.
111 result = utils.RunCmd(["fping", "-q", master_ip])
112 if not result.failed:
113 r2 = utils.RunCmd(["fping", "-q", "-S127.0.0.1", master_ip])
114 if not result.failed:
115 # we already have the ip:
117 sys.stderr.write("Notice: already started.\n")
120 return EXIT_DUPLICATE_IP
121 result = utils.RunCmd(["ip", "address", "add", "%s/32" % master_ip,
122 "dev", master_netdev, "label",
123 "%s:0" % master_netdev])
126 sys.stderr.write("Can't activate master IP: %s\n" % result.output)
127 return EXIT_SOME_ERROR
129 result = utils.RunCmd(["arping", "-q", "-U", "-c 3", "-I", master_netdev,
130 "-s", master_ip, master_ip])
131 # we'll ignore the exit code of arping
135 def StopMaster(master_netdev, master_ip, debug):
139 result = utils.RunCmd(["ip", "address", "del", "%s/32" % master_ip,
140 "dev", master_netdev])
143 sys.stderr.write("Can't remove the master IP, error: %s" % result.output)
144 # but otherwise ignore the failure
152 options, args = ParseOptions()
153 debug = options.debug
154 result = CheckNodeSetup(debug)
157 sys.stderr.write("Node configuration incomplete.\n")
158 return EXIT_NODESETUP_ERROR
160 master_node, master_netdev, master_ip = result
161 if socket.gethostname() != master_node and args[0] == "start":
163 sys.stderr.write("Not master, ignoring request.\n")
164 return EXIT_NOTMASTER
166 if args[0] == "start":
171 return fn(master_netdev, master_ip, debug)
174 if __name__=='__main__':