Statistics
| Branch: | Tag: | Revision:

root / tools / cfgupgrade @ b555101c

History | View | Annotate | Download (19.4 kB)

1 0006af7d Michael Hanselmann
#!/usr/bin/python
2 0006af7d Michael Hanselmann
#
3 0006af7d Michael Hanselmann
4 fdb85e3d Bernardo Dal Seno
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 0006af7d Michael Hanselmann
#
6 0006af7d Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 0006af7d Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 0006af7d Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 0006af7d Michael Hanselmann
# (at your option) any later version.
10 0006af7d Michael Hanselmann
#
11 0006af7d Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 0006af7d Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 0006af7d Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 0006af7d Michael Hanselmann
# General Public License for more details.
15 0006af7d Michael Hanselmann
#
16 0006af7d Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 0006af7d Michael Hanselmann
# along with this program; if not, write to the Free Software
18 0006af7d Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 0006af7d Michael Hanselmann
# 02110-1301, USA.
20 0006af7d Michael Hanselmann
21 0006af7d Michael Hanselmann
22 0006af7d Michael Hanselmann
"""Tool to upgrade the configuration file.
23 0006af7d Michael Hanselmann
24 a421fdeb Iustin Pop
This code handles only the types supported by simplejson. As an
25 a421fdeb Iustin Pop
example, 'set' is a 'list'.
26 0006af7d Michael Hanselmann
27 0006af7d Michael Hanselmann
"""
28 0006af7d Michael Hanselmann
29 0006af7d Michael Hanselmann
30 0006af7d Michael Hanselmann
import os
31 0006af7d Michael Hanselmann
import os.path
32 0006af7d Michael Hanselmann
import sys
33 0006af7d Michael Hanselmann
import optparse
34 eda37a5a Michael Hanselmann
import logging
35 7939f60c Michael Hanselmann
import time
36 7939f60c Michael Hanselmann
from cStringIO import StringIO
37 0006af7d Michael Hanselmann
38 95e4a814 Michael Hanselmann
from ganeti import constants
39 95e4a814 Michael Hanselmann
from ganeti import serializer
40 319856a9 Michael Hanselmann
from ganeti import utils
41 f97c7901 Michael Hanselmann
from ganeti import cli
42 a421fdeb Iustin Pop
from ganeti import bootstrap
43 ac4d25b6 Iustin Pop
from ganeti import config
44 011974df Michael Hanselmann
from ganeti import netutils
45 09bf5d24 Michael Hanselmann
from ganeti import pathutils
46 0006af7d Michael Hanselmann
47 0006af7d Michael Hanselmann
48 319856a9 Michael Hanselmann
options = None
49 319856a9 Michael Hanselmann
args = None
50 0006af7d Michael Hanselmann
51 6f285030 Iustin Pop
52 93fd9bb1 Iustin Pop
#: Target major version we will upgrade to
53 93fd9bb1 Iustin Pop
TARGET_MAJOR = 2
54 93fd9bb1 Iustin Pop
#: Target minor version we will upgrade to
55 b830193c Guido Trotter
TARGET_MINOR = 7
56 1709435e Bernardo Dal Seno
#: Target major version for downgrade
57 1709435e Bernardo Dal Seno
DOWNGRADE_MAJOR = 2
58 1709435e Bernardo Dal Seno
#: Target minor version for downgrade
59 1709435e Bernardo Dal Seno
DOWNGRADE_MINOR = 7
60 93fd9bb1 Iustin Pop
61 93fd9bb1 Iustin Pop
62 319856a9 Michael Hanselmann
class Error(Exception):
63 319856a9 Michael Hanselmann
  """Generic exception"""
64 319856a9 Michael Hanselmann
  pass
65 0006af7d Michael Hanselmann
66 0006af7d Michael Hanselmann
67 eda37a5a Michael Hanselmann
def SetupLogging():
68 eda37a5a Michael Hanselmann
  """Configures the logging module.
69 eda37a5a Michael Hanselmann
70 eda37a5a Michael Hanselmann
  """
71 eda37a5a Michael Hanselmann
  formatter = logging.Formatter("%(asctime)s: %(message)s")
72 eda37a5a Michael Hanselmann
73 eda37a5a Michael Hanselmann
  stderr_handler = logging.StreamHandler()
74 eda37a5a Michael Hanselmann
  stderr_handler.setFormatter(formatter)
75 eda37a5a Michael Hanselmann
  if options.debug:
76 eda37a5a Michael Hanselmann
    stderr_handler.setLevel(logging.NOTSET)
77 eda37a5a Michael Hanselmann
  elif options.verbose:
78 eda37a5a Michael Hanselmann
    stderr_handler.setLevel(logging.INFO)
79 eda37a5a Michael Hanselmann
  else:
80 011974df Michael Hanselmann
    stderr_handler.setLevel(logging.WARNING)
81 eda37a5a Michael Hanselmann
82 eda37a5a Michael Hanselmann
  root_logger = logging.getLogger("")
83 eda37a5a Michael Hanselmann
  root_logger.setLevel(logging.NOTSET)
84 eda37a5a Michael Hanselmann
  root_logger.addHandler(stderr_handler)
85 eda37a5a Michael Hanselmann
86 eda37a5a Michael Hanselmann
87 011974df Michael Hanselmann
def CheckHostname(path):
88 011974df Michael Hanselmann
  """Ensures hostname matches ssconf value.
89 011974df Michael Hanselmann
90 011974df Michael Hanselmann
  @param path: Path to ssconf file
91 011974df Michael Hanselmann
92 011974df Michael Hanselmann
  """
