Package ganeti :: Package utils :: Module version
[hide private]
[frames] | no frames]

Source Code for Module ganeti.utils.version

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2013 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30   
 31  """Version utilities.""" 
 32   
 33  import re 
 34   
 35  from ganeti import constants 
 36   
 37  _FULL_VERSION_RE = re.compile(r"(\d+)\.(\d+)\.(\d+)") 
 38  _SHORT_VERSION_RE = re.compile(r"(\d+)\.(\d+)") 
 39   
 40  # The first Ganeti version that supports automatic upgrades 
 41  FIRST_UPGRADE_VERSION = (2, 10, 0) 
 42   
 43  CURRENT_VERSION = (constants.VERSION_MAJOR, constants.VERSION_MINOR, 
 44                     constants.VERSION_REVISION) 
 45   
 46  # Format for CONFIG_VERSION: 
 47  #   01 03 0123 = 01030123 
 48  #   ^^ ^^ ^^^^ 
 49  #   |  |  + Configuration version/revision 
 50  #   |  + Minor version 
 51  #   + Major version 
 52  # 
 53  # It is stored as an integer. Make sure not to write an octal number. 
 54   
 55  # BuildVersion and SplitVersion must be in here because we can't import other 
 56  # modules. The cfgupgrade tool must be able to read and write version numbers 
 57  # and thus requires these functions. To avoid code duplication, they're kept in 
 58  # here. 
 59   
 60   
61 -def BuildVersion(major, minor, revision):
62 """Calculates int version number from major, minor and revision numbers. 63 64 Returns: int representing version number 65 66 """ 67 assert isinstance(major, int) 68 assert isinstance(minor, int) 69 assert isinstance(revision, int) 70 return (1000000 * major + 71 10000 * minor + 72 1 * revision)
73 74
75 -def SplitVersion(version):
76 """Splits version number stored in an int. 77 78 Returns: tuple; (major, minor, revision) 79 80 """ 81 assert isinstance(version, int) 82 83 (major, remainder) = divmod(version, 1000000) 84 (minor, revision) = divmod(remainder, 10000) 85 86 return (major, minor, revision)
87 88
89 -def ParseVersion(versionstring):
90 """Parses a version string. 91 92 @param versionstring: the version string to parse 93 @type versionstring: string 94 @rtype: tuple or None 95 @return: (major, minor, revision) if parsable, None otherwise. 96 97 """ 98 m = _FULL_VERSION_RE.match(versionstring) 99 if m is not None: 100 return (int(m.group(1)), int(m.group(2)), int(m.group(3))) 101 102 m = _SHORT_VERSION_RE.match(versionstring) 103 if m is not None: 104 return (int(m.group(1)), int(m.group(2)), 0) 105 106 return None
107 108
109 -def UpgradeRange(target, current=CURRENT_VERSION):
110 """Verify whether a version is within the range of automatic upgrades. 111 112 @param target: The version to upgrade to as (major, minor, revision) 113 @type target: tuple 114 @param current: The version to upgrade from as (major, minor, revision) 115 @type current: tuple 116 @rtype: string or None 117 @return: None, if within the range, and a human-readable error message 118 otherwise 119 120 """ 121 if target < FIRST_UPGRADE_VERSION or current < FIRST_UPGRADE_VERSION: 122 return "automatic upgrades only supported from 2.10 onwards" 123 124 if target[0] != current[0]: 125 return "different major versions" 126 127 if target[1] < current[1] - 1: 128 return "can only downgrade one minor version at a time" 129 130 return None
131 132
133 -def ShouldCfgdowngrade(version, current=CURRENT_VERSION):
134 """Decide whether cfgupgrade --downgrade should be called. 135 136 Given the current version and the version to change to, decide 137 if in the transition process cfgupgrade --downgrade should 138 be called 139 140 @param version: The version to upgrade to as (major, minor, revision) 141 @type version: tuple 142 @param current: The version to upgrade from as (major, minor, revision) 143 @type current: tuple 144 @rtype: bool 145 @return: True, if cfgupgrade --downgrade should be called. 146 147 """ 148 return version[0] == current[0] and version[1] == current[1] - 1
149 150
151 -def IsCorrectConfigVersion(targetversion, configversion):
152 """Decide whether configuration version is compatible with the target. 153 154 @param targetversion: The version to upgrade to as (major, minor, revision) 155 @type targetversion: tuple 156 @param configversion: The version of the current configuration 157 @type configversion: tuple 158 @rtype: bool 159 @return: True, if the configversion fits with the target version. 160 161 """ 162 return (configversion[0] == targetversion[0] and 163 configversion[1] == targetversion[1])
164 165
166 -def IsBefore(version, major, minor, revision):
167 """Decide if a given version is strictly before a given version. 168 169 @param version: (major, minor, revision) or None, with None being 170 before all versions 171 @type version: (int, int, int) or None 172 @param major: major version 173 @type major: int 174 @param minor: minor version 175 @type minor: int 176 @param revision: revision 177 @type revision: int 178 179 """ 180 if version is None: 181 return True 182 183 return version < (major, minor, revision)
184 185
186 -def IsEqual(version, major, minor, revision):
187 """Decide if a given version matches the given version. 188 189 If the revision is set to None, only major and minor are compared. 190 191 @param version: (major, minor, revision) or None, with None being 192 before all versions 193 @type version: (int, int, int) or None 194 @param major: major version 195 @type major: int 196 @param minor: minor version 197 @type minor: int 198 @param revision: revision 199 @type revision: int 200 201 """ 202 if version is None: 203 return False 204 205 if revision is None: 206 current_major, current_minor, _ = version 207 return (current_major, current_minor) == (major, minor) 208 209 return version == (major, minor, revision)
210