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"
37 from ganeti import utils
38 from ganeti import cli
45 class Error(Exception):
46 """Generic exception"""
51 """Reads configuration file.
56 return simplejson.load(f)
61 def WriteConfig(path, data):
62 """Writes the configuration file.
65 if not options.dry_run:
66 utils.CreateBackup(path)
68 (fd, name) = tempfile.mkstemp(dir=os.path.dirname(path))
69 f = os.fdopen(fd, 'w')
72 simplejson.dump(data, f)
85 def UpdateFromVersion2To3(cfg):
86 """Updates the configuration from version 2 to 3.
89 if cfg['cluster']['config_version'] != 2:
93 if 'tcpudp_port_pool' not in cfg['cluster']:
94 cfg['cluster']['tcpudp_port_pool'] = []
97 if 'default_bridge' not in cfg['cluster']:
98 cfg['cluster']['default_bridge'] = 'xen-br0'
99 for inst in cfg['instances'].values():
100 for nic in inst['nics']:
101 if 'bridge' not in nic:
104 cfg['cluster']['config_version'] = 3
108 if __name__ == "__main__":
109 program = os.path.basename(sys.argv[0])
112 parser = optparse.OptionParser(usage="%prog [options] <config-file>")
113 parser.add_option('--dry-run', dest='dry_run',
115 help="Try to do the conversion, but don't write"
117 parser.add_option(cli.FORCE_OPT)
118 parser.add_option('--verbose', dest='verbose',
120 help="Verbose output")
121 (options, args) = parser.parse_args()
127 raise Error("Configuration file not specified")
129 if not options.force:
130 usertext = ("%s MUST run on the master node. Is this the master"
132 if not cli.AskUser(usertext):
135 config = ReadConfig(cfg_file)
139 print "Before upgrade:"
140 pprint.pprint(config)
143 UpdateFromVersion2To3(config)
146 print "After upgrade:"
147 pprint.pprint(config)
150 WriteConfig(cfg_file, config)
152 print "The configuration file has been updated successfully. Please run"
153 print " gnt-cluster copyfile %s" % cfg_file
156 # vim: set foldmethod=marker :