93 011974df Michael Hanselmann
  ssconf_master_node = utils.ReadOneLineFile(path)
94 011974df Michael Hanselmann
  hostname = netutils.GetHostname().name
95 011974df Michael Hanselmann
96 011974df Michael Hanselmann
  if ssconf_master_node == hostname:
97 011974df Michael Hanselmann
    return True
98 011974df Michael Hanselmann
99 011974df Michael Hanselmann
  logging.warning("Warning: ssconf says master node is '%s', but this"
100 011974df Michael Hanselmann
                  " machine's name is '%s'; this tool must be run on"
101 011974df Michael Hanselmann
                  " the master node", ssconf_master_node, hostname)
102 011974df Michael Hanselmann
  return False
103 011974df Michael Hanselmann
104 3c286190 Dimitris Aragiorgis
105 e94fc80c Bernardo Dal Seno
def _FillIPolicySpecs(default_ipolicy, ipolicy):
106 e94fc80c Bernardo Dal Seno
  if "minmax" in ipolicy:
107 41044e04 Bernardo Dal Seno
    for (key, spec) in ipolicy["minmax"][0].items():
108 41044e04 Bernardo Dal Seno
      for (par, val) in default_ipolicy["minmax"][0][key].items():
109 e94fc80c Bernardo Dal Seno
        if par not in spec:
110 e94fc80c Bernardo Dal Seno
          spec[par] = val
111 e94fc80c Bernardo Dal Seno
112 e94fc80c Bernardo Dal Seno
113 e94fc80c Bernardo Dal Seno
def UpgradeIPolicy(ipolicy, default_ipolicy, isgroup):
114 0b94cda8 Bernardo Dal Seno
  minmax_keys = ["min", "max"]
115 0b94cda8 Bernardo Dal Seno
  if any((k in ipolicy) for k in minmax_keys):
116 0b94cda8 Bernardo Dal Seno
    minmax = {}
117 0b94cda8 Bernardo Dal Seno
    for key in minmax_keys:
118 0b94cda8 Bernardo Dal Seno
      if key in ipolicy:
119 e94fc80c Bernardo Dal Seno
        if ipolicy[key]:
120 e94fc80c Bernardo Dal Seno
          minmax[key] = ipolicy[key]
121 0b94cda8 Bernardo Dal Seno
        del ipolicy[key]
122 e94fc80c Bernardo Dal Seno
    if minmax:
123 41044e04 Bernardo Dal Seno
      ipolicy["minmax"] = [minmax]
124 e94fc80c Bernardo Dal Seno
  if isgroup and "std" in ipolicy:
125 e94fc80c Bernardo Dal Seno
    del ipolicy["std"]
126 e94fc80c Bernardo Dal Seno
  _FillIPolicySpecs(default_ipolicy, ipolicy)
127 0b94cda8 Bernardo Dal Seno
128 0b94cda8 Bernardo Dal Seno
129 58bf877f Dimitris Aragiorgis
def UpgradeNetworks(config_data):
130 58bf877f Dimitris Aragiorgis
  networks = config_data.get("networks", None)
131 58bf877f Dimitris Aragiorgis
  if not networks:
132 58bf877f Dimitris Aragiorgis
    config_data["networks"] = {}
133 58bf877f Dimitris Aragiorgis
134 58bf877f Dimitris Aragiorgis
135 0b94cda8 Bernardo Dal Seno
def UpgradeCluster(config_data):
136 0b94cda8 Bernardo Dal Seno
  cluster = config_data.get("cluster", None)
137 0b94cda8 Bernardo Dal Seno
  if cluster is None:
138 0b94cda8 Bernardo Dal Seno
    raise Error("Cannot find cluster")
139 e94fc80c Bernardo Dal Seno
  ipolicy = cluster.setdefault("ipolicy", None)
140 0b94cda8 Bernardo Dal Seno
  if ipolicy:
141 e94fc80c Bernardo Dal Seno
    UpgradeIPolicy(ipolicy, constants.IPOLICY_DEFAULTS, False)
142 0b94cda8 Bernardo Dal Seno
143 0b94cda8 Bernardo Dal Seno
144 58bf877f Dimitris Aragiorgis
def UpgradeGroups(config_data):
145 e94fc80c Bernardo Dal Seno
  cl_ipolicy = config_data["cluster"].get("ipolicy")
146 58bf877f Dimitris Aragiorgis
  for group in config_data["nodegroups"].values():
147 58bf877f Dimitris Aragiorgis
    networks = group.get("networks", None)
148 58bf877f Dimitris Aragiorgis
    if not networks:
149 58bf877f Dimitris Aragiorgis
      group["networks"] = {}
150 0b94cda8 Bernardo Dal Seno
    ipolicy = group.get("ipolicy", None)
151 0b94cda8 Bernardo Dal Seno
    if ipolicy:
152 e94fc80c Bernardo Dal Seno
      if cl_ipolicy is None:
153 e94fc80c Bernardo Dal Seno
        raise Error("A group defines an instance policy but there is no"
154 e94fc80c Bernardo Dal Seno
                    " instance policy at cluster level")
155 e94fc80c Bernardo Dal Seno
      UpgradeIPolicy(ipolicy, cl_ipolicy, True)
156 58bf877f Dimitris Aragiorgis
157 011974df Michael Hanselmann
158 c69b147d Bernardo Dal Seno
def GetExclusiveStorageValue(config_data):
159 c69b147d Bernardo Dal Seno
  """Return a conservative value of the exclusive_storage flag.
160 c69b147d Bernardo Dal Seno
161 c69b147d Bernardo Dal Seno
  Return C{True} if the cluster or at least a nodegroup have the flag set.
162 c69b147d Bernardo Dal Seno
163 c69b147d Bernardo Dal Seno
  """
