4 # Copyright (C) 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 """Tool to upgrade the configuration file.
24 This code handles only the types supported by simplejson. As an example, "set"
38 from ganeti import utils
39 from ganeti import cli
46 class Error(Exception):
47 """Generic exception"""
52 """Reads configuration file.
57 return simplejson.load(f)
62 def WriteConfig(path, data):
63 """Writes the configuration file.
66 if not options.dry_run:
67 utils.CreateBackup(path)
69 (fd, name) = tempfile.mkstemp(dir=os.path.dirname(path))
70 f = os.fdopen(fd, 'w')
73 simplejson.dump(data, f)
86 def UpdateFromVersion2To3(cfg):
87 """Updates the configuration from version 2 to 3.
90 if cfg['cluster']['config_version'] != 2:
94 if 'tcpudp_port_pool' not in cfg['cluster']:
95 cfg['cluster']['tcpudp_port_pool'] = []
98 if 'default_bridge' not in cfg['cluster']:
99 cfg['cluster']['default_bridge'] = 'xen-br0'
100 for inst in cfg['instances'].values():
101 for nic in inst['nics']:
102 if 'bridge' not in nic:
105 cfg['cluster']['config_version'] = 3
109 """Configures the logging module.
112 formatter = logging.Formatter("%(asctime)s: %(message)s")
114 stderr_handler = logging.StreamHandler()
115 stderr_handler.setFormatter(formatter)
117 stderr_handler.setLevel(logging.NOTSET)
118 elif options.verbose:
119 stderr_handler.setLevel(logging.INFO)
121 stderr_handler.setLevel(logging.CRITICAL)
123 root_logger = logging.getLogger("")
124 root_logger.setLevel(logging.NOTSET)
125 root_logger.addHandler(stderr_handler)
129 if __name__ == "__main__":
130 program = os.path.basename(sys.argv[0])
133 parser = optparse.OptionParser(usage="%prog [options] <config-file>")
134 parser.add_option('--dry-run', dest='dry_run',
136 help="Try to do the conversion, but don't write"
138 parser.add_option(cli.FORCE_OPT)
139 parser.add_option(cli.DEBUG_OPT)
140 parser.add_option('--verbose', dest='verbose',
142 help="Verbose output")
143 (options, args) = parser.parse_args()
151 raise Error("Configuration file not specified")
153 if not options.force:
154 usertext = ("%s MUST run on the master node. Is this the master"
156 if not cli.AskUser(usertext):
159 config = ReadConfig(cfg_file)
163 print "Before upgrade:"
164 pprint.pprint(config)
167 UpdateFromVersion2To3(config)
170 print "After upgrade:"
171 pprint.pprint(config)
174 WriteConfig(cfg_file, config)
176 print "The configuration file has been updated successfully. Please run"
177 print " gnt-cluster copyfile %s" % cfg_file
180 # vim: set foldmethod=marker :