164 c69b147d Bernardo Dal Seno
  ret = False
165 c69b147d Bernardo Dal Seno
  cluster = config_data["cluster"]
166 c69b147d Bernardo Dal Seno
  ndparams = cluster.get("ndparams")
167 c69b147d Bernardo Dal Seno
  if ndparams is not None and ndparams.get("exclusive_storage"):
168 c69b147d Bernardo Dal Seno
    ret = True
169 c69b147d Bernardo Dal Seno
  for group in config_data["nodegroups"].values():
170 c69b147d Bernardo Dal Seno
    ndparams = group.get("ndparams")
171 c69b147d Bernardo Dal Seno
    if ndparams is not None and ndparams.get("exclusive_storage"):
172 c69b147d Bernardo Dal Seno
      ret = True
173 c69b147d Bernardo Dal Seno
  return ret
174 c69b147d Bernardo Dal Seno
175 c69b147d Bernardo Dal Seno
176 f032d55c Dimitris Aragiorgis
def UpgradeInstances(config_data):
177 f032d55c Dimitris Aragiorgis
  network2uuid = dict((n["name"], n["uuid"])
178 f032d55c Dimitris Aragiorgis
                      for n in config_data["networks"].values())
179 bb553e5a Bernardo Dal Seno
  if "instances" not in config_data:
180 bb553e5a Bernardo Dal Seno
    raise Error("Can't find the 'instances' key in the configuration!")
181 bb553e5a Bernardo Dal Seno
182 c69b147d Bernardo Dal Seno
  missing_spindles = False
183 bb553e5a Bernardo Dal Seno
  for instance, iobj in config_data["instances"].items():
184 bb553e5a Bernardo Dal Seno
    for nic in iobj["nics"]:
185 f032d55c Dimitris Aragiorgis
      name = nic.get("network", None)
186 f032d55c Dimitris Aragiorgis
      if name:
187 f032d55c Dimitris Aragiorgis
        uuid = network2uuid.get(name, None)
188 f032d55c Dimitris Aragiorgis
        if uuid:
189 f032d55c Dimitris Aragiorgis
          print("NIC with network name %s found."
190 f032d55c Dimitris Aragiorgis
                " Substituting with uuid %s." % (name, uuid))
191 f032d55c Dimitris Aragiorgis
          nic["network"] = uuid
192 f032d55c Dimitris Aragiorgis
193 bb553e5a Bernardo Dal Seno
    if "disks" not in iobj:
194 bb553e5a Bernardo Dal Seno
      raise Error("Instance '%s' doesn't have a disks entry?!" % instance)
195 bb553e5a Bernardo Dal Seno
    disks = iobj["disks"]
196 bb553e5a Bernardo Dal Seno
    for idx, dobj in enumerate(disks):
197 bb553e5a Bernardo Dal Seno
      expected = "disk/%s" % idx
198 bb553e5a Bernardo Dal Seno
      current = dobj.get("iv_name", "")
199 bb553e5a Bernardo Dal Seno
      if current != expected:
200 bb553e5a Bernardo Dal Seno
        logging.warning("Updating iv_name for instance %s/disk %s"
201 bb553e5a Bernardo Dal Seno
                        " from '%s' to '%s'",
202 bb553e5a Bernardo Dal Seno
                        instance, idx, current, expected)
203 bb553e5a Bernardo Dal Seno
        dobj["iv_name"] = expected
204 c69b147d Bernardo Dal Seno
      if not "spindles" in dobj:
205 c69b147d Bernardo Dal Seno
        missing_spindles = True
206 c69b147d Bernardo Dal Seno
207 c69b147d Bernardo Dal Seno
  if GetExclusiveStorageValue(config_data) and missing_spindles:
208 c69b147d Bernardo Dal Seno
    # We cannot be sure that the instances that are missing spindles have
209 c69b147d Bernardo Dal Seno
    # exclusive storage enabled (the check would be more complicated), so we
210 c69b147d Bernardo Dal Seno
    # give a noncommittal message
211 c69b147d Bernardo Dal Seno
    logging.warning("Some instance disks could be needing to update the"
212 c69b147d Bernardo Dal Seno
                    " spindles parameter; you can check by running"
213 c69b147d Bernardo Dal Seno
                    " 'gnt-cluster verify', and fix any problem with"
214 c69b147d Bernardo Dal Seno
                    " 'gnt-cluster repair-disk-sizes'")
215 bb553e5a Bernardo Dal Seno
216 bb553e5a Bernardo Dal Seno
217 bb553e5a Bernardo Dal Seno
def UpgradeRapiUsers():
218 bb553e5a Bernardo Dal Seno
  if (os.path.isfile(options.RAPI_USERS_FILE_PRE24) and
219 bb553e5a Bernardo Dal Seno
      not os.path.islink(options.RAPI_USERS_FILE_PRE24)):
220 bb553e5a Bernardo Dal Seno
    if os.path.exists(options.RAPI_USERS_FILE):
221 bb553e5a Bernardo Dal Seno
      raise Error("Found pre-2.4 RAPI users file at %s, but another file"
222 bb553e5a Bernardo Dal Seno
                  " already exists at %s" %
223 bb553e5a Bernardo Dal Seno
                  (options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE))
224 bb553e5a Bernardo Dal Seno
    logging.info("Found pre-2.4 RAPI users file at %s, renaming to %s",
225 bb553e5a Bernardo Dal Seno
                 options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE)
226 bb553e5a Bernardo Dal Seno
    if not options.dry_run:
227 bb553e5a Bernardo Dal Seno
      utils.RenameFile(options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE,
228 bb553e5a Bernardo Dal Seno
                       mkdir=True, mkdir_mode=0750)
229 bb553e5a Bernardo Dal Seno
230 bb553e5a Bernardo Dal Seno
  # Create a symlink for RAPI users file
231 bb553e5a Bernardo Dal Seno
  if (not (os.path.islink(options.RAPI_USERS_FILE_PRE24) or
232 bb553e5a Bernardo Dal Seno
           os.path.isfile(options.RAPI_USERS_FILE_PRE24)) and
233 bb553e5a Bernardo Dal Seno
      os.path.isfile(options.RAPI_USERS_FILE)):
234 bb553e5a Bernardo Dal Seno
    logging.info("Creating symlink from %s to %s",
235 bb553e5a Bernardo Dal Seno
                 options.RAPI_USERS_FILE_PRE24, options.RAPI_USERS_FILE)
236 bb553e5a Bernardo Dal Seno
    if not options.dry_run:
237 bb553e5a Bernardo Dal Seno
      os.symlink(options.RAPI_USERS_FILE, options.RAPI_USERS_FILE_PRE24)
238 bb553e5a Bernardo Dal Seno
239 bb553e5a Bernardo Dal Seno
240 bb553e5a Bernardo Dal Seno
def UpgradeWatcher():
241 bb553e5a Bernardo Dal Seno
  # Remove old watcher state file if it exists
242 bb553e5a Bernardo Dal Seno
  if os.path.exists(options.WATCHER_STATEFILE):
243 bb553e5a Bernardo Dal Seno
    logging.info("Removing watcher state file %s", options.WATCHER_STATEFILE)
244 bb553e5a Bernardo Dal Seno
    if not options.dry_run:
245 bb553e5a Bernardo Dal Seno
      utils.RemoveFile(options.WATCHER_STATEFILE)
246 bb553e5a Bernardo Dal Seno
247 bb553e5a Bernardo Dal Seno
248 bb553e5a Bernardo Dal Seno
def UpgradeFileStoragePaths(config_data):
249 bb553e5a Bernardo Dal Seno
  # Write file storage paths
250 bb553e5a Bernardo Dal Seno
  if not os.path.exists(options.FILE_STORAGE_PATHS_FILE):
251 bb553e5a Bernardo Dal Seno
    cluster = config_data["cluster"]
252 bb553e5a Bernardo Dal Seno
    file_storage_dir = cluster.get("file_storage_dir")
253 bb553e5a Bernardo Dal Seno
    shared_file_storage_dir = cluster.get("shared_file_storage_dir")
254 bb553e5a Bernardo Dal Seno
    del cluster
255 bb553e5a Bernardo Dal Seno
256 bb553e5a Bernardo Dal Seno
    logging.info("Ganeti 2.7 and later only allow whitelisted directories"
257 bb553e5a Bernardo Dal Seno
                 " for file storage; writing existing configuration values"
258 bb553e5a Bernardo Dal Seno
                 " into '%s'",
259 bb553e5a Bernardo Dal Seno
                 options.FILE_STORAGE_PATHS_FILE)
260 bb553e5a Bernardo Dal Seno
261 bb553e5a Bernardo Dal Seno
    if file_storage_dir:
262 bb553e5a Bernardo Dal Seno
      logging.info("File storage directory: %s", file_storage_dir)
263 bb553e5a Bernardo Dal Seno
    if shared_file_storage_dir:
264 bb553e5a Bernardo Dal Seno
      logging.info("Shared file storage directory: %s",
265 bb553e5a Bernardo Dal Seno
                   shared_file_storage_dir)
266 bb553e5a Bernardo Dal Seno
267 bb553e5a Bernardo Dal Seno
    buf = StringIO()
268 bb553e5a Bernardo Dal Seno
    buf.write("# List automatically generated from configuration by\n")
269 bb553e5a Bernardo Dal Seno
    buf.write("# cfgupgrade at %s\n" % time.asctime())
270 bb553e5a Bernardo Dal Seno
    if file_storage_dir:
271 bb553e5a Bernardo Dal Seno
      buf.write("%s\n" % file_storage_dir)
272 bb553e5a Bernardo Dal Seno
    if shared_file_storage_dir:
273 bb553e5a Bernardo Dal Seno
      buf.write("%s\n" % shared_file_storage_dir)
274 bb553e5a Bernardo Dal Seno
    utils.WriteFile(file_name=options.FILE_STORAGE_PATHS_FILE,
275 bb553e5a Bernardo Dal Seno
                    data=buf.getvalue(),
276 bb553e5a Bernardo Dal Seno
                    mode=0600,
277 bb553e5a Bernardo Dal Seno
                    dry_run=options.dry_run,
278 bb553e5a Bernardo Dal Seno
                    backup=True)
279 bb553e5a Bernardo Dal Seno
280 bb553e5a Bernardo Dal Seno
281 b555101c Thomas Thrainer
def GetNewNodeIndex(nodes_by_old_key, old_key, new_key_field):
282 b555101c Thomas Thrainer
  if old_key not in nodes_by_old_key:
283 b555101c Thomas Thrainer
    logging.warning("Can't find node '%s' in configuration, assuming that it's"
284 b555101c Thomas Thrainer
                    " already up-to-date", old_key)
285 b555101c Thomas Thrainer
    return old_key
286 b555101c Thomas Thrainer
  return nodes_by_old_key[old_key][new_key_field]
287 b555101c Thomas Thrainer
288 b555101c Thomas Thrainer
289 b555101c Thomas Thrainer
def ChangeNodeIndices(config_data, old_key_field, new_key_field):
290 b555101c Thomas Thrainer
  def ChangeDiskNodeIndices(disk):
291 b555101c Thomas Thrainer
    if disk["dev_type"] in constants.LDS_DRBD:
292 b555101c Thomas Thrainer
      for i in range(0, 2):
293 b555101c Thomas Thrainer
        disk["logical_id"][i] = GetNewNodeIndex(nodes_by_old_key,
294 b555101c Thomas Thrainer
                                                disk["logical_id"][i],
295 b555101c Thomas Thrainer
                                                new_key_field)
296 b555101c Thomas Thrainer
    if "children" in disk:
297 b555101c Thomas Thrainer
      for child in disk["children"]:
298 b555101c Thomas Thrainer
        ChangeDiskNodeIndices(child)
299 b555101c Thomas Thrainer
300 b555101c Thomas Thrainer
  nodes_by_old_key = {}
301 b555101c Thomas Thrainer
  nodes_by_new_key = {}
302 b555101c Thomas Thrainer
  for (_, node) in config_data["nodes"].items():
303 b555101c Thomas Thrainer
    nodes_by_old_key[node[old_key_field]] = node
304 b555101c Thomas Thrainer
    nodes_by_new_key[node[new_key_field]] = node
305 b555101c Thomas Thrainer
306 b555101c Thomas Thrainer
  config_data["nodes"] = nodes_by_new_key
307 b555101c Thomas Thrainer
308 b555101c Thomas Thrainer
  cluster = config_data["cluster"]
309 b555101c Thomas Thrainer
  cluster["master_node"] = GetNewNodeIndex(nodes_by_old_key,
310 b555101c Thomas Thrainer
                                           cluster["master_node"],
311 b555101c Thomas Thrainer
                                           new_key_field)
312 b555101c Thomas Thrainer
313 b555101c Thomas Thrainer
  for inst in config_data["instances"].values():
314 b555101c Thomas Thrainer
    inst["primary_node"] = GetNewNodeIndex(nodes_by_old_key,
315 b555101c Thomas Thrainer
                                           inst["primary_node"],
316 b555101c Thomas Thrainer
                                           new_key_field)
317 b555101c Thomas Thrainer
    for disk in inst["disks"]:
318 b555101c Thomas Thrainer
      ChangeDiskNodeIndices(disk)
319 b555101c Thomas Thrainer
320 b555101c Thomas Thrainer
321 b555101c Thomas Thrainer
def UpgradeNodeIndices(config_data):
322 b555101c Thomas Thrainer
  ChangeNodeIndices(config_data, "name", "uuid")
323 b555101c Thomas Thrainer
324 b555101c Thomas Thrainer
325 bb553e5a Bernardo Dal Seno
def UpgradeAll(config_data):
326 bb553e5a Bernardo Dal Seno
  config_data["version"] = constants.BuildVersion(TARGET_MAJOR,
327 bb553e5a Bernardo Dal Seno
                                                  TARGET_MINOR, 0)
328 bb553e5a Bernardo Dal Seno
  UpgradeRapiUsers()
329 bb553e5a Bernardo Dal Seno
  UpgradeWatcher()
330 bb553e5a Bernardo Dal Seno
  UpgradeFileStoragePaths(config_data)
331 bb553e5a Bernardo Dal Seno
  UpgradeNetworks(config_data)
332 0b94cda8 Bernardo Dal Seno
  UpgradeCluster(config_data)
333 bb553e5a Bernardo Dal Seno
  UpgradeGroups(config_data)
334 bb553e5a Bernardo Dal Seno
  UpgradeInstances(config_data)
335 b555101c Thomas Thrainer
  UpgradeNodeIndices(config_data)
336 bb553e5a Bernardo Dal Seno
337 f032d55c Dimitris Aragiorgis
338 30448dc7 Bernardo Dal Seno
def DowngradeDisks(disks, owner):
339 30448dc7 Bernardo Dal Seno
  for disk in disks:
340 30448dc7 Bernardo Dal Seno
    # Remove spindles to downgrade to 2.8
341 30448dc7 Bernardo Dal Seno
    if "spindles" in disk:
342 30448dc7 Bernardo Dal Seno
      logging.warning("Removing spindles (value=%s) from disk %s (%s) of"
343 30448dc7 Bernardo Dal Seno
                      " instance %s",
344 30448dc7 Bernardo Dal Seno
                      disk["spindles"], disk["iv_name"], disk["uuid"], owner)
345 30448dc7 Bernardo Dal Seno
      del disk["spindles"]
346 30448dc7 Bernardo Dal Seno
347 30448dc7 Bernardo Dal Seno
348 30448dc7 Bernardo Dal Seno
def DowngradeInstances(config_data):
349 30448dc7 Bernardo Dal Seno
  if "instances" not in config_data:
350 30448dc7 Bernardo Dal Seno
    raise Error("Cannot find the 'instances' key in the configuration!")
351 30448dc7 Bernardo Dal Seno
  for (iname, iobj) in config_data["instances"].items():
352 30448dc7 Bernardo Dal Seno
    if "disks" not in iobj:
353 30448dc7 Bernardo Dal Seno
      raise Error("Cannot find 'disks' key for instance %s" % iname)
354 30448dc7 Bernardo Dal Seno
    DowngradeDisks(iobj["disks"], iname)
355 30448dc7 Bernardo Dal Seno
356 30448dc7 Bernardo Dal Seno
357 b555101c Thomas Thrainer
def DowngradeNodeIndices(config_data):
358 b555101c Thomas Thrainer
  ChangeNodeIndices(config_data, "uuid", "name")
359 b555101c Thomas Thrainer
360 b555101c Thomas Thrainer
361 1709435e Bernardo Dal Seno
def DowngradeAll(config_data):
362 1709435e Bernardo Dal Seno
  # Any code specific to a particular version should be labeled that way, so
363 1709435e Bernardo Dal Seno
  # it can be removed when updating to the next version.
364 30448dc7 Bernardo Dal Seno
  DowngradeInstances(config_data)
365 b555101c Thomas Thrainer
  DowngradeNodeIndices(config_data)
366 1709435e Bernardo Dal Seno
367 1709435e Bernardo Dal Seno
368 6d691282 Michael Hanselmann
def main():
369 6d691282 Michael Hanselmann
  """Main program.
370 6d691282 Michael Hanselmann
371 6d691282 Michael Hanselmann
  """
372 b459a848 Andrea Spadaccini
  global options, args # pylint: disable=W0603
373 6d691282 Michael Hanselmann
374 0006af7d Michael Hanselmann
  # Option parsing
375 95e4a814 Michael Hanselmann
  parser = optparse.OptionParser(usage="%prog [--debug|--verbose] [--force]")
376 3ccb3a64 Michael Hanselmann
  parser.add_option("--dry-run", dest="dry_run",
377 60edf71e Michael Hanselmann
                    action="store_true",
378 f4bc1f2c Michael Hanselmann
                    help="Try to do the conversion, but don't write"
379 f4bc1f2c Michael Hanselmann
                         " output file")
380 f97c7901 Michael Hanselmann
  parser.add_option(cli.FORCE_OPT)
381 eda37a5a Michael Hanselmann
  parser.add_option(cli.DEBUG_OPT)
382 9cdb9578 Iustin Pop
  parser.add_option(cli.VERBOSE_OPT)
383 011974df Michael Hanselmann
  parser.add_option("--ignore-hostname", dest="ignore_hostname",
384 011974df Michael Hanselmann
                    action="store_true", default=False,
385 011974df Michael Hanselmann
                    help="Don't abort if hostname doesn't match")
386 3ccb3a64 Michael Hanselmann
  parser.add_option("--path", help="Convert configuration in this"
387 09bf5d24 Michael Hanselmann
                    " directory instead of '%s'" % pathutils.DATA_DIR,
388 09bf5d24 Michael Hanselmann
                    default=pathutils.DATA_DIR, dest="data_dir")
389 7939f60c Michael Hanselmann
  parser.add_option("--confdir",
390 7939f60c Michael Hanselmann
                    help=("Use this directory instead of '%s'" %
391 7939f60c Michael Hanselmann
                          pathutils.CONF_DIR),
392 7939f60c Michael Hanselmann
                    default=pathutils.CONF_DIR, dest="conf_dir")
393 02e1292d Michael Hanselmann
  parser.add_option("--no-verify",
394 02e1292d Michael Hanselmann
                    help="Do not verify configuration after upgrade",
395 02e1292d Michael Hanselmann
                    action="store_true", dest="no_verify", default=False)
396 1709435e Bernardo Dal Seno
  parser.add_option("--downgrade",
397 1709435e Bernardo Dal Seno
                    help="Downgrade to the previous stable version",
398 1709435e Bernardo Dal Seno
                    action="store_true", dest="downgrade", default=False)
399 0006af7d Michael Hanselmann
  (options, args) = parser.parse_args()
400 0006af7d Michael Hanselmann
401 ac4d25b6 Iustin Pop
  # We need to keep filenames locally because they might be renamed between
402 ac4d25b6 Iustin Pop
  # versions.
403 0cddd44d Iustin Pop
  options.data_dir = os.path.abspath(options.data_dir)
404 ac4d25b6 Iustin Pop
  options.CONFIG_DATA_PATH = options.data_dir + "/config.data"
405 ac4d25b6 Iustin Pop
  options.SERVER_PEM_PATH = options.data_dir + "/server.pem"
406 ac4d25b6 Iustin Pop
  options.KNOWN_HOSTS_PATH = options.data_dir + "/known_hosts"
407 ac4d25b6 Iustin Pop
  options.RAPI_CERT_FILE = options.data_dir + "/rapi.pem"
408 b6267745 Andrea Spadaccini
  options.SPICE_CERT_FILE = options.data_dir + "/spice.pem"
409 b6267745 Andrea Spadaccini
  options.SPICE_CACERT_FILE = options.data_dir + "/spice-ca.pem"
410 fdd9ac5b Michael Hanselmann
  options.RAPI_USERS_FILE = options.data_dir + "/rapi/users"
411 fdd9ac5b Michael Hanselmann
  options.RAPI_USERS_FILE_PRE24 = options.data_dir + "/rapi_users"
412 6b7d5878 Michael Hanselmann
  options.CONFD_HMAC_KEY = options.data_dir + "/hmac.key"
413 fc0726b9 Michael Hanselmann
  options.CDS_FILE = options.data_dir + "/cluster-domain-secret"
414 011974df Michael Hanselmann
  options.SSCONF_MASTER_NODE = options.data_dir + "/ssconf_master_node"
415 a292020f Michael Hanselmann
  options.WATCHER_STATEFILE = options.data_dir + "/watcher.data"
416 7939f60c Michael Hanselmann
  options.FILE_STORAGE_PATHS_FILE = options.conf_dir + "/file-storage-paths"
417 ac4d25b6 Iustin Pop
418 eda37a5a Michael Hanselmann
  SetupLogging()
419 eda37a5a Michael Hanselmann
420 0006af7d Michael Hanselmann
  # Option checking
421 0006af7d Michael Hanselmann
  if args:
422 95e4a814 Michael Hanselmann
    raise Error("No arguments expected")
423 1709435e Bernardo Dal Seno
  if options.downgrade and not options.no_verify:
424 1709435e Bernardo Dal Seno
    options.no_verify = True
425 0006af7d Michael Hanselmann
426 011974df Michael Hanselmann
  # Check master name
427 011974df Michael Hanselmann
  if not (CheckHostname(options.SSCONF_MASTER_NODE) or options.ignore_hostname):
428 011974df Michael Hanselmann
    logging.error("Aborting due to hostname mismatch")
429 011974df Michael Hanselmann
    sys.exit(constants.EXIT_FAILURE)
430 011974df Michael Hanselmann
431 319856a9 Michael Hanselmann
  if not options.force:
432 1709435e Bernardo Dal Seno
    if options.downgrade:
433 1709435e Bernardo Dal Seno
      usertext = ("The configuration is going to be DOWNGRADED to version %s.%s"
434 1709435e Bernardo Dal Seno
                  " Some configuration data might be removed if they don't fit"
435 1709435e Bernardo Dal Seno
                  " in the old format. Please make sure you have read the"
436 1709435e Bernardo Dal Seno
                  " upgrade notes (available in the UPGRADE file and included"
437 1709435e Bernardo Dal Seno
                  " in other documentation formats) to understand what they"
438 1709435e Bernardo Dal Seno
                  " are. Continue with *DOWNGRADING* the configuration?" %
439 1709435e Bernardo Dal Seno
                  (DOWNGRADE_MAJOR, DOWNGRADE_MINOR))
440 1709435e Bernardo Dal Seno
    else:
441 1709435e Bernardo Dal Seno
      usertext = ("Please make sure you have read the upgrade notes for"
442 1709435e Bernardo Dal Seno
                  " Ganeti %s (available in the UPGRADE file and included"
443 1709435e Bernardo Dal Seno
                  " in other documentation formats). Continue with upgrading"
444 1709435e Bernardo Dal Seno
                  " configuration?" % constants.RELEASE_VERSION)
445 f97c7901 Michael Hanselmann
    if not cli.AskUser(usertext):
446 a9221f09 Michael Hanselmann
      sys.exit(constants.EXIT_FAILURE)
447 319856a9 Michael Hanselmann
448 95e4a814 Michael Hanselmann
  # Check whether it's a Ganeti configuration directory
449 ac4d25b6 Iustin Pop
  if not (os.path.isfile(options.CONFIG_DATA_PATH) and
450 30acff6c Michael Hanselmann
          os.path.isfile(options.SERVER_PEM_PATH) and
451 ac4d25b6 Iustin Pop
          os.path.isfile(options.KNOWN_HOSTS_PATH)):
452 a9221f09 Michael Hanselmann
    raise Error(("%s does not seem to be a Ganeti configuration"
453 ac4d25b6 Iustin Pop
                 " directory") % options.data_dir)
454 95e4a814 Michael Hanselmann
455 7939f60c Michael Hanselmann
  if not os.path.isdir(options.conf_dir):
456 7939f60c Michael Hanselmann
    raise Error("Not a directory: %s" % options.conf_dir)
457 7939f60c Michael Hanselmann
458 11c31f5c Michael Hanselmann
  config_data = serializer.LoadJson(utils.ReadFile(options.CONFIG_DATA_PATH))
459 a421fdeb Iustin Pop
460 11c31f5c Michael Hanselmann
  try:
461 11c31f5c Michael Hanselmann
    config_version = config_data["version"]
462 11c31f5c Michael Hanselmann
  except KeyError:
463 11c31f5c Michael Hanselmann
    raise Error("Unable to determine configuration version")
464 0006af7d Michael Hanselmann
465 11c31f5c Michael Hanselmann
  (config_major, config_minor, config_revision) = \
466 11c31f5c Michael Hanselmann
    constants.SplitVersion(config_version)
467 319856a9 Michael Hanselmann
468 11c31f5c Michael Hanselmann
  logging.info("Found configuration version %s (%d.%d.%d)",
469 11c31f5c Michael Hanselmann
               config_version, config_major, config_minor, config_revision)
470 319856a9 Michael Hanselmann
471 11c31f5c Michael Hanselmann
  if "config_version" in config_data["cluster"]:
472 11c31f5c Michael Hanselmann
    raise Error("Inconsistent configuration: found config_version in"
473 11c31f5c Michael Hanselmann
                " configuration file")
474 95e4a814 Michael Hanselmann
475 1709435e Bernardo Dal Seno
  # Downgrade to the previous stable version
476 1709435e Bernardo Dal Seno
  if options.downgrade:
477 1709435e Bernardo Dal Seno
    if config_major != TARGET_MAJOR or config_minor != TARGET_MINOR:
478 1709435e Bernardo Dal Seno
      raise Error("Downgrade supported only from the latest version (%s.%s),"
479 1709435e Bernardo Dal Seno
                  " found %s (%s.%s.%s) instead" %
480 1709435e Bernardo Dal Seno
                  (TARGET_MAJOR, TARGET_MINOR, config_version, config_major,
481 1709435e Bernardo Dal Seno
                   config_minor, config_revision))
482 1709435e Bernardo Dal Seno
    DowngradeAll(config_data)
483 1709435e Bernardo Dal Seno
484 0b94cda8 Bernardo Dal Seno
  # Upgrade from 2.{0..7} to 2.7
485 0b94cda8 Bernardo Dal Seno
  elif config_major == 2 and config_minor in range(0, 8):
486 aeb0c953 Michael Hanselmann
    if config_revision != 0:
487 a9221f09 Michael Hanselmann
      logging.warning("Config revision is %s, not 0", config_revision)
488 bb553e5a Bernardo Dal Seno
    UpgradeAll(config_data)
489 904910c4 Iustin Pop
490 93fd9bb1 Iustin Pop
  elif config_major == TARGET_MAJOR and config_minor == TARGET_MINOR:
491 a9221f09 Michael Hanselmann
    logging.info("No changes necessary")
492 a9221f09 Michael Hanselmann
493 a9221f09 Michael Hanselmann
  else:
494 a9221f09 Michael Hanselmann
    raise Error("Configuration version %d.%d.%d not supported by this tool" %
495 a9221f09 Michael Hanselmann
                (config_major, config_minor, config_revision))
496 aeb0c953 Michael Hanselmann
497 11c31f5c Michael Hanselmann
  try:
498 11c31f5c Michael Hanselmann
    logging.info("Writing configuration file to %s", options.CONFIG_DATA_PATH)
499 11c31f5c Michael Hanselmann
    utils.WriteFile(file_name=options.CONFIG_DATA_PATH,
500 11c31f5c Michael Hanselmann
                    data=serializer.DumpJson(config_data),
501 11c31f5c Michael Hanselmann
                    mode=0600,
502 11c31f5c Michael Hanselmann
                    dry_run=options.dry_run,
503 11c31f5c Michael Hanselmann
                    backup=True)
504 a421fdeb Iustin Pop
505 a421fdeb Iustin Pop
    if not options.dry_run:
506 5ae4945a Iustin Pop
      bootstrap.GenerateClusterCrypto(
507 5ae4945a Iustin Pop
        False, False, False, False, False,
508 5ae4945a Iustin Pop
        nodecert_file=options.SERVER_PEM_PATH,
509 5ae4945a Iustin Pop
        rapicert_file=options.RAPI_CERT_FILE,
510 5ae4945a Iustin Pop
        spicecert_file=options.SPICE_CERT_FILE,
511 5ae4945a Iustin Pop
        spicecacert_file=options.SPICE_CACERT_FILE,
512 5ae4945a Iustin Pop
        hmackey_file=options.CONFD_HMAC_KEY,
513 5ae4945a Iustin Pop
        cds_file=options.CDS_FILE)
514 aeb0c953 Michael Hanselmann
515 a9221f09 Michael Hanselmann
  except Exception:
516 11c31f5c Michael Hanselmann
    logging.critical("Writing configuration failed. It is probably in an"
517 95e4a814 Michael Hanselmann
                     " inconsistent state and needs manual intervention.")
518 95e4a814 Michael Hanselmann
    raise
519 0006af7d Michael Hanselmann
520 ac4d25b6 Iustin Pop
  # test loading the config file
521 fdb85e3d Bernardo Dal Seno
  all_ok = True
522 02e1292d Michael Hanselmann
  if not (options.dry_run or options.no_verify):
523 ac4d25b6 Iustin Pop
    logging.info("Testing the new config file...")
524 ac4d25b6 Iustin Pop
    cfg = config.ConfigWriter(cfg_file=options.CONFIG_DATA_PATH,
525 959b6fe5 Apollon Oikonomopoulos
                              accept_foreign=options.ignore_hostname,
526 ac4d25b6 Iustin Pop
                              offline=True)
527 ac4d25b6 Iustin Pop
    # if we reached this, it's all fine
528 ac4d25b6 Iustin Pop
    vrfy = cfg.VerifyConfig()
529 ac4d25b6 Iustin Pop
    if vrfy:
530 ac4d25b6 Iustin Pop
      logging.error("Errors after conversion:")
531 ac4d25b6 Iustin Pop
      for item in vrfy:
532 07b8a2b5 Iustin Pop
        logging.error(" - %s", item)
533 fdb85e3d Bernardo Dal Seno
      all_ok = False
534 fdb85e3d Bernardo Dal Seno
    else:
535 fdb85e3d Bernardo Dal Seno
      logging.info("File loaded successfully after upgrading")
536 ac4d25b6 Iustin Pop
    del cfg
537 ac4d25b6 Iustin Pop
538 1709435e Bernardo Dal Seno
  if options.downgrade:
539 1709435e Bernardo Dal Seno
    action = "downgraded"
540 1709435e Bernardo Dal Seno
    out_ver = "%s.%s" % (DOWNGRADE_MAJOR, DOWNGRADE_MINOR)
541 1709435e Bernardo Dal Seno
  else:
542 1709435e Bernardo Dal Seno
    action = "upgraded"
543 1709435e Bernardo Dal Seno
    out_ver = constants.RELEASE_VERSION
544 fdb85e3d Bernardo Dal Seno
  if all_ok:
545 1709435e Bernardo Dal Seno
    cli.ToStderr("Configuration successfully %s to version %s.",
546 1709435e Bernardo Dal Seno
                 action, out_ver)
547 fdb85e3d Bernardo Dal Seno
  else:
548 1709435e Bernardo Dal Seno
    cli.ToStderr("Configuration %s to version %s, but there are errors."
549 1709435e Bernardo Dal Seno
                 "\nPlease review the file.", action, out_ver)
550 66a66fa7 Michael Hanselmann
551 6d691282 Michael Hanselmann
552 6d691282 Michael Hanselmann
if __name__ == "__main__":
553 6d691282 Michael Hanselmann
  main()